]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
3 | * | |
6601e61a | 4 | * @APPLE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
6601e61a 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. | |
8f6c56a5 | 11 | * |
6601e61a 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 | |
8f6c56a5 A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
6601e61a 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. | |
8f6c56a5 | 19 | * |
6601e61a | 20 | * @APPLE_LICENSE_HEADER_END@ |
1c79356b A |
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 | */ | |
60 | struct ipc_object; | |
61 | extern 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); | |
63 | extern 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); | |
68 | extern 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); | |
71 | extern thread_t convert_port_to_thread(ipc_port_t port); | |
72 | extern void ipc_port_release(ipc_port_t); | |
73 | ||
74 | ||
75 | ||
76 | ||
1c79356b A |
77 | /* |
78 | * Unix exception handler. | |
79 | */ | |
80 | ||
91447636 A |
81 | static void ux_exception(int exception, int code, int subcode, |
82 | int *ux_signal, int *ux_code); | |
1c79356b | 83 | |
1c79356b A |
84 | mach_port_name_t ux_exception_port; |
85 | static task_t ux_handler_self; | |
86 | ||
87 | static | |
88 | void | |
89 | ux_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 | ||
177 | void | |
178 | ux_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 | ||
188 | kern_return_t | |
189 | catch_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 | */ | |
0c530ab8 A |
237 | if (ux_signal != 0) { |
238 | ut->uu_exception = exception; | |
239 | //ut->uu_code = code[0]; // filled in by threadsignal | |
240 | ut->uu_subcode = code[1]; | |
241 | threadsignal(th_act, ux_signal, code[0]); | |
242 | } | |
1c79356b | 243 | |
91447636 | 244 | thread_deallocate(th_act); |
1c79356b A |
245 | } |
246 | else | |
247 | result = KERN_INVALID_ARGUMENT; | |
248 | } | |
249 | else | |
250 | result = KERN_INVALID_ARGUMENT; | |
251 | ||
252 | /* | |
253 | * Delete our send rights to the task and thread ports. | |
254 | */ | |
255 | (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self), task_name); | |
91447636 | 256 | (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self), thread_name); |
1c79356b A |
257 | |
258 | return (result); | |
259 | } | |
91447636 | 260 | |
1c79356b | 261 | kern_return_t |
91447636 A |
262 | catch_exception_raise_state( |
263 | __unused mach_port_t exception_port, | |
264 | __unused exception_type_t exception, | |
265 | __unused const exception_data_t code, | |
266 | __unused mach_msg_type_number_t codeCnt, | |
267 | __unused int *flavor, | |
268 | __unused const thread_state_t old_state, | |
269 | __unused mach_msg_type_number_t old_stateCnt, | |
270 | __unused thread_state_t new_state, | |
271 | __unused mach_msg_type_number_t *new_stateCnt) | |
1c79356b A |
272 | { |
273 | return(KERN_INVALID_ARGUMENT); | |
274 | } | |
91447636 | 275 | |
1c79356b | 276 | kern_return_t |
91447636 A |
277 | catch_exception_raise_state_identity( |
278 | __unused mach_port_t exception_port, | |
279 | __unused mach_port_t thread, | |
280 | __unused mach_port_t task, | |
281 | __unused exception_type_t exception, | |
282 | __unused exception_data_t code, | |
283 | __unused mach_msg_type_number_t codeCnt, | |
284 | __unused int *flavor, | |
285 | __unused thread_state_t old_state, | |
286 | __unused mach_msg_type_number_t old_stateCnt, | |
287 | __unused thread_state_t new_state, | |
288 | __unused mach_msg_type_number_t *new_stateCnt) | |
1c79356b A |
289 | { |
290 | return(KERN_INVALID_ARGUMENT); | |
291 | } | |
292 | ||
1c79356b A |
293 | /* |
294 | * ux_exception translates a mach exception, code and subcode to | |
295 | * a signal and u.u_code. Calls machine_exception (machine dependent) | |
296 | * to attempt translation first. | |
297 | */ | |
298 | ||
299 | static | |
300 | void ux_exception( | |
301 | int exception, | |
302 | int code, | |
303 | int subcode, | |
304 | int *ux_signal, | |
305 | int *ux_code | |
306 | ) | |
307 | { | |
308 | /* | |
309 | * Try machine-dependent translation first. | |
310 | */ | |
311 | if (machine_exception(exception, code, subcode, ux_signal, ux_code)) | |
312 | return; | |
313 | ||
314 | switch(exception) { | |
315 | ||
316 | case EXC_BAD_ACCESS: | |
317 | if (code == KERN_INVALID_ADDRESS) | |
318 | *ux_signal = SIGSEGV; | |
319 | else | |
320 | *ux_signal = SIGBUS; | |
321 | break; | |
322 | ||
323 | case EXC_BAD_INSTRUCTION: | |
324 | *ux_signal = SIGILL; | |
325 | break; | |
326 | ||
327 | case EXC_ARITHMETIC: | |
328 | *ux_signal = SIGFPE; | |
329 | break; | |
330 | ||
331 | case EXC_EMULATION: | |
332 | *ux_signal = SIGEMT; | |
333 | break; | |
334 | ||
335 | case EXC_SOFTWARE: | |
336 | switch (code) { | |
337 | ||
338 | case EXC_UNIX_BAD_SYSCALL: | |
339 | *ux_signal = SIGSYS; | |
340 | break; | |
341 | case EXC_UNIX_BAD_PIPE: | |
342 | *ux_signal = SIGPIPE; | |
343 | break; | |
344 | case EXC_UNIX_ABORT: | |
345 | *ux_signal = SIGABRT; | |
346 | break; | |
9bccf70c A |
347 | case EXC_SOFT_SIGNAL: |
348 | *ux_signal = SIGKILL; | |
349 | break; | |
1c79356b A |
350 | } |
351 | break; | |
352 | ||
353 | case EXC_BREAKPOINT: | |
354 | *ux_signal = SIGTRAP; | |
355 | break; | |
356 | } | |
357 | } |