2 * Copyright (c) 2000-2005 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.
62 #include <mach/mach_types.h>
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_map.h>
68 #include <mach/mach_vm.h>
69 #include <mach/vm_statistics.h>
71 #include <kern/kern_types.h>
72 #include <kern/assert.h>
73 #include <kern/ipc_kobject.h>
74 #include <kern/kalloc.h>
75 #include <kern/zalloc.h>
76 #include <kern/processor.h>
77 #include <kern/thread.h>
78 #include <kern/sched_prim.h>
80 #include <kern/misc_protos.h>
81 #include <kern/counters.h>
82 #include <kern/cpu_data.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_kern.h>
89 #include <ipc/ipc_types.h>
90 #include <ipc/ipc_entry.h>
91 #include <ipc/ipc_kmsg.h>
92 #include <ipc/ipc_notify.h>
93 #include <ipc/ipc_object.h>
94 #include <ipc/ipc_space.h>
95 #include <ipc/ipc_port.h>
96 #include <ipc/ipc_right.h>
97 #include <ipc/ipc_hash.h>
98 #include <ipc/ipc_table.h>
103 #include <ppc/Firmware.h>
104 #include <ppc/low_trace.h>
108 extern vm_map_t ipc_kernel_copy_map
;
109 extern vm_size_t ipc_kmsg_max_vm_space
;
110 extern vm_size_t msg_ool_size_small
;
112 #define MSG_OOL_SIZE_SMALL msg_ool_size_small
114 #if defined(__LP64__)
115 #define MAP_SIZE_DIFFERS(map) (map->max_offset < MACH_VM_MAX_ADDRESS)
116 #define OTHER_OOL_DESCRIPTOR mach_msg_ool_descriptor32_t
117 #define OTHER_OOL_PORTS_DESCRIPTOR mach_msg_ool_ports_descriptor32_t
119 #define MAP_SIZE_DIFFERS(map) (map->max_offset > VM_MAX_ADDRESS)
120 #define OTHER_OOL_DESCRIPTOR mach_msg_ool_descriptor64_t
121 #define OTHER_OOL_PORTS_DESCRIPTOR mach_msg_ool_ports_descriptor64_t
124 #define DESC_SIZE_ADJUSTMENT (sizeof(OTHER_OOL_DESCRIPTOR) - \
125 sizeof(mach_msg_ool_descriptor_t))
127 /* scatter list macros */
129 #define SKIP_PORT_DESCRIPTORS(s, c) \
131 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
133 if ((s)->type.type != MACH_MSG_PORT_DESCRIPTOR) \
138 (s) = MACH_MSG_DESCRIPTOR_NULL; \
142 #define INCREMENT_SCATTER(s, c, d) \
144 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
145 s = (d) ? (mach_msg_descriptor_t *) \
146 ((OTHER_OOL_DESCRIPTOR *)(s) + 1) : \
152 /* zone for cached ipc_kmsg_t structures */
153 zone_t ipc_kmsg_zone
;
156 * Forward declarations
162 void ipc_kmsg_clean_body(
164 mach_msg_type_number_t number
,
165 mach_msg_descriptor_t
*desc
);
167 void ipc_kmsg_clean_partial(
169 mach_msg_type_number_t number
,
170 mach_msg_descriptor_t
*desc
,
174 mach_msg_return_t
ipc_kmsg_copyin_body(
180 * We keep a per-processor cache of kernel message buffers.
181 * The cache saves the overhead/locking of using kalloc/kfree.
182 * The per-processor cache seems to miss less than a per-thread cache,
183 * and it also uses less memory. Access to the cache doesn't
188 * Routine: ipc_kmsg_alloc
190 * Allocate a kernel message structure. If we can get one from
191 * the cache, that is best. Otherwise, allocate a new one.
197 mach_msg_size_t msg_and_trailer_size
)
199 mach_msg_size_t max_expanded_size
;
202 #if !defined(__LP64__)
203 mach_msg_size_t size
= msg_and_trailer_size
- MAX_TRAILER_SIZE
;
207 * Pad the allocation in case we need to expand the
208 * message descrptors for user spaces with pointers larger than
209 * the kernel's own. We don't know how many descriptors
210 * there are yet, so just assume the whole body could be
211 * descriptors (if there could be any at all).
213 * The expansion space is left in front of the header,
214 * because it is easier to pull the header and descriptors
215 * forward as we process them than it is to push all the
219 (size
> sizeof(mach_msg_base_t
)) ?
220 (msg_and_trailer_size
+ DESC_SIZE_ADJUSTMENT
*
221 ((size
- sizeof(mach_msg_base_t
)) /
222 (sizeof(mach_msg_ool_descriptor_t
))))
224 (msg_and_trailer_size
);
226 max_expanded_size
= msg_and_trailer_size
;
229 /* round up for ikm_cache */
230 if (max_expanded_size
< IKM_SAVED_MSG_SIZE
)
231 max_expanded_size
= IKM_SAVED_MSG_SIZE
;
233 if (max_expanded_size
== IKM_SAVED_MSG_SIZE
) {
234 struct ikm_cache
*cache
;
237 disable_preemption();
238 cache
= &PROCESSOR_DATA(current_processor(), ikm_cache
);
239 if ((i
= cache
->avail
) > 0) {
240 assert(i
<= IKM_STASH
);
241 kmsg
= cache
->entries
[--i
];
243 ikm_check_init(kmsg
, max_expanded_size
);
245 kmsg
->ikm_header
= (mach_msg_header_t
*)
246 ((vm_offset_t
)(kmsg
+ 1) +
248 msg_and_trailer_size
);
252 kmsg
= (ipc_kmsg_t
)zalloc(ipc_kmsg_zone
);
254 kmsg
= (ipc_kmsg_t
)kalloc(ikm_plus_overhead(max_expanded_size
));
257 if (kmsg
!= IKM_NULL
) {
258 ikm_init(kmsg
, max_expanded_size
);
259 kmsg
->ikm_header
= (mach_msg_header_t
*)
260 ((vm_offset_t
)(kmsg
+ 1) +
262 msg_and_trailer_size
);
268 * Routine: ipc_kmsg_free
270 * Free a kernel message buffer. If the kms is preallocated
271 * to a port, just "put it back (marked unused)." We have to
272 * do this with the port locked. The port may have its hold
273 * on our message released. In that case, we have to just
274 * revert the message to a traditional one and free it normally.
283 mach_msg_size_t size
= kmsg
->ikm_size
;
287 * Check to see if the message is bound to the port. If so,
288 * mark it not in use. If the port isn't already dead, then
289 * leave the message associated with it. Otherwise, free it.
291 port
= ikm_prealloc_inuse_port(kmsg
);
292 if (port
!= IP_NULL
) {
294 ikm_prealloc_clear_inuse(kmsg
, port
);
295 if (ip_active(port
) && (port
->ip_premsg
== kmsg
)) {
296 assert(IP_PREALLOC(port
));
300 ip_check_unlock(port
); /* May be last reference */
304 * Peek and see if it has to go back in the cache.
306 if (kmsg
->ikm_size
== IKM_SAVED_MSG_SIZE
) {
307 struct ikm_cache
*cache
;
310 disable_preemption();
311 cache
= &PROCESSOR_DATA(current_processor(), ikm_cache
);
312 if ((i
= cache
->avail
) < IKM_STASH
) {
313 cache
->entries
[i
] = kmsg
;
314 cache
->avail
= i
+ 1;
319 zfree(ipc_kmsg_zone
, kmsg
);
322 kfree(kmsg
, ikm_plus_overhead(size
));
327 * Routine: ipc_kmsg_enqueue
334 ipc_kmsg_queue_t queue
,
337 ipc_kmsg_enqueue_macro(queue
, kmsg
);
341 * Routine: ipc_kmsg_dequeue
343 * Dequeue and return a kmsg.
348 ipc_kmsg_queue_t queue
)
352 first
= ipc_kmsg_queue_first(queue
);
354 if (first
!= IKM_NULL
)
355 ipc_kmsg_rmqueue_first_macro(queue
, first
);
361 * Routine: ipc_kmsg_rmqueue
363 * Pull a kmsg out of a queue.
368 ipc_kmsg_queue_t queue
,
371 ipc_kmsg_t next
, prev
;
373 assert(queue
->ikmq_base
!= IKM_NULL
);
375 next
= kmsg
->ikm_next
;
376 prev
= kmsg
->ikm_prev
;
379 assert(prev
== kmsg
);
380 assert(queue
->ikmq_base
== kmsg
);
382 queue
->ikmq_base
= IKM_NULL
;
384 if (queue
->ikmq_base
== kmsg
)
385 queue
->ikmq_base
= next
;
387 next
->ikm_prev
= prev
;
388 prev
->ikm_next
= next
;
390 /* XXX Temporary debug logic */
391 assert(kmsg
->ikm_next
= IKM_BOGUS
);
392 assert(kmsg
->ikm_prev
= IKM_BOGUS
);
396 * Routine: ipc_kmsg_queue_next
398 * Return the kmsg following the given kmsg.
399 * (Or IKM_NULL if it is the last one in the queue.)
404 ipc_kmsg_queue_t queue
,
409 assert(queue
->ikmq_base
!= IKM_NULL
);
411 next
= kmsg
->ikm_next
;
412 if (queue
->ikmq_base
== next
)
419 * Routine: ipc_kmsg_destroy
421 * Destroys a kernel message. Releases all rights,
422 * references, and memory held by the message.
432 ipc_kmsg_queue_t queue
;
436 * ipc_kmsg_clean can cause more messages to be destroyed.
437 * Curtail recursion by queueing messages. If a message
438 * is already queued, then this is a recursive call.
441 queue
= &(current_thread()->ith_messages
);
442 empty
= ipc_kmsg_queue_empty(queue
);
443 ipc_kmsg_enqueue(queue
, kmsg
);
446 /* must leave kmsg in queue while cleaning it */
448 while ((kmsg
= ipc_kmsg_queue_first(queue
)) != IKM_NULL
) {
449 ipc_kmsg_clean(kmsg
);
450 ipc_kmsg_rmqueue(queue
, kmsg
);
457 * Routine: ipc_kmsg_destroy_dest
459 * Destroys a kernel message. Releases all rights,
460 * references, and memory held by the message (including
461 * the destination port reference.
467 ipc_kmsg_destroy_dest(
472 port
= kmsg
->ikm_header
->msgh_remote_port
;
474 ipc_port_release(port
);
475 kmsg
->ikm_header
->msgh_remote_port
= MACH_PORT_NULL
;
476 ipc_kmsg_destroy(kmsg
);
480 * Routine: ipc_kmsg_clean_body
482 * Cleans the body of a kernel message.
483 * Releases all rights, references, and memory.
491 __unused ipc_kmsg_t kmsg
,
492 mach_msg_type_number_t number
,
493 mach_msg_descriptor_t
*saddr
)
495 mach_msg_descriptor_t
*eaddr
;
500 eaddr
= saddr
+ number
;
502 for ( ; saddr
< eaddr
; saddr
++ ) {
504 switch (saddr
->type
.type
) {
506 case MACH_MSG_PORT_DESCRIPTOR
: {
507 mach_msg_port_descriptor_t
*dsc
;
512 * Destroy port rights carried in the message
514 if (!IO_VALID((ipc_object_t
) dsc
->name
))
516 ipc_object_destroy((ipc_object_t
) dsc
->name
, dsc
->disposition
);
519 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
520 case MACH_MSG_OOL_DESCRIPTOR
: {
521 mach_msg_ool_descriptor_t
*dsc
;
523 dsc
= &saddr
->out_of_line
;
526 * Destroy memory carried in the message
528 if (dsc
->size
== 0) {
529 assert(dsc
->address
== (void *) 0);
531 vm_map_copy_discard((vm_map_copy_t
) dsc
->address
);
535 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
536 ipc_object_t
*objects
;
537 mach_msg_type_number_t j
;
538 mach_msg_ool_ports_descriptor_t
*dsc
;
540 dsc
= &saddr
->ool_ports
;
541 objects
= (ipc_object_t
*) dsc
->address
;
543 if (dsc
->count
== 0) {
547 assert(objects
!= (ipc_object_t
*) 0);
549 /* destroy port rights carried in the message */
551 for (j
= 0; j
< dsc
->count
; j
++) {
552 ipc_object_t object
= objects
[j
];
554 if (!IO_VALID(object
))
557 ipc_object_destroy(object
, dsc
->disposition
);
560 /* destroy memory carried in the message */
562 assert(dsc
->count
!= 0);
565 (vm_size_t
) dsc
->count
* sizeof(mach_port_name_t
));
569 printf("cleanup: don't understand this type of descriptor\n");
576 * Routine: ipc_kmsg_clean_partial
578 * Cleans a partially-acquired kernel message.
579 * number is the index of the type descriptor
580 * in the body of the message that contained the error.
581 * If dolast, the memory and port rights in this last
582 * type spec are also cleaned. In that case, number
583 * specifies the number of port rights to clean.
589 ipc_kmsg_clean_partial(
591 mach_msg_type_number_t number
,
592 mach_msg_descriptor_t
*desc
,
597 mach_msg_bits_t mbits
= kmsg
->ikm_header
->msgh_bits
;
599 object
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
600 assert(IO_VALID(object
));
601 ipc_object_destroy(object
, MACH_MSGH_BITS_REMOTE(mbits
));
603 object
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
604 if (IO_VALID(object
))
605 ipc_object_destroy(object
, MACH_MSGH_BITS_LOCAL(mbits
));
608 (void) vm_deallocate(ipc_kernel_copy_map
, paddr
, length
);
611 ipc_kmsg_clean_body(kmsg
, number
, desc
);
615 * Routine: ipc_kmsg_clean
617 * Cleans a kernel message. Releases all rights,
618 * references, and memory held by the message.
628 mach_msg_bits_t mbits
;
630 mbits
= kmsg
->ikm_header
->msgh_bits
;
631 object
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
632 if (IO_VALID(object
))
633 ipc_object_destroy(object
, MACH_MSGH_BITS_REMOTE(mbits
));
635 object
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
636 if (IO_VALID(object
))
637 ipc_object_destroy(object
, MACH_MSGH_BITS_LOCAL(mbits
));
639 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
640 mach_msg_body_t
*body
;
642 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
643 ipc_kmsg_clean_body(kmsg
, body
->msgh_descriptor_count
,
644 (mach_msg_descriptor_t
*)(body
+ 1));
649 * Routine: ipc_kmsg_set_prealloc
651 * Assign a kmsg as a preallocated message buffer to a port.
657 ipc_kmsg_set_prealloc(
661 assert(kmsg
->ikm_prealloc
== IP_NULL
);
663 kmsg
->ikm_prealloc
= IP_NULL
;
664 IP_SET_PREALLOC(port
, kmsg
);
668 * Routine: ipc_kmsg_clear_prealloc
670 * Release the Assignment of a preallocated message buffer from a port.
675 ipc_kmsg_clear_prealloc(
679 assert(kmsg
->ikm_prealloc
== port
);
681 kmsg
->ikm_prealloc
= IP_NULL
;
682 IP_CLEAR_PREALLOC(port
, kmsg
);
688 * Routine: ipc_kmsg_get
690 * Allocates a kernel message buffer.
691 * Copies a user message to the message buffer.
695 * MACH_MSG_SUCCESS Acquired a message buffer.
696 * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
697 * MACH_SEND_MSG_TOO_SMALL Message size not long-word multiple.
698 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
699 * MACH_SEND_INVALID_DATA Couldn't copy message data.
704 mach_vm_address_t msg_addr
,
705 mach_msg_size_t size
,
708 mach_msg_size_t msg_and_trailer_size
;
710 mach_msg_max_trailer_t
*trailer
;
712 if ((size
< sizeof(mach_msg_header_t
)) || (size
& 3))
713 return MACH_SEND_MSG_TOO_SMALL
;
715 msg_and_trailer_size
= size
+ MAX_TRAILER_SIZE
;
717 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
719 if (kmsg
== IKM_NULL
)
720 return MACH_SEND_NO_BUFFER
;
722 if (copyinmsg(msg_addr
, (char *) kmsg
->ikm_header
, size
)) {
724 return MACH_SEND_INVALID_DATA
;
727 kmsg
->ikm_header
->msgh_size
= size
;
730 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
731 * However, the internal size field of the trailer (msgh_trailer_size)
732 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
733 * the cases where no implicit data is requested.
735 trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ size
);
736 trailer
->msgh_sender
= current_thread()->task
->sec_token
;
737 trailer
->msgh_audit
= current_thread()->task
->audit_token
;
738 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
739 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
742 if(trcWork
.traceMask
) dbgTrace(0x1100, (unsigned int)kmsg
->ikm_header
->msgh_id
,
743 (unsigned int)kmsg
->ikm_header
->msgh_remote_port
,
744 (unsigned int)kmsg
->ikm_header
->msgh_local_port
, 0);
747 return MACH_MSG_SUCCESS
;
751 * Routine: ipc_kmsg_get_from_kernel
753 * First checks for a preallocated message
754 * reserved for kernel clients. If not found -
755 * allocates a new kernel message buffer.
756 * Copies a kernel message to the message buffer.
757 * Only resource errors are allowed.
760 * Ports in header are ipc_port_t.
762 * MACH_MSG_SUCCESS Acquired a message buffer.
763 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
767 ipc_kmsg_get_from_kernel(
768 mach_msg_header_t
*msg
,
769 mach_msg_size_t size
,
773 mach_msg_size_t msg_and_trailer_size
;
774 mach_msg_max_trailer_t
*trailer
;
775 ipc_port_t dest_port
;
777 assert(size
>= sizeof(mach_msg_header_t
));
778 // assert((size & 3) == 0);
780 assert(IP_VALID((ipc_port_t
) msg
->msgh_remote_port
));
781 dest_port
= (ipc_port_t
)msg
->msgh_remote_port
;
783 msg_and_trailer_size
= size
+ MAX_TRAILER_SIZE
;
786 * See if the port has a pre-allocated kmsg for kernel
787 * clients. These are set up for those kernel clients
788 * which cannot afford to wait.
790 if (IP_PREALLOC(dest_port
)) {
792 if (!ip_active(dest_port
)) {
793 ip_unlock(dest_port
);
794 return MACH_SEND_NO_BUFFER
;
796 assert(IP_PREALLOC(dest_port
));
797 kmsg
= dest_port
->ip_premsg
;
798 if (msg_and_trailer_size
> kmsg
->ikm_size
) {
799 ip_unlock(dest_port
);
800 return MACH_SEND_TOO_LARGE
;
802 if (ikm_prealloc_inuse(kmsg
)) {
803 ip_unlock(dest_port
);
804 return MACH_SEND_NO_BUFFER
;
806 ikm_prealloc_set_inuse(kmsg
, dest_port
);
807 ip_unlock(dest_port
);
809 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
810 if (kmsg
== IKM_NULL
)
811 return MACH_SEND_NO_BUFFER
;
814 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, size
);
816 kmsg
->ikm_header
->msgh_size
= size
;
819 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
820 * However, the internal size field of the trailer (msgh_trailer_size)
821 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to
822 * optimize the cases where no implicit data is requested.
824 trailer
= (mach_msg_max_trailer_t
*)
825 ((vm_offset_t
)kmsg
->ikm_header
+ size
);
826 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
827 trailer
->msgh_audit
= KERNEL_AUDIT_TOKEN
;
828 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
829 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
832 return MACH_MSG_SUCCESS
;
836 * Routine: ipc_kmsg_send
838 * Send a message. The message holds a reference
839 * for the destination port in the msgh_remote_port field.
841 * If unsuccessful, the caller still has possession of
842 * the message and must do something with it. If successful,
843 * the message is queued, given to a receiver, destroyed,
844 * or handled directly by the kernel via mach_msg.
848 * MACH_MSG_SUCCESS The message was accepted.
849 * MACH_SEND_TIMED_OUT Caller still has message.
850 * MACH_SEND_INTERRUPTED Caller still has message.
855 mach_msg_option_t option
,
856 mach_msg_timeout_t send_timeout
)
860 port
= (ipc_port_t
) kmsg
->ikm_header
->msgh_remote_port
;
861 assert(IP_VALID(port
));
865 if (port
->ip_receiver
== ipc_space_kernel
) {
868 * We can check ip_receiver == ipc_space_kernel
869 * before checking that the port is active because
870 * ipc_port_dealloc_kernel clears ip_receiver
871 * before destroying a kernel port.
873 assert(ip_active(port
));
874 port
->ip_messages
.imq_seqno
++;
877 current_task()->messages_sent
++;
880 * Call the server routine, and get the reply message to send.
882 kmsg
= ipc_kobject_server(kmsg
);
883 if (kmsg
== IKM_NULL
)
884 return MACH_MSG_SUCCESS
;
886 port
= (ipc_port_t
) kmsg
->ikm_header
->msgh_remote_port
;
887 assert(IP_VALID(port
));
889 /* fall thru with reply - same options */
893 * Can't deliver to a dead port.
894 * However, we can pretend it got sent
895 * and was then immediately destroyed.
897 if (!ip_active(port
)) {
899 * We can't let ipc_kmsg_destroy deallocate
900 * the port right, because we might end up
901 * in an infinite loop trying to deliver
902 * a send-once notification.
906 ip_check_unlock(port
);
907 kmsg
->ikm_header
->msgh_remote_port
= MACH_PORT_NULL
;
908 ipc_kmsg_destroy(kmsg
);
909 return MACH_MSG_SUCCESS
;
912 if (kmsg
->ikm_header
->msgh_bits
& MACH_MSGH_BITS_CIRCULAR
) {
915 /* don't allow the creation of a circular loop */
917 ipc_kmsg_destroy(kmsg
);
918 return MACH_MSG_SUCCESS
;
922 * We have a valid message and a valid reference on the port.
923 * we can unlock the port and call mqueue_send() on it's message
927 return (ipc_mqueue_send(&port
->ip_messages
, kmsg
, option
, send_timeout
));
931 * Routine: ipc_kmsg_put
933 * Copies a message buffer to a user message.
934 * Copies only the specified number of bytes.
935 * Frees the message buffer.
937 * Nothing locked. The message buffer must have clean
940 * MACH_MSG_SUCCESS Copied data out of message buffer.
941 * MACH_RCV_INVALID_DATA Couldn't copy to user message.
946 mach_vm_address_t msg_addr
,
948 mach_msg_size_t size
)
950 mach_msg_return_t mr
;
952 if (copyoutmsg((const char *) kmsg
->ikm_header
, msg_addr
, size
))
953 mr
= MACH_RCV_INVALID_DATA
;
955 mr
= MACH_MSG_SUCCESS
;
962 * Routine: ipc_kmsg_put_to_kernel
964 * Copies a message buffer to a kernel message.
965 * Frees the message buffer.
972 ipc_kmsg_put_to_kernel(
973 mach_msg_header_t
*msg
,
975 mach_msg_size_t size
)
977 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, size
);
983 * Routine: ipc_kmsg_copyin_header
985 * "Copy-in" port rights in the header of a message.
986 * Operates atomically; if it doesn't succeed the
987 * message header and the space are left untouched.
988 * If it does succeed the remote/local port fields
989 * contain object pointers instead of port names,
990 * and the bits field is updated. The destination port
991 * will be a valid port pointer.
993 * The notify argument implements the MACH_SEND_CANCEL option.
994 * If it is not MACH_PORT_NULL, it should name a receive right.
995 * If the processing of the destination port would generate
996 * a port-deleted notification (because the right for the
997 * destination port is destroyed and it had a request for
998 * a dead-name notification registered), and the port-deleted
999 * notification would be sent to the named receive right,
1000 * then it isn't sent and the send-once right for the notify
1001 * port is quietly destroyed.
1006 * MACH_MSG_SUCCESS Successful copyin.
1007 * MACH_SEND_INVALID_HEADER
1008 * Illegal value in the message header bits.
1009 * MACH_SEND_INVALID_DEST The space is dead.
1010 * MACH_SEND_INVALID_NOTIFY
1011 * Notify is non-null and doesn't name a receive right.
1012 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
1013 * MACH_SEND_INVALID_DEST Can't copyin destination port.
1014 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
1015 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
1016 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
1020 ipc_kmsg_copyin_header(
1021 mach_msg_header_t
*msg
,
1023 mach_port_name_t notify
)
1025 mach_msg_bits_t mbits
= msg
->msgh_bits
& MACH_MSGH_BITS_USER
;
1026 mach_port_name_t dest_name
= (mach_port_name_t
)msg
->msgh_remote_port
;
1027 mach_port_name_t reply_name
= (mach_port_name_t
)msg
->msgh_local_port
;
1030 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
1031 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
1032 ipc_object_t dest_port
, reply_port
;
1033 ipc_port_t dest_soright
, reply_soright
;
1034 ipc_port_t notify_port
;
1036 if ((mbits
!= msg
->msgh_bits
) ||
1037 (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type
)) ||
1038 ((reply_type
== 0) ?
1039 (reply_name
!= MACH_PORT_NULL
) :
1040 !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type
)))
1041 return MACH_SEND_INVALID_HEADER
;
1043 reply_soright
= IP_NULL
; /* in case we go to invalid dest early */
1045 is_write_lock(space
);
1046 if (!space
->is_active
)
1049 if (!MACH_PORT_VALID(dest_name
))
1052 if (notify
!= MACH_PORT_NULL
) {
1055 if ((entry
= ipc_entry_lookup(space
, notify
)) == IE_NULL
) {
1056 is_write_unlock(space
);
1057 return MACH_SEND_INVALID_NOTIFY
;
1059 if((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) == 0) {
1060 is_write_unlock(space
);
1061 return MACH_SEND_INVALID_NOTIFY
;
1064 notify_port
= (ipc_port_t
) entry
->ie_object
;
1066 notify_port
= IP_NULL
;
1068 if (dest_name
== reply_name
) {
1070 mach_port_name_t name
= dest_name
;
1073 * Destination and reply ports are the same!
1074 * This is a little tedious to make atomic, because
1075 * there are 25 combinations of dest_type/reply_type.
1076 * However, most are easy. If either is move-sonce,
1077 * then there must be an error. If either are
1078 * make-send or make-sonce, then we must be looking
1079 * at a receive right so the port can't die.
1080 * The hard cases are the combinations of
1081 * copy-send and make-send.
1084 entry
= ipc_entry_lookup(space
, name
);
1085 if (entry
== IE_NULL
)
1088 assert(reply_type
!= 0); /* because name not null */
1090 if (!ipc_right_copyin_check(space
, name
, entry
, reply_type
))
1093 if ((dest_type
== MACH_MSG_TYPE_MOVE_SEND_ONCE
) ||
1094 (reply_type
== MACH_MSG_TYPE_MOVE_SEND_ONCE
)) {
1096 * Why must there be an error? To get a valid
1097 * destination, this entry must name a live
1098 * port (not a dead name or dead port). However
1099 * a successful move-sonce will destroy a
1100 * live entry. Therefore the other copyin,
1101 * whatever it is, would fail. We've already
1102 * checked for reply port errors above,
1103 * so report a destination error.
1107 } else if ((dest_type
== MACH_MSG_TYPE_MAKE_SEND
) ||
1108 (dest_type
== MACH_MSG_TYPE_MAKE_SEND_ONCE
) ||
1109 (reply_type
== MACH_MSG_TYPE_MAKE_SEND
) ||
1110 (reply_type
== MACH_MSG_TYPE_MAKE_SEND_ONCE
)) {
1111 kr
= ipc_right_copyin(space
, name
, entry
,
1113 &dest_port
, &dest_soright
);
1114 if (kr
!= KERN_SUCCESS
)
1118 * Either dest or reply needs a receive right.
1119 * We know the receive right is there, because
1120 * of the copyin_check and copyin calls. Hence
1121 * the port is not in danger of dying. If dest
1122 * used the receive right, then the right needed
1123 * by reply (and verified by copyin_check) will
1127 assert(IO_VALID(dest_port
));
1128 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
1129 assert(dest_soright
== IP_NULL
);
1131 kr
= ipc_right_copyin(space
, name
, entry
,
1133 &reply_port
, &reply_soright
);
1135 assert(kr
== KERN_SUCCESS
);
1136 assert(reply_port
== dest_port
);
1137 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
1138 assert(reply_soright
== IP_NULL
);
1139 } else if ((dest_type
== MACH_MSG_TYPE_COPY_SEND
) &&
1140 (reply_type
== MACH_MSG_TYPE_COPY_SEND
)) {
1142 * To make this atomic, just do one copy-send,
1143 * and dup the send right we get out.
1146 kr
= ipc_right_copyin(space
, name
, entry
,
1148 &dest_port
, &dest_soright
);
1149 if (kr
!= KERN_SUCCESS
)
1152 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND
);
1153 assert(dest_soright
== IP_NULL
);
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
);
1163 reply_soright
= IP_NULL
;
1164 } else if ((dest_type
== MACH_MSG_TYPE_MOVE_SEND
) &&
1165 (reply_type
== MACH_MSG_TYPE_MOVE_SEND
)) {
1167 * This is an easy case. Just use our
1168 * handy-dandy special-purpose copyin call
1169 * to get two send rights for the price of one.
1172 kr
= ipc_right_copyin_two(space
, name
, entry
,
1173 &dest_port
, &dest_soright
);
1174 if (kr
!= KERN_SUCCESS
)
1177 /* the entry might need to be deallocated */
1178 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1179 ipc_entry_dealloc(space
, name
, entry
);
1181 reply_port
= dest_port
;
1182 reply_soright
= IP_NULL
;
1186 assert(((dest_type
== MACH_MSG_TYPE_COPY_SEND
) &&
1187 (reply_type
== MACH_MSG_TYPE_MOVE_SEND
)) ||
1188 ((dest_type
== MACH_MSG_TYPE_MOVE_SEND
) &&
1189 (reply_type
== MACH_MSG_TYPE_COPY_SEND
)));
1192 * To make this atomic, just do a move-send,
1193 * and dup the send right we get out.
1196 kr
= ipc_right_copyin(space
, name
, entry
,
1197 MACH_MSG_TYPE_MOVE_SEND
, FALSE
,
1198 &dest_port
, &soright
);
1199 if (kr
!= KERN_SUCCESS
)
1202 /* the entry might need to be deallocated */
1204 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1205 ipc_entry_dealloc(space
, name
, entry
);
1208 * It's OK if the port we got is dead now,
1209 * so reply_port is IP_DEAD, because the msg
1210 * won't go anywhere anyway.
1213 reply_port
= (ipc_object_t
)
1214 ipc_port_copy_send((ipc_port_t
) dest_port
);
1216 if (dest_type
== MACH_MSG_TYPE_MOVE_SEND
) {
1217 dest_soright
= soright
;
1218 reply_soright
= IP_NULL
;
1220 dest_soright
= IP_NULL
;
1221 reply_soright
= soright
;
1224 } else if (!MACH_PORT_VALID(reply_name
)) {
1228 * No reply port! This is an easy case
1229 * to make atomic. Just copyin the destination.
1232 entry
= ipc_entry_lookup(space
, dest_name
);
1233 if (entry
== IE_NULL
)
1236 kr
= ipc_right_copyin(space
, dest_name
, entry
,
1238 &dest_port
, &dest_soright
);
1239 if (kr
!= KERN_SUCCESS
)
1242 /* the entry might need to be deallocated */
1244 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1245 ipc_entry_dealloc(space
, dest_name
, entry
);
1247 reply_port
= (ipc_object_t
) reply_name
;
1248 reply_soright
= IP_NULL
;
1250 ipc_entry_t dest_entry
, reply_entry
;
1253 * This is the tough case to make atomic.
1254 * The difficult problem is serializing with port death.
1255 * At the time we copyin dest_port, it must be alive.
1256 * If reply_port is alive when we copyin it, then
1257 * we are OK, because we serialize before the death
1258 * of both ports. Assume reply_port is dead at copyin.
1259 * Then if dest_port dies/died after reply_port died,
1260 * we are OK, because we serialize between the death
1261 * of the two ports. So the bad case is when dest_port
1262 * dies after its copyin, reply_port dies before its
1263 * copyin, and dest_port dies before reply_port. Then
1264 * the copyins operated as if dest_port was alive
1265 * and reply_port was dead, which shouldn't have happened
1266 * because they died in the other order.
1268 * Note that it is easy for a user task to tell if
1269 * a copyin happened before or after a port died.
1270 * For example, suppose both dest and reply are
1271 * send-once rights (types are both move-sonce) and
1272 * both rights have dead-name requests registered.
1273 * If a port dies before copyin, a dead-name notification
1274 * is generated and the dead name's urefs are incremented,
1275 * and if the copyin happens first, a port-deleted
1276 * notification is generated.
1278 * Note that although the entries are different,
1279 * dest_port and reply_port might still be the same.
1281 * JMM - The code to handle this was too expensive and, anyway,
1282 * we intend to separate the dest lookup from the reply copyin
1283 * by a wide margin, so the user will have to learn to deal!
1284 * I will be making the change soon!
1287 dest_entry
= ipc_entry_lookup(space
, dest_name
);
1288 if (dest_entry
== IE_NULL
)
1291 reply_entry
= ipc_entry_lookup(space
, reply_name
);
1292 if (reply_entry
== IE_NULL
)
1295 assert(dest_entry
!= reply_entry
); /* names are not equal */
1296 assert(reply_type
!= 0); /* because reply_name not null */
1298 if (!ipc_right_copyin_check(space
, reply_name
, reply_entry
,
1302 kr
= ipc_right_copyin(space
, dest_name
, dest_entry
,
1304 &dest_port
, &dest_soright
);
1305 if (kr
!= KERN_SUCCESS
)
1308 assert(IO_VALID(dest_port
));
1310 kr
= ipc_right_copyin(space
, reply_name
, reply_entry
,
1312 &reply_port
, &reply_soright
);
1314 assert(kr
== KERN_SUCCESS
);
1316 /* the entries might need to be deallocated */
1318 if (IE_BITS_TYPE(reply_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1319 ipc_entry_dealloc(space
, reply_name
, reply_entry
);
1321 if (IE_BITS_TYPE(dest_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
1322 ipc_entry_dealloc(space
, dest_name
, dest_entry
);
1326 * At this point, dest_port, reply_port,
1327 * dest_soright, reply_soright are all initialized.
1328 * Any defunct entries have been deallocated.
1329 * The space is still write-locked, and we need to
1330 * make the MACH_SEND_CANCEL check. The notify_port pointer
1331 * is still usable, because the copyin code above won't ever
1332 * deallocate a receive right, so its entry still exists
1333 * and holds a ref. Note notify_port might even equal
1334 * dest_port or reply_port.
1337 if ((notify
!= MACH_PORT_NULL
) &&
1338 (dest_soright
== notify_port
)) {
1339 ipc_port_release_sonce(dest_soright
);
1340 dest_soright
= IP_NULL
;
1343 is_write_unlock(space
);
1345 if (dest_soright
!= IP_NULL
)
1346 ipc_notify_port_deleted(dest_soright
, dest_name
);
1348 if (reply_soright
!= IP_NULL
)
1349 ipc_notify_port_deleted(reply_soright
, reply_name
);
1351 dest_type
= ipc_object_copyin_type(dest_type
);
1352 reply_type
= ipc_object_copyin_type(reply_type
);
1354 msg
->msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
1355 MACH_MSGH_BITS(dest_type
, reply_type
));
1356 msg
->msgh_remote_port
= (ipc_port_t
)dest_port
;
1357 msg
->msgh_local_port
= (ipc_port_t
)reply_port
;
1359 return MACH_MSG_SUCCESS
;
1362 is_write_unlock(space
);
1363 return MACH_SEND_INVALID_REPLY
;
1366 is_write_unlock(space
);
1367 if (reply_soright
!= IP_NULL
)
1368 ipc_notify_port_deleted(reply_soright
, reply_name
);
1369 return MACH_SEND_INVALID_DEST
;
1373 * Routine: ipc_kmsg_copyin_body
1375 * "Copy-in" port rights and out-of-line memory
1376 * in the message body.
1378 * In all failure cases, the message is left holding
1379 * no rights or memory. However, the message buffer
1380 * is not deallocated. If successful, the message
1381 * contains a valid destination port.
1385 * MACH_MSG_SUCCESS Successful copyin.
1386 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
1387 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
1388 * MACH_SEND_INVALID_TYPE Bad type specification.
1389 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
1390 * MACH_SEND_INVALID_RT_OOL_SIZE OOL Buffer too large for RT
1391 * MACH_MSG_INVALID_RT_DESCRIPTOR Dealloc and RT are incompatible
1394 #define DESC_COUNT_SMALL 64
1397 ipc_kmsg_copyin_body(
1403 mach_msg_body_t
*body
;
1404 mach_msg_descriptor_t
*daddr
, *naddr
;
1405 mach_msg_type_number_t dsc_count
;
1406 boolean_t differs
= MAP_SIZE_DIFFERS(map
);
1407 boolean_t
complex = FALSE
;
1408 vm_size_t space_needed
= 0;
1409 vm_size_t desc_size_space
[DESC_COUNT_SMALL
];
1410 vm_size_t
*user_desc_sizes
= NULL
;
1411 vm_offset_t paddr
= 0;
1412 vm_map_copy_t copy
= VM_MAP_COPY_NULL
;
1414 mach_msg_type_number_t i
;
1415 mach_msg_return_t mr
= MACH_MSG_SUCCESS
;
1418 * Determine if the target is a kernel port.
1420 dest
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
1421 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
1422 naddr
= (mach_msg_descriptor_t
*) (body
+ 1);
1424 dsc_count
= body
->msgh_descriptor_count
;
1426 return MACH_MSG_SUCCESS
;
1429 user_desc_sizes
= (dsc_count
<= DESC_COUNT_SMALL
) ?
1430 &desc_size_space
: kalloc(dsc_count
* sizeof(vm_size_t
));
1431 if (user_desc_sizes
== NULL
) {
1432 ipc_kmsg_clean_partial(kmsg
,0,0,0,0);
1433 return KERN_RESOURCE_SHORTAGE
;
1438 * Make an initial pass to determine kernal VM space requirements for
1439 * physical copies and possible contraction of the descriptors from
1440 * processes with pointers larger than the kernel's.
1443 for (i
= 0; i
< dsc_count
; i
++) {
1446 /* make sure the descriptor fits in the message */
1448 switch (daddr
->type
.type
) {
1449 case MACH_MSG_OOL_DESCRIPTOR
:
1450 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1451 case MACH_MSG_OOL_PORTS_DESCRIPTOR
:
1452 user_desc_sizes
[i
] = sizeof(OTHER_OOL_DESCRIPTOR
);
1455 user_desc_sizes
[i
] = sizeof(*daddr
);
1458 naddr
= (mach_msg_descriptor_t
*)
1459 ((vm_offset_t
)daddr
+ user_desc_sizes
[i
]);
1464 if (naddr
> (mach_msg_descriptor_t
*)
1465 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
)) {
1466 ipc_kmsg_clean_partial(kmsg
,0,0,0,0);
1467 mr
= MACH_SEND_MSG_TOO_SMALL
;
1471 switch (daddr
->type
.type
) {
1472 mach_msg_size_t size
;
1474 case MACH_MSG_OOL_DESCRIPTOR
:
1475 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1477 ((OTHER_OOL_DESCRIPTOR
*)daddr
)->size
:
1478 daddr
->out_of_line
.size
;
1480 if (daddr
->out_of_line
.copy
!= MACH_MSG_PHYSICAL_COPY
&&
1481 daddr
->out_of_line
.copy
!= MACH_MSG_VIRTUAL_COPY
) {
1483 * Invalid copy option
1485 ipc_kmsg_clean_partial(kmsg
,0,0,0,0);
1486 mr
= MACH_SEND_INVALID_TYPE
;
1490 if ((size
>= MSG_OOL_SIZE_SMALL
) &&
1491 (daddr
->out_of_line
.copy
== MACH_MSG_PHYSICAL_COPY
) &&
1492 !(daddr
->out_of_line
.deallocate
)) {
1495 * Out-of-line memory descriptor, accumulate kernel
1496 * memory requirements
1498 space_needed
+= round_page(size
);
1499 if (space_needed
> ipc_kmsg_max_vm_space
) {
1502 * Per message kernel memory limit exceeded
1504 ipc_kmsg_clean_partial(kmsg
,0,0,0,0);
1505 mr
= MACH_MSG_VM_KERNEL
;
1513 * Allocate space in the pageable kernel ipc copy map for all the
1514 * ool data that is to be physically copied. Map is marked wait for
1518 if (vm_allocate(ipc_kernel_copy_map
, &paddr
, space_needed
, VM_FLAGS_ANYWHERE
) !=
1520 ipc_kmsg_clean_partial(kmsg
,0,0,0,0);
1521 mr
= MACH_MSG_VM_KERNEL
;
1527 * handle the OOL regions and port descriptors.
1528 * We process them in reverse order starting with the last one
1529 * scanned above. That way, we can compact them up against
1530 * the message body (if the user-descriptor size is larger than
1531 * the kernel representation).
1536 switch (daddr
->type
.type
) {
1538 /* port descriptors are the same size everywhere, how nice */
1539 case MACH_MSG_PORT_DESCRIPTOR
: {
1540 mach_msg_type_name_t user_disp
;
1541 mach_msg_type_name_t result_disp
;
1542 mach_port_name_t name
;
1543 ipc_object_t object
;
1544 volatile mach_msg_port_descriptor_t
*dsc
;
1545 volatile mach_msg_port_descriptor_t
*user_dsc
;
1547 user_dsc
= &daddr
->port
;
1550 user_disp
= user_dsc
->disposition
;
1551 result_disp
= ipc_object_copyin_type(user_disp
);
1553 name
= (mach_port_name_t
)user_dsc
->name
;
1554 if (MACH_PORT_VALID(name
)) {
1556 kr
= ipc_object_copyin(space
, name
, user_disp
, &object
);
1557 if (kr
!= KERN_SUCCESS
) {
1558 mr
= MACH_SEND_INVALID_RIGHT
;
1562 if ((result_disp
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1563 ipc_port_check_circularity((ipc_port_t
) object
,
1564 (ipc_port_t
) dest
)) {
1565 kmsg
->ikm_header
->msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1567 dsc
->name
= (ipc_port_t
) object
;
1569 dsc
->name
= (mach_port_t
)name
;
1571 dsc
->disposition
= result_disp
;
1572 dsc
->type
= MACH_MSG_PORT_DESCRIPTOR
;
1577 /* out of line descriptors differ in size between 32 and 64 bit processes */
1578 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1579 case MACH_MSG_OOL_DESCRIPTOR
: {
1582 mach_msg_copy_options_t copy_options
;
1583 mach_vm_offset_t addr
;
1584 mach_msg_descriptor_type_t dsc_type
;
1586 volatile mach_msg_ool_descriptor_t
*dsc
;
1589 volatile OTHER_OOL_DESCRIPTOR
*user_dsc
;
1591 user_dsc
= (OTHER_OOL_DESCRIPTOR
*)&daddr
->out_of_line
;
1592 addr
= (mach_vm_offset_t
) user_dsc
->address
;
1593 length
= user_dsc
->size
;
1594 dealloc
= user_dsc
->deallocate
;
1595 copy_options
= user_dsc
->copy
;
1596 dsc_type
= user_dsc
->type
;
1598 volatile mach_msg_ool_descriptor_t
*user_dsc
;
1600 user_dsc
= &daddr
->out_of_line
;
1601 addr
= CAST_USER_ADDR_T(user_dsc
->address
);
1602 dealloc
= user_dsc
->deallocate
;
1603 copy_options
= user_dsc
->copy
;
1604 dsc_type
= user_dsc
->type
;
1605 length
= user_dsc
->size
;
1608 dsc
= &naddr
->out_of_line
;
1610 dsc
->deallocate
= dealloc
;
1611 dsc
->copy
= copy_options
;
1612 dsc
->type
= dsc_type
;
1616 } else if ((length
>= MSG_OOL_SIZE_SMALL
) &&
1617 (copy_options
== MACH_MSG_PHYSICAL_COPY
) && !dealloc
) {
1620 * If the request is a physical copy and the source
1621 * is not being deallocated, then allocate space
1622 * in the kernel's pageable ipc copy map and copy
1623 * the data in. The semantics guarantee that the
1624 * data will have been physically copied before
1625 * the send operation terminates. Thus if the data
1626 * is not being deallocated, we must be prepared
1627 * to page if the region is sufficiently large.
1629 if (copyin(addr
, (char *) paddr
, length
)) {
1630 mr
= MACH_SEND_INVALID_MEMORY
;
1635 * The kernel ipc copy map is marked no_zero_fill.
1636 * If the transfer is not a page multiple, we need
1637 * to zero fill the balance.
1639 if (!page_aligned(length
)) {
1640 (void) memset((void *) (paddr
+ length
), 0,
1641 round_page(length
) - length
);
1643 if (vm_map_copyin(ipc_kernel_copy_map
, (vm_map_address_t
)paddr
,
1644 (vm_map_size_t
)length
, TRUE
, ©
) != KERN_SUCCESS
) {
1645 mr
= MACH_MSG_VM_KERNEL
;
1648 dsc
->address
= (void *) copy
;
1649 paddr
+= round_page(length
);
1650 space_needed
-= round_page(length
);
1654 * Make a vm_map_copy_t of the of the data. If the
1655 * data is small, this will do an optimized physical
1656 * copy. Otherwise, it will do a virtual copy.
1658 * NOTE: A virtual copy is OK if the original is being
1659 * deallocted, even if a physical copy was requested.
1661 kr
= vm_map_copyin(map
, addr
,
1662 (vm_map_size_t
)length
, dealloc
, ©
);
1663 if (kr
!= KERN_SUCCESS
) {
1664 mr
= (kr
== KERN_RESOURCE_SHORTAGE
) ?
1665 MACH_MSG_VM_KERNEL
:
1666 MACH_SEND_INVALID_MEMORY
;
1669 dsc
->address
= (void *) copy
;
1674 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
1677 ipc_object_t
*objects
;
1679 mach_vm_offset_t addr
;
1680 mach_msg_type_name_t user_disp
;
1681 mach_msg_type_name_t result_disp
;
1682 mach_msg_type_number_t count
;
1683 mach_msg_copy_options_t copy_option
;
1684 boolean_t deallocate
;
1686 volatile mach_msg_ool_ports_descriptor_t
*dsc
;
1689 volatile OTHER_OOL_PORTS_DESCRIPTOR
*user_dsc
;
1691 user_dsc
= (OTHER_OOL_PORTS_DESCRIPTOR
*)&daddr
->ool_ports
;
1692 addr
= (mach_vm_offset_t
)user_dsc
->address
;
1693 count
= user_dsc
->count
;
1694 deallocate
= user_dsc
->deallocate
;
1695 copy_option
= user_dsc
->copy
;
1696 user_disp
= user_dsc
->disposition
;
1698 volatile mach_msg_ool_ports_descriptor_t
*user_dsc
;
1700 user_dsc
= &daddr
->ool_ports
;
1701 addr
= CAST_USER_ADDR_T(user_dsc
->address
);
1702 count
= user_dsc
->count
;
1703 deallocate
= user_dsc
->deallocate
;
1704 copy_option
= user_dsc
->copy
;
1705 user_disp
= user_dsc
->disposition
;
1708 dsc
= &naddr
->ool_ports
;
1709 dsc
->deallocate
= deallocate
;
1710 dsc
->copy
= copy_option
;
1711 dsc
->type
= daddr
->type
.type
;
1714 /* calculate length of data in bytes, rounding up */
1715 length
= count
* sizeof(mach_port_name_t
);
1719 dsc
->address
= (void *) 0;
1723 data
= kalloc(length
);
1726 mr
= MACH_SEND_NO_BUFFER
;
1730 if (copyinmap(map
, addr
, data
, length
) != KERN_SUCCESS
) {
1731 kfree(data
, length
);
1732 mr
= MACH_SEND_INVALID_MEMORY
;
1737 (void) mach_vm_deallocate(map
, addr
, (mach_vm_size_t
)length
);
1740 dsc
->address
= data
;
1742 result_disp
= ipc_object_copyin_type(user_disp
);
1743 dsc
->disposition
= result_disp
;
1745 objects
= (ipc_object_t
*) data
;
1747 for ( j
= 0; j
< count
; j
++) {
1748 mach_port_name_t port
= (mach_port_name_t
) objects
[j
];
1749 ipc_object_t object
;
1751 if (!MACH_PORT_VALID(port
))
1754 kr
= ipc_object_copyin(space
, port
, user_disp
, &object
);
1756 if (kr
!= KERN_SUCCESS
) {
1759 for(k
= 0; k
< j
; k
++) {
1760 object
= objects
[k
];
1761 if (IPC_OBJECT_VALID(object
))
1762 ipc_object_destroy(object
, result_disp
);
1764 kfree(data
, length
);
1765 mr
= MACH_SEND_INVALID_RIGHT
;
1769 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1770 ipc_port_check_circularity(
1771 (ipc_port_t
) object
,
1773 kmsg
->ikm_header
->msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
1775 objects
[j
] = object
;
1783 * Invalid descriptor
1785 mr
= MACH_SEND_INVALID_TYPE
;
1790 if (MACH_MSG_SUCCESS
!= mr
) {
1791 ipc_kmsg_clean_partial(kmsg
, dsc_count
- i
,
1792 naddr
+ 1, paddr
, space_needed
);
1798 (daddr
= (differs
) ? (mach_msg_descriptor_t
*)((vm_offset_t
)(daddr
) -
1799 user_desc_sizes
[i
- 1]) : daddr
- 1)
1804 kmsg
->ikm_header
->msgh_bits
&= ~MACH_MSGH_BITS_COMPLEX
;
1807 if (differs
&& naddr
!= daddr
) {
1808 mach_msg_base_t
*old_base
= (mach_msg_base_t
*)kmsg
->ikm_header
;
1809 mach_msg_base_t
*new_base
= (mach_msg_base_t
*)naddr
- 1;
1811 memmove(new_base
, old_base
, sizeof(mach_msg_base_t
));
1812 new_base
->header
.msgh_size
-= (vm_offset_t
)naddr
- (vm_offset_t
)daddr
;
1813 kmsg
->ikm_header
= &new_base
->header
;
1817 if (differs
&& dsc_count
> DESC_COUNT_SMALL
)
1818 kfree(user_desc_sizes
, body
->msgh_descriptor_count
* sizeof(vm_size_t
));
1825 * Routine: ipc_kmsg_copyin
1827 * "Copy-in" port rights and out-of-line memory
1830 * In all failure cases, the message is left holding
1831 * no rights or memory. However, the message buffer
1832 * is not deallocated. If successful, the message
1833 * contains a valid destination port.
1837 * MACH_MSG_SUCCESS Successful copyin.
1838 * MACH_SEND_INVALID_HEADER
1839 * Illegal value in the message header bits.
1840 * MACH_SEND_INVALID_NOTIFY Bad notify port.
1841 * MACH_SEND_INVALID_DEST Can't copyin destination port.
1842 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
1843 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
1844 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
1845 * MACH_SEND_INVALID_TYPE Bad type specification.
1846 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
1854 mach_port_name_t notify
)
1856 mach_msg_return_t mr
;
1858 mr
= ipc_kmsg_copyin_header(kmsg
->ikm_header
, space
, notify
);
1859 if (mr
!= MACH_MSG_SUCCESS
)
1862 if ((kmsg
->ikm_header
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
1863 return MACH_MSG_SUCCESS
;
1865 return( ipc_kmsg_copyin_body( kmsg
, space
, map
) );
1869 * Routine: ipc_kmsg_copyin_from_kernel
1871 * "Copy-in" port rights and out-of-line memory
1872 * in a message sent from the kernel.
1874 * Because the message comes from the kernel,
1875 * the implementation assumes there are no errors
1876 * or peculiarities in the message.
1878 * Returns TRUE if queueing the message
1879 * would result in a circularity.
1885 ipc_kmsg_copyin_from_kernel(
1888 mach_msg_bits_t bits
= kmsg
->ikm_header
->msgh_bits
;
1889 mach_msg_type_name_t rname
= MACH_MSGH_BITS_REMOTE(bits
);
1890 mach_msg_type_name_t lname
= MACH_MSGH_BITS_LOCAL(bits
);
1891 ipc_object_t remote
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
1892 ipc_object_t local
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
1894 /* translate the destination and reply ports */
1896 ipc_object_copyin_from_kernel(remote
, rname
);
1897 if (IO_VALID(local
))
1898 ipc_object_copyin_from_kernel(local
, lname
);
1901 * The common case is a complex message with no reply port,
1902 * because that is what the memory_object interface uses.
1905 if (bits
== (MACH_MSGH_BITS_COMPLEX
|
1906 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0))) {
1907 bits
= (MACH_MSGH_BITS_COMPLEX
|
1908 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND
, 0));
1910 kmsg
->ikm_header
->msgh_bits
= bits
;
1912 bits
= (MACH_MSGH_BITS_OTHER(bits
) |
1913 MACH_MSGH_BITS(ipc_object_copyin_type(rname
),
1914 ipc_object_copyin_type(lname
)));
1916 kmsg
->ikm_header
->msgh_bits
= bits
;
1917 if ((bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
1921 mach_msg_descriptor_t
*saddr
, *eaddr
;
1922 mach_msg_body_t
*body
;
1924 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
1925 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
1926 eaddr
= (mach_msg_descriptor_t
*) saddr
+ body
->msgh_descriptor_count
;
1928 for ( ; saddr
< eaddr
; saddr
++) {
1930 switch (saddr
->type
.type
) {
1932 case MACH_MSG_PORT_DESCRIPTOR
: {
1933 mach_msg_type_name_t name
;
1934 ipc_object_t object
;
1935 mach_msg_port_descriptor_t
*dsc
;
1939 /* this is really the type SEND, SEND_ONCE, etc. */
1940 name
= dsc
->disposition
;
1941 object
= (ipc_object_t
) dsc
->name
;
1942 dsc
->disposition
= ipc_object_copyin_type(name
);
1944 if (!IO_VALID(object
)) {
1948 ipc_object_copyin_from_kernel(object
, name
);
1950 /* CDY avoid circularity when the destination is also */
1951 /* the kernel. This check should be changed into an */
1952 /* assert when the new kobject model is in place since*/
1953 /* ports will not be used in kernel to kernel chats */
1955 if (((ipc_port_t
)remote
)->ip_receiver
!= ipc_space_kernel
) {
1956 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1957 ipc_port_check_circularity((ipc_port_t
) object
,
1958 (ipc_port_t
) remote
)) {
1959 kmsg
->ikm_header
->msgh_bits
|=
1960 MACH_MSGH_BITS_CIRCULAR
;
1965 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1966 case MACH_MSG_OOL_DESCRIPTOR
: {
1968 * The sender should supply ready-made memory, i.e.
1969 * a vm_map_copy_t, so we don't need to do anything.
1973 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
1974 ipc_object_t
*objects
;
1976 mach_msg_type_name_t name
;
1977 mach_msg_ool_ports_descriptor_t
*dsc
;
1979 dsc
= &saddr
->ool_ports
;
1981 /* this is really the type SEND, SEND_ONCE, etc. */
1982 name
= dsc
->disposition
;
1983 dsc
->disposition
= ipc_object_copyin_type(name
);
1985 objects
= (ipc_object_t
*) dsc
->address
;
1987 for ( j
= 0; j
< dsc
->count
; j
++) {
1988 ipc_object_t object
= objects
[j
];
1990 if (!IO_VALID(object
))
1993 ipc_object_copyin_from_kernel(object
, name
);
1995 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
1996 ipc_port_check_circularity(
1997 (ipc_port_t
) object
,
1998 (ipc_port_t
) remote
))
1999 kmsg
->ikm_header
->msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
2005 panic("ipc_kmsg_copyin_from_kernel: bad descriptor");
2006 #endif /* MACH_ASSERT */
2014 * Routine: ipc_kmsg_copyout_header
2016 * "Copy-out" port rights in the header of a message.
2017 * Operates atomically; if it doesn't succeed the
2018 * message header and the space are left untouched.
2019 * If it does succeed the remote/local port fields
2020 * contain port names instead of object pointers,
2021 * and the bits field is updated.
2023 * The notify argument implements the MACH_RCV_NOTIFY option.
2024 * If it is not MACH_PORT_NULL, it should name a receive right.
2025 * If the process of receiving the reply port creates a
2026 * new right in the receiving task, then the new right is
2027 * automatically registered for a dead-name notification,
2028 * with the notify port supplying the send-once right.
2032 * MACH_MSG_SUCCESS Copied out port rights.
2033 * MACH_RCV_INVALID_NOTIFY
2034 * Notify is non-null and doesn't name a receive right.
2035 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
2036 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
2037 * The space is dead.
2038 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
2039 * No room in space for another name.
2040 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
2041 * Couldn't allocate memory for the reply port.
2042 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
2043 * Couldn't allocate memory for the dead-name request.
2047 ipc_kmsg_copyout_header(
2048 mach_msg_header_t
*msg
,
2050 mach_port_name_t notify
)
2052 mach_msg_bits_t mbits
= msg
->msgh_bits
;
2053 ipc_port_t dest
= (ipc_port_t
) msg
->msgh_remote_port
;
2055 assert(IP_VALID(dest
));
2058 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
2059 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
2060 ipc_port_t reply
= (ipc_port_t
) msg
->msgh_local_port
;
2061 mach_port_name_t dest_name
, reply_name
;
2063 if (IP_VALID(reply
)) {
2064 ipc_port_t notify_port
;
2069 * Handling notify (for MACH_RCV_NOTIFY) is tricky.
2070 * The problem is atomically making a send-once right
2071 * from the notify port and installing it for a
2072 * dead-name request in the new entry, because this
2073 * requires two port locks (on the notify port and
2074 * the reply port). However, we can safely make
2075 * and consume send-once rights for the notify port
2076 * as long as we hold the space locked. This isn't
2077 * an atomicity problem, because the only way
2078 * to detect that a send-once right has been created
2079 * and then consumed if it wasn't needed is by getting
2080 * at the receive right to look at ip_sorights, and
2081 * because the space is write-locked status calls can't
2082 * lookup the notify port receive right. When we make
2083 * the send-once right, we lock the notify port,
2084 * so any status calls in progress will be done.
2087 is_write_lock(space
);
2090 ipc_port_request_index_t request
;
2092 if (!space
->is_active
) {
2093 is_write_unlock(space
);
2094 return (MACH_RCV_HEADER_ERROR
|
2095 MACH_MSG_IPC_SPACE
);
2098 if (notify
!= MACH_PORT_NULL
) {
2099 notify_port
= ipc_port_lookup_notify(space
,
2101 if (notify_port
== IP_NULL
) {
2102 is_write_unlock(space
);
2103 return MACH_RCV_INVALID_NOTIFY
;
2106 notify_port
= IP_NULL
;
2108 if ((reply_type
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
2109 ipc_right_reverse(space
, (ipc_object_t
) reply
,
2110 &reply_name
, &entry
)) {
2111 /* reply port is locked and active */
2114 * We don't need the notify_port
2115 * send-once right, but we can't release
2116 * it here because reply port is locked.
2117 * Wait until after the copyout to
2118 * release the notify port right.
2121 assert(entry
->ie_bits
&
2122 MACH_PORT_TYPE_SEND_RECEIVE
);
2127 if (!ip_active(reply
)) {
2129 ip_check_unlock(reply
);
2131 if (notify_port
!= IP_NULL
)
2132 ipc_port_release_sonce(notify_port
);
2135 is_write_unlock(space
);
2138 reply_name
= MACH_PORT_DEAD
;
2142 reply_name
= (mach_port_name_t
)reply
;
2143 kr
= ipc_entry_get(space
, &reply_name
, &entry
);
2144 if (kr
!= KERN_SUCCESS
) {
2147 if (notify_port
!= IP_NULL
)
2148 ipc_port_release_sonce(notify_port
);
2150 /* space is locked */
2151 kr
= ipc_entry_grow_table(space
,
2153 if (kr
!= KERN_SUCCESS
) {
2154 /* space is unlocked */
2156 if (kr
== KERN_RESOURCE_SHORTAGE
)
2157 return (MACH_RCV_HEADER_ERROR
|
2158 MACH_MSG_IPC_KERNEL
);
2160 return (MACH_RCV_HEADER_ERROR
|
2161 MACH_MSG_IPC_SPACE
);
2163 /* space is locked again; start over */
2167 assert(IE_BITS_TYPE(entry
->ie_bits
) ==
2168 MACH_PORT_TYPE_NONE
);
2169 assert(entry
->ie_object
== IO_NULL
);
2171 if (notify_port
== IP_NULL
) {
2172 /* not making a dead-name request */
2174 entry
->ie_object
= (ipc_object_t
) reply
;
2178 kr
= ipc_port_dnrequest(reply
, reply_name
,
2179 notify_port
, &request
);
2180 if (kr
!= KERN_SUCCESS
) {
2183 ipc_port_release_sonce(notify_port
);
2185 ipc_entry_dealloc(space
, reply_name
, entry
);
2186 is_write_unlock(space
);
2189 if (!ip_active(reply
)) {
2190 /* will fail next time around loop */
2193 is_write_lock(space
);
2197 kr
= ipc_port_dngrow(reply
, ITS_SIZE_NONE
);
2198 /* port is unlocked */
2199 if (kr
!= KERN_SUCCESS
)
2200 return (MACH_RCV_HEADER_ERROR
|
2201 MACH_MSG_IPC_KERNEL
);
2203 is_write_lock(space
);
2207 notify_port
= IP_NULL
; /* don't release right below */
2209 entry
->ie_object
= (ipc_object_t
) reply
;
2210 entry
->ie_request
= request
;
2214 /* space and reply port are locked and active */
2216 ip_reference(reply
); /* hold onto the reply port */
2218 kr
= ipc_right_copyout(space
, reply_name
, entry
,
2219 reply_type
, TRUE
, (ipc_object_t
) reply
);
2220 /* reply port is unlocked */
2221 assert(kr
== KERN_SUCCESS
);
2223 if (notify_port
!= IP_NULL
)
2224 ipc_port_release_sonce(notify_port
);
2227 is_write_unlock(space
);
2230 * No reply port! This is an easy case.
2231 * We only need to have the space locked
2232 * when checking notify and when locking
2233 * the destination (to ensure atomicity).
2236 is_read_lock(space
);
2237 if (!space
->is_active
) {
2238 is_read_unlock(space
);
2239 return MACH_RCV_HEADER_ERROR
|MACH_MSG_IPC_SPACE
;
2242 if (notify
!= MACH_PORT_NULL
) {
2245 /* must check notify even though it won't be used */
2247 if ((entry
= ipc_entry_lookup(space
, notify
)) == IE_NULL
) {
2248 is_read_unlock(space
);
2249 return MACH_RCV_INVALID_NOTIFY
;
2252 if ((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) == 0) {
2253 is_read_unlock(space
);
2254 return MACH_RCV_INVALID_NOTIFY
;
2259 is_read_unlock(space
);
2261 reply_name
= (mach_port_name_t
) reply
;
2265 * At this point, the space is unlocked and the destination
2266 * port is locked. (Lock taken while space was locked.)
2267 * reply_name is taken care of; we still need dest_name.
2268 * We still hold a ref for reply (if it is valid).
2270 * If the space holds receive rights for the destination,
2271 * we return its name for the right. Otherwise the task
2272 * managed to destroy or give away the receive right between
2273 * receiving the message and this copyout. If the destination
2274 * is dead, return MACH_PORT_DEAD, and if the receive right
2275 * exists somewhere else (another space, in transit)
2276 * return MACH_PORT_NULL.
2278 * Making this copyout operation atomic with the previous
2279 * copyout of the reply port is a bit tricky. If there was
2280 * no real reply port (it wasn't IP_VALID) then this isn't
2281 * an issue. If the reply port was dead at copyout time,
2282 * then we are OK, because if dest is dead we serialize
2283 * after the death of both ports and if dest is alive
2284 * we serialize after reply died but before dest's (later) death.
2285 * So assume reply was alive when we copied it out. If dest
2286 * is alive, then we are OK because we serialize before
2287 * the ports' deaths. So assume dest is dead when we look at it.
2288 * If reply dies/died after dest, then we are OK because
2289 * we serialize after dest died but before reply dies.
2290 * So the hard case is when reply is alive at copyout,
2291 * dest is dead at copyout, and reply died before dest died.
2292 * In this case pretend that dest is still alive, so
2293 * we serialize while both ports are alive.
2295 * Because the space lock is held across the copyout of reply
2296 * and locking dest, the receive right for dest can't move
2297 * in or out of the space while the copyouts happen, so
2298 * that isn't an atomicity problem. In the last hard case
2299 * above, this implies that when dest is dead that the
2300 * space couldn't have had receive rights for dest at
2301 * the time reply was copied-out, so when we pretend
2302 * that dest is still alive, we can return MACH_PORT_NULL.
2304 * If dest == reply, then we have to make it look like
2305 * either both copyouts happened before the port died,
2306 * or both happened after the port died. This special
2307 * case works naturally if the timestamp comparison
2308 * is done correctly.
2313 if (ip_active(dest
)) {
2314 ipc_object_copyout_dest(space
, (ipc_object_t
) dest
,
2315 dest_type
, &dest_name
);
2316 /* dest is unlocked */
2318 ipc_port_timestamp_t timestamp
;
2320 timestamp
= dest
->ip_timestamp
;
2322 ip_check_unlock(dest
);
2324 if (IP_VALID(reply
)) {
2326 if (ip_active(reply
) ||
2327 IP_TIMESTAMP_ORDER(timestamp
,
2328 reply
->ip_timestamp
))
2329 dest_name
= MACH_PORT_DEAD
;
2331 dest_name
= MACH_PORT_NULL
;
2334 dest_name
= MACH_PORT_DEAD
;
2337 if (IP_VALID(reply
))
2338 ipc_port_release(reply
);
2340 msg
->msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
2341 MACH_MSGH_BITS(reply_type
, dest_type
));
2342 msg
->msgh_local_port
= (ipc_port_t
)dest_name
;
2343 msg
->msgh_remote_port
= (ipc_port_t
)reply_name
;
2346 return MACH_MSG_SUCCESS
;
2350 * Routine: ipc_kmsg_copyout_object
2352 * Copy-out a port right. Always returns a name,
2353 * even for unsuccessful return codes. Always
2354 * consumes the supplied object.
2358 * MACH_MSG_SUCCESS The space acquired the right
2359 * (name is valid) or the object is dead (MACH_PORT_DEAD).
2360 * MACH_MSG_IPC_SPACE No room in space for the right,
2361 * or the space is dead. (Name is MACH_PORT_NULL.)
2362 * MACH_MSG_IPC_KERNEL Kernel resource shortage.
2363 * (Name is MACH_PORT_NULL.)
2367 ipc_kmsg_copyout_object(
2369 ipc_object_t object
,
2370 mach_msg_type_name_t msgt_name
,
2371 mach_port_name_t
*namep
)
2375 if (!IO_VALID(object
)) {
2376 *namep
= (mach_port_name_t
) object
;
2377 return MACH_MSG_SUCCESS
;
2380 kr
= ipc_object_copyout(space
, object
, msgt_name
, TRUE
, namep
);
2381 if (kr
!= KERN_SUCCESS
) {
2382 ipc_object_destroy(object
, msgt_name
);
2384 if (kr
== KERN_INVALID_CAPABILITY
)
2385 *namep
= MACH_PORT_DEAD
;
2387 *namep
= MACH_PORT_NULL
;
2389 if (kr
== KERN_RESOURCE_SHORTAGE
)
2390 return MACH_MSG_IPC_KERNEL
;
2392 return MACH_MSG_IPC_SPACE
;
2396 return MACH_MSG_SUCCESS
;
2400 * Routine: ipc_kmsg_copyout_body
2402 * "Copy-out" port rights and out-of-line memory
2403 * in the body of a message.
2405 * The error codes are a combination of special bits.
2406 * The copyout proceeds despite errors.
2410 * MACH_MSG_SUCCESS Successful copyout.
2411 * MACH_MSG_IPC_SPACE No room for port right in name space.
2412 * MACH_MSG_VM_SPACE No room for memory in address space.
2413 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
2414 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
2415 * MACH_MSG_INVALID_RT_DESCRIPTOR Descriptor incompatible with RT
2419 ipc_kmsg_copyout_body(
2423 mach_msg_body_t
*slist
)
2425 mach_msg_body_t
*body
;
2426 mach_msg_descriptor_t
*daddr
, *naddr
;
2427 mach_msg_descriptor_t
*saddr
;
2428 mach_msg_type_number_t i
, dsc_count
, sdsc_count
;
2429 mach_msg_return_t mr
= MACH_MSG_SUCCESS
;
2432 boolean_t differs
= MAP_SIZE_DIFFERS(map
);
2434 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
2435 dsc_count
= body
->msgh_descriptor_count
;
2436 daddr
= (mach_msg_descriptor_t
*) (body
+ 1);
2439 * Do scatter list setup
2441 if (slist
!= MACH_MSG_BODY_NULL
) {
2442 saddr
= (mach_msg_descriptor_t
*) (slist
+ 1);
2443 sdsc_count
= slist
->msgh_descriptor_count
;
2446 saddr
= MACH_MSG_DESCRIPTOR_NULL
;
2451 * Compute the true size of the resulting descriptors
2452 * after potential expansion and adjust the header
2453 * and body location accordingly.
2456 mach_msg_size_t dsc_adjust
;
2460 for (i
= 0; i
< dsc_count
; i
++, naddr
++)
2461 switch (naddr
->type
.type
) {
2462 case MACH_MSG_OOL_DESCRIPTOR
:
2463 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
2464 case MACH_MSG_OOL_PORTS_DESCRIPTOR
:
2465 dsc_adjust
+= DESC_SIZE_ADJUSTMENT
;
2471 mach_msg_base_t
*old_base
= (mach_msg_base_t
*)kmsg
->ikm_header
;
2472 mach_msg_base_t
*new_base
;
2474 new_base
= (mach_msg_base_t
*)((vm_offset_t
)old_base
- dsc_adjust
);
2475 memmove(new_base
, old_base
, sizeof(mach_msg_base_t
));
2476 kmsg
->ikm_header
= &new_base
->header
;
2477 kmsg
->ikm_header
->msgh_size
+= dsc_adjust
;
2478 naddr
= (mach_msg_descriptor_t
*)(new_base
+ 1);
2487 * Now process the descriptors
2489 for ( i
= 0; i
< dsc_count
; i
++, daddr
++ ) {
2490 switch (daddr
->type
.type
) {
2492 case MACH_MSG_PORT_DESCRIPTOR
: {
2493 volatile mach_msg_port_descriptor_t
*dsc
;
2494 volatile mach_msg_port_descriptor_t
*user_dsc
;
2496 mach_port_name_t name
;
2497 mach_msg_type_name_t disp
;
2500 * Copyout port right carried in the message
2503 user_dsc
= &naddr
->port
;
2505 disp
= dsc
->disposition
;
2506 mr
|= ipc_kmsg_copyout_object(space
,
2510 user_dsc
->name
= (mach_port_t
)name
;
2511 user_dsc
->disposition
= disp
;
2512 user_dsc
->type
= MACH_MSG_PORT_DESCRIPTOR
;
2517 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
2518 case MACH_MSG_OOL_DESCRIPTOR
: {
2520 mach_vm_offset_t rcv_addr
;
2521 mach_msg_ool_descriptor_t
*dsc
;
2522 mach_msg_copy_options_t copy_options
;
2523 mach_msg_size_t size
;
2524 mach_msg_descriptor_type_t dsc_type
;
2526 SKIP_PORT_DESCRIPTORS(saddr
, sdsc_count
);
2528 dsc
= &daddr
->out_of_line
;
2529 copy
= (vm_map_copy_t
) dsc
->address
;
2531 copy_options
= dsc
->copy
;
2532 assert(copy_options
!= MACH_MSG_KALLOC_COPY_T
);
2533 dsc_type
= dsc
->type
;
2535 if (copy
!= VM_MAP_COPY_NULL
) {
2537 * Check to see if there is an overwrite descriptor
2538 * specified in the scatter list for this ool data.
2539 * The descriptor has already been verified.
2541 if (saddr
!= MACH_MSG_DESCRIPTOR_NULL
) {
2543 OTHER_OOL_DESCRIPTOR
*scatter_dsc
;
2545 scatter_dsc
= (OTHER_OOL_DESCRIPTOR
*)saddr
;
2546 if (scatter_dsc
->copy
== MACH_MSG_OVERWRITE
) {
2547 rcv_addr
= (mach_vm_offset_t
) scatter_dsc
->address
;
2548 copy_options
= MACH_MSG_OVERWRITE
;
2551 copy_options
= MACH_MSG_VIRTUAL_COPY
;
2554 mach_msg_ool_descriptor_t
*scatter_dsc
;
2556 scatter_dsc
= &saddr
->out_of_line
;
2557 if (scatter_dsc
->copy
== MACH_MSG_OVERWRITE
) {
2558 rcv_addr
= CAST_USER_ADDR_T(scatter_dsc
->address
);
2559 copy_options
= MACH_MSG_OVERWRITE
;
2562 copy_options
= MACH_MSG_VIRTUAL_COPY
;
2565 INCREMENT_SCATTER(saddr
, sdsc_count
, differs
);
2570 * Whether the data was virtually or physically
2571 * copied we have a vm_map_copy_t for it.
2572 * If there's an overwrite region specified
2573 * overwrite it, otherwise do a virtual copy out.
2575 if (copy_options
== MACH_MSG_OVERWRITE
) {
2576 kr
= vm_map_copy_overwrite(map
, rcv_addr
,
2579 kr
= vm_map_copyout(map
, &rcv_addr
, copy
);
2581 if (kr
!= KERN_SUCCESS
) {
2582 if (kr
== KERN_RESOURCE_SHORTAGE
)
2583 mr
|= MACH_MSG_VM_KERNEL
;
2585 mr
|= MACH_MSG_VM_SPACE
;
2586 vm_map_copy_discard(copy
);
2596 * Now update the descriptor as the user would see it.
2597 * This may require expanding the descriptor to the user
2598 * visible size. There is already space allocated for
2599 * this in what naddr points to.
2602 volatile OTHER_OOL_DESCRIPTOR
*user_dsc
;
2604 user_dsc
= (OTHER_OOL_DESCRIPTOR
*)naddr
;
2605 user_dsc
->address
= rcv_addr
;
2606 user_dsc
->deallocate
= (copy_options
== MACH_MSG_VIRTUAL_COPY
) ?
2608 user_dsc
->copy
= copy_options
;
2609 user_dsc
->type
= dsc_type
;
2610 user_dsc
->size
= size
;
2611 naddr
= (mach_msg_descriptor_t
*)((OTHER_OOL_DESCRIPTOR
*)naddr
+ 1);
2613 volatile mach_msg_ool_descriptor_t
*user_dsc
;
2615 user_dsc
= &naddr
->out_of_line
;
2616 user_dsc
->address
= CAST_DOWN(void *, rcv_addr
);
2617 user_dsc
->size
= size
;
2618 user_dsc
->deallocate
= (copy_options
== MACH_MSG_VIRTUAL_COPY
) ?
2620 user_dsc
->copy
= copy_options
;
2621 user_dsc
->type
= dsc_type
;
2627 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
2628 mach_vm_offset_t rcv_addr
;
2629 mach_port_name_t
*objects
;
2630 mach_msg_type_name_t disp
;
2631 mach_msg_type_number_t count
, j
;
2634 volatile mach_msg_ool_ports_descriptor_t
*dsc
;
2635 mach_msg_copy_options_t copy_options
= MACH_MSG_VIRTUAL_COPY
;
2637 SKIP_PORT_DESCRIPTORS(saddr
, sdsc_count
);
2639 dsc
= &daddr
->ool_ports
;
2641 disp
= dsc
->disposition
;
2642 length
= count
* sizeof(mach_port_name_t
);
2644 if (length
!= 0 && dsc
->address
!= 0) {
2647 * Check to see if there is an overwrite descriptor
2648 * specified in the scatter list for this ool data.
2649 * The descriptor has already been verified.
2651 if (saddr
!= MACH_MSG_DESCRIPTOR_NULL
) {
2653 OTHER_OOL_DESCRIPTOR
*scatter_dsc
;
2655 scatter_dsc
= (OTHER_OOL_DESCRIPTOR
*)saddr
;
2656 rcv_addr
= (mach_vm_offset_t
) scatter_dsc
->address
;
2657 copy_options
= scatter_dsc
->copy
;
2659 mach_msg_ool_descriptor_t
*scatter_dsc
;
2661 scatter_dsc
= &saddr
->out_of_line
;
2662 rcv_addr
= CAST_USER_ADDR_T(scatter_dsc
->address
);
2663 copy_options
= scatter_dsc
->copy
;
2665 INCREMENT_SCATTER(saddr
, sdsc_count
, differs
);
2668 if (copy_options
== MACH_MSG_VIRTUAL_COPY
) {
2670 * Dynamically allocate the region
2672 int anywhere
= VM_MAKE_TAG(VM_MEMORY_MACH_MSG
)|
2675 if ((kr
= mach_vm_allocate(map
, &rcv_addr
,
2676 (mach_vm_size_t
)length
,
2677 anywhere
)) != KERN_SUCCESS
) {
2678 ipc_kmsg_clean_body(kmsg
, 1, daddr
);
2681 if (kr
== KERN_RESOURCE_SHORTAGE
){
2682 mr
|= MACH_MSG_VM_KERNEL
;
2684 mr
|= MACH_MSG_VM_SPACE
;
2691 * Handle the port rights and copy out the names
2692 * for those rights out to user-space.
2694 if (rcv_addr
!= 0) {
2695 objects
= (mach_port_name_t
*) dsc
->address
;
2697 /* copyout port rights carried in the message */
2699 for ( j
= 0; j
< count
; j
++) {
2700 ipc_object_t object
=
2701 (ipc_object_t
) objects
[j
];
2703 mr
|= ipc_kmsg_copyout_object(space
, object
,
2707 /* copyout to memory allocated above */
2708 data
= dsc
->address
;
2709 if (copyoutmap(map
, data
, rcv_addr
, length
) != KERN_SUCCESS
)
2710 mr
|= MACH_MSG_VM_SPACE
;
2711 kfree(data
, length
);
2718 * Now update the descriptor based on the information
2722 volatile OTHER_OOL_PORTS_DESCRIPTOR
*user_dsc
;
2724 user_dsc
= (OTHER_OOL_PORTS_DESCRIPTOR
*)naddr
;
2725 user_dsc
->address
= rcv_addr
;
2726 user_dsc
->deallocate
= (copy_options
== MACH_MSG_VIRTUAL_COPY
) ?
2728 user_dsc
->copy
= copy_options
;
2729 user_dsc
->disposition
= disp
;
2730 user_dsc
->type
= MACH_MSG_OOL_PORTS_DESCRIPTOR
;
2731 user_dsc
->count
= count
;
2732 naddr
= (mach_msg_descriptor_t
*)((OTHER_OOL_PORTS_DESCRIPTOR
*)naddr
+ 1);
2734 volatile mach_msg_ool_ports_descriptor_t
*user_dsc
;
2736 user_dsc
= &naddr
->ool_ports
;
2737 user_dsc
->address
= CAST_DOWN(void *, rcv_addr
);
2738 user_dsc
->count
= count
;
2739 user_dsc
->deallocate
= (copy_options
== MACH_MSG_VIRTUAL_COPY
) ?
2741 user_dsc
->copy
= copy_options
;
2742 user_dsc
->disposition
= disp
;
2743 user_dsc
->type
= MACH_MSG_OOL_PORTS_DESCRIPTOR
;
2749 panic("untyped IPC copyout body: invalid message descriptor");
2757 * Routine: ipc_kmsg_copyout_size
2759 * Compute the size of the message as copied out to the given
2760 * map. If the destination map's pointers are a different size
2761 * than the kernel's, we have to allow for expansion/
2762 * contraction of the descriptors as appropriate.
2766 * size of the message as it would be received.
2770 ipc_kmsg_copyout_size(
2774 mach_msg_size_t send_size
;
2776 send_size
= kmsg
->ikm_header
->msgh_size
;
2778 if ((kmsg
->ikm_header
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) &&
2779 MAP_SIZE_DIFFERS(map
)) {
2781 mach_msg_body_t
*body
;
2782 mach_msg_descriptor_t
*saddr
, *eaddr
;
2784 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
2785 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
2786 eaddr
= saddr
+ body
->msgh_descriptor_count
;
2788 for ( ; saddr
< eaddr
; saddr
++ ) {
2789 switch (saddr
->type
.type
) {
2791 case MACH_MSG_OOL_DESCRIPTOR
:
2792 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
2793 case MACH_MSG_OOL_PORTS_DESCRIPTOR
:
2794 send_size
+= DESC_SIZE_ADJUSTMENT
;
2806 * Routine: ipc_kmsg_copyout
2808 * "Copy-out" port rights and out-of-line memory
2813 * MACH_MSG_SUCCESS Copied out all rights and memory.
2814 * MACH_RCV_INVALID_NOTIFY Bad notify port.
2815 * Rights and memory in the message are intact.
2816 * MACH_RCV_HEADER_ERROR + special bits
2817 * Rights and memory in the message are intact.
2818 * MACH_RCV_BODY_ERROR + special bits
2819 * The message header was successfully copied out.
2820 * As much of the body was handled as possible.
2828 mach_port_name_t notify
,
2829 mach_msg_body_t
*slist
)
2831 mach_msg_return_t mr
;
2833 mr
= ipc_kmsg_copyout_header(kmsg
->ikm_header
, space
, notify
);
2834 if (mr
!= MACH_MSG_SUCCESS
)
2837 if (kmsg
->ikm_header
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
2838 mr
= ipc_kmsg_copyout_body(kmsg
, space
, map
, slist
);
2840 if (mr
!= MACH_MSG_SUCCESS
)
2841 mr
|= MACH_RCV_BODY_ERROR
;
2848 * Routine: ipc_kmsg_copyout_pseudo
2850 * Does a pseudo-copyout of the message.
2851 * This is like a regular copyout, except
2852 * that the ports in the header are handled
2853 * as if they are in the body. They aren't reversed.
2855 * The error codes are a combination of special bits.
2856 * The copyout proceeds despite errors.
2860 * MACH_MSG_SUCCESS Successful copyout.
2861 * MACH_MSG_IPC_SPACE No room for port right in name space.
2862 * MACH_MSG_VM_SPACE No room for memory in address space.
2863 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
2864 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
2868 ipc_kmsg_copyout_pseudo(
2872 mach_msg_body_t
*slist
)
2874 mach_msg_bits_t mbits
= kmsg
->ikm_header
->msgh_bits
;
2875 ipc_object_t dest
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
2876 ipc_object_t reply
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
2877 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
2878 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
2879 mach_port_name_t dest_name
, reply_name
;
2880 mach_msg_return_t mr
;
2882 assert(IO_VALID(dest
));
2884 mr
= (ipc_kmsg_copyout_object(space
, dest
, dest_type
, &dest_name
) |
2885 ipc_kmsg_copyout_object(space
, reply
, reply_type
, &reply_name
));
2887 kmsg
->ikm_header
->msgh_bits
= mbits
&~ MACH_MSGH_BITS_CIRCULAR
;
2888 kmsg
->ikm_header
->msgh_remote_port
= (ipc_port_t
)dest_name
;
2889 kmsg
->ikm_header
->msgh_local_port
= (ipc_port_t
)reply_name
;
2891 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2892 mr
|= ipc_kmsg_copyout_body(kmsg
, space
, map
, slist
);
2899 * Routine: ipc_kmsg_copyout_dest
2901 * Copies out the destination port in the message.
2902 * Destroys all other rights and memory in the message.
2908 ipc_kmsg_copyout_dest(
2912 mach_msg_bits_t mbits
;
2915 mach_msg_type_name_t dest_type
;
2916 mach_msg_type_name_t reply_type
;
2917 mach_port_name_t dest_name
, reply_name
;
2919 mbits
= kmsg
->ikm_header
->msgh_bits
;
2920 dest
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
2921 reply
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
2922 dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
2923 reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
2925 assert(IO_VALID(dest
));
2928 if (io_active(dest
)) {
2929 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
2930 /* dest is unlocked */
2933 io_check_unlock(dest
);
2934 dest_name
= MACH_PORT_DEAD
;
2937 if (IO_VALID(reply
)) {
2938 ipc_object_destroy(reply
, reply_type
);
2939 reply_name
= MACH_PORT_NULL
;
2941 reply_name
= (mach_port_name_t
) reply
;
2943 kmsg
->ikm_header
->msgh_bits
= (MACH_MSGH_BITS_OTHER(mbits
) |
2944 MACH_MSGH_BITS(reply_type
, dest_type
));
2945 kmsg
->ikm_header
->msgh_local_port
= (ipc_port_t
)dest_name
;
2946 kmsg
->ikm_header
->msgh_remote_port
= (ipc_port_t
)reply_name
;
2948 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
2949 mach_msg_body_t
*body
;
2951 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
2952 ipc_kmsg_clean_body(kmsg
, body
->msgh_descriptor_count
,
2953 (mach_msg_descriptor_t
*)(body
+ 1));
2958 * Routine: ipc_kmsg_copyin_scatter
2960 * allocate and copyin a scatter list
2962 * The gather (kmsg) is valid since it has been copied in.
2963 * Gather list descriptors are sequentially paired with scatter
2964 * list descriptors, with port descriptors in either list ignored.
2965 * Descriptors are consistent if the type fileds match and size
2966 * of the scatter descriptor is less than or equal to the
2967 * size of the gather descriptor. A MACH_MSG_ALLOCATE copy
2968 * strategy in a scatter descriptor matches any size in the
2969 * corresponding gather descriptor assuming they are the same type.
2970 * Either list may be larger than the other. During the
2971 * subsequent copy out, excess scatter descriptors are ignored
2972 * and excess gather descriptors default to dynamic allocation.
2974 * In the case of a size error, the scatter list is released.
2978 * the allocated message body containing the scatter list.
2982 ipc_kmsg_get_scatter(
2983 mach_vm_address_t msg_addr
,
2984 mach_msg_size_t slist_size
,
2987 mach_msg_body_t
*slist
;
2988 mach_msg_body_t
*body
;
2989 mach_msg_descriptor_t
*gstart
, *gend
;
2990 mach_msg_descriptor_t
*sstart
, *send
;
2993 if (slist_size
< sizeof(mach_msg_base_t
))
2994 return MACH_MSG_BODY_NULL
;
2996 slist_size
-= sizeof(mach_msg_header_t
);
2997 slist
= (mach_msg_body_t
*)kalloc(slist_size
);
2998 if (slist
== MACH_MSG_BODY_NULL
)
3001 if (copyin(msg_addr
+ sizeof(mach_msg_header_t
), (char *)slist
, slist_size
)) {
3002 kfree(slist
, slist_size
);
3003 return MACH_MSG_BODY_NULL
;
3006 if ((slist
->msgh_descriptor_count
* sizeof(mach_msg_descriptor_t
)
3007 + sizeof(mach_msg_size_t
)) > slist_size
) {
3008 kfree(slist
, slist_size
);
3009 return MACH_MSG_BODY_NULL
;
3012 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
3013 gstart
= (mach_msg_descriptor_t
*) (body
+ 1);
3014 gend
= gstart
+ body
->msgh_descriptor_count
;
3016 sstart
= (mach_msg_descriptor_t
*) (slist
+ 1);
3017 send
= sstart
+ slist
->msgh_descriptor_count
;
3019 while (gstart
< gend
) {
3020 mach_msg_descriptor_type_t g_type
;
3023 * Skip port descriptors in gather list.
3025 g_type
= gstart
->type
.type
;
3027 if (g_type
!= MACH_MSG_PORT_DESCRIPTOR
) {
3030 * A scatter list with a 0 descriptor count is treated as an
3031 * automatic size mismatch.
3033 if (slist
->msgh_descriptor_count
== 0) {
3034 kfree(slist
, slist_size
);
3035 return MACH_MSG_BODY_NULL
;
3039 * Skip port descriptors in scatter list.
3041 while (sstart
< send
) {
3042 if (sstart
->type
.type
!= MACH_MSG_PORT_DESCRIPTOR
)
3048 * No more scatter descriptors, we're done
3050 if (sstart
>= send
) {
3055 * Check type, copy and size fields
3057 if (g_type
== MACH_MSG_OOL_DESCRIPTOR
||
3058 g_type
== MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
3059 if (sstart
->type
.type
!= MACH_MSG_OOL_DESCRIPTOR
&&
3060 sstart
->type
.type
!= MACH_MSG_OOL_VOLATILE_DESCRIPTOR
) {
3061 kfree(slist
, slist_size
);
3062 return MACH_MSG_BODY_NULL
;
3064 if (sstart
->out_of_line
.copy
== MACH_MSG_OVERWRITE
&&
3065 gstart
->out_of_line
.size
> sstart
->out_of_line
.size
) {
3066 kfree(slist
, slist_size
);
3067 return MACH_MSG_BODY_NULL
;
3071 if (sstart
->type
.type
!= MACH_MSG_OOL_PORTS_DESCRIPTOR
) {
3072 kfree(slist
, slist_size
);
3073 return MACH_MSG_BODY_NULL
;
3075 if (sstart
->ool_ports
.copy
== MACH_MSG_OVERWRITE
&&
3076 gstart
->ool_ports
.count
> sstart
->ool_ports
.count
) {
3077 kfree(slist
, slist_size
);
3078 return MACH_MSG_BODY_NULL
;
3090 * Routine: ipc_kmsg_free_scatter
3092 * Deallocate a scatter list. Since we actually allocated
3093 * a body without a header, and since the header was originally
3094 * accounted for in slist_size, we have to ajust it down
3095 * before freeing the scatter list.
3098 ipc_kmsg_free_scatter(
3099 mach_msg_body_t
*slist
,
3100 mach_msg_size_t slist_size
)
3102 slist_size
-= sizeof(mach_msg_header_t
);
3103 kfree(slist
, slist_size
);
3108 * Routine: ipc_kmsg_copyout_to_kernel
3110 * Copies out the destination and reply ports in the message.
3111 * Leaves all other rights and memory in the message alone.
3115 * Derived from ipc_kmsg_copyout_dest.
3116 * Use by mach_msg_rpc_from_kernel (which used to use copyout_dest).
3117 * We really do want to save rights and memory.
3121 ipc_kmsg_copyout_to_kernel(
3127 mach_msg_type_name_t dest_type
;
3128 mach_msg_type_name_t reply_type
;
3129 mach_port_name_t dest_name
, reply_name
;
3131 dest
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
3132 reply
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
3133 dest_type
= MACH_MSGH_BITS_REMOTE(kmsg
->ikm_header
->msgh_bits
);
3134 reply_type
= MACH_MSGH_BITS_LOCAL(kmsg
->ikm_header
->msgh_bits
);
3136 assert(IO_VALID(dest
));
3139 if (io_active(dest
)) {
3140 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
3141 /* dest is unlocked */
3144 io_check_unlock(dest
);
3145 dest_name
= MACH_PORT_DEAD
;
3148 reply_name
= (mach_port_name_t
) reply
;
3150 kmsg
->ikm_header
->msgh_bits
=
3151 (MACH_MSGH_BITS_OTHER(kmsg
->ikm_header
->msgh_bits
) |
3152 MACH_MSGH_BITS(reply_type
, dest_type
));
3153 kmsg
->ikm_header
->msgh_local_port
= (ipc_port_t
)dest_name
;
3154 kmsg
->ikm_header
->msgh_remote_port
= (ipc_port_t
)reply_name
;
3157 #include <mach_kdb.h>
3160 #include <ddb/db_output.h>
3161 #include <ipc/ipc_print.h>
3163 * Forward declarations
3165 void ipc_msg_print_untyped(
3166 mach_msg_body_t
*body
);
3168 const char * ipc_type_name(
3170 boolean_t received
);
3172 void ipc_print_type_name(
3177 mach_msg_bits_t bit
);
3180 mm_copy_options_string(
3181 mach_msg_copy_options_t option
);
3183 void db_print_msg_uid(mach_msg_header_t
*);
3191 switch (type_name
) {
3192 case MACH_MSG_TYPE_PORT_NAME
:
3195 case MACH_MSG_TYPE_MOVE_RECEIVE
:
3197 return "port_receive";
3199 return "move_receive";
3202 case MACH_MSG_TYPE_MOVE_SEND
:
3209 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
3211 return "port_send_once";
3213 return "move_send_once";
3216 case MACH_MSG_TYPE_COPY_SEND
:
3219 case MACH_MSG_TYPE_MAKE_SEND
:
3222 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
3223 return "make_send_once";
3231 ipc_print_type_name(
3234 const char *name
= ipc_type_name(type_name
, TRUE
);
3238 printf("type%d", type_name
);
3243 * ipc_kmsg_print [ debug ]
3249 iprintf("kmsg=0x%x\n", kmsg
);
3250 iprintf("ikm_next=0x%x, prev=0x%x, size=%d",
3255 ipc_msg_print(kmsg
->ikm_header
);
3260 mach_msg_bits_t bit
)
3263 case MACH_MSGH_BITS_COMPLEX
: return "complex";
3264 case MACH_MSGH_BITS_CIRCULAR
: return "circular";
3265 default: return (char *) 0;
3270 * ipc_msg_print [ debug ]
3274 mach_msg_header_t
*msgh
)
3276 mach_msg_bits_t mbits
;
3277 unsigned int bit
, i
;
3278 const char *bit_name
;
3281 mbits
= msgh
->msgh_bits
;
3282 iprintf("msgh_bits=0x%x: l=0x%x,r=0x%x\n",
3284 MACH_MSGH_BITS_LOCAL(msgh
->msgh_bits
),
3285 MACH_MSGH_BITS_REMOTE(msgh
->msgh_bits
));
3287 mbits
= MACH_MSGH_BITS_OTHER(mbits
) & MACH_MSGH_BITS_USED
;
3290 iprintf("decoded bits: ");
3292 for (i
= 0, bit
= 1; i
< sizeof(mbits
) * 8; ++i
, bit
<<= 1) {
3293 if ((mbits
& bit
) == 0)
3295 bit_name
= msgh_bit_decode((mach_msg_bits_t
)bit
);
3297 printf("%s%s", needs_comma
? "," : "", bit_name
);
3299 printf("%sunknown(0x%x),", needs_comma
? "," : "", bit
);
3302 if (msgh
->msgh_bits
& ~MACH_MSGH_BITS_USED
) {
3303 printf("%sunused=0x%x,", needs_comma
? "," : "",
3304 msgh
->msgh_bits
& ~MACH_MSGH_BITS_USED
);
3310 if (msgh
->msgh_remote_port
) {
3311 iprintf("remote=0x%x(", msgh
->msgh_remote_port
);
3312 ipc_print_type_name(MACH_MSGH_BITS_REMOTE(msgh
->msgh_bits
));
3315 iprintf("remote=null");
3318 if (msgh
->msgh_local_port
) {
3319 printf("%slocal=0x%x(", needs_comma
? "," : "",
3320 msgh
->msgh_local_port
);
3321 ipc_print_type_name(MACH_MSGH_BITS_LOCAL(msgh
->msgh_bits
));
3324 printf("local=null\n");
3327 iprintf("msgh_id=%d, size=%d\n",
3331 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
3332 ipc_msg_print_untyped((mach_msg_body_t
*) (msgh
+ 1));
3338 mm_copy_options_string(
3339 mach_msg_copy_options_t option
)
3344 case MACH_MSG_PHYSICAL_COPY
:
3347 case MACH_MSG_VIRTUAL_COPY
:
3350 case MACH_MSG_OVERWRITE
:
3353 case MACH_MSG_ALLOCATE
:
3356 case MACH_MSG_KALLOC_COPY_T
:
3357 name
= "KALLOC_COPY_T";
3367 ipc_msg_print_untyped(
3368 mach_msg_body_t
*body
)
3370 mach_msg_descriptor_t
*saddr
, *send
;
3371 mach_msg_descriptor_type_t type
;
3373 iprintf("%d descriptors %d: \n", body
->msgh_descriptor_count
);
3375 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
3376 send
= saddr
+ body
->msgh_descriptor_count
;
3378 for ( ; saddr
< send
; saddr
++ ) {
3380 type
= saddr
->type
.type
;
3384 case MACH_MSG_PORT_DESCRIPTOR
: {
3385 mach_msg_port_descriptor_t
*dsc
;
3388 iprintf("-- PORT name = 0x%x disp = ", dsc
->name
);
3389 ipc_print_type_name(dsc
->disposition
);
3393 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
3394 case MACH_MSG_OOL_DESCRIPTOR
: {
3395 mach_msg_ool_descriptor_t
*dsc
;
3397 dsc
= &saddr
->out_of_line
;
3398 iprintf("-- OOL%s addr = 0x%x size = 0x%x copy = %s %s\n",
3399 type
== MACH_MSG_OOL_DESCRIPTOR
? "" : " VOLATILE",
3400 dsc
->address
, dsc
->size
,
3401 mm_copy_options_string(dsc
->copy
),
3402 dsc
->deallocate
? "DEALLOC" : "");
3405 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
3406 mach_msg_ool_ports_descriptor_t
*dsc
;
3408 dsc
= &saddr
->ool_ports
;
3410 iprintf("-- OOL_PORTS addr = 0x%x count = 0x%x ",
3411 dsc
->address
, dsc
->count
);
3413 ipc_print_type_name(dsc
->disposition
);
3414 printf(" copy = %s %s\n",
3415 mm_copy_options_string(dsc
->copy
),
3416 dsc
->deallocate
? "DEALLOC" : "");
3421 iprintf("-- UNKNOWN DESCRIPTOR 0x%x\n", type
);
3427 #endif /* MACH_KDB */