1/*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
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>
68#include <mach/exc.h>
69#include <mach/mach_exc.h>
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>
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 <string.h>
87#include <pexpert/pexpert.h>
88
89unsigned long c_thr_exc_raise = 0;
90unsigned long c_thr_exc_raise_state = 0;
91unsigned long c_thr_exc_raise_state_id = 0;
92unsigned long c_tsk_exc_raise = 0;
93unsigned long c_tsk_exc_raise_state = 0;
94unsigned long c_tsk_exc_raise_state_id = 0;
95
96/* forward declarations */
97kern_return_t exception_deliver(
98	thread_t 		thread,
99	exception_type_t	exception,
100	mach_exception_data_t	code,
101	mach_msg_type_number_t  codeCnt,
102	struct exception_action *excp,
103	lck_mtx_t			*mutex);
104
105#ifdef MACH_BSD
106kern_return_t bsd_exception(
107	exception_type_t	exception,
108	mach_exception_data_t	code,
109	mach_msg_type_number_t  codeCnt);
110#endif /* MACH_BSD */
111
112/*
113 *	Routine:	exception_deliver
114 *	Purpose:
115 *		Make an upcall to the exception server provided.
116 *	Conditions:
117 *		Nothing locked and no resources held.
118 *		Called from an exception context, so
119 *		thread_exception_return and thread_kdb_return
120 *		are possible.
121 *	Returns:
122 *		KERN_SUCCESS if the exception was handled
123 */
124kern_return_t
125exception_deliver(
126	thread_t		thread,
127	exception_type_t	exception,
128	mach_exception_data_t	code,
129	mach_msg_type_number_t  codeCnt,
130	struct exception_action *excp,
131	lck_mtx_t			*mutex)
132{
133	ipc_port_t		exc_port;
134	exception_data_type_t	small_code[EXCEPTION_CODE_MAX];
135	int			code64;
136	int			behavior;
137	int			flavor;
138	kern_return_t		kr;
139
140	/*
141	 *  Save work if we are terminating.
142	 *  Just go back to our AST handler.
143	 */
144	if (!thread->active)
145		return KERN_SUCCESS;
146
147	/*
148	 * Snapshot the exception action data under lock for consistency.
149	 * Hold a reference to the port over the exception_raise_* calls
150	 * so it can't be destroyed.  This seems like overkill, but keeps
151	 * the port from disappearing between now and when
152	 * ipc_object_copyin_from_kernel is finally called.
153	 */
154	lck_mtx_lock(mutex);
155	exc_port = excp->port;
156	if (!IP_VALID(exc_port)) {
157		lck_mtx_unlock(mutex);
158		return KERN_FAILURE;
159	}
160	ip_lock(exc_port);
161	if (!ip_active(exc_port)) {
162		ip_unlock(exc_port);
163		lck_mtx_unlock(mutex);
164		return KERN_FAILURE;
165	}
166	ip_reference(exc_port);
167	exc_port->ip_srights++;
168	ip_unlock(exc_port);
169
170	flavor = excp->flavor;
171	behavior = excp->behavior;
172	lck_mtx_unlock(mutex);
173
174	code64 = (behavior & MACH_EXCEPTION_CODES);
175	behavior &= ~MACH_EXCEPTION_CODES;
176
177	if (!code64) {
178		small_code[0] = CAST_DOWN_EXPLICIT(exception_data_type_t, code[0]);
179		small_code[1] = CAST_DOWN_EXPLICIT(exception_data_type_t, code[1]);
180	}
181
182
183	switch (behavior) {
184	case EXCEPTION_STATE: {
185		mach_msg_type_number_t state_cnt;
186		thread_state_data_t state;
187
188		c_thr_exc_raise_state++;
189		state_cnt = _MachineStateCount[flavor];
190		kr = thread_getstatus(thread, flavor,
191				      (thread_state_t)state,
192				      &state_cnt);
193		if (kr == KERN_SUCCESS) {
194			if (code64) {
195				kr = mach_exception_raise_state(exc_port,
196						exception,
197						code,
198						codeCnt,
199						&flavor,
200						state, state_cnt,
201						state, &state_cnt);
202			} else {
203				kr = exception_raise_state(exc_port, exception,
204						small_code,
205						codeCnt,
206						&flavor,
207						state, state_cnt,
208						state, &state_cnt);
209			}
210			if (kr == MACH_MSG_SUCCESS)
211				kr = thread_setstatus(thread, flavor,
212						(thread_state_t)state,
213						state_cnt);
214		}
215
216		return kr;
217	}
218
219	case EXCEPTION_DEFAULT:
220		c_thr_exc_raise++;
221		if (code64) {
222			kr = mach_exception_raise(exc_port,
223					retrieve_thread_self_fast(thread),
224					retrieve_task_self_fast(thread->task),
225					exception,
226					code,
227					codeCnt);
228		} else {
229			kr = exception_raise(exc_port,
230					retrieve_thread_self_fast(thread),
231					retrieve_task_self_fast(thread->task),
232					exception,
233					small_code,
234					codeCnt);
235		}
236
237		return kr;
238
239	case EXCEPTION_STATE_IDENTITY: {
240		mach_msg_type_number_t state_cnt;
241		thread_state_data_t state;
242
243		c_thr_exc_raise_state_id++;
244		state_cnt = _MachineStateCount[flavor];
245		kr = thread_getstatus(thread, flavor,
246				      (thread_state_t)state,
247				      &state_cnt);
248		if (kr == KERN_SUCCESS) {
249			if (code64) {
250				kr = mach_exception_raise_state_identity(
251						exc_port,
252						retrieve_thread_self_fast(thread),
253						retrieve_task_self_fast(thread->task),
254						exception,
255						code,
256						codeCnt,
257						&flavor,
258						state, state_cnt,
259						state, &state_cnt);
260			} else {
261				kr = exception_raise_state_identity(exc_port,
262						retrieve_thread_self_fast(thread),
263						retrieve_task_self_fast(thread->task),
264						exception,
265						small_code,
266						codeCnt,
267						&flavor,
268						state, state_cnt,
269						state, &state_cnt);
270			}
271			if (kr == MACH_MSG_SUCCESS)
272				kr = thread_setstatus(thread, flavor,
273						(thread_state_t)state,
274						state_cnt);
275		}
276
277		return kr;
278	}
279
280	default:
281	       panic ("bad exception behavior!");
282	       return KERN_FAILURE;
283	}/* switch */
284}
285
286/*
287 *	Routine:	exception
288 *	Purpose:
289 *		The current thread caught an exception.
290 *		We make an up-call to the thread's exception server.
291 *	Conditions:
292 *		Nothing locked and no resources held.
293 *		Called from an exception context, so
294 *		thread_exception_return and thread_kdb_return
295 *		are possible.
296 *	Returns:
297 *		Doesn't return.
298 */
299void
300exception_triage(
301	exception_type_t	exception,
302	mach_exception_data_t	code,
303	mach_msg_type_number_t  codeCnt)
304{
305	thread_t		thread;
306	task_t			task;
307	host_priv_t		host_priv;
308	struct exception_action *excp;
309	lck_mtx_t			*mutex;
310	kern_return_t		kr;
311
312	assert(exception != EXC_RPC_ALERT);
313
314	thread = current_thread();
315
316	/*
317	 * Try to raise the exception at the activation level.
318	 */
319	mutex = &thread->mutex;
320	excp = &thread->exc_actions[exception];
321	kr = exception_deliver(thread, exception, code, codeCnt, excp, mutex);
322	if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED)
323		goto out;
324
325	/*
326	 * Maybe the task level will handle it.
327	 */
328	task = current_task();
329	mutex = &task->lock;
330	excp = &task->exc_actions[exception];
331	kr = exception_deliver(thread, exception, code, codeCnt, excp, mutex);
332	if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED)
333		goto out;
334
335	/*
336	 * How about at the host level?
337	 */
338	host_priv = host_priv_self();
339	mutex = &host_priv->lock;
340	excp = &host_priv->exc_actions[exception];
341	kr = exception_deliver(thread, exception, code, codeCnt, excp, mutex);
342	if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED)
343		goto out;
344
345	/*
346	 * Nobody handled it, terminate the task.
347	 */
348	(void) task_terminate(task);
349
350out:
351	if ((exception != EXC_CRASH) && (exception != EXC_RESOURCE))
352		thread_exception_return();
353	return;
354}
355
356kern_return_t
357bsd_exception(
358	exception_type_t	exception,
359	mach_exception_data_t	code,
360	mach_msg_type_number_t  codeCnt)
361{
362	task_t			task;
363	struct exception_action *excp;
364	lck_mtx_t		*mutex;
365	thread_t		self = current_thread();
366	kern_return_t		kr;
367
368	/*
369	 * Maybe the task level will handle it.
370	 */
371	task = current_task();
372	mutex = &task->lock;
373	excp = &task->exc_actions[exception];
374
375	kr = exception_deliver(self, exception, code, codeCnt, excp, mutex);
376
377	if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED)
378		return(KERN_SUCCESS);
379	return(KERN_FAILURE);
380}
381
382
383/*
384 * Raise an exception on a task.
385 * This should tell launchd to launch Crash Reporter for this task.
386 */
387kern_return_t task_exception_notify(exception_type_t exception,
388	mach_exception_data_type_t exccode, mach_exception_data_type_t excsubcode)
389{
390	mach_exception_data_type_t	code[EXCEPTION_CODE_MAX];
391	wait_interrupt_t		wsave;
392
393	code[0] = exccode;
394	code[1] = excsubcode;
395
396	wsave = thread_interrupt_level(THREAD_UNINT);
397	exception_triage(exception, code, EXCEPTION_CODE_MAX);
398	(void) thread_interrupt_level(wsave);
399	return (KERN_SUCCESS);
400}
401
402
403/*
404 *	Handle interface for special performance monitoring
405 *	This is a special case of the host exception handler
406 */
407kern_return_t sys_perf_notify(thread_t thread, int pid)
408{
409	host_priv_t		hostp;
410	struct exception_action *excp;
411	ipc_port_t		xport;
412	wait_interrupt_t	wsave;
413	kern_return_t		ret;
414
415	hostp = host_priv_self();	/* Get the host privileged ports */
416	mach_exception_data_type_t	code[EXCEPTION_CODE_MAX];
417	code[0] = 0xFF000001;		/* Set terminate code */
418	code[1] = pid;		/* Pass out the pid */
419
420	struct task *task = thread->task;
421	excp = &hostp->exc_actions[EXC_RPC_ALERT];
422	xport = excp->port;
423
424	/* Make sure we're not catching our own exception */
425	if (!IP_VALID(xport) ||
426			!ip_active(xport) ||
427			task->itk_space == xport->data.receiver) {
428
429		return(KERN_FAILURE);
430	}
431
432	wsave = thread_interrupt_level(THREAD_UNINT);
433	ret = exception_deliver(
434			thread,
435			EXC_RPC_ALERT,
436			code,
437			2,
438			excp,
439			&hostp->lock);
440	(void)thread_interrupt_level(wsave);
441
442	return(ret);
443}
444
445