]> git.saurik.com Git - apple/xnu.git/blame - bsd/uxkern/ux_exception.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / bsd / uxkern / ux_exception.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
A
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
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.
33 */
34
35/*
36 *********************************************************************
37 * HISTORY
38 **********************************************************************
39 */
40
41#include <sys/param.h>
42
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>
91447636 48#include <mach/mach_port.h>
1c79356b 49#include <mach/mig_errors.h>
91447636 50#include <mach/exc_server.h>
1c79356b
A
51#include <kern/task.h>
52#include <kern/thread.h>
9bccf70c 53#include <kern/sched_prim.h>
1c79356b
A
54#include <kern/kalloc.h>
55
56#include <sys/proc.h>
57#include <sys/user.h>
58#include <sys/systm.h>
59#include <sys/ux_exception.h>
60
91447636
A
61#include <vm/vm_protos.h> /* get_task_ipcspace() */
62
63/*
64 * XXX Things that should be retrieved from Mach headers, but aren't
65 */
66struct ipc_object;
67extern 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);
69extern 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);
74extern 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);
77extern thread_t convert_port_to_thread(ipc_port_t port);
78extern void ipc_port_release(ipc_port_t);
79
80
81
82
1c79356b
A
83/*
84 * Unix exception handler.
85 */
86
91447636
A
87static void ux_exception(int exception, int code, int subcode,
88 int *ux_signal, int *ux_code);
1c79356b 89
1c79356b
A
90mach_port_name_t ux_exception_port;
91static task_t ux_handler_self;
92
93static
94void
95ux_handler(void)
96{
97 task_t self = current_task();
98 mach_port_name_t exc_port_name;
99 mach_port_name_t exc_set_name;
100
101 (void) thread_funnel_set(kernel_flock, TRUE);
102
103 /* self->kernel_vm_space = TRUE; */
104 ux_handler_self = self;
105
106
107 /*
108 * Allocate a port set that we will receive on.
109 */
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");
112
113 /*
114 * Allocate an exception port and use object_copyin to
115 * translate it to the global name. Put it into the set.
116 */
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");
122
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");
127
128 thread_wakeup(&ux_exception_port);
129
130 /* Message handling loop. */
131
132 for (;;) {
133 struct rep_msg {
134 mach_msg_header_t Head;
135 NDR_record_t NDR;
136 kern_return_t RetCode;
137 } rep_msg;
138 struct exc_msg {
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 */
145 NDR_record_t NDR;
146 exception_type_t exception;
147 mach_msg_type_number_t codeCnt;
148 exception_data_t code;
149 /* some times RCV_TO_LARGE probs */
150 char pad[512];
151 } exc_msg;
152 mach_port_name_t reply_port;
153 kern_return_t result;
154
155 exc_msg.Head.msgh_local_port = (mach_port_t)exc_set_name;
156 exc_msg.Head.msgh_size = sizeof (exc_msg);
157#if 0
158 result = mach_msg_receive(&exc_msg.Head);
159#else
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,
163 0);
164#endif
165 if (result == MACH_MSG_SUCCESS) {
166 reply_port = (mach_port_name_t)exc_msg.Head.msgh_remote_port;
167
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);
171
172 if (reply_port != MACH_PORT_NULL)
173 (void) mach_port_deallocate(get_task_ipcspace(ux_handler_self), reply_port);
174 }
175 else if (result == MACH_RCV_TOO_LARGE)
176 /* ignore oversized messages */;
177 else
178 panic("exception_handler");
179 }
180 thread_funnel_set(kernel_flock, FALSE);
181}
182
183void
184ux_handler_init(void)
185{
1c79356b 186 ux_exception_port = MACH_PORT_NULL;
55e303ae 187 (void) kernel_thread(kernel_task, ux_handler);
1c79356b 188 if (ux_exception_port == MACH_PORT_NULL) {
1c79356b 189 assert_wait(&ux_exception_port, THREAD_UNINT);
9bccf70c 190 thread_block(THREAD_CONTINUE_NULL);
1c79356b 191 }
1c79356b
A
192}
193
194kern_return_t
195catch_exception_raise(
91447636
A
196 __unused mach_port_t exception_port,
197 mach_port_t thread,
198 mach_port_t task,
199 exception_type_t exception,
200 exception_data_t code,
201 __unused mach_msg_type_number_t codeCnt
1c79356b
A
202)
203{
204 task_t self = current_task();
91447636 205 thread_t th_act;
1c79356b 206 ipc_port_t thread_port;
1c79356b 207 kern_return_t result = MACH_MSG_SUCCESS;
91447636 208 int ux_signal = 0;
1c79356b
A
209 u_long ucode = 0;
210 struct uthread *ut;
91447636
A
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 */
1c79356b
A
213
214 /*
215 * Convert local thread name to global port.
216 */
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)) {
9bccf70c 221 if (IPC_PORT_VALID(thread_port)) {
91447636 222 th_act = convert_port_to_thread(thread_port);
1c79356b
A
223 ipc_port_release(thread_port);
224 } else {
91447636 225 th_act = THREAD_NULL;
1c79356b
A
226 }
227
228 /*
229 * Catch bogus ports
230 */
91447636 231 if (th_act != THREAD_NULL) {
1c79356b
A
232
233 /*
234 * Convert exception to unix signal and code.
235 */
236 ut = get_bsdthread_info(th_act);
237 ux_exception(exception, code[0], code[1],
91447636 238 &ux_signal, (int *)&ucode);
1c79356b
A
239
240 /*
241 * Send signal.
242 */
4452a7af
A
243 if (ux_signal != 0) {
244 ut->uu_exception = exception;
245 //ut->uu_code = code[0]; // filled in by threadsignal
246 ut->uu_subcode = code[1];
247 threadsignal(th_act, ux_signal, code[0]);
248 }
1c79356b 249
91447636 250 thread_deallocate(th_act);
1c79356b
A
251 }
252 else
253 result = KERN_INVALID_ARGUMENT;
254 }
255 else
256 result = KERN_INVALID_ARGUMENT;
257
258 /*
259 * Delete our send rights to the task and thread ports.
260 */
261 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self), task_name);
91447636 262 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self), thread_name);
1c79356b
A
263
264 return (result);
265}
91447636 266
1c79356b 267kern_return_t
91447636
A
268catch_exception_raise_state(
269 __unused mach_port_t exception_port,
270 __unused exception_type_t exception,
271 __unused const exception_data_t code,
272 __unused mach_msg_type_number_t codeCnt,
273 __unused int *flavor,
274 __unused const thread_state_t old_state,
275 __unused mach_msg_type_number_t old_stateCnt,
276 __unused thread_state_t new_state,
277 __unused mach_msg_type_number_t *new_stateCnt)
1c79356b
A
278{
279 return(KERN_INVALID_ARGUMENT);
280}
91447636 281
1c79356b 282kern_return_t
91447636
A
283catch_exception_raise_state_identity(
284 __unused mach_port_t exception_port,
285 __unused mach_port_t thread,
286 __unused mach_port_t task,
287 __unused exception_type_t exception,
288 __unused exception_data_t code,
289 __unused mach_msg_type_number_t codeCnt,
290 __unused int *flavor,
291 __unused thread_state_t old_state,
292 __unused mach_msg_type_number_t old_stateCnt,
293 __unused thread_state_t new_state,
294 __unused mach_msg_type_number_t *new_stateCnt)
1c79356b
A
295{
296 return(KERN_INVALID_ARGUMENT);
297}
298
1c79356b
A
299/*
300 * ux_exception translates a mach exception, code and subcode to
301 * a signal and u.u_code. Calls machine_exception (machine dependent)
302 * to attempt translation first.
303 */
304
305static
306void ux_exception(
307 int exception,
308 int code,
309 int subcode,
310 int *ux_signal,
311 int *ux_code
312)
313{
314 /*
315 * Try machine-dependent translation first.
316 */
317 if (machine_exception(exception, code, subcode, ux_signal, ux_code))
318 return;
319
320 switch(exception) {
321
322 case EXC_BAD_ACCESS:
323 if (code == KERN_INVALID_ADDRESS)
324 *ux_signal = SIGSEGV;
325 else
326 *ux_signal = SIGBUS;
327 break;
328
329 case EXC_BAD_INSTRUCTION:
330 *ux_signal = SIGILL;
331 break;
332
333 case EXC_ARITHMETIC:
334 *ux_signal = SIGFPE;
335 break;
336
337 case EXC_EMULATION:
338 *ux_signal = SIGEMT;
339 break;
340
341 case EXC_SOFTWARE:
342 switch (code) {
343
344 case EXC_UNIX_BAD_SYSCALL:
345 *ux_signal = SIGSYS;
346 break;
347 case EXC_UNIX_BAD_PIPE:
348 *ux_signal = SIGPIPE;
349 break;
350 case EXC_UNIX_ABORT:
351 *ux_signal = SIGABRT;
352 break;
9bccf70c
A
353 case EXC_SOFT_SIGNAL:
354 *ux_signal = SIGKILL;
355 break;
1c79356b
A
356 }
357 break;
358
359 case EXC_BREAKPOINT:
360 *ux_signal = SIGTRAP;
361 break;
362 }
363}