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 <mach/mach_exc_server.h>
52 #include <kern/task.h>
53 #include <kern/thread.h>
54 #include <kern/sched_prim.h>
55 #include <kern/kalloc.h>
59 #include <sys/systm.h>
60 #include <sys/ux_exception.h>
61 #include <sys/vmparam.h> /* MAXSSIZ */
63 #include <vm/vm_protos.h> /* get_task_ipcspace() */
65 * XXX Things that should be retrieved from Mach headers, but aren't
68 extern kern_return_t
ipc_object_copyin(ipc_space_t space
, mach_port_name_t name
,
69 mach_msg_type_name_t msgt_name
, struct ipc_object
**objectp
);
70 extern mach_msg_return_t
mach_msg_receive(mach_msg_header_t
*msg
,
71 mach_msg_option_t option
, mach_msg_size_t rcv_size
,
72 mach_port_name_t rcv_name
, mach_msg_timeout_t rcv_timeout
,
73 void (*continuation
)(mach_msg_return_t
),
74 mach_msg_size_t slist_size
);
75 extern mach_msg_return_t
mach_msg_send(mach_msg_header_t
*msg
,
76 mach_msg_option_t option
, mach_msg_size_t send_size
,
77 mach_msg_timeout_t send_timeout
, mach_port_name_t notify
);
78 extern thread_t
convert_port_to_thread(ipc_port_t port
);
79 extern void ipc_port_release(ipc_port_t
);
86 * Unix exception handler.
89 static void ux_exception(int exception
, mach_exception_code_t code
,
90 mach_exception_subcode_t subcode
,
91 int *ux_signal
, mach_exception_code_t
*ux_code
);
93 mach_port_name_t ux_exception_port
;
94 static task_t ux_handler_self
;
100 task_t self
= current_task();
101 mach_port_name_t exc_port_name
;
102 mach_port_name_t exc_set_name
;
104 /* self->kernel_vm_space = TRUE; */
105 ux_handler_self
= self
;
109 * Allocate a port set that we will receive on.
111 if (mach_port_allocate(get_task_ipcspace(ux_handler_self
), MACH_PORT_RIGHT_PORT_SET
, &exc_set_name
) != MACH_MSG_SUCCESS
)
112 panic("ux_handler: port_set_allocate failed");
115 * Allocate an exception port and use object_copyin to
116 * translate it to the global name. Put it into the set.
118 if (mach_port_allocate(get_task_ipcspace(ux_handler_self
), MACH_PORT_RIGHT_RECEIVE
, &exc_port_name
) != MACH_MSG_SUCCESS
)
119 panic("ux_handler: port_allocate failed");
120 if (mach_port_move_member(get_task_ipcspace(ux_handler_self
),
121 exc_port_name
, exc_set_name
) != MACH_MSG_SUCCESS
)
122 panic("ux_handler: port_set_add failed");
124 if (ipc_object_copyin(get_task_ipcspace(self
), exc_port_name
,
125 MACH_MSG_TYPE_MAKE_SEND
,
126 (void *) &ux_exception_port
) != MACH_MSG_SUCCESS
)
127 panic("ux_handler: object_copyin(ux_exception_port) failed");
130 thread_wakeup(&ux_exception_port
);
133 /* Message handling loop. */
137 mach_msg_header_t Head
;
139 kern_return_t RetCode
;
142 mach_msg_header_t Head
;
143 /* start of the kernel processed data */
144 mach_msg_body_t msgh_body
;
145 mach_msg_port_descriptor_t thread
;
146 mach_msg_port_descriptor_t task
;
147 /* end of the kernel processed data */
149 exception_type_t exception
;
150 mach_msg_type_number_t codeCnt
;
151 mach_exception_data_t code
;
152 /* some times RCV_TO_LARGE probs */
155 mach_port_name_t reply_port
;
156 kern_return_t result
;
158 exc_msg
.Head
.msgh_local_port
= (mach_port_t
)exc_set_name
;
159 exc_msg
.Head
.msgh_size
= sizeof (exc_msg
);
161 result
= mach_msg_receive(&exc_msg
.Head
);
163 result
= mach_msg_receive(&exc_msg
.Head
, MACH_RCV_MSG
,
164 sizeof (exc_msg
), exc_set_name
,
165 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
,
168 if (result
== MACH_MSG_SUCCESS
) {
169 reply_port
= (mach_port_name_t
)exc_msg
.Head
.msgh_remote_port
;
171 if (mach_exc_server(&exc_msg
.Head
, &rep_msg
.Head
))
172 (void) mach_msg_send(&rep_msg
.Head
, MACH_SEND_MSG
,
173 sizeof (rep_msg
),MACH_MSG_TIMEOUT_NONE
,MACH_PORT_NULL
);
175 if (reply_port
!= MACH_PORT_NULL
)
176 (void) mach_port_deallocate(get_task_ipcspace(ux_handler_self
), reply_port
);
178 else if (result
== MACH_RCV_TOO_LARGE
)
179 /* ignore oversized messages */;
181 panic("exception_handler");
186 ux_handler_init(void)
188 ux_exception_port
= MACH_PORT_NULL
;
189 (void) kernel_thread(kernel_task
, ux_handler
);
191 if (ux_exception_port
== MACH_PORT_NULL
) {
192 (void)msleep(&ux_exception_port
, proc_list_mlock
, 0, "ux_handler_wait", 0);
198 catch_exception_raise(
199 __unused mach_port_t exception_port
,
202 exception_type_t exception
,
203 exception_data_t code
,
204 __unused mach_msg_type_number_t codeCnt
207 mach_exception_data_type_t big_code
[EXCEPTION_CODE_MAX
];
208 big_code
[0] = code
[0];
209 big_code
[1] = code
[1];
211 return catch_mach_exception_raise(exception_port
,
221 catch_mach_exception_raise(
222 __unused mach_port_t exception_port
,
225 exception_type_t exception
,
226 mach_exception_data_t code
,
227 __unused mach_msg_type_number_t codeCnt
230 task_t self
= current_task();
232 ipc_port_t thread_port
;
233 struct task
*sig_task
;
235 kern_return_t result
= MACH_MSG_SUCCESS
;
237 mach_exception_code_t ucode
= 0;
239 mach_port_name_t thread_name
= (mach_port_name_t
)thread
; /* XXX */
240 mach_port_name_t task_name
= (mach_port_name_t
)task
; /* XXX */
243 * Convert local thread name to global port.
245 if (MACH_PORT_VALID(thread_name
) &&
246 (ipc_object_copyin(get_task_ipcspace(self
), thread_name
,
247 MACH_MSG_TYPE_PORT_SEND
,
248 (void *) &thread_port
) == MACH_MSG_SUCCESS
)) {
249 if (IPC_PORT_VALID(thread_port
)) {
250 th_act
= convert_port_to_thread(thread_port
);
251 ipc_port_release(thread_port
);
253 th_act
= THREAD_NULL
;
259 if (th_act
!= THREAD_NULL
) {
262 * Convert exception to unix signal and code.
264 ux_exception(exception
, code
[0], code
[1], &ux_signal
, &ucode
);
266 ut
= get_bsdthread_info(th_act
);
267 sig_task
= get_threadtask(th_act
);
268 p
= (struct proc
*) get_bsdtask_info(sig_task
);
270 /* Can't deliver a signal without a bsd process */
273 result
= KERN_FAILURE
;
277 * Stack overflow should result in a SIGSEGV signal
278 * on the alternate stack.
279 * but we have one or more guard pages after the
280 * stack top, so we would get a KERN_PROTECTION_FAILURE
281 * exception instead of KERN_INVALID_ADDRESS, resulting in
283 * Detect that situation and select the correct signal.
285 if (code
[0] == KERN_PROTECTION_FAILURE
&&
286 ux_signal
== SIGBUS
) {
287 user_addr_t sp
, stack_min
, stack_max
;
292 if (ut
&& (ut
->uu_flag
& UT_VFORK
))
295 stack_min
= p
->user_stack
;
296 stack_max
= p
->user_stack
+ MAXSSIZ
;
297 #else /* STACK_GROWTH_UP */
298 stack_max
= p
->user_stack
;
299 stack_min
= p
->user_stack
- MAXSSIZ
;
300 #endif /* STACK_GROWTH_UP */
301 if (sp
>= stack_min
&&
304 * This is indeed a stack overflow. Deliver a
310 * If the thread/process is not ready to handle
311 * SIGSEGV on an alternate stack, force-deliver
312 * SIGSEGV with a SIG_DFL handler.
314 mask
= sigmask(ux_signal
);
316 if ((p
->p_sigignore
& mask
) ||
317 (ut
->uu_sigwait
& mask
) ||
318 (ut
->uu_sigmask
& mask
) ||
319 (ps
->ps_sigact
[SIGSEGV
] == SIG_IGN
) ||
320 (! (ps
->ps_sigonstack
& mask
))) {
321 p
->p_sigignore
&= ~mask
;
322 p
->p_sigcatch
&= ~mask
;
323 ps
->ps_sigact
[SIGSEGV
] = SIG_DFL
;
324 ut
->uu_sigwait
&= ~mask
;
325 ut
->uu_sigmask
&= ~mask
;
332 if (ux_signal
!= 0) {
333 ut
->uu_exception
= exception
;
334 //ut->uu_code = code[0]; // filled in by threadsignal
335 ut
->uu_subcode
= code
[1];
336 threadsignal(th_act
, ux_signal
, code
[0]);
339 thread_deallocate(th_act
);
342 result
= KERN_INVALID_ARGUMENT
;
345 result
= KERN_INVALID_ARGUMENT
;
348 * Delete our send rights to the task and thread ports.
350 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self
), task_name
);
351 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self
), thread_name
);
357 catch_exception_raise_state(
358 __unused mach_port_t exception_port
,
359 __unused exception_type_t exception
,
360 __unused
const exception_data_t code
,
361 __unused mach_msg_type_number_t codeCnt
,
362 __unused
int *flavor
,
363 __unused
const thread_state_t old_state
,
364 __unused mach_msg_type_number_t old_stateCnt
,
365 __unused thread_state_t new_state
,
366 __unused mach_msg_type_number_t
*new_stateCnt
)
368 return(KERN_INVALID_ARGUMENT
);
372 catch_mach_exception_raise_state(
373 __unused mach_port_t exception_port
,
374 __unused exception_type_t exception
,
375 __unused
const mach_exception_data_t code
,
376 __unused mach_msg_type_number_t codeCnt
,
377 __unused
int *flavor
,
378 __unused
const thread_state_t old_state
,
379 __unused mach_msg_type_number_t old_stateCnt
,
380 __unused thread_state_t new_state
,
381 __unused mach_msg_type_number_t
*new_stateCnt
)
383 return(KERN_INVALID_ARGUMENT
);
387 catch_exception_raise_state_identity(
388 __unused mach_port_t exception_port
,
389 __unused mach_port_t thread
,
390 __unused mach_port_t task
,
391 __unused exception_type_t exception
,
392 __unused exception_data_t code
,
393 __unused mach_msg_type_number_t codeCnt
,
394 __unused
int *flavor
,
395 __unused thread_state_t old_state
,
396 __unused mach_msg_type_number_t old_stateCnt
,
397 __unused thread_state_t new_state
,
398 __unused mach_msg_type_number_t
*new_stateCnt
)
400 return(KERN_INVALID_ARGUMENT
);
404 catch_mach_exception_raise_state_identity(
405 __unused mach_port_t exception_port
,
406 __unused mach_port_t thread
,
407 __unused mach_port_t task
,
408 __unused exception_type_t exception
,
409 __unused mach_exception_data_t code
,
410 __unused mach_msg_type_number_t codeCnt
,
411 __unused
int *flavor
,
412 __unused thread_state_t old_state
,
413 __unused mach_msg_type_number_t old_stateCnt
,
414 __unused thread_state_t new_state
,
415 __unused mach_msg_type_number_t
*new_stateCnt
)
417 return(KERN_INVALID_ARGUMENT
);
422 * ux_exception translates a mach exception, code and subcode to
423 * a signal and u.u_code. Calls machine_exception (machine dependent)
424 * to attempt translation first.
430 mach_exception_code_t code
,
431 mach_exception_subcode_t subcode
,
433 mach_exception_code_t
*ux_code
)
436 * Try machine-dependent translation first.
438 if (machine_exception(exception
, code
, subcode
, ux_signal
, ux_code
))
444 if (code
== KERN_INVALID_ADDRESS
)
445 *ux_signal
= SIGSEGV
;
450 case EXC_BAD_INSTRUCTION
:
465 case EXC_UNIX_BAD_SYSCALL
:
468 case EXC_UNIX_BAD_PIPE
:
469 *ux_signal
= SIGPIPE
;
472 *ux_signal
= SIGABRT
;
474 case EXC_SOFT_SIGNAL
:
475 *ux_signal
= SIGKILL
;
481 *ux_signal
= SIGTRAP
;