]> git.saurik.com Git - apple/xnu.git/blob - bsd/uxkern/ux_exception.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / uxkern / ux_exception.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
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.
35 */
36
37 /*
38 *********************************************************************
39 * HISTORY
40 **********************************************************************
41 */
42
43 #include <sys/param.h>
44
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>
57
58 #include <sys/proc.h>
59 #include <sys/user.h>
60 #include <sys/systm.h>
61 #include <sys/ux_exception.h>
62
63 #include <vm/vm_protos.h> /* get_task_ipcspace() */
64
65 /*
66 * XXX Things that should be retrieved from Mach headers, but aren't
67 */
68 struct ipc_object;
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);
81
82
83
84
85 /*
86 * Unix exception handler.
87 */
88
89 static void ux_exception(int exception, int code, int subcode,
90 int *ux_signal, int *ux_code);
91
92 mach_port_name_t ux_exception_port;
93 static task_t ux_handler_self;
94
95 static
96 void
97 ux_handler(void)
98 {
99 task_t self = current_task();
100 mach_port_name_t exc_port_name;
101 mach_port_name_t exc_set_name;
102
103 (void) thread_funnel_set(kernel_flock, TRUE);
104
105 /* self->kernel_vm_space = TRUE; */
106 ux_handler_self = self;
107
108
109 /*
110 * Allocate a port set that we will receive on.
111 */
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");
114
115 /*
116 * Allocate an exception port and use object_copyin to
117 * translate it to the global name. Put it into the set.
118 */
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");
124
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");
129
130 thread_wakeup(&ux_exception_port);
131
132 /* Message handling loop. */
133
134 for (;;) {
135 struct rep_msg {
136 mach_msg_header_t Head;
137 NDR_record_t NDR;
138 kern_return_t RetCode;
139 } rep_msg;
140 struct exc_msg {
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 */
147 NDR_record_t NDR;
148 exception_type_t exception;
149 mach_msg_type_number_t codeCnt;
150 exception_data_t code;
151 /* some times RCV_TO_LARGE probs */
152 char pad[512];
153 } exc_msg;
154 mach_port_name_t reply_port;
155 kern_return_t result;
156
157 exc_msg.Head.msgh_local_port = (mach_port_t)exc_set_name;
158 exc_msg.Head.msgh_size = sizeof (exc_msg);
159 #if 0
160 result = mach_msg_receive(&exc_msg.Head);
161 #else
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,
165 0);
166 #endif
167 if (result == MACH_MSG_SUCCESS) {
168 reply_port = (mach_port_name_t)exc_msg.Head.msgh_remote_port;
169
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);
173
174 if (reply_port != MACH_PORT_NULL)
175 (void) mach_port_deallocate(get_task_ipcspace(ux_handler_self), reply_port);
176 }
177 else if (result == MACH_RCV_TOO_LARGE)
178 /* ignore oversized messages */;
179 else
180 panic("exception_handler");
181 }
182 thread_funnel_set(kernel_flock, FALSE);
183 }
184
185 void
186 ux_handler_init(void)
187 {
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);
193 }
194 }
195
196 kern_return_t
197 catch_exception_raise(
198 __unused mach_port_t exception_port,
199 mach_port_t thread,
200 mach_port_t task,
201 exception_type_t exception,
202 exception_data_t code,
203 __unused mach_msg_type_number_t codeCnt
204 )
205 {
206 task_t self = current_task();
207 thread_t th_act;
208 ipc_port_t thread_port;
209 kern_return_t result = MACH_MSG_SUCCESS;
210 int ux_signal = 0;
211 u_long ucode = 0;
212 struct uthread *ut;
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 */
215
216 /*
217 * Convert local thread name to global port.
218 */
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);
226 } else {
227 th_act = THREAD_NULL;
228 }
229
230 /*
231 * Catch bogus ports
232 */
233 if (th_act != THREAD_NULL) {
234
235 /*
236 * Convert exception to unix signal and code.
237 */
238 ut = get_bsdthread_info(th_act);
239 ux_exception(exception, code[0], code[1],
240 &ux_signal, (int *)&ucode);
241
242 /*
243 * Send signal.
244 */
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]);
250 }
251
252 thread_deallocate(th_act);
253 }
254 else
255 result = KERN_INVALID_ARGUMENT;
256 }
257 else
258 result = KERN_INVALID_ARGUMENT;
259
260 /*
261 * Delete our send rights to the task and thread ports.
262 */
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);
265
266 return (result);
267 }
268
269 kern_return_t
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)
280 {
281 return(KERN_INVALID_ARGUMENT);
282 }
283
284 kern_return_t
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)
297 {
298 return(KERN_INVALID_ARGUMENT);
299 }
300
301 /*
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.
305 */
306
307 static
308 void ux_exception(
309 int exception,
310 int code,
311 int subcode,
312 int *ux_signal,
313 int *ux_code
314 )
315 {
316 /*
317 * Try machine-dependent translation first.
318 */
319 if (machine_exception(exception, code, subcode, ux_signal, ux_code))
320 return;
321
322 switch(exception) {
323
324 case EXC_BAD_ACCESS:
325 if (code == KERN_INVALID_ADDRESS)
326 *ux_signal = SIGSEGV;
327 else
328 *ux_signal = SIGBUS;
329 break;
330
331 case EXC_BAD_INSTRUCTION:
332 *ux_signal = SIGILL;
333 break;
334
335 case EXC_ARITHMETIC:
336 *ux_signal = SIGFPE;
337 break;
338
339 case EXC_EMULATION:
340 *ux_signal = SIGEMT;
341 break;
342
343 case EXC_SOFTWARE:
344 switch (code) {
345
346 case EXC_UNIX_BAD_SYSCALL:
347 *ux_signal = SIGSYS;
348 break;
349 case EXC_UNIX_BAD_PIPE:
350 *ux_signal = SIGPIPE;
351 break;
352 case EXC_UNIX_ABORT:
353 *ux_signal = SIGABRT;
354 break;
355 case EXC_SOFT_SIGNAL:
356 *ux_signal = SIGKILL;
357 break;
358 }
359 break;
360
361 case EXC_BREAKPOINT:
362 *ux_signal = SIGTRAP;
363 break;
364 }
365 }