]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990,1989 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
36 | * Permission to use, copy, modify and distribute this software and its | |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
41 | * | |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
2d21ac55 A |
56 | /* |
57 | * NOTICE: This file was modified by McAfee Research in 2004 to introduce | |
58 | * support for mandatory and extensible security protections. This notice | |
59 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
60 | * Version 2.0. | |
61 | * Copyright (c) 2005 SPARTA, Inc. | |
62 | */ | |
1c79356b A |
63 | /* |
64 | */ | |
65 | /* | |
66 | * File: ipc/mach_msg.c | |
67 | * Author: Rich Draves | |
68 | * Date: 1989 | |
69 | * | |
70 | * Exported message traps. See mach/message.h. | |
71 | */ | |
72 | ||
91447636 | 73 | #include <mach/mach_types.h> |
1c79356b A |
74 | #include <mach/kern_return.h> |
75 | #include <mach/port.h> | |
76 | #include <mach/message.h> | |
77 | #include <mach/mig_errors.h> | |
91447636 A |
78 | #include <mach/mach_traps.h> |
79 | ||
80 | #include <kern/kern_types.h> | |
1c79356b A |
81 | #include <kern/assert.h> |
82 | #include <kern/counters.h> | |
83 | #include <kern/cpu_number.h> | |
91447636 A |
84 | #include <kern/ipc_kobject.h> |
85 | #include <kern/ipc_mig.h> | |
1c79356b A |
86 | #include <kern/task.h> |
87 | #include <kern/thread.h> | |
1c79356b A |
88 | #include <kern/sched_prim.h> |
89 | #include <kern/exception.h> | |
90 | #include <kern/misc_protos.h> | |
91447636 A |
91 | #include <kern/kalloc.h> |
92 | #include <kern/processor.h> | |
93 | #include <kern/syscall_subr.h> | |
94 | ||
1c79356b | 95 | #include <vm/vm_map.h> |
91447636 A |
96 | |
97 | #include <ipc/ipc_types.h> | |
1c79356b A |
98 | #include <ipc/ipc_kmsg.h> |
99 | #include <ipc/ipc_mqueue.h> | |
100 | #include <ipc/ipc_object.h> | |
101 | #include <ipc/ipc_notify.h> | |
102 | #include <ipc/ipc_port.h> | |
103 | #include <ipc/ipc_pset.h> | |
104 | #include <ipc/ipc_space.h> | |
105 | #include <ipc/ipc_entry.h> | |
fe8ab488 | 106 | #include <ipc/ipc_importance.h> |
0b4e3aa0 | 107 | |
1c79356b | 108 | #include <machine/machine_routines.h> |
2d21ac55 | 109 | #include <security/mac_mach_internal.h> |
91447636 | 110 | |
1c79356b A |
111 | #include <sys/kdebug.h> |
112 | ||
91447636 A |
113 | #ifndef offsetof |
114 | #define offsetof(type, member) ((size_t)(&((type *)0)->member)) | |
115 | #endif /* offsetof */ | |
116 | ||
1c79356b | 117 | /* |
91447636 | 118 | * Forward declarations - kernel internal routines |
1c79356b A |
119 | */ |
120 | ||
121 | mach_msg_return_t mach_msg_send( | |
122 | mach_msg_header_t *msg, | |
123 | mach_msg_option_t option, | |
124 | mach_msg_size_t send_size, | |
91447636 | 125 | mach_msg_timeout_t send_timeout, |
1c79356b A |
126 | mach_port_name_t notify); |
127 | ||
128 | mach_msg_return_t mach_msg_receive( | |
129 | mach_msg_header_t *msg, | |
130 | mach_msg_option_t option, | |
131 | mach_msg_size_t rcv_size, | |
132 | mach_port_name_t rcv_name, | |
91447636 | 133 | mach_msg_timeout_t rcv_timeout, |
1c79356b A |
134 | void (*continuation)(mach_msg_return_t), |
135 | mach_msg_size_t slist_size); | |
136 | ||
137 | ||
138 | mach_msg_return_t msg_receive_error( | |
139 | ipc_kmsg_t kmsg, | |
91447636 | 140 | mach_vm_address_t msg_addr, |
1c79356b A |
141 | mach_msg_option_t option, |
142 | mach_port_seqno_t seqno, | |
143 | ipc_space_t space); | |
144 | ||
1c79356b | 145 | security_token_t KERNEL_SECURITY_TOKEN = KERNEL_SECURITY_TOKEN_VALUE; |
55e303ae | 146 | audit_token_t KERNEL_AUDIT_TOKEN = KERNEL_AUDIT_TOKEN_VALUE; |
1c79356b A |
147 | |
148 | mach_msg_format_0_trailer_t trailer_template = { | |
149 | /* mach_msg_trailer_type_t */ MACH_MSG_TRAILER_FORMAT_0, | |
150 | /* mach_msg_trailer_size_t */ MACH_MSG_TRAILER_MINIMUM_SIZE, | |
151 | /* mach_port_seqno_t */ 0, | |
152 | /* security_token_t */ KERNEL_SECURITY_TOKEN_VALUE | |
153 | }; | |
154 | ||
155 | /* | |
39236c6e | 156 | * Routine: mach_msg_send [Kernel Internal] |
1c79356b | 157 | * Purpose: |
39236c6e A |
158 | * Routine for kernel-task threads to send a message. |
159 | * | |
160 | * Unlike mach_msg_send_from_kernel(), this routine | |
161 | * looks port names up in the kernel's port namespace | |
162 | * and copies in the kernel virtual memory (instead | |
163 | * of taking a vm_map_copy_t pointer for OOL descriptors). | |
1c79356b A |
164 | * Conditions: |
165 | * Nothing locked. | |
166 | * Returns: | |
167 | * MACH_MSG_SUCCESS Sent the message. | |
168 | * MACH_SEND_MSG_TOO_SMALL Message smaller than a header. | |
169 | * MACH_SEND_NO_BUFFER Couldn't allocate buffer. | |
170 | * MACH_SEND_INVALID_DATA Couldn't copy message data. | |
171 | * MACH_SEND_INVALID_HEADER | |
172 | * Illegal value in the message header bits. | |
173 | * MACH_SEND_INVALID_DEST The space is dead. | |
174 | * MACH_SEND_INVALID_NOTIFY Bad notify port. | |
175 | * MACH_SEND_INVALID_DEST Can't copyin destination port. | |
176 | * MACH_SEND_INVALID_REPLY Can't copyin reply port. | |
177 | * MACH_SEND_TIMED_OUT Timeout expired without delivery. | |
178 | * MACH_SEND_INTERRUPTED Delivery interrupted. | |
1c79356b A |
179 | */ |
180 | ||
181 | mach_msg_return_t | |
182 | mach_msg_send( | |
183 | mach_msg_header_t *msg, | |
184 | mach_msg_option_t option, | |
185 | mach_msg_size_t send_size, | |
91447636 | 186 | mach_msg_timeout_t send_timeout, |
6d2010ae | 187 | __unused mach_port_name_t notify) |
1c79356b A |
188 | { |
189 | ipc_space_t space = current_space(); | |
190 | vm_map_t map = current_map(); | |
191 | ipc_kmsg_t kmsg; | |
192 | mach_msg_return_t mr; | |
91447636 A |
193 | mach_msg_size_t msg_and_trailer_size; |
194 | mach_msg_max_trailer_t *trailer; | |
1c79356b | 195 | |
91447636 A |
196 | if ((send_size < sizeof(mach_msg_header_t)) || (send_size & 3)) |
197 | return MACH_SEND_MSG_TOO_SMALL; | |
1c79356b | 198 | |
8ad349bb A |
199 | if (send_size > MACH_MSG_SIZE_MAX - MAX_TRAILER_SIZE) |
200 | return MACH_SEND_TOO_LARGE; | |
201 | ||
91447636 A |
202 | msg_and_trailer_size = send_size + MAX_TRAILER_SIZE; |
203 | ||
204 | kmsg = ipc_kmsg_alloc(msg_and_trailer_size); | |
205 | ||
206 | if (kmsg == IKM_NULL) | |
207 | return MACH_SEND_NO_BUFFER; | |
208 | ||
209 | (void) memcpy((void *) kmsg->ikm_header, (const void *) msg, send_size); | |
1c79356b | 210 | |
91447636 A |
211 | kmsg->ikm_header->msgh_size = send_size; |
212 | ||
213 | /* | |
214 | * reserve for the trailer the largest space (MAX_TRAILER_SIZE) | |
215 | * However, the internal size field of the trailer (msgh_trailer_size) | |
216 | * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize | |
217 | * the cases where no implicit data is requested. | |
218 | */ | |
219 | trailer = (mach_msg_max_trailer_t *) ((vm_offset_t)kmsg->ikm_header + send_size); | |
220 | trailer->msgh_sender = current_thread()->task->sec_token; | |
221 | trailer->msgh_audit = current_thread()->task->audit_token; | |
222 | trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0; | |
223 | trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE; | |
39236c6e A |
224 | |
225 | mr = ipc_kmsg_copyin(kmsg, space, map, &option); | |
226 | ||
1c79356b A |
227 | if (mr != MACH_MSG_SUCCESS) { |
228 | ipc_kmsg_free(kmsg); | |
229 | return mr; | |
230 | } | |
231 | ||
39236c6e A |
232 | mr = ipc_kmsg_send(kmsg, option, send_timeout); |
233 | ||
1c79356b A |
234 | if (mr != MACH_MSG_SUCCESS) { |
235 | mr |= ipc_kmsg_copyout_pseudo(kmsg, space, map, MACH_MSG_BODY_NULL); | |
91447636 A |
236 | (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, |
237 | kmsg->ikm_header->msgh_size); | |
238 | ipc_kmsg_free(kmsg); | |
1c79356b A |
239 | } |
240 | ||
241 | return mr; | |
242 | } | |
243 | ||
39236c6e A |
244 | /* |
245 | * message header as seen at user-space | |
246 | * (for MACH_RCV_LARGE/IDENTITY updating) | |
247 | */ | |
248 | typedef struct | |
249 | { | |
250 | mach_msg_bits_t msgh_bits; | |
251 | mach_msg_size_t msgh_size; | |
252 | mach_port_name_t msgh_remote_port; | |
253 | mach_port_name_t msgh_local_port; | |
254 | mach_msg_size_t msgh_reserved; | |
255 | mach_msg_id_t msgh_id; | |
256 | } mach_msg_user_header_t; | |
257 | ||
1c79356b | 258 | /* |
6d2010ae | 259 | * Routine: mach_msg_receive_results |
1c79356b A |
260 | * Purpose: |
261 | * Receive a message. | |
262 | * Conditions: | |
263 | * Nothing locked. | |
264 | * Returns: | |
265 | * MACH_MSG_SUCCESS Received a message. | |
266 | * MACH_RCV_INVALID_NAME The name doesn't denote a right, | |
267 | * or the denoted right is not receive or port set. | |
268 | * MACH_RCV_IN_SET Receive right is a member of a set. | |
269 | * MACH_RCV_TOO_LARGE Message wouldn't fit into buffer. | |
270 | * MACH_RCV_TIMED_OUT Timeout expired without a message. | |
271 | * MACH_RCV_INTERRUPTED Reception interrupted. | |
272 | * MACH_RCV_PORT_DIED Port/set died while receiving. | |
273 | * MACH_RCV_PORT_CHANGED Port moved into set while receiving. | |
274 | * MACH_RCV_INVALID_DATA Couldn't copy to user buffer. | |
275 | * MACH_RCV_INVALID_NOTIFY Bad notify port. | |
276 | * MACH_RCV_HEADER_ERROR | |
277 | */ | |
278 | ||
279 | mach_msg_return_t | |
280 | mach_msg_receive_results(void) | |
281 | { | |
282 | thread_t self = current_thread(); | |
283 | ipc_space_t space = current_space(); | |
316670eb | 284 | vm_map_t map = current_map(); |
1c79356b A |
285 | |
286 | ipc_object_t object = self->ith_object; | |
287 | mach_msg_return_t mr = self->ith_state; | |
91447636 | 288 | mach_vm_address_t msg_addr = self->ith_msg_addr; |
1c79356b A |
289 | mach_msg_option_t option = self->ith_option; |
290 | ipc_kmsg_t kmsg = self->ith_kmsg; | |
291 | mach_port_seqno_t seqno = self->ith_seqno; | |
316670eb | 292 | mach_msg_trailer_size_t trailer_size; |
1c79356b | 293 | |
316670eb | 294 | io_release(object); |
1c79356b A |
295 | |
296 | if (mr != MACH_MSG_SUCCESS) { | |
297 | ||
298 | if (mr == MACH_RCV_TOO_LARGE ) { | |
299 | if (option & MACH_RCV_LARGE) { | |
300 | /* | |
301 | * We need to inform the user-level code that it needs more | |
302 | * space. The value for how much space was returned in the | |
303 | * msize save area instead of the message (which was left on | |
304 | * the queue). | |
305 | */ | |
39236c6e A |
306 | if (option & MACH_RCV_LARGE_IDENTITY) { |
307 | if (copyout((char *) &self->ith_receiver_name, | |
308 | msg_addr + offsetof(mach_msg_user_header_t, msgh_local_port), | |
309 | sizeof(mach_port_name_t))) | |
310 | mr = MACH_RCV_INVALID_DATA; | |
311 | } | |
1c79356b | 312 | if (copyout((char *) &self->ith_msize, |
39236c6e | 313 | msg_addr + offsetof(mach_msg_user_header_t, msgh_size), |
1c79356b | 314 | sizeof(mach_msg_size_t))) |
fe8ab488 A |
315 | mr = MACH_RCV_INVALID_DATA; |
316 | } else { | |
317 | ||
318 | /* discard importance in message */ | |
319 | ipc_importance_clean(kmsg); | |
320 | ||
321 | if (msg_receive_error(kmsg, msg_addr, option, seqno, space) | |
322 | == MACH_RCV_INVALID_DATA) | |
323 | mr = MACH_RCV_INVALID_DATA; | |
1c79356b | 324 | } |
1c79356b | 325 | } |
fe8ab488 | 326 | return mr; |
1c79356b A |
327 | } |
328 | ||
39236c6e | 329 | #if IMPORTANCE_INHERITANCE |
39236c6e | 330 | |
fe8ab488 A |
331 | /* adopt/transform any importance attributes carried in the message */ |
332 | ipc_importance_receive(kmsg, option); | |
333 | ||
39236c6e A |
334 | #endif /* IMPORTANCE_INHERITANCE */ |
335 | ||
316670eb A |
336 | trailer_size = ipc_kmsg_add_trailer(kmsg, space, option, self, seqno, FALSE, |
337 | kmsg->ikm_header->msgh_remote_port->ip_context); | |
1c79356b A |
338 | /* |
339 | * If MACH_RCV_OVERWRITE was specified, try to get the scatter | |
340 | * list and verify it against the contents of the message. If | |
341 | * there is any problem with it, we will continue without it as | |
342 | * normal. | |
343 | */ | |
344 | if (option & MACH_RCV_OVERWRITE) { | |
345 | mach_msg_size_t slist_size = self->ith_scatter_list_size; | |
346 | mach_msg_body_t *slist; | |
347 | ||
91447636 | 348 | slist = ipc_kmsg_get_scatter(msg_addr, slist_size, kmsg); |
fe8ab488 | 349 | mr = ipc_kmsg_copyout(kmsg, space, map, slist, option); |
1c79356b A |
350 | ipc_kmsg_free_scatter(slist, slist_size); |
351 | } else { | |
fe8ab488 | 352 | mr = ipc_kmsg_copyout(kmsg, space, map, MACH_MSG_BODY_NULL, option); |
1c79356b A |
353 | } |
354 | ||
355 | if (mr != MACH_MSG_SUCCESS) { | |
fe8ab488 A |
356 | /* already received importance, so have to undo that here */ |
357 | ipc_importance_unreceive(kmsg, option); | |
358 | ||
1c79356b | 359 | if ((mr &~ MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) { |
91447636 | 360 | if (ipc_kmsg_put(msg_addr, kmsg, kmsg->ikm_header->msgh_size + |
316670eb | 361 | trailer_size) == MACH_RCV_INVALID_DATA) |
1c79356b A |
362 | mr = MACH_RCV_INVALID_DATA; |
363 | } | |
364 | else { | |
91447636 | 365 | if (msg_receive_error(kmsg, msg_addr, option, seqno, space) |
1c79356b A |
366 | == MACH_RCV_INVALID_DATA) |
367 | mr = MACH_RCV_INVALID_DATA; | |
368 | } | |
fe8ab488 A |
369 | } else { |
370 | mr = ipc_kmsg_put(msg_addr, | |
371 | kmsg, | |
372 | kmsg->ikm_header->msgh_size + | |
373 | trailer_size); | |
1c79356b | 374 | } |
fe8ab488 | 375 | |
1c79356b A |
376 | return mr; |
377 | } | |
378 | ||
39236c6e A |
379 | /* |
380 | * Routine: mach_msg_receive [Kernel Internal] | |
381 | * Purpose: | |
382 | * Routine for kernel-task threads to actively receive a message. | |
383 | * | |
384 | * Unlike being dispatched to by ipc_kobject_server() or the | |
385 | * reply part of mach_msg_rpc_from_kernel(), this routine | |
386 | * looks up the receive port name in the kernel's port | |
387 | * namespace and copies out received port rights to that namespace | |
388 | * as well. Out-of-line memory is copied out the kernel's | |
389 | * address space (rather than just providing the vm_map_copy_t). | |
390 | * Conditions: | |
391 | * Nothing locked. | |
392 | * Returns: | |
393 | * MACH_MSG_SUCCESS Received a message. | |
394 | * See <mach/message.h> for list of MACH_RCV_XXX errors. | |
395 | */ | |
1c79356b A |
396 | mach_msg_return_t |
397 | mach_msg_receive( | |
398 | mach_msg_header_t *msg, | |
399 | mach_msg_option_t option, | |
400 | mach_msg_size_t rcv_size, | |
401 | mach_port_name_t rcv_name, | |
91447636 | 402 | mach_msg_timeout_t rcv_timeout, |
1c79356b A |
403 | void (*continuation)(mach_msg_return_t), |
404 | mach_msg_size_t slist_size) | |
405 | { | |
406 | thread_t self = current_thread(); | |
407 | ipc_space_t space = current_space(); | |
1c79356b A |
408 | ipc_object_t object; |
409 | ipc_mqueue_t mqueue; | |
1c79356b | 410 | mach_msg_return_t mr; |
1c79356b A |
411 | |
412 | mr = ipc_mqueue_copyin(space, rcv_name, &mqueue, &object); | |
413 | if (mr != MACH_MSG_SUCCESS) { | |
414 | return mr; | |
415 | } | |
416 | /* hold ref for object */ | |
417 | ||
91447636 | 418 | self->ith_msg_addr = CAST_DOWN(mach_vm_address_t, msg); |
1c79356b A |
419 | self->ith_object = object; |
420 | self->ith_msize = rcv_size; | |
421 | self->ith_option = option; | |
422 | self->ith_scatter_list_size = slist_size; | |
423 | self->ith_continuation = continuation; | |
424 | ||
91447636 A |
425 | ipc_mqueue_receive(mqueue, option, rcv_size, rcv_timeout, THREAD_ABORTSAFE); |
426 | if ((option & MACH_RCV_TIMEOUT) && rcv_timeout == 0) | |
427 | thread_poll_yield(self); | |
1c79356b A |
428 | return mach_msg_receive_results(); |
429 | } | |
430 | ||
431 | void | |
432 | mach_msg_receive_continue(void) | |
433 | { | |
434 | thread_t self = current_thread(); | |
435 | ||
436 | (*self->ith_continuation)(mach_msg_receive_results()); | |
437 | } | |
438 | ||
1c79356b A |
439 | |
440 | /* | |
441 | * Routine: mach_msg_overwrite_trap [mach trap] | |
442 | * Purpose: | |
443 | * Possibly send a message; possibly receive a message. | |
444 | * Conditions: | |
445 | * Nothing locked. | |
446 | * Returns: | |
447 | * All of mach_msg_send and mach_msg_receive error codes. | |
448 | */ | |
449 | ||
450 | mach_msg_return_t | |
451 | mach_msg_overwrite_trap( | |
91447636 | 452 | struct mach_msg_overwrite_trap_args *args) |
1c79356b | 453 | { |
91447636 A |
454 | mach_vm_address_t msg_addr = args->msg; |
455 | mach_msg_option_t option = args->option; | |
456 | mach_msg_size_t send_size = args->send_size; | |
457 | mach_msg_size_t rcv_size = args->rcv_size; | |
458 | mach_port_name_t rcv_name = args->rcv_name; | |
459 | mach_msg_timeout_t msg_timeout = args->timeout; | |
6d2010ae | 460 | __unused mach_port_name_t notify = args->notify; |
91447636 A |
461 | mach_vm_address_t rcv_msg_addr = args->rcv_msg; |
462 | mach_msg_size_t scatter_list_size = 0; /* NOT INITIALIZED - but not used in pactice */ | |
2d21ac55 | 463 | __unused mach_port_seqno_t temp_seqno = 0; |
91447636 | 464 | |
1c79356b | 465 | mach_msg_return_t mr = MACH_MSG_SUCCESS; |
b0d623f7 | 466 | vm_map_t map = current_map(); |
39236c6e A |
467 | |
468 | /* Only accept options allowed by the user */ | |
469 | option &= MACH_MSG_OPTION_USER; | |
470 | ||
1c79356b | 471 | if (option & MACH_SEND_MSG) { |
91447636 | 472 | ipc_space_t space = current_space(); |
91447636 A |
473 | ipc_kmsg_t kmsg; |
474 | ||
475 | mr = ipc_kmsg_get(msg_addr, send_size, &kmsg); | |
476 | ||
477 | if (mr != MACH_MSG_SUCCESS) | |
478 | return mr; | |
479 | ||
39236c6e A |
480 | mr = ipc_kmsg_copyin(kmsg, space, map, &option); |
481 | ||
1c79356b | 482 | if (mr != MACH_MSG_SUCCESS) { |
91447636 | 483 | ipc_kmsg_free(kmsg); |
1c79356b A |
484 | return mr; |
485 | } | |
91447636 | 486 | |
39236c6e | 487 | mr = ipc_kmsg_send(kmsg, option, msg_timeout); |
91447636 A |
488 | |
489 | if (mr != MACH_MSG_SUCCESS) { | |
490 | mr |= ipc_kmsg_copyout_pseudo(kmsg, space, map, MACH_MSG_BODY_NULL); | |
491 | (void) ipc_kmsg_put(msg_addr, kmsg, kmsg->ikm_header->msgh_size); | |
492 | return mr; | |
493 | } | |
494 | ||
1c79356b A |
495 | } |
496 | ||
497 | if (option & MACH_RCV_MSG) { | |
91447636 A |
498 | thread_t self = current_thread(); |
499 | ipc_space_t space = current_space(); | |
500 | ipc_object_t object; | |
501 | ipc_mqueue_t mqueue; | |
502 | ||
503 | mr = ipc_mqueue_copyin(space, rcv_name, &mqueue, &object); | |
504 | if (mr != MACH_MSG_SUCCESS) { | |
505 | return mr; | |
506 | } | |
507 | /* hold ref for object */ | |
1c79356b A |
508 | |
509 | /* | |
510 | * 1. MACH_RCV_OVERWRITE is on, and rcv_msg is our scatter list | |
511 | * and receive buffer | |
512 | * 2. MACH_RCV_OVERWRITE is off, and rcv_msg might be the | |
513 | * alternate receive buffer (separate send and receive buffers). | |
514 | */ | |
515 | if (option & MACH_RCV_OVERWRITE) | |
91447636 A |
516 | self->ith_msg_addr = rcv_msg_addr; |
517 | else if (rcv_msg_addr != (mach_vm_address_t)0) | |
518 | self->ith_msg_addr = rcv_msg_addr; | |
1c79356b | 519 | else |
91447636 A |
520 | self->ith_msg_addr = msg_addr; |
521 | self->ith_object = object; | |
522 | self->ith_msize = rcv_size; | |
523 | self->ith_option = option; | |
524 | self->ith_scatter_list_size = scatter_list_size; | |
b0d623f7 | 525 | self->ith_receiver_name = MACH_PORT_NULL; |
91447636 A |
526 | self->ith_continuation = thread_syscall_return; |
527 | ||
528 | ipc_mqueue_receive(mqueue, option, rcv_size, msg_timeout, THREAD_ABORTSAFE); | |
529 | if ((option & MACH_RCV_TIMEOUT) && msg_timeout == 0) | |
530 | thread_poll_yield(self); | |
531 | return mach_msg_receive_results(); | |
1c79356b A |
532 | } |
533 | ||
534 | return MACH_MSG_SUCCESS; | |
535 | } | |
536 | ||
9bccf70c A |
537 | /* |
538 | * Routine: mach_msg_trap [mach trap] | |
539 | * Purpose: | |
540 | * Possibly send a message; possibly receive a message. | |
541 | * Conditions: | |
542 | * Nothing locked. | |
543 | * Returns: | |
544 | * All of mach_msg_send and mach_msg_receive error codes. | |
545 | */ | |
546 | ||
547 | mach_msg_return_t | |
548 | mach_msg_trap( | |
91447636 | 549 | struct mach_msg_overwrite_trap_args *args) |
9bccf70c | 550 | { |
91447636 A |
551 | kern_return_t kr; |
552 | args->rcv_msg = (mach_vm_address_t)0; | |
553 | ||
554 | kr = mach_msg_overwrite_trap(args); | |
555 | return kr; | |
9bccf70c A |
556 | } |
557 | ||
558 | ||
1c79356b A |
559 | /* |
560 | * Routine: msg_receive_error [internal] | |
561 | * Purpose: | |
562 | * Builds a minimal header/trailer and copies it to | |
563 | * the user message buffer. Invoked when in the case of a | |
564 | * MACH_RCV_TOO_LARGE or MACH_RCV_BODY_ERROR error. | |
565 | * Conditions: | |
566 | * Nothing locked. | |
567 | * Returns: | |
568 | * MACH_MSG_SUCCESS minimal header/trailer copied | |
569 | * MACH_RCV_INVALID_DATA copyout to user buffer failed | |
570 | */ | |
571 | ||
572 | mach_msg_return_t | |
573 | msg_receive_error( | |
574 | ipc_kmsg_t kmsg, | |
91447636 | 575 | mach_vm_address_t msg_addr, |
1c79356b A |
576 | mach_msg_option_t option, |
577 | mach_port_seqno_t seqno, | |
578 | ipc_space_t space) | |
579 | { | |
b0d623f7 | 580 | mach_vm_address_t context; |
316670eb A |
581 | mach_msg_trailer_size_t trailer_size; |
582 | mach_msg_max_trailer_t *trailer; | |
b0d623f7 A |
583 | |
584 | context = kmsg->ikm_header->msgh_remote_port->ip_context; | |
1c79356b A |
585 | |
586 | /* | |
587 | * Copy out the destination port in the message. | |
588 | * Destroy all other rights and memory in the message. | |
589 | */ | |
590 | ipc_kmsg_copyout_dest(kmsg, space); | |
591 | ||
592 | /* | |
593 | * Build a minimal message with the requested trailer. | |
594 | */ | |
b0d623f7 | 595 | trailer = (mach_msg_max_trailer_t *) |
91447636 | 596 | ((vm_offset_t)kmsg->ikm_header + |
1c79356b | 597 | round_msg(sizeof(mach_msg_header_t))); |
91447636 | 598 | kmsg->ikm_header->msgh_size = sizeof(mach_msg_header_t); |
1c79356b A |
599 | bcopy( (char *)&trailer_template, |
600 | (char *)trailer, | |
601 | sizeof(trailer_template)); | |
316670eb A |
602 | |
603 | trailer_size = ipc_kmsg_add_trailer(kmsg, space, | |
604 | option, current_thread(), seqno, | |
605 | TRUE, context); | |
1c79356b A |
606 | |
607 | /* | |
608 | * Copy the message to user space | |
609 | */ | |
91447636 | 610 | if (ipc_kmsg_put(msg_addr, kmsg, kmsg->ikm_header->msgh_size + |
316670eb | 611 | trailer_size) == MACH_RCV_INVALID_DATA) |
1c79356b A |
612 | return(MACH_RCV_INVALID_DATA); |
613 | else | |
614 | return(MACH_MSG_SUCCESS); | |
615 | } |