2 * Copyright (c) 1999-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
55 #include <mach/mach.h>
56 #include <mach/boolean.h>
57 #include <mach/kern_return.h>
58 #include <mach/message.h>
59 #include <mach/mig_errors.h>
60 #include <mach/vm_statistics.h>
62 #define MACH_MSG_TRAP(msg, opt, ssize, rsize, rname, to, not) \
63 mach_msg_trap((msg), (opt), (ssize), (rsize), (rname), (to), (not))
65 #define LIBMACH_OPTIONS (MACH_SEND_INTERRUPT|MACH_RCV_INTERRUPT)
70 * Send and/or receive a message. If the message operation
71 * is interrupted, and the user did not request an indication
72 * of that fact, then restart the appropriate parts of the
76 mach_msg(msg
, option
, send_size
, rcv_size
, rcv_name
, timeout
, notify
)
77 mach_msg_header_t
*msg
;
78 mach_msg_option_t option
;
79 mach_msg_size_t send_size
;
80 mach_msg_size_t rcv_size
;
82 mach_msg_timeout_t timeout
;
88 * Consider the following cases:
89 * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED
91 * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options.
92 * 3) RPC calls with interruptions in one/both halves.
94 * We refrain from passing the option bits that we implement
95 * to the kernel. This prevents their presence from inhibiting
96 * the kernel's fast paths (when it checks the option value).
99 mr
= MACH_MSG_TRAP(msg
, option
&~ LIBMACH_OPTIONS
,
100 send_size
, rcv_size
, rcv_name
,
102 if (mr
== MACH_MSG_SUCCESS
)
103 return MACH_MSG_SUCCESS
;
105 if ((option
& MACH_SEND_INTERRUPT
) == 0)
106 while (mr
== MACH_SEND_INTERRUPTED
)
107 mr
= MACH_MSG_TRAP(msg
,
108 option
&~ LIBMACH_OPTIONS
,
109 send_size
, rcv_size
, rcv_name
,
112 if ((option
& MACH_RCV_INTERRUPT
) == 0)
113 while (mr
== MACH_RCV_INTERRUPTED
)
114 mr
= MACH_MSG_TRAP(msg
,
115 option
&~ (LIBMACH_OPTIONS
|MACH_SEND_MSG
),
116 0, rcv_size
, rcv_name
,
123 * Routine: mach_msg_overwrite
125 * Send and/or receive a message. If the message operation
126 * is interrupted, and the user did not request an indication
127 * of that fact, then restart the appropriate parts of the
130 * Distinct send and receive buffers may be specified. If
131 * no separate receive buffer is specified, the msg parameter
132 * will be used for both send and receive operations.
134 * In addition to a distinct receive buffer, that buffer may
135 * already contain scatter control information to direct the
136 * receiving of the message.
139 mach_msg_overwrite(msg
, option
, send_size
, rcv_limit
, rcv_name
, timeout
,
140 notify
, rcv_msg
, rcv_scatter_size
)
141 mach_msg_header_t
*msg
;
142 mach_msg_option_t option
;
143 mach_msg_size_t send_size
;
144 mach_msg_size_t rcv_limit
;
145 mach_port_t rcv_name
;
146 mach_msg_timeout_t timeout
;
148 mach_msg_header_t
*rcv_msg
;
149 mach_msg_size_t rcv_scatter_size
;
151 mach_msg_return_t mr
;
154 * Consider the following cases:
155 * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED
156 * plus special bits).
157 * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options.
158 * 3) RPC calls with interruptions in one/both halves.
160 * We refrain from passing the option bits that we implement
161 * to the kernel. This prevents their presence from inhibiting
162 * the kernel's fast paths (when it checks the option value).
165 mr
= mach_msg_overwrite_trap(msg
, option
&~ LIBMACH_OPTIONS
,
166 send_size
, rcv_limit
, rcv_name
,
167 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
168 if (mr
== MACH_MSG_SUCCESS
)
169 return MACH_MSG_SUCCESS
;
171 if ((option
& MACH_SEND_INTERRUPT
) == 0)
172 while (mr
== MACH_SEND_INTERRUPTED
)
173 mr
= mach_msg_overwrite_trap(msg
,
174 option
&~ LIBMACH_OPTIONS
,
175 send_size
, rcv_limit
, rcv_name
,
176 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
178 if ((option
& MACH_RCV_INTERRUPT
) == 0)
179 while (mr
== MACH_RCV_INTERRUPTED
)
180 mr
= mach_msg_overwrite_trap(msg
,
181 option
&~ (LIBMACH_OPTIONS
|MACH_SEND_MSG
),
182 0, rcv_limit
, rcv_name
,
183 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
190 mach_msg_send(mach_msg_header_t
*msg
)
192 return mach_msg(msg
, MACH_SEND_MSG
,
193 msg
->msgh_size
, 0, MACH_PORT_NULL
,
194 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
198 mach_msg_receive(mach_msg_header_t
*msg
)
200 return mach_msg(msg
, MACH_RCV_MSG
,
201 0, msg
->msgh_size
, msg
->msgh_local_port
,
202 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
207 mach_msg_destroy_port(mach_port_t port
, mach_msg_type_name_t type
)
209 if (MACH_PORT_VALID(port
)) switch (type
) {
210 case MACH_MSG_TYPE_MOVE_SEND
:
211 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
212 /* destroy the send/send-once right */
213 (void) mach_port_deallocate(mach_task_self_
, port
);
216 case MACH_MSG_TYPE_MOVE_RECEIVE
:
217 /* destroy the receive right */
218 (void) mach_port_mod_refs(mach_task_self_
, port
,
219 MACH_PORT_RIGHT_RECEIVE
, -1);
222 case MACH_MSG_TYPE_MAKE_SEND
:
223 /* create a send right and then destroy it */
224 (void) mach_port_insert_right(mach_task_self_
, port
,
225 port
, MACH_MSG_TYPE_MAKE_SEND
);
226 (void) mach_port_deallocate(mach_task_self_
, port
);
229 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
230 /* create a send-once right and then destroy it */
231 (void) mach_port_extract_right(mach_task_self_
, port
,
232 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
234 (void) mach_port_deallocate(mach_task_self_
, port
);
240 mach_msg_destroy_memory(vm_offset_t addr
, vm_size_t size
)
243 (void) vm_deallocate(mach_task_self_
, addr
, size
);
248 * Routine: mach_msg_destroy
250 * mach_msg_destroy is useful in two contexts.
252 * First, it can deallocate all port rights and
253 * out-of-line memory in a received message.
254 * When a server receives a request it doesn't want,
255 * it needs this functionality.
257 * Second, it can mimic the side-effects of a msg-send
258 * operation. The effect is as if the message were sent
259 * and then destroyed inside the kernel. When a server
260 * can't send a reply (because the client died),
261 * it needs this functionality.
264 mach_msg_destroy(mach_msg_header_t
*msg
)
266 mach_msg_bits_t mbits
= msg
->msgh_bits
;
269 * The msgh_local_port field doesn't hold a port right.
270 * The receive operation consumes the destination port right.
273 mach_msg_destroy_port(msg
->msgh_remote_port
, MACH_MSGH_BITS_REMOTE(mbits
));
275 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
276 mach_msg_base_t
*base
;
277 mach_msg_type_number_t count
, i
;
278 mach_msg_descriptor_t
*daddr
;
280 base
= (mach_msg_base_t
*) msg
;
281 count
= base
->body
.msgh_descriptor_count
;
283 daddr
= (mach_msg_descriptor_t
*) (base
+ 1);
284 for (i
= 0; i
< count
; i
++) {
286 switch (daddr
->type
.type
) {
288 case MACH_MSG_PORT_DESCRIPTOR
: {
289 mach_msg_port_descriptor_t
*dsc
;
292 * Destroy port rights carried in the message
295 mach_msg_destroy_port(dsc
->name
, dsc
->disposition
);
296 daddr
= (mach_msg_descriptor_t
*)(dsc
+ 1);
300 case MACH_MSG_OOL_DESCRIPTOR
: {
301 mach_msg_ool_descriptor_t
*dsc
;
304 * Destroy memory carried in the message
306 dsc
= &daddr
->out_of_line
;
307 if (dsc
->deallocate
) {
308 mach_msg_destroy_memory((vm_offset_t
)dsc
->address
,
311 daddr
= (mach_msg_descriptor_t
*)(dsc
+ 1);
315 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
: {
316 mach_msg_ool_descriptor_t
*dsc
;
321 daddr
= (mach_msg_descriptor_t
*)(dsc
+ 1);
325 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
327 mach_msg_ool_ports_descriptor_t
*dsc
;
328 mach_msg_type_number_t j
;
331 * Destroy port rights carried in the message
333 dsc
= &daddr
->ool_ports
;
334 ports
= (mach_port_t
*) dsc
->address
;
335 for (j
= 0; j
< dsc
->count
; j
++, ports
++) {
336 mach_msg_destroy_port(*ports
, dsc
->disposition
);
340 * Destroy memory carried in the message
342 if (dsc
->deallocate
) {
343 mach_msg_destroy_memory((vm_offset_t
)dsc
->address
,
344 dsc
->count
* sizeof(mach_port_t
));
346 daddr
= (mach_msg_descriptor_t
*)(dsc
+ 1);
355 * Routine: mach_msg_server_once
357 * A simple generic server function. It allows more flexibility
358 * than mach_msg_server by processing only one message request
359 * and then returning to the user. Note that more in the way
360 * of error codes are returned to the user; specifically, any
361 * failing error from mach_msg calls will be returned
362 * (though errors from the demux routine or the routine it
363 * calls will not be).
366 mach_msg_server_once(
367 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
368 mach_msg_size_t max_size
,
369 mach_port_t rcv_name
,
370 mach_msg_options_t options
)
372 mig_reply_error_t
*bufRequest
, *bufReply
;
373 mach_msg_size_t request_size
;
374 mach_msg_size_t request_alloc
;
375 mach_msg_size_t trailer_alloc
;
376 mach_msg_size_t reply_alloc
;
377 mach_msg_return_t mr
;
379 mach_port_t self
= mach_task_self_
;
381 options
&= ~(MACH_SEND_MSG
|MACH_RCV_MSG
);
383 trailer_alloc
= REQUESTED_TRAILER_SIZE(options
);
384 request_alloc
= round_page(max_size
+ trailer_alloc
);
386 request_size
= (options
& MACH_RCV_LARGE
) ?
387 request_alloc
: max_size
+ trailer_alloc
;
389 reply_alloc
= round_page((options
& MACH_SEND_TRAILER
) ?
390 (max_size
+ MAX_TRAILER_SIZE
) :
393 kr
= vm_allocate(self
,
394 (vm_address_t
*)&bufReply
,
396 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
397 if (kr
!= KERN_SUCCESS
)
401 mach_msg_size_t new_request_alloc
;
403 kr
= vm_allocate(self
,
404 (vm_address_t
*)&bufRequest
,
406 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
407 if (kr
!= KERN_SUCCESS
) {
409 (vm_address_t
)bufReply
,
414 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
415 0, request_size
, rcv_name
,
416 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
418 if (!((mr
== MACH_RCV_TOO_LARGE
) && (options
& MACH_RCV_LARGE
)))
421 new_request_alloc
= round_page(bufRequest
->Head
.msgh_size
+
424 (vm_address_t
) bufRequest
,
426 request_size
= request_alloc
= new_request_alloc
;
429 if (mr
== MACH_MSG_SUCCESS
) {
430 /* we have a request message */
432 (void) (*demux
)(&bufRequest
->Head
, &bufReply
->Head
);
434 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
435 if (bufReply
->RetCode
== MIG_NO_REPLY
)
436 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
437 else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
438 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
439 /* destroy the request - but not the reply port */
440 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
441 mach_msg_destroy(&bufRequest
->Head
);
446 * We don't want to block indefinitely because the client
447 * isn't receiving messages from the reply port.
448 * If we have a send-once right for the reply port, then
449 * this isn't a concern because the send won't block.
450 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
451 * To avoid falling off the kernel's fast RPC path unnecessarily,
452 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
454 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
456 mr
= mach_msg(&bufReply
->Head
,
457 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
458 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
459 MACH_SEND_MSG
|options
:
460 MACH_SEND_MSG
|MACH_SEND_TIMEOUT
|options
,
461 bufReply
->Head
.msgh_size
, 0, MACH_PORT_NULL
,
462 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
464 if ((mr
!= MACH_SEND_INVALID_DEST
) &&
465 (mr
!= MACH_SEND_TIMED_OUT
))
467 mr
= MACH_MSG_SUCCESS
;
469 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
470 mach_msg_destroy(&bufReply
->Head
);
474 (void)vm_deallocate(self
,
475 (vm_address_t
) bufRequest
,
477 (void)vm_deallocate(self
,
478 (vm_address_t
) bufReply
,
484 * Routine: mach_msg_server
486 * A simple generic server function. Note that changes here
487 * should be considered for duplication above.
491 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
492 mach_msg_size_t max_size
,
493 mach_port_t rcv_name
,
494 mach_msg_options_t options
)
496 mig_reply_error_t
*bufRequest
, *bufReply
;
497 mach_msg_size_t request_size
;
498 mach_msg_size_t new_request_alloc
;
499 mach_msg_size_t request_alloc
;
500 mach_msg_size_t trailer_alloc
;
501 mach_msg_size_t reply_alloc
;
502 mach_msg_return_t mr
;
504 mach_port_t self
= mach_task_self_
;
506 options
&= ~(MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_RCV_OVERWRITE
);
508 reply_alloc
= round_page((options
& MACH_SEND_TRAILER
) ?
509 (max_size
+ MAX_TRAILER_SIZE
) : max_size
);
511 kr
= vm_allocate(self
,
512 (vm_address_t
*)&bufReply
,
514 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
515 if (kr
!= KERN_SUCCESS
)
519 trailer_alloc
= REQUESTED_TRAILER_SIZE(options
);
520 new_request_alloc
= round_page(max_size
+ trailer_alloc
);
522 request_size
= (options
& MACH_RCV_LARGE
) ?
523 new_request_alloc
: max_size
+ trailer_alloc
;
526 if (request_alloc
< new_request_alloc
) {
527 request_alloc
= new_request_alloc
;
528 kr
= vm_allocate(self
,
529 (vm_address_t
*)&bufRequest
,
531 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
532 if (kr
!= KERN_SUCCESS
) {
534 (vm_address_t
)bufReply
,
540 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
541 0, request_size
, rcv_name
,
542 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
544 while (mr
== MACH_MSG_SUCCESS
) {
545 /* we have another request message */
547 (void) (*demux
)(&bufRequest
->Head
, &bufReply
->Head
);
549 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
550 if (bufReply
->RetCode
== MIG_NO_REPLY
)
551 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
552 else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
553 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
554 /* destroy the request - but not the reply port */
555 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
556 mach_msg_destroy(&bufRequest
->Head
);
561 * We don't want to block indefinitely because the client
562 * isn't receiving messages from the reply port.
563 * If we have a send-once right for the reply port, then
564 * this isn't a concern because the send won't block.
565 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
566 * To avoid falling off the kernel's fast RPC path,
567 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
569 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
570 if (request_alloc
== reply_alloc
) {
571 mig_reply_error_t
*bufTemp
;
575 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
576 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
577 MACH_SEND_MSG
|MACH_RCV_MSG
|options
:
578 MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_SEND_TIMEOUT
|options
,
579 bufReply
->Head
.msgh_size
, request_size
, rcv_name
,
580 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
582 /* swap request and reply */
583 bufTemp
= bufRequest
;
584 bufRequest
= bufReply
;
588 mr
= mach_msg_overwrite(
590 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
591 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
592 MACH_SEND_MSG
|MACH_RCV_MSG
|options
:
593 MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_SEND_TIMEOUT
|options
,
594 bufReply
->Head
.msgh_size
, request_size
, rcv_name
,
595 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
,
596 &bufRequest
->Head
, 0);
599 if ((mr
!= MACH_SEND_INVALID_DEST
) &&
600 (mr
!= MACH_SEND_TIMED_OUT
))
603 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
604 mach_msg_destroy(&bufReply
->Head
);
606 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
607 0, request_size
, rcv_name
,
608 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
610 } /* while (mr == MACH_MSG_SUCCESS) */
612 if ((mr
== MACH_RCV_TOO_LARGE
) && (options
& MACH_RCV_LARGE
)) {
613 new_request_alloc
= round_page(bufRequest
->Head
.msgh_size
+
615 request_size
= new_request_alloc
;
617 (vm_address_t
) bufRequest
,
626 (void)vm_deallocate(self
,
627 (vm_address_t
) bufRequest
,
629 (void)vm_deallocate(self
,
630 (vm_address_t
) bufReply
,