2 * Copyright (c) 2000-2003 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>
92 #include <ppc/Firmware.h>
93 #include <ppc/low_trace.h>
97 extern vm_map_t ipc_kernel_copy_map
;
98 extern vm_size_t ipc_kmsg_max_vm_space
;
99 extern vm_size_t msg_ool_size_small
;
101 #define MSG_OOL_SIZE_SMALL msg_ool_size_small
105 * Forward declarations
111 void ipc_kmsg_clean_body(
113 mach_msg_type_number_t number
);
115 void ipc_kmsg_clean_partial(
117 mach_msg_type_number_t number
,
121 mach_msg_return_t
ipc_kmsg_copyout_body(
125 mach_msg_body_t
*slist
);
127 mach_msg_return_t
ipc_kmsg_copyin_body(
132 void ikm_cache_init(void);
134 * We keep a per-processor cache of kernel message buffers.
135 * The cache saves the overhead/locking of using kalloc/kfree.
136 * The per-processor cache seems to miss less than a per-thread cache,
137 * and it also uses less memory. Access to the cache doesn't
140 #define IKM_STASH 16 /* # of cache entries per cpu */
141 ipc_kmsg_t ipc_kmsg_cache
[ NCPUS
][ IKM_STASH
];
142 unsigned int ipc_kmsg_cache_avail
[NCPUS
];
145 * Routine: ipc_kmsg_init
147 * Initialize the kmsg system. For each CPU, we need to
148 * pre-stuff the kmsg cache.
155 for (cpu
= 0; cpu
< NCPUS
; ++cpu
) {
156 for (i
= 0; i
< IKM_STASH
; ++i
) {
160 kalloc(ikm_plus_overhead(IKM_SAVED_MSG_SIZE
));
161 if (kmsg
== IKM_NULL
)
162 panic("ipc_kmsg_init");
163 ikm_init(kmsg
, IKM_SAVED_MSG_SIZE
);
164 ipc_kmsg_cache
[cpu
][i
] = kmsg
;
166 ipc_kmsg_cache_avail
[cpu
] = IKM_STASH
;
171 * Routine: ipc_kmsg_alloc
173 * Allocate a kernel message structure. If we can get one from
174 * the cache, that is best. Otherwise, allocate a new one.
180 mach_msg_size_t msg_and_trailer_size
)
184 if ((msg_and_trailer_size
<= IKM_SAVED_MSG_SIZE
)) {
187 disable_preemption();
189 if ((i
= ipc_kmsg_cache_avail
[cpu
]) > 0) {
190 assert(i
<= IKM_STASH
);
191 kmsg
= ipc_kmsg_cache
[cpu
][--i
];
192 ipc_kmsg_cache_avail
[cpu
] = i
;
193 ikm_check_init(kmsg
, IKM_SAVED_MSG_SIZE
);
200 /* round up for ikm_cache */
201 if (msg_and_trailer_size
< IKM_SAVED_MSG_SIZE
)
202 msg_and_trailer_size
= IKM_SAVED_MSG_SIZE
;
204 kmsg
= (ipc_kmsg_t
)kalloc(ikm_plus_overhead(msg_and_trailer_size
));
205 if (kmsg
!= IKM_NULL
) {
206 ikm_init(kmsg
, msg_and_trailer_size
);
212 * Routine: ipc_kmsg_free
214 * Free a kernel message buffer. If the kms is preallocated
215 * to a port, just "put it back (marked unused)." We have to
216 * do this with the port locked. The port may have its hold
217 * on our message released. In that case, we have to just
218 * revert the message to a traditional one and free it normally.
227 mach_msg_size_t size
= kmsg
->ikm_size
;
231 * Check to see if the message is bound to the port. If so,
232 * mark it not in use. If the port isn't already dead, then
233 * leave the message associated with it. Otherwise, free it
234 * (not to the cache).
236 port
= ikm_prealloc_inuse_port(kmsg
);
237 if (port
!= IP_NULL
) {
239 ikm_prealloc_clear_inuse(kmsg
, port
);
240 if (ip_active(port
) && (port
->ip_premsg
== kmsg
)) {
241 assert(IP_PREALLOC(port
));
245 ip_check_unlock(port
); /* May be last reference */
250 * Peek and see if it has to go back in the cache.
252 if (kmsg
->ikm_size
== IKM_SAVED_MSG_SIZE
&&
253 ipc_kmsg_cache_avail
[cpu_number()] < IKM_STASH
) {
256 disable_preemption();
259 i
= ipc_kmsg_cache_avail
[cpu
];
262 ipc_kmsg_cache
[cpu
][i
] = kmsg
;
263 ipc_kmsg_cache_avail
[cpu
] = i
+ 1;
271 kfree((vm_offset_t
) kmsg
, ikm_plus_overhead(size
));
276 * Routine: ipc_kmsg_enqueue
283 ipc_kmsg_queue_t queue
,
286 ipc_kmsg_enqueue_macro(queue
, kmsg
);
290 * Routine: ipc_kmsg_dequeue
292 * Dequeue and return a kmsg.
297 ipc_kmsg_queue_t queue
)
301 first
= ipc_kmsg_queue_first(queue
);
303 if (first
!= IKM_NULL
)
304 ipc_kmsg_rmqueue_first_macro(queue
, first
);
310 * Routine: ipc_kmsg_rmqueue
312 * Pull a kmsg out of a queue.
317 ipc_kmsg_queue_t queue
,
320 ipc_kmsg_t next
, prev
;
322 assert(queue
->ikmq_base
!= IKM_NULL
);
324 next
= kmsg
->ikm_next
;
325 prev
= kmsg
->ikm_prev
;
328 assert(prev
== kmsg
);
329 assert(queue
->ikmq_base
== kmsg
);
331 queue
->ikmq_base
= IKM_NULL
;
333 if (queue
->ikmq_base
== kmsg
)
334 queue
->ikmq_base
= next
;
336 next
->ikm_prev
= prev
;
337 prev
->ikm_next
= next
;
339 /* XXX Temporary debug logic */
340 assert(kmsg
->ikm_next
= IKM_BOGUS
);
341 assert(kmsg
->ikm_prev
= IKM_BOGUS
);
345 * Routine: ipc_kmsg_queue_next
347 * Return the kmsg following the given kmsg.
348 * (Or IKM_NULL if it is the last one in the queue.)
353 ipc_kmsg_queue_t queue
,
358 assert(queue
->ikmq_base
!= IKM_NULL
);
360 next
= kmsg
->ikm_next
;
361 if (queue
->ikmq_base
== next
)
368 * Routine: ipc_kmsg_destroy
370 * Destroys a kernel message. Releases all rights,
371 * references, and memory held by the message.
381 ipc_kmsg_queue_t queue
;
385 * ipc_kmsg_clean can cause more messages to be destroyed.
386 * Curtail recursion by queueing messages. If a message
387 * is already queued, then this is a recursive call.
390 queue
= &(current_thread()->ith_messages
);
391 empty
= ipc_kmsg_queue_empty(queue
);
392 ipc_kmsg_enqueue(queue
, kmsg
);
395 /* must leave kmsg in queue while cleaning it */
397 while ((kmsg
= ipc_kmsg_queue_first(queue
)) != IKM_NULL
) {
398 ipc_kmsg_clean(kmsg
);
399 ipc_kmsg_rmqueue(queue
, kmsg
);
406 * Routine: ipc_kmsg_destroy_dest
408 * Destroys a kernel message. Releases all rights,
409 * references, and memory held by the message (including
410 * the destination port reference.
416 ipc_kmsg_destroy_dest(
421 port
= kmsg
->ikm_header
.msgh_remote_port
;
423 ipc_port_release(port
);
424 kmsg
->ikm_header
.msgh_remote_port
= MACH_PORT_NULL
;
425 ipc_kmsg_destroy(kmsg
);
429 * Routine: ipc_kmsg_clean_body
431 * Cleans the body of a kernel message.
432 * Releases all rights, references, and memory.
441 mach_msg_type_number_t number
)
443 mach_msg_descriptor_t
*saddr
, *eaddr
;
448 saddr
= (mach_msg_descriptor_t
*)
449 ((mach_msg_base_t
*) &kmsg
->ikm_header
+ 1);
450 eaddr
= saddr
+ number
;
452 for ( ; saddr
< eaddr
; saddr
++ ) {
454 switch (saddr
->type
.type
) {
456 case MACH_MSG_PORT_DESCRIPTOR
: {
457 mach_msg_port_descriptor_t
*dsc
;
462 * Destroy port rights carried in the message
464 if (!IO_VALID((ipc_object_t
) dsc
->name
))
466 ipc_object_destroy((ipc_object_t
) dsc
->name
, dsc
->disposition
);
469 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
470 case MACH_MSG_OOL_DESCRIPTOR
: {
471 mach_msg_ool_descriptor_t
*dsc
;
473 dsc
= &saddr
->out_of_line
;
476 * Destroy memory carried in the message
478 if (dsc
->size
== 0) {
479 assert(dsc
->address
== (void *) 0);
481 vm_map_copy_discard((vm_map_copy_t
) dsc
->address
);
485 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
486 ipc_object_t
*objects
;
487 mach_msg_type_number_t j
;
488 mach_msg_ool_ports_descriptor_t
*dsc
;
490 dsc
= &saddr
->ool_ports
;
491 objects
= (ipc_object_t
*) dsc
->address
;
493 if (dsc
->count
== 0) {
497 assert(objects
!= (ipc_object_t
*) 0);
499 /* destroy port rights carried in the message */
501 for (j
= 0; j
< dsc
->count
; j
++) {
502 ipc_object_t object
= objects
[j
];
504 if (!IO_VALID(object
))
507 ipc_object_destroy(object
, dsc
->disposition
);
510 /* destroy memory carried in the message */
512 assert(dsc
->count
!= 0);
514 kfree((vm_offset_t
) dsc
->address
,
515 (vm_size_t
) dsc
->count
* sizeof(mach_port_name_t
));
519 printf("cleanup: don't understand this type of descriptor\n");
526 * Routine: ipc_kmsg_clean_partial
528 * Cleans a partially-acquired kernel message.
529 * number is the index of the type descriptor
530 * in the body of the message that contained the error.
531 * If dolast, the memory and port rights in this last
532 * type spec are also cleaned. In that case, number
533 * specifies the number of port rights to clean.
539 ipc_kmsg_clean_partial(
541 mach_msg_type_number_t number
,
546 mach_msg_bits_t mbits
= kmsg
->ikm_header
.msgh_bits
;
548 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
549 assert(IO_VALID(object
));
550 ipc_object_destroy(object
, MACH_MSGH_BITS_REMOTE(mbits
));
552 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
553 if (IO_VALID(object
))
554 ipc_object_destroy(object
, MACH_MSGH_BITS_LOCAL(mbits
));
557 (void) vm_deallocate(ipc_kernel_copy_map
, paddr
, length
);
560 ipc_kmsg_clean_body(kmsg
, number
);
564 * Routine: ipc_kmsg_clean
566 * Cleans a kernel message. Releases all rights,
567 * references, and memory held by the message.
577 mach_msg_bits_t mbits
;
579 mbits
= kmsg
->ikm_header
.msgh_bits
;
580 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
581 if (IO_VALID(object
))
582 ipc_object_destroy(object
, MACH_MSGH_BITS_REMOTE(mbits
));
584 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
585 if (IO_VALID(object
))
586 ipc_object_destroy(object
, MACH_MSGH_BITS_LOCAL(mbits
));
588 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
589 mach_msg_body_t
*body
;
591 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
592 ipc_kmsg_clean_body(kmsg
, body
->msgh_descriptor_count
);
597 * Routine: ipc_kmsg_set_prealloc
599 * Assign a kmsg as a preallocated message buffer to a port.
605 ipc_kmsg_set_prealloc(
609 assert(kmsg
->ikm_prealloc
== IP_NULL
);
611 kmsg
->ikm_prealloc
= IP_NULL
;
612 IP_SET_PREALLOC(port
, kmsg
);
616 * Routine: ipc_kmsg_clear_prealloc
618 * Release the Assignment of a preallocated message buffer from a port.
623 ipc_kmsg_clear_prealloc(
627 assert(kmsg
->ikm_prealloc
== port
);
629 kmsg
->ikm_prealloc
= IP_NULL
;
630 IP_CLEAR_PREALLOC(port
, kmsg
);
634 * Routine: ipc_kmsg_get
636 * Allocates a kernel message buffer.
637 * Copies a user message to the message buffer.
641 * MACH_MSG_SUCCESS Acquired a message buffer.
642 * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
643 * MACH_SEND_MSG_TOO_SMALL Message size not long-word multiple.
644 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
645 * MACH_SEND_INVALID_DATA Couldn't copy message data.
650 mach_msg_header_t
*msg
,
651 mach_msg_size_t size
,
654 mach_msg_size_t msg_and_trailer_size
;
656 mach_msg_max_trailer_t
*trailer
;
657 mach_port_name_t dest_name
;
658 ipc_entry_t dest_entry
;
659 ipc_port_t dest_port
;
661 if ((size
< sizeof(mach_msg_header_t
)) || (size
& 3))
662 return MACH_SEND_MSG_TOO_SMALL
;
664 msg_and_trailer_size
= size
+ MAX_TRAILER_SIZE
;
666 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
668 if (kmsg
== IKM_NULL
)
669 return MACH_SEND_NO_BUFFER
;
671 if (copyinmsg((char *) msg
, (char *) &kmsg
->ikm_header
, size
)) {
673 return MACH_SEND_INVALID_DATA
;
676 kmsg
->ikm_header
.msgh_size
= size
;
679 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
680 * However, the internal size field of the trailer (msgh_trailer_size)
681 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
682 * the cases where no implicit data is requested.
684 trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)&kmsg
->ikm_header
+ size
);
685 trailer
->msgh_sender
= current_act()->task
->sec_token
;
686 trailer
->msgh_audit
= current_act()->task
->audit_token
;
687 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
688 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
691 if(trcWork
.traceMask
) dbgTrace((unsigned int)kmsg
->ikm_header
.msgh_id
,
692 (unsigned int)kmsg
->ikm_header
.msgh_remote_port
,
693 (unsigned int)kmsg
->ikm_header
.msgh_local_port
, 0);
696 return MACH_MSG_SUCCESS
;
700 * Routine: ipc_kmsg_get_from_kernel
702 * Allocates a kernel message buffer.
703 * Copies a kernel message to the message buffer.
704 * Only resource errors are allowed.
707 * Ports in header are ipc_port_t.
709 * MACH_MSG_SUCCESS Acquired a message buffer.
710 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
714 ipc_kmsg_get_from_kernel(
715 mach_msg_header_t
*msg
,
716 mach_msg_size_t size
,
720 mach_msg_size_t msg_and_trailer_size
;
721 mach_msg_max_trailer_t
*trailer
;
722 ipc_port_t dest_port
;
724 assert(size
>= sizeof(mach_msg_header_t
));
725 assert((size
& 3) == 0);
727 assert(IP_VALID((ipc_port_t
) msg
->msgh_remote_port
));
728 dest_port
= (ipc_port_t
)msg
->msgh_remote_port
;
730 msg_and_trailer_size
= size
+ MAX_TRAILER_SIZE
;
733 * See if the port has a pre-allocated kmsg for kernel
734 * clients. These are set up for those kernel clients
735 * which cannot afford to wait.
737 if (IP_PREALLOC(dest_port
)) {
739 if (!ip_active(dest_port
)) {
740 ip_unlock(dest_port
);
741 return MACH_SEND_NO_BUFFER
;
743 assert(IP_PREALLOC(dest_port
));
744 kmsg
= dest_port
->ip_premsg
;
745 if (msg_and_trailer_size
> kmsg
->ikm_size
) {
746 ip_unlock(dest_port
);
747 return MACH_SEND_TOO_LARGE
;
749 if (ikm_prealloc_inuse(kmsg
)) {
750 ip_unlock(dest_port
);
751 return MACH_SEND_NO_BUFFER
;
753 ikm_prealloc_set_inuse(kmsg
, dest_port
);
754 ip_unlock(dest_port
);
756 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
757 if (kmsg
== IKM_NULL
)
758 return MACH_SEND_NO_BUFFER
;
761 (void) memcpy((void *) &kmsg
->ikm_header
, (const void *) msg
, size
);
763 kmsg
->ikm_header
.msgh_size
= size
;
766 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
767 * However, the internal size field of the trailer (msgh_trailer_size)
768 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to
769 * optimize the cases where no implicit data is requested.
771 trailer
= (mach_msg_max_trailer_t
*)
772 ((vm_offset_t
)&kmsg
->ikm_header
+ size
);
773 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
774 trailer
->msgh_audit
= KERNEL_AUDIT_TOKEN
;
775 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
776 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
779 return MACH_MSG_SUCCESS
;
783 * Routine: ipc_kmsg_send
785 * Send a message. The message holds a reference
786 * for the destination port in the msgh_remote_port field.
788 * If unsuccessful, the caller still has possession of
789 * the message and must do something with it. If successful,
790 * the message is queued, given to a receiver, destroyed,
791 * or handled directly by the kernel via mach_msg.
795 * MACH_MSG_SUCCESS The message was accepted.
796 * MACH_SEND_TIMED_OUT Caller still has message.
797 * MACH_SEND_INTERRUPTED Caller still has message.
802 mach_msg_option_t option
,
803 mach_msg_timeout_t timeout
)
805 kern_return_t save_wait_result
;
808 port
= (ipc_port_t
) kmsg
->ikm_header
.msgh_remote_port
;
809 assert(IP_VALID(port
));
813 if (port
->ip_receiver
== ipc_space_kernel
) {
816 * We can check ip_receiver == ipc_space_kernel
817 * before checking that the port is active because
818 * ipc_port_dealloc_kernel clears ip_receiver
819 * before destroying a kernel port.
821 assert(ip_active(port
));
822 port
->ip_messages
.imq_seqno
++;
825 current_task()->messages_sent
++;
828 * Call the server routine, and get the reply message to send.
830 kmsg
= ipc_kobject_server(kmsg
);
831 if (kmsg
== IKM_NULL
)
832 return MACH_MSG_SUCCESS
;
834 port
= (ipc_port_t
) kmsg
->ikm_header
.msgh_remote_port
;
835 assert(IP_VALID(port
));
837 /* fall thru with reply - same options */
841 * Can't deliver to a dead port.
842 * However, we can pretend it got sent
843 * and was then immediately destroyed.
845 if (!ip_active(port
)) {
847 * We can't let ipc_kmsg_destroy deallocate
848 * the port right, because we might end up
849 * in an infinite loop trying to deliver
850 * a send-once notification.
854 ip_check_unlock(port
);
855 kmsg
->ikm_header
.msgh_remote_port
= MACH_PORT_NULL
;
856 ipc_kmsg_destroy(kmsg
);
857 return MACH_MSG_SUCCESS
;
860 if (kmsg
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_CIRCULAR
) {
863 /* don't allow the creation of a circular loop */
865 ipc_kmsg_destroy(kmsg
);
866 return MACH_MSG_SUCCESS
;
870 * We have a valid message and a valid reference on the port.
871 * we can unlock the port and call mqueue_send() on it's message
875 return (ipc_mqueue_send(&port
->ip_messages
, kmsg
, option
, timeout
));
879 * Routine: ipc_kmsg_put
881 * Copies a message buffer to a user message.
882 * Copies only the specified number of bytes.
883 * Frees the message buffer.
885 * Nothing locked. The message buffer must have clean
888 * MACH_MSG_SUCCESS Copied data out of message buffer.
889 * MACH_RCV_INVALID_DATA Couldn't copy to user message.
894 mach_msg_header_t
*msg
,
896 mach_msg_size_t size
)
898 mach_msg_return_t mr
;
900 if (copyoutmsg((const char *) &kmsg
->ikm_header
, (char *) msg
, size
))
901 mr
= MACH_RCV_INVALID_DATA
;
903 mr
= MACH_MSG_SUCCESS
;
910 * Routine: ipc_kmsg_put_to_kernel
912 * Copies a message buffer to a kernel message.
913 * Frees the message buffer.
920 ipc_kmsg_put_to_kernel(
921 mach_msg_header_t
*msg
,
923 mach_msg_size_t size
)
925 (void) memcpy((void *) msg
, (const void *) &kmsg
->ikm_header
, size
);
931 * Routine: ipc_kmsg_copyin_header
933 * "Copy-in" port rights in the header of a message.
934 * Operates atomically; if it doesn't succeed the
935 * message header and the space are left untouched.
936 * If it does succeed the remote/local port fields
937 * contain object pointers instead of port names,
938 * and the bits field is updated. The destination port
939 * will be a valid port pointer.
941 * The notify argument implements the MACH_SEND_CANCEL option.
942 * If it is not MACH_PORT_NULL, it should name a receive right.
943 * If the processing of the destination port would generate
944 * a port-deleted notification (because the right for the
945 * destination port is destroyed and it had a request for
946 * a dead-name notification registered), and the port-deleted
947 * notification would be sent to the named receive right,
948 * then it isn't sent and the send-once right for the notify
949 * port is quietly destroyed.
954 * MACH_MSG_SUCCESS Successful copyin.
955 * MACH_SEND_INVALID_HEADER
956 * Illegal value in the message header bits.
957 * MACH_SEND_INVALID_DEST The space is dead.
958 * MACH_SEND_INVALID_NOTIFY
959 * Notify is non-null and doesn't name a receive right.
960 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
961 * MACH_SEND_INVALID_DEST Can't copyin destination port.
962 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
963 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
964 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
968 ipc_kmsg_copyin_header(
969 mach_msg_header_t
*msg
,
971 mach_port_name_t notify
)
973 mach_msg_bits_t mbits
= msg
->msgh_bits
& MACH_MSGH_BITS_USER
;
974 mach_port_name_t dest_name
= (mach_port_name_t
)msg
->msgh_remote_port
;
975 mach_port_name_t reply_name
= (mach_port_name_t
)msg
->msgh_local_port
;
978 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
979 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
980 ipc_object_t dest_port
, reply_port
;
981 ipc_port_t dest_soright
, reply_soright
;
982 ipc_port_t notify_port
;
984 if ((mbits
!= msg
->msgh_bits
) ||
985 (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type
)) ||
987 (reply_name
!= MACH_PORT_NULL
) :
988 !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type
)))
989 return MACH_SEND_INVALID_HEADER
;
991 reply_soright
= IP_NULL
; /* in case we go to invalid dest early */
993 is_write_lock(space
);
994 if (!space
->is_active
)
997 if (!MACH_PORT_VALID(dest_name
))
1000 if (notify
!= MACH_PORT_NULL
) {
1003 if ((entry
= ipc_entry_lookup(space
, notify
)) == IE_NULL
) {
1004 is_write_unlock(space
);
1005 return MACH_SEND_INVALID_NOTIFY
;
1007 if((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) == 0) {
1008 is_write_unlock(space
);
1009 return MACH_SEND_INVALID_NOTIFY
;
1012 notify_port
= (ipc_port_t
) entry
->ie_object
;
1015 if (dest_name
== reply_name
) {
1017 mach_port_name_t name
= dest_name
;
1020 * Destination and reply ports are the same!
1021 * This is a little tedious to make atomic, because
1022 * there are 25 combinations of dest_type/reply_type.
1023 * However, most are easy. If either is move-sonce,
1024 * then there must be an error. If either are
1025 * make-send or make-sonce, then we must be looking
1026 * at a receive right so the port can't die.
1027 * The hard cases are the combinations of
1028 * copy-send and make-send.
1031 entry
= ipc_entry_lookup(space
, name
);
1032 if (entry
== IE_NULL
)
1035 assert(reply_type
!= 0); /* because name not null */
1037 if (!ipc_right_copyin_check(space
, name
, entry
, reply_type
))
1040 if ((dest_type
== MACH_MSG_TYPE_MOVE_SEND_ONCE
) ||
1041 (reply_type
== MACH_MSG_TYPE_MOVE_SEND_ONCE
)) {
1043 * Why must there be an error? To get a valid
1044 * destination, this entry must name a live
1045 * port (not a dead name or dead port). However
1046 * a successful move-sonce will destroy a
1047 * live entry. Therefore the other copyin,
1048 * whatever it is, would fail. We've already
1049 * checked for reply port errors above,
1050 * so report a destination error.
1054 } else if ((dest_type
== MACH_MSG_TYPE_MAKE_SEND
) ||
1055 (dest_type
== MACH_MSG_TYPE_MAKE_SEND_ONCE
) ||
1056 (reply_type
== MACH_MSG_TYPE_MAKE_SEND
) ||
1057 (reply_type
== MACH_MSG_TYPE_MAKE_SEND_ONCE
)) {
1058 kr
= ipc_right_copyin(space
, name
, entry
,
1060 &dest_port
, &dest_soright
);
1061 if (kr
!= KERN_SUCCESS
)
1065 * Either dest or reply needs a receive right.
1066 * We know the receive right is there, because
1067 * of the copyin_check and copyin calls. Hence
1068 * the port is not in danger of dying. If dest
1069 * used the receive right, then the right needed
1070 * by reply (and verified by copyin_check) will
1074 assert(IO_VALID(dest_port
));
1075 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
1076 assert(dest_soright
== IP_NULL
);
1078 kr
= ipc_right_copyin(space
, name
, entry
,
1080 &reply_port
, &reply_soright
);
1082 assert(kr
== KERN_SUCCESS
);
1083 assert(reply_port
== dest_port
);
1084 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
1085 assert(reply_soright
== IP_NULL
);
1086 } else if ((dest_type
== MACH_MSG_TYPE_COPY_SEND
) &&
1087 (reply_type
== MACH_MSG_TYPE_COPY_SEND
)) {
1089 * To make this atomic, just do one copy-send,
1090 * and dup the send right we get out.
1093 kr
= ipc_right_copyin(space
, name
, entry
,
1095 &dest_port
, &dest_soright
);
1096 if (kr
!= KERN_SUCCESS
)
1099 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND
);
1100 assert(dest_soright
== IP_NULL
);
1103 * It's OK if the port we got is dead now,
1104 * so reply_port is IP_DEAD, because the msg
1105 * won't go anywhere anyway.
1108 reply_port
= (ipc_object_t
)
1109 ipc_port_copy_send((ipc_port_t
) dest_port
);
1110 reply_soright
= IP_NULL
;
1111 } else if ((dest_type
== MACH_MSG_TYPE_MOVE_SEND
) &&
1112 (reply_type
== MACH_MSG_TYPE_MOVE_SEND
)) {
1114 * This is an easy case. Just use our
1115 * handy-dandy special-purpose copyin call
1116 * to get two send rights for the price of one.
1119 kr
= ipc_right_copyin_two(space
, name
, entry
,
1120 &dest_port
, &dest_soright
);
1121 if (kr
!= KERN_SUCCESS
)
1124 /* the entry might need to be deallocated */
1125 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1126 ipc_entry_dealloc(space
, name
, entry
);
1128 reply_port
= dest_port
;
1129 reply_soright
= IP_NULL
;
1133 assert(((dest_type
== MACH_MSG_TYPE_COPY_SEND
) &&
1134 (reply_type
== MACH_MSG_TYPE_MOVE_SEND
)) ||
1135 ((dest_type
== MACH_MSG_TYPE_MOVE_SEND
) &&
1136 (reply_type
== MACH_MSG_TYPE_COPY_SEND
)));
1139 * To make this atomic, just do a move-send,
1140 * and dup the send right we get out.
1143 kr
= ipc_right_copyin(space
, name
, entry
,
1144 MACH_MSG_TYPE_MOVE_SEND
, FALSE
,
1145 &dest_port
, &soright
);
1146 if (kr
!= KERN_SUCCESS
)
1149 /* the entry might need to be deallocated */
1151 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1152 ipc_entry_dealloc(space
, name
, entry
);
1155 * It's OK if the port we got is dead now,
1156 * so reply_port is IP_DEAD, because the msg
1157 * won't go anywhere anyway.
1160 reply_port
= (ipc_object_t
)
1161 ipc_port_copy_send((ipc_port_t
) dest_port
);
1163 if (dest_type
== MACH_MSG_TYPE_MOVE_SEND
) {
1164 dest_soright
= soright
;
1165 reply_soright
= IP_NULL
;
1167 dest_soright
= IP_NULL
;
1168 reply_soright
= soright
;
1171 } else if (!MACH_PORT_VALID(reply_name
)) {
1175 * No reply port! This is an easy case
1176 * to make atomic. Just copyin the destination.
1179 entry
= ipc_entry_lookup(space
, dest_name
);
1180 if (entry
== IE_NULL
)
1183 kr
= ipc_right_copyin(space
, dest_name
, entry
,
1185 &dest_port
, &dest_soright
);
1186 if (kr
!= KERN_SUCCESS
)
1189 /* the entry might need to be deallocated */
1191 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1192 ipc_entry_dealloc(space
, dest_name
, entry
);
1194 reply_port
= (ipc_object_t
) reply_name
;
1195 reply_soright
= IP_NULL
;
1197 ipc_entry_t dest_entry
, reply_entry
;
1198 ipc_port_t saved_reply
;
1201 * This is the tough case to make atomic.
1202 * The difficult problem is serializing with port death.
1203 * At the time we copyin dest_port, it must be alive.
1204 * If reply_port is alive when we copyin it, then
1205 * we are OK, because we serialize before the death
1206 * of both ports. Assume reply_port is dead at copyin.
1207 * Then if dest_port dies/died after reply_port died,
1208 * we are OK, because we serialize between the death
1209 * of the two ports. So the bad case is when dest_port
1210 * dies after its copyin, reply_port dies before its
1211 * copyin, and dest_port dies before reply_port. Then
1212 * the copyins operated as if dest_port was alive
1213 * and reply_port was dead, which shouldn't have happened
1214 * because they died in the other order.
1216 * Note that it is easy for a user task to tell if
1217 * a copyin happened before or after a port died.
1218 * For example, suppose both dest and reply are
1219 * send-once rights (types are both move-sonce) and
1220 * both rights have dead-name requests registered.
1221 * If a port dies before copyin, a dead-name notification
1222 * is generated and the dead name's urefs are incremented,
1223 * and if the copyin happens first, a port-deleted
1224 * notification is generated.
1226 * Note that although the entries are different,
1227 * dest_port and reply_port might still be the same.
1229 * JMM - The code to handle this was too expensive and, anyway,
1230 * we intend to separate the dest lookup from the reply copyin
1231 * by a wide margin, so the user will have to learn to deal!
1232 * I will be making the change soon!
1235 dest_entry
= ipc_entry_lookup(space
, dest_name
);
1236 if (dest_entry
== IE_NULL
)
1239 reply_entry
= ipc_entry_lookup(space
, reply_name
);
1240 if (reply_entry
== IE_NULL
)
1243 assert(dest_entry
!= reply_entry
); /* names are not equal */
1244 assert(reply_type
!= 0); /* because reply_name not null */
1246 if (!ipc_right_copyin_check(space
, reply_name
, reply_entry
,
1250 kr
= ipc_right_copyin(space
, dest_name
, dest_entry
,
1252 &dest_port
, &dest_soright
);
1253 if (kr
!= KERN_SUCCESS
)
1256 assert(IO_VALID(dest_port
));
1258 kr
= ipc_right_copyin(space
, reply_name
, reply_entry
,
1260 &reply_port
, &reply_soright
);
1262 assert(kr
== KERN_SUCCESS
);
1264 /* the entries might need to be deallocated */
1266 if (IE_BITS_TYPE(reply_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1267 ipc_entry_dealloc(space
, reply_name
, reply_entry
);
1269 if (IE_BITS_TYPE(dest_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1270 ipc_entry_dealloc(space
, dest_name
, dest_entry
);
1274 * At this point, dest_port, reply_port,
1275 * dest_soright, reply_soright are all initialized.
1276 * Any defunct entries have been deallocated.
1277 * The space is still write-locked, and we need to
1278 * make the MACH_SEND_CANCEL check. The notify_port pointer
1279 * is still usable, because the copyin code above won't ever
1280 * deallocate a receive right, so its entry still exists
1281 * and holds a ref. Note notify_port might even equal
1282 * dest_port or reply_port.
1285 if ((notify
!= MACH_PORT_NULL
) &&
1286 (dest_soright
== notify_port
)) {
1287 ipc_port_release_sonce(dest_soright
);
1288 dest_soright
= IP_NULL
;
1291 is_write_unlock(space
);
1293 if (dest_soright
!= IP_NULL
)
1294 ipc_notify_port_deleted(dest_soright
, dest_name
);
1296 if (reply_soright
!= IP_NULL
)
1297 ipc_notify_port_deleted(reply_soright
, reply_name
);
1299 dest_type
= ipc_object_copyin_type(dest_type
);
1300 reply_type
= ipc_object_copyin_type(reply_type
);
1302 msg
->msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
1303 MACH_MSGH_BITS(dest_type
, reply_type
));
1304 msg
->msgh_remote_port
= (ipc_port_t
)dest_port
;
1305 msg
->msgh_local_port
= (ipc_port_t
)reply_port
;
1307 return MACH_MSG_SUCCESS
;
1310 is_write_unlock(space
);
1311 return MACH_SEND_INVALID_REPLY
;
1314 is_write_unlock(space
);
1315 if (reply_soright
!= IP_NULL
)
1316 ipc_notify_port_deleted(reply_soright
, reply_name
);
1317 return MACH_SEND_INVALID_DEST
;
1321 * Routine: ipc_kmsg_copyin_body
1323 * "Copy-in" port rights and out-of-line memory
1324 * in the message body.
1326 * In all failure cases, the message is left holding
1327 * no rights or memory. However, the message buffer
1328 * is not deallocated. If successful, the message
1329 * contains a valid destination port.
1333 * MACH_MSG_SUCCESS Successful copyin.
1334 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
1335 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
1336 * MACH_SEND_INVALID_TYPE Bad type specification.
1337 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
1338 * MACH_SEND_INVALID_RT_OOL_SIZE OOL Buffer too large for RT
1339 * MACH_MSG_INVALID_RT_DESCRIPTOR Dealloc and RT are incompatible
1343 ipc_kmsg_copyin_body(
1349 mach_msg_body_t
*body
;
1350 mach_msg_descriptor_t
*saddr
, *eaddr
;
1352 mach_msg_return_t mr
;
1355 vm_size_t space_needed
= 0;
1356 vm_offset_t paddr
= 0;
1357 mach_msg_descriptor_t
*sstart
;
1358 vm_map_copy_t copy
= VM_MAP_COPY_NULL
;
1361 * Determine if the target is a kernel port.
1363 dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
1366 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
1367 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
1368 eaddr
= saddr
+ body
->msgh_descriptor_count
;
1370 /* make sure the message does not ask for more msg descriptors
1371 * than the message can hold.
1374 if (eaddr
<= saddr
||
1375 eaddr
> (mach_msg_descriptor_t
*) (&kmsg
->ikm_header
+
1376 kmsg
->ikm_header
.msgh_size
)) {
1377 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1378 return MACH_SEND_MSG_TOO_SMALL
;
1382 * Make an initial pass to determine kernal VM space requirements for
1385 for (sstart
= saddr
; sstart
< eaddr
; sstart
++) {
1387 if (sstart
->type
.type
== MACH_MSG_OOL_DESCRIPTOR
||
1388 sstart
->type
.type
== MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
1390 if (sstart
->out_of_line
.copy
!= MACH_MSG_PHYSICAL_COPY
&&
1391 sstart
->out_of_line
.copy
!= MACH_MSG_VIRTUAL_COPY
) {
1393 * Invalid copy option
1395 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1396 return MACH_SEND_INVALID_TYPE
;
1399 if ((sstart
->out_of_line
.size
>= MSG_OOL_SIZE_SMALL
) &&
1400 (sstart
->out_of_line
.copy
== MACH_MSG_PHYSICAL_COPY
) &&
1401 !(sstart
->out_of_line
.deallocate
)) {
1404 * Out-of-line memory descriptor, accumulate kernel
1405 * memory requirements
1407 space_needed
+= round_page_32(sstart
->out_of_line
.size
);
1408 if (space_needed
> ipc_kmsg_max_vm_space
) {
1411 * Per message kernel memory limit exceeded
1413 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1414 return MACH_MSG_VM_KERNEL
;
1421 * Allocate space in the pageable kernel ipc copy map for all the
1422 * ool data that is to be physically copied. Map is marked wait for
1426 if (vm_allocate(ipc_kernel_copy_map
, &paddr
, space_needed
, TRUE
) !=
1428 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1429 return MACH_MSG_VM_KERNEL
;
1434 * handle the OOL regions and port descriptors.
1435 * the check for complex messages was done earlier.
1438 for (i
= 0, sstart
= saddr
; sstart
< eaddr
; sstart
++) {
1440 switch (sstart
->type
.type
) {
1442 case MACH_MSG_PORT_DESCRIPTOR
: {
1443 mach_msg_type_name_t name
;
1444 ipc_object_t object
;
1445 mach_msg_port_descriptor_t
*dsc
;
1447 dsc
= &sstart
->port
;
1449 /* this is really the type SEND, SEND_ONCE, etc. */
1450 name
= dsc
->disposition
;
1451 dsc
->disposition
= ipc_object_copyin_type(name
);
1453 if (!MACH_PORT_VALID((mach_port_name_t
)dsc
->name
)) {
1457 kr
= ipc_object_copyin(space
, (mach_port_name_t
)dsc
->name
, name
, &object
);
1458 if (kr
!= KERN_SUCCESS
) {
1459 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1460 return MACH_SEND_INVALID_RIGHT
;
1462 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1463 ipc_port_check_circularity((ipc_port_t
) object
,
1464 (ipc_port_t
) dest
)) {
1465 kmsg
->ikm_header
.msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1467 dsc
->name
= (ipc_port_t
) object
;
1471 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1472 case MACH_MSG_OOL_DESCRIPTOR
: {
1477 mach_msg_ool_descriptor_t
*dsc
;
1479 dsc
= &sstart
->out_of_line
;
1480 dealloc
= dsc
->deallocate
;
1481 addr
= (vm_offset_t
) dsc
->address
;
1487 } else if ((length
>= MSG_OOL_SIZE_SMALL
) &&
1488 (dsc
->copy
== MACH_MSG_PHYSICAL_COPY
) && !dealloc
) {
1491 * If the request is a physical copy and the source
1492 * is not being deallocated, then allocate space
1493 * in the kernel's pageable ipc copy map and copy
1494 * the data in. The semantics guarantee that the
1495 * data will have been physically copied before
1496 * the send operation terminates. Thus if the data
1497 * is not being deallocated, we must be prepared
1498 * to page if the region is sufficiently large.
1500 if (copyin((const char *) addr
, (char *) paddr
,
1502 ipc_kmsg_clean_partial(kmsg
, i
, paddr
,
1504 return MACH_SEND_INVALID_MEMORY
;
1508 * The kernel ipc copy map is marked no_zero_fill.
1509 * If the transfer is not a page multiple, we need
1510 * to zero fill the balance.
1512 if (!page_aligned(length
)) {
1513 (void) memset((void *) (paddr
+ length
), 0,
1514 round_page_32(length
) - length
);
1516 if (vm_map_copyin(ipc_kernel_copy_map
, paddr
, length
,
1517 TRUE
, ©
) != KERN_SUCCESS
) {
1518 ipc_kmsg_clean_partial(kmsg
, i
, paddr
,
1520 return MACH_MSG_VM_KERNEL
;
1522 dsc
->address
= (void *) copy
;
1523 paddr
+= round_page_32(length
);
1524 space_needed
-= round_page_32(length
);
1528 * Make a vm_map_copy_t of the of the data. If the
1529 * data is small, this will do an optimized physical
1530 * copy. Otherwise, it will do a virtual copy.
1532 * NOTE: A virtual copy is OK if the original is being
1533 * deallocted, even if a physical copy was requested.
1535 kr
= vm_map_copyin(map
, addr
, length
, dealloc
, ©
);
1536 if (kr
!= KERN_SUCCESS
) {
1537 ipc_kmsg_clean_partial(kmsg
,i
,paddr
,space_needed
);
1538 return (kr
== KERN_RESOURCE_SHORTAGE
) ?
1539 MACH_MSG_VM_KERNEL
:
1540 MACH_SEND_INVALID_MEMORY
;
1542 dsc
->address
= (void *) copy
;
1547 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
1551 ipc_object_t
*objects
;
1553 mach_msg_type_name_t name
;
1554 mach_msg_ool_ports_descriptor_t
*dsc
;
1556 dsc
= &sstart
->ool_ports
;
1557 addr
= (vm_offset_t
) dsc
->address
;
1559 /* calculate length of data in bytes, rounding up */
1560 length
= dsc
->count
* sizeof(mach_port_name_t
);
1564 dsc
->address
= (void *) 0;
1568 data
= kalloc(length
);
1571 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1572 return MACH_SEND_NO_BUFFER
;
1575 if (copyinmap(map
, addr
, data
, length
)) {
1576 kfree(data
, length
);
1577 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1578 return MACH_SEND_INVALID_MEMORY
;
1581 if (dsc
->deallocate
) {
1582 (void) vm_deallocate(map
, addr
, length
);
1585 dsc
->address
= (void *) data
;
1587 /* this is really the type SEND, SEND_ONCE, etc. */
1588 name
= dsc
->disposition
;
1589 dsc
->disposition
= ipc_object_copyin_type(name
);
1591 objects
= (ipc_object_t
*) data
;
1593 for ( j
= 0; j
< dsc
->count
; j
++) {
1594 mach_port_name_t port
= (mach_port_name_t
) objects
[j
];
1595 ipc_object_t object
;
1597 if (!MACH_PORT_VALID(port
))
1600 kr
= ipc_object_copyin(space
, port
, name
, &object
);
1602 if (kr
!= KERN_SUCCESS
) {
1605 for(k
= 0; k
< j
; k
++) {
1606 object
= objects
[k
];
1607 if (IPC_OBJECT_VALID(object
))
1608 ipc_object_destroy(object
, dsc
->disposition
);
1610 kfree(data
, length
);
1611 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1612 return MACH_SEND_INVALID_RIGHT
;
1615 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1616 ipc_port_check_circularity(
1617 (ipc_port_t
) object
,
1619 kmsg
->ikm_header
.msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1621 objects
[j
] = object
;
1629 * Invalid descriptor
1631 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1632 return MACH_SEND_INVALID_TYPE
;
1639 kmsg
->ikm_header
.msgh_bits
&= ~MACH_MSGH_BITS_COMPLEX
;
1640 return MACH_MSG_SUCCESS
;
1645 * Routine: ipc_kmsg_copyin
1647 * "Copy-in" port rights and out-of-line memory
1650 * In all failure cases, the message is left holding
1651 * no rights or memory. However, the message buffer
1652 * is not deallocated. If successful, the message
1653 * contains a valid destination port.
1657 * MACH_MSG_SUCCESS Successful copyin.
1658 * MACH_SEND_INVALID_HEADER
1659 * Illegal value in the message header bits.
1660 * MACH_SEND_INVALID_NOTIFY Bad notify port.
1661 * MACH_SEND_INVALID_DEST Can't copyin destination port.
1662 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
1663 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
1664 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
1665 * MACH_SEND_INVALID_TYPE Bad type specification.
1666 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
1674 mach_port_name_t notify
)
1676 mach_msg_return_t mr
;
1678 mr
= ipc_kmsg_copyin_header(&kmsg
->ikm_header
, space
, notify
);
1679 if (mr
!= MACH_MSG_SUCCESS
)
1682 if ((kmsg
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
1683 return MACH_MSG_SUCCESS
;
1685 return( ipc_kmsg_copyin_body( kmsg
, space
, map
) );
1689 * Routine: ipc_kmsg_copyin_from_kernel
1691 * "Copy-in" port rights and out-of-line memory
1692 * in a message sent from the kernel.
1694 * Because the message comes from the kernel,
1695 * the implementation assumes there are no errors
1696 * or peculiarities in the message.
1698 * Returns TRUE if queueing the message
1699 * would result in a circularity.
1705 ipc_kmsg_copyin_from_kernel(
1708 mach_msg_bits_t bits
= kmsg
->ikm_header
.msgh_bits
;
1709 mach_msg_type_name_t rname
= MACH_MSGH_BITS_REMOTE(bits
);
1710 mach_msg_type_name_t lname
= MACH_MSGH_BITS_LOCAL(bits
);
1711 ipc_object_t remote
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
1712 ipc_object_t local
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
1714 /* translate the destination and reply ports */
1716 ipc_object_copyin_from_kernel(remote
, rname
);
1717 if (IO_VALID(local
))
1718 ipc_object_copyin_from_kernel(local
, lname
);
1721 * The common case is a complex message with no reply port,
1722 * because that is what the memory_object interface uses.
1725 if (bits
== (MACH_MSGH_BITS_COMPLEX
|
1726 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0))) {
1727 bits
= (MACH_MSGH_BITS_COMPLEX
|
1728 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND
, 0));
1730 kmsg
->ikm_header
.msgh_bits
= bits
;
1732 bits
= (MACH_MSGH_BITS_OTHER(bits
) |
1733 MACH_MSGH_BITS(ipc_object_copyin_type(rname
),
1734 ipc_object_copyin_type(lname
)));
1736 kmsg
->ikm_header
.msgh_bits
= bits
;
1737 if ((bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
1741 mach_msg_descriptor_t
*saddr
, *eaddr
;
1742 mach_msg_body_t
*body
;
1744 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
1745 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
1746 eaddr
= (mach_msg_descriptor_t
*) saddr
+ body
->msgh_descriptor_count
;
1748 for ( ; saddr
< eaddr
; saddr
++) {
1750 switch (saddr
->type
.type
) {
1752 case MACH_MSG_PORT_DESCRIPTOR
: {
1753 mach_msg_type_name_t name
;
1754 ipc_object_t object
;
1755 mach_msg_port_descriptor_t
*dsc
;
1759 /* this is really the type SEND, SEND_ONCE, etc. */
1760 name
= dsc
->disposition
;
1761 object
= (ipc_object_t
) dsc
->name
;
1762 dsc
->disposition
= ipc_object_copyin_type(name
);
1764 if (!IO_VALID(object
)) {
1768 ipc_object_copyin_from_kernel(object
, name
);
1770 /* CDY avoid circularity when the destination is also */
1771 /* the kernel. This check should be changed into an */
1772 /* assert when the new kobject model is in place since*/
1773 /* ports will not be used in kernel to kernel chats */
1775 if (((ipc_port_t
)remote
)->ip_receiver
!= ipc_space_kernel
) {
1776 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1777 ipc_port_check_circularity((ipc_port_t
) object
,
1778 (ipc_port_t
) remote
)) {
1779 kmsg
->ikm_header
.msgh_bits
|=
1780 MACH_MSGH_BITS_CIRCULAR
;
1785 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1786 case MACH_MSG_OOL_DESCRIPTOR
: {
1788 * The sender should supply ready-made memory, i.e.
1789 * a vm_map_copy_t, so we don't need to do anything.
1793 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
1794 ipc_object_t
*objects
;
1796 mach_msg_type_name_t name
;
1797 mach_msg_ool_ports_descriptor_t
*dsc
;
1799 dsc
= &saddr
->ool_ports
;
1801 /* this is really the type SEND, SEND_ONCE, etc. */
1802 name
= dsc
->disposition
;
1803 dsc
->disposition
= ipc_object_copyin_type(name
);
1805 objects
= (ipc_object_t
*) dsc
->address
;
1807 for ( j
= 0; j
< dsc
->count
; j
++) {
1808 ipc_object_t object
= objects
[j
];
1810 if (!IO_VALID(object
))
1813 ipc_object_copyin_from_kernel(object
, name
);
1815 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1816 ipc_port_check_circularity(
1817 (ipc_port_t
) object
,
1818 (ipc_port_t
) remote
))
1819 kmsg
->ikm_header
.msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1825 panic("ipc_kmsg_copyin_from_kernel: bad descriptor");
1826 #endif /* MACH_ASSERT */
1834 * Routine: ipc_kmsg_copyout_header
1836 * "Copy-out" port rights in the header of a message.
1837 * Operates atomically; if it doesn't succeed the
1838 * message header and the space are left untouched.
1839 * If it does succeed the remote/local port fields
1840 * contain port names instead of object pointers,
1841 * and the bits field is updated.
1843 * The notify argument implements the MACH_RCV_NOTIFY option.
1844 * If it is not MACH_PORT_NULL, it should name a receive right.
1845 * If the process of receiving the reply port creates a
1846 * new right in the receiving task, then the new right is
1847 * automatically registered for a dead-name notification,
1848 * with the notify port supplying the send-once right.
1852 * MACH_MSG_SUCCESS Copied out port rights.
1853 * MACH_RCV_INVALID_NOTIFY
1854 * Notify is non-null and doesn't name a receive right.
1855 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
1856 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
1857 * The space is dead.
1858 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
1859 * No room in space for another name.
1860 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
1861 * Couldn't allocate memory for the reply port.
1862 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
1863 * Couldn't allocate memory for the dead-name request.
1867 ipc_kmsg_copyout_header(
1868 mach_msg_header_t
*msg
,
1870 mach_port_name_t notify
)
1872 mach_msg_bits_t mbits
= msg
->msgh_bits
;
1873 ipc_port_t dest
= (ipc_port_t
) msg
->msgh_remote_port
;
1875 assert(IP_VALID(dest
));
1878 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
1879 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
1880 ipc_port_t reply
= (ipc_port_t
) msg
->msgh_local_port
;
1881 mach_port_name_t dest_name
, reply_name
;
1883 if (IP_VALID(reply
)) {
1884 ipc_port_t notify_port
;
1889 * Handling notify (for MACH_RCV_NOTIFY) is tricky.
1890 * The problem is atomically making a send-once right
1891 * from the notify port and installing it for a
1892 * dead-name request in the new entry, because this
1893 * requires two port locks (on the notify port and
1894 * the reply port). However, we can safely make
1895 * and consume send-once rights for the notify port
1896 * as long as we hold the space locked. This isn't
1897 * an atomicity problem, because the only way
1898 * to detect that a send-once right has been created
1899 * and then consumed if it wasn't needed is by getting
1900 * at the receive right to look at ip_sorights, and
1901 * because the space is write-locked status calls can't
1902 * lookup the notify port receive right. When we make
1903 * the send-once right, we lock the notify port,
1904 * so any status calls in progress will be done.
1907 is_write_lock(space
);
1910 ipc_port_request_index_t request
;
1912 if (!space
->is_active
) {
1913 is_write_unlock(space
);
1914 return (MACH_RCV_HEADER_ERROR
|
1915 MACH_MSG_IPC_SPACE
);
1918 if (notify
!= MACH_PORT_NULL
) {
1919 notify_port
= ipc_port_lookup_notify(space
,
1921 if (notify_port
== IP_NULL
) {
1922 is_write_unlock(space
);
1923 return MACH_RCV_INVALID_NOTIFY
;
1926 notify_port
= IP_NULL
;
1928 if ((reply_type
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
1929 ipc_right_reverse(space
, (ipc_object_t
) reply
,
1930 &reply_name
, &entry
)) {
1931 /* reply port is locked and active */
1934 * We don't need the notify_port
1935 * send-once right, but we can't release
1936 * it here because reply port is locked.
1937 * Wait until after the copyout to
1938 * release the notify port right.
1941 assert(entry
->ie_bits
&
1942 MACH_PORT_TYPE_SEND_RECEIVE
);
1947 if (!ip_active(reply
)) {
1949 ip_check_unlock(reply
);
1951 if (notify_port
!= IP_NULL
)
1952 ipc_port_release_sonce(notify_port
);
1955 is_write_unlock(space
);
1958 reply_name
= MACH_PORT_DEAD
;
1962 reply_name
= (mach_port_name_t
)reply
;
1963 kr
= ipc_entry_get(space
, &reply_name
, &entry
);
1964 if (kr
!= KERN_SUCCESS
) {
1967 if (notify_port
!= IP_NULL
)
1968 ipc_port_release_sonce(notify_port
);
1970 /* space is locked */
1971 kr
= ipc_entry_grow_table(space
,
1973 if (kr
!= KERN_SUCCESS
) {
1974 /* space is unlocked */
1976 if (kr
== KERN_RESOURCE_SHORTAGE
)
1977 return (MACH_RCV_HEADER_ERROR
|
1978 MACH_MSG_IPC_KERNEL
);
1980 return (MACH_RCV_HEADER_ERROR
|
1981 MACH_MSG_IPC_SPACE
);
1983 /* space is locked again; start over */
1987 assert(IE_BITS_TYPE(entry
->ie_bits
) ==
1988 MACH_PORT_TYPE_NONE
);
1989 assert(entry
->ie_object
== IO_NULL
);
1991 if (notify_port
== IP_NULL
) {
1992 /* not making a dead-name request */
1994 entry
->ie_object
= (ipc_object_t
) reply
;
1998 kr
= ipc_port_dnrequest(reply
, reply_name
,
1999 notify_port
, &request
);
2000 if (kr
!= KERN_SUCCESS
) {
2003 ipc_port_release_sonce(notify_port
);
2005 ipc_entry_dealloc(space
, reply_name
, entry
);
2006 is_write_unlock(space
);
2009 if (!ip_active(reply
)) {
2010 /* will fail next time around loop */
2013 is_write_lock(space
);
2017 kr
= ipc_port_dngrow(reply
, ITS_SIZE_NONE
);
2018 /* port is unlocked */
2019 if (kr
!= KERN_SUCCESS
)
2020 return (MACH_RCV_HEADER_ERROR
|
2021 MACH_MSG_IPC_KERNEL
);
2023 is_write_lock(space
);
2027 notify_port
= IP_NULL
; /* don't release right below */
2029 entry
->ie_object
= (ipc_object_t
) reply
;
2030 entry
->ie_request
= request
;
2034 /* space and reply port are locked and active */
2036 ip_reference(reply
); /* hold onto the reply port */
2038 kr
= ipc_right_copyout(space
, reply_name
, entry
,
2039 reply_type
, TRUE
, (ipc_object_t
) reply
);
2040 /* reply port is unlocked */
2041 assert(kr
== KERN_SUCCESS
);
2043 if (notify_port
!= IP_NULL
)
2044 ipc_port_release_sonce(notify_port
);
2047 is_write_unlock(space
);
2050 * No reply port! This is an easy case.
2051 * We only need to have the space locked
2052 * when checking notify and when locking
2053 * the destination (to ensure atomicity).
2056 is_read_lock(space
);
2057 if (!space
->is_active
) {
2058 is_read_unlock(space
);
2059 return MACH_RCV_HEADER_ERROR
|MACH_MSG_IPC_SPACE
;
2062 if (notify
!= MACH_PORT_NULL
) {
2065 /* must check notify even though it won't be used */
2067 if ((entry
= ipc_entry_lookup(space
, notify
)) == IE_NULL
) {
2068 is_read_unlock(space
);
2069 return MACH_RCV_INVALID_NOTIFY
;
2072 if ((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) == 0) {
2073 is_read_unlock(space
);
2074 return MACH_RCV_INVALID_NOTIFY
;
2079 is_read_unlock(space
);
2081 reply_name
= (mach_port_name_t
) reply
;
2085 * At this point, the space is unlocked and the destination
2086 * port is locked. (Lock taken while space was locked.)
2087 * reply_name is taken care of; we still need dest_name.
2088 * We still hold a ref for reply (if it is valid).
2090 * If the space holds receive rights for the destination,
2091 * we return its name for the right. Otherwise the task
2092 * managed to destroy or give away the receive right between
2093 * receiving the message and this copyout. If the destination
2094 * is dead, return MACH_PORT_DEAD, and if the receive right
2095 * exists somewhere else (another space, in transit)
2096 * return MACH_PORT_NULL.
2098 * Making this copyout operation atomic with the previous
2099 * copyout of the reply port is a bit tricky. If there was
2100 * no real reply port (it wasn't IP_VALID) then this isn't
2101 * an issue. If the reply port was dead at copyout time,
2102 * then we are OK, because if dest is dead we serialize
2103 * after the death of both ports and if dest is alive
2104 * we serialize after reply died but before dest's (later) death.
2105 * So assume reply was alive when we copied it out. If dest
2106 * is alive, then we are OK because we serialize before
2107 * the ports' deaths. So assume dest is dead when we look at it.
2108 * If reply dies/died after dest, then we are OK because
2109 * we serialize after dest died but before reply dies.
2110 * So the hard case is when reply is alive at copyout,
2111 * dest is dead at copyout, and reply died before dest died.
2112 * In this case pretend that dest is still alive, so
2113 * we serialize while both ports are alive.
2115 * Because the space lock is held across the copyout of reply
2116 * and locking dest, the receive right for dest can't move
2117 * in or out of the space while the copyouts happen, so
2118 * that isn't an atomicity problem. In the last hard case
2119 * above, this implies that when dest is dead that the
2120 * space couldn't have had receive rights for dest at
2121 * the time reply was copied-out, so when we pretend
2122 * that dest is still alive, we can return MACH_PORT_NULL.
2124 * If dest == reply, then we have to make it look like
2125 * either both copyouts happened before the port died,
2126 * or both happened after the port died. This special
2127 * case works naturally if the timestamp comparison
2128 * is done correctly.
2133 if (ip_active(dest
)) {
2134 ipc_object_copyout_dest(space
, (ipc_object_t
) dest
,
2135 dest_type
, &dest_name
);
2136 /* dest is unlocked */
2138 ipc_port_timestamp_t timestamp
;
2140 timestamp
= dest
->ip_timestamp
;
2142 ip_check_unlock(dest
);
2144 if (IP_VALID(reply
)) {
2146 if (ip_active(reply
) ||
2147 IP_TIMESTAMP_ORDER(timestamp
,
2148 reply
->ip_timestamp
))
2149 dest_name
= MACH_PORT_DEAD
;
2151 dest_name
= MACH_PORT_NULL
;
2154 dest_name
= MACH_PORT_DEAD
;
2157 if (IP_VALID(reply
))
2158 ipc_port_release(reply
);
2160 msg
->msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
2161 MACH_MSGH_BITS(reply_type
, dest_type
));
2162 msg
->msgh_local_port
= (ipc_port_t
)dest_name
;
2163 msg
->msgh_remote_port
= (ipc_port_t
)reply_name
;
2166 return MACH_MSG_SUCCESS
;
2170 * Routine: ipc_kmsg_copyout_object
2172 * Copy-out a port right. Always returns a name,
2173 * even for unsuccessful return codes. Always
2174 * consumes the supplied object.
2178 * MACH_MSG_SUCCESS The space acquired the right
2179 * (name is valid) or the object is dead (MACH_PORT_DEAD).
2180 * MACH_MSG_IPC_SPACE No room in space for the right,
2181 * or the space is dead. (Name is MACH_PORT_NULL.)
2182 * MACH_MSG_IPC_KERNEL Kernel resource shortage.
2183 * (Name is MACH_PORT_NULL.)
2187 ipc_kmsg_copyout_object(
2189 ipc_object_t object
,
2190 mach_msg_type_name_t msgt_name
,
2191 mach_port_name_t
*namep
)
2195 if (!IO_VALID(object
)) {
2196 *namep
= (mach_port_name_t
) object
;
2197 return MACH_MSG_SUCCESS
;
2200 kr
= ipc_object_copyout(space
, object
, msgt_name
, TRUE
, namep
);
2201 if (kr
!= KERN_SUCCESS
) {
2202 ipc_object_destroy(object
, msgt_name
);
2204 if (kr
== KERN_INVALID_CAPABILITY
)
2205 *namep
= MACH_PORT_DEAD
;
2207 *namep
= MACH_PORT_NULL
;
2209 if (kr
== KERN_RESOURCE_SHORTAGE
)
2210 return MACH_MSG_IPC_KERNEL
;
2212 return MACH_MSG_IPC_SPACE
;
2216 return MACH_MSG_SUCCESS
;
2220 * Routine: ipc_kmsg_copyout_body
2222 * "Copy-out" port rights and out-of-line memory
2223 * in the body of a message.
2225 * The error codes are a combination of special bits.
2226 * The copyout proceeds despite errors.
2230 * MACH_MSG_SUCCESS Successful copyout.
2231 * MACH_MSG_IPC_SPACE No room for port right in name space.
2232 * MACH_MSG_VM_SPACE No room for memory in address space.
2233 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
2234 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
2235 * MACH_MSG_INVALID_RT_DESCRIPTOR Descriptor incompatible with RT
2239 ipc_kmsg_copyout_body(
2243 mach_msg_body_t
*slist
)
2245 mach_msg_body_t
*body
;
2246 mach_msg_descriptor_t
*saddr
, *eaddr
;
2247 mach_msg_return_t mr
= MACH_MSG_SUCCESS
;
2250 mach_msg_descriptor_t
*sstart
, *send
;
2252 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
2253 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
2254 eaddr
= saddr
+ body
->msgh_descriptor_count
;
2257 * Do scatter list setup
2259 if (slist
!= MACH_MSG_BODY_NULL
) {
2260 sstart
= (mach_msg_descriptor_t
*) (slist
+ 1);
2261 send
= sstart
+ slist
->msgh_descriptor_count
;
2264 sstart
= MACH_MSG_DESCRIPTOR_NULL
;
2267 for ( ; saddr
< eaddr
; saddr
++ ) {
2269 switch (saddr
->type
.type
) {
2271 case MACH_MSG_PORT_DESCRIPTOR
: {
2272 mach_msg_port_descriptor_t
*dsc
;
2275 * Copyout port right carried in the message
2278 mr
|= ipc_kmsg_copyout_object(space
,
2279 (ipc_object_t
) dsc
->name
,
2281 (mach_port_name_t
*) &dsc
->name
);
2285 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
2286 case MACH_MSG_OOL_DESCRIPTOR
: {
2287 vm_offset_t rcv_addr
;
2288 vm_offset_t snd_addr
;
2289 mach_msg_ool_descriptor_t
*dsc
;
2290 mach_msg_copy_options_t copy_option
;
2292 SKIP_PORT_DESCRIPTORS(sstart
, send
);
2294 dsc
= &saddr
->out_of_line
;
2296 assert(dsc
->copy
!= MACH_MSG_KALLOC_COPY_T
);
2298 copy_option
= dsc
->copy
;
2300 if ((snd_addr
= (vm_offset_t
) dsc
->address
) != 0) {
2301 if (sstart
!= MACH_MSG_DESCRIPTOR_NULL
&&
2302 sstart
->out_of_line
.copy
== MACH_MSG_OVERWRITE
) {
2305 * There is an overwrite descriptor specified in the
2306 * scatter list for this ool data. The descriptor
2307 * has already been verified
2309 rcv_addr
= (vm_offset_t
) sstart
->out_of_line
.address
;
2310 dsc
->copy
= MACH_MSG_OVERWRITE
;
2312 dsc
->copy
= MACH_MSG_ALLOCATE
;
2316 * Whether the data was virtually or physically
2317 * copied we have a vm_map_copy_t for it.
2318 * If there's an overwrite region specified
2319 * overwrite it, otherwise do a virtual copy out.
2321 if (dsc
->copy
== MACH_MSG_OVERWRITE
) {
2322 kr
= vm_map_copy_overwrite(map
, rcv_addr
,
2323 (vm_map_copy_t
) dsc
->address
, TRUE
);
2325 kr
= vm_map_copyout(map
, &rcv_addr
,
2326 (vm_map_copy_t
) dsc
->address
);
2328 if (kr
!= KERN_SUCCESS
) {
2329 if (kr
== KERN_RESOURCE_SHORTAGE
)
2330 mr
|= MACH_MSG_VM_KERNEL
;
2332 mr
|= MACH_MSG_VM_SPACE
;
2333 vm_map_copy_discard((vm_map_copy_t
) dsc
->address
);
2335 INCREMENT_SCATTER(sstart
);
2338 dsc
->address
= (void *) rcv_addr
;
2340 INCREMENT_SCATTER(sstart
);
2343 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
2345 mach_port_name_t
*objects
;
2346 mach_msg_type_number_t j
;
2348 mach_msg_ool_ports_descriptor_t
*dsc
;
2350 SKIP_PORT_DESCRIPTORS(sstart
, send
);
2352 dsc
= &saddr
->ool_ports
;
2354 length
= dsc
->count
* sizeof(mach_port_name_t
);
2357 if (sstart
!= MACH_MSG_DESCRIPTOR_NULL
&&
2358 sstart
->ool_ports
.copy
== MACH_MSG_OVERWRITE
) {
2361 * There is an overwrite descriptor specified in the
2362 * scatter list for this ool data. The descriptor
2363 * has already been verified
2365 addr
= (vm_offset_t
) sstart
->out_of_line
.address
;
2366 dsc
->copy
= MACH_MSG_OVERWRITE
;
2371 * Dynamically allocate the region
2373 int anywhere
= VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|
2376 dsc
->copy
= MACH_MSG_ALLOCATE
;
2377 if ((kr
= vm_allocate(map
, &addr
, length
,
2378 anywhere
)) != KERN_SUCCESS
) {
2379 ipc_kmsg_clean_body(kmsg
,
2380 body
->msgh_descriptor_count
);
2383 if (kr
== KERN_RESOURCE_SHORTAGE
){
2384 mr
|= MACH_MSG_VM_KERNEL
;
2386 mr
|= MACH_MSG_VM_SPACE
;
2388 INCREMENT_SCATTER(sstart
);
2393 INCREMENT_SCATTER(sstart
);
2398 objects
= (mach_port_name_t
*) dsc
->address
;
2400 /* copyout port rights carried in the message */
2402 for ( j
= 0; j
< dsc
->count
; j
++) {
2403 ipc_object_t object
=
2404 (ipc_object_t
) objects
[j
];
2406 mr
|= ipc_kmsg_copyout_object(space
, object
,
2407 dsc
->disposition
, &objects
[j
]);
2410 /* copyout to memory allocated above */
2412 data
= (vm_offset_t
) dsc
->address
;
2413 (void) copyoutmap(map
, data
, addr
, length
);
2414 kfree(data
, length
);
2416 dsc
->address
= (void *) addr
;
2417 INCREMENT_SCATTER(sstart
);
2421 panic("untyped IPC copyout body: invalid message descriptor");
2429 * Routine: ipc_kmsg_copyout
2431 * "Copy-out" port rights and out-of-line memory
2436 * MACH_MSG_SUCCESS Copied out all rights and memory.
2437 * MACH_RCV_INVALID_NOTIFY Bad notify port.
2438 * Rights and memory in the message are intact.
2439 * MACH_RCV_HEADER_ERROR + special bits
2440 * Rights and memory in the message are intact.
2441 * MACH_RCV_BODY_ERROR + special bits
2442 * The message header was successfully copied out.
2443 * As much of the body was handled as possible.
2451 mach_port_name_t notify
,
2452 mach_msg_body_t
*slist
)
2454 mach_msg_return_t mr
;
2456 mr
= ipc_kmsg_copyout_header(&kmsg
->ikm_header
, space
, notify
);
2457 if (mr
!= MACH_MSG_SUCCESS
)
2460 if (kmsg
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
2461 mr
= ipc_kmsg_copyout_body(kmsg
, space
, map
, slist
);
2463 if (mr
!= MACH_MSG_SUCCESS
)
2464 mr
|= MACH_RCV_BODY_ERROR
;
2471 * Routine: ipc_kmsg_copyout_pseudo
2473 * Does a pseudo-copyout of the message.
2474 * This is like a regular copyout, except
2475 * that the ports in the header are handled
2476 * as if they are in the body. They aren't reversed.
2478 * The error codes are a combination of special bits.
2479 * The copyout proceeds despite errors.
2483 * MACH_MSG_SUCCESS Successful copyout.
2484 * MACH_MSG_IPC_SPACE No room for port right in name space.
2485 * MACH_MSG_VM_SPACE No room for memory in address space.
2486 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
2487 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
2491 ipc_kmsg_copyout_pseudo(
2495 mach_msg_body_t
*slist
)
2497 mach_msg_bits_t mbits
= kmsg
->ikm_header
.msgh_bits
;
2498 ipc_object_t dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
2499 ipc_object_t reply
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
2500 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
2501 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
2502 mach_port_name_t dest_name
, reply_name
;
2503 mach_msg_return_t mr
;
2505 assert(IO_VALID(dest
));
2507 mr
= (ipc_kmsg_copyout_object(space
, dest
, dest_type
, &dest_name
) |
2508 ipc_kmsg_copyout_object(space
, reply
, reply_type
, &reply_name
));
2510 kmsg
->ikm_header
.msgh_bits
= mbits
&~ MACH_MSGH_BITS_CIRCULAR
;
2511 kmsg
->ikm_header
.msgh_remote_port
= (ipc_port_t
)dest_name
;
2512 kmsg
->ikm_header
.msgh_local_port
= (ipc_port_t
)reply_name
;
2514 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2515 mr
|= ipc_kmsg_copyout_body(kmsg
, space
, map
, slist
);
2522 * Routine: ipc_kmsg_copyout_dest
2524 * Copies out the destination port in the message.
2525 * Destroys all other rights and memory in the message.
2531 ipc_kmsg_copyout_dest(
2535 mach_msg_bits_t mbits
;
2538 mach_msg_type_name_t dest_type
;
2539 mach_msg_type_name_t reply_type
;
2540 mach_port_name_t dest_name
, reply_name
;
2542 mbits
= kmsg
->ikm_header
.msgh_bits
;
2543 dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
2544 reply
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
2545 dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
2546 reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
2548 assert(IO_VALID(dest
));
2551 if (io_active(dest
)) {
2552 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
2553 /* dest is unlocked */
2556 io_check_unlock(dest
);
2557 dest_name
= MACH_PORT_DEAD
;
2560 if (IO_VALID(reply
)) {
2561 ipc_object_destroy(reply
, reply_type
);
2562 reply_name
= MACH_PORT_NULL
;
2564 reply_name
= (mach_port_name_t
) reply
;
2566 kmsg
->ikm_header
.msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
2567 MACH_MSGH_BITS(reply_type
, dest_type
));
2568 kmsg
->ikm_header
.msgh_local_port
= (ipc_port_t
)dest_name
;
2569 kmsg
->ikm_header
.msgh_remote_port
= (ipc_port_t
)reply_name
;
2571 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2572 mach_msg_body_t
*body
;
2574 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
2575 ipc_kmsg_clean_body(kmsg
, body
->msgh_descriptor_count
);
2579 * Routine: ipc_kmsg_copyin_scatter
2581 * allocate and copyin a scatter list
2583 * The gather (kmsg) is valid since it has been copied in.
2584 * Gather list descriptors are sequentially paired with scatter
2585 * list descriptors, with port descriptors in either list ignored.
2586 * Descriptors are consistent if the type fileds match and size
2587 * of the scatter descriptor is less than or equal to the
2588 * size of the gather descriptor. A MACH_MSG_ALLOCATE copy
2589 * strategy in a scatter descriptor matches any size in the
2590 * corresponding gather descriptor assuming they are the same type.
2591 * Either list may be larger than the other. During the
2592 * subsequent copy out, excess scatter descriptors are ignored
2593 * and excess gather descriptors default to dynamic allocation.
2595 * In the case of a size error, the scatter list is released.
2599 * the allocated message body containing the scatter list.
2603 ipc_kmsg_copyin_scatter(
2604 mach_msg_header_t
*msg
,
2605 mach_msg_size_t slist_size
,
2608 mach_msg_body_t
*slist
;
2609 mach_msg_body_t
*body
;
2610 mach_msg_descriptor_t
*gstart
, *gend
;
2611 mach_msg_descriptor_t
*sstart
, *send
;
2614 if (slist_size
< sizeof(mach_msg_base_t
))
2615 return MACH_MSG_BODY_NULL
;
2617 slist_size
-= sizeof(mach_msg_header_t
);
2618 slist
= (mach_msg_body_t
*)kalloc(slist_size
);
2619 if (slist
== MACH_MSG_BODY_NULL
)
2622 if (copyin((char *) (msg
+ 1), (char *)slist
, slist_size
)) {
2623 kfree((vm_offset_t
)slist
, slist_size
);
2624 return MACH_MSG_BODY_NULL
;
2627 if ((slist
->msgh_descriptor_count
* sizeof(mach_msg_descriptor_t
)
2628 + sizeof(mach_msg_size_t
)) > slist_size
) {
2629 kfree((vm_offset_t
)slist
, slist_size
);
2630 return MACH_MSG_BODY_NULL
;
2633 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
2634 gstart
= (mach_msg_descriptor_t
*) (body
+ 1);
2635 gend
= gstart
+ body
->msgh_descriptor_count
;
2637 sstart
= (mach_msg_descriptor_t
*) (slist
+ 1);
2638 send
= sstart
+ slist
->msgh_descriptor_count
;
2640 while (gstart
< gend
) {
2641 mach_msg_descriptor_type_t g_type
;
2644 * Skip port descriptors in gather list.
2646 g_type
= gstart
->type
.type
;
2648 if (g_type
!= MACH_MSG_PORT_DESCRIPTOR
) {
2651 * A scatter list with a 0 descriptor count is treated as an
2652 * automatic size mismatch.
2654 if (slist
->msgh_descriptor_count
== 0) {
2655 kfree((vm_offset_t
)slist
, slist_size
);
2656 return MACH_MSG_BODY_NULL
;
2660 * Skip port descriptors in scatter list.
2662 while (sstart
< send
) {
2663 if (sstart
->type
.type
!= MACH_MSG_PORT_DESCRIPTOR
)
2669 * No more scatter descriptors, we're done
2671 if (sstart
>= send
) {
2676 * Check type, copy and size fields
2678 if (g_type
== MACH_MSG_OOL_DESCRIPTOR
||
2679 g_type
== MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
2680 if (sstart
->type
.type
!= MACH_MSG_OOL_DESCRIPTOR
&&
2681 sstart
->type
.type
!= MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
2682 kfree((vm_offset_t
)slist
, slist_size
);
2683 return MACH_MSG_BODY_NULL
;
2685 if (sstart
->out_of_line
.copy
== MACH_MSG_OVERWRITE
&&
2686 gstart
->out_of_line
.size
> sstart
->out_of_line
.size
) {
2687 kfree((vm_offset_t
)slist
, slist_size
);
2688 return MACH_MSG_BODY_NULL
;
2692 if (sstart
->type
.type
!= MACH_MSG_OOL_PORTS_DESCRIPTOR
) {
2693 kfree((vm_offset_t
)slist
, slist_size
);
2694 return MACH_MSG_BODY_NULL
;
2696 if (sstart
->ool_ports
.copy
== MACH_MSG_OVERWRITE
&&
2697 gstart
->ool_ports
.count
> sstart
->ool_ports
.count
) {
2698 kfree((vm_offset_t
)slist
, slist_size
);
2699 return MACH_MSG_BODY_NULL
;
2711 * Routine: ipc_kmsg_free_scatter
2713 * Deallocate a scatter list. Since we actually allocated
2714 * a body without a header, and since the header was originally
2715 * accounted for in slist_size, we have to ajust it down
2716 * before freeing the scatter list.
2719 ipc_kmsg_free_scatter(
2720 mach_msg_body_t
*slist
,
2721 mach_msg_size_t slist_size
)
2723 slist_size
-= sizeof(mach_msg_header_t
);
2724 kfree((vm_offset_t
)slist
, slist_size
);
2729 * Routine: ipc_kmsg_copyout_to_kernel
2731 * Copies out the destination and reply ports in the message.
2732 * Leaves all other rights and memory in the message alone.
2736 * Derived from ipc_kmsg_copyout_dest.
2737 * Use by mach_msg_rpc_from_kernel (which used to use copyout_dest).
2738 * We really do want to save rights and memory.
2742 ipc_kmsg_copyout_to_kernel(
2748 mach_msg_type_name_t dest_type
;
2749 mach_msg_type_name_t reply_type
;
2750 mach_port_name_t dest_name
, reply_name
;
2752 dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
2753 reply
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
2754 dest_type
= MACH_MSGH_BITS_REMOTE(kmsg
->ikm_header
.msgh_bits
);
2755 reply_type
= MACH_MSGH_BITS_LOCAL(kmsg
->ikm_header
.msgh_bits
);
2757 assert(IO_VALID(dest
));
2760 if (io_active(dest
)) {
2761 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
2762 /* dest is unlocked */
2765 io_check_unlock(dest
);
2766 dest_name
= MACH_PORT_DEAD
;
2769 reply_name
= (mach_port_name_t
) reply
;
2771 kmsg
->ikm_header
.msgh_bits
=
2772 (MACH_MSGH_BITS_OTHER(kmsg
->ikm_header
.msgh_bits
) |
2773 MACH_MSGH_BITS(reply_type
, dest_type
));
2774 kmsg
->ikm_header
.msgh_local_port
= (ipc_port_t
)dest_name
;
2775 kmsg
->ikm_header
.msgh_remote_port
= (ipc_port_t
)reply_name
;
2778 #include <mach_kdb.h>
2781 #include <ddb/db_output.h>
2782 #include <ipc/ipc_print.h>
2784 * Forward declarations
2786 void ipc_msg_print_untyped(
2787 mach_msg_body_t
*body
);
2789 char * ipc_type_name(
2791 boolean_t received
);
2793 void ipc_print_type_name(
2798 mach_msg_bits_t bit
);
2801 mm_copy_options_string(
2802 mach_msg_copy_options_t option
);
2804 void db_print_msg_uid(mach_msg_header_t
*);
2812 switch (type_name
) {
2813 case MACH_MSG_TYPE_PORT_NAME
:
2816 case MACH_MSG_TYPE_MOVE_RECEIVE
:
2818 return "port_receive";
2820 return "move_receive";
2823 case MACH_MSG_TYPE_MOVE_SEND
:
2830 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
2832 return "port_send_once";
2834 return "move_send_once";
2837 case MACH_MSG_TYPE_COPY_SEND
:
2840 case MACH_MSG_TYPE_MAKE_SEND
:
2843 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
2844 return "make_send_once";
2852 ipc_print_type_name(
2855 char *name
= ipc_type_name(type_name
, TRUE
);
2859 printf("type%d", type_name
);
2864 * ipc_kmsg_print [ debug ]
2870 iprintf("kmsg=0x%x\n", kmsg
);
2871 iprintf("ikm_next=0x%x, prev=0x%x, size=%d",
2876 ipc_msg_print(&kmsg
->ikm_header
);
2881 mach_msg_bits_t bit
)
2884 case MACH_MSGH_BITS_COMPLEX
: return "complex";
2885 case MACH_MSGH_BITS_CIRCULAR
: return "circular";
2886 default: return (char *) 0;
2891 * ipc_msg_print [ debug ]
2895 mach_msg_header_t
*msgh
)
2897 mach_msg_bits_t mbits
;
2898 unsigned int bit
, i
;
2902 mbits
= msgh
->msgh_bits
;
2903 iprintf("msgh_bits=0x%x: l=0x%x,r=0x%x\n",
2905 MACH_MSGH_BITS_LOCAL(msgh
->msgh_bits
),
2906 MACH_MSGH_BITS_REMOTE(msgh
->msgh_bits
));
2908 mbits
= MACH_MSGH_BITS_OTHER(mbits
) & MACH_MSGH_BITS_USED
;
2911 iprintf("decoded bits: ");
2913 for (i
= 0, bit
= 1; i
< sizeof(mbits
) * 8; ++i
, bit
<<= 1) {
2914 if ((mbits
& bit
) == 0)
2916 bit_name
= msgh_bit_decode((mach_msg_bits_t
)bit
);
2918 printf("%s%s", needs_comma
? "," : "", bit_name
);
2920 printf("%sunknown(0x%x),", needs_comma
? "," : "", bit
);
2923 if (msgh
->msgh_bits
& ~MACH_MSGH_BITS_USED
) {
2924 printf("%sunused=0x%x,", needs_comma
? "," : "",
2925 msgh
->msgh_bits
& ~MACH_MSGH_BITS_USED
);
2931 if (msgh
->msgh_remote_port
) {
2932 iprintf("remote=0x%x(", msgh
->msgh_remote_port
);
2933 ipc_print_type_name(MACH_MSGH_BITS_REMOTE(msgh
->msgh_bits
));
2936 iprintf("remote=null");
2939 if (msgh
->msgh_local_port
) {
2940 printf("%slocal=0x%x(", needs_comma
? "," : "",
2941 msgh
->msgh_local_port
);
2942 ipc_print_type_name(MACH_MSGH_BITS_LOCAL(msgh
->msgh_bits
));
2945 printf("local=null\n");
2948 iprintf("msgh_id=%d, size=%d\n",
2952 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2953 ipc_msg_print_untyped((mach_msg_body_t
*) (msgh
+ 1));
2959 mm_copy_options_string(
2960 mach_msg_copy_options_t option
)
2965 case MACH_MSG_PHYSICAL_COPY
:
2968 case MACH_MSG_VIRTUAL_COPY
:
2971 case MACH_MSG_OVERWRITE
:
2974 case MACH_MSG_ALLOCATE
:
2977 case MACH_MSG_KALLOC_COPY_T
:
2978 name
= "KALLOC_COPY_T";
2988 ipc_msg_print_untyped(
2989 mach_msg_body_t
*body
)
2991 mach_msg_descriptor_t
*saddr
, *send
;
2992 mach_msg_descriptor_type_t type
;
2994 iprintf("%d descriptors %d: \n", body
->msgh_descriptor_count
);
2996 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
2997 send
= saddr
+ body
->msgh_descriptor_count
;
2999 for ( ; saddr
< send
; saddr
++ ) {
3001 type
= saddr
->type
.type
;
3005 case MACH_MSG_PORT_DESCRIPTOR
: {
3006 mach_msg_port_descriptor_t
*dsc
;
3009 iprintf("-- PORT name = 0x%x disp = ", dsc
->name
);
3010 ipc_print_type_name(dsc
->disposition
);
3014 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
3015 case MACH_MSG_OOL_DESCRIPTOR
: {
3016 mach_msg_ool_descriptor_t
*dsc
;
3018 dsc
= &saddr
->out_of_line
;
3019 iprintf("-- OOL%s addr = 0x%x size = 0x%x copy = %s %s\n",
3020 type
== MACH_MSG_OOL_DESCRIPTOR
? "" : " VOLATILE",
3021 dsc
->address
, dsc
->size
,
3022 mm_copy_options_string(dsc
->copy
),
3023 dsc
->deallocate
? "DEALLOC" : "");
3026 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
3027 mach_msg_ool_ports_descriptor_t
*dsc
;
3029 dsc
= &saddr
->ool_ports
;
3031 iprintf("-- OOL_PORTS addr = 0x%x count = 0x%x ",
3032 dsc
->address
, dsc
->count
);
3034 ipc_print_type_name(dsc
->disposition
);
3035 printf(" copy = %s %s\n",
3036 mm_copy_options_string(dsc
->copy
),
3037 dsc
->deallocate
? "DEALLOC" : "");
3042 iprintf("-- UNKNOWN DESCRIPTOR 0x%x\n", type
);
3048 #endif /* MACH_KDB */