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>
71 #include <ipc/ipc_entry.h>
72 #include <ipc/ipc_object.h>
73 #include <ipc/ipc_notify.h>
74 #include <ipc/ipc_space.h>
75 #include <ipc/ipc_pset.h>
76 #include <ipc/ipc_machdep.h>
77 #include <kern/counters.h>
78 #include <kern/ipc_tt.h>
79 #include <kern/task.h>
80 #include <kern/thread.h>
81 #include <kern/processor.h>
82 #include <kern/sched.h>
83 #include <kern/sched_prim.h>
84 #include <kern/host.h>
85 #include <kern/misc_protos.h>
86 #include <security/mac_mach_internal.h>
88 #include <pexpert/pexpert.h>
90 extern int panic_on_exception_triage
;
92 unsigned long c_thr_exc_raise
= 0;
93 unsigned long c_thr_exc_raise_state
= 0;
94 unsigned long c_thr_exc_raise_state_id
= 0;
95 unsigned long c_tsk_exc_raise
= 0;
96 unsigned long c_tsk_exc_raise_state
= 0;
97 unsigned long c_tsk_exc_raise_state_id
= 0;
99 /* forward declarations */
100 kern_return_t
exception_deliver(
102 exception_type_t exception
,
103 mach_exception_data_t code
,
104 mach_msg_type_number_t codeCnt
,
105 struct exception_action
*excp
,
109 check_exc_receiver_dependency(
110 exception_type_t exception
,
111 struct exception_action
*excp
,
115 kern_return_t
bsd_exception(
116 exception_type_t exception
,
117 mach_exception_data_t code
,
118 mach_msg_type_number_t codeCnt
);
119 #endif /* MACH_BSD */
122 * Routine: exception_deliver
124 * Make an upcall to the exception server provided.
126 * Nothing locked and no resources held.
127 * Called from an exception context, so
128 * thread_exception_return and thread_kdb_return
131 * KERN_SUCCESS if the exception was handled
136 exception_type_t exception
,
137 mach_exception_data_t code
,
138 mach_msg_type_number_t codeCnt
,
139 struct exception_action
*excp
,
142 ipc_port_t exc_port
= IPC_PORT_NULL
;
143 exception_data_type_t small_code
[EXCEPTION_CODE_MAX
];
149 ipc_port_t thread_port
= IPC_PORT_NULL
, task_port
= IPC_PORT_NULL
;
152 * Save work if we are terminating.
153 * Just go back to our AST handler.
155 if (!thread
->active
&& !thread
->inspection
)
159 * If there are no exception actions defined for this entity,
160 * we can't deliver here.
165 assert(exception
< EXC_TYPES_COUNT
);
166 if (exception
>= EXC_TYPES_COUNT
)
169 excp
= &excp
[exception
];
172 * Snapshot the exception action data under lock for consistency.
173 * Hold a reference to the port over the exception_raise_* calls
174 * so it can't be destroyed. This seems like overkill, but keeps
175 * the port from disappearing between now and when
176 * ipc_object_copyin_from_kernel is finally called.
179 exc_port
= excp
->port
;
180 if (!IP_VALID(exc_port
)) {
181 lck_mtx_unlock(mutex
);
185 if (!ip_active(exc_port
)) {
187 lck_mtx_unlock(mutex
);
190 ip_reference(exc_port
);
191 exc_port
->ip_srights
++;
194 flavor
= excp
->flavor
;
195 behavior
= excp
->behavior
;
196 lck_mtx_unlock(mutex
);
198 code64
= (behavior
& MACH_EXCEPTION_CODES
);
199 behavior
&= ~MACH_EXCEPTION_CODES
;
202 small_code
[0] = CAST_DOWN_EXPLICIT(exception_data_type_t
, code
[0]);
203 small_code
[1] = CAST_DOWN_EXPLICIT(exception_data_type_t
, code
[1]);
209 /* Now is a reasonably good time to check if the exception action is
210 * permitted for this process, because after this point we will send
211 * the message out almost certainly.
212 * As with other failures, exception_triage_thread will go on
215 if (mac_exc_action_check_exception_send(task
, excp
) != 0) {
217 goto out_release_right
;
221 if (behavior
!= EXCEPTION_STATE
) {
222 if (thread
!= current_thread() || exception
== EXC_CORPSE_NOTIFY
) {
224 task_reference(task
);
225 task_port
= convert_task_to_port(task
);
226 /* task ref consumed */
227 thread_reference(thread
);
228 thread_port
= convert_thread_to_port(thread
);
229 /* thread ref consumed */
232 task_port
= retrieve_task_self_fast(thread
->task
);
233 thread_port
= retrieve_thread_self_fast(thread
);
238 case EXCEPTION_STATE
: {
239 mach_msg_type_number_t state_cnt
;
240 thread_state_data_t state
;
242 c_thr_exc_raise_state
++;
243 state_cnt
= _MachineStateCount
[flavor
];
244 kr
= thread_getstatus(thread
, flavor
,
245 (thread_state_t
)state
,
247 if (kr
== KERN_SUCCESS
) {
249 kr
= mach_exception_raise_state(exc_port
,
257 kr
= exception_raise_state(exc_port
, exception
,
264 if (kr
== KERN_SUCCESS
) {
265 if (exception
!= EXC_CORPSE_NOTIFY
)
266 kr
= thread_setstatus(thread
, flavor
,
267 (thread_state_t
)state
,
269 goto out_release_right
;
274 goto out_release_right
;
277 case EXCEPTION_DEFAULT
:
280 kr
= mach_exception_raise(exc_port
,
287 kr
= exception_raise(exc_port
,
295 goto out_release_right
;
297 case EXCEPTION_STATE_IDENTITY
: {
298 mach_msg_type_number_t state_cnt
;
299 thread_state_data_t state
;
301 c_thr_exc_raise_state_id
++;
302 state_cnt
= _MachineStateCount
[flavor
];
303 kr
= thread_getstatus(thread
, flavor
,
304 (thread_state_t
)state
,
306 if (kr
== KERN_SUCCESS
) {
308 kr
= mach_exception_raise_state_identity(
319 kr
= exception_raise_state_identity(exc_port
,
330 if (kr
== KERN_SUCCESS
) {
331 if (exception
!= EXC_CORPSE_NOTIFY
)
332 kr
= thread_setstatus(thread
, flavor
,
333 (thread_state_t
)state
,
335 goto out_release_right
;
340 goto out_release_right
;
344 panic ("bad exception behavior!");
351 ipc_port_release_send(task_port
);
355 ipc_port_release_send(thread_port
);
359 ipc_port_release_send(exc_port
);
366 * Routine: check_exc_receiver_dependency
368 * Verify that the port destined for receiving this exception is not
369 * on the current task. This would cause hang in kernel for
370 * EXC_CRASH primarily. Note: If port is transferred
371 * between check and delivery then deadlock may happen.
374 * Nothing locked and no resources held.
375 * Called from an exception context.
377 * KERN_SUCCESS if its ok to send exception message.
380 check_exc_receiver_dependency(
381 exception_type_t exception
,
382 struct exception_action
*excp
,
385 kern_return_t retval
= KERN_SUCCESS
;
387 if (excp
== NULL
|| exception
!= EXC_CRASH
)
390 task_t task
= current_task();
392 ipc_port_t xport
= excp
[exception
].port
;
395 && task
->itk_space
== xport
->ip_receiver
)
396 retval
= KERN_FAILURE
;
397 lck_mtx_unlock(mutex
);
403 * Routine: exception_triage_thread
405 * The thread caught an exception.
406 * We make an up-call to the thread's exception server.
408 * Nothing locked and no resources held.
409 * Called from an exception context, so
410 * thread_exception_return and thread_kdb_return
413 * KERN_SUCCESS if exception is handled by any of the handlers.
416 exception_triage_thread(
417 exception_type_t exception
,
418 mach_exception_data_t code
,
419 mach_msg_type_number_t codeCnt
,
423 host_priv_t host_priv
;
425 kern_return_t kr
= KERN_FAILURE
;
427 assert(exception
!= EXC_RPC_ALERT
);
430 * If this behavior has been requested by the the kernel
431 * (due to the boot environment), we should panic if we
432 * enter this function. This is intended as a debugging
433 * aid; it should allow us to debug why we caught an
434 * exception in environments where debugging is especially
437 if (panic_on_exception_triage
) {
438 panic("called exception_triage when it was forbidden by the boot environment");
442 * Try to raise the exception at the activation level.
444 mutex
= &thread
->mutex
;
445 if (KERN_SUCCESS
== check_exc_receiver_dependency(exception
, thread
->exc_actions
, mutex
))
447 kr
= exception_deliver(thread
, exception
, code
, codeCnt
, thread
->exc_actions
, mutex
);
448 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
453 * Maybe the task level will handle it.
456 mutex
= &task
->itk_lock_data
;
457 if (KERN_SUCCESS
== check_exc_receiver_dependency(exception
, task
->exc_actions
, mutex
))
459 kr
= exception_deliver(thread
, exception
, code
, codeCnt
, task
->exc_actions
, mutex
);
460 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
465 * How about at the host level?
467 host_priv
= host_priv_self();
468 mutex
= &host_priv
->lock
;
470 if (KERN_SUCCESS
== check_exc_receiver_dependency(exception
, host_priv
->exc_actions
, mutex
))
472 kr
= exception_deliver(thread
, exception
, code
, codeCnt
, host_priv
->exc_actions
, mutex
);
473 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
478 if ((exception
!= EXC_CRASH
) && (exception
!= EXC_RESOURCE
) &&
479 (exception
!= EXC_GUARD
) && (exception
!= EXC_CORPSE_NOTIFY
))
480 thread_exception_return();
485 * Routine: exception_triage
487 * The current thread caught an exception.
488 * We make an up-call to the thread's exception server.
490 * Nothing locked and no resources held.
491 * Called from an exception context, so
492 * thread_exception_return and thread_kdb_return
495 * KERN_SUCCESS if exception is handled by any of the handlers.
499 exception_type_t exception
,
500 mach_exception_data_t code
,
501 mach_msg_type_number_t codeCnt
)
503 thread_t thread
= current_thread();
504 return exception_triage_thread(exception
, code
, codeCnt
, thread
);
509 exception_type_t exception
,
510 mach_exception_data_t code
,
511 mach_msg_type_number_t codeCnt
)
515 thread_t self
= current_thread();
519 * Maybe the task level will handle it.
521 task
= current_task();
522 mutex
= &task
->itk_lock_data
;
524 kr
= exception_deliver(self
, exception
, code
, codeCnt
, task
->exc_actions
, mutex
);
526 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
527 return(KERN_SUCCESS
);
528 return(KERN_FAILURE
);
533 * Raise an exception on a task.
534 * This should tell launchd to launch Crash Reporter for this task.
536 kern_return_t
task_exception_notify(exception_type_t exception
,
537 mach_exception_data_type_t exccode
, mach_exception_data_type_t excsubcode
)
539 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
540 wait_interrupt_t wsave
;
541 kern_return_t kr
= KERN_SUCCESS
;
544 code
[1] = excsubcode
;
546 wsave
= thread_interrupt_level(THREAD_UNINT
);
547 kr
= exception_triage(exception
, code
, EXCEPTION_CODE_MAX
);
548 (void) thread_interrupt_level(wsave
);
554 * Handle interface for special performance monitoring
555 * This is a special case of the host exception handler
557 kern_return_t
sys_perf_notify(thread_t thread
, int pid
)
561 wait_interrupt_t wsave
;
564 hostp
= host_priv_self(); /* Get the host privileged ports */
565 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
566 code
[0] = 0xFF000001; /* Set terminate code */
567 code
[1] = pid
; /* Pass out the pid */
569 struct task
*task
= thread
->task
;
570 xport
= hostp
->exc_actions
[EXC_RPC_ALERT
].port
;
572 /* Make sure we're not catching our own exception */
573 if (!IP_VALID(xport
) ||
575 task
->itk_space
== xport
->data
.receiver
) {
577 return(KERN_FAILURE
);
580 wsave
= thread_interrupt_level(THREAD_UNINT
);
581 ret
= exception_deliver(
588 (void)thread_interrupt_level(wsave
);