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
,
143 exception_data_type_t small_code
[EXCEPTION_CODE_MAX
];
148 int use_fast_retrieve
= TRUE
;
150 ipc_port_t thread_port
= NULL
, task_port
= NULL
;
153 * Save work if we are terminating.
154 * Just go back to our AST handler.
156 if (!thread
->active
&& !thread
->inspection
)
160 * If there are no exception actions defined for this entity,
161 * we can't deliver here.
166 assert(exception
< EXC_TYPES_COUNT
);
167 if (exception
>= EXC_TYPES_COUNT
)
170 excp
= &excp
[exception
];
173 * Snapshot the exception action data under lock for consistency.
174 * Hold a reference to the port over the exception_raise_* calls
175 * so it can't be destroyed. This seems like overkill, but keeps
176 * the port from disappearing between now and when
177 * ipc_object_copyin_from_kernel is finally called.
180 exc_port
= excp
->port
;
181 if (!IP_VALID(exc_port
)) {
182 lck_mtx_unlock(mutex
);
186 if (!ip_active(exc_port
)) {
188 lck_mtx_unlock(mutex
);
191 ip_reference(exc_port
);
192 exc_port
->ip_srights
++;
195 flavor
= excp
->flavor
;
196 behavior
= excp
->behavior
;
197 lck_mtx_unlock(mutex
);
199 code64
= (behavior
& MACH_EXCEPTION_CODES
);
200 behavior
&= ~MACH_EXCEPTION_CODES
;
203 small_code
[0] = CAST_DOWN_EXPLICIT(exception_data_type_t
, code
[0]);
204 small_code
[1] = CAST_DOWN_EXPLICIT(exception_data_type_t
, code
[1]);
210 /* Now is a reasonably good time to check if the exception action is
211 * permitted for this process, because after this point we will send
212 * the message out almost certainly.
213 * As with other failures, exception_triage_thread will go on
216 if (mac_exc_action_check_exception_send(task
, excp
) != 0) {
221 if ((thread
!= current_thread() || exception
== EXC_CORPSE_NOTIFY
)
222 && behavior
!= EXCEPTION_STATE
) {
223 use_fast_retrieve
= FALSE
;
225 task_reference(task
);
226 task_port
= convert_task_to_port(task
);
227 /* task ref consumed */
228 thread_reference(thread
);
229 thread_port
= convert_thread_to_port(thread
);
230 /* thread ref consumed */
235 case EXCEPTION_STATE
: {
236 mach_msg_type_number_t state_cnt
;
237 thread_state_data_t state
;
239 c_thr_exc_raise_state
++;
240 state_cnt
= _MachineStateCount
[flavor
];
241 kr
= thread_getstatus(thread
, flavor
,
242 (thread_state_t
)state
,
244 if (kr
== KERN_SUCCESS
) {
246 kr
= mach_exception_raise_state(exc_port
,
254 kr
= exception_raise_state(exc_port
, exception
,
261 if (kr
== MACH_MSG_SUCCESS
&& exception
!= EXC_CORPSE_NOTIFY
)
262 kr
= thread_setstatus(thread
, flavor
,
263 (thread_state_t
)state
,
270 case EXCEPTION_DEFAULT
:
273 kr
= mach_exception_raise(exc_port
,
274 use_fast_retrieve
? retrieve_thread_self_fast(thread
) :
276 use_fast_retrieve
? retrieve_task_self_fast(thread
->task
) :
282 kr
= exception_raise(exc_port
,
283 use_fast_retrieve
? retrieve_thread_self_fast(thread
) :
285 use_fast_retrieve
? retrieve_task_self_fast(thread
->task
) :
294 case EXCEPTION_STATE_IDENTITY
: {
295 mach_msg_type_number_t state_cnt
;
296 thread_state_data_t state
;
298 c_thr_exc_raise_state_id
++;
299 state_cnt
= _MachineStateCount
[flavor
];
300 kr
= thread_getstatus(thread
, flavor
,
301 (thread_state_t
)state
,
303 if (kr
== KERN_SUCCESS
) {
305 kr
= mach_exception_raise_state_identity(
307 use_fast_retrieve
? retrieve_thread_self_fast(thread
) :
309 use_fast_retrieve
? retrieve_task_self_fast(thread
->task
) :
318 kr
= exception_raise_state_identity(exc_port
,
319 use_fast_retrieve
? retrieve_thread_self_fast(thread
) :
321 use_fast_retrieve
? retrieve_task_self_fast(thread
->task
) :
330 if (kr
== MACH_MSG_SUCCESS
&& exception
!= EXC_CORPSE_NOTIFY
)
331 kr
= thread_setstatus(thread
, flavor
,
332 (thread_state_t
)state
,
340 panic ("bad exception behavior!");
346 * Routine: check_exc_receiver_dependency
348 * Verify that the port destined for receiving this exception is not
349 * on the current task. This would cause hang in kernel for
350 * EXC_CRASH primarily. Note: If port is transferred
351 * between check and delivery then deadlock may happen.
354 * Nothing locked and no resources held.
355 * Called from an exception context.
357 * KERN_SUCCESS if its ok to send exception message.
360 check_exc_receiver_dependency(
361 exception_type_t exception
,
362 struct exception_action
*excp
,
365 kern_return_t retval
= KERN_SUCCESS
;
367 if (excp
== NULL
|| exception
!= EXC_CRASH
)
370 task_t task
= current_task();
372 ipc_port_t xport
= excp
[exception
].port
;
375 && task
->itk_space
== xport
->ip_receiver
)
376 retval
= KERN_FAILURE
;
377 lck_mtx_unlock(mutex
);
383 * Routine: exception_triage_thread
385 * The thread caught an exception.
386 * We make an up-call to the thread's exception server.
388 * Nothing locked and no resources held.
389 * Called from an exception context, so
390 * thread_exception_return and thread_kdb_return
393 * KERN_SUCCESS if exception is handled by any of the handlers.
396 exception_triage_thread(
397 exception_type_t exception
,
398 mach_exception_data_t code
,
399 mach_msg_type_number_t codeCnt
,
403 host_priv_t host_priv
;
405 kern_return_t kr
= KERN_FAILURE
;
407 assert(exception
!= EXC_RPC_ALERT
);
410 * If this behavior has been requested by the the kernel
411 * (due to the boot environment), we should panic if we
412 * enter this function. This is intended as a debugging
413 * aid; it should allow us to debug why we caught an
414 * exception in environments where debugging is especially
417 if (panic_on_exception_triage
) {
418 panic("called exception_triage when it was forbidden by the boot environment");
422 * Try to raise the exception at the activation level.
424 mutex
= &thread
->mutex
;
425 if (KERN_SUCCESS
== check_exc_receiver_dependency(exception
, thread
->exc_actions
, mutex
))
427 kr
= exception_deliver(thread
, exception
, code
, codeCnt
, thread
->exc_actions
, mutex
);
428 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
433 * Maybe the task level will handle it.
436 mutex
= &task
->itk_lock_data
;
437 if (KERN_SUCCESS
== check_exc_receiver_dependency(exception
, task
->exc_actions
, mutex
))
439 kr
= exception_deliver(thread
, exception
, code
, codeCnt
, task
->exc_actions
, mutex
);
440 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
445 * How about at the host level?
447 host_priv
= host_priv_self();
448 mutex
= &host_priv
->lock
;
450 if (KERN_SUCCESS
== check_exc_receiver_dependency(exception
, host_priv
->exc_actions
, mutex
))
452 kr
= exception_deliver(thread
, exception
, code
, codeCnt
, host_priv
->exc_actions
, mutex
);
453 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
458 if ((exception
!= EXC_CRASH
) && (exception
!= EXC_RESOURCE
) &&
459 (exception
!= EXC_GUARD
) && (exception
!= EXC_CORPSE_NOTIFY
))
460 thread_exception_return();
465 * Routine: exception_triage
467 * The current thread caught an exception.
468 * We make an up-call to the thread's exception server.
470 * Nothing locked and no resources held.
471 * Called from an exception context, so
472 * thread_exception_return and thread_kdb_return
475 * KERN_SUCCESS if exception is handled by any of the handlers.
479 exception_type_t exception
,
480 mach_exception_data_t code
,
481 mach_msg_type_number_t codeCnt
)
483 thread_t thread
= current_thread();
484 return exception_triage_thread(exception
, code
, codeCnt
, thread
);
489 exception_type_t exception
,
490 mach_exception_data_t code
,
491 mach_msg_type_number_t codeCnt
)
495 thread_t self
= current_thread();
499 * Maybe the task level will handle it.
501 task
= current_task();
502 mutex
= &task
->itk_lock_data
;
504 kr
= exception_deliver(self
, exception
, code
, codeCnt
, task
->exc_actions
, mutex
);
506 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
507 return(KERN_SUCCESS
);
508 return(KERN_FAILURE
);
513 * Raise an exception on a task.
514 * This should tell launchd to launch Crash Reporter for this task.
516 kern_return_t
task_exception_notify(exception_type_t exception
,
517 mach_exception_data_type_t exccode
, mach_exception_data_type_t excsubcode
)
519 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
520 wait_interrupt_t wsave
;
521 kern_return_t kr
= KERN_SUCCESS
;
524 code
[1] = excsubcode
;
526 wsave
= thread_interrupt_level(THREAD_UNINT
);
527 kr
= exception_triage(exception
, code
, EXCEPTION_CODE_MAX
);
528 (void) thread_interrupt_level(wsave
);
534 * Handle interface for special performance monitoring
535 * This is a special case of the host exception handler
537 kern_return_t
sys_perf_notify(thread_t thread
, int pid
)
541 wait_interrupt_t wsave
;
544 hostp
= host_priv_self(); /* Get the host privileged ports */
545 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
546 code
[0] = 0xFF000001; /* Set terminate code */
547 code
[1] = pid
; /* Pass out the pid */
549 struct task
*task
= thread
->task
;
550 xport
= hostp
->exc_actions
[EXC_RPC_ALERT
].port
;
552 /* Make sure we're not catching our own exception */
553 if (!IP_VALID(xport
) ||
555 task
->itk_space
== xport
->data
.receiver
) {
557 return(KERN_FAILURE
);
560 wsave
= thread_interrupt_level(THREAD_UNINT
);
561 ret
= exception_deliver(
568 (void)thread_interrupt_level(wsave
);