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>
87 #include <pexpert/pexpert.h>
89 extern int panic_on_exception_triage
;
91 unsigned long c_thr_exc_raise
= 0;
92 unsigned long c_thr_exc_raise_state
= 0;
93 unsigned long c_thr_exc_raise_state_id
= 0;
94 unsigned long c_tsk_exc_raise
= 0;
95 unsigned long c_tsk_exc_raise_state
= 0;
96 unsigned long c_tsk_exc_raise_state_id
= 0;
98 /* forward declarations */
99 kern_return_t
exception_deliver(
101 exception_type_t exception
,
102 mach_exception_data_t code
,
103 mach_msg_type_number_t codeCnt
,
104 struct exception_action
*excp
,
108 check_exc_receiver_dependency(
109 exception_type_t exception
,
110 struct exception_action
*excp
,
114 kern_return_t
bsd_exception(
115 exception_type_t exception
,
116 mach_exception_data_t code
,
117 mach_msg_type_number_t codeCnt
);
118 #endif /* MACH_BSD */
121 * Routine: exception_deliver
123 * Make an upcall to the exception server provided.
125 * Nothing locked and no resources held.
126 * Called from an exception context, so
127 * thread_exception_return and thread_kdb_return
130 * KERN_SUCCESS if the exception was handled
135 exception_type_t exception
,
136 mach_exception_data_t code
,
137 mach_msg_type_number_t codeCnt
,
138 struct exception_action
*excp
,
142 exception_data_type_t small_code
[EXCEPTION_CODE_MAX
];
149 * Save work if we are terminating.
150 * Just go back to our AST handler.
152 if (!thread
->active
&& !thread
->inspection
)
156 * If there are no exception actions defined for this entity,
157 * we can't deliver here.
162 assert(exception
< EXC_TYPES_COUNT
);
163 if (exception
>= EXC_TYPES_COUNT
)
166 excp
= &excp
[exception
];
169 * Snapshot the exception action data under lock for consistency.
170 * Hold a reference to the port over the exception_raise_* calls
171 * so it can't be destroyed. This seems like overkill, but keeps
172 * the port from disappearing between now and when
173 * ipc_object_copyin_from_kernel is finally called.
176 exc_port
= excp
->port
;
177 if (!IP_VALID(exc_port
)) {
178 lck_mtx_unlock(mutex
);
182 if (!ip_active(exc_port
)) {
184 lck_mtx_unlock(mutex
);
187 ip_reference(exc_port
);
188 exc_port
->ip_srights
++;
191 flavor
= excp
->flavor
;
192 behavior
= excp
->behavior
;
193 lck_mtx_unlock(mutex
);
195 code64
= (behavior
& MACH_EXCEPTION_CODES
);
196 behavior
&= ~MACH_EXCEPTION_CODES
;
199 small_code
[0] = CAST_DOWN_EXPLICIT(exception_data_type_t
, code
[0]);
200 small_code
[1] = CAST_DOWN_EXPLICIT(exception_data_type_t
, code
[1]);
205 case EXCEPTION_STATE
: {
206 mach_msg_type_number_t state_cnt
;
207 thread_state_data_t state
;
209 c_thr_exc_raise_state
++;
210 state_cnt
= _MachineStateCount
[flavor
];
211 kr
= thread_getstatus(thread
, flavor
,
212 (thread_state_t
)state
,
214 if (kr
== KERN_SUCCESS
) {
216 kr
= mach_exception_raise_state(exc_port
,
224 kr
= exception_raise_state(exc_port
, exception
,
231 if (kr
== MACH_MSG_SUCCESS
&& exception
!= EXC_CORPSE_NOTIFY
)
232 kr
= thread_setstatus(thread
, flavor
,
233 (thread_state_t
)state
,
240 case EXCEPTION_DEFAULT
:
243 kr
= mach_exception_raise(exc_port
,
244 retrieve_thread_self_fast(thread
),
245 retrieve_task_self_fast(thread
->task
),
250 kr
= exception_raise(exc_port
,
251 retrieve_thread_self_fast(thread
),
252 retrieve_task_self_fast(thread
->task
),
260 case EXCEPTION_STATE_IDENTITY
: {
261 mach_msg_type_number_t state_cnt
;
262 thread_state_data_t state
;
264 c_thr_exc_raise_state_id
++;
265 state_cnt
= _MachineStateCount
[flavor
];
266 kr
= thread_getstatus(thread
, flavor
,
267 (thread_state_t
)state
,
269 if (kr
== KERN_SUCCESS
) {
271 kr
= mach_exception_raise_state_identity(
273 retrieve_thread_self_fast(thread
),
274 retrieve_task_self_fast(thread
->task
),
282 kr
= exception_raise_state_identity(exc_port
,
283 retrieve_thread_self_fast(thread
),
284 retrieve_task_self_fast(thread
->task
),
292 if (kr
== MACH_MSG_SUCCESS
&& exception
!= EXC_CORPSE_NOTIFY
)
293 kr
= thread_setstatus(thread
, flavor
,
294 (thread_state_t
)state
,
302 panic ("bad exception behavior!");
308 * Routine: check_exc_receiver_dependency
310 * Verify that the port destined for receiving this exception is not
311 * on the current task. This would cause hang in kernel for
312 * EXC_CRASH primarily. Note: If port is transferred
313 * between check and delivery then deadlock may happen.
316 * Nothing locked and no resources held.
317 * Called from an exception context.
319 * KERN_SUCCESS if its ok to send exception message.
322 check_exc_receiver_dependency(
323 exception_type_t exception
,
324 struct exception_action
*excp
,
327 kern_return_t retval
= KERN_SUCCESS
;
329 if (excp
== NULL
|| exception
!= EXC_CRASH
)
332 task_t task
= current_task();
334 ipc_port_t xport
= excp
[exception
].port
;
337 && task
->itk_space
== xport
->ip_receiver
)
338 retval
= KERN_FAILURE
;
339 lck_mtx_unlock(mutex
);
344 * Routine: exception_triage
346 * The current thread caught an exception.
347 * We make an up-call to the thread's exception server.
349 * Nothing locked and no resources held.
350 * Called from an exception context, so
351 * thread_exception_return and thread_kdb_return
354 * KERN_SUCCESS if exception is handled by any of the handlers.
358 exception_type_t exception
,
359 mach_exception_data_t code
,
360 mach_msg_type_number_t codeCnt
)
364 host_priv_t host_priv
;
366 kern_return_t kr
= KERN_FAILURE
;
368 assert(exception
!= EXC_RPC_ALERT
);
371 * If this behavior has been requested by the the kernel
372 * (due to the boot environment), we should panic if we
373 * enter this function. This is intended as a debugging
374 * aid; it should allow us to debug why we caught an
375 * exception in environments where debugging is especially
378 if (panic_on_exception_triage
) {
379 panic("called exception_triage when it was forbidden by the boot environment");
382 thread
= current_thread();
385 * Try to raise the exception at the activation level.
387 mutex
= &thread
->mutex
;
388 if (KERN_SUCCESS
== check_exc_receiver_dependency(exception
, thread
->exc_actions
, mutex
))
390 kr
= exception_deliver(thread
, exception
, code
, codeCnt
, thread
->exc_actions
, mutex
);
391 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
396 * Maybe the task level will handle it.
398 task
= current_task();
400 if (KERN_SUCCESS
== check_exc_receiver_dependency(exception
, task
->exc_actions
, mutex
))
402 kr
= exception_deliver(thread
, exception
, code
, codeCnt
, task
->exc_actions
, mutex
);
403 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
408 * How about at the host level?
410 host_priv
= host_priv_self();
411 mutex
= &host_priv
->lock
;
413 if (KERN_SUCCESS
== check_exc_receiver_dependency(exception
, host_priv
->exc_actions
, mutex
))
415 kr
= exception_deliver(thread
, exception
, code
, codeCnt
, host_priv
->exc_actions
, mutex
);
416 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
421 if ((exception
!= EXC_CRASH
) && (exception
!= EXC_RESOURCE
) &&
422 (exception
!= EXC_GUARD
) && (exception
!= EXC_CORPSE_NOTIFY
))
423 thread_exception_return();
429 exception_type_t exception
,
430 mach_exception_data_t code
,
431 mach_msg_type_number_t codeCnt
)
435 thread_t self
= current_thread();
439 * Maybe the task level will handle it.
441 task
= current_task();
444 kr
= exception_deliver(self
, exception
, code
, codeCnt
, task
->exc_actions
, mutex
);
446 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
)
447 return(KERN_SUCCESS
);
448 return(KERN_FAILURE
);
453 * Raise an exception on a task.
454 * This should tell launchd to launch Crash Reporter for this task.
456 kern_return_t
task_exception_notify(exception_type_t exception
,
457 mach_exception_data_type_t exccode
, mach_exception_data_type_t excsubcode
)
459 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
460 wait_interrupt_t wsave
;
461 kern_return_t kr
= KERN_SUCCESS
;
464 code
[1] = excsubcode
;
466 wsave
= thread_interrupt_level(THREAD_UNINT
);
467 kr
= exception_triage(exception
, code
, EXCEPTION_CODE_MAX
);
468 (void) thread_interrupt_level(wsave
);
474 * Handle interface for special performance monitoring
475 * This is a special case of the host exception handler
477 kern_return_t
sys_perf_notify(thread_t thread
, int pid
)
481 wait_interrupt_t wsave
;
484 hostp
= host_priv_self(); /* Get the host privileged ports */
485 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
486 code
[0] = 0xFF000001; /* Set terminate code */
487 code
[1] = pid
; /* Pass out the pid */
489 struct task
*task
= thread
->task
;
490 xport
= hostp
->exc_actions
[EXC_RPC_ALERT
].port
;
492 /* Make sure we're not catching our own exception */
493 if (!IP_VALID(xport
) ||
495 task
->itk_space
== xport
->data
.receiver
) {
497 return(KERN_FAILURE
);
500 wsave
= thread_interrupt_level(THREAD_UNINT
);
501 ret
= exception_deliver(
508 (void)thread_interrupt_level(wsave
);