2 * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Mach Operating System
24 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
25 * All Rights Reserved.
27 * Permission to use, copy, modify and distribute this software and its
28 * documentation is hereby granted, provided that both the copyright
29 * notice and this permission notice appear in all copies of the
30 * software, derivative works or modified versions, and any portions
31 * thereof, and that both notices appear in supporting documentation.
33 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
34 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
35 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
37 * Carnegie Mellon requests users of this software to return to
39 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
40 * School of Computer Science
41 * Carnegie Mellon University
42 * Pittsburgh PA 15213-3890
44 * any improvements or extensions that they make and grant Carnegie Mellon
45 * the rights to redistribute these changes.
49 #include <mach/mach.h>
50 #include <mach/boolean.h>
51 #include <mach/kern_return.h>
52 #include <mach/message.h>
53 #include <mach/mig_errors.h>
54 #include <mach/vm_statistics.h>
56 #ifdef HOST_MACH_MSG_TRAP
57 __private_extern__ kern_return_t _host_mach_msg_trap_return_
= KERN_FAILURE
;
59 #define MACH_MSG_TRAP(msg, opt, ssize, rsize, rname, to, not) \
60 ((_host_mach_msg_trap_return_ == KERN_SUCCESS) ? \
61 mach_msg_trap((msg), (opt), (ssize), (rsize), (rname), (to), (not)) : \
62 mach_msg_overwrite_trap((msg), (opt), (ssize), (rsize), (rname), \
63 (to), (not), MACH_MSG_NULL, 0))
67 #define MACH_MSG_TRAP(msg, opt, ssize, rsize, rname, to, not) \
68 mach_msg_overwrite_trap((msg), (opt), (ssize), (rsize), (rname), \
69 (to), (not), MACH_MSG_NULL, 0))
72 #define LIBMACH_OPTIONS (MACH_SEND_INTERRUPT|MACH_RCV_INTERRUPT)
77 * Send and/or receive a message. If the message operation
78 * is interrupted, and the user did not request an indication
79 * of that fact, then restart the appropriate parts of the
83 mach_msg(msg
, option
, send_size
, rcv_size
, rcv_name
, timeout
, notify
)
84 mach_msg_header_t
*msg
;
85 mach_msg_option_t option
;
86 mach_msg_size_t send_size
;
87 mach_msg_size_t rcv_size
;
89 mach_msg_timeout_t timeout
;
95 * Consider the following cases:
96 * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED
98 * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options.
99 * 3) RPC calls with interruptions in one/both halves.
101 * We refrain from passing the option bits that we implement
102 * to the kernel. This prevents their presence from inhibiting
103 * the kernel's fast paths (when it checks the option value).
106 mr
= MACH_MSG_TRAP(msg
, option
&~ LIBMACH_OPTIONS
,
107 send_size
, rcv_size
, rcv_name
,
109 if (mr
== MACH_MSG_SUCCESS
)
110 return MACH_MSG_SUCCESS
;
112 if ((option
& MACH_SEND_INTERRUPT
) == 0)
113 while (mr
== MACH_SEND_INTERRUPTED
)
114 mr
= MACH_MSG_TRAP(msg
,
115 option
&~ LIBMACH_OPTIONS
,
116 send_size
, rcv_size
, rcv_name
,
119 if ((option
& MACH_RCV_INTERRUPT
) == 0)
120 while (mr
== MACH_RCV_INTERRUPTED
)
121 mr
= MACH_MSG_TRAP(msg
,
122 option
&~ (LIBMACH_OPTIONS
|MACH_SEND_MSG
),
123 0, rcv_size
, rcv_name
,
130 * Routine: mach_msg_overwrite
132 * Send and/or receive a message. If the message operation
133 * is interrupted, and the user did not request an indication
134 * of that fact, then restart the appropriate parts of the
137 * Distinct send and receive buffers may be specified. If
138 * no separate receive buffer is specified, the msg parameter
139 * will be used for both send and receive operations.
141 * In addition to a distinct receive buffer, that buffer may
142 * already contain scatter control information to direct the
143 * receiving of the message.
146 mach_msg_overwrite(msg
, option
, send_size
, rcv_limit
, rcv_name
, timeout
,
147 notify
, rcv_msg
, rcv_scatter_size
)
148 mach_msg_header_t
*msg
;
149 mach_msg_option_t option
;
150 mach_msg_size_t send_size
;
151 mach_msg_size_t rcv_limit
;
152 mach_port_t rcv_name
;
153 mach_msg_timeout_t timeout
;
155 mach_msg_header_t
*rcv_msg
;
156 mach_msg_size_t rcv_scatter_size
;
158 mach_msg_return_t mr
;
161 * Consider the following cases:
162 * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED
163 * plus special bits).
164 * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options.
165 * 3) RPC calls with interruptions in one/both halves.
167 * We refrain from passing the option bits that we implement
168 * to the kernel. This prevents their presence from inhibiting
169 * the kernel's fast paths (when it checks the option value).
172 mr
= mach_msg_overwrite_trap(msg
, option
&~ LIBMACH_OPTIONS
,
173 send_size
, rcv_limit
, rcv_name
,
174 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
175 if (mr
== MACH_MSG_SUCCESS
)
176 return MACH_MSG_SUCCESS
;
178 if ((option
& MACH_SEND_INTERRUPT
) == 0)
179 while (mr
== MACH_SEND_INTERRUPTED
)
180 mr
= mach_msg_overwrite_trap(msg
,
181 option
&~ LIBMACH_OPTIONS
,
182 send_size
, rcv_limit
, rcv_name
,
183 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
185 if ((option
& MACH_RCV_INTERRUPT
) == 0)
186 while (mr
== MACH_RCV_INTERRUPTED
)
187 mr
= mach_msg_overwrite_trap(msg
,
188 option
&~ (LIBMACH_OPTIONS
|MACH_SEND_MSG
),
189 0, rcv_limit
, rcv_name
,
190 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
197 mach_msg_send(mach_msg_header_t
*msg
)
199 return mach_msg(msg
, MACH_SEND_MSG
,
200 msg
->msgh_size
, 0, MACH_PORT_NULL
,
201 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
205 mach_msg_receive(mach_msg_header_t
*msg
)
207 return mach_msg(msg
, MACH_RCV_MSG
,
208 0, msg
->msgh_size
, msg
->msgh_local_port
,
209 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
214 mach_msg_destroy_port(mach_port_t port
, mach_msg_type_name_t type
)
216 if (MACH_PORT_VALID(port
)) switch (type
) {
217 case MACH_MSG_TYPE_MOVE_SEND
:
218 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
219 /* destroy the send/send-once right */
220 (void) mach_port_deallocate(mach_task_self(), port
);
223 case MACH_MSG_TYPE_MOVE_RECEIVE
:
224 /* destroy the receive right */
225 (void) mach_port_mod_refs(mach_task_self(), port
,
226 MACH_PORT_RIGHT_RECEIVE
, -1);
229 case MACH_MSG_TYPE_MAKE_SEND
:
230 /* create a send right and then destroy it */
231 (void) mach_port_insert_right(mach_task_self(), port
,
232 port
, MACH_MSG_TYPE_MAKE_SEND
);
233 (void) mach_port_deallocate(mach_task_self(), port
);
236 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
237 /* create a send-once right and then destroy it */
238 (void) mach_port_extract_right(mach_task_self(), port
,
239 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
241 (void) mach_port_deallocate(mach_task_self(), port
);
247 mach_msg_destroy_memory(vm_offset_t addr
, vm_size_t size
)
250 (void) vm_deallocate(mach_task_self(), addr
, size
);
255 * Routine: mach_msg_destroy
257 * mach_msg_destroy is useful in two contexts.
259 * First, it can deallocate all port rights and
260 * out-of-line memory in a received message.
261 * When a server receives a request it doesn't want,
262 * it needs this functionality.
264 * Second, it can mimic the side-effects of a msg-send
265 * operation. The effect is as if the message were sent
266 * and then destroyed inside the kernel. When a server
267 * can't send a reply (because the client died),
268 * it needs this functionality.
271 mach_msg_destroy(mach_msg_header_t
*msg
)
273 mach_msg_bits_t mbits
= msg
->msgh_bits
;
276 * The msgh_local_port field doesn't hold a port right.
277 * The receive operation consumes the destination port right.
280 mach_msg_destroy_port(msg
->msgh_remote_port
, MACH_MSGH_BITS_REMOTE(mbits
));
282 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
283 mach_msg_body_t
*body
;
284 mach_msg_descriptor_t
*saddr
, *eaddr
;
286 body
= (mach_msg_body_t
*) (msg
+ 1);
287 saddr
= (mach_msg_descriptor_t
*)
288 ((mach_msg_base_t
*) msg
+ 1);
289 eaddr
= saddr
+ body
->msgh_descriptor_count
;
291 for ( ; saddr
< eaddr
; saddr
++) {
292 switch (saddr
->type
.type
) {
294 case MACH_MSG_PORT_DESCRIPTOR
: {
295 mach_msg_port_descriptor_t
*dsc
;
298 * Destroy port rights carried in the message
301 mach_msg_destroy_port(dsc
->name
, dsc
->disposition
);
305 case MACH_MSG_OOL_DESCRIPTOR
: {
306 mach_msg_ool_descriptor_t
*dsc
;
309 * Destroy memory carried in the message
311 dsc
= &saddr
->out_of_line
;
312 if (dsc
->deallocate
) {
313 mach_msg_destroy_memory((vm_offset_t
)dsc
->address
,
319 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
321 mach_msg_ool_ports_descriptor_t
*dsc
;
322 mach_msg_type_number_t j
;
325 * Destroy port rights carried in the message
327 dsc
= &saddr
->ool_ports
;
328 ports
= (mach_port_t
*) dsc
->address
;
329 for (j
= 0; j
< dsc
->count
; j
++, ports
++) {
330 mach_msg_destroy_port(*ports
, dsc
->disposition
);
334 * Destroy memory carried in the message
336 if (dsc
->deallocate
) {
337 mach_msg_destroy_memory((vm_offset_t
)dsc
->address
,
338 dsc
->count
* sizeof(mach_port_t
));
348 * Routine: mach_msg_server_once
350 * A simple generic server function. It allows more flexibility
351 * than mach_msg_server by processing only one message request
352 * and then returning to the user. Note that more in the way
353 * of error codes are returned to the user; specifically, any
354 * failing error from mach_msg calls will be returned
355 * (though errors from the demux routine or the routine it
356 * calls will not be).
359 mach_msg_server_once(
360 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
361 mach_msg_size_t max_size
,
362 mach_port_t rcv_name
,
363 mach_msg_options_t options
)
365 mig_reply_error_t
*bufRequest
, *bufReply
;
366 mach_msg_size_t request_size
;
367 mach_msg_size_t request_alloc
;
368 mach_msg_size_t trailer_alloc
;
369 mach_msg_size_t reply_alloc
;
370 mach_msg_return_t mr
;
372 mach_port_t self
= mach_task_self();
374 options
&= ~(MACH_SEND_MSG
|MACH_RCV_MSG
);
376 trailer_alloc
= REQUESTED_TRAILER_SIZE(options
);
377 request_alloc
= round_page(max_size
+ trailer_alloc
);
379 request_size
= (options
& MACH_RCV_LARGE
) ?
380 request_alloc
: max_size
+ trailer_alloc
;
382 reply_alloc
= round_page((options
& MACH_SEND_TRAILER
) ?
383 (max_size
+ MAX_TRAILER_SIZE
) :
386 kr
= vm_allocate(self
,
387 (vm_address_t
*)&bufReply
,
389 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
390 if (kr
!= KERN_SUCCESS
)
394 mach_msg_size_t new_request_alloc
;
396 kr
= vm_allocate(self
,
397 (vm_address_t
*)&bufRequest
,
399 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
400 if (kr
!= KERN_SUCCESS
) {
402 (vm_address_t
)bufReply
,
407 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
408 0, request_size
, rcv_name
,
409 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
411 if (!((mr
== MACH_RCV_TOO_LARGE
) && (options
& MACH_RCV_LARGE
)))
414 new_request_alloc
= round_page(bufRequest
->Head
.msgh_size
+
417 (vm_address_t
) bufRequest
,
419 request_size
= request_alloc
= new_request_alloc
;
422 if (mr
== MACH_MSG_SUCCESS
) {
423 /* we have a request message */
425 (void) (*demux
)(&bufRequest
->Head
, &bufReply
->Head
);
427 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
428 if (bufReply
->RetCode
== MIG_NO_REPLY
)
429 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
430 else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
431 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
432 /* destroy the request - but not the reply port */
433 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
434 mach_msg_destroy(&bufRequest
->Head
);
439 * We don't want to block indefinitely because the client
440 * isn't receiving messages from the reply port.
441 * If we have a send-once right for the reply port, then
442 * this isn't a concern because the send won't block.
443 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
444 * To avoid falling off the kernel's fast RPC path unnecessarily,
445 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
447 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
449 mr
= mach_msg(&bufReply
->Head
,
450 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
451 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
452 MACH_SEND_MSG
|options
:
453 MACH_SEND_MSG
|MACH_SEND_TIMEOUT
|options
,
454 bufReply
->Head
.msgh_size
, 0, MACH_PORT_NULL
,
455 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
457 if ((mr
!= MACH_SEND_INVALID_DEST
) &&
458 (mr
!= MACH_SEND_TIMED_OUT
))
460 mr
= MACH_MSG_SUCCESS
;
462 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
463 mach_msg_destroy(&bufReply
->Head
);
467 (void)vm_deallocate(self
,
468 (vm_address_t
) bufRequest
,
470 (void)vm_deallocate(self
,
471 (vm_address_t
) bufReply
,
477 * Routine: mach_msg_server
479 * A simple generic server function. Note that changes here
480 * should be considered for duplication above.
484 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
485 mach_msg_size_t max_size
,
486 mach_port_t rcv_name
,
487 mach_msg_options_t options
)
489 mig_reply_error_t
*bufRequest
, *bufReply
;
490 mach_msg_size_t request_size
;
491 mach_msg_size_t new_request_alloc
;
492 mach_msg_size_t request_alloc
;
493 mach_msg_size_t trailer_alloc
;
494 mach_msg_size_t reply_alloc
;
495 mach_msg_return_t mr
;
497 mach_port_t self
= mach_task_self();
499 options
&= ~(MACH_SEND_MSG
|MACH_RCV_MSG
);
501 reply_alloc
= round_page((options
& MACH_SEND_TRAILER
) ?
502 (max_size
+ MAX_TRAILER_SIZE
) : max_size
);
504 kr
= vm_allocate(self
,
505 (vm_address_t
*)&bufReply
,
507 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
508 if (kr
!= KERN_SUCCESS
)
512 trailer_alloc
= REQUESTED_TRAILER_SIZE(options
);
513 new_request_alloc
= round_page(max_size
+ trailer_alloc
);
515 request_size
= (options
& MACH_RCV_LARGE
) ?
516 new_request_alloc
: max_size
+ trailer_alloc
;
519 if (request_alloc
< new_request_alloc
) {
520 request_alloc
= new_request_alloc
;
521 kr
= vm_allocate(self
,
522 (vm_address_t
*)&bufRequest
,
524 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|TRUE
);
525 if (kr
!= KERN_SUCCESS
) {
527 (vm_address_t
)bufReply
,
533 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
534 0, request_size
, rcv_name
,
535 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
537 while (mr
== MACH_MSG_SUCCESS
) {
538 /* we have another request message */
540 (void) (*demux
)(&bufRequest
->Head
, &bufReply
->Head
);
542 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
543 if (bufReply
->RetCode
== MIG_NO_REPLY
)
544 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
545 else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
546 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
547 /* destroy the request - but not the reply port */
548 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
549 mach_msg_destroy(&bufRequest
->Head
);
554 * We don't want to block indefinitely because the client
555 * isn't receiving messages from the reply port.
556 * If we have a send-once right for the reply port, then
557 * this isn't a concern because the send won't block.
558 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
559 * To avoid falling off the kernel's fast RPC path,
560 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
562 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
563 if (request_alloc
== reply_alloc
) {
564 mig_reply_error_t
*bufTemp
;
568 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
569 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
570 MACH_SEND_MSG
|MACH_RCV_MSG
|options
:
571 MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_SEND_TIMEOUT
|options
,
572 bufReply
->Head
.msgh_size
, request_size
, rcv_name
,
573 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
575 /* swap request and reply */
576 bufTemp
= bufRequest
;
577 bufRequest
= bufReply
;
581 mr
= mach_msg_overwrite(
583 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
584 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
585 MACH_SEND_MSG
|MACH_RCV_MSG
|options
:
586 MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_SEND_TIMEOUT
|options
,
587 bufReply
->Head
.msgh_size
, request_size
, rcv_name
,
588 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
,
589 &bufRequest
->Head
, request_size
);
592 if ((mr
!= MACH_SEND_INVALID_DEST
) &&
593 (mr
!= MACH_SEND_TIMED_OUT
))
596 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
597 mach_msg_destroy(&bufReply
->Head
);
599 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
|options
,
600 0, request_size
, rcv_name
,
601 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
603 } /* while (mr == MACH_MSG_SUCCESS) */
605 if ((mr
== MACH_RCV_TOO_LARGE
) && (options
& MACH_RCV_LARGE
)) {
606 new_request_alloc
= round_page(bufRequest
->Head
.msgh_size
+
608 request_size
= new_request_alloc
;
610 (vm_address_t
) bufRequest
,
619 (void)vm_deallocate(self
,
620 (vm_address_t
) bufRequest
,
622 (void)vm_deallocate(self
,
623 (vm_address_t
) bufReply
,