]> git.saurik.com Git - apple/xnu.git/blob - bsd/uxkern/ux_exception.c
xnu-517.11.1.tar.gz
[apple/xnu.git] / bsd / uxkern / ux_exception.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
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>
42 #include <mach/mig_errors.h>
43 #include <kern/task.h>
44 #include <kern/thread.h>
45 #include <kern/sched_prim.h>
46 #include <kern/thread_act.h>
47 #include <kern/kalloc.h>
48
49 #include <sys/proc.h>
50 #include <sys/user.h>
51 #include <sys/systm.h>
52 #include <sys/ux_exception.h>
53
54 /*
55 * Unix exception handler.
56 */
57
58 static void ux_exception();
59
60 decl_simple_lock_data(static, ux_handler_init_lock)
61 mach_port_name_t ux_exception_port;
62 static task_t ux_handler_self;
63
64 static
65 void
66 ux_handler(void)
67 {
68 task_t self = current_task();
69 mach_port_name_t exc_port_name;
70 mach_port_name_t exc_set_name;
71
72 (void) thread_funnel_set(kernel_flock, TRUE);
73
74 /* self->kernel_vm_space = TRUE; */
75 ux_handler_self = self;
76
77
78 /*
79 * Allocate a port set that we will receive on.
80 */
81 if (mach_port_allocate(get_task_ipcspace(ux_handler_self), MACH_PORT_RIGHT_PORT_SET, &exc_set_name) != MACH_MSG_SUCCESS)
82 panic("ux_handler: port_set_allocate failed");
83
84 /*
85 * Allocate an exception port and use object_copyin to
86 * translate it to the global name. Put it into the set.
87 */
88 if (mach_port_allocate(get_task_ipcspace(ux_handler_self), MACH_PORT_RIGHT_RECEIVE, &exc_port_name) != MACH_MSG_SUCCESS)
89 panic("ux_handler: port_allocate failed");
90 if (mach_port_move_member(get_task_ipcspace(ux_handler_self),
91 exc_port_name, exc_set_name) != MACH_MSG_SUCCESS)
92 panic("ux_handler: port_set_add failed");
93
94 if (ipc_object_copyin(get_task_ipcspace(self), exc_port_name,
95 MACH_MSG_TYPE_MAKE_SEND,
96 (void *) &ux_exception_port) != MACH_MSG_SUCCESS)
97 panic("ux_handler: object_copyin(ux_exception_port) failed");
98
99 thread_wakeup(&ux_exception_port);
100
101 /* Message handling loop. */
102
103 for (;;) {
104 struct rep_msg {
105 mach_msg_header_t Head;
106 NDR_record_t NDR;
107 kern_return_t RetCode;
108 } rep_msg;
109 struct exc_msg {
110 mach_msg_header_t Head;
111 /* start of the kernel processed data */
112 mach_msg_body_t msgh_body;
113 mach_msg_port_descriptor_t thread;
114 mach_msg_port_descriptor_t task;
115 /* end of the kernel processed data */
116 NDR_record_t NDR;
117 exception_type_t exception;
118 mach_msg_type_number_t codeCnt;
119 exception_data_t code;
120 /* some times RCV_TO_LARGE probs */
121 char pad[512];
122 } exc_msg;
123 mach_port_name_t reply_port;
124 kern_return_t result;
125
126 exc_msg.Head.msgh_local_port = (mach_port_t)exc_set_name;
127 exc_msg.Head.msgh_size = sizeof (exc_msg);
128 #if 0
129 result = mach_msg_receive(&exc_msg.Head);
130 #else
131 result = mach_msg_receive(&exc_msg.Head, MACH_RCV_MSG,
132 sizeof (exc_msg), exc_set_name,
133 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL,
134 0);
135 #endif
136 if (result == MACH_MSG_SUCCESS) {
137 reply_port = (mach_port_name_t)exc_msg.Head.msgh_remote_port;
138
139 if (exc_server(&exc_msg.Head, &rep_msg.Head))
140 (void) mach_msg_send(&rep_msg.Head, MACH_SEND_MSG,
141 sizeof (rep_msg),MACH_MSG_TIMEOUT_NONE,MACH_PORT_NULL);
142
143 if (reply_port != MACH_PORT_NULL)
144 (void) mach_port_deallocate(get_task_ipcspace(ux_handler_self), reply_port);
145 }
146 else if (result == MACH_RCV_TOO_LARGE)
147 /* ignore oversized messages */;
148 else
149 panic("exception_handler");
150 }
151 thread_funnel_set(kernel_flock, FALSE);
152 }
153
154 void
155 ux_handler_init(void)
156 {
157 simple_lock_init(&ux_handler_init_lock);
158 ux_exception_port = MACH_PORT_NULL;
159 (void) kernel_thread(kernel_task, ux_handler);
160 simple_lock(&ux_handler_init_lock);
161 if (ux_exception_port == MACH_PORT_NULL) {
162 simple_unlock(&ux_handler_init_lock);
163 assert_wait(&ux_exception_port, THREAD_UNINT);
164 thread_block(THREAD_CONTINUE_NULL);
165 }
166 else
167 simple_unlock(&ux_handler_init_lock);
168 }
169
170 kern_return_t
171 catch_exception_raise(
172 mach_port_name_t exception_port,
173 mach_port_name_t thread_name,
174 mach_port_name_t task_name,
175 int exception,
176 exception_data_t code,
177 mach_msg_type_number_t codecnt
178 )
179 {
180 task_t self = current_task();
181 thread_act_t th_act;
182 ipc_port_t thread_port;
183 ipc_port_t task_port;
184 kern_return_t result = MACH_MSG_SUCCESS;
185 int signal = 0;
186 u_long ucode = 0;
187 struct uthread *ut;
188
189 /*
190 * Convert local thread name to global port.
191 */
192 if (MACH_PORT_VALID(thread_name) &&
193 (ipc_object_copyin(get_task_ipcspace(self), thread_name,
194 MACH_MSG_TYPE_PORT_SEND,
195 (void *) &thread_port) == MACH_MSG_SUCCESS)) {
196 if (IPC_PORT_VALID(thread_port)) {
197 th_act = (thread_act_t)convert_port_to_act(thread_port);
198 ipc_port_release(thread_port);
199 } else {
200 th_act = THR_ACT_NULL;
201 }
202
203 /*
204 * Catch bogus ports
205 */
206 if (th_act != THR_ACT_NULL) {
207
208 /*
209 * Convert exception to unix signal and code.
210 */
211 ut = get_bsdthread_info(th_act);
212 ux_exception(exception, code[0], code[1],
213 &signal, &ucode);
214
215 /*
216 * Send signal.
217 */
218 if (signal != 0)
219 threadsignal(th_act, signal, ucode);
220
221 act_deallocate(th_act);
222 }
223 else
224 result = KERN_INVALID_ARGUMENT;
225 }
226 else
227 result = KERN_INVALID_ARGUMENT;
228
229 /*
230 * Delete our send rights to the task and thread ports.
231 */
232 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self), task_name);
233 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self),thread_name);
234
235 return (result);
236 }
237 kern_return_t
238 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)
239 {
240 return(KERN_INVALID_ARGUMENT);
241 }
242 kern_return_t
243 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)
244 {
245 return(KERN_INVALID_ARGUMENT);
246 }
247
248 boolean_t machine_exception();
249
250 /*
251 * ux_exception translates a mach exception, code and subcode to
252 * a signal and u.u_code. Calls machine_exception (machine dependent)
253 * to attempt translation first.
254 */
255
256 static
257 void ux_exception(
258 int exception,
259 int code,
260 int subcode,
261 int *ux_signal,
262 int *ux_code
263 )
264 {
265 /*
266 * Try machine-dependent translation first.
267 */
268 if (machine_exception(exception, code, subcode, ux_signal, ux_code))
269 return;
270
271 switch(exception) {
272
273 case EXC_BAD_ACCESS:
274 if (code == KERN_INVALID_ADDRESS)
275 *ux_signal = SIGSEGV;
276 else
277 *ux_signal = SIGBUS;
278 break;
279
280 case EXC_BAD_INSTRUCTION:
281 *ux_signal = SIGILL;
282 break;
283
284 case EXC_ARITHMETIC:
285 *ux_signal = SIGFPE;
286 break;
287
288 case EXC_EMULATION:
289 *ux_signal = SIGEMT;
290 break;
291
292 case EXC_SOFTWARE:
293 switch (code) {
294
295 case EXC_UNIX_BAD_SYSCALL:
296 *ux_signal = SIGSYS;
297 break;
298 case EXC_UNIX_BAD_PIPE:
299 *ux_signal = SIGPIPE;
300 break;
301 case EXC_UNIX_ABORT:
302 *ux_signal = SIGABRT;
303 break;
304 case EXC_SOFT_SIGNAL:
305 *ux_signal = SIGKILL;
306 break;
307 }
308 break;
309
310 case EXC_BREAKPOINT:
311 *ux_signal = SIGTRAP;
312 break;
313 }
314 }