2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Mach Operating System
24 * Copyright (c) 1987 Carnegie-Mellon University
25 * All rights reserved. The CMU software License Agreement specifies
26 * the terms and conditions for use and redistribution.
30 *********************************************************************
32 **********************************************************************
35 #include <sys/param.h>
37 #include <mach/boolean.h>
38 #include <mach/exception.h>
39 #include <mach/kern_return.h>
40 #include <mach/message.h>
41 #include <mach/port.h>
42 #include <mach/mig_errors.h>
43 #include <kern/task.h>
44 #include <kern/thread.h>
45 #include <kern/thread_act.h>
46 #include <kern/kalloc.h>
50 #include <sys/systm.h>
51 #include <sys/ux_exception.h>
54 * Unix exception handler.
57 static void ux_exception();
59 decl_simple_lock_data(static, ux_handler_init_lock
)
60 mach_port_name_t ux_exception_port
;
61 static task_t ux_handler_self
;
67 task_t self
= current_task();
68 mach_port_name_t exc_port_name
;
69 mach_port_name_t exc_set_name
;
71 (void) thread_funnel_set(kernel_flock
, TRUE
);
73 /* self->kernel_vm_space = TRUE; */
74 ux_handler_self
= self
;
78 * Allocate a port set that we will receive on.
80 if (mach_port_allocate(get_task_ipcspace(ux_handler_self
), MACH_PORT_RIGHT_PORT_SET
, &exc_set_name
) != MACH_MSG_SUCCESS
)
81 panic("ux_handler: port_set_allocate failed");
84 * Allocate an exception port and use object_copyin to
85 * translate it to the global name. Put it into the set.
87 if (mach_port_allocate(get_task_ipcspace(ux_handler_self
), MACH_PORT_RIGHT_RECEIVE
, &exc_port_name
) != MACH_MSG_SUCCESS
)
88 panic("ux_handler: port_allocate failed");
89 if (mach_port_move_member(get_task_ipcspace(ux_handler_self
),
90 exc_port_name
, exc_set_name
) != MACH_MSG_SUCCESS
)
91 panic("ux_handler: port_set_add failed");
93 if (ipc_object_copyin(get_task_ipcspace(self
), exc_port_name
,
94 MACH_MSG_TYPE_MAKE_SEND
,
95 (void *) &ux_exception_port
) != MACH_MSG_SUCCESS
)
96 panic("ux_handler: object_copyin(ux_exception_port) failed");
98 thread_wakeup(&ux_exception_port
);
100 /* Message handling loop. */
104 mach_msg_header_t Head
;
106 kern_return_t RetCode
;
109 mach_msg_header_t Head
;
110 /* start of the kernel processed data */
111 mach_msg_body_t msgh_body
;
112 mach_msg_port_descriptor_t thread
;
113 mach_msg_port_descriptor_t task
;
114 /* end of the kernel processed data */
116 exception_type_t exception
;
117 mach_msg_type_number_t codeCnt
;
118 exception_data_t code
;
119 /* some times RCV_TO_LARGE probs */
122 mach_port_name_t reply_port
;
123 kern_return_t result
;
125 exc_msg
.Head
.msgh_local_port
= (mach_port_t
)exc_set_name
;
126 exc_msg
.Head
.msgh_size
= sizeof (exc_msg
);
128 result
= mach_msg_receive(&exc_msg
.Head
);
130 result
= mach_msg_receive(&exc_msg
.Head
, MACH_RCV_MSG
,
131 sizeof (exc_msg
), exc_set_name
,
132 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
,
135 if (result
== MACH_MSG_SUCCESS
) {
136 reply_port
= (mach_port_name_t
)exc_msg
.Head
.msgh_remote_port
;
138 if (exc_server(&exc_msg
.Head
, &rep_msg
.Head
))
139 (void) mach_msg_send(&rep_msg
.Head
, MACH_SEND_MSG
,
140 sizeof (rep_msg
),MACH_MSG_TIMEOUT_NONE
,MACH_PORT_NULL
);
142 if (reply_port
!= MACH_PORT_NULL
)
143 (void) mach_port_deallocate(get_task_ipcspace(ux_handler_self
), reply_port
);
145 else if (result
== MACH_RCV_TOO_LARGE
)
146 /* ignore oversized messages */;
148 panic("exception_handler");
150 thread_funnel_set(kernel_flock
, FALSE
);
154 ux_handler_init(void)
158 simple_lock_init(&ux_handler_init_lock
);
159 ux_exception_port
= MACH_PORT_NULL
;
160 if (kernel_task_create(kernel_task
,
161 0, 0, &handler_task
) != MACH_MSG_SUCCESS
) {
162 panic("Failed to created ux handler task\n");
164 (void) kernel_thread(handler_task
, ux_handler
);
165 simple_lock(&ux_handler_init_lock
);
166 if (ux_exception_port
== MACH_PORT_NULL
) {
167 simple_unlock(&ux_handler_init_lock
);
168 assert_wait(&ux_exception_port
, THREAD_UNINT
);
169 thread_block((void (*)(void)) 0);
172 simple_unlock(&ux_handler_init_lock
);
176 catch_exception_raise(
177 mach_port_name_t exception_port
,
178 mach_port_name_t thread_name
,
179 mach_port_name_t task_name
,
181 exception_data_t code
,
182 mach_msg_type_number_t codecnt
185 task_t self
= current_task();
187 ipc_port_t thread_port
;
188 ipc_port_t task_port
;
189 kern_return_t result
= MACH_MSG_SUCCESS
;
195 * Convert local thread name to global port.
197 if (MACH_PORT_VALID(thread_name
) &&
198 (ipc_object_copyin(get_task_ipcspace(self
), thread_name
,
199 MACH_MSG_TYPE_PORT_SEND
,
200 (void *) &thread_port
) == MACH_MSG_SUCCESS
)) {
201 if (IPC_OBJECT_VALID(thread_port
)) {
202 th_act
= (thread_act_t
)convert_port_to_act(thread_port
);
203 ipc_port_release(thread_port
);
205 th_act
= THR_ACT_NULL
;
211 if (th_act
!= THR_ACT_NULL
) {
214 * Convert exception to unix signal and code.
216 ut
= get_bsdthread_info(th_act
);
217 ux_exception(exception
, code
[0], code
[1],
224 threadsignal(th_act
, signal
, ucode
);
226 act_deallocate(th_act
);
229 result
= KERN_INVALID_ARGUMENT
;
232 result
= KERN_INVALID_ARGUMENT
;
235 * Delete our send rights to the task and thread ports.
237 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self
), task_name
);
238 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self
),thread_name
);
243 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
)
245 return(KERN_INVALID_ARGUMENT
);
248 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
)
250 return(KERN_INVALID_ARGUMENT
);
253 boolean_t
machine_exception();
256 * ux_exception translates a mach exception, code and subcode to
257 * a signal and u.u_code. Calls machine_exception (machine dependent)
258 * to attempt translation first.
271 * Try machine-dependent translation first.
273 if (machine_exception(exception
, code
, subcode
, ux_signal
, ux_code
))
279 if (code
== KERN_INVALID_ADDRESS
)
280 *ux_signal
= SIGSEGV
;
285 case EXC_BAD_INSTRUCTION
:
300 case EXC_UNIX_BAD_SYSCALL
:
303 case EXC_UNIX_BAD_PIPE
:
304 *ux_signal
= SIGPIPE
;
307 *ux_signal
= SIGABRT
;
313 *ux_signal
= SIGTRAP
;