]> git.saurik.com Git - apple/xnu.git/blame - bsd/uxkern/ux_exception.c
xnu-792.6.76.tar.gz
[apple/xnu.git] / bsd / uxkern / ux_exception.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
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.
27 */
28
29/*
30 *********************************************************************
31 * HISTORY
32 **********************************************************************
33 */
34
35#include <sys/param.h>
36
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>
91447636 42#include <mach/mach_port.h>
1c79356b 43#include <mach/mig_errors.h>
91447636 44#include <mach/exc_server.h>
1c79356b
A
45#include <kern/task.h>
46#include <kern/thread.h>
9bccf70c 47#include <kern/sched_prim.h>
1c79356b
A
48#include <kern/kalloc.h>
49
50#include <sys/proc.h>
51#include <sys/user.h>
52#include <sys/systm.h>
53#include <sys/ux_exception.h>
54
91447636
A
55#include <vm/vm_protos.h> /* get_task_ipcspace() */
56
57/*
58 * XXX Things that should be retrieved from Mach headers, but aren't
59 */
60struct ipc_object;
61extern kern_return_t ipc_object_copyin(ipc_space_t space, mach_port_name_t name,
62 mach_msg_type_name_t msgt_name, struct ipc_object **objectp);
63extern mach_msg_return_t mach_msg_receive(mach_msg_header_t *msg,
64 mach_msg_option_t option, mach_msg_size_t rcv_size,
65 mach_port_name_t rcv_name, mach_msg_timeout_t rcv_timeout,
66 void (*continuation)(mach_msg_return_t),
67 mach_msg_size_t slist_size);
68extern mach_msg_return_t mach_msg_send(mach_msg_header_t *msg,
69 mach_msg_option_t option, mach_msg_size_t send_size,
70 mach_msg_timeout_t send_timeout, mach_port_name_t notify);
71extern thread_t convert_port_to_thread(ipc_port_t port);
72extern void ipc_port_release(ipc_port_t);
73
74
75
76
1c79356b
A
77/*
78 * Unix exception handler.
79 */
80
91447636
A
81static void ux_exception(int exception, int code, int subcode,
82 int *ux_signal, int *ux_code);
1c79356b 83
1c79356b
A
84mach_port_name_t ux_exception_port;
85static task_t ux_handler_self;
86
87static
88void
89ux_handler(void)
90{
91 task_t self = current_task();
92 mach_port_name_t exc_port_name;
93 mach_port_name_t exc_set_name;
94
95 (void) thread_funnel_set(kernel_flock, TRUE);
96
97 /* self->kernel_vm_space = TRUE; */
98 ux_handler_self = self;
99
100
101 /*
102 * Allocate a port set that we will receive on.
103 */
104 if (mach_port_allocate(get_task_ipcspace(ux_handler_self), MACH_PORT_RIGHT_PORT_SET, &exc_set_name) != MACH_MSG_SUCCESS)
105 panic("ux_handler: port_set_allocate failed");
106
107 /*
108 * Allocate an exception port and use object_copyin to
109 * translate it to the global name. Put it into the set.
110 */
111 if (mach_port_allocate(get_task_ipcspace(ux_handler_self), MACH_PORT_RIGHT_RECEIVE, &exc_port_name) != MACH_MSG_SUCCESS)
112 panic("ux_handler: port_allocate failed");
113 if (mach_port_move_member(get_task_ipcspace(ux_handler_self),
114 exc_port_name, exc_set_name) != MACH_MSG_SUCCESS)
115 panic("ux_handler: port_set_add failed");
116
117 if (ipc_object_copyin(get_task_ipcspace(self), exc_port_name,
118 MACH_MSG_TYPE_MAKE_SEND,
119 (void *) &ux_exception_port) != MACH_MSG_SUCCESS)
120 panic("ux_handler: object_copyin(ux_exception_port) failed");
121
122 thread_wakeup(&ux_exception_port);
123
124 /* Message handling loop. */
125
126 for (;;) {
127 struct rep_msg {
128 mach_msg_header_t Head;
129 NDR_record_t NDR;
130 kern_return_t RetCode;
131 } rep_msg;
132 struct exc_msg {
133 mach_msg_header_t Head;
134 /* start of the kernel processed data */
135 mach_msg_body_t msgh_body;
136 mach_msg_port_descriptor_t thread;
137 mach_msg_port_descriptor_t task;
138 /* end of the kernel processed data */
139 NDR_record_t NDR;
140 exception_type_t exception;
141 mach_msg_type_number_t codeCnt;
142 exception_data_t code;
143 /* some times RCV_TO_LARGE probs */
144 char pad[512];
145 } exc_msg;
146 mach_port_name_t reply_port;
147 kern_return_t result;
148
149 exc_msg.Head.msgh_local_port = (mach_port_t)exc_set_name;
150 exc_msg.Head.msgh_size = sizeof (exc_msg);
151#if 0
152 result = mach_msg_receive(&exc_msg.Head);
153#else
154 result = mach_msg_receive(&exc_msg.Head, MACH_RCV_MSG,
155 sizeof (exc_msg), exc_set_name,
156 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL,
157 0);
158#endif
159 if (result == MACH_MSG_SUCCESS) {
160 reply_port = (mach_port_name_t)exc_msg.Head.msgh_remote_port;
161
162 if (exc_server(&exc_msg.Head, &rep_msg.Head))
163 (void) mach_msg_send(&rep_msg.Head, MACH_SEND_MSG,
164 sizeof (rep_msg),MACH_MSG_TIMEOUT_NONE,MACH_PORT_NULL);
165
166 if (reply_port != MACH_PORT_NULL)
167 (void) mach_port_deallocate(get_task_ipcspace(ux_handler_self), reply_port);
168 }
169 else if (result == MACH_RCV_TOO_LARGE)
170 /* ignore oversized messages */;
171 else
172 panic("exception_handler");
173 }
174 thread_funnel_set(kernel_flock, FALSE);
175}
176
177void
178ux_handler_init(void)
179{
1c79356b 180 ux_exception_port = MACH_PORT_NULL;
55e303ae 181 (void) kernel_thread(kernel_task, ux_handler);
1c79356b 182 if (ux_exception_port == MACH_PORT_NULL) {
1c79356b 183 assert_wait(&ux_exception_port, THREAD_UNINT);
9bccf70c 184 thread_block(THREAD_CONTINUE_NULL);
1c79356b 185 }
1c79356b
A
186}
187
188kern_return_t
189catch_exception_raise(
91447636
A
190 __unused mach_port_t exception_port,
191 mach_port_t thread,
192 mach_port_t task,
193 exception_type_t exception,
194 exception_data_t code,
195 __unused mach_msg_type_number_t codeCnt
1c79356b
A
196)
197{
198 task_t self = current_task();
91447636 199 thread_t th_act;
1c79356b 200 ipc_port_t thread_port;
1c79356b 201 kern_return_t result = MACH_MSG_SUCCESS;
91447636 202 int ux_signal = 0;
1c79356b
A
203 u_long ucode = 0;
204 struct uthread *ut;
91447636
A
205 mach_port_name_t thread_name = (mach_port_name_t)thread; /* XXX */
206 mach_port_name_t task_name = (mach_port_name_t)task; /* XXX */
1c79356b
A
207
208 /*
209 * Convert local thread name to global port.
210 */
211 if (MACH_PORT_VALID(thread_name) &&
212 (ipc_object_copyin(get_task_ipcspace(self), thread_name,
213 MACH_MSG_TYPE_PORT_SEND,
214 (void *) &thread_port) == MACH_MSG_SUCCESS)) {
9bccf70c 215 if (IPC_PORT_VALID(thread_port)) {
91447636 216 th_act = convert_port_to_thread(thread_port);
1c79356b
A
217 ipc_port_release(thread_port);
218 } else {
91447636 219 th_act = THREAD_NULL;
1c79356b
A
220 }
221
222 /*
223 * Catch bogus ports
224 */
91447636 225 if (th_act != THREAD_NULL) {
1c79356b
A
226
227 /*
228 * Convert exception to unix signal and code.
229 */
230 ut = get_bsdthread_info(th_act);
231 ux_exception(exception, code[0], code[1],
91447636 232 &ux_signal, (int *)&ucode);
1c79356b
A
233
234 /*
235 * Send signal.
236 */
91447636
A
237 if (ux_signal != 0)
238 threadsignal(th_act, ux_signal, ucode);
1c79356b 239
91447636 240 thread_deallocate(th_act);
1c79356b
A
241 }
242 else
243 result = KERN_INVALID_ARGUMENT;
244 }
245 else
246 result = KERN_INVALID_ARGUMENT;
247
248 /*
249 * Delete our send rights to the task and thread ports.
250 */
251 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self), task_name);
91447636 252 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self), thread_name);
1c79356b
A
253
254 return (result);
255}
91447636 256
1c79356b 257kern_return_t
91447636
A
258catch_exception_raise_state(
259 __unused mach_port_t exception_port,
260 __unused exception_type_t exception,
261 __unused const exception_data_t code,
262 __unused mach_msg_type_number_t codeCnt,
263 __unused int *flavor,
264 __unused const thread_state_t old_state,
265 __unused mach_msg_type_number_t old_stateCnt,
266 __unused thread_state_t new_state,
267 __unused mach_msg_type_number_t *new_stateCnt)
1c79356b
A
268{
269 return(KERN_INVALID_ARGUMENT);
270}
91447636 271
1c79356b 272kern_return_t
91447636
A
273catch_exception_raise_state_identity(
274 __unused mach_port_t exception_port,
275 __unused mach_port_t thread,
276 __unused mach_port_t task,
277 __unused exception_type_t exception,
278 __unused exception_data_t code,
279 __unused mach_msg_type_number_t codeCnt,
280 __unused int *flavor,
281 __unused thread_state_t old_state,
282 __unused mach_msg_type_number_t old_stateCnt,
283 __unused thread_state_t new_state,
284 __unused mach_msg_type_number_t *new_stateCnt)
1c79356b
A
285{
286 return(KERN_INVALID_ARGUMENT);
287}
288
1c79356b
A
289/*
290 * ux_exception translates a mach exception, code and subcode to
291 * a signal and u.u_code. Calls machine_exception (machine dependent)
292 * to attempt translation first.
293 */
294
295static
296void ux_exception(
297 int exception,
298 int code,
299 int subcode,
300 int *ux_signal,
301 int *ux_code
302)
303{
304 /*
305 * Try machine-dependent translation first.
306 */
307 if (machine_exception(exception, code, subcode, ux_signal, ux_code))
308 return;
309
310 switch(exception) {
311
312 case EXC_BAD_ACCESS:
313 if (code == KERN_INVALID_ADDRESS)
314 *ux_signal = SIGSEGV;
315 else
316 *ux_signal = SIGBUS;
317 break;
318
319 case EXC_BAD_INSTRUCTION:
320 *ux_signal = SIGILL;
321 break;
322
323 case EXC_ARITHMETIC:
324 *ux_signal = SIGFPE;
325 break;
326
327 case EXC_EMULATION:
328 *ux_signal = SIGEMT;
329 break;
330
331 case EXC_SOFTWARE:
332 switch (code) {
333
334 case EXC_UNIX_BAD_SYSCALL:
335 *ux_signal = SIGSYS;
336 break;
337 case EXC_UNIX_BAD_PIPE:
338 *ux_signal = SIGPIPE;
339 break;
340 case EXC_UNIX_ABORT:
341 *ux_signal = SIGABRT;
342 break;
9bccf70c
A
343 case EXC_SOFT_SIGNAL:
344 *ux_signal = SIGKILL;
345 break;
1c79356b
A
346 }
347 break;
348
349 case EXC_BREAKPOINT:
350 *ux_signal = SIGTRAP;
351 break;
352 }
353}