]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/exception.c
xnu-4570.71.2.tar.gz
[apple/xnu.git] / osfmk / kern / exception.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
55e303ae 59#include <mach/mach_types.h>
1c79356b
A
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>
91447636 65#include <mach/task.h>
1c79356b
A
66#include <mach/thread_status.h>
67#include <mach/exception_types.h>
2d21ac55
A
68#include <mach/exc.h>
69#include <mach/mach_exc.h>
1c79356b
A
70#include <ipc/port.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>
1c79356b
A
77#include <kern/counters.h>
78#include <kern/ipc_tt.h>
79#include <kern/task.h>
80#include <kern/thread.h>
1c79356b
A
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>
39037602 86#include <security/mac_mach_internal.h>
1c79356b 87#include <string.h>
316670eb 88#include <pexpert/pexpert.h>
1c79356b 89
3e170ce0
A
90extern int panic_on_exception_triage;
91
1c79356b
A
92unsigned long c_thr_exc_raise = 0;
93unsigned long c_thr_exc_raise_state = 0;
94unsigned long c_thr_exc_raise_state_id = 0;
95unsigned long c_tsk_exc_raise = 0;
96unsigned long c_tsk_exc_raise_state = 0;
97unsigned long c_tsk_exc_raise_state_id = 0;
98
91447636 99/* forward declarations */
2d21ac55
A
100kern_return_t exception_deliver(
101 thread_t thread,
91447636 102 exception_type_t exception,
2d21ac55 103 mach_exception_data_t code,
91447636
A
104 mach_msg_type_number_t codeCnt,
105 struct exception_action *excp,
b0d623f7 106 lck_mtx_t *mutex);
91447636 107
fe8ab488 108static kern_return_t
3e170ce0 109check_exc_receiver_dependency(
fe8ab488
A
110 exception_type_t exception,
111 struct exception_action *excp,
112 lck_mtx_t *mutex);
113
91447636
A
114#ifdef MACH_BSD
115kern_return_t bsd_exception(
116 exception_type_t exception,
2d21ac55 117 mach_exception_data_t code,
91447636
A
118 mach_msg_type_number_t codeCnt);
119#endif /* MACH_BSD */
1c79356b
A
120
121/*
122 * Routine: exception_deliver
123 * Purpose:
124 * Make an upcall to the exception server provided.
125 * Conditions:
126 * Nothing locked and no resources held.
127 * Called from an exception context, so
128 * thread_exception_return and thread_kdb_return
129 * are possible.
130 * Returns:
2d21ac55 131 * KERN_SUCCESS if the exception was handled
1c79356b 132 */
2d21ac55 133kern_return_t
1c79356b 134exception_deliver(
2d21ac55 135 thread_t thread,
1c79356b 136 exception_type_t exception,
2d21ac55 137 mach_exception_data_t code,
1c79356b
A
138 mach_msg_type_number_t codeCnt,
139 struct exception_action *excp,
b0d623f7 140 lck_mtx_t *mutex)
1c79356b 141{
5ba3f43e 142 ipc_port_t exc_port = IPC_PORT_NULL;
2d21ac55
A
143 exception_data_type_t small_code[EXCEPTION_CODE_MAX];
144 int code64;
1c79356b
A
145 int behavior;
146 int flavor;
147 kern_return_t kr;
39037602 148 task_t task;
5ba3f43e 149 ipc_port_t thread_port = IPC_PORT_NULL, task_port = IPC_PORT_NULL;
1c79356b
A
150
151 /*
152 * Save work if we are terminating.
153 * Just go back to our AST handler.
154 */
3e170ce0 155 if (!thread->active && !thread->inspection)
2d21ac55 156 return KERN_SUCCESS;
1c79356b 157
39236c6e
A
158 /*
159 * If there are no exception actions defined for this entity,
160 * we can't deliver here.
161 */
162 if (excp == NULL)
163 return KERN_FAILURE;
164
165 assert(exception < EXC_TYPES_COUNT);
166 if (exception >= EXC_TYPES_COUNT)
167 return KERN_FAILURE;
168
169 excp = &excp[exception];
170
1c79356b
A
171 /*
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.
177 */
b0d623f7 178 lck_mtx_lock(mutex);
1c79356b
A
179 exc_port = excp->port;
180 if (!IP_VALID(exc_port)) {
b0d623f7 181 lck_mtx_unlock(mutex);
2d21ac55 182 return KERN_FAILURE;
1c79356b
A
183 }
184 ip_lock(exc_port);
185 if (!ip_active(exc_port)) {
186 ip_unlock(exc_port);
b0d623f7 187 lck_mtx_unlock(mutex);
2d21ac55 188 return KERN_FAILURE;
1c79356b
A
189 }
190 ip_reference(exc_port);
191 exc_port->ip_srights++;
192 ip_unlock(exc_port);
193
194 flavor = excp->flavor;
195 behavior = excp->behavior;
b0d623f7 196 lck_mtx_unlock(mutex);
1c79356b 197
2d21ac55
A
198 code64 = (behavior & MACH_EXCEPTION_CODES);
199 behavior &= ~MACH_EXCEPTION_CODES;
200
201 if (!code64) {
b0d623f7
A
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]);
2d21ac55
A
204 }
205
39037602
A
206 task = thread->task;
207
208#if CONFIG_MACF
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
213 * to the next level.
214 */
215 if (mac_exc_action_check_exception_send(task, excp) != 0) {
5ba3f43e
A
216 kr = KERN_FAILURE;
217 goto out_release_right;
39037602
A
218 }
219#endif
220
5ba3f43e
A
221 if (behavior != EXCEPTION_STATE) {
222 if (thread != current_thread() || exception == EXC_CORPSE_NOTIFY) {
39037602 223
5ba3f43e
A
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 */
230 }
231 else {
232 task_port = retrieve_task_self_fast(thread->task);
233 thread_port = retrieve_thread_self_fast(thread);
234 }
39037602 235 }
2d21ac55 236
1c79356b
A
237 switch (behavior) {
238 case EXCEPTION_STATE: {
239 mach_msg_type_number_t state_cnt;
55e303ae 240 thread_state_data_t state;
1c79356b
A
241
242 c_thr_exc_raise_state++;
91447636 243 state_cnt = _MachineStateCount[flavor];
2d21ac55 244 kr = thread_getstatus(thread, flavor,
1c79356b
A
245 (thread_state_t)state,
246 &state_cnt);
247 if (kr == KERN_SUCCESS) {
2d21ac55
A
248 if (code64) {
249 kr = mach_exception_raise_state(exc_port,
250 exception,
251 code,
252 codeCnt,
253 &flavor,
254 state, state_cnt,
255 state, &state_cnt);
256 } else {
257 kr = exception_raise_state(exc_port, exception,
258 small_code,
259 codeCnt,
260 &flavor,
261 state, state_cnt,
262 state, &state_cnt);
263 }
5ba3f43e
A
264 if (kr == KERN_SUCCESS) {
265 if (exception != EXC_CORPSE_NOTIFY)
266 kr = thread_setstatus(thread, flavor,
267 (thread_state_t)state,
268 state_cnt);
269 goto out_release_right;
270 }
271
1c79356b
A
272 }
273
5ba3f43e 274 goto out_release_right;
1c79356b
A
275 }
276
277 case EXCEPTION_DEFAULT:
278 c_thr_exc_raise++;
2d21ac55
A
279 if (code64) {
280 kr = mach_exception_raise(exc_port,
5ba3f43e
A
281 thread_port,
282 task_port,
2d21ac55
A
283 exception,
284 code,
285 codeCnt);
286 } else {
287 kr = exception_raise(exc_port,
5ba3f43e
A
288 thread_port,
289 task_port,
2d21ac55
A
290 exception,
291 small_code,
292 codeCnt);
293 }
1c79356b 294
5ba3f43e 295 goto out_release_right;
1c79356b
A
296
297 case EXCEPTION_STATE_IDENTITY: {
298 mach_msg_type_number_t state_cnt;
55e303ae 299 thread_state_data_t state;
1c79356b
A
300
301 c_thr_exc_raise_state_id++;
91447636 302 state_cnt = _MachineStateCount[flavor];
2d21ac55 303 kr = thread_getstatus(thread, flavor,
1c79356b
A
304 (thread_state_t)state,
305 &state_cnt);
306 if (kr == KERN_SUCCESS) {
2d21ac55
A
307 if (code64) {
308 kr = mach_exception_raise_state_identity(
309 exc_port,
5ba3f43e
A
310 thread_port,
311 task_port,
2d21ac55
A
312 exception,
313 code,
314 codeCnt,
315 &flavor,
316 state, state_cnt,
317 state, &state_cnt);
318 } else {
319 kr = exception_raise_state_identity(exc_port,
5ba3f43e
A
320 thread_port,
321 task_port,
2d21ac55
A
322 exception,
323 small_code,
324 codeCnt,
325 &flavor,
326 state, state_cnt,
327 state, &state_cnt);
328 }
5ba3f43e
A
329
330 if (kr == KERN_SUCCESS) {
331 if (exception != EXC_CORPSE_NOTIFY)
332 kr = thread_setstatus(thread, flavor,
333 (thread_state_t)state,
334 state_cnt);
335 goto out_release_right;
336 }
337
1c79356b
A
338 }
339
5ba3f43e 340 goto out_release_right;
1c79356b 341 }
2d21ac55 342
1c79356b 343 default:
2d21ac55
A
344 panic ("bad exception behavior!");
345 return KERN_FAILURE;
1c79356b 346 }/* switch */
5ba3f43e
A
347
348out_release_right:
349
350 if (task_port) {
351 ipc_port_release_send(task_port);
352 }
353
354 if (thread_port) {
355 ipc_port_release_send(thread_port);
356 }
357
358 if (exc_port) {
359 ipc_port_release_send(exc_port);
360 }
361
362 return kr;
1c79356b
A
363}
364
fe8ab488 365/*
3e170ce0 366 * Routine: check_exc_receiver_dependency
fe8ab488
A
367 * Purpose:
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.
372 *
373 * Conditions:
374 * Nothing locked and no resources held.
375 * Called from an exception context.
376 * Returns:
377 * KERN_SUCCESS if its ok to send exception message.
378 */
379kern_return_t
3e170ce0 380check_exc_receiver_dependency(
fe8ab488
A
381 exception_type_t exception,
382 struct exception_action *excp,
383 lck_mtx_t *mutex)
384{
385 kern_return_t retval = KERN_SUCCESS;
386
387 if (excp == NULL || exception != EXC_CRASH)
388 return retval;
389
390 task_t task = current_task();
391 lck_mtx_lock(mutex);
392 ipc_port_t xport = excp[exception].port;
393 if ( IP_VALID(xport)
394 && ip_active(xport)
395 && task->itk_space == xport->ip_receiver)
396 retval = KERN_FAILURE;
397 lck_mtx_unlock(mutex);
398 return retval;
399}
400
39037602 401
1c79356b 402/*
39037602 403 * Routine: exception_triage_thread
1c79356b 404 * Purpose:
39037602 405 * The thread caught an exception.
1c79356b
A
406 * We make an up-call to the thread's exception server.
407 * Conditions:
408 * Nothing locked and no resources held.
409 * Called from an exception context, so
410 * thread_exception_return and thread_kdb_return
411 * are possible.
412 * Returns:
3e170ce0 413 * KERN_SUCCESS if exception is handled by any of the handlers.
1c79356b 414 */
3e170ce0 415kern_return_t
39037602 416exception_triage_thread(
1c79356b 417 exception_type_t exception,
2d21ac55 418 mach_exception_data_t code,
39037602
A
419 mach_msg_type_number_t codeCnt,
420 thread_t thread)
1c79356b 421{
1c79356b
A
422 task_t task;
423 host_priv_t host_priv;
39236c6e 424 lck_mtx_t *mutex;
3e170ce0 425 kern_return_t kr = KERN_FAILURE;
1c79356b
A
426
427 assert(exception != EXC_RPC_ALERT);
428
3e170ce0
A
429 /*
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
435 * difficult.
436 */
437 if (panic_on_exception_triage) {
438 panic("called exception_triage when it was forbidden by the boot environment");
439 }
440
1c79356b
A
441 /*
442 * Try to raise the exception at the activation level.
443 */
b0d623f7 444 mutex = &thread->mutex;
3e170ce0 445 if (KERN_SUCCESS == check_exc_receiver_dependency(exception, thread->exc_actions, mutex))
fe8ab488
A
446 {
447 kr = exception_deliver(thread, exception, code, codeCnt, thread->exc_actions, mutex);
448 if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED)
449 goto out;
450 }
1c79356b
A
451
452 /*
453 * Maybe the task level will handle it.
454 */
39037602
A
455 task = thread->task;
456 mutex = &task->itk_lock_data;
3e170ce0 457 if (KERN_SUCCESS == check_exc_receiver_dependency(exception, task->exc_actions, mutex))
fe8ab488
A
458 {
459 kr = exception_deliver(thread, exception, code, codeCnt, task->exc_actions, mutex);
460 if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED)
461 goto out;
462 }
1c79356b
A
463
464 /*
465 * How about at the host level?
466 */
467 host_priv = host_priv_self();
b0d623f7 468 mutex = &host_priv->lock;
5ba3f43e 469
3e170ce0 470 if (KERN_SUCCESS == check_exc_receiver_dependency(exception, host_priv->exc_actions, mutex))
fe8ab488
A
471 {
472 kr = exception_deliver(thread, exception, code, codeCnt, host_priv->exc_actions, mutex);
473 if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED)
474 goto out;
475 }
1c79356b 476
2d21ac55 477out:
39236c6e 478 if ((exception != EXC_CRASH) && (exception != EXC_RESOURCE) &&
3e170ce0 479 (exception != EXC_GUARD) && (exception != EXC_CORPSE_NOTIFY))
2d21ac55 480 thread_exception_return();
3e170ce0 481 return kr;
1c79356b 482}
9bccf70c 483
39037602
A
484/*
485 * Routine: exception_triage
486 * Purpose:
487 * The current thread caught an exception.
488 * We make an up-call to the thread's exception server.
489 * Conditions:
490 * Nothing locked and no resources held.
491 * Called from an exception context, so
492 * thread_exception_return and thread_kdb_return
493 * are possible.
494 * Returns:
495 * KERN_SUCCESS if exception is handled by any of the handlers.
496 */
497kern_return_t
498exception_triage(
499 exception_type_t exception,
500 mach_exception_data_t code,
501 mach_msg_type_number_t codeCnt)
502{
503 thread_t thread = current_thread();
504 return exception_triage_thread(exception, code, codeCnt, thread);
505}
506
9bccf70c
A
507kern_return_t
508bsd_exception(
509 exception_type_t exception,
2d21ac55 510 mach_exception_data_t code,
9bccf70c
A
511 mach_msg_type_number_t codeCnt)
512{
513 task_t task;
b0d623f7 514 lck_mtx_t *mutex;
91447636 515 thread_t self = current_thread();
9bccf70c
A
516 kern_return_t kr;
517
518 /*
519 * Maybe the task level will handle it.
520 */
521 task = current_task();
39037602 522 mutex = &task->itk_lock_data;
9bccf70c 523
39236c6e 524 kr = exception_deliver(self, exception, code, codeCnt, task->exc_actions, mutex);
9bccf70c 525
2d21ac55
A
526 if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED)
527 return(KERN_SUCCESS);
528 return(KERN_FAILURE);
529}
9bccf70c 530
9bccf70c 531
2d21ac55 532/*
316670eb 533 * Raise an exception on a task.
2d21ac55
A
534 * This should tell launchd to launch Crash Reporter for this task.
535 */
316670eb
A
536kern_return_t task_exception_notify(exception_type_t exception,
537 mach_exception_data_type_t exccode, mach_exception_data_type_t excsubcode)
2d21ac55
A
538{
539 mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
540 wait_interrupt_t wsave;
3e170ce0 541 kern_return_t kr = KERN_SUCCESS;
9bccf70c 542
2d21ac55
A
543 code[0] = exccode;
544 code[1] = excsubcode;
9bccf70c 545
2d21ac55 546 wsave = thread_interrupt_level(THREAD_UNINT);
3e170ce0 547 kr = exception_triage(exception, code, EXCEPTION_CODE_MAX);
2d21ac55 548 (void) thread_interrupt_level(wsave);
3e170ce0 549 return kr;
9bccf70c
A
550}
551
55e303ae 552
55e303ae 553/*
2d21ac55 554 * Handle interface for special performance monitoring
55e303ae
A
555 * This is a special case of the host exception handler
556 */
2d21ac55 557kern_return_t sys_perf_notify(thread_t thread, int pid)
55e303ae
A
558{
559 host_priv_t hostp;
55e303ae 560 ipc_port_t xport;
55e303ae 561 wait_interrupt_t wsave;
2d21ac55 562 kern_return_t ret;
55e303ae 563
2d21ac55
A
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 */
55e303ae 568
2d21ac55 569 struct task *task = thread->task;
39236c6e 570 xport = hostp->exc_actions[EXC_RPC_ALERT].port;
55e303ae 571
2d21ac55
A
572 /* Make sure we're not catching our own exception */
573 if (!IP_VALID(xport) ||
574 !ip_active(xport) ||
575 task->itk_space == xport->data.receiver) {
55e303ae 576
2d21ac55 577 return(KERN_FAILURE);
55e303ae 578 }
2d21ac55
A
579
580 wsave = thread_interrupt_level(THREAD_UNINT);
581 ret = exception_deliver(
582 thread,
583 EXC_RPC_ALERT,
584 code,
585 2,
39236c6e 586 hostp->exc_actions,
2d21ac55
A
587 &hostp->lock);
588 (void)thread_interrupt_level(wsave);
589
590 return(ret);
55e303ae 591}
2d21ac55 592