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_body_t
*body
;
277 mach_msg_descriptor_t
*saddr
, *eaddr
;
279 body
= (mach_msg_body_t
*) (msg
+ 1);
280 saddr
= (mach_msg_descriptor_t
*)
281 ((mach_msg_base_t
*) msg
+ 1);
282 eaddr
= saddr
+ body
->msgh_descriptor_count
;
284 for ( ; saddr
< eaddr
; saddr
++) {
285 switch (saddr
->type
.type
) {
287 case MACH_MSG_PORT_DESCRIPTOR
: {
288 mach_msg_port_descriptor_t
*dsc
;
291 * Destroy port rights carried in the message
294 mach_msg_destroy_port(dsc
->name
, dsc
->disposition
);
298 case MACH_MSG_OOL_DESCRIPTOR
: {
299 mach_msg_ool_descriptor_t
*dsc
;
302 * Destroy memory carried in the message
304 dsc
= &saddr
->out_of_line
;
305 if (dsc
->deallocate
) {
306 mach_msg_destroy_memory((vm_offset_t
)dsc
->address
,
312 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
314 mach_msg_ool_ports_descriptor_t
*dsc
;
315 mach_msg_type_number_t j
;
318 * Destroy port rights carried in the message
320 dsc
= &saddr
->ool_ports
;
321 ports
= (mach_port_t
*) dsc
->address
;
322 for (j
= 0; j
< dsc
->count
; j
++, ports
++) {
323 mach_msg_destroy_port(*ports
, dsc
->disposition
);
327 * Destroy memory carried in the message
329 if (dsc
->deallocate
) {
330 mach_msg_destroy_memory((vm_offset_t
)dsc
->address
,
331 dsc
->count
* sizeof(mach_port_t
));
341 * Routine: mach_msg_server_once
343 * A simple generic server function. It allows more flexibility
344 * than mach_msg_server by processing only one message request
345 * and then returning to the user. Note that more in the way
346 * of error codes are returned to the user; specifically, any
347 * failing error from mach_msg calls will be returned
348 * (though errors from the demux routine or the routine it
349 * calls will not be).
352 mach_msg_server_once(
353 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
354 mach_msg_size_t max_size
,
355 mach_port_t rcv_name
,
356 mach_msg_options_t options
)
358 mig_reply_error_t
*bufRequest
, *bufReply
;
359 mach_msg_size_t request_size
;
360 mach_msg_size_t request_alloc
;
361 mach_msg_size_t trailer_alloc
;
362 mach_msg_size_t reply_alloc
;
363 mach_msg_return_t mr
;
365 mach_port_t self
= mach_task_self();
367 options
&= ~(MACH_SEND_MSG
|MACH_RCV_MSG
);
369 trailer_alloc
= REQUESTED_TRAILER_SIZE(options
);
370 request_alloc
= round_page(max_size
+ trailer_alloc
);
372 request_size
= (options
& MACH_RCV_LARGE
) ?
373 request_alloc
: max_size
+ trailer_alloc
;
375 reply_alloc
= round_page((options
& MACH_SEND_TRAILER
) ?
376 (max_size
+ MAX_TRAILER_SIZE
) :
379 kr
= vm_allocate(self
,
380 (vm_address_t
*)&bufReply
,
382 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
383 if (kr
!= KERN_SUCCESS
)
387 mach_msg_size_t new_request_alloc
;
389 kr
= vm_allocate(self
,
390 (vm_address_t
*)&bufRequest
,
392 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
393 if (kr
!= KERN_SUCCESS
) {
395 (vm_address_t
)bufReply
,
400 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
401 0, request_size
, rcv_name
,
402 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
404 if (!((mr
== MACH_RCV_TOO_LARGE
) && (options
& MACH_RCV_LARGE
)))
407 new_request_alloc
= round_page(bufRequest
->Head
.msgh_size
+
410 (vm_address_t
) bufRequest
,
412 request_size
= request_alloc
= new_request_alloc
;
415 if (mr
== MACH_MSG_SUCCESS
) {
416 /* we have a request message */
418 (void) (*demux
)(&bufRequest
->Head
, &bufReply
->Head
);
420 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
421 if (bufReply
->RetCode
== MIG_NO_REPLY
)
422 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
423 else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
424 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
425 /* destroy the request - but not the reply port */
426 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
427 mach_msg_destroy(&bufRequest
->Head
);
432 * We don't want to block indefinitely because the client
433 * isn't receiving messages from the reply port.
434 * If we have a send-once right for the reply port, then
435 * this isn't a concern because the send won't block.
436 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
437 * To avoid falling off the kernel's fast RPC path unnecessarily,
438 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
440 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
442 mr
= mach_msg(&bufReply
->Head
,
443 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
444 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
445 MACH_SEND_MSG
|options
:
446 MACH_SEND_MSG
|MACH_SEND_TIMEOUT
|options
,
447 bufReply
->Head
.msgh_size
, 0, MACH_PORT_NULL
,
448 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
450 if ((mr
!= MACH_SEND_INVALID_DEST
) &&
451 (mr
!= MACH_SEND_TIMED_OUT
))
453 mr
= MACH_MSG_SUCCESS
;
455 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
456 mach_msg_destroy(&bufReply
->Head
);
460 (void)vm_deallocate(self
,
461 (vm_address_t
) bufRequest
,
463 (void)vm_deallocate(self
,
464 (vm_address_t
) bufReply
,
470 * Routine: mach_msg_server
472 * A simple generic server function. Note that changes here
473 * should be considered for duplication above.
477 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
478 mach_msg_size_t max_size
,
479 mach_port_t rcv_name
,
480 mach_msg_options_t options
)
482 mig_reply_error_t
*bufRequest
, *bufReply
;
483 mach_msg_size_t request_size
;
484 mach_msg_size_t new_request_alloc
;
485 mach_msg_size_t request_alloc
;
486 mach_msg_size_t trailer_alloc
;
487 mach_msg_size_t reply_alloc
;
488 mach_msg_return_t mr
;
490 mach_port_t self
= mach_task_self();
492 options
&= ~(MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_RCV_OVERWRITE
);
494 reply_alloc
= round_page((options
& MACH_SEND_TRAILER
) ?
495 (max_size
+ MAX_TRAILER_SIZE
) : max_size
);
497 kr
= vm_allocate(self
,
498 (vm_address_t
*)&bufReply
,
500 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
501 if (kr
!= KERN_SUCCESS
)
505 trailer_alloc
= REQUESTED_TRAILER_SIZE(options
);
506 new_request_alloc
= round_page(max_size
+ trailer_alloc
);
508 request_size
= (options
& MACH_RCV_LARGE
) ?
509 new_request_alloc
: max_size
+ trailer_alloc
;
512 if (request_alloc
< new_request_alloc
) {
513 request_alloc
= new_request_alloc
;
514 kr
= vm_allocate(self
,
515 (vm_address_t
*)&bufRequest
,
517 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
518 if (kr
!= KERN_SUCCESS
) {
520 (vm_address_t
)bufReply
,
526 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
527 0, request_size
, rcv_name
,
528 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
530 while (mr
== MACH_MSG_SUCCESS
) {
531 /* we have another request message */
533 (void) (*demux
)(&bufRequest
->Head
, &bufReply
->Head
);
535 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
536 if (bufReply
->RetCode
== MIG_NO_REPLY
)
537 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
538 else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
539 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
540 /* destroy the request - but not the reply port */
541 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
542 mach_msg_destroy(&bufRequest
->Head
);
547 * We don't want to block indefinitely because the client
548 * isn't receiving messages from the reply port.
549 * If we have a send-once right for the reply port, then
550 * this isn't a concern because the send won't block.
551 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
552 * To avoid falling off the kernel's fast RPC path,
553 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
555 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
556 if (request_alloc
== reply_alloc
) {
557 mig_reply_error_t
*bufTemp
;
561 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
562 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
563 MACH_SEND_MSG
|MACH_RCV_MSG
|options
:
564 MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_SEND_TIMEOUT
|options
,
565 bufReply
->Head
.msgh_size
, request_size
, rcv_name
,
566 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
568 /* swap request and reply */
569 bufTemp
= bufRequest
;
570 bufRequest
= bufReply
;
574 mr
= mach_msg_overwrite(
576 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
577 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
578 MACH_SEND_MSG
|MACH_RCV_MSG
|options
:
579 MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_SEND_TIMEOUT
|options
,
580 bufReply
->Head
.msgh_size
, request_size
, rcv_name
,
581 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
,
582 &bufRequest
->Head
, 0);
585 if ((mr
!= MACH_SEND_INVALID_DEST
) &&
586 (mr
!= MACH_SEND_TIMED_OUT
))
589 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
590 mach_msg_destroy(&bufReply
->Head
);
592 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
593 0, request_size
, rcv_name
,
594 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
596 } /* while (mr == MACH_MSG_SUCCESS) */
598 if ((mr
== MACH_RCV_TOO_LARGE
) && (options
& MACH_RCV_LARGE
)) {
599 new_request_alloc
= round_page(bufRequest
->Head
.msgh_size
+
601 request_size
= new_request_alloc
;
603 (vm_address_t
) bufRequest
,
612 (void)vm_deallocate(self
,
613 (vm_address_t
) bufRequest
,
615 (void)vm_deallocate(self
,
616 (vm_address_t
) bufReply
,