2 * Copyright (c) 2000 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@
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.
53 * File: ipc/ipc_kmsg.c
57 * Operations on kernel messages.
63 #include <mach/boolean.h>
64 #include <mach/kern_return.h>
65 #include <mach/message.h>
66 #include <mach/port.h>
67 #include <mach/vm_statistics.h>
68 #include <kern/assert.h>
69 #include <kern/kalloc.h>
70 #include <kern/thread.h>
71 #include <kern/sched_prim.h>
73 #include <kern/misc_protos.h>
74 #include <kern/counters.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_kern.h>
79 #include <ipc/ipc_entry.h>
80 #include <ipc/ipc_kmsg.h>
81 #include <ipc/ipc_notify.h>
82 #include <ipc/ipc_object.h>
83 #include <ipc/ipc_space.h>
84 #include <ipc/ipc_port.h>
85 #include <ipc/ipc_right.h>
86 #include <ipc/ipc_hash.h>
87 #include <ipc/ipc_table.h>
91 extern vm_map_t ipc_kernel_copy_map
;
92 extern vm_size_t ipc_kmsg_max_vm_space
;
93 extern vm_size_t msg_ool_size_small
;
95 #define MSG_OOL_SIZE_SMALL msg_ool_size_small
99 * Forward declarations
105 void ipc_kmsg_clean_body(
107 mach_msg_type_number_t number
);
109 void ipc_kmsg_clean_partial(
111 mach_msg_type_number_t number
,
115 mach_msg_return_t
ipc_kmsg_copyout_body(
119 mach_msg_body_t
*slist
);
121 mach_msg_return_t
ipc_kmsg_copyin_body(
126 void ikm_cache_init(void);
128 * We keep a per-processor cache of kernel message buffers.
129 * The cache saves the overhead/locking of using kalloc/kfree.
130 * The per-processor cache seems to miss less than a per-thread cache,
131 * and it also uses less memory. Access to the cache doesn't
134 #define IKM_STASH 16 /* # of cache entries per cpu */
135 ipc_kmsg_t ipc_kmsg_cache
[ NCPUS
][ IKM_STASH
];
136 unsigned int ipc_kmsg_cache_avail
[NCPUS
];
139 * Routine: ipc_kmsg_init
141 * Initialize the kmsg system. For each CPU, we need to
142 * pre-stuff the kmsg cache.
149 for (cpu
= 0; cpu
< NCPUS
; ++cpu
) {
150 for (i
= 0; i
< IKM_STASH
; ++i
) {
154 kalloc(ikm_plus_overhead(IKM_SAVED_MSG_SIZE
));
155 if (kmsg
== IKM_NULL
)
156 panic("ipc_kmsg_init");
157 ikm_init(kmsg
, IKM_SAVED_MSG_SIZE
);
158 ipc_kmsg_cache
[cpu
][i
] = kmsg
;
160 ipc_kmsg_cache_avail
[cpu
] = IKM_STASH
;
165 * Routine: ipc_kmsg_alloc
167 * Allocate a kernel message structure. If we can get one from
168 * the cache, that is best. Otherwise, allocate a new one.
174 mach_msg_size_t msg_and_trailer_size
)
178 if ((msg_and_trailer_size
<= IKM_SAVED_MSG_SIZE
)) {
181 disable_preemption();
183 if ((i
= ipc_kmsg_cache_avail
[cpu
]) > 0) {
184 assert(i
<= IKM_STASH
);
185 kmsg
= ipc_kmsg_cache
[cpu
][--i
];
186 ipc_kmsg_cache_avail
[cpu
] = i
;
187 ikm_check_init(kmsg
, IKM_SAVED_MSG_SIZE
);
194 /* round up for ikm_cache */
195 if (msg_and_trailer_size
< IKM_SAVED_MSG_SIZE
)
196 msg_and_trailer_size
= IKM_SAVED_MSG_SIZE
;
198 kmsg
= (ipc_kmsg_t
)kalloc(ikm_plus_overhead(msg_and_trailer_size
));
199 if (kmsg
!= IKM_NULL
) {
200 ikm_init(kmsg
, msg_and_trailer_size
);
206 * Routine: ipc_kmsg_free
208 * Free a kernel message buffer. If the kms is preallocated
209 * to a port, just "put it back (marked unused)." We have to
210 * do this with the port locked. The port may have its hold
211 * on our message released. In that case, we have to just
212 * revert the message to a traditional one and free it normally.
221 mach_msg_size_t size
= kmsg
->ikm_size
;
225 * Check to see if the message is bound to the port. If so,
226 * mark it not in use. If the port isn't already dead, then
227 * leave the message associated with it. Otherwise, free it
228 * (not to the cache).
230 port
= ikm_prealloc_inuse_port(kmsg
);
231 if (port
!= IP_NULL
) {
233 ikm_prealloc_clear_inuse(kmsg
, port
);
234 if (ip_active(port
) && (port
->ip_premsg
== kmsg
)) {
235 assert(IP_PREALLOC(port
));
244 * Peek and see if it has to go back in the cache.
246 if (kmsg
->ikm_size
== IKM_SAVED_MSG_SIZE
&&
247 ipc_kmsg_cache_avail
[cpu_number()] < IKM_STASH
) {
250 disable_preemption();
253 i
= ipc_kmsg_cache_avail
[cpu
];
256 ipc_kmsg_cache
[cpu
][i
] = kmsg
;
257 ipc_kmsg_cache_avail
[cpu
] = i
+ 1;
265 kfree((vm_offset_t
) kmsg
, ikm_plus_overhead(size
));
270 * Routine: ipc_kmsg_enqueue
277 ipc_kmsg_queue_t queue
,
280 ipc_kmsg_enqueue_macro(queue
, kmsg
);
284 * Routine: ipc_kmsg_dequeue
286 * Dequeue and return a kmsg.
291 ipc_kmsg_queue_t queue
)
295 first
= ipc_kmsg_queue_first(queue
);
297 if (first
!= IKM_NULL
)
298 ipc_kmsg_rmqueue_first_macro(queue
, first
);
304 * Routine: ipc_kmsg_rmqueue
306 * Pull a kmsg out of a queue.
311 ipc_kmsg_queue_t queue
,
314 ipc_kmsg_t next
, prev
;
316 assert(queue
->ikmq_base
!= IKM_NULL
);
318 next
= kmsg
->ikm_next
;
319 prev
= kmsg
->ikm_prev
;
322 assert(prev
== kmsg
);
323 assert(queue
->ikmq_base
== kmsg
);
325 queue
->ikmq_base
= IKM_NULL
;
327 if (queue
->ikmq_base
== kmsg
)
328 queue
->ikmq_base
= next
;
330 next
->ikm_prev
= prev
;
331 prev
->ikm_next
= next
;
333 /* XXX Temporary debug logic */
334 assert(kmsg
->ikm_next
= IKM_BOGUS
);
335 assert(kmsg
->ikm_prev
= IKM_BOGUS
);
339 * Routine: ipc_kmsg_queue_next
341 * Return the kmsg following the given kmsg.
342 * (Or IKM_NULL if it is the last one in the queue.)
347 ipc_kmsg_queue_t queue
,
352 assert(queue
->ikmq_base
!= IKM_NULL
);
354 next
= kmsg
->ikm_next
;
355 if (queue
->ikmq_base
== next
)
362 * Routine: ipc_kmsg_destroy
364 * Destroys a kernel message. Releases all rights,
365 * references, and memory held by the message.
375 ipc_kmsg_queue_t queue
;
379 * ipc_kmsg_clean can cause more messages to be destroyed.
380 * Curtail recursion by queueing messages. If a message
381 * is already queued, then this is a recursive call.
384 queue
= &(current_thread()->ith_messages
);
385 empty
= ipc_kmsg_queue_empty(queue
);
386 ipc_kmsg_enqueue(queue
, kmsg
);
389 /* must leave kmsg in queue while cleaning it */
391 while ((kmsg
= ipc_kmsg_queue_first(queue
)) != IKM_NULL
) {
392 ipc_kmsg_clean(kmsg
);
393 ipc_kmsg_rmqueue(queue
, kmsg
);
400 * Routine: ipc_kmsg_destroy_dest
402 * Destroys a kernel message. Releases all rights,
403 * references, and memory held by the message (including
404 * the destination port reference.
410 ipc_kmsg_destroy_dest(
415 port
= kmsg
->ikm_header
.msgh_remote_port
;
417 ipc_port_release(port
);
418 kmsg
->ikm_header
.msgh_remote_port
= MACH_PORT_NULL
;
419 ipc_kmsg_destroy(kmsg
);
423 * Routine: ipc_kmsg_clean_body
425 * Cleans the body of a kernel message.
426 * Releases all rights, references, and memory.
435 mach_msg_type_number_t number
)
437 mach_msg_descriptor_t
*saddr
, *eaddr
;
442 saddr
= (mach_msg_descriptor_t
*)
443 ((mach_msg_base_t
*) &kmsg
->ikm_header
+ 1);
444 eaddr
= saddr
+ number
;
446 for ( ; saddr
< eaddr
; saddr
++ ) {
448 switch (saddr
->type
.type
) {
450 case MACH_MSG_PORT_DESCRIPTOR
: {
451 mach_msg_port_descriptor_t
*dsc
;
456 * Destroy port rights carried in the message
458 if (!IO_VALID((ipc_object_t
) dsc
->name
))
460 ipc_object_destroy((ipc_object_t
) dsc
->name
, dsc
->disposition
);
463 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
464 case MACH_MSG_OOL_DESCRIPTOR
: {
465 mach_msg_ool_descriptor_t
*dsc
;
467 dsc
= &saddr
->out_of_line
;
470 * Destroy memory carried in the message
472 if (dsc
->size
== 0) {
473 assert(dsc
->address
== (void *) 0);
475 vm_map_copy_discard((vm_map_copy_t
) dsc
->address
);
479 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
480 ipc_object_t
*objects
;
481 mach_msg_type_number_t j
;
482 mach_msg_ool_ports_descriptor_t
*dsc
;
484 dsc
= &saddr
->ool_ports
;
485 objects
= (ipc_object_t
*) dsc
->address
;
487 if (dsc
->count
== 0) {
491 assert(objects
!= (ipc_object_t
*) 0);
493 /* destroy port rights carried in the message */
495 for (j
= 0; j
< dsc
->count
; j
++) {
496 ipc_object_t object
= objects
[j
];
498 if (!IO_VALID(object
))
501 ipc_object_destroy(object
, dsc
->disposition
);
504 /* destroy memory carried in the message */
506 assert(dsc
->count
!= 0);
508 kfree((vm_offset_t
) dsc
->address
,
509 (vm_size_t
) dsc
->count
* sizeof(mach_port_name_t
));
513 printf("cleanup: don't understand this type of descriptor\n");
520 * Routine: ipc_kmsg_clean_partial
522 * Cleans a partially-acquired kernel message.
523 * number is the index of the type descriptor
524 * in the body of the message that contained the error.
525 * If dolast, the memory and port rights in this last
526 * type spec are also cleaned. In that case, number
527 * specifies the number of port rights to clean.
533 ipc_kmsg_clean_partial(
535 mach_msg_type_number_t number
,
540 mach_msg_bits_t mbits
= kmsg
->ikm_header
.msgh_bits
;
542 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
543 assert(IO_VALID(object
));
544 ipc_object_destroy(object
, MACH_MSGH_BITS_REMOTE(mbits
));
546 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
547 if (IO_VALID(object
))
548 ipc_object_destroy(object
, MACH_MSGH_BITS_LOCAL(mbits
));
551 (void) vm_deallocate(ipc_kernel_copy_map
, paddr
, length
);
554 ipc_kmsg_clean_body(kmsg
, number
);
558 * Routine: ipc_kmsg_clean
560 * Cleans a kernel message. Releases all rights,
561 * references, and memory held by the message.
571 mach_msg_bits_t mbits
;
573 mbits
= kmsg
->ikm_header
.msgh_bits
;
574 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
575 if (IO_VALID(object
))
576 ipc_object_destroy(object
, MACH_MSGH_BITS_REMOTE(mbits
));
578 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
579 if (IO_VALID(object
))
580 ipc_object_destroy(object
, MACH_MSGH_BITS_LOCAL(mbits
));
582 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
583 mach_msg_body_t
*body
;
585 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
586 ipc_kmsg_clean_body(kmsg
, body
->msgh_descriptor_count
);
591 * Routine: ipc_kmsg_set_prealloc
593 * Assign a kmsg as a preallocated message buffer to a port.
599 ipc_kmsg_set_prealloc(
603 assert(kmsg
->ikm_prealloc
== IP_NULL
);
605 kmsg
->ikm_prealloc
= IP_NULL
;
606 IP_SET_PREALLOC(port
, kmsg
);
610 * Routine: ipc_kmsg_clear_prealloc
612 * Release the Assignment of a preallocated message buffer from a port.
617 ipc_kmsg_clear_prealloc(
621 assert(kmsg
->ikm_prealloc
== port
);
623 kmsg
->ikm_prealloc
= IP_NULL
;
624 IP_CLEAR_PREALLOC(port
, kmsg
);
628 * Routine: ipc_kmsg_get
630 * Allocates a kernel message buffer.
631 * Copies a user message to the message buffer.
635 * MACH_MSG_SUCCESS Acquired a message buffer.
636 * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
637 * MACH_SEND_MSG_TOO_SMALL Message size not long-word multiple.
638 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
639 * MACH_SEND_INVALID_DATA Couldn't copy message data.
644 mach_msg_header_t
*msg
,
645 mach_msg_size_t size
,
648 mach_msg_size_t msg_and_trailer_size
;
650 mach_msg_format_0_trailer_t
*trailer
;
651 mach_port_name_t dest_name
;
652 ipc_entry_t dest_entry
;
653 ipc_port_t dest_port
;
655 if ((size
< sizeof(mach_msg_header_t
)) || (size
& 3))
656 return MACH_SEND_MSG_TOO_SMALL
;
658 msg_and_trailer_size
= size
+ MAX_TRAILER_SIZE
;
660 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
662 if (kmsg
== IKM_NULL
)
663 return MACH_SEND_NO_BUFFER
;
665 if (copyinmsg((char *) msg
, (char *) &kmsg
->ikm_header
, size
)) {
667 return MACH_SEND_INVALID_DATA
;
670 kmsg
->ikm_header
.msgh_size
= size
;
673 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
674 * However, the internal size field of the trailer (msgh_trailer_size)
675 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
676 * the cases where no implicit data is requested.
678 trailer
= (mach_msg_format_0_trailer_t
*) ((vm_offset_t
)&kmsg
->ikm_header
+ size
);
679 trailer
->msgh_sender
= current_thread()->top_act
->task
->sec_token
;
680 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
681 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
684 return MACH_MSG_SUCCESS
;
688 * Routine: ipc_kmsg_get_from_kernel
690 * Allocates a kernel message buffer.
691 * Copies a kernel message to the message buffer.
692 * Only resource errors are allowed.
695 * Ports in header are ipc_port_t.
697 * MACH_MSG_SUCCESS Acquired a message buffer.
698 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
702 ipc_kmsg_get_from_kernel(
703 mach_msg_header_t
*msg
,
704 mach_msg_size_t size
,
708 mach_msg_size_t msg_and_trailer_size
;
709 mach_msg_format_0_trailer_t
*trailer
;
710 ipc_port_t dest_port
;
712 assert(size
>= sizeof(mach_msg_header_t
));
713 assert((size
& 3) == 0);
715 assert(IP_VALID((ipc_port_t
) msg
->msgh_remote_port
));
716 dest_port
= (ipc_port_t
)msg
->msgh_remote_port
;
718 msg_and_trailer_size
= size
+ MAX_TRAILER_SIZE
;
721 * See if the port has a pre-allocated kmsg for kernel
722 * clients. These are set up for those kernel clients
723 * which cannot afford to wait.
725 if (IP_PREALLOC(dest_port
)) {
727 if (!ip_active(dest_port
)) {
728 ip_unlock(dest_port
);
729 return MACH_SEND_NO_BUFFER
;
731 assert(IP_PREALLOC(dest_port
));
732 kmsg
= dest_port
->ip_premsg
;
733 if (msg_and_trailer_size
> kmsg
->ikm_size
) {
734 ip_unlock(dest_port
);
735 return MACH_SEND_TOO_LARGE
;
737 if (ikm_prealloc_inuse(kmsg
)) {
738 ip_unlock(dest_port
);
739 return MACH_SEND_NO_BUFFER
;
741 ikm_prealloc_set_inuse(kmsg
, dest_port
);
742 ip_unlock(dest_port
);
744 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
745 if (kmsg
== IKM_NULL
)
746 return MACH_SEND_NO_BUFFER
;
749 (void) memcpy((void *) &kmsg
->ikm_header
, (const void *) msg
, size
);
751 kmsg
->ikm_header
.msgh_size
= size
;
754 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
755 * However, the internal size field of the trailer (msgh_trailer_size)
756 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to
757 * optimize the cases where no implicit data is requested.
759 trailer
= (mach_msg_format_0_trailer_t
*)
760 ((vm_offset_t
)&kmsg
->ikm_header
+ size
);
761 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
762 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
763 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
766 return MACH_MSG_SUCCESS
;
770 * Routine: ipc_kmsg_send
772 * Send a message. The message holds a reference
773 * for the destination port in the msgh_remote_port field.
775 * If unsuccessful, the caller still has possession of
776 * the message and must do something with it. If successful,
777 * the message is queued, given to a receiver, destroyed,
778 * or handled directly by the kernel via mach_msg.
782 * MACH_MSG_SUCCESS The message was accepted.
783 * MACH_SEND_TIMED_OUT Caller still has message.
784 * MACH_SEND_INTERRUPTED Caller still has message.
789 mach_msg_option_t option
,
790 mach_msg_timeout_t timeout
)
792 kern_return_t save_wait_result
;
795 port
= (ipc_port_t
) kmsg
->ikm_header
.msgh_remote_port
;
796 assert(IP_VALID(port
));
800 if (port
->ip_receiver
== ipc_space_kernel
) {
803 * We can check ip_receiver == ipc_space_kernel
804 * before checking that the port is active because
805 * ipc_port_dealloc_kernel clears ip_receiver
806 * before destroying a kernel port.
808 assert(ip_active(port
));
809 port
->ip_messages
.imq_seqno
++;
812 current_task()->messages_sent
++;
815 * Call the server routine, and get the reply message to send.
817 kmsg
= ipc_kobject_server(kmsg
);
818 if (kmsg
== IKM_NULL
)
819 return MACH_MSG_SUCCESS
;
821 port
= (ipc_port_t
) kmsg
->ikm_header
.msgh_remote_port
;
822 assert(IP_VALID(port
));
824 /* fall thru with reply - same options */
828 * Can't deliver to a dead port.
829 * However, we can pretend it got sent
830 * and was then immediately destroyed.
832 if (!ip_active(port
)) {
834 * We can't let ipc_kmsg_destroy deallocate
835 * the port right, because we might end up
836 * in an infinite loop trying to deliver
837 * a send-once notification.
841 ip_check_unlock(port
);
842 kmsg
->ikm_header
.msgh_remote_port
= MACH_PORT_NULL
;
843 ipc_kmsg_destroy(kmsg
);
844 return MACH_MSG_SUCCESS
;
847 if (kmsg
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_CIRCULAR
) {
850 /* don't allow the creation of a circular loop */
852 ipc_kmsg_destroy(kmsg
);
853 return MACH_MSG_SUCCESS
;
857 * We have a valid message and a valid reference on the port.
858 * we can unlock the port and call mqueue_send() on it's message
862 return (ipc_mqueue_send(&port
->ip_messages
, kmsg
, option
, timeout
));
866 * Routine: ipc_kmsg_put
868 * Copies a message buffer to a user message.
869 * Copies only the specified number of bytes.
870 * Frees the message buffer.
872 * Nothing locked. The message buffer must have clean
875 * MACH_MSG_SUCCESS Copied data out of message buffer.
876 * MACH_RCV_INVALID_DATA Couldn't copy to user message.
881 mach_msg_header_t
*msg
,
883 mach_msg_size_t size
)
885 mach_msg_return_t mr
;
887 if (copyoutmsg((const char *) &kmsg
->ikm_header
, (char *) msg
, size
))
888 mr
= MACH_RCV_INVALID_DATA
;
890 mr
= MACH_MSG_SUCCESS
;
897 * Routine: ipc_kmsg_put_to_kernel
899 * Copies a message buffer to a kernel message.
900 * Frees the message buffer.
907 ipc_kmsg_put_to_kernel(
908 mach_msg_header_t
*msg
,
910 mach_msg_size_t size
)
912 (void) memcpy((void *) msg
, (const void *) &kmsg
->ikm_header
, size
);
918 * Routine: ipc_kmsg_copyin_header
920 * "Copy-in" port rights in the header of a message.
921 * Operates atomically; if it doesn't succeed the
922 * message header and the space are left untouched.
923 * If it does succeed the remote/local port fields
924 * contain object pointers instead of port names,
925 * and the bits field is updated. The destination port
926 * will be a valid port pointer.
928 * The notify argument implements the MACH_SEND_CANCEL option.
929 * If it is not MACH_PORT_NULL, it should name a receive right.
930 * If the processing of the destination port would generate
931 * a port-deleted notification (because the right for the
932 * destination port is destroyed and it had a request for
933 * a dead-name notification registered), and the port-deleted
934 * notification would be sent to the named receive right,
935 * then it isn't sent and the send-once right for the notify
936 * port is quietly destroyed.
941 * MACH_MSG_SUCCESS Successful copyin.
942 * MACH_SEND_INVALID_HEADER
943 * Illegal value in the message header bits.
944 * MACH_SEND_INVALID_DEST The space is dead.
945 * MACH_SEND_INVALID_NOTIFY
946 * Notify is non-null and doesn't name a receive right.
947 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
948 * MACH_SEND_INVALID_DEST Can't copyin destination port.
949 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
950 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
951 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
955 ipc_kmsg_copyin_header(
956 mach_msg_header_t
*msg
,
958 mach_port_name_t notify
)
960 mach_msg_bits_t mbits
= msg
->msgh_bits
& MACH_MSGH_BITS_USER
;
961 mach_port_name_t dest_name
= (mach_port_name_t
)msg
->msgh_remote_port
;
962 mach_port_name_t reply_name
= (mach_port_name_t
)msg
->msgh_local_port
;
965 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
966 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
967 ipc_object_t dest_port
, reply_port
;
968 ipc_port_t dest_soright
, reply_soright
;
969 ipc_port_t notify_port
;
971 if ((mbits
!= msg
->msgh_bits
) ||
972 (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type
)) ||
974 (reply_name
!= MACH_PORT_NULL
) :
975 !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type
)))
976 return MACH_SEND_INVALID_HEADER
;
978 reply_soright
= IP_NULL
; /* in case we go to invalid dest early */
980 is_write_lock(space
);
981 if (!space
->is_active
)
984 if (!MACH_PORT_VALID(dest_name
))
987 if (notify
!= MACH_PORT_NULL
) {
990 if ((entry
= ipc_entry_lookup(space
, notify
)) == IE_NULL
) {
991 is_write_unlock(space
);
992 return MACH_SEND_INVALID_NOTIFY
;
994 if((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) == 0) {
995 is_write_unlock(space
);
996 return MACH_SEND_INVALID_NOTIFY
;
999 notify_port
= (ipc_port_t
) entry
->ie_object
;
1002 if (dest_name
== reply_name
) {
1004 mach_port_name_t name
= dest_name
;
1007 * Destination and reply ports are the same!
1008 * This is a little tedious to make atomic, because
1009 * there are 25 combinations of dest_type/reply_type.
1010 * However, most are easy. If either is move-sonce,
1011 * then there must be an error. If either are
1012 * make-send or make-sonce, then we must be looking
1013 * at a receive right so the port can't die.
1014 * The hard cases are the combinations of
1015 * copy-send and make-send.
1018 entry
= ipc_entry_lookup(space
, name
);
1019 if (entry
== IE_NULL
)
1022 assert(reply_type
!= 0); /* because name not null */
1024 if (!ipc_right_copyin_check(space
, name
, entry
, reply_type
))
1027 if ((dest_type
== MACH_MSG_TYPE_MOVE_SEND_ONCE
) ||
1028 (reply_type
== MACH_MSG_TYPE_MOVE_SEND_ONCE
)) {
1030 * Why must there be an error? To get a valid
1031 * destination, this entry must name a live
1032 * port (not a dead name or dead port). However
1033 * a successful move-sonce will destroy a
1034 * live entry. Therefore the other copyin,
1035 * whatever it is, would fail. We've already
1036 * checked for reply port errors above,
1037 * so report a destination error.
1041 } else if ((dest_type
== MACH_MSG_TYPE_MAKE_SEND
) ||
1042 (dest_type
== MACH_MSG_TYPE_MAKE_SEND_ONCE
) ||
1043 (reply_type
== MACH_MSG_TYPE_MAKE_SEND
) ||
1044 (reply_type
== MACH_MSG_TYPE_MAKE_SEND_ONCE
)) {
1045 kr
= ipc_right_copyin(space
, name
, entry
,
1047 &dest_port
, &dest_soright
);
1048 if (kr
!= KERN_SUCCESS
)
1052 * Either dest or reply needs a receive right.
1053 * We know the receive right is there, because
1054 * of the copyin_check and copyin calls. Hence
1055 * the port is not in danger of dying. If dest
1056 * used the receive right, then the right needed
1057 * by reply (and verified by copyin_check) will
1061 assert(IO_VALID(dest_port
));
1062 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
1063 assert(dest_soright
== IP_NULL
);
1065 kr
= ipc_right_copyin(space
, name
, entry
,
1067 &reply_port
, &reply_soright
);
1069 assert(kr
== KERN_SUCCESS
);
1070 assert(reply_port
== dest_port
);
1071 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
1072 assert(reply_soright
== IP_NULL
);
1073 } else if ((dest_type
== MACH_MSG_TYPE_COPY_SEND
) &&
1074 (reply_type
== MACH_MSG_TYPE_COPY_SEND
)) {
1076 * To make this atomic, just do one copy-send,
1077 * and dup the send right we get out.
1080 kr
= ipc_right_copyin(space
, name
, entry
,
1082 &dest_port
, &dest_soright
);
1083 if (kr
!= KERN_SUCCESS
)
1086 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND
);
1087 assert(dest_soright
== IP_NULL
);
1090 * It's OK if the port we got is dead now,
1091 * so reply_port is IP_DEAD, because the msg
1092 * won't go anywhere anyway.
1095 reply_port
= (ipc_object_t
)
1096 ipc_port_copy_send((ipc_port_t
) dest_port
);
1097 reply_soright
= IP_NULL
;
1098 } else if ((dest_type
== MACH_MSG_TYPE_MOVE_SEND
) &&
1099 (reply_type
== MACH_MSG_TYPE_MOVE_SEND
)) {
1101 * This is an easy case. Just use our
1102 * handy-dandy special-purpose copyin call
1103 * to get two send rights for the price of one.
1106 kr
= ipc_right_copyin_two(space
, name
, entry
,
1107 &dest_port
, &dest_soright
);
1108 if (kr
!= KERN_SUCCESS
)
1111 /* the entry might need to be deallocated */
1112 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1113 ipc_entry_dealloc(space
, name
, entry
);
1115 reply_port
= dest_port
;
1116 reply_soright
= IP_NULL
;
1120 assert(((dest_type
== MACH_MSG_TYPE_COPY_SEND
) &&
1121 (reply_type
== MACH_MSG_TYPE_MOVE_SEND
)) ||
1122 ((dest_type
== MACH_MSG_TYPE_MOVE_SEND
) &&
1123 (reply_type
== MACH_MSG_TYPE_COPY_SEND
)));
1126 * To make this atomic, just do a move-send,
1127 * and dup the send right we get out.
1130 kr
= ipc_right_copyin(space
, name
, entry
,
1131 MACH_MSG_TYPE_MOVE_SEND
, FALSE
,
1132 &dest_port
, &soright
);
1133 if (kr
!= KERN_SUCCESS
)
1136 /* the entry might need to be deallocated */
1138 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1139 ipc_entry_dealloc(space
, name
, entry
);
1142 * It's OK if the port we got is dead now,
1143 * so reply_port is IP_DEAD, because the msg
1144 * won't go anywhere anyway.
1147 reply_port
= (ipc_object_t
)
1148 ipc_port_copy_send((ipc_port_t
) dest_port
);
1150 if (dest_type
== MACH_MSG_TYPE_MOVE_SEND
) {
1151 dest_soright
= soright
;
1152 reply_soright
= IP_NULL
;
1154 dest_soright
= IP_NULL
;
1155 reply_soright
= soright
;
1158 } else if (!MACH_PORT_VALID(reply_name
)) {
1162 * No reply port! This is an easy case
1163 * to make atomic. Just copyin the destination.
1166 entry
= ipc_entry_lookup(space
, dest_name
);
1167 if (entry
== IE_NULL
)
1170 kr
= ipc_right_copyin(space
, dest_name
, entry
,
1172 &dest_port
, &dest_soright
);
1173 if (kr
!= KERN_SUCCESS
)
1176 /* the entry might need to be deallocated */
1178 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1179 ipc_entry_dealloc(space
, dest_name
, entry
);
1181 reply_port
= (ipc_object_t
) reply_name
;
1182 reply_soright
= IP_NULL
;
1184 ipc_entry_t dest_entry
, reply_entry
;
1185 ipc_port_t saved_reply
;
1188 * This is the tough case to make atomic.
1189 * The difficult problem is serializing with port death.
1190 * At the time we copyin dest_port, it must be alive.
1191 * If reply_port is alive when we copyin it, then
1192 * we are OK, because we serialize before the death
1193 * of both ports. Assume reply_port is dead at copyin.
1194 * Then if dest_port dies/died after reply_port died,
1195 * we are OK, because we serialize between the death
1196 * of the two ports. So the bad case is when dest_port
1197 * dies after its copyin, reply_port dies before its
1198 * copyin, and dest_port dies before reply_port. Then
1199 * the copyins operated as if dest_port was alive
1200 * and reply_port was dead, which shouldn't have happened
1201 * because they died in the other order.
1203 * Note that it is easy for a user task to tell if
1204 * a copyin happened before or after a port died.
1205 * For example, suppose both dest and reply are
1206 * send-once rights (types are both move-sonce) and
1207 * both rights have dead-name requests registered.
1208 * If a port dies before copyin, a dead-name notification
1209 * is generated and the dead name's urefs are incremented,
1210 * and if the copyin happens first, a port-deleted
1211 * notification is generated.
1213 * Note that although the entries are different,
1214 * dest_port and reply_port might still be the same.
1216 * JMM - The code to handle this was too expensive and, anyway,
1217 * we intend to separate the dest lookup from the reply copyin
1218 * by a wide margin, so the user will have to learn to deal!
1219 * I will be making the change soon!
1222 dest_entry
= ipc_entry_lookup(space
, dest_name
);
1223 if (dest_entry
== IE_NULL
)
1226 reply_entry
= ipc_entry_lookup(space
, reply_name
);
1227 if (reply_entry
== IE_NULL
)
1230 assert(dest_entry
!= reply_entry
); /* names are not equal */
1231 assert(reply_type
!= 0); /* because reply_name not null */
1233 if (!ipc_right_copyin_check(space
, reply_name
, reply_entry
,
1237 kr
= ipc_right_copyin(space
, dest_name
, dest_entry
,
1239 &dest_port
, &dest_soright
);
1240 if (kr
!= KERN_SUCCESS
)
1243 assert(IO_VALID(dest_port
));
1245 kr
= ipc_right_copyin(space
, reply_name
, reply_entry
,
1247 &reply_port
, &reply_soright
);
1249 assert(kr
== KERN_SUCCESS
);
1251 /* the entries might need to be deallocated */
1253 if (IE_BITS_TYPE(reply_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1254 ipc_entry_dealloc(space
, reply_name
, reply_entry
);
1256 if (IE_BITS_TYPE(dest_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1257 ipc_entry_dealloc(space
, dest_name
, dest_entry
);
1261 * At this point, dest_port, reply_port,
1262 * dest_soright, reply_soright are all initialized.
1263 * Any defunct entries have been deallocated.
1264 * The space is still write-locked, and we need to
1265 * make the MACH_SEND_CANCEL check. The notify_port pointer
1266 * is still usable, because the copyin code above won't ever
1267 * deallocate a receive right, so its entry still exists
1268 * and holds a ref. Note notify_port might even equal
1269 * dest_port or reply_port.
1272 if ((notify
!= MACH_PORT_NULL
) &&
1273 (dest_soright
== notify_port
)) {
1274 ipc_port_release_sonce(dest_soright
);
1275 dest_soright
= IP_NULL
;
1278 is_write_unlock(space
);
1280 if (dest_soright
!= IP_NULL
)
1281 ipc_notify_port_deleted(dest_soright
, dest_name
);
1283 if (reply_soright
!= IP_NULL
)
1284 ipc_notify_port_deleted(reply_soright
, reply_name
);
1286 dest_type
= ipc_object_copyin_type(dest_type
);
1287 reply_type
= ipc_object_copyin_type(reply_type
);
1289 msg
->msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
1290 MACH_MSGH_BITS(dest_type
, reply_type
));
1291 msg
->msgh_remote_port
= (ipc_port_t
)dest_port
;
1292 msg
->msgh_local_port
= (ipc_port_t
)reply_port
;
1294 return MACH_MSG_SUCCESS
;
1297 is_write_unlock(space
);
1298 return MACH_SEND_INVALID_REPLY
;
1301 is_write_unlock(space
);
1302 if (reply_soright
!= IP_NULL
)
1303 ipc_notify_port_deleted(reply_soright
, reply_name
);
1304 return MACH_SEND_INVALID_DEST
;
1308 * Routine: ipc_kmsg_copyin_body
1310 * "Copy-in" port rights and out-of-line memory
1311 * in the message body.
1313 * In all failure cases, the message is left holding
1314 * no rights or memory. However, the message buffer
1315 * is not deallocated. If successful, the message
1316 * contains a valid destination port.
1320 * MACH_MSG_SUCCESS Successful copyin.
1321 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
1322 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
1323 * MACH_SEND_INVALID_TYPE Bad type specification.
1324 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
1325 * MACH_SEND_INVALID_RT_OOL_SIZE OOL Buffer too large for RT
1326 * MACH_MSG_INVALID_RT_DESCRIPTOR Dealloc and RT are incompatible
1330 ipc_kmsg_copyin_body(
1336 mach_msg_body_t
*body
;
1337 mach_msg_descriptor_t
*saddr
, *eaddr
;
1339 mach_msg_return_t mr
;
1342 vm_size_t space_needed
= 0;
1343 vm_offset_t paddr
= 0;
1344 mach_msg_descriptor_t
*sstart
;
1345 vm_map_copy_t copy
= VM_MAP_COPY_NULL
;
1348 * Determine if the target is a kernel port.
1350 dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
1353 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
1354 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
1355 eaddr
= saddr
+ body
->msgh_descriptor_count
;
1357 /* make sure the message does not ask for more msg descriptors
1358 * than the message can hold.
1361 if (eaddr
<= saddr
||
1362 eaddr
> (mach_msg_descriptor_t
*) (&kmsg
->ikm_header
+
1363 kmsg
->ikm_header
.msgh_size
)) {
1364 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1365 return MACH_SEND_MSG_TOO_SMALL
;
1369 * Make an initial pass to determine kernal VM space requirements for
1372 for (sstart
= saddr
; sstart
< eaddr
; sstart
++) {
1374 if (sstart
->type
.type
== MACH_MSG_OOL_DESCRIPTOR
||
1375 sstart
->type
.type
== MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
1377 if (sstart
->out_of_line
.copy
!= MACH_MSG_PHYSICAL_COPY
&&
1378 sstart
->out_of_line
.copy
!= MACH_MSG_VIRTUAL_COPY
) {
1380 * Invalid copy option
1382 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1383 return MACH_SEND_INVALID_TYPE
;
1386 if ((sstart
->out_of_line
.size
>= MSG_OOL_SIZE_SMALL
) &&
1387 (sstart
->out_of_line
.copy
== MACH_MSG_PHYSICAL_COPY
) &&
1388 !(sstart
->out_of_line
.deallocate
)) {
1391 * Out-of-line memory descriptor, accumulate kernel
1392 * memory requirements
1394 space_needed
+= round_page(sstart
->out_of_line
.size
);
1395 if (space_needed
> ipc_kmsg_max_vm_space
) {
1398 * Per message kernel memory limit exceeded
1400 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1401 return MACH_MSG_VM_KERNEL
;
1408 * Allocate space in the pageable kernel ipc copy map for all the
1409 * ool data that is to be physically copied. Map is marked wait for
1413 if (vm_allocate(ipc_kernel_copy_map
, &paddr
, space_needed
, TRUE
) !=
1415 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1416 return MACH_MSG_VM_KERNEL
;
1421 * handle the OOL regions and port descriptors.
1422 * the check for complex messages was done earlier.
1425 for (i
= 0, sstart
= saddr
; sstart
< eaddr
; sstart
++) {
1427 switch (sstart
->type
.type
) {
1429 case MACH_MSG_PORT_DESCRIPTOR
: {
1430 mach_msg_type_name_t name
;
1431 ipc_object_t object
;
1432 mach_msg_port_descriptor_t
*dsc
;
1434 dsc
= &sstart
->port
;
1436 /* this is really the type SEND, SEND_ONCE, etc. */
1437 name
= dsc
->disposition
;
1438 dsc
->disposition
= ipc_object_copyin_type(name
);
1440 if (!MACH_PORT_VALID((mach_port_name_t
)dsc
->name
)) {
1444 kr
= ipc_object_copyin(space
, (mach_port_name_t
)dsc
->name
, name
, &object
);
1445 if (kr
!= KERN_SUCCESS
) {
1446 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1447 return MACH_SEND_INVALID_RIGHT
;
1449 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1450 ipc_port_check_circularity((ipc_port_t
) object
,
1451 (ipc_port_t
) dest
)) {
1452 kmsg
->ikm_header
.msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1454 dsc
->name
= (ipc_port_t
) object
;
1458 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1459 case MACH_MSG_OOL_DESCRIPTOR
: {
1464 mach_msg_ool_descriptor_t
*dsc
;
1466 dsc
= &sstart
->out_of_line
;
1467 dealloc
= dsc
->deallocate
;
1468 addr
= (vm_offset_t
) dsc
->address
;
1474 } else if ((length
>= MSG_OOL_SIZE_SMALL
) &&
1475 (dsc
->copy
== MACH_MSG_PHYSICAL_COPY
) && !dealloc
) {
1478 * If the request is a physical copy and the source
1479 * is not being deallocated, then allocate space
1480 * in the kernel's pageable ipc copy map and copy
1481 * the data in. The semantics guarantee that the
1482 * data will have been physically copied before
1483 * the send operation terminates. Thus if the data
1484 * is not being deallocated, we must be prepared
1485 * to page if the region is sufficiently large.
1487 if (copyin((const char *) addr
, (char *) paddr
,
1489 ipc_kmsg_clean_partial(kmsg
, i
, paddr
,
1491 return MACH_SEND_INVALID_MEMORY
;
1495 * The kernel ipc copy map is marked no_zero_fill.
1496 * If the transfer is not a page multiple, we need
1497 * to zero fill the balance.
1499 if (!page_aligned(length
)) {
1500 (void) memset((void *) (paddr
+ length
), 0,
1501 round_page(length
) - length
);
1503 if (vm_map_copyin(ipc_kernel_copy_map
, paddr
, length
,
1504 TRUE
, ©
) != KERN_SUCCESS
) {
1505 ipc_kmsg_clean_partial(kmsg
, i
, paddr
,
1507 return MACH_MSG_VM_KERNEL
;
1509 dsc
->address
= (void *) copy
;
1510 paddr
+= round_page(length
);
1511 space_needed
-= round_page(length
);
1515 * Make a vm_map_copy_t of the of the data. If the
1516 * data is small, this will do an optimized physical
1517 * copy. Otherwise, it will do a virtual copy.
1519 * NOTE: A virtual copy is OK if the original is being
1520 * deallocted, even if a physical copy was requested.
1522 kr
= vm_map_copyin(map
, addr
, length
, dealloc
, ©
);
1523 if (kr
!= KERN_SUCCESS
) {
1524 ipc_kmsg_clean_partial(kmsg
,i
,paddr
,space_needed
);
1525 return (kr
== KERN_RESOURCE_SHORTAGE
) ?
1526 MACH_MSG_VM_KERNEL
:
1527 MACH_SEND_INVALID_MEMORY
;
1529 dsc
->address
= (void *) copy
;
1534 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
1538 ipc_object_t
*objects
;
1540 mach_msg_type_name_t name
;
1541 mach_msg_ool_ports_descriptor_t
*dsc
;
1543 dsc
= &sstart
->ool_ports
;
1544 addr
= (vm_offset_t
) dsc
->address
;
1546 /* calculate length of data in bytes, rounding up */
1547 length
= dsc
->count
* sizeof(mach_port_name_t
);
1551 dsc
->address
= (void *) 0;
1555 data
= kalloc(length
);
1558 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1559 return MACH_SEND_NO_BUFFER
;
1562 if (copyinmap(map
, addr
, data
, length
)) {
1563 kfree(data
, length
);
1564 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1565 return MACH_SEND_INVALID_MEMORY
;
1568 if (dsc
->deallocate
) {
1569 (void) vm_deallocate(map
, addr
, length
);
1572 dsc
->address
= (void *) data
;
1574 /* this is really the type SEND, SEND_ONCE, etc. */
1575 name
= dsc
->disposition
;
1576 dsc
->disposition
= ipc_object_copyin_type(name
);
1578 objects
= (ipc_object_t
*) data
;
1580 for ( j
= 0; j
< dsc
->count
; j
++) {
1581 mach_port_name_t port
= (mach_port_name_t
) objects
[j
];
1582 ipc_object_t object
;
1584 if (!MACH_PORT_VALID(port
))
1587 kr
= ipc_object_copyin(space
, port
, name
, &object
);
1589 if (kr
!= KERN_SUCCESS
) {
1592 for(k
= 0; k
< j
; k
++) {
1593 object
= objects
[k
];
1594 if (!MACH_PORT_VALID(port
))
1596 ipc_object_destroy(object
, dsc
->disposition
);
1598 kfree(data
, length
);
1599 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1600 return MACH_SEND_INVALID_RIGHT
;
1603 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1604 ipc_port_check_circularity(
1605 (ipc_port_t
) object
,
1607 kmsg
->ikm_header
.msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1609 objects
[j
] = object
;
1617 * Invalid descriptor
1619 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1620 return MACH_SEND_INVALID_TYPE
;
1627 kmsg
->ikm_header
.msgh_bits
&= ~MACH_MSGH_BITS_COMPLEX
;
1628 return MACH_MSG_SUCCESS
;
1633 * Routine: ipc_kmsg_copyin
1635 * "Copy-in" port rights and out-of-line memory
1638 * In all failure cases, the message is left holding
1639 * no rights or memory. However, the message buffer
1640 * is not deallocated. If successful, the message
1641 * contains a valid destination port.
1645 * MACH_MSG_SUCCESS Successful copyin.
1646 * MACH_SEND_INVALID_HEADER
1647 * Illegal value in the message header bits.
1648 * MACH_SEND_INVALID_NOTIFY Bad notify port.
1649 * MACH_SEND_INVALID_DEST Can't copyin destination port.
1650 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
1651 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
1652 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
1653 * MACH_SEND_INVALID_TYPE Bad type specification.
1654 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
1662 mach_port_name_t notify
)
1664 mach_msg_return_t mr
;
1666 mr
= ipc_kmsg_copyin_header(&kmsg
->ikm_header
, space
, notify
);
1667 if (mr
!= MACH_MSG_SUCCESS
)
1670 if ((kmsg
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
1671 return MACH_MSG_SUCCESS
;
1673 return( ipc_kmsg_copyin_body( kmsg
, space
, map
) );
1677 * Routine: ipc_kmsg_copyin_from_kernel
1679 * "Copy-in" port rights and out-of-line memory
1680 * in a message sent from the kernel.
1682 * Because the message comes from the kernel,
1683 * the implementation assumes there are no errors
1684 * or peculiarities in the message.
1686 * Returns TRUE if queueing the message
1687 * would result in a circularity.
1693 ipc_kmsg_copyin_from_kernel(
1696 mach_msg_bits_t bits
= kmsg
->ikm_header
.msgh_bits
;
1697 mach_msg_type_name_t rname
= MACH_MSGH_BITS_REMOTE(bits
);
1698 mach_msg_type_name_t lname
= MACH_MSGH_BITS_LOCAL(bits
);
1699 ipc_object_t remote
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
1700 ipc_object_t local
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
1702 /* translate the destination and reply ports */
1704 ipc_object_copyin_from_kernel(remote
, rname
);
1705 if (IO_VALID(local
))
1706 ipc_object_copyin_from_kernel(local
, lname
);
1709 * The common case is a complex message with no reply port,
1710 * because that is what the memory_object interface uses.
1713 if (bits
== (MACH_MSGH_BITS_COMPLEX
|
1714 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0))) {
1715 bits
= (MACH_MSGH_BITS_COMPLEX
|
1716 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND
, 0));
1718 kmsg
->ikm_header
.msgh_bits
= bits
;
1720 bits
= (MACH_MSGH_BITS_OTHER(bits
) |
1721 MACH_MSGH_BITS(ipc_object_copyin_type(rname
),
1722 ipc_object_copyin_type(lname
)));
1724 kmsg
->ikm_header
.msgh_bits
= bits
;
1725 if ((bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
1729 mach_msg_descriptor_t
*saddr
, *eaddr
;
1730 mach_msg_body_t
*body
;
1732 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
1733 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
1734 eaddr
= (mach_msg_descriptor_t
*) saddr
+ body
->msgh_descriptor_count
;
1736 for ( ; saddr
< eaddr
; saddr
++) {
1738 switch (saddr
->type
.type
) {
1740 case MACH_MSG_PORT_DESCRIPTOR
: {
1741 mach_msg_type_name_t name
;
1742 ipc_object_t object
;
1743 mach_msg_port_descriptor_t
*dsc
;
1747 /* this is really the type SEND, SEND_ONCE, etc. */
1748 name
= dsc
->disposition
;
1749 object
= (ipc_object_t
) dsc
->name
;
1750 dsc
->disposition
= ipc_object_copyin_type(name
);
1752 if (!IO_VALID(object
)) {
1756 ipc_object_copyin_from_kernel(object
, name
);
1758 /* CDY avoid circularity when the destination is also */
1759 /* the kernel. This check should be changed into an */
1760 /* assert when the new kobject model is in place since*/
1761 /* ports will not be used in kernel to kernel chats */
1763 if (((ipc_port_t
)remote
)->ip_receiver
!= ipc_space_kernel
) {
1764 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1765 ipc_port_check_circularity((ipc_port_t
) object
,
1766 (ipc_port_t
) remote
)) {
1767 kmsg
->ikm_header
.msgh_bits
|=
1768 MACH_MSGH_BITS_CIRCULAR
;
1773 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1774 case MACH_MSG_OOL_DESCRIPTOR
: {
1776 * The sender should supply ready-made memory, i.e.
1777 * a vm_map_copy_t, so we don't need to do anything.
1781 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
1782 ipc_object_t
*objects
;
1784 mach_msg_type_name_t name
;
1785 mach_msg_ool_ports_descriptor_t
*dsc
;
1787 dsc
= &saddr
->ool_ports
;
1789 /* this is really the type SEND, SEND_ONCE, etc. */
1790 name
= dsc
->disposition
;
1791 dsc
->disposition
= ipc_object_copyin_type(name
);
1793 objects
= (ipc_object_t
*) dsc
->address
;
1795 for ( j
= 0; j
< dsc
->count
; j
++) {
1796 ipc_object_t object
= objects
[j
];
1798 if (!IO_VALID(object
))
1801 ipc_object_copyin_from_kernel(object
, name
);
1803 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1804 ipc_port_check_circularity(
1805 (ipc_port_t
) object
,
1806 (ipc_port_t
) remote
))
1807 kmsg
->ikm_header
.msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1813 panic("ipc_kmsg_copyin_from_kernel: bad descriptor");
1814 #endif /* MACH_ASSERT */
1822 * Routine: ipc_kmsg_copyout_header
1824 * "Copy-out" port rights in the header of a message.
1825 * Operates atomically; if it doesn't succeed the
1826 * message header and the space are left untouched.
1827 * If it does succeed the remote/local port fields
1828 * contain port names instead of object pointers,
1829 * and the bits field is updated.
1831 * The notify argument implements the MACH_RCV_NOTIFY option.
1832 * If it is not MACH_PORT_NULL, it should name a receive right.
1833 * If the process of receiving the reply port creates a
1834 * new right in the receiving task, then the new right is
1835 * automatically registered for a dead-name notification,
1836 * with the notify port supplying the send-once right.
1840 * MACH_MSG_SUCCESS Copied out port rights.
1841 * MACH_RCV_INVALID_NOTIFY
1842 * Notify is non-null and doesn't name a receive right.
1843 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
1844 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
1845 * The space is dead.
1846 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
1847 * No room in space for another name.
1848 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
1849 * Couldn't allocate memory for the reply port.
1850 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
1851 * Couldn't allocate memory for the dead-name request.
1855 ipc_kmsg_copyout_header(
1856 mach_msg_header_t
*msg
,
1858 mach_port_name_t notify
)
1860 mach_msg_bits_t mbits
= msg
->msgh_bits
;
1861 ipc_port_t dest
= (ipc_port_t
) msg
->msgh_remote_port
;
1863 assert(IP_VALID(dest
));
1866 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
1867 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
1868 ipc_port_t reply
= (ipc_port_t
) msg
->msgh_local_port
;
1869 mach_port_name_t dest_name
, reply_name
;
1871 if (IP_VALID(reply
)) {
1872 ipc_port_t notify_port
;
1877 * Handling notify (for MACH_RCV_NOTIFY) is tricky.
1878 * The problem is atomically making a send-once right
1879 * from the notify port and installing it for a
1880 * dead-name request in the new entry, because this
1881 * requires two port locks (on the notify port and
1882 * the reply port). However, we can safely make
1883 * and consume send-once rights for the notify port
1884 * as long as we hold the space locked. This isn't
1885 * an atomicity problem, because the only way
1886 * to detect that a send-once right has been created
1887 * and then consumed if it wasn't needed is by getting
1888 * at the receive right to look at ip_sorights, and
1889 * because the space is write-locked status calls can't
1890 * lookup the notify port receive right. When we make
1891 * the send-once right, we lock the notify port,
1892 * so any status calls in progress will be done.
1895 is_write_lock(space
);
1898 ipc_port_request_index_t request
;
1900 if (!space
->is_active
) {
1901 is_write_unlock(space
);
1902 return (MACH_RCV_HEADER_ERROR
|
1903 MACH_MSG_IPC_SPACE
);
1906 if (notify
!= MACH_PORT_NULL
) {
1907 notify_port
= ipc_port_lookup_notify(space
,
1909 if (notify_port
== IP_NULL
) {
1910 is_write_unlock(space
);
1911 return MACH_RCV_INVALID_NOTIFY
;
1914 notify_port
= IP_NULL
;
1916 if ((reply_type
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
1917 ipc_right_reverse(space
, (ipc_object_t
) reply
,
1918 &reply_name
, &entry
)) {
1919 /* reply port is locked and active */
1922 * We don't need the notify_port
1923 * send-once right, but we can't release
1924 * it here because reply port is locked.
1925 * Wait until after the copyout to
1926 * release the notify port right.
1929 assert(entry
->ie_bits
&
1930 MACH_PORT_TYPE_SEND_RECEIVE
);
1935 if (!ip_active(reply
)) {
1937 ip_check_unlock(reply
);
1939 if (notify_port
!= IP_NULL
)
1940 ipc_port_release_sonce(notify_port
);
1943 is_write_unlock(space
);
1946 reply_name
= MACH_PORT_DEAD
;
1950 reply_name
= (mach_port_name_t
)reply
;
1951 kr
= ipc_entry_get(space
, &reply_name
, &entry
);
1952 if (kr
!= KERN_SUCCESS
) {
1955 if (notify_port
!= IP_NULL
)
1956 ipc_port_release_sonce(notify_port
);
1958 /* space is locked */
1959 kr
= ipc_entry_grow_table(space
,
1961 if (kr
!= KERN_SUCCESS
) {
1962 /* space is unlocked */
1964 if (kr
== KERN_RESOURCE_SHORTAGE
)
1965 return (MACH_RCV_HEADER_ERROR
|
1966 MACH_MSG_IPC_KERNEL
);
1968 return (MACH_RCV_HEADER_ERROR
|
1969 MACH_MSG_IPC_SPACE
);
1971 /* space is locked again; start over */
1975 assert(IE_BITS_TYPE(entry
->ie_bits
) ==
1976 MACH_PORT_TYPE_NONE
);
1977 assert(entry
->ie_object
== IO_NULL
);
1979 if (notify_port
== IP_NULL
) {
1980 /* not making a dead-name request */
1982 entry
->ie_object
= (ipc_object_t
) reply
;
1986 kr
= ipc_port_dnrequest(reply
, reply_name
,
1987 notify_port
, &request
);
1988 if (kr
!= KERN_SUCCESS
) {
1991 ipc_port_release_sonce(notify_port
);
1993 ipc_entry_dealloc(space
, reply_name
, entry
);
1994 is_write_unlock(space
);
1997 if (!ip_active(reply
)) {
1998 /* will fail next time around loop */
2001 is_write_lock(space
);
2005 kr
= ipc_port_dngrow(reply
, ITS_SIZE_NONE
);
2006 /* port is unlocked */
2007 if (kr
!= KERN_SUCCESS
)
2008 return (MACH_RCV_HEADER_ERROR
|
2009 MACH_MSG_IPC_KERNEL
);
2011 is_write_lock(space
);
2015 notify_port
= IP_NULL
; /* don't release right below */
2017 entry
->ie_object
= (ipc_object_t
) reply
;
2018 entry
->ie_request
= request
;
2022 /* space and reply port are locked and active */
2024 ip_reference(reply
); /* hold onto the reply port */
2026 kr
= ipc_right_copyout(space
, reply_name
, entry
,
2027 reply_type
, TRUE
, (ipc_object_t
) reply
);
2028 /* reply port is unlocked */
2029 assert(kr
== KERN_SUCCESS
);
2031 if (notify_port
!= IP_NULL
)
2032 ipc_port_release_sonce(notify_port
);
2035 is_write_unlock(space
);
2038 * No reply port! This is an easy case.
2039 * We only need to have the space locked
2040 * when checking notify and when locking
2041 * the destination (to ensure atomicity).
2044 is_read_lock(space
);
2045 if (!space
->is_active
) {
2046 is_read_unlock(space
);
2047 return MACH_RCV_HEADER_ERROR
|MACH_MSG_IPC_SPACE
;
2050 if (notify
!= MACH_PORT_NULL
) {
2053 /* must check notify even though it won't be used */
2055 if ((entry
= ipc_entry_lookup(space
, notify
)) == IE_NULL
) {
2056 is_read_unlock(space
);
2057 return MACH_RCV_INVALID_NOTIFY
;
2060 if ((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) == 0) {
2061 is_read_unlock(space
);
2062 return MACH_RCV_INVALID_NOTIFY
;
2067 is_read_unlock(space
);
2069 reply_name
= (mach_port_name_t
) reply
;
2073 * At this point, the space is unlocked and the destination
2074 * port is locked. (Lock taken while space was locked.)
2075 * reply_name is taken care of; we still need dest_name.
2076 * We still hold a ref for reply (if it is valid).
2078 * If the space holds receive rights for the destination,
2079 * we return its name for the right. Otherwise the task
2080 * managed to destroy or give away the receive right between
2081 * receiving the message and this copyout. If the destination
2082 * is dead, return MACH_PORT_DEAD, and if the receive right
2083 * exists somewhere else (another space, in transit)
2084 * return MACH_PORT_NULL.
2086 * Making this copyout operation atomic with the previous
2087 * copyout of the reply port is a bit tricky. If there was
2088 * no real reply port (it wasn't IP_VALID) then this isn't
2089 * an issue. If the reply port was dead at copyout time,
2090 * then we are OK, because if dest is dead we serialize
2091 * after the death of both ports and if dest is alive
2092 * we serialize after reply died but before dest's (later) death.
2093 * So assume reply was alive when we copied it out. If dest
2094 * is alive, then we are OK because we serialize before
2095 * the ports' deaths. So assume dest is dead when we look at it.
2096 * If reply dies/died after dest, then we are OK because
2097 * we serialize after dest died but before reply dies.
2098 * So the hard case is when reply is alive at copyout,
2099 * dest is dead at copyout, and reply died before dest died.
2100 * In this case pretend that dest is still alive, so
2101 * we serialize while both ports are alive.
2103 * Because the space lock is held across the copyout of reply
2104 * and locking dest, the receive right for dest can't move
2105 * in or out of the space while the copyouts happen, so
2106 * that isn't an atomicity problem. In the last hard case
2107 * above, this implies that when dest is dead that the
2108 * space couldn't have had receive rights for dest at
2109 * the time reply was copied-out, so when we pretend
2110 * that dest is still alive, we can return MACH_PORT_NULL.
2112 * If dest == reply, then we have to make it look like
2113 * either both copyouts happened before the port died,
2114 * or both happened after the port died. This special
2115 * case works naturally if the timestamp comparison
2116 * is done correctly.
2121 if (ip_active(dest
)) {
2122 ipc_object_copyout_dest(space
, (ipc_object_t
) dest
,
2123 dest_type
, &dest_name
);
2124 /* dest is unlocked */
2126 ipc_port_timestamp_t timestamp
;
2128 timestamp
= dest
->ip_timestamp
;
2130 ip_check_unlock(dest
);
2132 if (IP_VALID(reply
)) {
2134 if (ip_active(reply
) ||
2135 IP_TIMESTAMP_ORDER(timestamp
,
2136 reply
->ip_timestamp
))
2137 dest_name
= MACH_PORT_DEAD
;
2139 dest_name
= MACH_PORT_NULL
;
2142 dest_name
= MACH_PORT_DEAD
;
2145 if (IP_VALID(reply
))
2146 ipc_port_release(reply
);
2148 msg
->msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
2149 MACH_MSGH_BITS(reply_type
, dest_type
));
2150 msg
->msgh_local_port
= (ipc_port_t
)dest_name
;
2151 msg
->msgh_remote_port
= (ipc_port_t
)reply_name
;
2154 return MACH_MSG_SUCCESS
;
2158 * Routine: ipc_kmsg_copyout_object
2160 * Copy-out a port right. Always returns a name,
2161 * even for unsuccessful return codes. Always
2162 * consumes the supplied object.
2166 * MACH_MSG_SUCCESS The space acquired the right
2167 * (name is valid) or the object is dead (MACH_PORT_DEAD).
2168 * MACH_MSG_IPC_SPACE No room in space for the right,
2169 * or the space is dead. (Name is MACH_PORT_NULL.)
2170 * MACH_MSG_IPC_KERNEL Kernel resource shortage.
2171 * (Name is MACH_PORT_NULL.)
2175 ipc_kmsg_copyout_object(
2177 ipc_object_t object
,
2178 mach_msg_type_name_t msgt_name
,
2179 mach_port_name_t
*namep
)
2183 if (!IO_VALID(object
)) {
2184 *namep
= (mach_port_name_t
) object
;
2185 return MACH_MSG_SUCCESS
;
2188 kr
= ipc_object_copyout(space
, object
, msgt_name
, TRUE
, namep
);
2189 if (kr
!= KERN_SUCCESS
) {
2190 ipc_object_destroy(object
, msgt_name
);
2192 if (kr
== KERN_INVALID_CAPABILITY
)
2193 *namep
= MACH_PORT_DEAD
;
2195 *namep
= MACH_PORT_NULL
;
2197 if (kr
== KERN_RESOURCE_SHORTAGE
)
2198 return MACH_MSG_IPC_KERNEL
;
2200 return MACH_MSG_IPC_SPACE
;
2204 return MACH_MSG_SUCCESS
;
2208 * Routine: ipc_kmsg_copyout_body
2210 * "Copy-out" port rights and out-of-line memory
2211 * in the body of a message.
2213 * The error codes are a combination of special bits.
2214 * The copyout proceeds despite errors.
2218 * MACH_MSG_SUCCESS Successful copyout.
2219 * MACH_MSG_IPC_SPACE No room for port right in name space.
2220 * MACH_MSG_VM_SPACE No room for memory in address space.
2221 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
2222 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
2223 * MACH_MSG_INVALID_RT_DESCRIPTOR Descriptor incompatible with RT
2227 ipc_kmsg_copyout_body(
2231 mach_msg_body_t
*slist
)
2233 mach_msg_body_t
*body
;
2234 mach_msg_descriptor_t
*saddr
, *eaddr
;
2235 mach_msg_return_t mr
= MACH_MSG_SUCCESS
;
2238 mach_msg_descriptor_t
*sstart
, *send
;
2240 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
2241 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
2242 eaddr
= saddr
+ body
->msgh_descriptor_count
;
2245 * Do scatter list setup
2247 if (slist
!= MACH_MSG_BODY_NULL
) {
2248 sstart
= (mach_msg_descriptor_t
*) (slist
+ 1);
2249 send
= sstart
+ slist
->msgh_descriptor_count
;
2252 sstart
= MACH_MSG_DESCRIPTOR_NULL
;
2255 for ( ; saddr
< eaddr
; saddr
++ ) {
2257 switch (saddr
->type
.type
) {
2259 case MACH_MSG_PORT_DESCRIPTOR
: {
2260 mach_msg_port_descriptor_t
*dsc
;
2263 * Copyout port right carried in the message
2266 mr
|= ipc_kmsg_copyout_object(space
,
2267 (ipc_object_t
) dsc
->name
,
2269 (mach_port_name_t
*) &dsc
->name
);
2273 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
2274 case MACH_MSG_OOL_DESCRIPTOR
: {
2275 vm_offset_t rcv_addr
;
2276 vm_offset_t snd_addr
;
2277 mach_msg_ool_descriptor_t
*dsc
;
2278 mach_msg_copy_options_t copy_option
;
2280 SKIP_PORT_DESCRIPTORS(sstart
, send
);
2282 dsc
= &saddr
->out_of_line
;
2284 assert(dsc
->copy
!= MACH_MSG_KALLOC_COPY_T
);
2286 copy_option
= dsc
->copy
;
2288 if ((snd_addr
= (vm_offset_t
) dsc
->address
) != 0) {
2289 if (sstart
!= MACH_MSG_DESCRIPTOR_NULL
&&
2290 sstart
->out_of_line
.copy
== MACH_MSG_OVERWRITE
) {
2293 * There is an overwrite descriptor specified in the
2294 * scatter list for this ool data. The descriptor
2295 * has already been verified
2297 rcv_addr
= (vm_offset_t
) sstart
->out_of_line
.address
;
2298 dsc
->copy
= MACH_MSG_OVERWRITE
;
2300 dsc
->copy
= MACH_MSG_ALLOCATE
;
2304 * Whether the data was virtually or physically
2305 * copied we have a vm_map_copy_t for it.
2306 * If there's an overwrite region specified
2307 * overwrite it, otherwise do a virtual copy out.
2309 if (dsc
->copy
== MACH_MSG_OVERWRITE
) {
2310 kr
= vm_map_copy_overwrite(map
, rcv_addr
,
2311 (vm_map_copy_t
) dsc
->address
, TRUE
);
2313 kr
= vm_map_copyout(map
, &rcv_addr
,
2314 (vm_map_copy_t
) dsc
->address
);
2316 if (kr
!= KERN_SUCCESS
) {
2317 if (kr
== KERN_RESOURCE_SHORTAGE
)
2318 mr
|= MACH_MSG_VM_KERNEL
;
2320 mr
|= MACH_MSG_VM_SPACE
;
2321 vm_map_copy_discard((vm_map_copy_t
) dsc
->address
);
2323 INCREMENT_SCATTER(sstart
);
2326 dsc
->address
= (void *) rcv_addr
;
2328 INCREMENT_SCATTER(sstart
);
2331 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
2333 mach_port_name_t
*objects
;
2334 mach_msg_type_number_t j
;
2336 mach_msg_ool_ports_descriptor_t
*dsc
;
2338 SKIP_PORT_DESCRIPTORS(sstart
, send
);
2340 dsc
= &saddr
->ool_ports
;
2342 length
= dsc
->count
* sizeof(mach_port_name_t
);
2345 if (sstart
!= MACH_MSG_DESCRIPTOR_NULL
&&
2346 sstart
->ool_ports
.copy
== MACH_MSG_OVERWRITE
) {
2349 * There is an overwrite descriptor specified in the
2350 * scatter list for this ool data. The descriptor
2351 * has already been verified
2353 addr
= (vm_offset_t
) sstart
->out_of_line
.address
;
2354 dsc
->copy
= MACH_MSG_OVERWRITE
;
2359 * Dynamically allocate the region
2361 int anywhere
= VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|
2364 dsc
->copy
= MACH_MSG_ALLOCATE
;
2365 if ((kr
= vm_allocate(map
, &addr
, length
,
2366 anywhere
)) != KERN_SUCCESS
) {
2367 ipc_kmsg_clean_body(kmsg
,
2368 body
->msgh_descriptor_count
);
2371 if (kr
== KERN_RESOURCE_SHORTAGE
){
2372 mr
|= MACH_MSG_VM_KERNEL
;
2374 mr
|= MACH_MSG_VM_SPACE
;
2376 INCREMENT_SCATTER(sstart
);
2381 INCREMENT_SCATTER(sstart
);
2386 objects
= (mach_port_name_t
*) dsc
->address
;
2388 /* copyout port rights carried in the message */
2390 for ( j
= 0; j
< dsc
->count
; j
++) {
2391 ipc_object_t object
=
2392 (ipc_object_t
) objects
[j
];
2394 mr
|= ipc_kmsg_copyout_object(space
, object
,
2395 dsc
->disposition
, &objects
[j
]);
2398 /* copyout to memory allocated above */
2400 data
= (vm_offset_t
) dsc
->address
;
2401 (void) copyoutmap(map
, data
, addr
, length
);
2402 kfree(data
, length
);
2404 dsc
->address
= (void *) addr
;
2405 INCREMENT_SCATTER(sstart
);
2409 panic("untyped IPC copyout body: invalid message descriptor");
2417 * Routine: ipc_kmsg_copyout
2419 * "Copy-out" port rights and out-of-line memory
2424 * MACH_MSG_SUCCESS Copied out all rights and memory.
2425 * MACH_RCV_INVALID_NOTIFY Bad notify port.
2426 * Rights and memory in the message are intact.
2427 * MACH_RCV_HEADER_ERROR + special bits
2428 * Rights and memory in the message are intact.
2429 * MACH_RCV_BODY_ERROR + special bits
2430 * The message header was successfully copied out.
2431 * As much of the body was handled as possible.
2439 mach_port_name_t notify
,
2440 mach_msg_body_t
*slist
)
2442 mach_msg_return_t mr
;
2444 mr
= ipc_kmsg_copyout_header(&kmsg
->ikm_header
, space
, notify
);
2445 if (mr
!= MACH_MSG_SUCCESS
)
2448 if (kmsg
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
2449 mr
= ipc_kmsg_copyout_body(kmsg
, space
, map
, slist
);
2451 if (mr
!= MACH_MSG_SUCCESS
)
2452 mr
|= MACH_RCV_BODY_ERROR
;
2459 * Routine: ipc_kmsg_copyout_pseudo
2461 * Does a pseudo-copyout of the message.
2462 * This is like a regular copyout, except
2463 * that the ports in the header are handled
2464 * as if they are in the body. They aren't reversed.
2466 * The error codes are a combination of special bits.
2467 * The copyout proceeds despite errors.
2471 * MACH_MSG_SUCCESS Successful copyout.
2472 * MACH_MSG_IPC_SPACE No room for port right in name space.
2473 * MACH_MSG_VM_SPACE No room for memory in address space.
2474 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
2475 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
2479 ipc_kmsg_copyout_pseudo(
2483 mach_msg_body_t
*slist
)
2485 mach_msg_bits_t mbits
= kmsg
->ikm_header
.msgh_bits
;
2486 ipc_object_t dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
2487 ipc_object_t reply
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
2488 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
2489 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
2490 mach_port_name_t dest_name
, reply_name
;
2491 mach_msg_return_t mr
;
2493 assert(IO_VALID(dest
));
2495 mr
= (ipc_kmsg_copyout_object(space
, dest
, dest_type
, &dest_name
) |
2496 ipc_kmsg_copyout_object(space
, reply
, reply_type
, &reply_name
));
2498 kmsg
->ikm_header
.msgh_bits
= mbits
&~ MACH_MSGH_BITS_CIRCULAR
;
2499 kmsg
->ikm_header
.msgh_remote_port
= (ipc_port_t
)dest_name
;
2500 kmsg
->ikm_header
.msgh_local_port
= (ipc_port_t
)reply_name
;
2502 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2503 mr
|= ipc_kmsg_copyout_body(kmsg
, space
, map
, slist
);
2510 * Routine: ipc_kmsg_copyout_dest
2512 * Copies out the destination port in the message.
2513 * Destroys all other rights and memory in the message.
2519 ipc_kmsg_copyout_dest(
2523 mach_msg_bits_t mbits
;
2526 mach_msg_type_name_t dest_type
;
2527 mach_msg_type_name_t reply_type
;
2528 mach_port_name_t dest_name
, reply_name
;
2530 mbits
= kmsg
->ikm_header
.msgh_bits
;
2531 dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
2532 reply
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
2533 dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
2534 reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
2536 assert(IO_VALID(dest
));
2539 if (io_active(dest
)) {
2540 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
2541 /* dest is unlocked */
2544 io_check_unlock(dest
);
2545 dest_name
= MACH_PORT_DEAD
;
2548 if (IO_VALID(reply
)) {
2549 ipc_object_destroy(reply
, reply_type
);
2550 reply_name
= MACH_PORT_NULL
;
2552 reply_name
= (mach_port_name_t
) reply
;
2554 kmsg
->ikm_header
.msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
2555 MACH_MSGH_BITS(reply_type
, dest_type
));
2556 kmsg
->ikm_header
.msgh_local_port
= (ipc_port_t
)dest_name
;
2557 kmsg
->ikm_header
.msgh_remote_port
= (ipc_port_t
)reply_name
;
2559 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2560 mach_msg_body_t
*body
;
2562 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
2563 ipc_kmsg_clean_body(kmsg
, body
->msgh_descriptor_count
);
2567 * Routine: ipc_kmsg_copyin_scatter
2569 * allocate and copyin a scatter list
2571 * The gather (kmsg) is valid since it has been copied in.
2572 * Gather list descriptors are sequentially paired with scatter
2573 * list descriptors, with port descriptors in either list ignored.
2574 * Descriptors are consistent if the type fileds match and size
2575 * of the scatter descriptor is less than or equal to the
2576 * size of the gather descriptor. A MACH_MSG_ALLOCATE copy
2577 * strategy in a scatter descriptor matches any size in the
2578 * corresponding gather descriptor assuming they are the same type.
2579 * Either list may be larger than the other. During the
2580 * subsequent copy out, excess scatter descriptors are ignored
2581 * and excess gather descriptors default to dynamic allocation.
2583 * In the case of a size error, the scatter list is released.
2587 * the allocated message body containing the scatter list.
2591 ipc_kmsg_copyin_scatter(
2592 mach_msg_header_t
*msg
,
2593 mach_msg_size_t slist_size
,
2596 mach_msg_body_t
*slist
;
2597 mach_msg_body_t
*body
;
2598 mach_msg_descriptor_t
*gstart
, *gend
;
2599 mach_msg_descriptor_t
*sstart
, *send
;
2602 if (slist_size
< sizeof(mach_msg_base_t
))
2603 return MACH_MSG_BODY_NULL
;
2605 slist_size
-= sizeof(mach_msg_header_t
);
2606 slist
= (mach_msg_body_t
*)kalloc(slist_size
);
2607 if (slist
== MACH_MSG_BODY_NULL
)
2610 if (copyin((char *) (msg
+ 1), (char *)slist
, slist_size
)) {
2611 kfree((vm_offset_t
)slist
, slist_size
);
2612 return MACH_MSG_BODY_NULL
;
2615 if ((slist
->msgh_descriptor_count
* sizeof(mach_msg_descriptor_t
)
2616 + sizeof(mach_msg_size_t
)) > slist_size
) {
2617 kfree((vm_offset_t
)slist
, slist_size
);
2618 return MACH_MSG_BODY_NULL
;
2621 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
2622 gstart
= (mach_msg_descriptor_t
*) (body
+ 1);
2623 gend
= gstart
+ body
->msgh_descriptor_count
;
2625 sstart
= (mach_msg_descriptor_t
*) (slist
+ 1);
2626 send
= sstart
+ slist
->msgh_descriptor_count
;
2628 while (gstart
< gend
) {
2629 mach_msg_descriptor_type_t g_type
;
2632 * Skip port descriptors in gather list.
2634 g_type
= gstart
->type
.type
;
2636 if (g_type
!= MACH_MSG_PORT_DESCRIPTOR
) {
2639 * A scatter list with a 0 descriptor count is treated as an
2640 * automatic size mismatch.
2642 if (slist
->msgh_descriptor_count
== 0) {
2643 kfree((vm_offset_t
)slist
, slist_size
);
2644 return MACH_MSG_BODY_NULL
;
2648 * Skip port descriptors in scatter list.
2650 while (sstart
< send
) {
2651 if (sstart
->type
.type
!= MACH_MSG_PORT_DESCRIPTOR
)
2657 * No more scatter descriptors, we're done
2659 if (sstart
>= send
) {
2664 * Check type, copy and size fields
2666 if (g_type
== MACH_MSG_OOL_DESCRIPTOR
||
2667 g_type
== MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
2668 if (sstart
->type
.type
!= MACH_MSG_OOL_DESCRIPTOR
&&
2669 sstart
->type
.type
!= MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
2670 kfree((vm_offset_t
)slist
, slist_size
);
2671 return MACH_MSG_BODY_NULL
;
2673 if (sstart
->out_of_line
.copy
== MACH_MSG_OVERWRITE
&&
2674 gstart
->out_of_line
.size
> sstart
->out_of_line
.size
) {
2675 kfree((vm_offset_t
)slist
, slist_size
);
2676 return MACH_MSG_BODY_NULL
;
2680 if (sstart
->type
.type
!= MACH_MSG_OOL_PORTS_DESCRIPTOR
) {
2681 kfree((vm_offset_t
)slist
, slist_size
);
2682 return MACH_MSG_BODY_NULL
;
2684 if (sstart
->ool_ports
.copy
== MACH_MSG_OVERWRITE
&&
2685 gstart
->ool_ports
.count
> sstart
->ool_ports
.count
) {
2686 kfree((vm_offset_t
)slist
, slist_size
);
2687 return MACH_MSG_BODY_NULL
;
2699 * Routine: ipc_kmsg_free_scatter
2701 * Deallocate a scatter list. Since we actually allocated
2702 * a body without a header, and since the header was originally
2703 * accounted for in slist_size, we have to ajust it down
2704 * before freeing the scatter list.
2707 ipc_kmsg_free_scatter(
2708 mach_msg_body_t
*slist
,
2709 mach_msg_size_t slist_size
)
2711 slist_size
-= sizeof(mach_msg_header_t
);
2712 kfree((vm_offset_t
)slist
, slist_size
);
2717 * Routine: ipc_kmsg_copyout_to_kernel
2719 * Copies out the destination and reply ports in the message.
2720 * Leaves all other rights and memory in the message alone.
2724 * Derived from ipc_kmsg_copyout_dest.
2725 * Use by mach_msg_rpc_from_kernel (which used to use copyout_dest).
2726 * We really do want to save rights and memory.
2730 ipc_kmsg_copyout_to_kernel(
2736 mach_msg_type_name_t dest_type
;
2737 mach_msg_type_name_t reply_type
;
2738 mach_port_name_t dest_name
, reply_name
;
2740 dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
2741 reply
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
2742 dest_type
= MACH_MSGH_BITS_REMOTE(kmsg
->ikm_header
.msgh_bits
);
2743 reply_type
= MACH_MSGH_BITS_LOCAL(kmsg
->ikm_header
.msgh_bits
);
2745 assert(IO_VALID(dest
));
2748 if (io_active(dest
)) {
2749 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
2750 /* dest is unlocked */
2753 io_check_unlock(dest
);
2754 dest_name
= MACH_PORT_DEAD
;
2757 reply_name
= (mach_port_name_t
) reply
;
2759 kmsg
->ikm_header
.msgh_bits
=
2760 (MACH_MSGH_BITS_OTHER(kmsg
->ikm_header
.msgh_bits
) |
2761 MACH_MSGH_BITS(reply_type
, dest_type
));
2762 kmsg
->ikm_header
.msgh_local_port
= (ipc_port_t
)dest_name
;
2763 kmsg
->ikm_header
.msgh_remote_port
= (ipc_port_t
)reply_name
;
2766 #include <mach_kdb.h>
2769 #include <ddb/db_output.h>
2770 #include <ipc/ipc_print.h>
2772 * Forward declarations
2774 void ipc_msg_print_untyped(
2775 mach_msg_body_t
*body
);
2777 char * ipc_type_name(
2779 boolean_t received
);
2781 void ipc_print_type_name(
2786 mach_msg_bits_t bit
);
2789 mm_copy_options_string(
2790 mach_msg_copy_options_t option
);
2792 void db_print_msg_uid(mach_msg_header_t
*);
2800 switch (type_name
) {
2801 case MACH_MSG_TYPE_PORT_NAME
:
2804 case MACH_MSG_TYPE_MOVE_RECEIVE
:
2806 return "port_receive";
2808 return "move_receive";
2811 case MACH_MSG_TYPE_MOVE_SEND
:
2818 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
2820 return "port_send_once";
2822 return "move_send_once";
2825 case MACH_MSG_TYPE_COPY_SEND
:
2828 case MACH_MSG_TYPE_MAKE_SEND
:
2831 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
2832 return "make_send_once";
2840 ipc_print_type_name(
2843 char *name
= ipc_type_name(type_name
, TRUE
);
2847 printf("type%d", type_name
);
2852 * ipc_kmsg_print [ debug ]
2858 iprintf("kmsg=0x%x\n", kmsg
);
2859 iprintf("ikm_next=0x%x, prev=0x%x, size=%d",
2864 ipc_msg_print(&kmsg
->ikm_header
);
2869 mach_msg_bits_t bit
)
2872 case MACH_MSGH_BITS_COMPLEX
: return "complex";
2873 case MACH_MSGH_BITS_CIRCULAR
: return "circular";
2874 default: return (char *) 0;
2879 * ipc_msg_print [ debug ]
2883 mach_msg_header_t
*msgh
)
2885 mach_msg_bits_t mbits
;
2886 unsigned int bit
, i
;
2890 mbits
= msgh
->msgh_bits
;
2891 iprintf("msgh_bits=0x%x: l=0x%x,r=0x%x\n",
2893 MACH_MSGH_BITS_LOCAL(msgh
->msgh_bits
),
2894 MACH_MSGH_BITS_REMOTE(msgh
->msgh_bits
));
2896 mbits
= MACH_MSGH_BITS_OTHER(mbits
) & MACH_MSGH_BITS_USED
;
2899 iprintf("decoded bits: ");
2901 for (i
= 0, bit
= 1; i
< sizeof(mbits
) * 8; ++i
, bit
<<= 1) {
2902 if ((mbits
& bit
) == 0)
2904 bit_name
= msgh_bit_decode((mach_msg_bits_t
)bit
);
2906 printf("%s%s", needs_comma
? "," : "", bit_name
);
2908 printf("%sunknown(0x%x),", needs_comma
? "," : "", bit
);
2911 if (msgh
->msgh_bits
& ~MACH_MSGH_BITS_USED
) {
2912 printf("%sunused=0x%x,", needs_comma
? "," : "",
2913 msgh
->msgh_bits
& ~MACH_MSGH_BITS_USED
);
2919 if (msgh
->msgh_remote_port
) {
2920 iprintf("remote=0x%x(", msgh
->msgh_remote_port
);
2921 ipc_print_type_name(MACH_MSGH_BITS_REMOTE(msgh
->msgh_bits
));
2924 iprintf("remote=null");
2927 if (msgh
->msgh_local_port
) {
2928 printf("%slocal=0x%x(", needs_comma
? "," : "",
2929 msgh
->msgh_local_port
);
2930 ipc_print_type_name(MACH_MSGH_BITS_LOCAL(msgh
->msgh_bits
));
2933 printf("local=null\n");
2936 iprintf("msgh_id=%d, size=%d\n",
2940 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2941 ipc_msg_print_untyped((mach_msg_body_t
*) (msgh
+ 1));
2947 mm_copy_options_string(
2948 mach_msg_copy_options_t option
)
2953 case MACH_MSG_PHYSICAL_COPY
:
2956 case MACH_MSG_VIRTUAL_COPY
:
2959 case MACH_MSG_OVERWRITE
:
2962 case MACH_MSG_ALLOCATE
:
2965 case MACH_MSG_KALLOC_COPY_T
:
2966 name
= "KALLOC_COPY_T";
2976 ipc_msg_print_untyped(
2977 mach_msg_body_t
*body
)
2979 mach_msg_descriptor_t
*saddr
, *send
;
2980 mach_msg_descriptor_type_t type
;
2982 iprintf("%d descriptors %d: \n", body
->msgh_descriptor_count
);
2984 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
2985 send
= saddr
+ body
->msgh_descriptor_count
;
2987 for ( ; saddr
< send
; saddr
++ ) {
2989 type
= saddr
->type
.type
;
2993 case MACH_MSG_PORT_DESCRIPTOR
: {
2994 mach_msg_port_descriptor_t
*dsc
;
2997 iprintf("-- PORT name = 0x%x disp = ", dsc
->name
);
2998 ipc_print_type_name(dsc
->disposition
);
3002 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
3003 case MACH_MSG_OOL_DESCRIPTOR
: {
3004 mach_msg_ool_descriptor_t
*dsc
;
3006 dsc
= &saddr
->out_of_line
;
3007 iprintf("-- OOL%s addr = 0x%x size = 0x%x copy = %s %s\n",
3008 type
== MACH_MSG_OOL_DESCRIPTOR
? "" : " VOLATILE",
3009 dsc
->address
, dsc
->size
,
3010 mm_copy_options_string(dsc
->copy
),
3011 dsc
->deallocate
? "DEALLOC" : "");
3014 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
3015 mach_msg_ool_ports_descriptor_t
*dsc
;
3017 dsc
= &saddr
->ool_ports
;
3019 iprintf("-- OOL_PORTS addr = 0x%x count = 0x%x ",
3020 dsc
->address
, dsc
->count
);
3022 ipc_print_type_name(dsc
->disposition
);
3023 printf(" copy = %s %s\n",
3024 mm_copy_options_string(dsc
->copy
),
3025 dsc
->deallocate
? "DEALLOC" : "");
3030 iprintf("-- UNKNOWN DESCRIPTOR 0x%x\n", type
);
3036 #endif /* MACH_KDB */