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@
29 * Mach Operating System
30 * Copyright (c) 1987 Carnegie-Mellon University
31 * All rights reserved. The CMU software License Agreement specifies
32 * the terms and conditions for use and redistribution.
36 *********************************************************************
38 **********************************************************************
41 #include <sys/param.h>
43 #include <mach/boolean.h>
44 #include <mach/exception.h>
45 #include <mach/kern_return.h>
46 #include <mach/message.h>
47 #include <mach/port.h>
48 #include <mach/mach_port.h>
49 #include <mach/mig_errors.h>
50 #include <mach/exc_server.h>
51 #include <kern/task.h>
52 #include <kern/thread.h>
53 #include <kern/sched_prim.h>
54 #include <kern/kalloc.h>
58 #include <sys/systm.h>
59 #include <sys/ux_exception.h>
61 #include <vm/vm_protos.h> /* get_task_ipcspace() */
64 * XXX Things that should be retrieved from Mach headers, but aren't
67 extern kern_return_t
ipc_object_copyin(ipc_space_t space
, mach_port_name_t name
,
68 mach_msg_type_name_t msgt_name
, struct ipc_object
**objectp
);
69 extern mach_msg_return_t
mach_msg_receive(mach_msg_header_t
*msg
,
70 mach_msg_option_t option
, mach_msg_size_t rcv_size
,
71 mach_port_name_t rcv_name
, mach_msg_timeout_t rcv_timeout
,
72 void (*continuation
)(mach_msg_return_t
),
73 mach_msg_size_t slist_size
);
74 extern mach_msg_return_t
mach_msg_send(mach_msg_header_t
*msg
,
75 mach_msg_option_t option
, mach_msg_size_t send_size
,
76 mach_msg_timeout_t send_timeout
, mach_port_name_t notify
);
77 extern thread_t
convert_port_to_thread(ipc_port_t port
);
78 extern void ipc_port_release(ipc_port_t
);
84 * Unix exception handler.
87 static void ux_exception(int exception
, int code
, int subcode
,
88 int *ux_signal
, int *ux_code
);
90 mach_port_name_t ux_exception_port
;
91 static task_t ux_handler_self
;
97 task_t self
= current_task();
98 mach_port_name_t exc_port_name
;
99 mach_port_name_t exc_set_name
;
101 (void) thread_funnel_set(kernel_flock
, TRUE
);
103 /* self->kernel_vm_space = TRUE; */
104 ux_handler_self
= self
;
108 * Allocate a port set that we will receive on.
110 if (mach_port_allocate(get_task_ipcspace(ux_handler_self
), MACH_PORT_RIGHT_PORT_SET
, &exc_set_name
) != MACH_MSG_SUCCESS
)
111 panic("ux_handler: port_set_allocate failed");
114 * Allocate an exception port and use object_copyin to
115 * translate it to the global name. Put it into the set.
117 if (mach_port_allocate(get_task_ipcspace(ux_handler_self
), MACH_PORT_RIGHT_RECEIVE
, &exc_port_name
) != MACH_MSG_SUCCESS
)
118 panic("ux_handler: port_allocate failed");
119 if (mach_port_move_member(get_task_ipcspace(ux_handler_self
),
120 exc_port_name
, exc_set_name
) != MACH_MSG_SUCCESS
)
121 panic("ux_handler: port_set_add failed");
123 if (ipc_object_copyin(get_task_ipcspace(self
), exc_port_name
,
124 MACH_MSG_TYPE_MAKE_SEND
,
125 (void *) &ux_exception_port
) != MACH_MSG_SUCCESS
)
126 panic("ux_handler: object_copyin(ux_exception_port) failed");
128 thread_wakeup(&ux_exception_port
);
130 /* Message handling loop. */
134 mach_msg_header_t Head
;
136 kern_return_t RetCode
;
139 mach_msg_header_t Head
;
140 /* start of the kernel processed data */
141 mach_msg_body_t msgh_body
;
142 mach_msg_port_descriptor_t thread
;
143 mach_msg_port_descriptor_t task
;
144 /* end of the kernel processed data */
146 exception_type_t exception
;
147 mach_msg_type_number_t codeCnt
;
148 exception_data_t code
;
149 /* some times RCV_TO_LARGE probs */
152 mach_port_name_t reply_port
;
153 kern_return_t result
;
155 exc_msg
.Head
.msgh_local_port
= (mach_port_t
)exc_set_name
;
156 exc_msg
.Head
.msgh_size
= sizeof (exc_msg
);
158 result
= mach_msg_receive(&exc_msg
.Head
);
160 result
= mach_msg_receive(&exc_msg
.Head
, MACH_RCV_MSG
,
161 sizeof (exc_msg
), exc_set_name
,
162 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
,
165 if (result
== MACH_MSG_SUCCESS
) {
166 reply_port
= (mach_port_name_t
)exc_msg
.Head
.msgh_remote_port
;
168 if (exc_server(&exc_msg
.Head
, &rep_msg
.Head
))
169 (void) mach_msg_send(&rep_msg
.Head
, MACH_SEND_MSG
,
170 sizeof (rep_msg
),MACH_MSG_TIMEOUT_NONE
,MACH_PORT_NULL
);
172 if (reply_port
!= MACH_PORT_NULL
)
173 (void) mach_port_deallocate(get_task_ipcspace(ux_handler_self
), reply_port
);
175 else if (result
== MACH_RCV_TOO_LARGE
)
176 /* ignore oversized messages */;
178 panic("exception_handler");
180 thread_funnel_set(kernel_flock
, FALSE
);
184 ux_handler_init(void)
186 ux_exception_port
= MACH_PORT_NULL
;
187 (void) kernel_thread(kernel_task
, ux_handler
);
188 if (ux_exception_port
== MACH_PORT_NULL
) {
189 assert_wait(&ux_exception_port
, THREAD_UNINT
);
190 thread_block(THREAD_CONTINUE_NULL
);
195 catch_exception_raise(
196 __unused mach_port_t exception_port
,
199 exception_type_t exception
,
200 exception_data_t code
,
201 __unused mach_msg_type_number_t codeCnt
204 task_t self
= current_task();
206 ipc_port_t thread_port
;
207 kern_return_t result
= MACH_MSG_SUCCESS
;
211 mach_port_name_t thread_name
= (mach_port_name_t
)thread
; /* XXX */
212 mach_port_name_t task_name
= (mach_port_name_t
)task
; /* XXX */
215 * Convert local thread name to global port.
217 if (MACH_PORT_VALID(thread_name
) &&
218 (ipc_object_copyin(get_task_ipcspace(self
), thread_name
,
219 MACH_MSG_TYPE_PORT_SEND
,
220 (void *) &thread_port
) == MACH_MSG_SUCCESS
)) {
221 if (IPC_PORT_VALID(thread_port
)) {
222 th_act
= convert_port_to_thread(thread_port
);
223 ipc_port_release(thread_port
);
225 th_act
= THREAD_NULL
;
231 if (th_act
!= THREAD_NULL
) {
234 * Convert exception to unix signal and code.
236 ut
= get_bsdthread_info(th_act
);
237 ux_exception(exception
, code
[0], code
[1],
238 &ux_signal
, (int *)&ucode
);
244 threadsignal(th_act
, ux_signal
, ucode
);
246 thread_deallocate(th_act
);
249 result
= KERN_INVALID_ARGUMENT
;
252 result
= KERN_INVALID_ARGUMENT
;
255 * Delete our send rights to the task and thread ports.
257 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self
), task_name
);
258 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self
), thread_name
);
264 catch_exception_raise_state(
265 __unused mach_port_t exception_port
,
266 __unused exception_type_t exception
,
267 __unused
const exception_data_t code
,
268 __unused mach_msg_type_number_t codeCnt
,
269 __unused
int *flavor
,
270 __unused
const thread_state_t old_state
,
271 __unused mach_msg_type_number_t old_stateCnt
,
272 __unused thread_state_t new_state
,
273 __unused mach_msg_type_number_t
*new_stateCnt
)
275 return(KERN_INVALID_ARGUMENT
);
279 catch_exception_raise_state_identity(
280 __unused mach_port_t exception_port
,
281 __unused mach_port_t thread
,
282 __unused mach_port_t task
,
283 __unused exception_type_t exception
,
284 __unused exception_data_t code
,
285 __unused mach_msg_type_number_t codeCnt
,
286 __unused
int *flavor
,
287 __unused thread_state_t old_state
,
288 __unused mach_msg_type_number_t old_stateCnt
,
289 __unused thread_state_t new_state
,
290 __unused mach_msg_type_number_t
*new_stateCnt
)
292 return(KERN_INVALID_ARGUMENT
);
296 * ux_exception translates a mach exception, code and subcode to
297 * a signal and u.u_code. Calls machine_exception (machine dependent)
298 * to attempt translation first.
311 * Try machine-dependent translation first.
313 if (machine_exception(exception
, code
, subcode
, ux_signal
, ux_code
))
319 if (code
== KERN_INVALID_ADDRESS
)
320 *ux_signal
= SIGSEGV
;
325 case EXC_BAD_INSTRUCTION
:
340 case EXC_UNIX_BAD_SYSCALL
:
343 case EXC_UNIX_BAD_PIPE
:
344 *ux_signal
= SIGPIPE
;
347 *ux_signal
= SIGABRT
;
349 case EXC_SOFT_SIGNAL
:
350 *ux_signal
= SIGKILL
;
356 *ux_signal
= SIGTRAP
;