2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Mach Operating System
32 * Copyright (c) 1987 Carnegie-Mellon University
33 * All rights reserved. The CMU software License Agreement specifies
34 * the terms and conditions for use and redistribution.
38 *********************************************************************
40 **********************************************************************
43 #include <sys/param.h>
45 #include <mach/boolean.h>
46 #include <mach/exception.h>
47 #include <mach/kern_return.h>
48 #include <mach/message.h>
49 #include <mach/port.h>
50 #include <mach/mach_port.h>
51 #include <mach/mig_errors.h>
52 #include <mach/exc_server.h>
53 #include <kern/task.h>
54 #include <kern/thread.h>
55 #include <kern/sched_prim.h>
56 #include <kern/kalloc.h>
60 #include <sys/systm.h>
61 #include <sys/ux_exception.h>
63 #include <vm/vm_protos.h> /* get_task_ipcspace() */
66 * XXX Things that should be retrieved from Mach headers, but aren't
69 extern kern_return_t
ipc_object_copyin(ipc_space_t space
, mach_port_name_t name
,
70 mach_msg_type_name_t msgt_name
, struct ipc_object
**objectp
);
71 extern mach_msg_return_t
mach_msg_receive(mach_msg_header_t
*msg
,
72 mach_msg_option_t option
, mach_msg_size_t rcv_size
,
73 mach_port_name_t rcv_name
, mach_msg_timeout_t rcv_timeout
,
74 void (*continuation
)(mach_msg_return_t
),
75 mach_msg_size_t slist_size
);
76 extern mach_msg_return_t
mach_msg_send(mach_msg_header_t
*msg
,
77 mach_msg_option_t option
, mach_msg_size_t send_size
,
78 mach_msg_timeout_t send_timeout
, mach_port_name_t notify
);
79 extern thread_t
convert_port_to_thread(ipc_port_t port
);
80 extern void ipc_port_release(ipc_port_t
);
86 * Unix exception handler.
89 static void ux_exception(int exception
, int code
, int subcode
,
90 int *ux_signal
, int *ux_code
);
92 mach_port_name_t ux_exception_port
;
93 static task_t ux_handler_self
;
99 task_t self
= current_task();
100 mach_port_name_t exc_port_name
;
101 mach_port_name_t exc_set_name
;
103 (void) thread_funnel_set(kernel_flock
, TRUE
);
105 /* self->kernel_vm_space = TRUE; */
106 ux_handler_self
= self
;
110 * Allocate a port set that we will receive on.
112 if (mach_port_allocate(get_task_ipcspace(ux_handler_self
), MACH_PORT_RIGHT_PORT_SET
, &exc_set_name
) != MACH_MSG_SUCCESS
)
113 panic("ux_handler: port_set_allocate failed");
116 * Allocate an exception port and use object_copyin to
117 * translate it to the global name. Put it into the set.
119 if (mach_port_allocate(get_task_ipcspace(ux_handler_self
), MACH_PORT_RIGHT_RECEIVE
, &exc_port_name
) != MACH_MSG_SUCCESS
)
120 panic("ux_handler: port_allocate failed");
121 if (mach_port_move_member(get_task_ipcspace(ux_handler_self
),
122 exc_port_name
, exc_set_name
) != MACH_MSG_SUCCESS
)
123 panic("ux_handler: port_set_add failed");
125 if (ipc_object_copyin(get_task_ipcspace(self
), exc_port_name
,
126 MACH_MSG_TYPE_MAKE_SEND
,
127 (void *) &ux_exception_port
) != MACH_MSG_SUCCESS
)
128 panic("ux_handler: object_copyin(ux_exception_port) failed");
130 thread_wakeup(&ux_exception_port
);
132 /* Message handling loop. */
136 mach_msg_header_t Head
;
138 kern_return_t RetCode
;
141 mach_msg_header_t Head
;
142 /* start of the kernel processed data */
143 mach_msg_body_t msgh_body
;
144 mach_msg_port_descriptor_t thread
;
145 mach_msg_port_descriptor_t task
;
146 /* end of the kernel processed data */
148 exception_type_t exception
;
149 mach_msg_type_number_t codeCnt
;
150 exception_data_t code
;
151 /* some times RCV_TO_LARGE probs */
154 mach_port_name_t reply_port
;
155 kern_return_t result
;
157 exc_msg
.Head
.msgh_local_port
= (mach_port_t
)exc_set_name
;
158 exc_msg
.Head
.msgh_size
= sizeof (exc_msg
);
160 result
= mach_msg_receive(&exc_msg
.Head
);
162 result
= mach_msg_receive(&exc_msg
.Head
, MACH_RCV_MSG
,
163 sizeof (exc_msg
), exc_set_name
,
164 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
,
167 if (result
== MACH_MSG_SUCCESS
) {
168 reply_port
= (mach_port_name_t
)exc_msg
.Head
.msgh_remote_port
;
170 if (exc_server(&exc_msg
.Head
, &rep_msg
.Head
))
171 (void) mach_msg_send(&rep_msg
.Head
, MACH_SEND_MSG
,
172 sizeof (rep_msg
),MACH_MSG_TIMEOUT_NONE
,MACH_PORT_NULL
);
174 if (reply_port
!= MACH_PORT_NULL
)
175 (void) mach_port_deallocate(get_task_ipcspace(ux_handler_self
), reply_port
);
177 else if (result
== MACH_RCV_TOO_LARGE
)
178 /* ignore oversized messages */;
180 panic("exception_handler");
182 thread_funnel_set(kernel_flock
, FALSE
);
186 ux_handler_init(void)
188 ux_exception_port
= MACH_PORT_NULL
;
189 (void) kernel_thread(kernel_task
, ux_handler
);
190 if (ux_exception_port
== MACH_PORT_NULL
) {
191 assert_wait(&ux_exception_port
, THREAD_UNINT
);
192 thread_block(THREAD_CONTINUE_NULL
);
197 catch_exception_raise(
198 __unused mach_port_t exception_port
,
201 exception_type_t exception
,
202 exception_data_t code
,
203 __unused mach_msg_type_number_t codeCnt
206 task_t self
= current_task();
208 ipc_port_t thread_port
;
209 kern_return_t result
= MACH_MSG_SUCCESS
;
213 mach_port_name_t thread_name
= (mach_port_name_t
)thread
; /* XXX */
214 mach_port_name_t task_name
= (mach_port_name_t
)task
; /* XXX */
217 * Convert local thread name to global port.
219 if (MACH_PORT_VALID(thread_name
) &&
220 (ipc_object_copyin(get_task_ipcspace(self
), thread_name
,
221 MACH_MSG_TYPE_PORT_SEND
,
222 (void *) &thread_port
) == MACH_MSG_SUCCESS
)) {
223 if (IPC_PORT_VALID(thread_port
)) {
224 th_act
= convert_port_to_thread(thread_port
);
225 ipc_port_release(thread_port
);
227 th_act
= THREAD_NULL
;
233 if (th_act
!= THREAD_NULL
) {
236 * Convert exception to unix signal and code.
238 ut
= get_bsdthread_info(th_act
);
239 ux_exception(exception
, code
[0], code
[1],
240 &ux_signal
, (int *)&ucode
);
245 if (ux_signal
!= 0) {
246 ut
->uu_exception
= exception
;
247 //ut->uu_code = code[0]; // filled in by threadsignal
248 ut
->uu_subcode
= code
[1];
249 threadsignal(th_act
, ux_signal
, code
[0]);
252 thread_deallocate(th_act
);
255 result
= KERN_INVALID_ARGUMENT
;
258 result
= KERN_INVALID_ARGUMENT
;
261 * Delete our send rights to the task and thread ports.
263 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self
), task_name
);
264 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self
), thread_name
);
270 catch_exception_raise_state(
271 __unused mach_port_t exception_port
,
272 __unused exception_type_t exception
,
273 __unused
const exception_data_t code
,
274 __unused mach_msg_type_number_t codeCnt
,
275 __unused
int *flavor
,
276 __unused
const thread_state_t old_state
,
277 __unused mach_msg_type_number_t old_stateCnt
,
278 __unused thread_state_t new_state
,
279 __unused mach_msg_type_number_t
*new_stateCnt
)
281 return(KERN_INVALID_ARGUMENT
);
285 catch_exception_raise_state_identity(
286 __unused mach_port_t exception_port
,
287 __unused mach_port_t thread
,
288 __unused mach_port_t task
,
289 __unused exception_type_t exception
,
290 __unused exception_data_t code
,
291 __unused mach_msg_type_number_t codeCnt
,
292 __unused
int *flavor
,
293 __unused thread_state_t old_state
,
294 __unused mach_msg_type_number_t old_stateCnt
,
295 __unused thread_state_t new_state
,
296 __unused mach_msg_type_number_t
*new_stateCnt
)
298 return(KERN_INVALID_ARGUMENT
);
302 * ux_exception translates a mach exception, code and subcode to
303 * a signal and u.u_code. Calls machine_exception (machine dependent)
304 * to attempt translation first.
317 * Try machine-dependent translation first.
319 if (machine_exception(exception
, code
, subcode
, ux_signal
, ux_code
))
325 if (code
== KERN_INVALID_ADDRESS
)
326 *ux_signal
= SIGSEGV
;
331 case EXC_BAD_INSTRUCTION
:
346 case EXC_UNIX_BAD_SYSCALL
:
349 case EXC_UNIX_BAD_PIPE
:
350 *ux_signal
= SIGPIPE
;
353 *ux_signal
= SIGABRT
;
355 case EXC_SOFT_SIGNAL
:
356 *ux_signal
= SIGKILL
;
362 *ux_signal
= SIGTRAP
;