2 * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
52 #include <mach/mach.h>
53 #include <mach/boolean.h>
54 #include <mach/kern_return.h>
55 #include <mach/message.h>
56 #include <mach/mig_errors.h>
57 #include <mach/vm_statistics.h>
59 #ifdef HOST_MACH_MSG_TRAP
60 __private_extern__ kern_return_t _host_mach_msg_trap_return_
= KERN_FAILURE
;
62 #define MACH_MSG_TRAP(msg, opt, ssize, rsize, rname, to, not) \
63 ((_host_mach_msg_trap_return_ == KERN_SUCCESS) ? \
64 mach_msg_trap((msg), (opt), (ssize), (rsize), (rname), (to), (not)) : \
65 mach_msg_overwrite_trap((msg), (opt), (ssize), (rsize), (rname), \
66 (to), (not), MACH_MSG_NULL, 0))
70 #define MACH_MSG_TRAP(msg, opt, ssize, rsize, rname, to, not) \
71 mach_msg_overwrite_trap((msg), (opt), (ssize), (rsize), (rname), \
72 (to), (not), MACH_MSG_NULL, 0))
75 #define LIBMACH_OPTIONS (MACH_SEND_INTERRUPT|MACH_RCV_INTERRUPT)
80 * Send and/or receive a message. If the message operation
81 * is interrupted, and the user did not request an indication
82 * of that fact, then restart the appropriate parts of the
86 mach_msg(msg
, option
, send_size
, rcv_size
, rcv_name
, timeout
, notify
)
87 mach_msg_header_t
*msg
;
88 mach_msg_option_t option
;
89 mach_msg_size_t send_size
;
90 mach_msg_size_t rcv_size
;
92 mach_msg_timeout_t timeout
;
98 * Consider the following cases:
99 * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED
100 * plus special bits).
101 * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options.
102 * 3) RPC calls with interruptions in one/both halves.
104 * We refrain from passing the option bits that we implement
105 * to the kernel. This prevents their presence from inhibiting
106 * the kernel's fast paths (when it checks the option value).
109 mr
= MACH_MSG_TRAP(msg
, option
&~ LIBMACH_OPTIONS
,
110 send_size
, rcv_size
, rcv_name
,
112 if (mr
== MACH_MSG_SUCCESS
)
113 return MACH_MSG_SUCCESS
;
115 if ((option
& MACH_SEND_INTERRUPT
) == 0)
116 while (mr
== MACH_SEND_INTERRUPTED
)
117 mr
= MACH_MSG_TRAP(msg
,
118 option
&~ LIBMACH_OPTIONS
,
119 send_size
, rcv_size
, rcv_name
,
122 if ((option
& MACH_RCV_INTERRUPT
) == 0)
123 while (mr
== MACH_RCV_INTERRUPTED
)
124 mr
= MACH_MSG_TRAP(msg
,
125 option
&~ (LIBMACH_OPTIONS
|MACH_SEND_MSG
),
126 0, rcv_size
, rcv_name
,
133 * Routine: mach_msg_overwrite
135 * Send and/or receive a message. If the message operation
136 * is interrupted, and the user did not request an indication
137 * of that fact, then restart the appropriate parts of the
140 * Distinct send and receive buffers may be specified. If
141 * no separate receive buffer is specified, the msg parameter
142 * will be used for both send and receive operations.
144 * In addition to a distinct receive buffer, that buffer may
145 * already contain scatter control information to direct the
146 * receiving of the message.
149 mach_msg_overwrite(msg
, option
, send_size
, rcv_limit
, rcv_name
, timeout
,
150 notify
, rcv_msg
, rcv_scatter_size
)
151 mach_msg_header_t
*msg
;
152 mach_msg_option_t option
;
153 mach_msg_size_t send_size
;
154 mach_msg_size_t rcv_limit
;
155 mach_port_t rcv_name
;
156 mach_msg_timeout_t timeout
;
158 mach_msg_header_t
*rcv_msg
;
159 mach_msg_size_t rcv_scatter_size
;
161 mach_msg_return_t mr
;
164 * Consider the following cases:
165 * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED
166 * plus special bits).
167 * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options.
168 * 3) RPC calls with interruptions in one/both halves.
170 * We refrain from passing the option bits that we implement
171 * to the kernel. This prevents their presence from inhibiting
172 * the kernel's fast paths (when it checks the option value).
175 mr
= mach_msg_overwrite_trap(msg
, option
&~ LIBMACH_OPTIONS
,
176 send_size
, rcv_limit
, rcv_name
,
177 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
178 if (mr
== MACH_MSG_SUCCESS
)
179 return MACH_MSG_SUCCESS
;
181 if ((option
& MACH_SEND_INTERRUPT
) == 0)
182 while (mr
== MACH_SEND_INTERRUPTED
)
183 mr
= mach_msg_overwrite_trap(msg
,
184 option
&~ LIBMACH_OPTIONS
,
185 send_size
, rcv_limit
, rcv_name
,
186 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
188 if ((option
& MACH_RCV_INTERRUPT
) == 0)
189 while (mr
== MACH_RCV_INTERRUPTED
)
190 mr
= mach_msg_overwrite_trap(msg
,
191 option
&~ (LIBMACH_OPTIONS
|MACH_SEND_MSG
),
192 0, rcv_limit
, rcv_name
,
193 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
200 mach_msg_send(mach_msg_header_t
*msg
)
202 return mach_msg(msg
, MACH_SEND_MSG
,
203 msg
->msgh_size
, 0, MACH_PORT_NULL
,
204 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
208 mach_msg_receive(mach_msg_header_t
*msg
)
210 return mach_msg(msg
, MACH_RCV_MSG
,
211 0, msg
->msgh_size
, msg
->msgh_local_port
,
212 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
217 mach_msg_destroy_port(mach_port_t port
, mach_msg_type_name_t type
)
219 if (MACH_PORT_VALID(port
)) switch (type
) {
220 case MACH_MSG_TYPE_MOVE_SEND
:
221 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
222 /* destroy the send/send-once right */
223 (void) mach_port_deallocate(mach_task_self(), port
);
226 case MACH_MSG_TYPE_MOVE_RECEIVE
:
227 /* destroy the receive right */
228 (void) mach_port_mod_refs(mach_task_self(), port
,
229 MACH_PORT_RIGHT_RECEIVE
, -1);
232 case MACH_MSG_TYPE_MAKE_SEND
:
233 /* create a send right and then destroy it */
234 (void) mach_port_insert_right(mach_task_self(), port
,
235 port
, MACH_MSG_TYPE_MAKE_SEND
);
236 (void) mach_port_deallocate(mach_task_self(), port
);
239 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
240 /* create a send-once right and then destroy it */
241 (void) mach_port_extract_right(mach_task_self(), port
,
242 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
244 (void) mach_port_deallocate(mach_task_self(), port
);
250 mach_msg_destroy_memory(vm_offset_t addr
, vm_size_t size
)
253 (void) vm_deallocate(mach_task_self(), addr
, size
);
258 * Routine: mach_msg_destroy
260 * mach_msg_destroy is useful in two contexts.
262 * First, it can deallocate all port rights and
263 * out-of-line memory in a received message.
264 * When a server receives a request it doesn't want,
265 * it needs this functionality.
267 * Second, it can mimic the side-effects of a msg-send
268 * operation. The effect is as if the message were sent
269 * and then destroyed inside the kernel. When a server
270 * can't send a reply (because the client died),
271 * it needs this functionality.
274 mach_msg_destroy(mach_msg_header_t
*msg
)
276 mach_msg_bits_t mbits
= msg
->msgh_bits
;
279 * The msgh_local_port field doesn't hold a port right.
280 * The receive operation consumes the destination port right.
283 mach_msg_destroy_port(msg
->msgh_remote_port
, MACH_MSGH_BITS_REMOTE(mbits
));
285 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
286 mach_msg_body_t
*body
;
287 mach_msg_descriptor_t
*saddr
, *eaddr
;
289 body
= (mach_msg_body_t
*) (msg
+ 1);
290 saddr
= (mach_msg_descriptor_t
*)
291 ((mach_msg_base_t
*) msg
+ 1);
292 eaddr
= saddr
+ body
->msgh_descriptor_count
;
294 for ( ; saddr
< eaddr
; saddr
++) {
295 switch (saddr
->type
.type
) {
297 case MACH_MSG_PORT_DESCRIPTOR
: {
298 mach_msg_port_descriptor_t
*dsc
;
301 * Destroy port rights carried in the message
304 mach_msg_destroy_port(dsc
->name
, dsc
->disposition
);
308 case MACH_MSG_OOL_DESCRIPTOR
: {
309 mach_msg_ool_descriptor_t
*dsc
;
312 * Destroy memory carried in the message
314 dsc
= &saddr
->out_of_line
;
315 if (dsc
->deallocate
) {
316 mach_msg_destroy_memory((vm_offset_t
)dsc
->address
,
322 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
324 mach_msg_ool_ports_descriptor_t
*dsc
;
325 mach_msg_type_number_t j
;
328 * Destroy port rights carried in the message
330 dsc
= &saddr
->ool_ports
;
331 ports
= (mach_port_t
*) dsc
->address
;
332 for (j
= 0; j
< dsc
->count
; j
++, ports
++) {
333 mach_msg_destroy_port(*ports
, dsc
->disposition
);
337 * Destroy memory carried in the message
339 if (dsc
->deallocate
) {
340 mach_msg_destroy_memory((vm_offset_t
)dsc
->address
,
341 dsc
->count
* sizeof(mach_port_t
));
351 * Routine: mach_msg_server_once
353 * A simple generic server function. It allows more flexibility
354 * than mach_msg_server by processing only one message request
355 * and then returning to the user. Note that more in the way
356 * of error codes are returned to the user; specifically, any
357 * failing error from mach_msg calls will be returned
358 * (though errors from the demux routine or the routine it
359 * calls will not be).
362 mach_msg_server_once(
363 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
364 mach_msg_size_t max_size
,
365 mach_port_t rcv_name
,
366 mach_msg_options_t options
)
368 mig_reply_error_t
*bufRequest
, *bufReply
;
369 mach_msg_size_t request_size
;
370 mach_msg_size_t request_alloc
;
371 mach_msg_size_t trailer_alloc
;
372 mach_msg_size_t reply_alloc
;
373 mach_msg_return_t mr
;
375 mach_port_t self
= mach_task_self();
377 options
&= ~(MACH_SEND_MSG
|MACH_RCV_MSG
);
379 trailer_alloc
= REQUESTED_TRAILER_SIZE(options
);
380 request_alloc
= round_page(max_size
+ trailer_alloc
);
382 request_size
= (options
& MACH_RCV_LARGE
) ?
383 request_alloc
: max_size
+ trailer_alloc
;
385 reply_alloc
= round_page((options
& MACH_SEND_TRAILER
) ?
386 (max_size
+ MAX_TRAILER_SIZE
) :
389 kr
= vm_allocate(self
,
390 (vm_address_t
*)&bufReply
,
392 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
393 if (kr
!= KERN_SUCCESS
)
397 mach_msg_size_t new_request_alloc
;
399 kr
= vm_allocate(self
,
400 (vm_address_t
*)&bufRequest
,
402 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
403 if (kr
!= KERN_SUCCESS
) {
405 (vm_address_t
)bufReply
,
410 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
411 0, request_size
, rcv_name
,
412 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
414 if (!((mr
== MACH_RCV_TOO_LARGE
) && (options
& MACH_RCV_LARGE
)))
417 new_request_alloc
= round_page(bufRequest
->Head
.msgh_size
+
420 (vm_address_t
) bufRequest
,
422 request_size
= request_alloc
= new_request_alloc
;
425 if (mr
== MACH_MSG_SUCCESS
) {
426 /* we have a request message */
428 (void) (*demux
)(&bufRequest
->Head
, &bufReply
->Head
);
430 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
431 if (bufReply
->RetCode
== MIG_NO_REPLY
)
432 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
433 else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
434 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
435 /* destroy the request - but not the reply port */
436 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
437 mach_msg_destroy(&bufRequest
->Head
);
442 * We don't want to block indefinitely because the client
443 * isn't receiving messages from the reply port.
444 * If we have a send-once right for the reply port, then
445 * this isn't a concern because the send won't block.
446 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
447 * To avoid falling off the kernel's fast RPC path unnecessarily,
448 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
450 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
452 mr
= mach_msg(&bufReply
->Head
,
453 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
454 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
455 MACH_SEND_MSG
|options
:
456 MACH_SEND_MSG
|MACH_SEND_TIMEOUT
|options
,
457 bufReply
->Head
.msgh_size
, 0, MACH_PORT_NULL
,
458 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
460 if ((mr
!= MACH_SEND_INVALID_DEST
) &&
461 (mr
!= MACH_SEND_TIMED_OUT
))
463 mr
= MACH_MSG_SUCCESS
;
465 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
466 mach_msg_destroy(&bufReply
->Head
);
470 (void)vm_deallocate(self
,
471 (vm_address_t
) bufRequest
,
473 (void)vm_deallocate(self
,
474 (vm_address_t
) bufReply
,
480 * Routine: mach_msg_server
482 * A simple generic server function. Note that changes here
483 * should be considered for duplication above.
487 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
488 mach_msg_size_t max_size
,
489 mach_port_t rcv_name
,
490 mach_msg_options_t options
)
492 mig_reply_error_t
*bufRequest
, *bufReply
;
493 mach_msg_size_t request_size
;
494 mach_msg_size_t new_request_alloc
;
495 mach_msg_size_t request_alloc
;
496 mach_msg_size_t trailer_alloc
;
497 mach_msg_size_t reply_alloc
;
498 mach_msg_return_t mr
;
500 mach_port_t self
= mach_task_self();
502 options
&= ~(MACH_SEND_MSG
|MACH_RCV_MSG
);
504 reply_alloc
= round_page((options
& MACH_SEND_TRAILER
) ?
505 (max_size
+ MAX_TRAILER_SIZE
) : max_size
);
507 kr
= vm_allocate(self
,
508 (vm_address_t
*)&bufReply
,
510 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
511 if (kr
!= KERN_SUCCESS
)
515 trailer_alloc
= REQUESTED_TRAILER_SIZE(options
);
516 new_request_alloc
= round_page(max_size
+ trailer_alloc
);
518 request_size
= (options
& MACH_RCV_LARGE
) ?
519 new_request_alloc
: max_size
+ trailer_alloc
;
522 if (request_alloc
< new_request_alloc
) {
523 request_alloc
= new_request_alloc
;
524 kr
= vm_allocate(self
,
525 (vm_address_t
*)&bufRequest
,
527 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
528 if (kr
!= KERN_SUCCESS
) {
530 (vm_address_t
)bufReply
,
536 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
537 0, request_size
, rcv_name
,
538 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
540 while (mr
== MACH_MSG_SUCCESS
) {
541 /* we have another request message */
543 (void) (*demux
)(&bufRequest
->Head
, &bufReply
->Head
);
545 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
546 if (bufReply
->RetCode
== MIG_NO_REPLY
)
547 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
548 else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
549 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
550 /* destroy the request - but not the reply port */
551 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
552 mach_msg_destroy(&bufRequest
->Head
);
557 * We don't want to block indefinitely because the client
558 * isn't receiving messages from the reply port.
559 * If we have a send-once right for the reply port, then
560 * this isn't a concern because the send won't block.
561 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
562 * To avoid falling off the kernel's fast RPC path,
563 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
565 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
566 if (request_alloc
== reply_alloc
) {
567 mig_reply_error_t
*bufTemp
;
571 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
572 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
573 MACH_SEND_MSG
|MACH_RCV_MSG
|options
:
574 MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_SEND_TIMEOUT
|options
,
575 bufReply
->Head
.msgh_size
, request_size
, rcv_name
,
576 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
578 /* swap request and reply */
579 bufTemp
= bufRequest
;
580 bufRequest
= bufReply
;
584 mr
= mach_msg_overwrite(
586 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
587 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
588 MACH_SEND_MSG
|MACH_RCV_MSG
|options
:
589 MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_SEND_TIMEOUT
|options
,
590 bufReply
->Head
.msgh_size
, request_size
, rcv_name
,
591 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
,
592 &bufRequest
->Head
, request_size
);
595 if ((mr
!= MACH_SEND_INVALID_DEST
) &&
596 (mr
!= MACH_SEND_TIMED_OUT
))
599 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
600 mach_msg_destroy(&bufReply
->Head
);
602 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
603 0, request_size
, rcv_name
,
604 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
606 } /* while (mr == MACH_MSG_SUCCESS) */
608 if ((mr
== MACH_RCV_TOO_LARGE
) && (options
& MACH_RCV_LARGE
)) {
609 new_request_alloc
= round_page(bufRequest
->Head
.msgh_size
+
611 request_size
= new_request_alloc
;
613 (vm_address_t
) bufRequest
,
622 (void)vm_deallocate(self
,
623 (vm_address_t
) bufRequest
,
625 (void)vm_deallocate(self
,
626 (vm_address_t
) bufReply
,