2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1987 Carnegie-Mellon University
28 * All rights reserved. The CMU software License Agreement specifies
29 * the terms and conditions for use and redistribution.
33 *********************************************************************
35 **********************************************************************
38 #include <sys/param.h>
40 #include <mach/boolean.h>
41 #include <mach/exception.h>
42 #include <mach/kern_return.h>
43 #include <mach/message.h>
44 #include <mach/port.h>
45 #include <mach/mig_errors.h>
46 #include <kern/task.h>
47 #include <kern/thread.h>
48 #include <kern/sched_prim.h>
49 #include <kern/thread_act.h>
50 #include <kern/kalloc.h>
54 #include <sys/systm.h>
55 #include <sys/ux_exception.h>
58 * Unix exception handler.
61 static void ux_exception();
63 decl_simple_lock_data(static, ux_handler_init_lock
)
64 mach_port_name_t ux_exception_port
;
65 static task_t ux_handler_self
;
71 task_t self
= current_task();
72 mach_port_name_t exc_port_name
;
73 mach_port_name_t exc_set_name
;
75 (void) thread_funnel_set(kernel_flock
, TRUE
);
77 /* self->kernel_vm_space = TRUE; */
78 ux_handler_self
= self
;
82 * Allocate a port set that we will receive on.
84 if (mach_port_allocate(get_task_ipcspace(ux_handler_self
), MACH_PORT_RIGHT_PORT_SET
, &exc_set_name
) != MACH_MSG_SUCCESS
)
85 panic("ux_handler: port_set_allocate failed");
88 * Allocate an exception port and use object_copyin to
89 * translate it to the global name. Put it into the set.
91 if (mach_port_allocate(get_task_ipcspace(ux_handler_self
), MACH_PORT_RIGHT_RECEIVE
, &exc_port_name
) != MACH_MSG_SUCCESS
)
92 panic("ux_handler: port_allocate failed");
93 if (mach_port_move_member(get_task_ipcspace(ux_handler_self
),
94 exc_port_name
, exc_set_name
) != MACH_MSG_SUCCESS
)
95 panic("ux_handler: port_set_add failed");
97 if (ipc_object_copyin(get_task_ipcspace(self
), exc_port_name
,
98 MACH_MSG_TYPE_MAKE_SEND
,
99 (void *) &ux_exception_port
) != MACH_MSG_SUCCESS
)
100 panic("ux_handler: object_copyin(ux_exception_port) failed");
102 thread_wakeup(&ux_exception_port
);
104 /* Message handling loop. */
108 mach_msg_header_t Head
;
110 kern_return_t RetCode
;
113 mach_msg_header_t Head
;
114 /* start of the kernel processed data */
115 mach_msg_body_t msgh_body
;
116 mach_msg_port_descriptor_t thread
;
117 mach_msg_port_descriptor_t task
;
118 /* end of the kernel processed data */
120 exception_type_t exception
;
121 mach_msg_type_number_t codeCnt
;
122 exception_data_t code
;
123 /* some times RCV_TO_LARGE probs */
126 mach_port_name_t reply_port
;
127 kern_return_t result
;
129 exc_msg
.Head
.msgh_local_port
= (mach_port_t
)exc_set_name
;
130 exc_msg
.Head
.msgh_size
= sizeof (exc_msg
);
132 result
= mach_msg_receive(&exc_msg
.Head
);
134 result
= mach_msg_receive(&exc_msg
.Head
, MACH_RCV_MSG
,
135 sizeof (exc_msg
), exc_set_name
,
136 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
,
139 if (result
== MACH_MSG_SUCCESS
) {
140 reply_port
= (mach_port_name_t
)exc_msg
.Head
.msgh_remote_port
;
142 if (exc_server(&exc_msg
.Head
, &rep_msg
.Head
))
143 (void) mach_msg_send(&rep_msg
.Head
, MACH_SEND_MSG
,
144 sizeof (rep_msg
),MACH_MSG_TIMEOUT_NONE
,MACH_PORT_NULL
);
146 if (reply_port
!= MACH_PORT_NULL
)
147 (void) mach_port_deallocate(get_task_ipcspace(ux_handler_self
), reply_port
);
149 else if (result
== MACH_RCV_TOO_LARGE
)
150 /* ignore oversized messages */;
152 panic("exception_handler");
154 thread_funnel_set(kernel_flock
, FALSE
);
158 ux_handler_init(void)
162 simple_lock_init(&ux_handler_init_lock
);
163 ux_exception_port
= MACH_PORT_NULL
;
164 if (kernel_task_create(kernel_task
,
165 0, 0, &handler_task
) != MACH_MSG_SUCCESS
) {
166 panic("Failed to created ux handler task\n");
168 (void) kernel_thread(handler_task
, ux_handler
);
169 simple_lock(&ux_handler_init_lock
);
170 if (ux_exception_port
== MACH_PORT_NULL
) {
171 simple_unlock(&ux_handler_init_lock
);
172 assert_wait(&ux_exception_port
, THREAD_UNINT
);
173 thread_block(THREAD_CONTINUE_NULL
);
176 simple_unlock(&ux_handler_init_lock
);
180 catch_exception_raise(
181 mach_port_name_t exception_port
,
182 mach_port_name_t thread_name
,
183 mach_port_name_t task_name
,
185 exception_data_t code
,
186 mach_msg_type_number_t codecnt
189 task_t self
= current_task();
191 ipc_port_t thread_port
;
192 ipc_port_t task_port
;
193 kern_return_t result
= MACH_MSG_SUCCESS
;
199 * Convert local thread name to global port.
201 if (MACH_PORT_VALID(thread_name
) &&
202 (ipc_object_copyin(get_task_ipcspace(self
), thread_name
,
203 MACH_MSG_TYPE_PORT_SEND
,
204 (void *) &thread_port
) == MACH_MSG_SUCCESS
)) {
205 if (IPC_PORT_VALID(thread_port
)) {
206 th_act
= (thread_act_t
)convert_port_to_act(thread_port
);
207 ipc_port_release(thread_port
);
209 th_act
= THR_ACT_NULL
;
215 if (th_act
!= THR_ACT_NULL
) {
218 * Convert exception to unix signal and code.
220 ut
= get_bsdthread_info(th_act
);
221 ux_exception(exception
, code
[0], code
[1],
228 threadsignal(th_act
, signal
, ucode
);
230 act_deallocate(th_act
);
233 result
= KERN_INVALID_ARGUMENT
;
236 result
= KERN_INVALID_ARGUMENT
;
239 * Delete our send rights to the task and thread ports.
241 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self
), task_name
);
242 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self
),thread_name
);
247 catch_exception_raise_state(mach_port_name_t exception_port
, int exception
, exception_data_t code
, mach_msg_type_number_t codeCnt
, int flavor
, thread_state_t old_state
, int old_stateCnt
, thread_state_t new_state
, int new_stateCnt
)
249 return(KERN_INVALID_ARGUMENT
);
252 catch_exception_raise_state_identity(mach_port_name_t exception_port
, mach_port_t thread
, mach_port_t task
, int exception
, exception_data_t code
, mach_msg_type_number_t codeCnt
, int flavor
, thread_state_t old_state
, int old_stateCnt
, thread_state_t new_state
, int new_stateCnt
)
254 return(KERN_INVALID_ARGUMENT
);
257 boolean_t
machine_exception();
260 * ux_exception translates a mach exception, code and subcode to
261 * a signal and u.u_code. Calls machine_exception (machine dependent)
262 * to attempt translation first.
275 * Try machine-dependent translation first.
277 if (machine_exception(exception
, code
, subcode
, ux_signal
, ux_code
))
283 if (code
== KERN_INVALID_ADDRESS
)
284 *ux_signal
= SIGSEGV
;
289 case EXC_BAD_INSTRUCTION
:
304 case EXC_UNIX_BAD_SYSCALL
:
307 case EXC_UNIX_BAD_PIPE
:
308 *ux_signal
= SIGPIPE
;
311 *ux_signal
= SIGABRT
;
313 case EXC_SOFT_SIGNAL
:
314 *ux_signal
= SIGKILL
;
320 *ux_signal
= SIGTRAP
;