2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * File: ipc/ipc_kmsg.c
60 * Operations on kernel messages.
66 #include <mach/boolean.h>
67 #include <mach/kern_return.h>
68 #include <mach/message.h>
69 #include <mach/port.h>
70 #include <mach/vm_statistics.h>
71 #include <kern/assert.h>
72 #include <kern/kalloc.h>
73 #include <kern/thread.h>
74 #include <kern/sched_prim.h>
76 #include <kern/misc_protos.h>
77 #include <kern/counters.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_kern.h>
82 #include <ipc/ipc_entry.h>
83 #include <ipc/ipc_kmsg.h>
84 #include <ipc/ipc_notify.h>
85 #include <ipc/ipc_object.h>
86 #include <ipc/ipc_space.h>
87 #include <ipc/ipc_port.h>
88 #include <ipc/ipc_right.h>
89 #include <ipc/ipc_hash.h>
90 #include <ipc/ipc_table.h>
95 #include <ppc/Firmware.h>
96 #include <ppc/low_trace.h>
100 extern vm_map_t ipc_kernel_copy_map
;
101 extern vm_size_t ipc_kmsg_max_vm_space
;
102 extern vm_size_t msg_ool_size_small
;
104 #define MSG_OOL_SIZE_SMALL msg_ool_size_small
108 * Forward declarations
114 void ipc_kmsg_clean_body(
116 mach_msg_type_number_t number
);
118 void ipc_kmsg_clean_partial(
120 mach_msg_type_number_t number
,
124 mach_msg_return_t
ipc_kmsg_copyout_body(
128 mach_msg_body_t
*slist
);
130 mach_msg_return_t
ipc_kmsg_copyin_body(
135 void ikm_cache_init(void);
137 * We keep a per-processor cache of kernel message buffers.
138 * The cache saves the overhead/locking of using kalloc/kfree.
139 * The per-processor cache seems to miss less than a per-thread cache,
140 * and it also uses less memory. Access to the cache doesn't
143 #define IKM_STASH 16 /* # of cache entries per cpu */
144 ipc_kmsg_t ipc_kmsg_cache
[ NCPUS
][ IKM_STASH
];
145 unsigned int ipc_kmsg_cache_avail
[NCPUS
];
148 * Routine: ipc_kmsg_init
150 * Initialize the kmsg system. For each CPU, we need to
151 * pre-stuff the kmsg cache.
158 for (cpu
= 0; cpu
< NCPUS
; ++cpu
) {
159 for (i
= 0; i
< IKM_STASH
; ++i
) {
163 kalloc(ikm_plus_overhead(IKM_SAVED_MSG_SIZE
));
164 if (kmsg
== IKM_NULL
)
165 panic("ipc_kmsg_init");
166 ikm_init(kmsg
, IKM_SAVED_MSG_SIZE
);
167 ipc_kmsg_cache
[cpu
][i
] = kmsg
;
169 ipc_kmsg_cache_avail
[cpu
] = IKM_STASH
;
174 * Routine: ipc_kmsg_alloc
176 * Allocate a kernel message structure. If we can get one from
177 * the cache, that is best. Otherwise, allocate a new one.
183 mach_msg_size_t msg_and_trailer_size
)
187 if ((msg_and_trailer_size
<= IKM_SAVED_MSG_SIZE
)) {
190 disable_preemption();
192 if ((i
= ipc_kmsg_cache_avail
[cpu
]) > 0) {
193 assert(i
<= IKM_STASH
);
194 kmsg
= ipc_kmsg_cache
[cpu
][--i
];
195 ipc_kmsg_cache_avail
[cpu
] = i
;
196 ikm_check_init(kmsg
, IKM_SAVED_MSG_SIZE
);
203 /* round up for ikm_cache */
204 if (msg_and_trailer_size
< IKM_SAVED_MSG_SIZE
)
205 msg_and_trailer_size
= IKM_SAVED_MSG_SIZE
;
207 kmsg
= (ipc_kmsg_t
)kalloc(ikm_plus_overhead(msg_and_trailer_size
));
208 if (kmsg
!= IKM_NULL
) {
209 ikm_init(kmsg
, msg_and_trailer_size
);
215 * Routine: ipc_kmsg_free
217 * Free a kernel message buffer. If the kms is preallocated
218 * to a port, just "put it back (marked unused)." We have to
219 * do this with the port locked. The port may have its hold
220 * on our message released. In that case, we have to just
221 * revert the message to a traditional one and free it normally.
230 mach_msg_size_t size
= kmsg
->ikm_size
;
234 * Check to see if the message is bound to the port. If so,
235 * mark it not in use. If the port isn't already dead, then
236 * leave the message associated with it. Otherwise, free it
237 * (not to the cache).
239 port
= ikm_prealloc_inuse_port(kmsg
);
240 if (port
!= IP_NULL
) {
242 ikm_prealloc_clear_inuse(kmsg
, port
);
243 if (ip_active(port
) && (port
->ip_premsg
== kmsg
)) {
244 assert(IP_PREALLOC(port
));
248 ip_check_unlock(port
); /* May be last reference */
253 * Peek and see if it has to go back in the cache.
255 if (kmsg
->ikm_size
== IKM_SAVED_MSG_SIZE
&&
256 ipc_kmsg_cache_avail
[cpu_number()] < IKM_STASH
) {
259 disable_preemption();
262 i
= ipc_kmsg_cache_avail
[cpu
];
265 ipc_kmsg_cache
[cpu
][i
] = kmsg
;
266 ipc_kmsg_cache_avail
[cpu
] = i
+ 1;
274 kfree((vm_offset_t
) kmsg
, ikm_plus_overhead(size
));
279 * Routine: ipc_kmsg_enqueue
286 ipc_kmsg_queue_t queue
,
289 ipc_kmsg_enqueue_macro(queue
, kmsg
);
293 * Routine: ipc_kmsg_dequeue
295 * Dequeue and return a kmsg.
300 ipc_kmsg_queue_t queue
)
304 first
= ipc_kmsg_queue_first(queue
);
306 if (first
!= IKM_NULL
)
307 ipc_kmsg_rmqueue_first_macro(queue
, first
);
313 * Routine: ipc_kmsg_rmqueue
315 * Pull a kmsg out of a queue.
320 ipc_kmsg_queue_t queue
,
323 ipc_kmsg_t next
, prev
;
325 assert(queue
->ikmq_base
!= IKM_NULL
);
327 next
= kmsg
->ikm_next
;
328 prev
= kmsg
->ikm_prev
;
331 assert(prev
== kmsg
);
332 assert(queue
->ikmq_base
== kmsg
);
334 queue
->ikmq_base
= IKM_NULL
;
336 if (queue
->ikmq_base
== kmsg
)
337 queue
->ikmq_base
= next
;
339 next
->ikm_prev
= prev
;
340 prev
->ikm_next
= next
;
342 /* XXX Temporary debug logic */
343 assert(kmsg
->ikm_next
= IKM_BOGUS
);
344 assert(kmsg
->ikm_prev
= IKM_BOGUS
);
348 * Routine: ipc_kmsg_queue_next
350 * Return the kmsg following the given kmsg.
351 * (Or IKM_NULL if it is the last one in the queue.)
356 ipc_kmsg_queue_t queue
,
361 assert(queue
->ikmq_base
!= IKM_NULL
);
363 next
= kmsg
->ikm_next
;
364 if (queue
->ikmq_base
== next
)
371 * Routine: ipc_kmsg_destroy
373 * Destroys a kernel message. Releases all rights,
374 * references, and memory held by the message.
384 ipc_kmsg_queue_t queue
;
388 * ipc_kmsg_clean can cause more messages to be destroyed.
389 * Curtail recursion by queueing messages. If a message
390 * is already queued, then this is a recursive call.
393 queue
= &(current_thread()->ith_messages
);
394 empty
= ipc_kmsg_queue_empty(queue
);
395 ipc_kmsg_enqueue(queue
, kmsg
);
398 /* must leave kmsg in queue while cleaning it */
400 while ((kmsg
= ipc_kmsg_queue_first(queue
)) != IKM_NULL
) {
401 ipc_kmsg_clean(kmsg
);
402 ipc_kmsg_rmqueue(queue
, kmsg
);
409 * Routine: ipc_kmsg_destroy_dest
411 * Destroys a kernel message. Releases all rights,
412 * references, and memory held by the message (including
413 * the destination port reference.
419 ipc_kmsg_destroy_dest(
424 port
= kmsg
->ikm_header
.msgh_remote_port
;
426 ipc_port_release(port
);
427 kmsg
->ikm_header
.msgh_remote_port
= MACH_PORT_NULL
;
428 ipc_kmsg_destroy(kmsg
);
432 * Routine: ipc_kmsg_clean_body
434 * Cleans the body of a kernel message.
435 * Releases all rights, references, and memory.
444 mach_msg_type_number_t number
)
446 mach_msg_descriptor_t
*saddr
, *eaddr
;
451 saddr
= (mach_msg_descriptor_t
*)
452 ((mach_msg_base_t
*) &kmsg
->ikm_header
+ 1);
453 eaddr
= saddr
+ number
;
455 for ( ; saddr
< eaddr
; saddr
++ ) {
457 switch (saddr
->type
.type
) {
459 case MACH_MSG_PORT_DESCRIPTOR
: {
460 mach_msg_port_descriptor_t
*dsc
;
465 * Destroy port rights carried in the message
467 if (!IO_VALID((ipc_object_t
) dsc
->name
))
469 ipc_object_destroy((ipc_object_t
) dsc
->name
, dsc
->disposition
);
472 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
473 case MACH_MSG_OOL_DESCRIPTOR
: {
474 mach_msg_ool_descriptor_t
*dsc
;
476 dsc
= &saddr
->out_of_line
;
479 * Destroy memory carried in the message
481 if (dsc
->size
== 0) {
482 assert(dsc
->address
== (void *) 0);
484 vm_map_copy_discard((vm_map_copy_t
) dsc
->address
);
488 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
489 ipc_object_t
*objects
;
490 mach_msg_type_number_t j
;
491 mach_msg_ool_ports_descriptor_t
*dsc
;
493 dsc
= &saddr
->ool_ports
;
494 objects
= (ipc_object_t
*) dsc
->address
;
496 if (dsc
->count
== 0) {
500 assert(objects
!= (ipc_object_t
*) 0);
502 /* destroy port rights carried in the message */
504 for (j
= 0; j
< dsc
->count
; j
++) {
505 ipc_object_t object
= objects
[j
];
507 if (!IO_VALID(object
))
510 ipc_object_destroy(object
, dsc
->disposition
);
513 /* destroy memory carried in the message */
515 assert(dsc
->count
!= 0);
517 kfree((vm_offset_t
) dsc
->address
,
518 (vm_size_t
) dsc
->count
* sizeof(mach_port_name_t
));
522 printf("cleanup: don't understand this type of descriptor\n");
529 * Routine: ipc_kmsg_clean_partial
531 * Cleans a partially-acquired kernel message.
532 * number is the index of the type descriptor
533 * in the body of the message that contained the error.
534 * If dolast, the memory and port rights in this last
535 * type spec are also cleaned. In that case, number
536 * specifies the number of port rights to clean.
542 ipc_kmsg_clean_partial(
544 mach_msg_type_number_t number
,
549 mach_msg_bits_t mbits
= kmsg
->ikm_header
.msgh_bits
;
551 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
552 assert(IO_VALID(object
));
553 ipc_object_destroy(object
, MACH_MSGH_BITS_REMOTE(mbits
));
555 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
556 if (IO_VALID(object
))
557 ipc_object_destroy(object
, MACH_MSGH_BITS_LOCAL(mbits
));
560 (void) vm_deallocate(ipc_kernel_copy_map
, paddr
, length
);
563 ipc_kmsg_clean_body(kmsg
, number
);
567 * Routine: ipc_kmsg_clean
569 * Cleans a kernel message. Releases all rights,
570 * references, and memory held by the message.
580 mach_msg_bits_t mbits
;
582 mbits
= kmsg
->ikm_header
.msgh_bits
;
583 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
584 if (IO_VALID(object
))
585 ipc_object_destroy(object
, MACH_MSGH_BITS_REMOTE(mbits
));
587 object
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
588 if (IO_VALID(object
))
589 ipc_object_destroy(object
, MACH_MSGH_BITS_LOCAL(mbits
));
591 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
592 mach_msg_body_t
*body
;
594 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
595 ipc_kmsg_clean_body(kmsg
, body
->msgh_descriptor_count
);
600 * Routine: ipc_kmsg_set_prealloc
602 * Assign a kmsg as a preallocated message buffer to a port.
608 ipc_kmsg_set_prealloc(
612 assert(kmsg
->ikm_prealloc
== IP_NULL
);
614 kmsg
->ikm_prealloc
= IP_NULL
;
615 IP_SET_PREALLOC(port
, kmsg
);
619 * Routine: ipc_kmsg_clear_prealloc
621 * Release the Assignment of a preallocated message buffer from a port.
626 ipc_kmsg_clear_prealloc(
630 assert(kmsg
->ikm_prealloc
== port
);
632 kmsg
->ikm_prealloc
= IP_NULL
;
633 IP_CLEAR_PREALLOC(port
, kmsg
);
637 * Routine: ipc_kmsg_get
639 * Allocates a kernel message buffer.
640 * Copies a user message to the message buffer.
644 * MACH_MSG_SUCCESS Acquired a message buffer.
645 * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
646 * MACH_SEND_MSG_TOO_SMALL Message size not long-word multiple.
647 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
648 * MACH_SEND_INVALID_DATA Couldn't copy message data.
653 mach_msg_header_t
*msg
,
654 mach_msg_size_t size
,
657 mach_msg_size_t msg_and_trailer_size
;
659 mach_msg_format_0_trailer_t
*trailer
;
660 mach_port_name_t dest_name
;
661 ipc_entry_t dest_entry
;
662 ipc_port_t dest_port
;
664 if ((size
< sizeof(mach_msg_header_t
)) || (size
& 3))
665 return MACH_SEND_MSG_TOO_SMALL
;
667 msg_and_trailer_size
= size
+ MAX_TRAILER_SIZE
;
669 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
671 if (kmsg
== IKM_NULL
)
672 return MACH_SEND_NO_BUFFER
;
674 if (copyinmsg((char *) msg
, (char *) &kmsg
->ikm_header
, size
)) {
676 return MACH_SEND_INVALID_DATA
;
679 kmsg
->ikm_header
.msgh_size
= size
;
682 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
683 * However, the internal size field of the trailer (msgh_trailer_size)
684 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
685 * the cases where no implicit data is requested.
687 trailer
= (mach_msg_format_0_trailer_t
*) ((vm_offset_t
)&kmsg
->ikm_header
+ size
);
688 trailer
->msgh_sender
= current_thread()->top_act
->task
->sec_token
;
689 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
690 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
693 if(trcWork
.traceMask
) dbgTrace((unsigned int)kmsg
->ikm_header
.msgh_id
,
694 (unsigned int)kmsg
->ikm_header
.msgh_remote_port
,
695 (unsigned int)kmsg
->ikm_header
.msgh_local_port
, 0);
698 return MACH_MSG_SUCCESS
;
702 * Routine: ipc_kmsg_get_from_kernel
704 * Allocates a kernel message buffer.
705 * Copies a kernel message to the message buffer.
706 * Only resource errors are allowed.
709 * Ports in header are ipc_port_t.
711 * MACH_MSG_SUCCESS Acquired a message buffer.
712 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
716 ipc_kmsg_get_from_kernel(
717 mach_msg_header_t
*msg
,
718 mach_msg_size_t size
,
722 mach_msg_size_t msg_and_trailer_size
;
723 mach_msg_format_0_trailer_t
*trailer
;
724 ipc_port_t dest_port
;
726 assert(size
>= sizeof(mach_msg_header_t
));
727 assert((size
& 3) == 0);
729 assert(IP_VALID((ipc_port_t
) msg
->msgh_remote_port
));
730 dest_port
= (ipc_port_t
)msg
->msgh_remote_port
;
732 msg_and_trailer_size
= size
+ MAX_TRAILER_SIZE
;
735 * See if the port has a pre-allocated kmsg for kernel
736 * clients. These are set up for those kernel clients
737 * which cannot afford to wait.
739 if (IP_PREALLOC(dest_port
)) {
741 if (!ip_active(dest_port
)) {
742 ip_unlock(dest_port
);
743 return MACH_SEND_NO_BUFFER
;
745 assert(IP_PREALLOC(dest_port
));
746 kmsg
= dest_port
->ip_premsg
;
747 if (msg_and_trailer_size
> kmsg
->ikm_size
) {
748 ip_unlock(dest_port
);
749 return MACH_SEND_TOO_LARGE
;
751 if (ikm_prealloc_inuse(kmsg
)) {
752 ip_unlock(dest_port
);
753 return MACH_SEND_NO_BUFFER
;
755 ikm_prealloc_set_inuse(kmsg
, dest_port
);
756 ip_unlock(dest_port
);
758 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
759 if (kmsg
== IKM_NULL
)
760 return MACH_SEND_NO_BUFFER
;
763 (void) memcpy((void *) &kmsg
->ikm_header
, (const void *) msg
, size
);
765 kmsg
->ikm_header
.msgh_size
= size
;
768 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
769 * However, the internal size field of the trailer (msgh_trailer_size)
770 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to
771 * optimize the cases where no implicit data is requested.
773 trailer
= (mach_msg_format_0_trailer_t
*)
774 ((vm_offset_t
)&kmsg
->ikm_header
+ size
);
775 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
776 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
777 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
780 return MACH_MSG_SUCCESS
;
784 * Routine: ipc_kmsg_send
786 * Send a message. The message holds a reference
787 * for the destination port in the msgh_remote_port field.
789 * If unsuccessful, the caller still has possession of
790 * the message and must do something with it. If successful,
791 * the message is queued, given to a receiver, destroyed,
792 * or handled directly by the kernel via mach_msg.
796 * MACH_MSG_SUCCESS The message was accepted.
797 * MACH_SEND_TIMED_OUT Caller still has message.
798 * MACH_SEND_INTERRUPTED Caller still has message.
803 mach_msg_option_t option
,
804 mach_msg_timeout_t timeout
)
806 kern_return_t save_wait_result
;
809 port
= (ipc_port_t
) kmsg
->ikm_header
.msgh_remote_port
;
810 assert(IP_VALID(port
));
814 if (port
->ip_receiver
== ipc_space_kernel
) {
817 * We can check ip_receiver == ipc_space_kernel
818 * before checking that the port is active because
819 * ipc_port_dealloc_kernel clears ip_receiver
820 * before destroying a kernel port.
822 assert(ip_active(port
));
823 port
->ip_messages
.imq_seqno
++;
826 current_task()->messages_sent
++;
829 * Call the server routine, and get the reply message to send.
831 kmsg
= ipc_kobject_server(kmsg
);
832 if (kmsg
== IKM_NULL
)
833 return MACH_MSG_SUCCESS
;
835 port
= (ipc_port_t
) kmsg
->ikm_header
.msgh_remote_port
;
836 assert(IP_VALID(port
));
838 /* fall thru with reply - same options */
842 * Can't deliver to a dead port.
843 * However, we can pretend it got sent
844 * and was then immediately destroyed.
846 if (!ip_active(port
)) {
848 * We can't let ipc_kmsg_destroy deallocate
849 * the port right, because we might end up
850 * in an infinite loop trying to deliver
851 * a send-once notification.
855 ip_check_unlock(port
);
856 kmsg
->ikm_header
.msgh_remote_port
= MACH_PORT_NULL
;
857 ipc_kmsg_destroy(kmsg
);
858 return MACH_MSG_SUCCESS
;
861 if (kmsg
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_CIRCULAR
) {
864 /* don't allow the creation of a circular loop */
866 ipc_kmsg_destroy(kmsg
);
867 return MACH_MSG_SUCCESS
;
871 * We have a valid message and a valid reference on the port.
872 * we can unlock the port and call mqueue_send() on it's message
876 return (ipc_mqueue_send(&port
->ip_messages
, kmsg
, option
, timeout
));
880 * Routine: ipc_kmsg_put
882 * Copies a message buffer to a user message.
883 * Copies only the specified number of bytes.
884 * Frees the message buffer.
886 * Nothing locked. The message buffer must have clean
889 * MACH_MSG_SUCCESS Copied data out of message buffer.
890 * MACH_RCV_INVALID_DATA Couldn't copy to user message.
895 mach_msg_header_t
*msg
,
897 mach_msg_size_t size
)
899 mach_msg_return_t mr
;
901 if (copyoutmsg((const char *) &kmsg
->ikm_header
, (char *) msg
, size
))
902 mr
= MACH_RCV_INVALID_DATA
;
904 mr
= MACH_MSG_SUCCESS
;
911 * Routine: ipc_kmsg_put_to_kernel
913 * Copies a message buffer to a kernel message.
914 * Frees the message buffer.
921 ipc_kmsg_put_to_kernel(
922 mach_msg_header_t
*msg
,
924 mach_msg_size_t size
)
926 (void) memcpy((void *) msg
, (const void *) &kmsg
->ikm_header
, size
);
932 * Routine: ipc_kmsg_copyin_header
934 * "Copy-in" port rights in the header of a message.
935 * Operates atomically; if it doesn't succeed the
936 * message header and the space are left untouched.
937 * If it does succeed the remote/local port fields
938 * contain object pointers instead of port names,
939 * and the bits field is updated. The destination port
940 * will be a valid port pointer.
942 * The notify argument implements the MACH_SEND_CANCEL option.
943 * If it is not MACH_PORT_NULL, it should name a receive right.
944 * If the processing of the destination port would generate
945 * a port-deleted notification (because the right for the
946 * destination port is destroyed and it had a request for
947 * a dead-name notification registered), and the port-deleted
948 * notification would be sent to the named receive right,
949 * then it isn't sent and the send-once right for the notify
950 * port is quietly destroyed.
955 * MACH_MSG_SUCCESS Successful copyin.
956 * MACH_SEND_INVALID_HEADER
957 * Illegal value in the message header bits.
958 * MACH_SEND_INVALID_DEST The space is dead.
959 * MACH_SEND_INVALID_NOTIFY
960 * Notify is non-null and doesn't name a receive right.
961 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
962 * MACH_SEND_INVALID_DEST Can't copyin destination port.
963 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
964 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
965 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
969 ipc_kmsg_copyin_header(
970 mach_msg_header_t
*msg
,
972 mach_port_name_t notify
)
974 mach_msg_bits_t mbits
= msg
->msgh_bits
& MACH_MSGH_BITS_USER
;
975 mach_port_name_t dest_name
= (mach_port_name_t
)msg
->msgh_remote_port
;
976 mach_port_name_t reply_name
= (mach_port_name_t
)msg
->msgh_local_port
;
979 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
980 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
981 ipc_object_t dest_port
, reply_port
;
982 ipc_port_t dest_soright
, reply_soright
;
983 ipc_port_t notify_port
;
985 if ((mbits
!= msg
->msgh_bits
) ||
986 (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type
)) ||
988 (reply_name
!= MACH_PORT_NULL
) :
989 !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type
)))
990 return MACH_SEND_INVALID_HEADER
;
992 reply_soright
= IP_NULL
; /* in case we go to invalid dest early */
994 is_write_lock(space
);
995 if (!space
->is_active
)
998 if (!MACH_PORT_VALID(dest_name
))
1001 if (notify
!= MACH_PORT_NULL
) {
1004 if ((entry
= ipc_entry_lookup(space
, notify
)) == IE_NULL
) {
1005 is_write_unlock(space
);
1006 return MACH_SEND_INVALID_NOTIFY
;
1008 if((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) == 0) {
1009 is_write_unlock(space
);
1010 return MACH_SEND_INVALID_NOTIFY
;
1013 notify_port
= (ipc_port_t
) entry
->ie_object
;
1016 if (dest_name
== reply_name
) {
1018 mach_port_name_t name
= dest_name
;
1021 * Destination and reply ports are the same!
1022 * This is a little tedious to make atomic, because
1023 * there are 25 combinations of dest_type/reply_type.
1024 * However, most are easy. If either is move-sonce,
1025 * then there must be an error. If either are
1026 * make-send or make-sonce, then we must be looking
1027 * at a receive right so the port can't die.
1028 * The hard cases are the combinations of
1029 * copy-send and make-send.
1032 entry
= ipc_entry_lookup(space
, name
);
1033 if (entry
== IE_NULL
)
1036 assert(reply_type
!= 0); /* because name not null */
1038 if (!ipc_right_copyin_check(space
, name
, entry
, reply_type
))
1041 if ((dest_type
== MACH_MSG_TYPE_MOVE_SEND_ONCE
) ||
1042 (reply_type
== MACH_MSG_TYPE_MOVE_SEND_ONCE
)) {
1044 * Why must there be an error? To get a valid
1045 * destination, this entry must name a live
1046 * port (not a dead name or dead port). However
1047 * a successful move-sonce will destroy a
1048 * live entry. Therefore the other copyin,
1049 * whatever it is, would fail. We've already
1050 * checked for reply port errors above,
1051 * so report a destination error.
1055 } else if ((dest_type
== MACH_MSG_TYPE_MAKE_SEND
) ||
1056 (dest_type
== MACH_MSG_TYPE_MAKE_SEND_ONCE
) ||
1057 (reply_type
== MACH_MSG_TYPE_MAKE_SEND
) ||
1058 (reply_type
== MACH_MSG_TYPE_MAKE_SEND_ONCE
)) {
1059 kr
= ipc_right_copyin(space
, name
, entry
,
1061 &dest_port
, &dest_soright
);
1062 if (kr
!= KERN_SUCCESS
)
1066 * Either dest or reply needs a receive right.
1067 * We know the receive right is there, because
1068 * of the copyin_check and copyin calls. Hence
1069 * the port is not in danger of dying. If dest
1070 * used the receive right, then the right needed
1071 * by reply (and verified by copyin_check) will
1075 assert(IO_VALID(dest_port
));
1076 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
1077 assert(dest_soright
== IP_NULL
);
1079 kr
= ipc_right_copyin(space
, name
, entry
,
1081 &reply_port
, &reply_soright
);
1083 assert(kr
== KERN_SUCCESS
);
1084 assert(reply_port
== dest_port
);
1085 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
1086 assert(reply_soright
== IP_NULL
);
1087 } else if ((dest_type
== MACH_MSG_TYPE_COPY_SEND
) &&
1088 (reply_type
== MACH_MSG_TYPE_COPY_SEND
)) {
1090 * To make this atomic, just do one copy-send,
1091 * and dup the send right we get out.
1094 kr
= ipc_right_copyin(space
, name
, entry
,
1096 &dest_port
, &dest_soright
);
1097 if (kr
!= KERN_SUCCESS
)
1100 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND
);
1101 assert(dest_soright
== IP_NULL
);
1104 * It's OK if the port we got is dead now,
1105 * so reply_port is IP_DEAD, because the msg
1106 * won't go anywhere anyway.
1109 reply_port
= (ipc_object_t
)
1110 ipc_port_copy_send((ipc_port_t
) dest_port
);
1111 reply_soright
= IP_NULL
;
1112 } else if ((dest_type
== MACH_MSG_TYPE_MOVE_SEND
) &&
1113 (reply_type
== MACH_MSG_TYPE_MOVE_SEND
)) {
1115 * This is an easy case. Just use our
1116 * handy-dandy special-purpose copyin call
1117 * to get two send rights for the price of one.
1120 kr
= ipc_right_copyin_two(space
, name
, entry
,
1121 &dest_port
, &dest_soright
);
1122 if (kr
!= KERN_SUCCESS
)
1125 /* the entry might need to be deallocated */
1126 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1127 ipc_entry_dealloc(space
, name
, entry
);
1129 reply_port
= dest_port
;
1130 reply_soright
= IP_NULL
;
1134 assert(((dest_type
== MACH_MSG_TYPE_COPY_SEND
) &&
1135 (reply_type
== MACH_MSG_TYPE_MOVE_SEND
)) ||
1136 ((dest_type
== MACH_MSG_TYPE_MOVE_SEND
) &&
1137 (reply_type
== MACH_MSG_TYPE_COPY_SEND
)));
1140 * To make this atomic, just do a move-send,
1141 * and dup the send right we get out.
1144 kr
= ipc_right_copyin(space
, name
, entry
,
1145 MACH_MSG_TYPE_MOVE_SEND
, FALSE
,
1146 &dest_port
, &soright
);
1147 if (kr
!= KERN_SUCCESS
)
1150 /* the entry might need to be deallocated */
1152 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1153 ipc_entry_dealloc(space
, name
, entry
);
1156 * It's OK if the port we got is dead now,
1157 * so reply_port is IP_DEAD, because the msg
1158 * won't go anywhere anyway.
1161 reply_port
= (ipc_object_t
)
1162 ipc_port_copy_send((ipc_port_t
) dest_port
);
1164 if (dest_type
== MACH_MSG_TYPE_MOVE_SEND
) {
1165 dest_soright
= soright
;
1166 reply_soright
= IP_NULL
;
1168 dest_soright
= IP_NULL
;
1169 reply_soright
= soright
;
1172 } else if (!MACH_PORT_VALID(reply_name
)) {
1176 * No reply port! This is an easy case
1177 * to make atomic. Just copyin the destination.
1180 entry
= ipc_entry_lookup(space
, dest_name
);
1181 if (entry
== IE_NULL
)
1184 kr
= ipc_right_copyin(space
, dest_name
, entry
,
1186 &dest_port
, &dest_soright
);
1187 if (kr
!= KERN_SUCCESS
)
1190 /* the entry might need to be deallocated */
1192 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1193 ipc_entry_dealloc(space
, dest_name
, entry
);
1195 reply_port
= (ipc_object_t
) reply_name
;
1196 reply_soright
= IP_NULL
;
1198 ipc_entry_t dest_entry
, reply_entry
;
1199 ipc_port_t saved_reply
;
1202 * This is the tough case to make atomic.
1203 * The difficult problem is serializing with port death.
1204 * At the time we copyin dest_port, it must be alive.
1205 * If reply_port is alive when we copyin it, then
1206 * we are OK, because we serialize before the death
1207 * of both ports. Assume reply_port is dead at copyin.
1208 * Then if dest_port dies/died after reply_port died,
1209 * we are OK, because we serialize between the death
1210 * of the two ports. So the bad case is when dest_port
1211 * dies after its copyin, reply_port dies before its
1212 * copyin, and dest_port dies before reply_port. Then
1213 * the copyins operated as if dest_port was alive
1214 * and reply_port was dead, which shouldn't have happened
1215 * because they died in the other order.
1217 * Note that it is easy for a user task to tell if
1218 * a copyin happened before or after a port died.
1219 * For example, suppose both dest and reply are
1220 * send-once rights (types are both move-sonce) and
1221 * both rights have dead-name requests registered.
1222 * If a port dies before copyin, a dead-name notification
1223 * is generated and the dead name's urefs are incremented,
1224 * and if the copyin happens first, a port-deleted
1225 * notification is generated.
1227 * Note that although the entries are different,
1228 * dest_port and reply_port might still be the same.
1230 * JMM - The code to handle this was too expensive and, anyway,
1231 * we intend to separate the dest lookup from the reply copyin
1232 * by a wide margin, so the user will have to learn to deal!
1233 * I will be making the change soon!
1236 dest_entry
= ipc_entry_lookup(space
, dest_name
);
1237 if (dest_entry
== IE_NULL
)
1240 reply_entry
= ipc_entry_lookup(space
, reply_name
);
1241 if (reply_entry
== IE_NULL
)
1244 assert(dest_entry
!= reply_entry
); /* names are not equal */
1245 assert(reply_type
!= 0); /* because reply_name not null */
1247 if (!ipc_right_copyin_check(space
, reply_name
, reply_entry
,
1251 kr
= ipc_right_copyin(space
, dest_name
, dest_entry
,
1253 &dest_port
, &dest_soright
);
1254 if (kr
!= KERN_SUCCESS
)
1257 assert(IO_VALID(dest_port
));
1259 kr
= ipc_right_copyin(space
, reply_name
, reply_entry
,
1261 &reply_port
, &reply_soright
);
1263 assert(kr
== KERN_SUCCESS
);
1265 /* the entries might need to be deallocated */
1267 if (IE_BITS_TYPE(reply_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1268 ipc_entry_dealloc(space
, reply_name
, reply_entry
);
1270 if (IE_BITS_TYPE(dest_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1271 ipc_entry_dealloc(space
, dest_name
, dest_entry
);
1275 * At this point, dest_port, reply_port,
1276 * dest_soright, reply_soright are all initialized.
1277 * Any defunct entries have been deallocated.
1278 * The space is still write-locked, and we need to
1279 * make the MACH_SEND_CANCEL check. The notify_port pointer
1280 * is still usable, because the copyin code above won't ever
1281 * deallocate a receive right, so its entry still exists
1282 * and holds a ref. Note notify_port might even equal
1283 * dest_port or reply_port.
1286 if ((notify
!= MACH_PORT_NULL
) &&
1287 (dest_soright
== notify_port
)) {
1288 ipc_port_release_sonce(dest_soright
);
1289 dest_soright
= IP_NULL
;
1292 is_write_unlock(space
);
1294 if (dest_soright
!= IP_NULL
)
1295 ipc_notify_port_deleted(dest_soright
, dest_name
);
1297 if (reply_soright
!= IP_NULL
)
1298 ipc_notify_port_deleted(reply_soright
, reply_name
);
1300 dest_type
= ipc_object_copyin_type(dest_type
);
1301 reply_type
= ipc_object_copyin_type(reply_type
);
1303 msg
->msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
1304 MACH_MSGH_BITS(dest_type
, reply_type
));
1305 msg
->msgh_remote_port
= (ipc_port_t
)dest_port
;
1306 msg
->msgh_local_port
= (ipc_port_t
)reply_port
;
1308 return MACH_MSG_SUCCESS
;
1311 is_write_unlock(space
);
1312 return MACH_SEND_INVALID_REPLY
;
1315 is_write_unlock(space
);
1316 if (reply_soright
!= IP_NULL
)
1317 ipc_notify_port_deleted(reply_soright
, reply_name
);
1318 return MACH_SEND_INVALID_DEST
;
1322 * Routine: ipc_kmsg_copyin_body
1324 * "Copy-in" port rights and out-of-line memory
1325 * in the message body.
1327 * In all failure cases, the message is left holding
1328 * no rights or memory. However, the message buffer
1329 * is not deallocated. If successful, the message
1330 * contains a valid destination port.
1334 * MACH_MSG_SUCCESS Successful copyin.
1335 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
1336 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
1337 * MACH_SEND_INVALID_TYPE Bad type specification.
1338 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
1339 * MACH_SEND_INVALID_RT_OOL_SIZE OOL Buffer too large for RT
1340 * MACH_MSG_INVALID_RT_DESCRIPTOR Dealloc and RT are incompatible
1344 ipc_kmsg_copyin_body(
1350 mach_msg_body_t
*body
;
1351 mach_msg_descriptor_t
*saddr
, *eaddr
;
1353 mach_msg_return_t mr
;
1356 vm_size_t space_needed
= 0;
1357 vm_offset_t paddr
= 0;
1358 mach_msg_descriptor_t
*sstart
;
1359 vm_map_copy_t copy
= VM_MAP_COPY_NULL
;
1362 * Determine if the target is a kernel port.
1364 dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
1367 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
1368 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
1369 eaddr
= saddr
+ body
->msgh_descriptor_count
;
1371 /* make sure the message does not ask for more msg descriptors
1372 * than the message can hold.
1375 if (eaddr
<= saddr
||
1376 eaddr
> (mach_msg_descriptor_t
*) (&kmsg
->ikm_header
+
1377 kmsg
->ikm_header
.msgh_size
)) {
1378 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1379 return MACH_SEND_MSG_TOO_SMALL
;
1383 * Make an initial pass to determine kernal VM space requirements for
1386 for (sstart
= saddr
; sstart
< eaddr
; sstart
++) {
1388 if (sstart
->type
.type
== MACH_MSG_OOL_DESCRIPTOR
||
1389 sstart
->type
.type
== MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
1391 if (sstart
->out_of_line
.copy
!= MACH_MSG_PHYSICAL_COPY
&&
1392 sstart
->out_of_line
.copy
!= MACH_MSG_VIRTUAL_COPY
) {
1394 * Invalid copy option
1396 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1397 return MACH_SEND_INVALID_TYPE
;
1400 if ((sstart
->out_of_line
.size
>= MSG_OOL_SIZE_SMALL
) &&
1401 (sstart
->out_of_line
.copy
== MACH_MSG_PHYSICAL_COPY
) &&
1402 !(sstart
->out_of_line
.deallocate
)) {
1405 * Out-of-line memory descriptor, accumulate kernel
1406 * memory requirements
1408 space_needed
+= round_page_32(sstart
->out_of_line
.size
);
1409 if (space_needed
> ipc_kmsg_max_vm_space
) {
1412 * Per message kernel memory limit exceeded
1414 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1415 return MACH_MSG_VM_KERNEL
;
1422 * Allocate space in the pageable kernel ipc copy map for all the
1423 * ool data that is to be physically copied. Map is marked wait for
1427 if (vm_allocate(ipc_kernel_copy_map
, &paddr
, space_needed
, TRUE
) !=
1429 ipc_kmsg_clean_partial(kmsg
,0,0,0);
1430 return MACH_MSG_VM_KERNEL
;
1435 * handle the OOL regions and port descriptors.
1436 * the check for complex messages was done earlier.
1439 for (i
= 0, sstart
= saddr
; sstart
< eaddr
; sstart
++) {
1441 switch (sstart
->type
.type
) {
1443 case MACH_MSG_PORT_DESCRIPTOR
: {
1444 mach_msg_type_name_t name
;
1445 ipc_object_t object
;
1446 mach_msg_port_descriptor_t
*dsc
;
1448 dsc
= &sstart
->port
;
1450 /* this is really the type SEND, SEND_ONCE, etc. */
1451 name
= dsc
->disposition
;
1452 dsc
->disposition
= ipc_object_copyin_type(name
);
1454 if (!MACH_PORT_VALID((mach_port_name_t
)dsc
->name
)) {
1458 kr
= ipc_object_copyin(space
, (mach_port_name_t
)dsc
->name
, name
, &object
);
1459 if (kr
!= KERN_SUCCESS
) {
1460 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1461 return MACH_SEND_INVALID_RIGHT
;
1463 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1464 ipc_port_check_circularity((ipc_port_t
) object
,
1465 (ipc_port_t
) dest
)) {
1466 kmsg
->ikm_header
.msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1468 dsc
->name
= (ipc_port_t
) object
;
1472 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1473 case MACH_MSG_OOL_DESCRIPTOR
: {
1478 mach_msg_ool_descriptor_t
*dsc
;
1480 dsc
= &sstart
->out_of_line
;
1481 dealloc
= dsc
->deallocate
;
1482 addr
= (vm_offset_t
) dsc
->address
;
1488 } else if ((length
>= MSG_OOL_SIZE_SMALL
) &&
1489 (dsc
->copy
== MACH_MSG_PHYSICAL_COPY
) && !dealloc
) {
1492 * If the request is a physical copy and the source
1493 * is not being deallocated, then allocate space
1494 * in the kernel's pageable ipc copy map and copy
1495 * the data in. The semantics guarantee that the
1496 * data will have been physically copied before
1497 * the send operation terminates. Thus if the data
1498 * is not being deallocated, we must be prepared
1499 * to page if the region is sufficiently large.
1501 if (copyin((const char *) addr
, (char *) paddr
,
1503 ipc_kmsg_clean_partial(kmsg
, i
, paddr
,
1505 return MACH_SEND_INVALID_MEMORY
;
1509 * The kernel ipc copy map is marked no_zero_fill.
1510 * If the transfer is not a page multiple, we need
1511 * to zero fill the balance.
1513 if (!page_aligned(length
)) {
1514 (void) memset((void *) (paddr
+ length
), 0,
1515 round_page_32(length
) - length
);
1517 if (vm_map_copyin(ipc_kernel_copy_map
, paddr
, length
,
1518 TRUE
, ©
) != KERN_SUCCESS
) {
1519 ipc_kmsg_clean_partial(kmsg
, i
, paddr
,
1521 return MACH_MSG_VM_KERNEL
;
1523 dsc
->address
= (void *) copy
;
1524 paddr
+= round_page_32(length
);
1525 space_needed
-= round_page_32(length
);
1529 * Make a vm_map_copy_t of the of the data. If the
1530 * data is small, this will do an optimized physical
1531 * copy. Otherwise, it will do a virtual copy.
1533 * NOTE: A virtual copy is OK if the original is being
1534 * deallocted, even if a physical copy was requested.
1536 kr
= vm_map_copyin(map
, addr
, length
, dealloc
, ©
);
1537 if (kr
!= KERN_SUCCESS
) {
1538 ipc_kmsg_clean_partial(kmsg
,i
,paddr
,space_needed
);
1539 return (kr
== KERN_RESOURCE_SHORTAGE
) ?
1540 MACH_MSG_VM_KERNEL
:
1541 MACH_SEND_INVALID_MEMORY
;
1543 dsc
->address
= (void *) copy
;
1548 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
1552 ipc_object_t
*objects
;
1554 mach_msg_type_name_t name
;
1555 mach_msg_ool_ports_descriptor_t
*dsc
;
1557 dsc
= &sstart
->ool_ports
;
1558 addr
= (vm_offset_t
) dsc
->address
;
1560 /* calculate length of data in bytes, rounding up */
1561 length
= dsc
->count
* sizeof(mach_port_name_t
);
1565 dsc
->address
= (void *) 0;
1569 data
= kalloc(length
);
1572 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1573 return MACH_SEND_NO_BUFFER
;
1576 if (copyinmap(map
, addr
, data
, length
)) {
1577 kfree(data
, length
);
1578 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1579 return MACH_SEND_INVALID_MEMORY
;
1582 if (dsc
->deallocate
) {
1583 (void) vm_deallocate(map
, addr
, length
);
1586 dsc
->address
= (void *) data
;
1588 /* this is really the type SEND, SEND_ONCE, etc. */
1589 name
= dsc
->disposition
;
1590 dsc
->disposition
= ipc_object_copyin_type(name
);
1592 objects
= (ipc_object_t
*) data
;
1594 for ( j
= 0; j
< dsc
->count
; j
++) {
1595 mach_port_name_t port
= (mach_port_name_t
) objects
[j
];
1596 ipc_object_t object
;
1598 if (!MACH_PORT_VALID(port
))
1601 kr
= ipc_object_copyin(space
, port
, name
, &object
);
1603 if (kr
!= KERN_SUCCESS
) {
1606 for(k
= 0; k
< j
; k
++) {
1607 object
= objects
[k
];
1608 if (!MACH_PORT_VALID(port
))
1610 ipc_object_destroy(object
, dsc
->disposition
);
1612 kfree(data
, length
);
1613 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1614 return MACH_SEND_INVALID_RIGHT
;
1617 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1618 ipc_port_check_circularity(
1619 (ipc_port_t
) object
,
1621 kmsg
->ikm_header
.msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1623 objects
[j
] = object
;
1631 * Invalid descriptor
1633 ipc_kmsg_clean_partial(kmsg
, i
, paddr
, space_needed
);
1634 return MACH_SEND_INVALID_TYPE
;
1641 kmsg
->ikm_header
.msgh_bits
&= ~MACH_MSGH_BITS_COMPLEX
;
1642 return MACH_MSG_SUCCESS
;
1647 * Routine: ipc_kmsg_copyin
1649 * "Copy-in" port rights and out-of-line memory
1652 * In all failure cases, the message is left holding
1653 * no rights or memory. However, the message buffer
1654 * is not deallocated. If successful, the message
1655 * contains a valid destination port.
1659 * MACH_MSG_SUCCESS Successful copyin.
1660 * MACH_SEND_INVALID_HEADER
1661 * Illegal value in the message header bits.
1662 * MACH_SEND_INVALID_NOTIFY Bad notify port.
1663 * MACH_SEND_INVALID_DEST Can't copyin destination port.
1664 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
1665 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
1666 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
1667 * MACH_SEND_INVALID_TYPE Bad type specification.
1668 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
1676 mach_port_name_t notify
)
1678 mach_msg_return_t mr
;
1680 mr
= ipc_kmsg_copyin_header(&kmsg
->ikm_header
, space
, notify
);
1681 if (mr
!= MACH_MSG_SUCCESS
)
1684 if ((kmsg
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
1685 return MACH_MSG_SUCCESS
;
1687 return( ipc_kmsg_copyin_body( kmsg
, space
, map
) );
1691 * Routine: ipc_kmsg_copyin_from_kernel
1693 * "Copy-in" port rights and out-of-line memory
1694 * in a message sent from the kernel.
1696 * Because the message comes from the kernel,
1697 * the implementation assumes there are no errors
1698 * or peculiarities in the message.
1700 * Returns TRUE if queueing the message
1701 * would result in a circularity.
1707 ipc_kmsg_copyin_from_kernel(
1710 mach_msg_bits_t bits
= kmsg
->ikm_header
.msgh_bits
;
1711 mach_msg_type_name_t rname
= MACH_MSGH_BITS_REMOTE(bits
);
1712 mach_msg_type_name_t lname
= MACH_MSGH_BITS_LOCAL(bits
);
1713 ipc_object_t remote
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
1714 ipc_object_t local
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
1716 /* translate the destination and reply ports */
1718 ipc_object_copyin_from_kernel(remote
, rname
);
1719 if (IO_VALID(local
))
1720 ipc_object_copyin_from_kernel(local
, lname
);
1723 * The common case is a complex message with no reply port,
1724 * because that is what the memory_object interface uses.
1727 if (bits
== (MACH_MSGH_BITS_COMPLEX
|
1728 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0))) {
1729 bits
= (MACH_MSGH_BITS_COMPLEX
|
1730 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND
, 0));
1732 kmsg
->ikm_header
.msgh_bits
= bits
;
1734 bits
= (MACH_MSGH_BITS_OTHER(bits
) |
1735 MACH_MSGH_BITS(ipc_object_copyin_type(rname
),
1736 ipc_object_copyin_type(lname
)));
1738 kmsg
->ikm_header
.msgh_bits
= bits
;
1739 if ((bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
1743 mach_msg_descriptor_t
*saddr
, *eaddr
;
1744 mach_msg_body_t
*body
;
1746 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
1747 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
1748 eaddr
= (mach_msg_descriptor_t
*) saddr
+ body
->msgh_descriptor_count
;
1750 for ( ; saddr
< eaddr
; saddr
++) {
1752 switch (saddr
->type
.type
) {
1754 case MACH_MSG_PORT_DESCRIPTOR
: {
1755 mach_msg_type_name_t name
;
1756 ipc_object_t object
;
1757 mach_msg_port_descriptor_t
*dsc
;
1761 /* this is really the type SEND, SEND_ONCE, etc. */
1762 name
= dsc
->disposition
;
1763 object
= (ipc_object_t
) dsc
->name
;
1764 dsc
->disposition
= ipc_object_copyin_type(name
);
1766 if (!IO_VALID(object
)) {
1770 ipc_object_copyin_from_kernel(object
, name
);
1772 /* CDY avoid circularity when the destination is also */
1773 /* the kernel. This check should be changed into an */
1774 /* assert when the new kobject model is in place since*/
1775 /* ports will not be used in kernel to kernel chats */
1777 if (((ipc_port_t
)remote
)->ip_receiver
!= ipc_space_kernel
) {
1778 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1779 ipc_port_check_circularity((ipc_port_t
) object
,
1780 (ipc_port_t
) remote
)) {
1781 kmsg
->ikm_header
.msgh_bits
|=
1782 MACH_MSGH_BITS_CIRCULAR
;
1787 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1788 case MACH_MSG_OOL_DESCRIPTOR
: {
1790 * The sender should supply ready-made memory, i.e.
1791 * a vm_map_copy_t, so we don't need to do anything.
1795 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
1796 ipc_object_t
*objects
;
1798 mach_msg_type_name_t name
;
1799 mach_msg_ool_ports_descriptor_t
*dsc
;
1801 dsc
= &saddr
->ool_ports
;
1803 /* this is really the type SEND, SEND_ONCE, etc. */
1804 name
= dsc
->disposition
;
1805 dsc
->disposition
= ipc_object_copyin_type(name
);
1807 objects
= (ipc_object_t
*) dsc
->address
;
1809 for ( j
= 0; j
< dsc
->count
; j
++) {
1810 ipc_object_t object
= objects
[j
];
1812 if (!IO_VALID(object
))
1815 ipc_object_copyin_from_kernel(object
, name
);
1817 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1818 ipc_port_check_circularity(
1819 (ipc_port_t
) object
,
1820 (ipc_port_t
) remote
))
1821 kmsg
->ikm_header
.msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1827 panic("ipc_kmsg_copyin_from_kernel: bad descriptor");
1828 #endif /* MACH_ASSERT */
1836 * Routine: ipc_kmsg_copyout_header
1838 * "Copy-out" port rights in the header of a message.
1839 * Operates atomically; if it doesn't succeed the
1840 * message header and the space are left untouched.
1841 * If it does succeed the remote/local port fields
1842 * contain port names instead of object pointers,
1843 * and the bits field is updated.
1845 * The notify argument implements the MACH_RCV_NOTIFY option.
1846 * If it is not MACH_PORT_NULL, it should name a receive right.
1847 * If the process of receiving the reply port creates a
1848 * new right in the receiving task, then the new right is
1849 * automatically registered for a dead-name notification,
1850 * with the notify port supplying the send-once right.
1854 * MACH_MSG_SUCCESS Copied out port rights.
1855 * MACH_RCV_INVALID_NOTIFY
1856 * Notify is non-null and doesn't name a receive right.
1857 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
1858 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
1859 * The space is dead.
1860 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
1861 * No room in space for another name.
1862 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
1863 * Couldn't allocate memory for the reply port.
1864 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
1865 * Couldn't allocate memory for the dead-name request.
1869 ipc_kmsg_copyout_header(
1870 mach_msg_header_t
*msg
,
1872 mach_port_name_t notify
)
1874 mach_msg_bits_t mbits
= msg
->msgh_bits
;
1875 ipc_port_t dest
= (ipc_port_t
) msg
->msgh_remote_port
;
1877 assert(IP_VALID(dest
));
1880 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
1881 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
1882 ipc_port_t reply
= (ipc_port_t
) msg
->msgh_local_port
;
1883 mach_port_name_t dest_name
, reply_name
;
1885 if (IP_VALID(reply
)) {
1886 ipc_port_t notify_port
;
1891 * Handling notify (for MACH_RCV_NOTIFY) is tricky.
1892 * The problem is atomically making a send-once right
1893 * from the notify port and installing it for a
1894 * dead-name request in the new entry, because this
1895 * requires two port locks (on the notify port and
1896 * the reply port). However, we can safely make
1897 * and consume send-once rights for the notify port
1898 * as long as we hold the space locked. This isn't
1899 * an atomicity problem, because the only way
1900 * to detect that a send-once right has been created
1901 * and then consumed if it wasn't needed is by getting
1902 * at the receive right to look at ip_sorights, and
1903 * because the space is write-locked status calls can't
1904 * lookup the notify port receive right. When we make
1905 * the send-once right, we lock the notify port,
1906 * so any status calls in progress will be done.
1909 is_write_lock(space
);
1912 ipc_port_request_index_t request
;
1914 if (!space
->is_active
) {
1915 is_write_unlock(space
);
1916 return (MACH_RCV_HEADER_ERROR
|
1917 MACH_MSG_IPC_SPACE
);
1920 if (notify
!= MACH_PORT_NULL
) {
1921 notify_port
= ipc_port_lookup_notify(space
,
1923 if (notify_port
== IP_NULL
) {
1924 is_write_unlock(space
);
1925 return MACH_RCV_INVALID_NOTIFY
;
1928 notify_port
= IP_NULL
;
1930 if ((reply_type
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
1931 ipc_right_reverse(space
, (ipc_object_t
) reply
,
1932 &reply_name
, &entry
)) {
1933 /* reply port is locked and active */
1936 * We don't need the notify_port
1937 * send-once right, but we can't release
1938 * it here because reply port is locked.
1939 * Wait until after the copyout to
1940 * release the notify port right.
1943 assert(entry
->ie_bits
&
1944 MACH_PORT_TYPE_SEND_RECEIVE
);
1949 if (!ip_active(reply
)) {
1951 ip_check_unlock(reply
);
1953 if (notify_port
!= IP_NULL
)
1954 ipc_port_release_sonce(notify_port
);
1957 is_write_unlock(space
);
1960 reply_name
= MACH_PORT_DEAD
;
1964 reply_name
= (mach_port_name_t
)reply
;
1965 kr
= ipc_entry_get(space
, &reply_name
, &entry
);
1966 if (kr
!= KERN_SUCCESS
) {
1969 if (notify_port
!= IP_NULL
)
1970 ipc_port_release_sonce(notify_port
);
1972 /* space is locked */
1973 kr
= ipc_entry_grow_table(space
,
1975 if (kr
!= KERN_SUCCESS
) {
1976 /* space is unlocked */
1978 if (kr
== KERN_RESOURCE_SHORTAGE
)
1979 return (MACH_RCV_HEADER_ERROR
|
1980 MACH_MSG_IPC_KERNEL
);
1982 return (MACH_RCV_HEADER_ERROR
|
1983 MACH_MSG_IPC_SPACE
);
1985 /* space is locked again; start over */
1989 assert(IE_BITS_TYPE(entry
->ie_bits
) ==
1990 MACH_PORT_TYPE_NONE
);
1991 assert(entry
->ie_object
== IO_NULL
);
1993 if (notify_port
== IP_NULL
) {
1994 /* not making a dead-name request */
1996 entry
->ie_object
= (ipc_object_t
) reply
;
2000 kr
= ipc_port_dnrequest(reply
, reply_name
,
2001 notify_port
, &request
);
2002 if (kr
!= KERN_SUCCESS
) {
2005 ipc_port_release_sonce(notify_port
);
2007 ipc_entry_dealloc(space
, reply_name
, entry
);
2008 is_write_unlock(space
);
2011 if (!ip_active(reply
)) {
2012 /* will fail next time around loop */
2015 is_write_lock(space
);
2019 kr
= ipc_port_dngrow(reply
, ITS_SIZE_NONE
);
2020 /* port is unlocked */
2021 if (kr
!= KERN_SUCCESS
)
2022 return (MACH_RCV_HEADER_ERROR
|
2023 MACH_MSG_IPC_KERNEL
);
2025 is_write_lock(space
);
2029 notify_port
= IP_NULL
; /* don't release right below */
2031 entry
->ie_object
= (ipc_object_t
) reply
;
2032 entry
->ie_request
= request
;
2036 /* space and reply port are locked and active */
2038 ip_reference(reply
); /* hold onto the reply port */
2040 kr
= ipc_right_copyout(space
, reply_name
, entry
,
2041 reply_type
, TRUE
, (ipc_object_t
) reply
);
2042 /* reply port is unlocked */
2043 assert(kr
== KERN_SUCCESS
);
2045 if (notify_port
!= IP_NULL
)
2046 ipc_port_release_sonce(notify_port
);
2049 is_write_unlock(space
);
2052 * No reply port! This is an easy case.
2053 * We only need to have the space locked
2054 * when checking notify and when locking
2055 * the destination (to ensure atomicity).
2058 is_read_lock(space
);
2059 if (!space
->is_active
) {
2060 is_read_unlock(space
);
2061 return MACH_RCV_HEADER_ERROR
|MACH_MSG_IPC_SPACE
;
2064 if (notify
!= MACH_PORT_NULL
) {
2067 /* must check notify even though it won't be used */
2069 if ((entry
= ipc_entry_lookup(space
, notify
)) == IE_NULL
) {
2070 is_read_unlock(space
);
2071 return MACH_RCV_INVALID_NOTIFY
;
2074 if ((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) == 0) {
2075 is_read_unlock(space
);
2076 return MACH_RCV_INVALID_NOTIFY
;
2081 is_read_unlock(space
);
2083 reply_name
= (mach_port_name_t
) reply
;
2087 * At this point, the space is unlocked and the destination
2088 * port is locked. (Lock taken while space was locked.)
2089 * reply_name is taken care of; we still need dest_name.
2090 * We still hold a ref for reply (if it is valid).
2092 * If the space holds receive rights for the destination,
2093 * we return its name for the right. Otherwise the task
2094 * managed to destroy or give away the receive right between
2095 * receiving the message and this copyout. If the destination
2096 * is dead, return MACH_PORT_DEAD, and if the receive right
2097 * exists somewhere else (another space, in transit)
2098 * return MACH_PORT_NULL.
2100 * Making this copyout operation atomic with the previous
2101 * copyout of the reply port is a bit tricky. If there was
2102 * no real reply port (it wasn't IP_VALID) then this isn't
2103 * an issue. If the reply port was dead at copyout time,
2104 * then we are OK, because if dest is dead we serialize
2105 * after the death of both ports and if dest is alive
2106 * we serialize after reply died but before dest's (later) death.
2107 * So assume reply was alive when we copied it out. If dest
2108 * is alive, then we are OK because we serialize before
2109 * the ports' deaths. So assume dest is dead when we look at it.
2110 * If reply dies/died after dest, then we are OK because
2111 * we serialize after dest died but before reply dies.
2112 * So the hard case is when reply is alive at copyout,
2113 * dest is dead at copyout, and reply died before dest died.
2114 * In this case pretend that dest is still alive, so
2115 * we serialize while both ports are alive.
2117 * Because the space lock is held across the copyout of reply
2118 * and locking dest, the receive right for dest can't move
2119 * in or out of the space while the copyouts happen, so
2120 * that isn't an atomicity problem. In the last hard case
2121 * above, this implies that when dest is dead that the
2122 * space couldn't have had receive rights for dest at
2123 * the time reply was copied-out, so when we pretend
2124 * that dest is still alive, we can return MACH_PORT_NULL.
2126 * If dest == reply, then we have to make it look like
2127 * either both copyouts happened before the port died,
2128 * or both happened after the port died. This special
2129 * case works naturally if the timestamp comparison
2130 * is done correctly.
2135 if (ip_active(dest
)) {
2136 ipc_object_copyout_dest(space
, (ipc_object_t
) dest
,
2137 dest_type
, &dest_name
);
2138 /* dest is unlocked */
2140 ipc_port_timestamp_t timestamp
;
2142 timestamp
= dest
->ip_timestamp
;
2144 ip_check_unlock(dest
);
2146 if (IP_VALID(reply
)) {
2148 if (ip_active(reply
) ||
2149 IP_TIMESTAMP_ORDER(timestamp
,
2150 reply
->ip_timestamp
))
2151 dest_name
= MACH_PORT_DEAD
;
2153 dest_name
= MACH_PORT_NULL
;
2156 dest_name
= MACH_PORT_DEAD
;
2159 if (IP_VALID(reply
))
2160 ipc_port_release(reply
);
2162 msg
->msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
2163 MACH_MSGH_BITS(reply_type
, dest_type
));
2164 msg
->msgh_local_port
= (ipc_port_t
)dest_name
;
2165 msg
->msgh_remote_port
= (ipc_port_t
)reply_name
;
2168 return MACH_MSG_SUCCESS
;
2172 * Routine: ipc_kmsg_copyout_object
2174 * Copy-out a port right. Always returns a name,
2175 * even for unsuccessful return codes. Always
2176 * consumes the supplied object.
2180 * MACH_MSG_SUCCESS The space acquired the right
2181 * (name is valid) or the object is dead (MACH_PORT_DEAD).
2182 * MACH_MSG_IPC_SPACE No room in space for the right,
2183 * or the space is dead. (Name is MACH_PORT_NULL.)
2184 * MACH_MSG_IPC_KERNEL Kernel resource shortage.
2185 * (Name is MACH_PORT_NULL.)
2189 ipc_kmsg_copyout_object(
2191 ipc_object_t object
,
2192 mach_msg_type_name_t msgt_name
,
2193 mach_port_name_t
*namep
)
2197 if (!IO_VALID(object
)) {
2198 *namep
= (mach_port_name_t
) object
;
2199 return MACH_MSG_SUCCESS
;
2202 kr
= ipc_object_copyout(space
, object
, msgt_name
, TRUE
, namep
);
2203 if (kr
!= KERN_SUCCESS
) {
2204 ipc_object_destroy(object
, msgt_name
);
2206 if (kr
== KERN_INVALID_CAPABILITY
)
2207 *namep
= MACH_PORT_DEAD
;
2209 *namep
= MACH_PORT_NULL
;
2211 if (kr
== KERN_RESOURCE_SHORTAGE
)
2212 return MACH_MSG_IPC_KERNEL
;
2214 return MACH_MSG_IPC_SPACE
;
2218 return MACH_MSG_SUCCESS
;
2222 * Routine: ipc_kmsg_copyout_body
2224 * "Copy-out" port rights and out-of-line memory
2225 * in the body of a message.
2227 * The error codes are a combination of special bits.
2228 * The copyout proceeds despite errors.
2232 * MACH_MSG_SUCCESS Successful copyout.
2233 * MACH_MSG_IPC_SPACE No room for port right in name space.
2234 * MACH_MSG_VM_SPACE No room for memory in address space.
2235 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
2236 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
2237 * MACH_MSG_INVALID_RT_DESCRIPTOR Descriptor incompatible with RT
2241 ipc_kmsg_copyout_body(
2245 mach_msg_body_t
*slist
)
2247 mach_msg_body_t
*body
;
2248 mach_msg_descriptor_t
*saddr
, *eaddr
;
2249 mach_msg_return_t mr
= MACH_MSG_SUCCESS
;
2252 mach_msg_descriptor_t
*sstart
, *send
;
2254 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
2255 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
2256 eaddr
= saddr
+ body
->msgh_descriptor_count
;
2259 * Do scatter list setup
2261 if (slist
!= MACH_MSG_BODY_NULL
) {
2262 sstart
= (mach_msg_descriptor_t
*) (slist
+ 1);
2263 send
= sstart
+ slist
->msgh_descriptor_count
;
2266 sstart
= MACH_MSG_DESCRIPTOR_NULL
;
2269 for ( ; saddr
< eaddr
; saddr
++ ) {
2271 switch (saddr
->type
.type
) {
2273 case MACH_MSG_PORT_DESCRIPTOR
: {
2274 mach_msg_port_descriptor_t
*dsc
;
2277 * Copyout port right carried in the message
2280 mr
|= ipc_kmsg_copyout_object(space
,
2281 (ipc_object_t
) dsc
->name
,
2283 (mach_port_name_t
*) &dsc
->name
);
2287 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
2288 case MACH_MSG_OOL_DESCRIPTOR
: {
2289 vm_offset_t rcv_addr
;
2290 vm_offset_t snd_addr
;
2291 mach_msg_ool_descriptor_t
*dsc
;
2292 mach_msg_copy_options_t copy_option
;
2294 SKIP_PORT_DESCRIPTORS(sstart
, send
);
2296 dsc
= &saddr
->out_of_line
;
2298 assert(dsc
->copy
!= MACH_MSG_KALLOC_COPY_T
);
2300 copy_option
= dsc
->copy
;
2302 if ((snd_addr
= (vm_offset_t
) dsc
->address
) != 0) {
2303 if (sstart
!= MACH_MSG_DESCRIPTOR_NULL
&&
2304 sstart
->out_of_line
.copy
== MACH_MSG_OVERWRITE
) {
2307 * There is an overwrite descriptor specified in the
2308 * scatter list for this ool data. The descriptor
2309 * has already been verified
2311 rcv_addr
= (vm_offset_t
) sstart
->out_of_line
.address
;
2312 dsc
->copy
= MACH_MSG_OVERWRITE
;
2314 dsc
->copy
= MACH_MSG_ALLOCATE
;
2318 * Whether the data was virtually or physically
2319 * copied we have a vm_map_copy_t for it.
2320 * If there's an overwrite region specified
2321 * overwrite it, otherwise do a virtual copy out.
2323 if (dsc
->copy
== MACH_MSG_OVERWRITE
) {
2324 kr
= vm_map_copy_overwrite(map
, rcv_addr
,
2325 (vm_map_copy_t
) dsc
->address
, TRUE
);
2327 kr
= vm_map_copyout(map
, &rcv_addr
,
2328 (vm_map_copy_t
) dsc
->address
);
2330 if (kr
!= KERN_SUCCESS
) {
2331 if (kr
== KERN_RESOURCE_SHORTAGE
)
2332 mr
|= MACH_MSG_VM_KERNEL
;
2334 mr
|= MACH_MSG_VM_SPACE
;
2335 vm_map_copy_discard((vm_map_copy_t
) dsc
->address
);
2337 INCREMENT_SCATTER(sstart
);
2340 dsc
->address
= (void *) rcv_addr
;
2342 INCREMENT_SCATTER(sstart
);
2345 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
2347 mach_port_name_t
*objects
;
2348 mach_msg_type_number_t j
;
2350 mach_msg_ool_ports_descriptor_t
*dsc
;
2352 SKIP_PORT_DESCRIPTORS(sstart
, send
);
2354 dsc
= &saddr
->ool_ports
;
2356 length
= dsc
->count
* sizeof(mach_port_name_t
);
2359 if (sstart
!= MACH_MSG_DESCRIPTOR_NULL
&&
2360 sstart
->ool_ports
.copy
== MACH_MSG_OVERWRITE
) {
2363 * There is an overwrite descriptor specified in the
2364 * scatter list for this ool data. The descriptor
2365 * has already been verified
2367 addr
= (vm_offset_t
) sstart
->out_of_line
.address
;
2368 dsc
->copy
= MACH_MSG_OVERWRITE
;
2373 * Dynamically allocate the region
2375 int anywhere
= VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|
2378 dsc
->copy
= MACH_MSG_ALLOCATE
;
2379 if ((kr
= vm_allocate(map
, &addr
, length
,
2380 anywhere
)) != KERN_SUCCESS
) {
2381 ipc_kmsg_clean_body(kmsg
,
2382 body
->msgh_descriptor_count
);
2385 if (kr
== KERN_RESOURCE_SHORTAGE
){
2386 mr
|= MACH_MSG_VM_KERNEL
;
2388 mr
|= MACH_MSG_VM_SPACE
;
2390 INCREMENT_SCATTER(sstart
);
2395 INCREMENT_SCATTER(sstart
);
2400 objects
= (mach_port_name_t
*) dsc
->address
;
2402 /* copyout port rights carried in the message */
2404 for ( j
= 0; j
< dsc
->count
; j
++) {
2405 ipc_object_t object
=
2406 (ipc_object_t
) objects
[j
];
2408 mr
|= ipc_kmsg_copyout_object(space
, object
,
2409 dsc
->disposition
, &objects
[j
]);
2412 /* copyout to memory allocated above */
2414 data
= (vm_offset_t
) dsc
->address
;
2415 (void) copyoutmap(map
, data
, addr
, length
);
2416 kfree(data
, length
);
2418 dsc
->address
= (void *) addr
;
2419 INCREMENT_SCATTER(sstart
);
2423 panic("untyped IPC copyout body: invalid message descriptor");
2431 * Routine: ipc_kmsg_copyout
2433 * "Copy-out" port rights and out-of-line memory
2438 * MACH_MSG_SUCCESS Copied out all rights and memory.
2439 * MACH_RCV_INVALID_NOTIFY Bad notify port.
2440 * Rights and memory in the message are intact.
2441 * MACH_RCV_HEADER_ERROR + special bits
2442 * Rights and memory in the message are intact.
2443 * MACH_RCV_BODY_ERROR + special bits
2444 * The message header was successfully copied out.
2445 * As much of the body was handled as possible.
2453 mach_port_name_t notify
,
2454 mach_msg_body_t
*slist
)
2456 mach_msg_return_t mr
;
2458 mr
= ipc_kmsg_copyout_header(&kmsg
->ikm_header
, space
, notify
);
2459 if (mr
!= MACH_MSG_SUCCESS
)
2462 if (kmsg
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
2463 mr
= ipc_kmsg_copyout_body(kmsg
, space
, map
, slist
);
2465 if (mr
!= MACH_MSG_SUCCESS
)
2466 mr
|= MACH_RCV_BODY_ERROR
;
2473 * Routine: ipc_kmsg_copyout_pseudo
2475 * Does a pseudo-copyout of the message.
2476 * This is like a regular copyout, except
2477 * that the ports in the header are handled
2478 * as if they are in the body. They aren't reversed.
2480 * The error codes are a combination of special bits.
2481 * The copyout proceeds despite errors.
2485 * MACH_MSG_SUCCESS Successful copyout.
2486 * MACH_MSG_IPC_SPACE No room for port right in name space.
2487 * MACH_MSG_VM_SPACE No room for memory in address space.
2488 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
2489 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
2493 ipc_kmsg_copyout_pseudo(
2497 mach_msg_body_t
*slist
)
2499 mach_msg_bits_t mbits
= kmsg
->ikm_header
.msgh_bits
;
2500 ipc_object_t dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
2501 ipc_object_t reply
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
2502 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
2503 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
2504 mach_port_name_t dest_name
, reply_name
;
2505 mach_msg_return_t mr
;
2507 assert(IO_VALID(dest
));
2509 mr
= (ipc_kmsg_copyout_object(space
, dest
, dest_type
, &dest_name
) |
2510 ipc_kmsg_copyout_object(space
, reply
, reply_type
, &reply_name
));
2512 kmsg
->ikm_header
.msgh_bits
= mbits
&~ MACH_MSGH_BITS_CIRCULAR
;
2513 kmsg
->ikm_header
.msgh_remote_port
= (ipc_port_t
)dest_name
;
2514 kmsg
->ikm_header
.msgh_local_port
= (ipc_port_t
)reply_name
;
2516 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2517 mr
|= ipc_kmsg_copyout_body(kmsg
, space
, map
, slist
);
2524 * Routine: ipc_kmsg_copyout_dest
2526 * Copies out the destination port in the message.
2527 * Destroys all other rights and memory in the message.
2533 ipc_kmsg_copyout_dest(
2537 mach_msg_bits_t mbits
;
2540 mach_msg_type_name_t dest_type
;
2541 mach_msg_type_name_t reply_type
;
2542 mach_port_name_t dest_name
, reply_name
;
2544 mbits
= kmsg
->ikm_header
.msgh_bits
;
2545 dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
2546 reply
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
2547 dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
2548 reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
2550 assert(IO_VALID(dest
));
2553 if (io_active(dest
)) {
2554 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
2555 /* dest is unlocked */
2558 io_check_unlock(dest
);
2559 dest_name
= MACH_PORT_DEAD
;
2562 if (IO_VALID(reply
)) {
2563 ipc_object_destroy(reply
, reply_type
);
2564 reply_name
= MACH_PORT_NULL
;
2566 reply_name
= (mach_port_name_t
) reply
;
2568 kmsg
->ikm_header
.msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
2569 MACH_MSGH_BITS(reply_type
, dest_type
));
2570 kmsg
->ikm_header
.msgh_local_port
= (ipc_port_t
)dest_name
;
2571 kmsg
->ikm_header
.msgh_remote_port
= (ipc_port_t
)reply_name
;
2573 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2574 mach_msg_body_t
*body
;
2576 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
2577 ipc_kmsg_clean_body(kmsg
, body
->msgh_descriptor_count
);
2581 * Routine: ipc_kmsg_copyin_scatter
2583 * allocate and copyin a scatter list
2585 * The gather (kmsg) is valid since it has been copied in.
2586 * Gather list descriptors are sequentially paired with scatter
2587 * list descriptors, with port descriptors in either list ignored.
2588 * Descriptors are consistent if the type fileds match and size
2589 * of the scatter descriptor is less than or equal to the
2590 * size of the gather descriptor. A MACH_MSG_ALLOCATE copy
2591 * strategy in a scatter descriptor matches any size in the
2592 * corresponding gather descriptor assuming they are the same type.
2593 * Either list may be larger than the other. During the
2594 * subsequent copy out, excess scatter descriptors are ignored
2595 * and excess gather descriptors default to dynamic allocation.
2597 * In the case of a size error, the scatter list is released.
2601 * the allocated message body containing the scatter list.
2605 ipc_kmsg_copyin_scatter(
2606 mach_msg_header_t
*msg
,
2607 mach_msg_size_t slist_size
,
2610 mach_msg_body_t
*slist
;
2611 mach_msg_body_t
*body
;
2612 mach_msg_descriptor_t
*gstart
, *gend
;
2613 mach_msg_descriptor_t
*sstart
, *send
;
2616 if (slist_size
< sizeof(mach_msg_base_t
))
2617 return MACH_MSG_BODY_NULL
;
2619 slist_size
-= sizeof(mach_msg_header_t
);
2620 slist
= (mach_msg_body_t
*)kalloc(slist_size
);
2621 if (slist
== MACH_MSG_BODY_NULL
)
2624 if (copyin((char *) (msg
+ 1), (char *)slist
, slist_size
)) {
2625 kfree((vm_offset_t
)slist
, slist_size
);
2626 return MACH_MSG_BODY_NULL
;
2629 if ((slist
->msgh_descriptor_count
* sizeof(mach_msg_descriptor_t
)
2630 + sizeof(mach_msg_size_t
)) > slist_size
) {
2631 kfree((vm_offset_t
)slist
, slist_size
);
2632 return MACH_MSG_BODY_NULL
;
2635 body
= (mach_msg_body_t
*) (&kmsg
->ikm_header
+ 1);
2636 gstart
= (mach_msg_descriptor_t
*) (body
+ 1);
2637 gend
= gstart
+ body
->msgh_descriptor_count
;
2639 sstart
= (mach_msg_descriptor_t
*) (slist
+ 1);
2640 send
= sstart
+ slist
->msgh_descriptor_count
;
2642 while (gstart
< gend
) {
2643 mach_msg_descriptor_type_t g_type
;
2646 * Skip port descriptors in gather list.
2648 g_type
= gstart
->type
.type
;
2650 if (g_type
!= MACH_MSG_PORT_DESCRIPTOR
) {
2653 * A scatter list with a 0 descriptor count is treated as an
2654 * automatic size mismatch.
2656 if (slist
->msgh_descriptor_count
== 0) {
2657 kfree((vm_offset_t
)slist
, slist_size
);
2658 return MACH_MSG_BODY_NULL
;
2662 * Skip port descriptors in scatter list.
2664 while (sstart
< send
) {
2665 if (sstart
->type
.type
!= MACH_MSG_PORT_DESCRIPTOR
)
2671 * No more scatter descriptors, we're done
2673 if (sstart
>= send
) {
2678 * Check type, copy and size fields
2680 if (g_type
== MACH_MSG_OOL_DESCRIPTOR
||
2681 g_type
== MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
2682 if (sstart
->type
.type
!= MACH_MSG_OOL_DESCRIPTOR
&&
2683 sstart
->type
.type
!= MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
2684 kfree((vm_offset_t
)slist
, slist_size
);
2685 return MACH_MSG_BODY_NULL
;
2687 if (sstart
->out_of_line
.copy
== MACH_MSG_OVERWRITE
&&
2688 gstart
->out_of_line
.size
> sstart
->out_of_line
.size
) {
2689 kfree((vm_offset_t
)slist
, slist_size
);
2690 return MACH_MSG_BODY_NULL
;
2694 if (sstart
->type
.type
!= MACH_MSG_OOL_PORTS_DESCRIPTOR
) {
2695 kfree((vm_offset_t
)slist
, slist_size
);
2696 return MACH_MSG_BODY_NULL
;
2698 if (sstart
->ool_ports
.copy
== MACH_MSG_OVERWRITE
&&
2699 gstart
->ool_ports
.count
> sstart
->ool_ports
.count
) {
2700 kfree((vm_offset_t
)slist
, slist_size
);
2701 return MACH_MSG_BODY_NULL
;
2713 * Routine: ipc_kmsg_free_scatter
2715 * Deallocate a scatter list. Since we actually allocated
2716 * a body without a header, and since the header was originally
2717 * accounted for in slist_size, we have to ajust it down
2718 * before freeing the scatter list.
2721 ipc_kmsg_free_scatter(
2722 mach_msg_body_t
*slist
,
2723 mach_msg_size_t slist_size
)
2725 slist_size
-= sizeof(mach_msg_header_t
);
2726 kfree((vm_offset_t
)slist
, slist_size
);
2731 * Routine: ipc_kmsg_copyout_to_kernel
2733 * Copies out the destination and reply ports in the message.
2734 * Leaves all other rights and memory in the message alone.
2738 * Derived from ipc_kmsg_copyout_dest.
2739 * Use by mach_msg_rpc_from_kernel (which used to use copyout_dest).
2740 * We really do want to save rights and memory.
2744 ipc_kmsg_copyout_to_kernel(
2750 mach_msg_type_name_t dest_type
;
2751 mach_msg_type_name_t reply_type
;
2752 mach_port_name_t dest_name
, reply_name
;
2754 dest
= (ipc_object_t
) kmsg
->ikm_header
.msgh_remote_port
;
2755 reply
= (ipc_object_t
) kmsg
->ikm_header
.msgh_local_port
;
2756 dest_type
= MACH_MSGH_BITS_REMOTE(kmsg
->ikm_header
.msgh_bits
);
2757 reply_type
= MACH_MSGH_BITS_LOCAL(kmsg
->ikm_header
.msgh_bits
);
2759 assert(IO_VALID(dest
));
2762 if (io_active(dest
)) {
2763 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
2764 /* dest is unlocked */
2767 io_check_unlock(dest
);
2768 dest_name
= MACH_PORT_DEAD
;
2771 reply_name
= (mach_port_name_t
) reply
;
2773 kmsg
->ikm_header
.msgh_bits
=
2774 (MACH_MSGH_BITS_OTHER(kmsg
->ikm_header
.msgh_bits
) |
2775 MACH_MSGH_BITS(reply_type
, dest_type
));
2776 kmsg
->ikm_header
.msgh_local_port
= (ipc_port_t
)dest_name
;
2777 kmsg
->ikm_header
.msgh_remote_port
= (ipc_port_t
)reply_name
;
2780 #include <mach_kdb.h>
2783 #include <ddb/db_output.h>
2784 #include <ipc/ipc_print.h>
2786 * Forward declarations
2788 void ipc_msg_print_untyped(
2789 mach_msg_body_t
*body
);
2791 char * ipc_type_name(
2793 boolean_t received
);
2795 void ipc_print_type_name(
2800 mach_msg_bits_t bit
);
2803 mm_copy_options_string(
2804 mach_msg_copy_options_t option
);
2806 void db_print_msg_uid(mach_msg_header_t
*);
2814 switch (type_name
) {
2815 case MACH_MSG_TYPE_PORT_NAME
:
2818 case MACH_MSG_TYPE_MOVE_RECEIVE
:
2820 return "port_receive";
2822 return "move_receive";
2825 case MACH_MSG_TYPE_MOVE_SEND
:
2832 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
2834 return "port_send_once";
2836 return "move_send_once";
2839 case MACH_MSG_TYPE_COPY_SEND
:
2842 case MACH_MSG_TYPE_MAKE_SEND
:
2845 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
2846 return "make_send_once";
2854 ipc_print_type_name(
2857 char *name
= ipc_type_name(type_name
, TRUE
);
2861 printf("type%d", type_name
);
2866 * ipc_kmsg_print [ debug ]
2872 iprintf("kmsg=0x%x\n", kmsg
);
2873 iprintf("ikm_next=0x%x, prev=0x%x, size=%d",
2878 ipc_msg_print(&kmsg
->ikm_header
);
2883 mach_msg_bits_t bit
)
2886 case MACH_MSGH_BITS_COMPLEX
: return "complex";
2887 case MACH_MSGH_BITS_CIRCULAR
: return "circular";
2888 default: return (char *) 0;
2893 * ipc_msg_print [ debug ]
2897 mach_msg_header_t
*msgh
)
2899 mach_msg_bits_t mbits
;
2900 unsigned int bit
, i
;
2904 mbits
= msgh
->msgh_bits
;
2905 iprintf("msgh_bits=0x%x: l=0x%x,r=0x%x\n",
2907 MACH_MSGH_BITS_LOCAL(msgh
->msgh_bits
),
2908 MACH_MSGH_BITS_REMOTE(msgh
->msgh_bits
));
2910 mbits
= MACH_MSGH_BITS_OTHER(mbits
) & MACH_MSGH_BITS_USED
;
2913 iprintf("decoded bits: ");
2915 for (i
= 0, bit
= 1; i
< sizeof(mbits
) * 8; ++i
, bit
<<= 1) {
2916 if ((mbits
& bit
) == 0)
2918 bit_name
= msgh_bit_decode((mach_msg_bits_t
)bit
);
2920 printf("%s%s", needs_comma
? "," : "", bit_name
);
2922 printf("%sunknown(0x%x),", needs_comma
? "," : "", bit
);
2925 if (msgh
->msgh_bits
& ~MACH_MSGH_BITS_USED
) {
2926 printf("%sunused=0x%x,", needs_comma
? "," : "",
2927 msgh
->msgh_bits
& ~MACH_MSGH_BITS_USED
);
2933 if (msgh
->msgh_remote_port
) {
2934 iprintf("remote=0x%x(", msgh
->msgh_remote_port
);
2935 ipc_print_type_name(MACH_MSGH_BITS_REMOTE(msgh
->msgh_bits
));
2938 iprintf("remote=null");
2941 if (msgh
->msgh_local_port
) {
2942 printf("%slocal=0x%x(", needs_comma
? "," : "",
2943 msgh
->msgh_local_port
);
2944 ipc_print_type_name(MACH_MSGH_BITS_LOCAL(msgh
->msgh_bits
));
2947 printf("local=null\n");
2950 iprintf("msgh_id=%d, size=%d\n",
2954 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2955 ipc_msg_print_untyped((mach_msg_body_t
*) (msgh
+ 1));
2961 mm_copy_options_string(
2962 mach_msg_copy_options_t option
)
2967 case MACH_MSG_PHYSICAL_COPY
:
2970 case MACH_MSG_VIRTUAL_COPY
:
2973 case MACH_MSG_OVERWRITE
:
2976 case MACH_MSG_ALLOCATE
:
2979 case MACH_MSG_KALLOC_COPY_T
:
2980 name
= "KALLOC_COPY_T";
2990 ipc_msg_print_untyped(
2991 mach_msg_body_t
*body
)
2993 mach_msg_descriptor_t
*saddr
, *send
;
2994 mach_msg_descriptor_type_t type
;
2996 iprintf("%d descriptors %d: \n", body
->msgh_descriptor_count
);
2998 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
2999 send
= saddr
+ body
->msgh_descriptor_count
;
3001 for ( ; saddr
< send
; saddr
++ ) {
3003 type
= saddr
->type
.type
;
3007 case MACH_MSG_PORT_DESCRIPTOR
: {
3008 mach_msg_port_descriptor_t
*dsc
;
3011 iprintf("-- PORT name = 0x%x disp = ", dsc
->name
);
3012 ipc_print_type_name(dsc
->disposition
);
3016 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
3017 case MACH_MSG_OOL_DESCRIPTOR
: {
3018 mach_msg_ool_descriptor_t
*dsc
;
3020 dsc
= &saddr
->out_of_line
;
3021 iprintf("-- OOL%s addr = 0x%x size = 0x%x copy = %s %s\n",
3022 type
== MACH_MSG_OOL_DESCRIPTOR
? "" : " VOLATILE",
3023 dsc
->address
, dsc
->size
,
3024 mm_copy_options_string(dsc
->copy
),
3025 dsc
->deallocate
? "DEALLOC" : "");
3028 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
3029 mach_msg_ool_ports_descriptor_t
*dsc
;
3031 dsc
= &saddr
->ool_ports
;
3033 iprintf("-- OOL_PORTS addr = 0x%x count = 0x%x ",
3034 dsc
->address
, dsc
->count
);
3036 ipc_print_type_name(dsc
->disposition
);
3037 printf(" copy = %s %s\n",
3038 mm_copy_options_string(dsc
->copy
),
3039 dsc
->deallocate
? "DEALLOC" : "");
3044 iprintf("-- UNKNOWN DESCRIPTOR 0x%x\n", type
);
3050 #endif /* MACH_KDB */