2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
61 * Copyright (c) 2005 SPARTA, Inc.
66 * File: ipc/ipc_kmsg.c
70 * Operations on kernel messages.
74 #include <mach/mach_types.h>
75 #include <mach/boolean.h>
76 #include <mach/kern_return.h>
77 #include <mach/message.h>
78 #include <mach/port.h>
79 #include <mach/vm_map.h>
80 #include <mach/mach_vm.h>
81 #include <mach/vm_statistics.h>
83 #include <kern/kern_types.h>
84 #include <kern/assert.h>
85 #include <kern/debug.h>
86 #include <kern/ipc_kobject.h>
87 #include <kern/kalloc.h>
88 #include <kern/zalloc.h>
89 #include <kern/processor.h>
90 #include <kern/thread.h>
91 #include <kern/sched_prim.h>
92 #include <kern/misc_protos.h>
93 #include <kern/counters.h>
94 #include <kern/cpu_data.h>
95 #include <kern/policy_internal.h>
97 #include <machine/machlimits.h>
99 #include <vm/vm_map.h>
100 #include <vm/vm_object.h>
101 #include <vm/vm_kern.h>
103 #include <ipc/port.h>
104 #include <ipc/ipc_types.h>
105 #include <ipc/ipc_entry.h>
106 #include <ipc/ipc_kmsg.h>
107 #include <ipc/ipc_notify.h>
108 #include <ipc/ipc_object.h>
109 #include <ipc/ipc_space.h>
110 #include <ipc/ipc_port.h>
111 #include <ipc/ipc_right.h>
112 #include <ipc/ipc_hash.h>
113 #include <ipc/ipc_table.h>
114 #include <ipc/ipc_importance.h>
116 #include <kern/mach_node.h>
117 #include <ipc/flipc.h>
120 #include <os/overflow.h>
122 #include <security/mac_mach_internal.h>
124 #include <device/device_server.h>
129 #include <ppc/Firmware.h>
130 #include <ppc/low_trace.h>
134 #define DEBUG_MSGS_K64 1
137 #include <sys/kdebug.h>
138 #include <libkern/OSAtomic.h>
144 mach_msg_bits_t msgh_bits
;
145 mach_msg_size_t msgh_size
;
146 mach_port_name_t msgh_remote_port
;
147 mach_port_name_t msgh_local_port
;
148 mach_port_name_t msgh_voucher_port
;
149 mach_msg_id_t msgh_id
;
150 } mach_msg_legacy_header_t
;
154 mach_msg_legacy_header_t header
;
155 mach_msg_body_t body
;
156 } mach_msg_legacy_base_t
;
160 mach_port_name_t name
;
161 mach_msg_size_t pad1
;
163 mach_msg_type_name_t disposition
: 8;
164 mach_msg_descriptor_type_t type
: 8;
165 } mach_msg_legacy_port_descriptor_t
;
170 mach_msg_legacy_port_descriptor_t port
;
171 mach_msg_ool_descriptor32_t out_of_line32
;
172 mach_msg_ool_ports_descriptor32_t ool_ports32
;
173 mach_msg_type_descriptor_t type
;
174 } mach_msg_legacy_descriptor_t
;
178 #define LEGACY_HEADER_SIZE_DELTA ((mach_msg_size_t)(sizeof(mach_msg_header_t) - sizeof(mach_msg_legacy_header_t)))
184 extern void ipc_pset_print64(
187 extern void ipc_kmsg_print64(
191 extern void ipc_msg_print64(
192 mach_msg_header_t
*msgh
);
194 extern ipc_port_t
ipc_name_to_data64(
196 mach_port_name_t name
);
199 * Forward declarations
201 void ipc_msg_print_untyped64(
202 mach_msg_body_t
*body
);
204 const char * ipc_type_name64(
208 void ipc_print_type_name64(
213 mach_msg_bits_t bit
);
216 mm_copy_options_string64(
217 mach_msg_copy_options_t option
);
219 void db_print_msg_uid64(mach_msg_header_t
*);
222 ipc_msg_body_print64(void *body
, int size
)
224 uint32_t *word
= (uint32_t *) body
;
225 uint32_t *end
= (uint32_t *)(((uintptr_t) body
) + size
226 - sizeof(mach_msg_header_t
));
229 kprintf(" body(%p-%p):\n %p: ", body
, end
, word
);
231 for (i
= 0; i
< 8; i
++, word
++) {
236 kprintf("%08x ", *word
);
238 kprintf("\n %p: ", word
);
249 case MACH_MSG_TYPE_PORT_NAME
:
252 case MACH_MSG_TYPE_MOVE_RECEIVE
:
254 return "port_receive";
256 return "move_receive";
259 case MACH_MSG_TYPE_MOVE_SEND
:
266 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
268 return "port_send_once";
270 return "move_send_once";
273 case MACH_MSG_TYPE_COPY_SEND
:
276 case MACH_MSG_TYPE_MAKE_SEND
:
279 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
280 return "make_send_once";
288 ipc_print_type_name64(
291 const char *name
= ipc_type_name64(type_name
, TRUE
);
295 kprintf("type%d", type_name
);
300 * ipc_kmsg_print64 [ debug ]
307 kprintf("%s kmsg=%p:\n", str
, kmsg
);
308 kprintf(" next=%p, prev=%p, size=%d",
313 ipc_msg_print64(kmsg
->ikm_header
);
321 case MACH_MSGH_BITS_COMPLEX
: return "complex";
322 case MACH_MSGH_BITS_CIRCULAR
: return "circular";
323 default: return (char *) 0;
328 * ipc_msg_print64 [ debug ]
332 mach_msg_header_t
*msgh
)
334 mach_msg_bits_t mbits
;
336 const char *bit_name
;
339 mbits
= msgh
->msgh_bits
;
340 kprintf(" msgh_bits=0x%x: l=0x%x,r=0x%x\n",
342 MACH_MSGH_BITS_LOCAL(msgh
->msgh_bits
),
343 MACH_MSGH_BITS_REMOTE(msgh
->msgh_bits
));
345 mbits
= MACH_MSGH_BITS_OTHER(mbits
) & MACH_MSGH_BITS_USED
;
346 kprintf(" decoded bits: ");
348 for (i
= 0, bit
= 1; i
< sizeof(mbits
) * 8; ++i
, bit
<<= 1) {
349 if ((mbits
& bit
) == 0)
351 bit_name
= msgh_bit_decode64((mach_msg_bits_t
)bit
);
353 kprintf("%s%s", needs_comma
? "," : "", bit_name
);
355 kprintf("%sunknown(0x%x),", needs_comma
? "," : "", bit
);
358 if (msgh
->msgh_bits
& ~MACH_MSGH_BITS_USED
) {
359 kprintf("%sunused=0x%x,", needs_comma
? "," : "",
360 msgh
->msgh_bits
& ~MACH_MSGH_BITS_USED
);
365 if (msgh
->msgh_remote_port
) {
366 kprintf(" remote=%p(", msgh
->msgh_remote_port
);
367 ipc_print_type_name64(MACH_MSGH_BITS_REMOTE(msgh
->msgh_bits
));
370 kprintf(" remote=null");
373 if (msgh
->msgh_local_port
) {
374 kprintf("%slocal=%p(", needs_comma
? "," : "",
375 msgh
->msgh_local_port
);
376 ipc_print_type_name64(MACH_MSGH_BITS_LOCAL(msgh
->msgh_bits
));
379 kprintf("local=null\n");
382 kprintf(" msgh_id=%d, size=%d\n",
386 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
387 ipc_msg_print_untyped64((mach_msg_body_t
*) (msgh
+ 1));
390 ipc_msg_body_print64((void *)(msgh
+ 1), msgh
->msgh_size
);
395 mm_copy_options_string64(
396 mach_msg_copy_options_t option
)
401 case MACH_MSG_PHYSICAL_COPY
:
404 case MACH_MSG_VIRTUAL_COPY
:
407 case MACH_MSG_OVERWRITE
:
408 name
= "OVERWRITE(DEPRECATED)";
410 case MACH_MSG_ALLOCATE
:
413 case MACH_MSG_KALLOC_COPY_T
:
414 name
= "KALLOC_COPY_T";
424 ipc_msg_print_untyped64(
425 mach_msg_body_t
*body
)
427 mach_msg_descriptor_t
*saddr
, *send
;
428 mach_msg_descriptor_type_t type
;
430 kprintf(" %d descriptors: \n", body
->msgh_descriptor_count
);
432 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
433 send
= saddr
+ body
->msgh_descriptor_count
;
435 for ( ; saddr
< send
; saddr
++ ) {
437 type
= saddr
->type
.type
;
441 case MACH_MSG_PORT_DESCRIPTOR
: {
442 mach_msg_port_descriptor_t
*dsc
;
445 kprintf(" PORT name = %p disp = ", dsc
->name
);
446 ipc_print_type_name64(dsc
->disposition
);
450 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
451 case MACH_MSG_OOL_DESCRIPTOR
: {
452 mach_msg_ool_descriptor_t
*dsc
;
454 dsc
= (mach_msg_ool_descriptor_t
*) &saddr
->out_of_line
;
455 kprintf(" OOL%s addr = %p size = 0x%x copy = %s %s\n",
456 type
== MACH_MSG_OOL_DESCRIPTOR
? "" : " VOLATILE",
457 dsc
->address
, dsc
->size
,
458 mm_copy_options_string64(dsc
->copy
),
459 dsc
->deallocate
? "DEALLOC" : "");
462 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
463 mach_msg_ool_ports_descriptor_t
*dsc
;
465 dsc
= (mach_msg_ool_ports_descriptor_t
*) &saddr
->ool_ports
;
467 kprintf(" OOL_PORTS addr = %p count = 0x%x ",
468 dsc
->address
, dsc
->count
);
470 ipc_print_type_name64(dsc
->disposition
);
471 kprintf(" copy = %s %s\n",
472 mm_copy_options_string64(dsc
->copy
),
473 dsc
->deallocate
? "DEALLOC" : "");
478 kprintf(" UNKNOWN DESCRIPTOR 0x%x\n", type
);
485 #define DEBUG_IPC_KMSG_PRINT(kmsg,string) \
486 __unreachable_ok_push \
487 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) { \
488 ipc_kmsg_print64(kmsg, string); \
492 #define DEBUG_IPC_MSG_BODY_PRINT(body,size) \
493 __unreachable_ok_push \
494 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) { \
495 ipc_msg_body_print64(body,size);\
498 #else /* !DEBUG_MSGS_K64 */
499 #define DEBUG_IPC_KMSG_PRINT(kmsg,string)
500 #define DEBUG_IPC_MSG_BODY_PRINT(body,size)
501 #endif /* !DEBUG_MSGS_K64 */
503 extern vm_map_t ipc_kernel_copy_map
;
504 extern vm_size_t ipc_kmsg_max_space
;
505 extern vm_size_t ipc_kmsg_max_vm_space
;
506 extern vm_size_t ipc_kmsg_max_body_space
;
507 extern vm_size_t msg_ool_size_small
;
509 #define MSG_OOL_SIZE_SMALL msg_ool_size_small
511 #if defined(__LP64__)
512 #define MAP_SIZE_DIFFERS(map) (map->max_offset < MACH_VM_MAX_ADDRESS)
513 #define OTHER_OOL_DESCRIPTOR mach_msg_ool_descriptor32_t
514 #define OTHER_OOL_PORTS_DESCRIPTOR mach_msg_ool_ports_descriptor32_t
516 #define MAP_SIZE_DIFFERS(map) (map->max_offset > VM_MAX_ADDRESS)
517 #define OTHER_OOL_DESCRIPTOR mach_msg_ool_descriptor64_t
518 #define OTHER_OOL_PORTS_DESCRIPTOR mach_msg_ool_ports_descriptor64_t
521 #define DESC_SIZE_ADJUSTMENT ((mach_msg_size_t)(sizeof(mach_msg_ool_descriptor64_t) - \
522 sizeof(mach_msg_ool_descriptor32_t)))
524 /* scatter list macros */
526 #define SKIP_PORT_DESCRIPTORS(s, c) \
528 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
530 if ((s)->type.type != MACH_MSG_PORT_DESCRIPTOR) \
535 (s) = MACH_MSG_DESCRIPTOR_NULL; \
539 #define INCREMENT_SCATTER(s, c, d) \
541 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
542 s = (d) ? (mach_msg_descriptor_t *) \
543 ((OTHER_OOL_DESCRIPTOR *)(s) + 1) : \
549 #define KMSG_TRACE_FLAG_TRACED 0x000001
550 #define KMSG_TRACE_FLAG_COMPLEX 0x000002
551 #define KMSG_TRACE_FLAG_OOLMEM 0x000004
552 #define KMSG_TRACE_FLAG_VCPY 0x000008
553 #define KMSG_TRACE_FLAG_PCPY 0x000010
554 #define KMSG_TRACE_FLAG_SND64 0x000020
555 #define KMSG_TRACE_FLAG_RAISEIMP 0x000040
556 #define KMSG_TRACE_FLAG_APP_SRC 0x000080
557 #define KMSG_TRACE_FLAG_APP_DST 0x000100
558 #define KMSG_TRACE_FLAG_DAEMON_SRC 0x000200
559 #define KMSG_TRACE_FLAG_DAEMON_DST 0x000400
560 #define KMSG_TRACE_FLAG_DST_NDFLTQ 0x000800
561 #define KMSG_TRACE_FLAG_SRC_NDFLTQ 0x001000
562 #define KMSG_TRACE_FLAG_DST_SONCE 0x002000
563 #define KMSG_TRACE_FLAG_SRC_SONCE 0x004000
564 #define KMSG_TRACE_FLAG_CHECKIN 0x008000
565 #define KMSG_TRACE_FLAG_ONEWAY 0x010000
566 #define KMSG_TRACE_FLAG_IOKIT 0x020000
567 #define KMSG_TRACE_FLAG_SNDRCV 0x040000
568 #define KMSG_TRACE_FLAG_DSTQFULL 0x080000
569 #define KMSG_TRACE_FLAG_VOUCHER 0x100000
570 #define KMSG_TRACE_FLAG_TIMER 0x200000
571 #define KMSG_TRACE_FLAG_SEMA 0x400000
572 #define KMSG_TRACE_FLAG_DTMPOWNER 0x800000
574 #define KMSG_TRACE_FLAGS_MASK 0xffffff
575 #define KMSG_TRACE_FLAGS_SHIFT 8
577 #define KMSG_TRACE_PORTS_MASK 0xff
578 #define KMSG_TRACE_PORTS_SHIFT 0
580 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
581 extern boolean_t
kdebug_debugid_enabled(uint32_t debugid
);
583 void ipc_kmsg_trace_send(ipc_kmsg_t kmsg
,
584 mach_msg_option_t option
)
586 task_t send_task
= TASK_NULL
;
587 ipc_port_t dst_port
, src_port
;
588 boolean_t is_task_64bit
;
589 mach_msg_header_t
*msg
;
590 mach_msg_trailer_t
*trailer
;
593 uint32_t msg_size
= 0;
594 uint32_t msg_flags
= KMSG_TRACE_FLAG_TRACED
;
595 uint32_t num_ports
= 0;
596 uint32_t send_pid
, dst_pid
;
599 * check to see not only if ktracing is enabled, but if we will
600 * _actually_ emit the KMSG_INFO tracepoint. This saves us a
601 * significant amount of processing (and a port lock hold) in
602 * the non-tracing case.
604 if (__probable((kdebug_enable
& KDEBUG_TRACE
) == 0))
606 if (!kdebug_debugid_enabled(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
)))
609 msg
= kmsg
->ikm_header
;
611 dst_port
= (ipc_port_t
)(msg
->msgh_remote_port
);
612 if (!IPC_PORT_VALID(dst_port
))
616 * Message properties / options
618 if ((option
& (MACH_SEND_MSG
|MACH_RCV_MSG
)) == (MACH_SEND_MSG
|MACH_RCV_MSG
))
619 msg_flags
|= KMSG_TRACE_FLAG_SNDRCV
;
621 if (msg
->msgh_id
>= is_iokit_subsystem
.start
&&
622 msg
->msgh_id
< is_iokit_subsystem
.end
+ 100)
623 msg_flags
|= KMSG_TRACE_FLAG_IOKIT
;
624 /* magic XPC checkin message id (XPC_MESSAGE_ID_CHECKIN) from libxpc */
625 else if (msg
->msgh_id
== 0x77303074u
/* w00t */)
626 msg_flags
|= KMSG_TRACE_FLAG_CHECKIN
;
628 if (msg
->msgh_bits
& MACH_MSGH_BITS_RAISEIMP
)
629 msg_flags
|= KMSG_TRACE_FLAG_RAISEIMP
;
631 if (unsafe_convert_port_to_voucher(kmsg
->ikm_voucher
))
632 msg_flags
|= KMSG_TRACE_FLAG_VOUCHER
;
635 * Sending task / port
637 send_task
= current_task();
638 send_pid
= task_pid(send_task
);
641 if (task_is_daemon(send_task
))
642 msg_flags
|= KMSG_TRACE_FLAG_DAEMON_SRC
;
643 else if (task_is_app(send_task
))
644 msg_flags
|= KMSG_TRACE_FLAG_APP_SRC
;
647 is_task_64bit
= (send_task
->map
->max_offset
> VM_MAX_ADDRESS
);
649 msg_flags
|= KMSG_TRACE_FLAG_SND64
;
651 src_port
= (ipc_port_t
)(msg
->msgh_local_port
);
653 if (src_port
->ip_messages
.imq_qlimit
!= MACH_PORT_QLIMIT_DEFAULT
)
654 msg_flags
|= KMSG_TRACE_FLAG_SRC_NDFLTQ
;
655 switch (MACH_MSGH_BITS_LOCAL(msg
->msgh_bits
)) {
656 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
657 msg_flags
|= KMSG_TRACE_FLAG_SRC_SONCE
;
663 msg_flags
|= KMSG_TRACE_FLAG_ONEWAY
;
668 * Destination task / port
671 if (!ip_active(dst_port
)) {
672 /* dst port is being torn down */
673 dst_pid
= (uint32_t)0xfffffff0;
674 } else if (dst_port
->ip_tempowner
) {
675 msg_flags
|= KMSG_TRACE_FLAG_DTMPOWNER
;
676 if (IIT_NULL
!= dst_port
->ip_imp_task
)
677 dst_pid
= task_pid(dst_port
->ip_imp_task
->iit_task
);
679 dst_pid
= (uint32_t)0xfffffff1;
680 } else if (dst_port
->ip_receiver_name
== MACH_PORT_NULL
) {
681 /* dst_port is otherwise in-transit */
682 dst_pid
= (uint32_t)0xfffffff2;
684 if (dst_port
->ip_receiver
== ipc_space_kernel
) {
687 ipc_space_t dst_space
;
688 dst_space
= dst_port
->ip_receiver
;
689 if (dst_space
&& is_active(dst_space
)) {
690 dst_pid
= task_pid(dst_space
->is_task
);
691 if (task_is_daemon(dst_space
->is_task
))
692 msg_flags
|= KMSG_TRACE_FLAG_DAEMON_DST
;
693 else if (task_is_app(dst_space
->is_task
))
694 msg_flags
|= KMSG_TRACE_FLAG_APP_DST
;
696 /* receiving task is being torn down */
697 dst_pid
= (uint32_t)0xfffffff3;
702 if (dst_port
->ip_messages
.imq_qlimit
!= MACH_PORT_QLIMIT_DEFAULT
)
703 msg_flags
|= KMSG_TRACE_FLAG_DST_NDFLTQ
;
704 if (imq_full(&dst_port
->ip_messages
))
705 msg_flags
|= KMSG_TRACE_FLAG_DSTQFULL
;
707 kotype
= ip_kotype(dst_port
);
713 msg_flags
|= KMSG_TRACE_FLAG_SEMA
;
717 msg_flags
|= KMSG_TRACE_FLAG_TIMER
;
719 case IKOT_MASTER_DEVICE
:
720 case IKOT_IOKIT_CONNECT
:
721 case IKOT_IOKIT_OBJECT
:
722 case IKOT_IOKIT_SPARE
:
723 msg_flags
|= KMSG_TRACE_FLAG_IOKIT
;
729 switch(MACH_MSGH_BITS_REMOTE(msg
->msgh_bits
)) {
730 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
731 msg_flags
|= KMSG_TRACE_FLAG_DST_SONCE
;
739 * Message size / content
741 msg_size
= msg
->msgh_size
- sizeof(mach_msg_header_t
);
743 if (msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
744 mach_msg_body_t
*msg_body
;
745 mach_msg_descriptor_t
*kern_dsc
;
748 msg_flags
|= KMSG_TRACE_FLAG_COMPLEX
;
750 msg_body
= (mach_msg_body_t
*)(kmsg
->ikm_header
+ 1);
751 dsc_count
= (int)msg_body
->msgh_descriptor_count
;
752 kern_dsc
= (mach_msg_descriptor_t
*)(msg_body
+ 1);
754 /* this is gross: see ipc_kmsg_copyin_body()... */
756 msg_size
-= (dsc_count
* 12);
758 for (int i
= 0; i
< dsc_count
; i
++) {
759 switch (kern_dsc
[i
].type
.type
) {
760 case MACH_MSG_PORT_DESCRIPTOR
:
765 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
766 case MACH_MSG_OOL_DESCRIPTOR
: {
767 mach_msg_ool_descriptor_t
*dsc
;
768 dsc
= (mach_msg_ool_descriptor_t
*)&kern_dsc
[i
];
769 msg_flags
|= KMSG_TRACE_FLAG_OOLMEM
;
770 msg_size
+= dsc
->size
;
771 if ((dsc
->size
>= MSG_OOL_SIZE_SMALL
) &&
772 (dsc
->copy
== MACH_MSG_PHYSICAL_COPY
) &&
774 msg_flags
|= KMSG_TRACE_FLAG_PCPY
;
775 else if (dsc
->size
<= MSG_OOL_SIZE_SMALL
)
776 msg_flags
|= KMSG_TRACE_FLAG_PCPY
;
778 msg_flags
|= KMSG_TRACE_FLAG_VCPY
;
782 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
783 mach_msg_ool_ports_descriptor_t
*dsc
;
784 dsc
= (mach_msg_ool_ports_descriptor_t
*)&kern_dsc
[i
];
785 num_ports
+= dsc
->count
;
798 trailer
= (mach_msg_trailer_t
*)((vm_offset_t
)msg
+
799 (vm_offset_t
)msg
->msgh_size
);
800 if (trailer
->msgh_trailer_size
<= sizeof(mach_msg_security_trailer_t
)) {
801 extern security_token_t KERNEL_SECURITY_TOKEN
;
802 mach_msg_security_trailer_t
*strailer
;
803 strailer
= (mach_msg_security_trailer_t
*)trailer
;
805 * verify the sender PID: replies from the kernel often look
806 * like self-talk because the sending port is not reset.
808 if (memcmp(&strailer
->msgh_sender
,
809 &KERNEL_SECURITY_TOKEN
,
810 sizeof(KERNEL_SECURITY_TOKEN
)) == 0) {
812 msg_flags
&= ~(KMSG_TRACE_FLAG_APP_SRC
| KMSG_TRACE_FLAG_DAEMON_SRC
);
816 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
,
821 ((msg_flags
& KMSG_TRACE_FLAGS_MASK
) << KMSG_TRACE_FLAGS_SHIFT
) |
822 ((num_ports
& KMSG_TRACE_PORTS_MASK
) << KMSG_TRACE_PORTS_SHIFT
)
828 /* zone for cached ipc_kmsg_t structures */
829 zone_t ipc_kmsg_zone
;
832 * Forward declarations
838 void ipc_kmsg_clean_body(
840 mach_msg_type_number_t number
,
841 mach_msg_descriptor_t
*desc
);
843 void ipc_kmsg_clean_partial(
845 mach_msg_type_number_t number
,
846 mach_msg_descriptor_t
*desc
,
850 mach_msg_return_t
ipc_kmsg_copyin_body(
856 * We keep a per-processor cache of kernel message buffers.
857 * The cache saves the overhead/locking of using kalloc/kfree.
858 * The per-processor cache seems to miss less than a per-thread cache,
859 * and it also uses less memory. Access to the cache doesn't
864 * Routine: ipc_kmsg_alloc
866 * Allocate a kernel message structure. If we can get one from
867 * the cache, that is best. Otherwise, allocate a new one.
873 mach_msg_size_t msg_and_trailer_size
)
875 mach_msg_size_t max_expanded_size
;
880 * Pad the allocation in case we need to expand the
881 * message descrptors for user spaces with pointers larger than
882 * the kernel's own, or vice versa. We don't know how many descriptors
883 * there are yet, so just assume the whole body could be
884 * descriptors (if there could be any at all).
886 * The expansion space is left in front of the header,
887 * because it is easier to pull the header and descriptors
888 * forward as we process them than it is to push all the
891 mach_msg_size_t size
= msg_and_trailer_size
- MAX_TRAILER_SIZE
;
893 /* compare against implementation upper limit for the body */
894 if (size
> ipc_kmsg_max_body_space
)
897 if (size
> sizeof(mach_msg_base_t
)) {
898 mach_msg_size_t max_desc
= (mach_msg_size_t
)(((size
- sizeof(mach_msg_base_t
)) /
899 sizeof(mach_msg_ool_descriptor32_t
)) *
900 DESC_SIZE_ADJUSTMENT
);
902 /* make sure expansion won't cause wrap */
903 if (msg_and_trailer_size
> MACH_MSG_SIZE_MAX
- max_desc
)
906 max_expanded_size
= msg_and_trailer_size
+ max_desc
;
908 max_expanded_size
= msg_and_trailer_size
;
910 if (max_expanded_size
< IKM_SAVED_MSG_SIZE
)
911 max_expanded_size
= IKM_SAVED_MSG_SIZE
; /* round up for ikm_cache */
913 if (max_expanded_size
== IKM_SAVED_MSG_SIZE
) {
914 struct ikm_cache
*cache
;
917 disable_preemption();
918 cache
= &PROCESSOR_DATA(current_processor(), ikm_cache
);
919 if ((i
= cache
->avail
) > 0) {
920 assert(i
<= IKM_STASH
);
921 kmsg
= cache
->entries
[--i
];
924 ikm_check_init(kmsg
, max_expanded_size
);
925 ikm_set_header(kmsg
, msg_and_trailer_size
);
929 kmsg
= (ipc_kmsg_t
)zalloc(ipc_kmsg_zone
);
931 kmsg
= (ipc_kmsg_t
)kalloc(ikm_plus_overhead(max_expanded_size
));
934 if (kmsg
!= IKM_NULL
) {
935 ikm_init(kmsg
, max_expanded_size
);
936 ikm_set_header(kmsg
, msg_and_trailer_size
);
943 * Routine: ipc_kmsg_free
945 * Free a kernel message buffer. If the kms is preallocated
946 * to a port, just "put it back (marked unused)." We have to
947 * do this with the port locked. The port may have its hold
948 * on our message released. In that case, we have to just
949 * revert the message to a traditional one and free it normally.
958 mach_msg_size_t size
= kmsg
->ikm_size
;
961 assert(!IP_VALID(kmsg
->ikm_voucher
));
963 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_FREE
) | DBG_FUNC_NONE
,
964 VM_KERNEL_ADDRPERM((uintptr_t)kmsg
),
968 * Check to see if the message is bound to the port. If so,
969 * mark it not in use. If the port isn't already dead, then
970 * leave the message associated with it. Otherwise, free it.
972 port
= ikm_prealloc_inuse_port(kmsg
);
973 if (port
!= IP_NULL
) {
975 ikm_prealloc_clear_inuse(kmsg
, port
);
976 if (ip_active(port
) && (port
->ip_premsg
== kmsg
)) {
977 assert(IP_PREALLOC(port
));
983 ip_release(port
); /* May be last reference */
987 * Peek and see if it has to go back in the cache.
989 if (kmsg
->ikm_size
== IKM_SAVED_MSG_SIZE
) {
990 struct ikm_cache
*cache
;
993 disable_preemption();
994 cache
= &PROCESSOR_DATA(current_processor(), ikm_cache
);
995 if ((i
= cache
->avail
) < IKM_STASH
) {
996 cache
->entries
[i
] = kmsg
;
997 cache
->avail
= i
+ 1;
1001 enable_preemption();
1002 zfree(ipc_kmsg_zone
, kmsg
);
1005 kfree(kmsg
, ikm_plus_overhead(size
));
1010 * Routine: ipc_kmsg_enqueue
1017 ipc_kmsg_queue_t queue
,
1020 ipc_kmsg_t first
= queue
->ikmq_base
;
1023 if (first
== IKM_NULL
) {
1024 queue
->ikmq_base
= kmsg
;
1025 kmsg
->ikm_next
= kmsg
;
1026 kmsg
->ikm_prev
= kmsg
;
1028 last
= first
->ikm_prev
;
1029 kmsg
->ikm_next
= first
;
1030 kmsg
->ikm_prev
= last
;
1031 first
->ikm_prev
= kmsg
;
1032 last
->ikm_next
= kmsg
;
1037 * Routine: ipc_kmsg_enqueue_qos
1039 * Enqueue a kmsg, propagating qos
1040 * overrides towards the head of the queue.
1043 * whether the head of the queue had
1044 * it's override-qos adjusted because
1045 * of this insertion.
1049 ipc_kmsg_enqueue_qos(
1050 ipc_kmsg_queue_t queue
,
1053 ipc_kmsg_t first
= queue
->ikmq_base
;
1055 mach_msg_priority_t override
;
1057 if (first
== IKM_NULL
) {
1058 /* insert a first message */
1059 queue
->ikmq_base
= kmsg
;
1060 kmsg
->ikm_next
= kmsg
;
1061 kmsg
->ikm_prev
= kmsg
;
1065 /* insert at the tail */
1066 prev
= first
->ikm_prev
;
1067 kmsg
->ikm_next
= first
;
1068 kmsg
->ikm_prev
= prev
;
1069 first
->ikm_prev
= kmsg
;
1070 prev
->ikm_next
= kmsg
;
1072 /* apply QoS overrides towards the head */
1073 override
= kmsg
->ikm_qos_override
;
1074 while (prev
!= kmsg
&&
1075 override
> prev
->ikm_qos_override
) {
1076 prev
->ikm_qos_override
= override
;
1077 prev
= prev
->ikm_prev
;
1080 /* did we adjust everything? */
1081 return (prev
== kmsg
);
1085 * Routine: ipc_kmsg_override_qos
1087 * Update the override for a given kmsg already
1088 * enqueued, propagating qos override adjustments
1089 * towards the head of the queue.
1092 * whether the head of the queue had
1093 * it's override-qos adjusted because
1094 * of this insertion.
1098 ipc_kmsg_override_qos(
1099 ipc_kmsg_queue_t queue
,
1101 mach_msg_priority_t override
)
1103 ipc_kmsg_t first
= queue
->ikmq_base
;
1104 ipc_kmsg_t cur
= kmsg
;
1106 /* apply QoS overrides towards the head */
1107 while (override
> cur
->ikm_qos_override
) {
1108 cur
->ikm_qos_override
= override
;
1111 cur
= cur
->ikm_next
;
1117 * Routine: ipc_kmsg_dequeue
1119 * Dequeue and return a kmsg.
1124 ipc_kmsg_queue_t queue
)
1128 first
= ipc_kmsg_queue_first(queue
);
1130 if (first
!= IKM_NULL
)
1131 ipc_kmsg_rmqueue(queue
, first
);
1137 * Routine: ipc_kmsg_rmqueue
1139 * Pull a kmsg out of a queue.
1144 ipc_kmsg_queue_t queue
,
1147 ipc_kmsg_t next
, prev
;
1149 assert(queue
->ikmq_base
!= IKM_NULL
);
1151 next
= kmsg
->ikm_next
;
1152 prev
= kmsg
->ikm_prev
;
1155 assert(prev
== kmsg
);
1156 assert(queue
->ikmq_base
== kmsg
);
1158 queue
->ikmq_base
= IKM_NULL
;
1160 if (queue
->ikmq_base
== kmsg
)
1161 queue
->ikmq_base
= next
;
1163 next
->ikm_prev
= prev
;
1164 prev
->ikm_next
= next
;
1166 /* XXX Temporary debug logic */
1167 assert((kmsg
->ikm_next
= IKM_BOGUS
) == IKM_BOGUS
);
1168 assert((kmsg
->ikm_prev
= IKM_BOGUS
) == IKM_BOGUS
);
1172 * Routine: ipc_kmsg_queue_next
1174 * Return the kmsg following the given kmsg.
1175 * (Or IKM_NULL if it is the last one in the queue.)
1179 ipc_kmsg_queue_next(
1180 ipc_kmsg_queue_t queue
,
1185 assert(queue
->ikmq_base
!= IKM_NULL
);
1187 next
= kmsg
->ikm_next
;
1188 if (queue
->ikmq_base
== next
)
1195 * Routine: ipc_kmsg_destroy
1197 * Destroys a kernel message. Releases all rights,
1198 * references, and memory held by the message.
1199 * Frees the message.
1209 * Destroying a message can cause more messages to be destroyed.
1210 * Curtail recursion by putting messages on the deferred
1211 * destruction queue. If this was the first message on the
1212 * queue, this instance must process the full queue.
1214 if (ipc_kmsg_delayed_destroy(kmsg
))
1215 ipc_kmsg_reap_delayed();
1219 * Routine: ipc_kmsg_delayed_destroy
1221 * Enqueues a kernel message for deferred destruction.
1223 * Boolean indicator that the caller is responsible to reap
1224 * deferred messages.
1227 boolean_t
ipc_kmsg_delayed_destroy(
1230 ipc_kmsg_queue_t queue
= &(current_thread()->ith_messages
);
1231 boolean_t first
= ipc_kmsg_queue_empty(queue
);
1233 ipc_kmsg_enqueue(queue
, kmsg
);
1238 * Routine: ipc_kmsg_destroy_queue
1240 * Destroys messages from the per-thread
1241 * deferred reaping queue.
1247 ipc_kmsg_reap_delayed(void)
1249 ipc_kmsg_queue_t queue
= &(current_thread()->ith_messages
);
1253 * must leave kmsg in queue while cleaning it to assure
1254 * no nested calls recurse into here.
1256 while ((kmsg
= ipc_kmsg_queue_first(queue
)) != IKM_NULL
) {
1257 ipc_kmsg_clean(kmsg
);
1258 ipc_kmsg_rmqueue(queue
, kmsg
);
1259 ipc_kmsg_free(kmsg
);
1264 * Routine: ipc_kmsg_clean_body
1266 * Cleans the body of a kernel message.
1267 * Releases all rights, references, and memory.
1272 static unsigned int _ipc_kmsg_clean_invalid_desc
= 0;
1274 ipc_kmsg_clean_body(
1275 __unused ipc_kmsg_t kmsg
,
1276 mach_msg_type_number_t number
,
1277 mach_msg_descriptor_t
*saddr
)
1279 mach_msg_type_number_t i
;
1284 for (i
= 0 ; i
< number
; i
++, saddr
++ ) {
1286 switch (saddr
->type
.type
) {
1288 case MACH_MSG_PORT_DESCRIPTOR
: {
1289 mach_msg_port_descriptor_t
*dsc
;
1294 * Destroy port rights carried in the message
1296 if (!IO_VALID((ipc_object_t
) dsc
->name
))
1298 ipc_object_destroy((ipc_object_t
) dsc
->name
, dsc
->disposition
);
1301 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
1302 case MACH_MSG_OOL_DESCRIPTOR
: {
1303 mach_msg_ool_descriptor_t
*dsc
;
1305 dsc
= (mach_msg_ool_descriptor_t
*)&saddr
->out_of_line
;
1308 * Destroy memory carried in the message
1310 if (dsc
->size
== 0) {
1311 assert(dsc
->address
== (void *) 0);
1313 vm_map_copy_discard((vm_map_copy_t
) dsc
->address
);
1317 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
1318 ipc_object_t
*objects
;
1319 mach_msg_type_number_t j
;
1320 mach_msg_ool_ports_descriptor_t
*dsc
;
1322 dsc
= (mach_msg_ool_ports_descriptor_t
*)&saddr
->ool_ports
;
1323 objects
= (ipc_object_t
*) dsc
->address
;
1325 if (dsc
->count
== 0) {
1329 assert(objects
!= (ipc_object_t
*) 0);
1331 /* destroy port rights carried in the message */
1333 for (j
= 0; j
< dsc
->count
; j
++) {
1334 ipc_object_t object
= objects
[j
];
1336 if (!IO_VALID(object
))
1339 ipc_object_destroy(object
, dsc
->disposition
);
1342 /* destroy memory carried in the message */
1344 assert(dsc
->count
!= 0);
1347 (vm_size_t
) dsc
->count
* sizeof(mach_port_t
));
1351 _ipc_kmsg_clean_invalid_desc
++; /* don't understand this type of descriptor */
1358 * Routine: ipc_kmsg_clean_partial
1360 * Cleans a partially-acquired kernel message.
1361 * number is the index of the type descriptor
1362 * in the body of the message that contained the error.
1363 * If dolast, the memory and port rights in this last
1364 * type spec are also cleaned. In that case, number
1365 * specifies the number of port rights to clean.
1371 ipc_kmsg_clean_partial(
1373 mach_msg_type_number_t number
,
1374 mach_msg_descriptor_t
*desc
,
1378 ipc_object_t object
;
1379 mach_msg_bits_t mbits
= kmsg
->ikm_header
->msgh_bits
;
1381 /* deal with importance chain while we still have dest and voucher references */
1382 ipc_importance_clean(kmsg
);
1384 object
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
1385 assert(IO_VALID(object
));
1386 ipc_object_destroy_dest(object
, MACH_MSGH_BITS_REMOTE(mbits
));
1388 object
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
1389 if (IO_VALID(object
))
1390 ipc_object_destroy(object
, MACH_MSGH_BITS_LOCAL(mbits
));
1392 object
= (ipc_object_t
) kmsg
->ikm_voucher
;
1393 if (IO_VALID(object
)) {
1394 assert(MACH_MSGH_BITS_VOUCHER(mbits
) == MACH_MSG_TYPE_MOVE_SEND
);
1395 ipc_object_destroy(object
, MACH_MSG_TYPE_PORT_SEND
);
1396 kmsg
->ikm_voucher
= IP_NULL
;
1400 (void) vm_deallocate(ipc_kernel_copy_map
, paddr
, length
);
1403 ipc_kmsg_clean_body(kmsg
, number
, desc
);
1407 * Routine: ipc_kmsg_clean
1409 * Cleans a kernel message. Releases all rights,
1410 * references, and memory held by the message.
1419 ipc_object_t object
;
1420 mach_msg_bits_t mbits
;
1422 /* deal with importance chain while we still have dest and voucher references */
1423 ipc_importance_clean(kmsg
);
1425 mbits
= kmsg
->ikm_header
->msgh_bits
;
1426 object
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
1427 if (IO_VALID(object
))
1428 ipc_object_destroy_dest(object
, MACH_MSGH_BITS_REMOTE(mbits
));
1430 object
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
1431 if (IO_VALID(object
))
1432 ipc_object_destroy(object
, MACH_MSGH_BITS_LOCAL(mbits
));
1434 object
= (ipc_object_t
) kmsg
->ikm_voucher
;
1435 if (IO_VALID(object
)) {
1436 assert(MACH_MSGH_BITS_VOUCHER(mbits
) == MACH_MSG_TYPE_MOVE_SEND
);
1437 ipc_object_destroy(object
, MACH_MSG_TYPE_PORT_SEND
);
1438 kmsg
->ikm_voucher
= IP_NULL
;
1441 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
1442 mach_msg_body_t
*body
;
1444 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
1445 ipc_kmsg_clean_body(kmsg
, body
->msgh_descriptor_count
,
1446 (mach_msg_descriptor_t
*)(body
+ 1));
1451 * Routine: ipc_kmsg_set_prealloc
1453 * Assign a kmsg as a preallocated message buffer to a port.
1459 ipc_kmsg_set_prealloc(
1463 assert(kmsg
->ikm_prealloc
== IP_NULL
);
1465 kmsg
->ikm_prealloc
= IP_NULL
;
1466 IP_SET_PREALLOC(port
, kmsg
);
1470 * Routine: ipc_kmsg_clear_prealloc
1472 * Release the Assignment of a preallocated message buffer from a port.
1477 ipc_kmsg_clear_prealloc(
1481 assert(kmsg
->ikm_prealloc
== port
);
1483 kmsg
->ikm_prealloc
= IP_NULL
;
1484 IP_CLEAR_PREALLOC(port
, kmsg
);
1488 * Routine: ipc_kmsg_prealloc
1490 * Wraper to ipc_kmsg_alloc() to account for
1491 * header expansion requirements.
1494 ipc_kmsg_prealloc(mach_msg_size_t size
)
1496 #if defined(__LP64__)
1497 if (size
> MACH_MSG_SIZE_MAX
- LEGACY_HEADER_SIZE_DELTA
)
1500 size
+= LEGACY_HEADER_SIZE_DELTA
;
1502 return ipc_kmsg_alloc(size
);
1507 * Routine: ipc_kmsg_get
1509 * Allocates a kernel message buffer.
1510 * Copies a user message to the message buffer.
1514 * MACH_MSG_SUCCESS Acquired a message buffer.
1515 * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
1516 * MACH_SEND_MSG_TOO_SMALL Message size not long-word multiple.
1517 * MACH_SEND_TOO_LARGE Message too large to ever be sent.
1518 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
1519 * MACH_SEND_INVALID_DATA Couldn't copy message data.
1524 mach_vm_address_t msg_addr
,
1525 mach_msg_size_t size
,
1528 mach_msg_size_t msg_and_trailer_size
;
1530 mach_msg_max_trailer_t
*trailer
;
1531 mach_msg_legacy_base_t legacy_base
;
1532 mach_msg_size_t len_copied
;
1533 legacy_base
.body
.msgh_descriptor_count
= 0;
1535 if ((size
< sizeof(mach_msg_legacy_header_t
)) || (size
& 3))
1536 return MACH_SEND_MSG_TOO_SMALL
;
1538 if (size
> ipc_kmsg_max_body_space
)
1539 return MACH_SEND_TOO_LARGE
;
1541 if(size
== sizeof(mach_msg_legacy_header_t
))
1542 len_copied
= sizeof(mach_msg_legacy_header_t
);
1544 len_copied
= sizeof(mach_msg_legacy_base_t
);
1546 if (copyinmsg(msg_addr
, (char *)&legacy_base
, len_copied
))
1547 return MACH_SEND_INVALID_DATA
;
1549 msg_addr
+= sizeof(legacy_base
.header
);
1550 #if defined(__LP64__)
1551 size
+= LEGACY_HEADER_SIZE_DELTA
;
1553 /* unreachable if !DEBUG */
1554 __unreachable_ok_push
1555 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK
)) {
1557 for (j
=0; j
<sizeof(legacy_base
.header
); j
++) {
1558 kprintf("%02x\n", ((unsigned char*)&legacy_base
.header
)[j
]);
1561 __unreachable_ok_pop
1563 msg_and_trailer_size
= size
+ MAX_TRAILER_SIZE
;
1564 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
1565 if (kmsg
== IKM_NULL
)
1566 return MACH_SEND_NO_BUFFER
;
1568 kmsg
->ikm_header
->msgh_size
= size
;
1569 kmsg
->ikm_header
->msgh_bits
= legacy_base
.header
.msgh_bits
;
1570 kmsg
->ikm_header
->msgh_remote_port
= CAST_MACH_NAME_TO_PORT(legacy_base
.header
.msgh_remote_port
);
1571 kmsg
->ikm_header
->msgh_local_port
= CAST_MACH_NAME_TO_PORT(legacy_base
.header
.msgh_local_port
);
1572 kmsg
->ikm_header
->msgh_voucher_port
= legacy_base
.header
.msgh_voucher_port
;
1573 kmsg
->ikm_header
->msgh_id
= legacy_base
.header
.msgh_id
;
1575 DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_get header:\n"
1578 " remote_port: %p\n"
1580 " voucher_port: 0x%.8x\n"
1582 kmsg
->ikm_header
->msgh_size
,
1583 kmsg
->ikm_header
->msgh_bits
,
1584 kmsg
->ikm_header
->msgh_remote_port
,
1585 kmsg
->ikm_header
->msgh_local_port
,
1586 kmsg
->ikm_header
->msgh_voucher_port
,
1587 kmsg
->ikm_header
->msgh_id
);
1589 if (copyinmsg(msg_addr
, (char *)(kmsg
->ikm_header
+ 1), size
- (mach_msg_size_t
)sizeof(mach_msg_header_t
))) {
1590 ipc_kmsg_free(kmsg
);
1591 return MACH_SEND_INVALID_DATA
;
1594 /* unreachable if !DEBUG */
1595 __unreachable_ok_push
1596 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK
))
1598 kprintf("body: size: %lu\n", (size
- sizeof(mach_msg_header_t
)));
1600 for(i
=0;i
*4 < (size
- sizeof(mach_msg_header_t
));i
++)
1602 kprintf("%.4x\n",((uint32_t *)(kmsg
->ikm_header
+ 1))[i
]);
1605 __unreachable_ok_pop
1606 DEBUG_IPC_KMSG_PRINT(kmsg
, "ipc_kmsg_get()");
1609 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
1610 * However, the internal size field of the trailer (msgh_trailer_size)
1611 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
1612 * the cases where no implicit data is requested.
1614 trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ size
);
1615 trailer
->msgh_sender
= current_thread()->task
->sec_token
;
1616 trailer
->msgh_audit
= current_thread()->task
->audit_token
;
1617 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
1618 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
1621 if(trcWork
.traceMask
) dbgTrace(0x1100, (unsigned int)kmsg
->ikm_header
->msgh_id
,
1622 (unsigned int)kmsg
->ikm_header
->msgh_remote_port
,
1623 (unsigned int)kmsg
->ikm_header
->msgh_local_port
, 0);
1626 trailer
->msgh_labels
.sender
= 0;
1628 return MACH_MSG_SUCCESS
;
1632 * Routine: ipc_kmsg_get_from_kernel
1634 * First checks for a preallocated message
1635 * reserved for kernel clients. If not found -
1636 * allocates a new kernel message buffer.
1637 * Copies a kernel message to the message buffer.
1638 * Only resource errors are allowed.
1641 * Ports in header are ipc_port_t.
1643 * MACH_MSG_SUCCESS Acquired a message buffer.
1644 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
1648 ipc_kmsg_get_from_kernel(
1649 mach_msg_header_t
*msg
,
1650 mach_msg_size_t size
,
1654 mach_msg_size_t msg_and_trailer_size
;
1655 mach_msg_max_trailer_t
*trailer
;
1656 ipc_port_t dest_port
;
1658 assert(size
>= sizeof(mach_msg_header_t
));
1659 assert((size
& 3) == 0);
1661 dest_port
= (ipc_port_t
)msg
->msgh_remote_port
;
1663 msg_and_trailer_size
= size
+ MAX_TRAILER_SIZE
;
1666 * See if the port has a pre-allocated kmsg for kernel
1667 * clients. These are set up for those kernel clients
1668 * which cannot afford to wait.
1670 if (IP_VALID(dest_port
) && IP_PREALLOC(dest_port
)) {
1671 mach_msg_size_t max_desc
= 0;
1674 if (!ip_active(dest_port
)) {
1675 ip_unlock(dest_port
);
1676 return MACH_SEND_NO_BUFFER
;
1678 assert(IP_PREALLOC(dest_port
));
1679 kmsg
= dest_port
->ip_premsg
;
1680 if (ikm_prealloc_inuse(kmsg
)) {
1681 ip_unlock(dest_port
);
1682 return MACH_SEND_NO_BUFFER
;
1684 #if !defined(__LP64__)
1685 if (msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
1686 assert(size
> sizeof(mach_msg_base_t
));
1687 max_desc
= ((mach_msg_base_t
*)msg
)->body
.msgh_descriptor_count
*
1688 DESC_SIZE_ADJUSTMENT
;
1691 if (msg_and_trailer_size
> kmsg
->ikm_size
- max_desc
) {
1692 ip_unlock(dest_port
);
1693 return MACH_SEND_TOO_LARGE
;
1695 ikm_prealloc_set_inuse(kmsg
, dest_port
);
1696 ikm_set_header(kmsg
, msg_and_trailer_size
);
1697 ip_unlock(dest_port
);
1701 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
1702 if (kmsg
== IKM_NULL
)
1703 return MACH_SEND_NO_BUFFER
;
1706 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, size
);
1708 kmsg
->ikm_header
->msgh_size
= size
;
1711 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
1712 * However, the internal size field of the trailer (msgh_trailer_size)
1713 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to
1714 * optimize the cases where no implicit data is requested.
1716 trailer
= (mach_msg_max_trailer_t
*)
1717 ((vm_offset_t
)kmsg
->ikm_header
+ size
);
1718 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
1719 trailer
->msgh_audit
= KERNEL_AUDIT_TOKEN
;
1720 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
1721 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
1723 trailer
->msgh_labels
.sender
= 0;
1726 return MACH_MSG_SUCCESS
;
1730 * Routine: ipc_kmsg_send
1732 * Send a message. The message holds a reference
1733 * for the destination port in the msgh_remote_port field.
1735 * If unsuccessful, the caller still has possession of
1736 * the message and must do something with it. If successful,
1737 * the message is queued, given to a receiver, destroyed,
1738 * or handled directly by the kernel via mach_msg.
1742 * MACH_MSG_SUCCESS The message was accepted.
1743 * MACH_SEND_TIMED_OUT Caller still has message.
1744 * MACH_SEND_INTERRUPTED Caller still has message.
1745 * MACH_SEND_INVALID_DEST Caller still has message.
1752 mach_msg_option_t option
,
1753 mach_msg_timeout_t send_timeout
)
1756 thread_t th
= current_thread();
1757 mach_msg_return_t error
= MACH_MSG_SUCCESS
;
1758 boolean_t kernel_reply
= FALSE
;
1760 /* Check if honor qlimit flag is set on thread. */
1761 if ((th
->options
& TH_OPT_HONOR_QLIMIT
) == TH_OPT_HONOR_QLIMIT
) {
1762 /* Remove the MACH_SEND_ALWAYS flag to honor queue limit. */
1763 option
&= (~MACH_SEND_ALWAYS
);
1764 /* Add the timeout flag since the message queue might be full. */
1765 option
|= MACH_SEND_TIMEOUT
;
1766 th
->options
&= (~TH_OPT_HONOR_QLIMIT
);
1769 #if IMPORTANCE_INHERITANCE
1770 boolean_t did_importance
= FALSE
;
1771 #if IMPORTANCE_DEBUG
1772 mach_msg_id_t imp_msgh_id
= -1;
1773 int sender_pid
= -1;
1774 #endif /* IMPORTANCE_DEBUG */
1775 #endif /* IMPORTANCE_INHERITANCE */
1777 /* don't allow the creation of a circular loop */
1778 if (kmsg
->ikm_header
->msgh_bits
& MACH_MSGH_BITS_CIRCULAR
) {
1779 ipc_kmsg_destroy(kmsg
);
1780 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, MACH_MSGH_BITS_CIRCULAR
);
1781 return MACH_MSG_SUCCESS
;
1784 ipc_voucher_send_preprocessing(kmsg
);
1786 port
= (ipc_port_t
) kmsg
->ikm_header
->msgh_remote_port
;
1787 assert(IP_VALID(port
));
1790 #if IMPORTANCE_INHERITANCE
1792 #endif /* IMPORTANCE_INHERITANCE */
1794 * Can't deliver to a dead port.
1795 * However, we can pretend it got sent
1796 * and was then immediately destroyed.
1798 if (!ip_active(port
)) {
1801 if (MACH_NODE_VALID(kmsg
->ikm_node
) && FPORT_VALID(port
->ip_messages
.imq_fport
))
1802 flipc_msg_ack(kmsg
->ikm_node
, &port
->ip_messages
, FALSE
);
1804 ip_release(port
); /* JMM - Future: release right, not just ref */
1805 kmsg
->ikm_header
->msgh_remote_port
= MACH_PORT_NULL
;
1806 ipc_kmsg_destroy(kmsg
);
1807 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, MACH_SEND_INVALID_DEST
);
1808 return MACH_MSG_SUCCESS
;
1811 if (port
->ip_receiver
== ipc_space_kernel
) {
1814 * We can check ip_receiver == ipc_space_kernel
1815 * before checking that the port is active because
1816 * ipc_port_dealloc_kernel clears ip_receiver
1817 * before destroying a kernel port.
1819 assert(ip_active(port
));
1820 port
->ip_messages
.imq_seqno
++;
1823 current_task()->messages_sent
++;
1826 * Call the server routine, and get the reply message to send.
1828 kmsg
= ipc_kobject_server(kmsg
, option
);
1829 if (kmsg
== IKM_NULL
)
1830 return MACH_MSG_SUCCESS
;
1832 /* restart the KMSG_INFO tracing for the reply message */
1833 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
1834 port
= (ipc_port_t
) kmsg
->ikm_header
->msgh_remote_port
;
1835 assert(IP_VALID(port
));
1837 /* fall thru with reply - same options */
1838 kernel_reply
= TRUE
;
1839 if (!ip_active(port
))
1840 error
= MACH_SEND_INVALID_DEST
;
1843 #if IMPORTANCE_INHERITANCE
1845 * Need to see if this message needs importance donation and/or
1846 * propagation. That routine can drop the port lock temporarily.
1847 * If it does we'll have to revalidate the destination.
1849 if (did_importance
== FALSE
) {
1850 did_importance
= TRUE
;
1851 if (ipc_importance_send(kmsg
, option
))
1854 #endif /* IMPORTANCE_INHERITANCE */
1856 if (error
!= MACH_MSG_SUCCESS
) {
1860 * We have a valid message and a valid reference on the port.
1861 * we can unlock the port and call mqueue_send() on its message
1862 * queue. Lock message queue while port is locked.
1864 imq_lock(&port
->ip_messages
);
1867 error
= ipc_mqueue_send(&port
->ip_messages
, kmsg
, option
,
1871 #if IMPORTANCE_INHERITANCE
1872 if (did_importance
== TRUE
) {
1873 __unused
int importance_cleared
= 0;
1875 case MACH_SEND_TIMED_OUT
:
1876 case MACH_SEND_NO_BUFFER
:
1877 case MACH_SEND_INTERRUPTED
:
1878 case MACH_SEND_INVALID_DEST
:
1880 * We still have the kmsg and its
1881 * reference on the port. But we
1882 * have to back out the importance
1885 * The port could have changed hands,
1886 * be inflight to another destination,
1887 * etc... But in those cases our
1888 * back-out will find the new owner
1889 * (and all the operations that
1890 * transferred the right should have
1891 * applied their own boost adjustments
1892 * to the old owner(s)).
1894 importance_cleared
= 1;
1895 ipc_importance_clean(kmsg
);
1898 case MACH_MSG_SUCCESS
:
1902 #if IMPORTANCE_DEBUG
1903 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_MSG
, IMP_MSG_SEND
)) | DBG_FUNC_END
,
1904 task_pid(current_task()), sender_pid
, imp_msgh_id
, importance_cleared
, 0);
1905 #endif /* IMPORTANCE_DEBUG */
1907 #endif /* IMPORTANCE_INHERITANCE */
1910 * If the port has been destroyed while we wait, treat the message
1911 * as a successful delivery (like we do for an inactive port).
1913 if (error
== MACH_SEND_INVALID_DEST
) {
1915 if (MACH_NODE_VALID(kmsg
->ikm_node
) && FPORT_VALID(port
->ip_messages
.imq_fport
))
1916 flipc_msg_ack(kmsg
->ikm_node
, &port
->ip_messages
, FALSE
);
1918 ip_release(port
); /* JMM - Future: release right, not just ref */
1919 kmsg
->ikm_header
->msgh_remote_port
= MACH_PORT_NULL
;
1920 ipc_kmsg_destroy(kmsg
);
1921 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, MACH_SEND_INVALID_DEST
);
1922 return MACH_MSG_SUCCESS
;
1925 if (error
!= MACH_MSG_SUCCESS
&& kernel_reply
) {
1927 * Kernel reply messages that fail can't be allowed to
1928 * pseudo-receive on error conditions. We need to just treat
1929 * the message as a successful delivery.
1932 if (MACH_NODE_VALID(kmsg
->ikm_node
) && FPORT_VALID(port
->ip_messages
.imq_fport
))
1933 flipc_msg_ack(kmsg
->ikm_node
, &port
->ip_messages
, FALSE
);
1935 ip_release(port
); /* JMM - Future: release right, not just ref */
1936 kmsg
->ikm_header
->msgh_remote_port
= MACH_PORT_NULL
;
1937 ipc_kmsg_destroy(kmsg
);
1938 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, error
);
1939 return MACH_MSG_SUCCESS
;
1945 * Routine: ipc_kmsg_put
1947 * Copies a message buffer to a user message.
1948 * Copies only the specified number of bytes.
1949 * Frees the message buffer.
1951 * Nothing locked. The message buffer must have clean
1954 * MACH_MSG_SUCCESS Copied data out of message buffer.
1955 * MACH_RCV_INVALID_DATA Couldn't copy to user message.
1961 mach_msg_option_t option
,
1962 mach_vm_address_t rcv_addr
,
1963 mach_msg_size_t rcv_size
,
1964 mach_msg_size_t trailer_size
,
1965 mach_msg_size_t
*sizep
)
1967 mach_msg_size_t size
= kmsg
->ikm_header
->msgh_size
+ trailer_size
;
1968 mach_msg_return_t mr
;
1970 DEBUG_IPC_KMSG_PRINT(kmsg
, "ipc_kmsg_put()");
1973 DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_put header:\n"
1976 " remote_port: %p\n"
1978 " voucher_port: 0x%.8x\n"
1980 kmsg
->ikm_header
->msgh_size
,
1981 kmsg
->ikm_header
->msgh_bits
,
1982 kmsg
->ikm_header
->msgh_remote_port
,
1983 kmsg
->ikm_header
->msgh_local_port
,
1984 kmsg
->ikm_header
->msgh_voucher_port
,
1985 kmsg
->ikm_header
->msgh_id
);
1987 #if defined(__LP64__)
1988 if (current_task() != kernel_task
) { /* don't if receiver expects fully-cooked in-kernel msg; ux_exception */
1989 mach_msg_legacy_header_t
*legacy_header
=
1990 (mach_msg_legacy_header_t
*)((vm_offset_t
)(kmsg
->ikm_header
) + LEGACY_HEADER_SIZE_DELTA
);
1992 mach_msg_bits_t bits
= kmsg
->ikm_header
->msgh_bits
;
1993 mach_msg_size_t msg_size
= kmsg
->ikm_header
->msgh_size
;
1994 mach_port_name_t remote_port
= CAST_MACH_PORT_TO_NAME(kmsg
->ikm_header
->msgh_remote_port
);
1995 mach_port_name_t local_port
= CAST_MACH_PORT_TO_NAME(kmsg
->ikm_header
->msgh_local_port
);
1996 mach_port_name_t voucher_port
= kmsg
->ikm_header
->msgh_voucher_port
;
1997 mach_msg_id_t id
= kmsg
->ikm_header
->msgh_id
;
1999 legacy_header
->msgh_id
= id
;
2000 legacy_header
->msgh_local_port
= local_port
;
2001 legacy_header
->msgh_remote_port
= remote_port
;
2002 legacy_header
->msgh_voucher_port
= voucher_port
;
2003 legacy_header
->msgh_size
= msg_size
- LEGACY_HEADER_SIZE_DELTA
;
2004 legacy_header
->msgh_bits
= bits
;
2006 size
-= LEGACY_HEADER_SIZE_DELTA
;
2007 kmsg
->ikm_header
= (mach_msg_header_t
*)legacy_header
;
2011 /* unreachable if !DEBUG */
2012 __unreachable_ok_push
2013 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK
)) {
2014 kprintf("ipc_kmsg_put header+body: %d\n", (size
));
2016 for(i
=0;i
*4 < size
;i
++)
2018 kprintf("%.4x\n",((uint32_t *)kmsg
->ikm_header
)[i
]);
2020 kprintf("type: %d\n", ((mach_msg_type_descriptor_t
*)(((mach_msg_base_t
*)kmsg
->ikm_header
)+1))->type
);
2022 __unreachable_ok_pop
2024 /* Re-Compute target address if using stack-style delivery */
2025 if (option
& MACH_RCV_STACK
) {
2026 rcv_addr
+= rcv_size
- size
;
2029 if (copyoutmsg((const char *) kmsg
->ikm_header
, rcv_addr
, size
)) {
2030 mr
= MACH_RCV_INVALID_DATA
;
2033 mr
= MACH_MSG_SUCCESS
;
2035 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_LINK
) | DBG_FUNC_NONE
,
2036 (rcv_addr
>= VM_MIN_KERNEL_AND_KEXT_ADDRESS
||
2037 rcv_addr
+ size
>= VM_MIN_KERNEL_AND_KEXT_ADDRESS
) ? (uintptr_t)0 : (uintptr_t)rcv_addr
,
2038 VM_KERNEL_ADDRPERM((uintptr_t)kmsg
),
2039 1 /* this is on the receive/copyout path */,
2042 ipc_kmsg_free(kmsg
);
2050 * Routine: ipc_kmsg_put_to_kernel
2052 * Copies a message buffer to a kernel message.
2053 * Frees the message buffer.
2054 * No errors allowed.
2060 ipc_kmsg_put_to_kernel(
2061 mach_msg_header_t
*msg
,
2063 mach_msg_size_t size
)
2065 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, size
);
2067 ipc_kmsg_free(kmsg
);
2070 unsigned long pthread_priority_canonicalize(unsigned long priority
, boolean_t propagation
);
2075 mach_msg_option_t options
,
2076 mach_msg_priority_t override
)
2080 kr
= ipc_get_pthpriority_from_kmsg_voucher(kmsg
, &kmsg
->ikm_qos
);
2081 if (kr
!= KERN_SUCCESS
) {
2082 kmsg
->ikm_qos
= MACH_MSG_PRIORITY_UNSPECIFIED
;
2084 kmsg
->ikm_qos_override
= kmsg
->ikm_qos
;
2086 if (options
& MACH_SEND_OVERRIDE
) {
2087 unsigned long canonical
;
2088 mach_msg_priority_t canon
;
2090 canonical
= pthread_priority_canonicalize(override
, TRUE
);
2091 canon
= (mach_msg_priority_t
)canonical
;
2092 if (canon
> kmsg
->ikm_qos
)
2093 kmsg
->ikm_qos_override
= canon
;
2098 * Routine: ipc_kmsg_copyin_header
2100 * "Copy-in" port rights in the header of a message.
2101 * Operates atomically; if it doesn't succeed the
2102 * message header and the space are left untouched.
2103 * If it does succeed the remote/local port fields
2104 * contain object pointers instead of port names,
2105 * and the bits field is updated. The destination port
2106 * will be a valid port pointer.
2111 * MACH_MSG_SUCCESS Successful copyin.
2112 * MACH_SEND_INVALID_HEADER
2113 * Illegal value in the message header bits.
2114 * MACH_SEND_INVALID_DEST The space is dead.
2115 * MACH_SEND_INVALID_DEST Can't copyin destination port.
2116 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
2117 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
2118 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
2122 ipc_kmsg_copyin_header(
2125 mach_msg_priority_t override
,
2126 mach_msg_option_t
*optionp
)
2128 mach_msg_header_t
*msg
= kmsg
->ikm_header
;
2129 mach_msg_bits_t mbits
= msg
->msgh_bits
& MACH_MSGH_BITS_USER
;
2130 mach_port_name_t dest_name
= CAST_MACH_PORT_TO_NAME(msg
->msgh_remote_port
);
2131 mach_port_name_t reply_name
= CAST_MACH_PORT_TO_NAME(msg
->msgh_local_port
);
2132 mach_port_name_t voucher_name
= MACH_PORT_NULL
;
2135 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
2136 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
2137 mach_msg_type_name_t voucher_type
= MACH_MSGH_BITS_VOUCHER(mbits
);
2138 ipc_object_t dest_port
= IO_NULL
;
2139 ipc_object_t reply_port
= IO_NULL
;
2140 ipc_port_t dest_soright
= IP_NULL
;
2141 ipc_port_t reply_soright
= IP_NULL
;
2142 ipc_port_t voucher_soright
= IP_NULL
;
2143 ipc_port_t release_port
= IP_NULL
;
2144 ipc_port_t voucher_port
= IP_NULL
;
2145 ipc_port_t voucher_release_port
= IP_NULL
;
2146 ipc_entry_t dest_entry
= IE_NULL
;
2147 ipc_entry_t reply_entry
= IE_NULL
;
2148 ipc_entry_t voucher_entry
= IE_NULL
;
2151 #if IMPORTANCE_INHERITANCE
2152 boolean_t needboost
= FALSE
;
2153 #endif /* IMPORTANCE_INHERITANCE */
2155 if ((mbits
!= msg
->msgh_bits
) ||
2156 (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type
)) ||
2157 ((reply_type
== 0) ?
2158 (reply_name
!= MACH_PORT_NULL
) :
2159 !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type
)))
2160 return MACH_SEND_INVALID_HEADER
;
2162 if (!MACH_PORT_VALID(dest_name
))
2163 return MACH_SEND_INVALID_DEST
;
2165 is_write_lock(space
);
2166 if (!is_active(space
)) {
2167 is_write_unlock(space
);
2168 return MACH_SEND_INVALID_DEST
;
2170 /* space locked and active */
2173 * If there is a voucher specified, make sure the disposition is
2174 * valid and the entry actually refers to a voucher port. Don't
2175 * actually copy in until we validate destination and reply.
2177 if (voucher_type
!= MACH_MSGH_BITS_ZERO
) {
2179 voucher_name
= msg
->msgh_voucher_port
;
2181 if (voucher_name
== MACH_PORT_DEAD
||
2182 (voucher_type
!= MACH_MSG_TYPE_MOVE_SEND
&&
2183 voucher_type
!= MACH_MSG_TYPE_COPY_SEND
)) {
2184 is_write_unlock(space
);
2185 return MACH_SEND_INVALID_VOUCHER
;
2188 if (voucher_name
!= MACH_PORT_NULL
) {
2189 voucher_entry
= ipc_entry_lookup(space
, voucher_name
);
2190 if (voucher_entry
== IE_NULL
||
2191 (voucher_entry
->ie_bits
& MACH_PORT_TYPE_SEND
) == 0 ||
2192 io_kotype(voucher_entry
->ie_object
) != IKOT_VOUCHER
) {
2193 is_write_unlock(space
);
2194 return MACH_SEND_INVALID_VOUCHER
;
2197 voucher_type
= MACH_MSG_TYPE_MOVE_SEND
;
2202 * Handle combinations of validating destination and reply; along
2203 * with copying in destination, reply, and voucher in an atomic way.
2206 if (dest_name
== voucher_name
) {
2209 * If the destination name is the same as the voucher name,
2210 * the voucher_entry must already be known. Either that or
2211 * the destination name is MACH_PORT_NULL (i.e. invalid).
2213 dest_entry
= voucher_entry
;
2214 if (dest_entry
== IE_NULL
) {
2219 * Make sure a future copyin of the reply port will succeed.
2220 * Once we start copying in the dest/voucher pair, we can't
2223 if (MACH_PORT_VALID(reply_name
)) {
2224 assert(reply_type
!= 0); /* because reply_name not null */
2226 /* It is just WRONG if dest, voucher, and reply are all the same. */
2227 if (voucher_name
== reply_name
) {
2230 reply_entry
= ipc_entry_lookup(space
, reply_name
);
2231 if (reply_entry
== IE_NULL
) {
2234 assert(dest_entry
!= reply_entry
); /* names are not equal */
2235 if (!ipc_right_copyin_check(space
, reply_name
, reply_entry
, reply_type
)) {
2241 * Do the joint copyin of the dest disposition and
2242 * voucher disposition from the one entry/port. We
2243 * already validated that the voucher copyin would
2244 * succeed (above). So, any failure in combining
2245 * the copyins can be blamed on the destination.
2247 kr
= ipc_right_copyin_two(space
, dest_name
, dest_entry
,
2248 dest_type
, voucher_type
,
2249 &dest_port
, &dest_soright
,
2251 if (kr
!= KERN_SUCCESS
) {
2252 assert(kr
!= KERN_INVALID_CAPABILITY
);
2255 voucher_port
= (ipc_port_t
)dest_port
;
2258 * could not have been one of these dispositions,
2259 * validated the port was a true kernel voucher port above,
2260 * AND was successfully able to copyin both dest and voucher.
2262 assert(dest_type
!= MACH_MSG_TYPE_MAKE_SEND
);
2263 assert(dest_type
!= MACH_MSG_TYPE_MAKE_SEND_ONCE
);
2264 assert(dest_type
!= MACH_MSG_TYPE_MOVE_SEND_ONCE
);
2267 * Perform the delayed reply right copyin (guaranteed success).
2269 if (reply_entry
!= IE_NULL
) {
2270 kr
= ipc_right_copyin(space
, reply_name
, reply_entry
,
2272 &reply_port
, &reply_soright
,
2273 &release_port
, &assertcnt
);
2274 assert(assertcnt
== 0);
2275 assert(kr
== KERN_SUCCESS
);
2279 if (dest_name
== reply_name
) {
2281 * Destination and reply ports are the same!
2282 * This is very similar to the case where the
2283 * destination and voucher ports were the same
2284 * (except the reply port disposition is not
2285 * previously validated).
2287 dest_entry
= ipc_entry_lookup(space
, dest_name
);
2288 if (dest_entry
== IE_NULL
) {
2291 reply_entry
= dest_entry
;
2292 assert(reply_type
!= 0); /* because name not null */
2295 * Do the joint copyin of the dest disposition and
2296 * reply disposition from the one entry/port.
2298 kr
= ipc_right_copyin_two(space
, dest_name
, dest_entry
,
2299 dest_type
, reply_type
,
2300 &dest_port
, &dest_soright
,
2302 if (kr
== KERN_INVALID_CAPABILITY
) {
2304 } else if (kr
!= KERN_SUCCESS
) {
2307 reply_port
= dest_port
;
2312 * Handle destination and reply independently, as
2313 * they are independent entries (even if the entries
2314 * refer to the same port).
2316 * This can be the tough case to make atomic.
2318 * The difficult problem is serializing with port death.
2319 * The bad case is when dest_port dies after its copyin,
2320 * reply_port dies before its copyin, and dest_port dies before
2321 * reply_port. Then the copyins operated as if dest_port was
2322 * alive and reply_port was dead, which shouldn't have happened
2323 * because they died in the other order.
2325 * Note that it is easy for a user task to tell if
2326 * a copyin happened before or after a port died.
2327 * If a port dies before copyin, a dead-name notification
2328 * is generated and the dead name's urefs are incremented,
2329 * and if the copyin happens first, a port-deleted
2330 * notification is generated.
2332 * Even so, avoiding that potentially detectable race is too
2333 * expensive - and no known code cares about it. So, we just
2334 * do the expedient thing and copy them in one after the other.
2337 dest_entry
= ipc_entry_lookup(space
, dest_name
);
2338 if (dest_entry
== IE_NULL
) {
2341 assert(dest_entry
!= voucher_entry
);
2344 * Make sure reply port entry is valid before dest copyin.
2346 if (MACH_PORT_VALID(reply_name
)) {
2347 if (reply_name
== voucher_name
) {
2350 reply_entry
= ipc_entry_lookup(space
, reply_name
);
2351 if (reply_entry
== IE_NULL
) {
2354 assert(dest_entry
!= reply_entry
); /* names are not equal */
2355 assert(reply_type
!= 0); /* because reply_name not null */
2357 if (!ipc_right_copyin_check(space
, reply_name
, reply_entry
, reply_type
)) {
2363 * copyin the destination.
2365 kr
= ipc_right_copyin(space
, dest_name
, dest_entry
,
2367 &dest_port
, &dest_soright
,
2368 &release_port
, &assertcnt
);
2369 assert(assertcnt
== 0);
2370 if (kr
!= KERN_SUCCESS
) {
2373 assert(IO_VALID(dest_port
));
2374 assert(!IP_VALID(release_port
));
2377 * Copyin the pre-validated reply right.
2378 * It's OK if the reply right has gone dead in the meantime.
2380 if (MACH_PORT_VALID(reply_name
)) {
2381 kr
= ipc_right_copyin(space
, reply_name
, reply_entry
,
2383 &reply_port
, &reply_soright
,
2384 &release_port
, &assertcnt
);
2385 assert(assertcnt
== 0);
2386 assert(kr
== KERN_SUCCESS
);
2388 /* convert invalid name to equivalent ipc_object type */
2389 reply_port
= (ipc_object_t
)CAST_MACH_NAME_TO_PORT(reply_name
);
2394 * Finally can copyin the voucher right now that dest and reply
2395 * are fully copied in (guaranteed success).
2397 if (IE_NULL
!= voucher_entry
) {
2398 kr
= ipc_right_copyin(space
, voucher_name
, voucher_entry
,
2399 voucher_type
, FALSE
,
2400 (ipc_object_t
*)&voucher_port
,
2402 &voucher_release_port
,
2404 assert(assertcnt
== 0);
2405 assert(KERN_SUCCESS
== kr
);
2406 assert(IP_VALID(voucher_port
));
2407 assert(ip_active(voucher_port
));
2412 * The entries might need to be deallocated.
2414 * Each entry should be deallocated only once,
2415 * even if it was specified in more than one slot in the header.
2416 * Note that dest can be the same entry as reply or voucher,
2417 * but reply and voucher must be distinct entries.
2419 assert(IE_NULL
!= dest_entry
);
2420 if (IE_NULL
!= reply_entry
)
2421 assert(reply_entry
!= voucher_entry
);
2423 if (IE_BITS_TYPE(dest_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
) {
2424 ipc_entry_dealloc(space
, dest_name
, dest_entry
);
2426 if (dest_entry
== reply_entry
) {
2427 reply_entry
= IE_NULL
;
2430 if (dest_entry
== voucher_entry
) {
2431 voucher_entry
= IE_NULL
;
2434 dest_entry
= IE_NULL
;
2436 if (IE_NULL
!= reply_entry
&&
2437 IE_BITS_TYPE(reply_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
) {
2438 ipc_entry_dealloc(space
, reply_name
, reply_entry
);
2439 reply_entry
= IE_NULL
;
2441 if (IE_NULL
!= voucher_entry
&&
2442 IE_BITS_TYPE(voucher_entry
->ie_bits
) == MACH_PORT_TYPE_NONE
) {
2443 ipc_entry_dealloc(space
, voucher_name
, voucher_entry
);
2444 voucher_entry
= IE_NULL
;
2447 dest_type
= ipc_object_copyin_type(dest_type
);
2448 reply_type
= ipc_object_copyin_type(reply_type
);
2451 * JMM - Without rdar://problem/6275821, this is the last place we can
2452 * re-arm the send-possible notifications. It may trigger unexpectedly
2453 * early (send may NOT have failed), but better than missing. We assure
2454 * we won't miss by forcing MACH_SEND_ALWAYS if we got past arming.
2456 if (((*optionp
& MACH_SEND_NOTIFY
) != 0) &&
2457 dest_type
!= MACH_MSG_TYPE_PORT_SEND_ONCE
&&
2458 dest_entry
!= IE_NULL
&& dest_entry
->ie_request
!= IE_REQ_NONE
) {
2459 ipc_port_t dport
= (ipc_port_t
)dest_port
;
2461 assert(dport
!= IP_NULL
);
2463 if (ip_active(dport
) && dport
->ip_receiver
!= ipc_space_kernel
) {
2464 if (ip_full(dport
)) {
2465 #if IMPORTANCE_INHERITANCE
2466 needboost
= ipc_port_request_sparm(dport
, dest_name
,
2467 dest_entry
->ie_request
,
2470 if (needboost
== FALSE
)
2473 ipc_port_request_sparm(dport
, dest_name
,
2474 dest_entry
->ie_request
,
2478 #endif /* IMPORTANCE_INHERITANCE */
2480 *optionp
|= MACH_SEND_ALWAYS
;
2488 is_write_unlock(space
);
2490 #if IMPORTANCE_INHERITANCE
2492 * If our request is the first boosting send-possible
2493 * notification this cycle, push the boost down the
2496 if (needboost
== TRUE
) {
2497 ipc_port_t dport
= (ipc_port_t
)dest_port
;
2499 /* dport still locked from above */
2500 if (ipc_port_importance_delta(dport
, IPID_OPTION_SENDPOSSIBLE
, 1) == FALSE
) {
2504 #endif /* IMPORTANCE_INHERITANCE */
2506 if (dest_soright
!= IP_NULL
) {
2507 ipc_notify_port_deleted(dest_soright
, dest_name
);
2509 if (reply_soright
!= IP_NULL
) {
2510 ipc_notify_port_deleted(reply_soright
, reply_name
);
2512 if (voucher_soright
!= IP_NULL
) {
2513 ipc_notify_port_deleted(voucher_soright
, voucher_name
);
2517 * No room to store voucher port in in-kernel msg header,
2518 * so we store it back in the kmsg itself. Extract the
2519 * qos, and apply any override before we enqueue the kmsg.
2521 if (IP_VALID(voucher_port
)) {
2523 kmsg
->ikm_voucher
= voucher_port
;
2524 voucher_type
= MACH_MSG_TYPE_MOVE_SEND
;
2527 /* capture the qos value(s) for the kmsg */
2528 ipc_kmsg_set_qos(kmsg
, *optionp
, override
);
2530 msg
->msgh_bits
= MACH_MSGH_BITS_SET(dest_type
, reply_type
, voucher_type
, mbits
);
2531 msg
->msgh_remote_port
= (ipc_port_t
)dest_port
;
2532 msg
->msgh_local_port
= (ipc_port_t
)reply_port
;
2534 if (release_port
!= IP_NULL
)
2535 ip_release(release_port
);
2537 if (voucher_release_port
!= IP_NULL
)
2538 ip_release(voucher_release_port
);
2540 return MACH_MSG_SUCCESS
;
2543 is_write_unlock(space
);
2545 if (release_port
!= IP_NULL
)
2546 ip_release(release_port
);
2548 assert(voucher_port
== IP_NULL
);
2549 assert(voucher_soright
== IP_NULL
);
2551 return MACH_SEND_INVALID_REPLY
;
2554 is_write_unlock(space
);
2556 if (release_port
!= IP_NULL
)
2557 ip_release(release_port
);
2559 if (reply_soright
!= IP_NULL
)
2560 ipc_notify_port_deleted(reply_soright
, reply_name
);
2562 assert(voucher_port
== IP_NULL
);
2563 assert(voucher_soright
== IP_NULL
);
2565 return MACH_SEND_INVALID_DEST
;
2568 mach_msg_descriptor_t
*ipc_kmsg_copyin_port_descriptor(
2569 volatile mach_msg_port_descriptor_t
*dsc
,
2570 mach_msg_legacy_port_descriptor_t
*user_dsc
,
2574 mach_msg_return_t
*mr
);
2576 void ipc_print_type_name(
2578 mach_msg_descriptor_t
*
2579 ipc_kmsg_copyin_port_descriptor(
2580 volatile mach_msg_port_descriptor_t
*dsc
,
2581 mach_msg_legacy_port_descriptor_t
*user_dsc_in
,
2585 mach_msg_return_t
*mr
)
2587 volatile mach_msg_legacy_port_descriptor_t
*user_dsc
= user_dsc_in
;
2588 mach_msg_type_name_t user_disp
;
2589 mach_msg_type_name_t result_disp
;
2590 mach_port_name_t name
;
2591 ipc_object_t object
;
2593 user_disp
= user_dsc
->disposition
;
2594 result_disp
= ipc_object_copyin_type(user_disp
);
2596 name
= (mach_port_name_t
)user_dsc
->name
;
2597 if (MACH_PORT_VALID(name
)) {
2599 kern_return_t kr
= ipc_object_copyin(space
, name
, user_disp
, &object
);
2600 if (kr
!= KERN_SUCCESS
) {
2601 *mr
= MACH_SEND_INVALID_RIGHT
;
2605 if ((result_disp
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
2606 ipc_port_check_circularity((ipc_port_t
) object
,
2607 (ipc_port_t
) dest
)) {
2608 kmsg
->ikm_header
->msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
2610 dsc
->name
= (ipc_port_t
) object
;
2612 dsc
->name
= CAST_MACH_NAME_TO_PORT(name
);
2614 dsc
->disposition
= result_disp
;
2615 dsc
->type
= MACH_MSG_PORT_DESCRIPTOR
;
2617 dsc
->pad_end
= 0; // debug, unnecessary
2619 return (mach_msg_descriptor_t
*)(user_dsc_in
+1);
2622 mach_msg_descriptor_t
* ipc_kmsg_copyin_ool_descriptor(
2623 mach_msg_ool_descriptor_t
*dsc
,
2624 mach_msg_descriptor_t
*user_dsc
,
2627 vm_map_copy_t
*copy
,
2628 vm_size_t
*space_needed
,
2630 mach_msg_return_t
*mr
);
2631 mach_msg_descriptor_t
*
2632 ipc_kmsg_copyin_ool_descriptor(
2633 mach_msg_ool_descriptor_t
*dsc
,
2634 mach_msg_descriptor_t
*user_dsc
,
2637 vm_map_copy_t
*copy
,
2638 vm_size_t
*space_needed
,
2640 mach_msg_return_t
*mr
)
2644 mach_msg_copy_options_t copy_options
;
2645 mach_vm_offset_t addr
;
2646 mach_msg_descriptor_type_t dsc_type
;
2649 mach_msg_ool_descriptor64_t
*user_ool_dsc
= (typeof(user_ool_dsc
))user_dsc
;
2651 addr
= (mach_vm_offset_t
) user_ool_dsc
->address
;
2652 length
= user_ool_dsc
->size
;
2653 dealloc
= user_ool_dsc
->deallocate
;
2654 copy_options
= user_ool_dsc
->copy
;
2655 dsc_type
= user_ool_dsc
->type
;
2657 user_dsc
= (typeof(user_dsc
))(user_ool_dsc
+1);
2659 mach_msg_ool_descriptor32_t
*user_ool_dsc
= (typeof(user_ool_dsc
))user_dsc
;
2661 addr
= CAST_USER_ADDR_T(user_ool_dsc
->address
);
2662 dealloc
= user_ool_dsc
->deallocate
;
2663 copy_options
= user_ool_dsc
->copy
;
2664 dsc_type
= user_ool_dsc
->type
;
2665 length
= user_ool_dsc
->size
;
2667 user_dsc
= (typeof(user_dsc
))(user_ool_dsc
+1);
2670 dsc
->size
= (mach_msg_size_t
)length
;
2671 dsc
->deallocate
= dealloc
;
2672 dsc
->copy
= copy_options
;
2673 dsc
->type
= dsc_type
;
2676 dsc
->address
= NULL
;
2677 } else if ((length
>= MSG_OOL_SIZE_SMALL
) &&
2678 (copy_options
== MACH_MSG_PHYSICAL_COPY
) && !dealloc
) {
2681 * If the request is a physical copy and the source
2682 * is not being deallocated, then allocate space
2683 * in the kernel's pageable ipc copy map and copy
2684 * the data in. The semantics guarantee that the
2685 * data will have been physically copied before
2686 * the send operation terminates. Thus if the data
2687 * is not being deallocated, we must be prepared
2688 * to page if the region is sufficiently large.
2690 if (copyin(addr
, (char *)*paddr
, length
)) {
2691 *mr
= MACH_SEND_INVALID_MEMORY
;
2696 * The kernel ipc copy map is marked no_zero_fill.
2697 * If the transfer is not a page multiple, we need
2698 * to zero fill the balance.
2700 if (!page_aligned(length
)) {
2701 (void) memset((void *) (*paddr
+ length
), 0,
2702 round_page(length
) - length
);
2704 if (vm_map_copyin(ipc_kernel_copy_map
, (vm_map_address_t
)*paddr
,
2705 (vm_map_size_t
)length
, TRUE
, copy
) != KERN_SUCCESS
) {
2706 *mr
= MACH_MSG_VM_KERNEL
;
2709 dsc
->address
= (void *)*copy
;
2710 *paddr
+= round_page(length
);
2711 *space_needed
-= round_page(length
);
2715 * Make a vm_map_copy_t of the of the data. If the
2716 * data is small, this will do an optimized physical
2717 * copy. Otherwise, it will do a virtual copy.
2719 * NOTE: A virtual copy is OK if the original is being
2720 * deallocted, even if a physical copy was requested.
2722 kern_return_t kr
= vm_map_copyin(map
, addr
,
2723 (vm_map_size_t
)length
, dealloc
, copy
);
2724 if (kr
!= KERN_SUCCESS
) {
2725 *mr
= (kr
== KERN_RESOURCE_SHORTAGE
) ?
2726 MACH_MSG_VM_KERNEL
:
2727 MACH_SEND_INVALID_MEMORY
;
2730 dsc
->address
= (void *)*copy
;
2735 mach_msg_descriptor_t
* ipc_kmsg_copyin_ool_ports_descriptor(
2736 mach_msg_ool_ports_descriptor_t
*dsc
,
2737 mach_msg_descriptor_t
*user_dsc
,
2743 mach_msg_return_t
*mr
);
2744 mach_msg_descriptor_t
*
2745 ipc_kmsg_copyin_ool_ports_descriptor(
2746 mach_msg_ool_ports_descriptor_t
*dsc
,
2747 mach_msg_descriptor_t
*user_dsc
,
2753 mach_msg_return_t
*mr
)
2756 ipc_object_t
*objects
;
2758 mach_vm_offset_t addr
;
2759 mach_msg_type_name_t user_disp
;
2760 mach_msg_type_name_t result_disp
;
2761 mach_msg_type_number_t count
;
2762 mach_msg_copy_options_t copy_option
;
2763 boolean_t deallocate
;
2764 mach_msg_descriptor_type_t type
;
2765 vm_size_t ports_length
, names_length
;
2768 mach_msg_ool_ports_descriptor64_t
*user_ool_dsc
= (typeof(user_ool_dsc
))user_dsc
;
2770 addr
= (mach_vm_offset_t
)user_ool_dsc
->address
;
2771 count
= user_ool_dsc
->count
;
2772 deallocate
= user_ool_dsc
->deallocate
;
2773 copy_option
= user_ool_dsc
->copy
;
2774 user_disp
= user_ool_dsc
->disposition
;
2775 type
= user_ool_dsc
->type
;
2777 user_dsc
= (typeof(user_dsc
))(user_ool_dsc
+1);
2779 mach_msg_ool_ports_descriptor32_t
*user_ool_dsc
= (typeof(user_ool_dsc
))user_dsc
;
2781 addr
= CAST_USER_ADDR_T(user_ool_dsc
->address
);
2782 count
= user_ool_dsc
->count
;
2783 deallocate
= user_ool_dsc
->deallocate
;
2784 copy_option
= user_ool_dsc
->copy
;
2785 user_disp
= user_ool_dsc
->disposition
;
2786 type
= user_ool_dsc
->type
;
2788 user_dsc
= (typeof(user_dsc
))(user_ool_dsc
+1);
2791 dsc
->deallocate
= deallocate
;
2792 dsc
->copy
= copy_option
;
2795 dsc
->address
= NULL
; /* for now */
2797 result_disp
= ipc_object_copyin_type(user_disp
);
2798 dsc
->disposition
= result_disp
;
2800 /* We always do a 'physical copy', but you have to specify something valid */
2801 if (copy_option
!= MACH_MSG_PHYSICAL_COPY
&&
2802 copy_option
!= MACH_MSG_VIRTUAL_COPY
) {
2803 *mr
= MACH_SEND_INVALID_TYPE
;
2807 /* calculate length of data in bytes, rounding up */
2809 if (os_mul_overflow(count
, sizeof(mach_port_t
), &ports_length
)) {
2810 *mr
= MACH_SEND_TOO_LARGE
;
2814 if (os_mul_overflow(count
, sizeof(mach_port_name_t
), &names_length
)) {
2815 *mr
= MACH_SEND_TOO_LARGE
;
2819 if (ports_length
== 0) {
2823 data
= kalloc(ports_length
);
2826 *mr
= MACH_SEND_NO_BUFFER
;
2831 mach_port_name_t
*names
= &((mach_port_name_t
*)data
)[count
];
2833 mach_port_name_t
*names
= ((mach_port_name_t
*)data
);
2836 if (copyinmap(map
, addr
, names
, names_length
) != KERN_SUCCESS
) {
2837 kfree(data
, ports_length
);
2838 *mr
= MACH_SEND_INVALID_MEMORY
;
2843 (void) mach_vm_deallocate(map
, addr
, (mach_vm_size_t
)ports_length
);
2846 objects
= (ipc_object_t
*) data
;
2847 dsc
->address
= data
;
2849 for ( i
= 0; i
< count
; i
++) {
2850 mach_port_name_t name
= names
[i
];
2851 ipc_object_t object
;
2853 if (!MACH_PORT_VALID(name
)) {
2854 objects
[i
] = (ipc_object_t
)CAST_MACH_NAME_TO_PORT(name
);
2858 kern_return_t kr
= ipc_object_copyin(space
, name
, user_disp
, &object
);
2860 if (kr
!= KERN_SUCCESS
) {
2863 for(j
= 0; j
< i
; j
++) {
2864 object
= objects
[j
];
2865 if (IPC_OBJECT_VALID(object
))
2866 ipc_object_destroy(object
, result_disp
);
2868 kfree(data
, ports_length
);
2869 dsc
->address
= NULL
;
2870 *mr
= MACH_SEND_INVALID_RIGHT
;
2874 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
2875 ipc_port_check_circularity(
2876 (ipc_port_t
) object
,
2878 kmsg
->ikm_header
->msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
2880 objects
[i
] = object
;
2887 * Routine: ipc_kmsg_copyin_body
2889 * "Copy-in" port rights and out-of-line memory
2890 * in the message body.
2892 * In all failure cases, the message is left holding
2893 * no rights or memory. However, the message buffer
2894 * is not deallocated. If successful, the message
2895 * contains a valid destination port.
2899 * MACH_MSG_SUCCESS Successful copyin.
2900 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
2901 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
2902 * MACH_SEND_INVALID_TYPE Bad type specification.
2903 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
2904 * MACH_SEND_INVALID_RT_OOL_SIZE OOL Buffer too large for RT
2905 * MACH_MSG_INVALID_RT_DESCRIPTOR Dealloc and RT are incompatible
2909 ipc_kmsg_copyin_body(
2915 mach_msg_body_t
*body
;
2916 mach_msg_descriptor_t
*daddr
, *naddr
;
2917 mach_msg_descriptor_t
*user_addr
, *kern_addr
;
2918 mach_msg_type_number_t dsc_count
;
2919 boolean_t is_task_64bit
= (map
->max_offset
> VM_MAX_ADDRESS
);
2920 boolean_t
complex = FALSE
;
2921 vm_size_t space_needed
= 0;
2922 vm_offset_t paddr
= 0;
2923 vm_map_copy_t copy
= VM_MAP_COPY_NULL
;
2924 mach_msg_type_number_t i
;
2925 mach_msg_return_t mr
= MACH_MSG_SUCCESS
;
2927 vm_size_t descriptor_size
= 0;
2929 mach_msg_type_number_t total_ool_port_count
= 0;
2932 * Determine if the target is a kernel port.
2934 dest
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
2935 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
2936 naddr
= (mach_msg_descriptor_t
*) (body
+ 1);
2938 dsc_count
= body
->msgh_descriptor_count
;
2940 return MACH_MSG_SUCCESS
;
2943 * Make an initial pass to determine kernal VM space requirements for
2944 * physical copies and possible contraction of the descriptors from
2945 * processes with pointers larger than the kernel's.
2948 for (i
= 0; i
< dsc_count
; i
++) {
2949 mach_msg_size_t size
;
2950 mach_msg_type_number_t ool_port_count
= 0;
2954 /* make sure the descriptor fits in the message */
2955 if (is_task_64bit
) {
2956 switch (daddr
->type
.type
) {
2957 case MACH_MSG_OOL_DESCRIPTOR
:
2958 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
2959 case MACH_MSG_OOL_PORTS_DESCRIPTOR
:
2960 descriptor_size
+= 16;
2961 naddr
= (typeof(naddr
))((vm_offset_t
)daddr
+ 16);
2964 descriptor_size
+= 12;
2965 naddr
= (typeof(naddr
))((vm_offset_t
)daddr
+ 12);
2969 descriptor_size
+= 12;
2970 naddr
= (typeof(naddr
))((vm_offset_t
)daddr
+ 12);
2973 if (naddr
> (mach_msg_descriptor_t
*)
2974 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
)) {
2975 mr
= MACH_SEND_MSG_TOO_SMALL
;
2979 switch (daddr
->type
.type
) {
2980 case MACH_MSG_OOL_DESCRIPTOR
:
2981 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
2982 size
= (is_task_64bit
) ?
2983 ((mach_msg_ool_descriptor64_t
*)daddr
)->size
:
2984 daddr
->out_of_line
.size
;
2986 if (daddr
->out_of_line
.copy
!= MACH_MSG_PHYSICAL_COPY
&&
2987 daddr
->out_of_line
.copy
!= MACH_MSG_VIRTUAL_COPY
) {
2989 * Invalid copy option
2991 mr
= MACH_SEND_INVALID_TYPE
;
2995 if ((size
>= MSG_OOL_SIZE_SMALL
) &&
2996 (daddr
->out_of_line
.copy
== MACH_MSG_PHYSICAL_COPY
) &&
2997 !(daddr
->out_of_line
.deallocate
)) {
3000 * Out-of-line memory descriptor, accumulate kernel
3001 * memory requirements
3003 if (space_needed
+ round_page(size
) <= space_needed
) {
3004 /* Overflow dectected */
3005 mr
= MACH_MSG_VM_KERNEL
;
3009 space_needed
+= round_page(size
);
3010 if (space_needed
> ipc_kmsg_max_vm_space
) {
3011 /* Per message kernel memory limit exceeded */
3012 mr
= MACH_MSG_VM_KERNEL
;
3017 case MACH_MSG_PORT_DESCRIPTOR
:
3018 if (os_add_overflow(total_ool_port_count
, 1, &total_ool_port_count
)) {
3019 /* Overflow detected */
3020 mr
= MACH_SEND_TOO_LARGE
;
3024 case MACH_MSG_OOL_PORTS_DESCRIPTOR
:
3025 ool_port_count
= (is_task_64bit
) ?
3026 ((mach_msg_ool_ports_descriptor64_t
*)daddr
)->count
:
3027 daddr
->ool_ports
.count
;
3029 if (os_add_overflow(total_ool_port_count
, ool_port_count
, &total_ool_port_count
)) {
3030 /* Overflow detected */
3031 mr
= MACH_SEND_TOO_LARGE
;
3035 if (ool_port_count
> (ipc_kmsg_max_vm_space
/sizeof(mach_port_t
))) {
3036 /* Per message kernel memory limit exceeded */
3037 mr
= MACH_SEND_TOO_LARGE
;
3044 /* Sending more than 16383 rights in one message seems crazy */
3045 if (total_ool_port_count
>= (MACH_PORT_UREFS_MAX
/ 4)) {
3046 mr
= MACH_SEND_TOO_LARGE
;
3051 * Allocate space in the pageable kernel ipc copy map for all the
3052 * ool data that is to be physically copied. Map is marked wait for
3056 if (vm_allocate(ipc_kernel_copy_map
, &paddr
, space_needed
,
3057 VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_IPC
)) != KERN_SUCCESS
) {
3058 mr
= MACH_MSG_VM_KERNEL
;
3063 /* user_addr = just after base as it was copied in */
3064 user_addr
= (mach_msg_descriptor_t
*)((vm_offset_t
)kmsg
->ikm_header
+ sizeof(mach_msg_base_t
));
3066 /* Shift the mach_msg_base_t down to make room for dsc_count*16bytes of descriptors */
3067 if(descriptor_size
!= 16*dsc_count
) {
3068 vm_offset_t dsc_adjust
= 16*dsc_count
- descriptor_size
;
3070 memmove((char *)(((vm_offset_t
)kmsg
->ikm_header
) - dsc_adjust
), kmsg
->ikm_header
, sizeof(mach_msg_base_t
));
3071 kmsg
->ikm_header
= (mach_msg_header_t
*)((vm_offset_t
)kmsg
->ikm_header
- dsc_adjust
);
3073 /* Update the message size for the larger in-kernel representation */
3074 kmsg
->ikm_header
->msgh_size
+= (mach_msg_size_t
)dsc_adjust
;
3078 /* kern_addr = just after base after it has been (conditionally) moved */
3079 kern_addr
= (mach_msg_descriptor_t
*)((vm_offset_t
)kmsg
->ikm_header
+ sizeof(mach_msg_base_t
));
3081 /* handle the OOL regions and port descriptors. */
3082 for(i
=0;i
<dsc_count
;i
++) {
3083 switch (user_addr
->type
.type
) {
3084 case MACH_MSG_PORT_DESCRIPTOR
:
3085 user_addr
= ipc_kmsg_copyin_port_descriptor((mach_msg_port_descriptor_t
*)kern_addr
,
3086 (mach_msg_legacy_port_descriptor_t
*)user_addr
, space
, dest
, kmsg
, &mr
);
3090 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
3091 case MACH_MSG_OOL_DESCRIPTOR
:
3092 user_addr
= ipc_kmsg_copyin_ool_descriptor((mach_msg_ool_descriptor_t
*)kern_addr
,
3093 user_addr
, is_task_64bit
, &paddr
, ©
, &space_needed
, map
, &mr
);
3097 case MACH_MSG_OOL_PORTS_DESCRIPTOR
:
3098 user_addr
= ipc_kmsg_copyin_ool_ports_descriptor((mach_msg_ool_ports_descriptor_t
*)kern_addr
,
3099 user_addr
, is_task_64bit
, map
, space
, dest
, kmsg
, &mr
);
3104 /* Invalid descriptor */
3105 mr
= MACH_SEND_INVALID_TYPE
;
3109 if (MACH_MSG_SUCCESS
!= mr
) {
3110 /* clean from start of message descriptors to i */
3111 ipc_kmsg_clean_partial(kmsg
, i
,
3112 (mach_msg_descriptor_t
*)((mach_msg_base_t
*)kmsg
->ikm_header
+ 1),
3113 paddr
, space_needed
);
3119 kmsg
->ikm_header
->msgh_bits
&= ~MACH_MSGH_BITS_COMPLEX
;
3125 /* no descriptors have been copied in yet */
3126 ipc_kmsg_clean_partial(kmsg
, 0, NULL
, 0, 0);
3132 * Routine: ipc_kmsg_copyin
3134 * "Copy-in" port rights and out-of-line memory
3137 * In all failure cases, the message is left holding
3138 * no rights or memory. However, the message buffer
3139 * is not deallocated. If successful, the message
3140 * contains a valid destination port.
3144 * MACH_MSG_SUCCESS Successful copyin.
3145 * MACH_SEND_INVALID_HEADER
3146 * Illegal value in the message header bits.
3147 * MACH_SEND_INVALID_DEST Can't copyin destination port.
3148 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
3149 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
3150 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
3151 * MACH_SEND_INVALID_TYPE Bad type specification.
3152 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
3160 mach_msg_priority_t override
,
3161 mach_msg_option_t
*optionp
)
3163 mach_msg_return_t mr
;
3165 kmsg
->ikm_header
->msgh_bits
&= MACH_MSGH_BITS_USER
;
3167 mr
= ipc_kmsg_copyin_header(kmsg
, space
, override
, optionp
);
3169 if (mr
!= MACH_MSG_SUCCESS
)
3172 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_MSG_SEND
) | DBG_FUNC_NONE
,
3173 VM_KERNEL_ADDRPERM((uintptr_t)kmsg
),
3174 (uintptr_t)kmsg
->ikm_header
->msgh_bits
,
3175 (uintptr_t)kmsg
->ikm_header
->msgh_id
,
3176 VM_KERNEL_ADDRPERM((uintptr_t)unsafe_convert_port_to_voucher(kmsg
->ikm_voucher
)),
3179 DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_copyin header:\n%.8x\n%.8x\n%p\n%p\n%p\n%.8x\n",
3180 kmsg
->ikm_header
->msgh_size
,
3181 kmsg
->ikm_header
->msgh_bits
,
3182 kmsg
->ikm_header
->msgh_remote_port
,
3183 kmsg
->ikm_header
->msgh_local_port
,
3185 kmsg
->ikm_header
->msgh_id
);
3187 if ((kmsg
->ikm_header
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
3188 return MACH_MSG_SUCCESS
;
3190 mr
= ipc_kmsg_copyin_body( kmsg
, space
, map
);
3192 /* unreachable if !DEBUG */
3193 __unreachable_ok_push
3194 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK
))
3198 for(i
=0;i
*4 < (kmsg
->ikm_header
->msgh_size
- sizeof(mach_msg_header_t
));i
++)
3200 kprintf("%.4x\n",((uint32_t *)(kmsg
->ikm_header
+ 1))[i
]);
3203 __unreachable_ok_pop
3209 * Routine: ipc_kmsg_copyin_from_kernel
3211 * "Copy-in" port rights and out-of-line memory
3212 * in a message sent from the kernel.
3214 * Because the message comes from the kernel,
3215 * the implementation assumes there are no errors
3216 * or peculiarities in the message.
3222 ipc_kmsg_copyin_from_kernel(
3225 mach_msg_bits_t bits
= kmsg
->ikm_header
->msgh_bits
;
3226 mach_msg_type_name_t rname
= MACH_MSGH_BITS_REMOTE(bits
);
3227 mach_msg_type_name_t lname
= MACH_MSGH_BITS_LOCAL(bits
);
3228 ipc_object_t remote
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
3229 ipc_object_t local
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
3231 /* translate the destination and reply ports */
3232 if (!IO_VALID(remote
))
3233 return MACH_SEND_INVALID_DEST
;
3235 ipc_object_copyin_from_kernel(remote
, rname
);
3236 if (IO_VALID(local
))
3237 ipc_object_copyin_from_kernel(local
, lname
);
3240 * The common case is a complex message with no reply port,
3241 * because that is what the memory_object interface uses.
3244 if (bits
== (MACH_MSGH_BITS_COMPLEX
|
3245 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0))) {
3246 bits
= (MACH_MSGH_BITS_COMPLEX
|
3247 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND
, 0));
3249 kmsg
->ikm_header
->msgh_bits
= bits
;
3251 bits
= (MACH_MSGH_BITS_OTHER(bits
) |
3252 MACH_MSGH_BITS(ipc_object_copyin_type(rname
),
3253 ipc_object_copyin_type(lname
)));
3255 kmsg
->ikm_header
->msgh_bits
= bits
;
3256 if ((bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
3257 return MACH_MSG_SUCCESS
;
3260 mach_msg_descriptor_t
*saddr
;
3261 mach_msg_body_t
*body
;
3262 mach_msg_type_number_t i
, count
;
3264 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
3265 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
3266 count
= body
->msgh_descriptor_count
;
3268 for (i
= 0; i
< count
; i
++, saddr
++) {
3270 switch (saddr
->type
.type
) {
3272 case MACH_MSG_PORT_DESCRIPTOR
: {
3273 mach_msg_type_name_t name
;
3274 ipc_object_t object
;
3275 mach_msg_port_descriptor_t
*dsc
;
3279 /* this is really the type SEND, SEND_ONCE, etc. */
3280 name
= dsc
->disposition
;
3281 object
= (ipc_object_t
) dsc
->name
;
3282 dsc
->disposition
= ipc_object_copyin_type(name
);
3284 if (!IO_VALID(object
)) {
3288 ipc_object_copyin_from_kernel(object
, name
);
3290 /* CDY avoid circularity when the destination is also */
3291 /* the kernel. This check should be changed into an */
3292 /* assert when the new kobject model is in place since*/
3293 /* ports will not be used in kernel to kernel chats */
3295 if (((ipc_port_t
)remote
)->ip_receiver
!= ipc_space_kernel
) {
3296 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
3297 ipc_port_check_circularity((ipc_port_t
) object
,
3298 (ipc_port_t
) remote
)) {
3299 kmsg
->ikm_header
->msgh_bits
|=
3300 MACH_MSGH_BITS_CIRCULAR
;
3305 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
3306 case MACH_MSG_OOL_DESCRIPTOR
: {
3308 * The sender should supply ready-made memory, i.e.
3309 * a vm_map_copy_t, so we don't need to do anything.
3313 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
3314 ipc_object_t
*objects
;
3316 mach_msg_type_name_t name
;
3317 mach_msg_ool_ports_descriptor_t
*dsc
;
3319 dsc
= (mach_msg_ool_ports_descriptor_t
*)&saddr
->ool_ports
;
3321 /* this is really the type SEND, SEND_ONCE, etc. */
3322 name
= dsc
->disposition
;
3323 dsc
->disposition
= ipc_object_copyin_type(name
);
3325 objects
= (ipc_object_t
*) dsc
->address
;
3327 for ( j
= 0; j
< dsc
->count
; j
++) {
3328 ipc_object_t object
= objects
[j
];
3330 if (!IO_VALID(object
))
3333 ipc_object_copyin_from_kernel(object
, name
);
3335 if ((dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
3336 ipc_port_check_circularity(
3337 (ipc_port_t
) object
,
3338 (ipc_port_t
) remote
))
3339 kmsg
->ikm_header
->msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
3345 panic("ipc_kmsg_copyin_from_kernel: bad descriptor");
3346 #endif /* MACH_ASSERT */
3351 return MACH_MSG_SUCCESS
;
3354 #if IKM_SUPPORT_LEGACY
3356 ipc_kmsg_copyin_from_kernel_legacy(
3359 mach_msg_bits_t bits
= kmsg
->ikm_header
->msgh_bits
;
3360 mach_msg_type_name_t rname
= MACH_MSGH_BITS_REMOTE(bits
);
3361 mach_msg_type_name_t lname
= MACH_MSGH_BITS_LOCAL(bits
);
3362 ipc_object_t remote
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
3363 ipc_object_t local
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
3365 /* translate the destination and reply ports */
3366 if (!IO_VALID(remote
))
3367 return MACH_SEND_INVALID_DEST
;
3369 ipc_object_copyin_from_kernel(remote
, rname
);
3370 if (IO_VALID(local
))
3371 ipc_object_copyin_from_kernel(local
, lname
);
3374 * The common case is a complex message with no reply port,
3375 * because that is what the memory_object interface uses.
3378 if (bits
== (MACH_MSGH_BITS_COMPLEX
|
3379 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0))) {
3380 bits
= (MACH_MSGH_BITS_COMPLEX
|
3381 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND
, 0));
3383 kmsg
->ikm_header
->msgh_bits
= bits
;
3385 bits
= (MACH_MSGH_BITS_OTHER(bits
) |
3386 MACH_MSGH_BITS(ipc_object_copyin_type(rname
),
3387 ipc_object_copyin_type(lname
)));
3389 kmsg
->ikm_header
->msgh_bits
= bits
;
3390 if ((bits
& MACH_MSGH_BITS_COMPLEX
) == 0)
3391 return MACH_MSG_SUCCESS
;
3394 mach_msg_legacy_descriptor_t
*saddr
;
3395 mach_msg_descriptor_t
*daddr
;
3396 mach_msg_body_t
*body
;
3397 mach_msg_type_number_t i
, count
;
3399 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
3400 saddr
= (typeof(saddr
)) (body
+ 1);
3401 count
= body
->msgh_descriptor_count
;
3404 vm_offset_t dsc_adjust
= 4*count
;
3405 memmove((char *)(((vm_offset_t
)kmsg
->ikm_header
) - dsc_adjust
), kmsg
->ikm_header
, sizeof(mach_msg_base_t
));
3406 kmsg
->ikm_header
= (mach_msg_header_t
*)((vm_offset_t
)kmsg
->ikm_header
- dsc_adjust
);
3407 /* Update the message size for the larger in-kernel representation */
3408 kmsg
->ikm_header
->msgh_size
+= dsc_adjust
;
3410 daddr
= (mach_msg_descriptor_t
*)((vm_offset_t
)kmsg
->ikm_header
+ sizeof(mach_msg_base_t
));
3412 for (i
= 0; i
< count
; i
++, saddr
++, daddr
++) {
3413 switch (saddr
->type
.type
) {
3415 case MACH_MSG_PORT_DESCRIPTOR
: {
3416 mach_msg_type_name_t name
;
3417 ipc_object_t object
;
3418 mach_msg_legacy_port_descriptor_t
*dsc
;
3419 mach_msg_port_descriptor_t
*dest_dsc
;
3421 dsc
= (typeof(dsc
))&saddr
->port
;
3422 dest_dsc
= &daddr
->port
;
3424 /* this is really the type SEND, SEND_ONCE, etc. */
3425 name
= dsc
->disposition
;
3426 object
= (ipc_object_t
) CAST_MACH_NAME_TO_PORT(dsc
->name
);
3427 dest_dsc
->disposition
= ipc_object_copyin_type(name
);
3428 dest_dsc
->name
= (mach_port_t
)object
;
3429 dest_dsc
->type
= MACH_MSG_PORT_DESCRIPTOR
;
3431 if (!IO_VALID(object
)) {
3435 ipc_object_copyin_from_kernel(object
, name
);
3437 /* CDY avoid circularity when the destination is also */
3438 /* the kernel. This check should be changed into an */
3439 /* assert when the new kobject model is in place since*/
3440 /* ports will not be used in kernel to kernel chats */
3442 if (((ipc_port_t
)remote
)->ip_receiver
!= ipc_space_kernel
) {
3443 if ((dest_dsc
->disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
3444 ipc_port_check_circularity((ipc_port_t
) object
,
3445 (ipc_port_t
) remote
)) {
3446 kmsg
->ikm_header
->msgh_bits
|=
3447 MACH_MSGH_BITS_CIRCULAR
;
3452 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
3453 case MACH_MSG_OOL_DESCRIPTOR
: {
3454 /* The sender should supply ready-made memory, i.e. a vm_map_copy_t
3455 * so we don't need to do anything special. */
3457 mach_msg_ool_descriptor32_t
*source_dsc
= &saddr
->out_of_line32
;
3458 mach_msg_ool_descriptor_t
*dest_dsc
= (typeof(dest_dsc
))&daddr
->out_of_line
;
3460 vm_offset_t address
= source_dsc
->address
;
3461 vm_size_t size
= source_dsc
->size
;
3462 boolean_t deallocate
= source_dsc
->deallocate
;
3463 mach_msg_copy_options_t copy
= source_dsc
->copy
;
3464 mach_msg_descriptor_type_t type
= source_dsc
->type
;
3466 dest_dsc
->address
= (void *)address
;
3467 dest_dsc
->size
= size
;
3468 dest_dsc
->deallocate
= deallocate
;
3469 dest_dsc
->copy
= copy
;
3470 dest_dsc
->type
= type
;
3473 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
3474 ipc_object_t
*objects
;
3476 mach_msg_type_name_t name
;
3477 mach_msg_ool_ports_descriptor_t
*dest_dsc
;
3479 mach_msg_ool_ports_descriptor32_t
*source_dsc
= &saddr
->ool_ports32
;
3480 dest_dsc
= (typeof(dest_dsc
))&daddr
->ool_ports
;
3482 boolean_t deallocate
= source_dsc
->deallocate
;
3483 mach_msg_copy_options_t copy
= source_dsc
->copy
;
3484 mach_msg_size_t port_count
= source_dsc
->count
;
3485 mach_msg_type_name_t disposition
= source_dsc
->disposition
;
3487 /* this is really the type SEND, SEND_ONCE, etc. */
3489 disposition
= ipc_object_copyin_type(name
);
3491 objects
= (ipc_object_t
*) (uintptr_t)source_dsc
->address
;
3493 for ( j
= 0; j
< port_count
; j
++) {
3494 ipc_object_t object
= objects
[j
];
3496 if (!IO_VALID(object
))
3499 ipc_object_copyin_from_kernel(object
, name
);
3501 if ((disposition
== MACH_MSG_TYPE_PORT_RECEIVE
) &&
3502 ipc_port_check_circularity(
3503 (ipc_port_t
) object
,
3504 (ipc_port_t
) remote
))
3505 kmsg
->ikm_header
->msgh_bits
|= MACH_MSGH_BITS_CIRCULAR
;
3508 dest_dsc
->address
= objects
;
3509 dest_dsc
->deallocate
= deallocate
;
3510 dest_dsc
->copy
= copy
;
3511 dest_dsc
->disposition
= disposition
;
3512 dest_dsc
->type
= MACH_MSG_OOL_PORTS_DESCRIPTOR
;
3513 dest_dsc
->count
= port_count
;
3518 panic("ipc_kmsg_copyin_from_kernel: bad descriptor");
3519 #endif /* MACH_ASSERT */
3524 return MACH_MSG_SUCCESS
;
3526 #endif /* IKM_SUPPORT_LEGACY */
3529 * Routine: ipc_kmsg_copyout_header
3531 * "Copy-out" port rights in the header of a message.
3532 * Operates atomically; if it doesn't succeed the
3533 * message header and the space are left untouched.
3534 * If it does succeed the remote/local port fields
3535 * contain port names instead of object pointers,
3536 * and the bits field is updated.
3540 * MACH_MSG_SUCCESS Copied out port rights.
3541 * MACH_RCV_INVALID_NOTIFY
3542 * Notify is non-null and doesn't name a receive right.
3543 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
3544 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
3545 * The space is dead.
3546 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
3547 * No room in space for another name.
3548 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
3549 * Couldn't allocate memory for the reply port.
3550 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
3551 * Couldn't allocate memory for the dead-name request.
3555 ipc_kmsg_copyout_header(
3558 mach_msg_option_t option
)
3560 mach_msg_header_t
*msg
= kmsg
->ikm_header
;
3561 mach_msg_bits_t mbits
= msg
->msgh_bits
;
3562 ipc_port_t dest
= (ipc_port_t
) msg
->msgh_remote_port
;
3564 assert(IP_VALID(dest
));
3567 * While we still hold a reference on the received-from port,
3568 * process all send-possible notfications we received along with
3571 ipc_port_spnotify(dest
);
3574 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
3575 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
3576 mach_msg_type_name_t voucher_type
= MACH_MSGH_BITS_VOUCHER(mbits
);
3577 ipc_port_t reply
= msg
->msgh_local_port
;
3578 ipc_port_t release_reply_port
= IP_NULL
;
3579 mach_port_name_t dest_name
, reply_name
;
3581 ipc_port_t voucher
= kmsg
->ikm_voucher
;
3582 ipc_port_t release_voucher_port
= IP_NULL
;
3583 mach_port_name_t voucher_name
;
3585 uint32_t entries_held
= 0;
3586 boolean_t need_write_lock
= FALSE
;
3590 * Reserve any potentially needed entries in the target space.
3591 * We'll free any unused before unlocking the space.
3593 if (IP_VALID(reply
)) {
3595 need_write_lock
= TRUE
;
3597 if (IP_VALID(voucher
)) {
3598 assert(voucher_type
== MACH_MSG_TYPE_MOVE_SEND
);
3600 if ((option
& MACH_RCV_VOUCHER
) != 0)
3602 need_write_lock
= TRUE
;
3605 if (need_write_lock
) {
3607 is_write_lock(space
);
3609 while(entries_held
) {
3610 if (!is_active(space
)) {
3611 is_write_unlock(space
);
3612 return (MACH_RCV_HEADER_ERROR
|
3613 MACH_MSG_IPC_SPACE
);
3616 kr
= ipc_entries_hold(space
, entries_held
);
3617 if (KERN_SUCCESS
== kr
)
3620 kr
= ipc_entry_grow_table(space
, ITS_SIZE_NONE
);
3621 if (KERN_SUCCESS
!= kr
)
3622 return(MACH_RCV_HEADER_ERROR
|
3623 MACH_MSG_IPC_SPACE
);
3624 /* space was unlocked and relocked - retry */
3627 /* Handle reply port. */
3628 if (IP_VALID(reply
)) {
3631 /* Is there already an entry we can use? */
3632 if ((reply_type
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
3633 ipc_right_reverse(space
, (ipc_object_t
) reply
, &reply_name
, &entry
)) {
3634 /* reply port is locked and active */
3635 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND_RECEIVE
);
3638 if (!ip_active(reply
)) {
3641 release_reply_port
= reply
;
3643 reply_name
= MACH_PORT_DEAD
;
3644 goto done_with_reply
;
3647 /* claim a held entry for the reply port */
3648 assert(entries_held
> 0);
3650 ipc_entry_claim(space
, &reply_name
, &entry
);
3651 assert(IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
);
3652 assert(entry
->ie_object
== IO_NULL
);
3653 entry
->ie_object
= (ipc_object_t
) reply
;
3656 /* space and reply port are locked and active */
3657 ip_reference(reply
); /* hold onto the reply port */
3659 kr
= ipc_right_copyout(space
, reply_name
, entry
,
3660 reply_type
, TRUE
, (ipc_object_t
) reply
);
3661 assert(kr
== KERN_SUCCESS
);
3662 /* reply port is unlocked */
3664 reply_name
= CAST_MACH_PORT_TO_NAME(reply
);
3668 /* Handle voucher port. */
3669 if (voucher_type
!= MACH_MSGH_BITS_ZERO
) {
3670 assert(voucher_type
== MACH_MSG_TYPE_MOVE_SEND
);
3672 if (!IP_VALID(voucher
)) {
3673 if ((option
& MACH_RCV_VOUCHER
) == 0) {
3674 voucher_type
= MACH_MSGH_BITS_ZERO
;
3676 voucher_name
= MACH_PORT_NULL
;
3677 goto done_with_voucher
;
3680 /* clear voucher from its hiding place back in the kmsg */
3681 kmsg
->ikm_voucher
= IP_NULL
;
3683 if ((option
& MACH_RCV_VOUCHER
) != 0) {
3686 if (ipc_right_reverse(space
, (ipc_object_t
) voucher
,
3687 &voucher_name
, &entry
)) {
3688 /* voucher port locked */
3689 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND
);
3691 assert(entries_held
> 0);
3693 ipc_entry_claim(space
, &voucher_name
, &entry
);
3694 assert(IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
);
3695 assert(entry
->ie_object
== IO_NULL
);
3696 entry
->ie_object
= (ipc_object_t
) voucher
;
3699 /* space is locked and active */
3701 assert(ip_active(voucher
));
3702 assert(ip_kotype(voucher
) == IKOT_VOUCHER
);
3703 kr
= ipc_right_copyout(space
, voucher_name
, entry
,
3704 MACH_MSG_TYPE_MOVE_SEND
, TRUE
,
3705 (ipc_object_t
) voucher
);
3706 /* voucher port is unlocked */
3708 voucher_type
= MACH_MSGH_BITS_ZERO
;
3709 release_voucher_port
= voucher
;
3710 voucher_name
= MACH_PORT_NULL
;
3713 voucher_name
= msg
->msgh_voucher_port
;
3719 is_write_unlock(space
);
3723 * No reply or voucher port! This is an easy case.
3724 * We only need to have the space locked
3725 * when locking the destination.
3728 is_read_lock(space
);
3729 if (!is_active(space
)) {
3730 is_read_unlock(space
);
3731 return MACH_RCV_HEADER_ERROR
|MACH_MSG_IPC_SPACE
;
3735 is_read_unlock(space
);
3737 reply_name
= CAST_MACH_PORT_TO_NAME(reply
);
3739 if (voucher_type
!= MACH_MSGH_BITS_ZERO
) {
3740 assert(voucher_type
== MACH_MSG_TYPE_MOVE_SEND
);
3741 if ((option
& MACH_RCV_VOUCHER
) == 0) {
3742 voucher_type
= MACH_MSGH_BITS_ZERO
;
3744 voucher_name
= MACH_PORT_NULL
;
3746 voucher_name
= msg
->msgh_voucher_port
;
3751 * At this point, the space is unlocked and the destination
3752 * port is locked. (Lock taken while space was locked.)
3753 * reply_name is taken care of; we still need dest_name.
3754 * We still hold a ref for reply (if it is valid).
3756 * If the space holds receive rights for the destination,
3757 * we return its name for the right. Otherwise the task
3758 * managed to destroy or give away the receive right between
3759 * receiving the message and this copyout. If the destination
3760 * is dead, return MACH_PORT_DEAD, and if the receive right
3761 * exists somewhere else (another space, in transit)
3762 * return MACH_PORT_NULL.
3764 * Making this copyout operation atomic with the previous
3765 * copyout of the reply port is a bit tricky. If there was
3766 * no real reply port (it wasn't IP_VALID) then this isn't
3767 * an issue. If the reply port was dead at copyout time,
3768 * then we are OK, because if dest is dead we serialize
3769 * after the death of both ports and if dest is alive
3770 * we serialize after reply died but before dest's (later) death.
3771 * So assume reply was alive when we copied it out. If dest
3772 * is alive, then we are OK because we serialize before
3773 * the ports' deaths. So assume dest is dead when we look at it.
3774 * If reply dies/died after dest, then we are OK because
3775 * we serialize after dest died but before reply dies.
3776 * So the hard case is when reply is alive at copyout,
3777 * dest is dead at copyout, and reply died before dest died.
3778 * In this case pretend that dest is still alive, so
3779 * we serialize while both ports are alive.
3781 * Because the space lock is held across the copyout of reply
3782 * and locking dest, the receive right for dest can't move
3783 * in or out of the space while the copyouts happen, so
3784 * that isn't an atomicity problem. In the last hard case
3785 * above, this implies that when dest is dead that the
3786 * space couldn't have had receive rights for dest at
3787 * the time reply was copied-out, so when we pretend
3788 * that dest is still alive, we can return MACH_PORT_NULL.
3790 * If dest == reply, then we have to make it look like
3791 * either both copyouts happened before the port died,
3792 * or both happened after the port died. This special
3793 * case works naturally if the timestamp comparison
3794 * is done correctly.
3797 if (ip_active(dest
)) {
3798 ipc_object_copyout_dest(space
, (ipc_object_t
) dest
,
3799 dest_type
, &dest_name
);
3800 /* dest is unlocked */
3803 ipc_port_timestamp_t timestamp
;
3805 timestamp
= dest
->ip_timestamp
;
3809 if (IP_VALID(reply
)) {
3811 if (ip_active(reply
) ||
3812 IP_TIMESTAMP_ORDER(timestamp
,
3813 reply
->ip_timestamp
))
3814 dest_name
= MACH_PORT_DEAD
;
3816 dest_name
= MACH_PORT_NULL
;
3819 dest_name
= MACH_PORT_DEAD
;
3822 if (IP_VALID(reply
))
3825 if (IP_VALID(release_reply_port
)) {
3826 if (reply_type
== MACH_MSG_TYPE_PORT_SEND_ONCE
)
3827 ipc_port_release_sonce(release_reply_port
);
3829 ipc_port_release_send(release_reply_port
);
3832 if (IP_VALID(release_voucher_port
))
3833 ipc_port_release_send(release_voucher_port
);
3836 if ((option
& MACH_RCV_VOUCHER
) != 0) {
3837 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_MSG_RECV
) | DBG_FUNC_NONE
,
3838 VM_KERNEL_ADDRPERM((uintptr_t)kmsg
),
3839 (uintptr_t)kmsg
->ikm_header
->msgh_bits
,
3840 (uintptr_t)kmsg
->ikm_header
->msgh_id
,
3841 VM_KERNEL_ADDRPERM((uintptr_t)unsafe_convert_port_to_voucher(voucher
)),
3844 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_MSG_RECV_VOUCHER_REFUSED
) | DBG_FUNC_NONE
,
3845 VM_KERNEL_ADDRPERM((uintptr_t)kmsg
),
3846 (uintptr_t)kmsg
->ikm_header
->msgh_bits
,
3847 (uintptr_t)kmsg
->ikm_header
->msgh_id
,
3848 VM_KERNEL_ADDRPERM((uintptr_t)unsafe_convert_port_to_voucher(voucher
)),
3852 msg
->msgh_bits
= MACH_MSGH_BITS_SET(reply_type
, dest_type
,
3853 voucher_type
, mbits
);
3854 msg
->msgh_local_port
= CAST_MACH_NAME_TO_PORT(dest_name
);
3855 msg
->msgh_remote_port
= CAST_MACH_NAME_TO_PORT(reply_name
);
3856 msg
->msgh_voucher_port
= voucher_name
;
3859 return MACH_MSG_SUCCESS
;
3863 * Routine: ipc_kmsg_copyout_object
3865 * Copy-out a port right. Always returns a name,
3866 * even for unsuccessful return codes. Always
3867 * consumes the supplied object.
3871 * MACH_MSG_SUCCESS The space acquired the right
3872 * (name is valid) or the object is dead (MACH_PORT_DEAD).
3873 * MACH_MSG_IPC_SPACE No room in space for the right,
3874 * or the space is dead. (Name is MACH_PORT_NULL.)
3875 * MACH_MSG_IPC_KERNEL Kernel resource shortage.
3876 * (Name is MACH_PORT_NULL.)
3880 ipc_kmsg_copyout_object(
3882 ipc_object_t object
,
3883 mach_msg_type_name_t msgt_name
,
3884 mach_port_name_t
*namep
)
3888 if (!IO_VALID(object
)) {
3889 *namep
= CAST_MACH_PORT_TO_NAME(object
);
3890 return MACH_MSG_SUCCESS
;
3893 kr
= ipc_object_copyout(space
, object
, msgt_name
, TRUE
, namep
);
3894 if (kr
!= KERN_SUCCESS
) {
3895 ipc_object_destroy(object
, msgt_name
);
3897 if (kr
== KERN_INVALID_CAPABILITY
)
3898 *namep
= MACH_PORT_DEAD
;
3900 *namep
= MACH_PORT_NULL
;
3902 if (kr
== KERN_RESOURCE_SHORTAGE
)
3903 return MACH_MSG_IPC_KERNEL
;
3905 return MACH_MSG_IPC_SPACE
;
3909 return MACH_MSG_SUCCESS
;
3912 mach_msg_descriptor_t
*
3913 ipc_kmsg_copyout_port_descriptor(mach_msg_descriptor_t
*dsc
,
3914 mach_msg_descriptor_t
*user_dsc
,
3917 mach_msg_descriptor_t
*
3918 ipc_kmsg_copyout_port_descriptor(mach_msg_descriptor_t
*dsc
,
3919 mach_msg_descriptor_t
*dest_dsc
,
3924 mach_port_name_t name
;
3925 mach_msg_type_name_t disp
;
3928 /* Copyout port right carried in the message */
3929 port
= dsc
->port
.name
;
3930 disp
= dsc
->port
.disposition
;
3931 *mr
|= ipc_kmsg_copyout_object(space
,
3936 if(current_task() == kernel_task
)
3938 mach_msg_port_descriptor_t
*user_dsc
= (typeof(user_dsc
))dest_dsc
;
3939 user_dsc
--; // point to the start of this port descriptor
3940 bzero((void *)user_dsc
, sizeof(*user_dsc
));
3941 user_dsc
->name
= CAST_MACH_NAME_TO_PORT(name
);
3942 user_dsc
->disposition
= disp
;
3943 user_dsc
->type
= MACH_MSG_PORT_DESCRIPTOR
;
3944 dest_dsc
= (typeof(dest_dsc
))user_dsc
;
3946 mach_msg_legacy_port_descriptor_t
*user_dsc
= (typeof(user_dsc
))dest_dsc
;
3947 user_dsc
--; // point to the start of this port descriptor
3948 bzero((void *)user_dsc
, sizeof(*user_dsc
));
3949 user_dsc
->name
= CAST_MACH_PORT_TO_NAME(name
);
3950 user_dsc
->disposition
= disp
;
3951 user_dsc
->type
= MACH_MSG_PORT_DESCRIPTOR
;
3952 dest_dsc
= (typeof(dest_dsc
))user_dsc
;
3955 return (mach_msg_descriptor_t
*)dest_dsc
;
3958 mach_msg_descriptor_t
*
3959 ipc_kmsg_copyout_ool_descriptor(mach_msg_ool_descriptor_t
*dsc
, mach_msg_descriptor_t
*user_dsc
, int is_64bit
, vm_map_t map
, mach_msg_return_t
*mr
);
3960 mach_msg_descriptor_t
*
3961 ipc_kmsg_copyout_ool_descriptor(mach_msg_ool_descriptor_t
*dsc
, mach_msg_descriptor_t
*user_dsc
, int is_64bit
, vm_map_t map
, mach_msg_return_t
*mr
)
3964 vm_map_address_t rcv_addr
;
3965 mach_msg_copy_options_t copy_options
;
3967 mach_msg_descriptor_type_t dsc_type
;
3969 //SKIP_PORT_DESCRIPTORS(saddr, sdsc_count);
3971 copy
= (vm_map_copy_t
)dsc
->address
;
3972 size
= (vm_map_size_t
)dsc
->size
;
3973 copy_options
= dsc
->copy
;
3974 assert(copy_options
!= MACH_MSG_KALLOC_COPY_T
);
3975 dsc_type
= dsc
->type
;
3977 if (copy
!= VM_MAP_COPY_NULL
) {
3981 if (vm_map_copy_validate_size(map
, copy
, &size
) == FALSE
)
3982 panic("Inconsistent OOL/copyout size on %p: expected %d, got %lld @%p",
3983 dsc
, dsc
->size
, (unsigned long long)copy
->size
, copy
);
3984 kr
= vm_map_copyout_size(map
, &rcv_addr
, copy
, size
);
3985 if (kr
!= KERN_SUCCESS
) {
3986 if (kr
== KERN_RESOURCE_SHORTAGE
)
3987 *mr
|= MACH_MSG_VM_KERNEL
;
3989 *mr
|= MACH_MSG_VM_SPACE
;
3990 vm_map_copy_discard(copy
);
4000 * Now update the descriptor as the user would see it.
4001 * This may require expanding the descriptor to the user
4002 * visible size. There is already space allocated for
4003 * this in what naddr points to.
4005 if(current_task() == kernel_task
)
4007 mach_msg_ool_descriptor_t
*user_ool_dsc
= (typeof(user_ool_dsc
))user_dsc
;
4009 bzero((void *)user_ool_dsc
, sizeof(*user_ool_dsc
));
4011 user_ool_dsc
->address
= (void *)(uintptr_t)rcv_addr
;
4012 user_ool_dsc
->deallocate
= (copy_options
== MACH_MSG_VIRTUAL_COPY
) ?
4014 user_ool_dsc
->copy
= copy_options
;
4015 user_ool_dsc
->type
= dsc_type
;
4016 user_ool_dsc
->size
= (mach_msg_size_t
)size
;
4018 user_dsc
= (typeof(user_dsc
))user_ool_dsc
;
4019 } else if (is_64bit
) {
4020 mach_msg_ool_descriptor64_t
*user_ool_dsc
= (typeof(user_ool_dsc
))user_dsc
;
4022 bzero((void *)user_ool_dsc
, sizeof(*user_ool_dsc
));
4024 user_ool_dsc
->address
= rcv_addr
;
4025 user_ool_dsc
->deallocate
= (copy_options
== MACH_MSG_VIRTUAL_COPY
) ?
4027 user_ool_dsc
->copy
= copy_options
;
4028 user_ool_dsc
->type
= dsc_type
;
4029 user_ool_dsc
->size
= (mach_msg_size_t
)size
;
4031 user_dsc
= (typeof(user_dsc
))user_ool_dsc
;
4033 mach_msg_ool_descriptor32_t
*user_ool_dsc
= (typeof(user_ool_dsc
))user_dsc
;
4035 bzero((void *)user_ool_dsc
, sizeof(*user_ool_dsc
));
4037 user_ool_dsc
->address
= CAST_DOWN_EXPLICIT(uint32_t, rcv_addr
);
4038 user_ool_dsc
->size
= (mach_msg_size_t
)size
;
4039 user_ool_dsc
->deallocate
= (copy_options
== MACH_MSG_VIRTUAL_COPY
) ?
4041 user_ool_dsc
->copy
= copy_options
;
4042 user_ool_dsc
->type
= dsc_type
;
4044 user_dsc
= (typeof(user_dsc
))user_ool_dsc
;
4049 mach_msg_descriptor_t
*
4050 ipc_kmsg_copyout_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t
*dsc
,
4051 mach_msg_descriptor_t
*user_dsc
,
4056 mach_msg_return_t
*mr
);
4057 mach_msg_descriptor_t
*
4058 ipc_kmsg_copyout_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t
*dsc
,
4059 mach_msg_descriptor_t
*user_dsc
,
4064 mach_msg_return_t
*mr
)
4066 mach_vm_offset_t rcv_addr
= 0;
4067 mach_msg_type_name_t disp
;
4068 mach_msg_type_number_t count
, i
;
4069 vm_size_t ports_length
, names_length
;
4071 mach_msg_copy_options_t copy_options
= MACH_MSG_VIRTUAL_COPY
;
4073 //SKIP_PORT_DESCRIPTORS(saddr, sdsc_count);
4076 disp
= dsc
->disposition
;
4077 ports_length
= count
* sizeof(mach_port_t
);
4078 names_length
= count
* sizeof(mach_port_name_t
);
4080 if (ports_length
!= 0 && dsc
->address
!= 0) {
4083 * Check to see if there is an overwrite descriptor
4084 * specified in the scatter list for this ool data.
4085 * The descriptor has already been verified.
4088 if (saddr
!= MACH_MSG_DESCRIPTOR_NULL
) {
4090 OTHER_OOL_DESCRIPTOR
*scatter_dsc
;
4092 scatter_dsc
= (OTHER_OOL_DESCRIPTOR
*)saddr
;
4093 rcv_addr
= (mach_vm_offset_t
) scatter_dsc
->address
;
4094 copy_options
= scatter_dsc
->copy
;
4096 mach_msg_ool_descriptor_t
*scatter_dsc
;
4098 scatter_dsc
= &saddr
->out_of_line
;
4099 rcv_addr
= CAST_USER_ADDR_T(scatter_dsc
->address
);
4100 copy_options
= scatter_dsc
->copy
;
4102 INCREMENT_SCATTER(saddr
, sdsc_count
, differs
);
4106 if (copy_options
== MACH_MSG_VIRTUAL_COPY
) {
4108 * Dynamically allocate the region
4110 int anywhere
= VM_FLAGS_ANYWHERE
;
4111 if (vm_kernel_map_is_kernel(map
)) anywhere
|= VM_MAKE_TAG(VM_KERN_MEMORY_IPC
);
4112 else anywhere
|= VM_MAKE_TAG(VM_MEMORY_MACH_MSG
);
4115 if ((kr
= mach_vm_allocate(map
, &rcv_addr
,
4116 (mach_vm_size_t
)names_length
,
4117 anywhere
)) != KERN_SUCCESS
) {
4118 ipc_kmsg_clean_body(kmsg
, 1, (mach_msg_descriptor_t
*)dsc
);
4121 if (kr
== KERN_RESOURCE_SHORTAGE
){
4122 *mr
|= MACH_MSG_VM_KERNEL
;
4124 *mr
|= MACH_MSG_VM_SPACE
;
4130 * Handle the port rights and copy out the names
4131 * for those rights out to user-space.
4133 if (rcv_addr
!= 0) {
4134 mach_port_t
*objects
= (mach_port_t
*) dsc
->address
;
4135 mach_port_name_t
*names
= (mach_port_name_t
*) dsc
->address
;
4137 /* copyout port rights carried in the message */
4139 for ( i
= 0; i
< count
; i
++) {
4140 ipc_object_t object
= (ipc_object_t
)objects
[i
];
4142 *mr
|= ipc_kmsg_copyout_object(space
, object
,
4146 /* copyout to memory allocated above */
4147 void *data
= dsc
->address
;
4148 if (copyoutmap(map
, data
, rcv_addr
, names_length
) != KERN_SUCCESS
)
4149 *mr
|= MACH_MSG_VM_SPACE
;
4150 kfree(data
, ports_length
);
4157 * Now update the descriptor based on the information
4160 if(current_task() == kernel_task
) {
4161 mach_msg_ool_ports_descriptor_t
*user_ool_dsc
= (typeof(user_ool_dsc
))user_dsc
;
4163 bzero((void *)user_ool_dsc
, sizeof(*user_ool_dsc
));
4165 user_ool_dsc
->address
= (void *)(uintptr_t)rcv_addr
;
4166 user_ool_dsc
->deallocate
= (copy_options
== MACH_MSG_VIRTUAL_COPY
) ?
4168 user_ool_dsc
->copy
= copy_options
;
4169 user_ool_dsc
->disposition
= disp
;
4170 user_ool_dsc
->type
= MACH_MSG_OOL_PORTS_DESCRIPTOR
;
4171 user_ool_dsc
->count
= count
;
4173 user_dsc
= (typeof(user_dsc
))user_ool_dsc
;
4175 mach_msg_ool_ports_descriptor64_t
*user_ool_dsc
= (typeof(user_ool_dsc
))user_dsc
;
4177 bzero((void *)user_ool_dsc
, sizeof(*user_ool_dsc
));
4179 user_ool_dsc
->address
= rcv_addr
;
4180 user_ool_dsc
->deallocate
= (copy_options
== MACH_MSG_VIRTUAL_COPY
) ?
4182 user_ool_dsc
->copy
= copy_options
;
4183 user_ool_dsc
->disposition
= disp
;
4184 user_ool_dsc
->type
= MACH_MSG_OOL_PORTS_DESCRIPTOR
;
4185 user_ool_dsc
->count
= count
;
4187 user_dsc
= (typeof(user_dsc
))user_ool_dsc
;
4189 mach_msg_ool_ports_descriptor32_t
*user_ool_dsc
= (typeof(user_ool_dsc
))user_dsc
;
4191 bzero((void *)user_ool_dsc
, sizeof(*user_ool_dsc
));
4193 user_ool_dsc
->address
= CAST_DOWN_EXPLICIT(uint32_t, rcv_addr
);
4194 user_ool_dsc
->count
= count
;
4195 user_ool_dsc
->deallocate
= (copy_options
== MACH_MSG_VIRTUAL_COPY
) ?
4197 user_ool_dsc
->copy
= copy_options
;
4198 user_ool_dsc
->disposition
= disp
;
4199 user_ool_dsc
->type
= MACH_MSG_OOL_PORTS_DESCRIPTOR
;
4201 user_dsc
= (typeof(user_dsc
))user_ool_dsc
;
4207 * Routine: ipc_kmsg_copyout_body
4209 * "Copy-out" port rights and out-of-line memory
4210 * in the body of a message.
4212 * The error codes are a combination of special bits.
4213 * The copyout proceeds despite errors.
4217 * MACH_MSG_SUCCESS Successful copyout.
4218 * MACH_MSG_IPC_SPACE No room for port right in name space.
4219 * MACH_MSG_VM_SPACE No room for memory in address space.
4220 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
4221 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
4222 * MACH_MSG_INVALID_RT_DESCRIPTOR Descriptor incompatible with RT
4226 ipc_kmsg_copyout_body(
4230 mach_msg_body_t
*slist
)
4232 mach_msg_body_t
*body
;
4233 mach_msg_descriptor_t
*kern_dsc
, *user_dsc
;
4234 mach_msg_descriptor_t
*saddr
;
4235 mach_msg_type_number_t dsc_count
, sdsc_count
;
4237 mach_msg_return_t mr
= MACH_MSG_SUCCESS
;
4238 boolean_t is_task_64bit
= (map
->max_offset
> VM_MAX_ADDRESS
);
4240 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
4241 dsc_count
= body
->msgh_descriptor_count
;
4242 kern_dsc
= (mach_msg_descriptor_t
*) (body
+ 1);
4243 /* Point user_dsc just after the end of all the descriptors */
4244 user_dsc
= &kern_dsc
[dsc_count
];
4246 /* Do scatter list setup */
4247 if (slist
!= MACH_MSG_BODY_NULL
) {
4248 panic("Scatter lists disabled");
4249 saddr
= (mach_msg_descriptor_t
*) (slist
+ 1);
4250 sdsc_count
= slist
->msgh_descriptor_count
;
4253 saddr
= MACH_MSG_DESCRIPTOR_NULL
;
4257 /* Now process the descriptors */
4258 for (i
= dsc_count
-1; i
>= 0; i
--) {
4259 switch (kern_dsc
[i
].type
.type
) {
4261 case MACH_MSG_PORT_DESCRIPTOR
:
4262 user_dsc
= ipc_kmsg_copyout_port_descriptor(&kern_dsc
[i
], user_dsc
, space
, &mr
);
4264 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
4265 case MACH_MSG_OOL_DESCRIPTOR
:
4266 user_dsc
= ipc_kmsg_copyout_ool_descriptor(
4267 (mach_msg_ool_descriptor_t
*)&kern_dsc
[i
], user_dsc
, is_task_64bit
, map
, &mr
);
4269 case MACH_MSG_OOL_PORTS_DESCRIPTOR
:
4270 user_dsc
= ipc_kmsg_copyout_ool_ports_descriptor(
4271 (mach_msg_ool_ports_descriptor_t
*)&kern_dsc
[i
], user_dsc
, is_task_64bit
, map
, space
, kmsg
, &mr
);
4274 panic("untyped IPC copyout body: invalid message descriptor");
4279 if(user_dsc
!= kern_dsc
) {
4280 vm_offset_t dsc_adjust
= (vm_offset_t
)user_dsc
- (vm_offset_t
)kern_dsc
;
4281 memmove((char *)((vm_offset_t
)kmsg
->ikm_header
+ dsc_adjust
), kmsg
->ikm_header
, sizeof(mach_msg_base_t
));
4282 kmsg
->ikm_header
= (mach_msg_header_t
*)((vm_offset_t
)kmsg
->ikm_header
+ dsc_adjust
);
4283 /* Update the message size for the smaller user representation */
4284 kmsg
->ikm_header
->msgh_size
-= (mach_msg_size_t
)dsc_adjust
;
4291 * Routine: ipc_kmsg_copyout_size
4293 * Compute the size of the message as copied out to the given
4294 * map. If the destination map's pointers are a different size
4295 * than the kernel's, we have to allow for expansion/
4296 * contraction of the descriptors as appropriate.
4300 * size of the message as it would be received.
4304 ipc_kmsg_copyout_size(
4308 mach_msg_size_t send_size
;
4310 send_size
= kmsg
->ikm_header
->msgh_size
;
4312 boolean_t is_task_64bit
= (map
->max_offset
> VM_MAX_ADDRESS
);
4314 #if defined(__LP64__)
4315 send_size
-= LEGACY_HEADER_SIZE_DELTA
;
4318 if (kmsg
->ikm_header
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
4320 mach_msg_body_t
*body
;
4321 mach_msg_descriptor_t
*saddr
, *eaddr
;
4323 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
4324 saddr
= (mach_msg_descriptor_t
*) (body
+ 1);
4325 eaddr
= saddr
+ body
->msgh_descriptor_count
;
4327 for ( ; saddr
< eaddr
; saddr
++ ) {
4328 switch (saddr
->type
.type
) {
4329 case MACH_MSG_OOL_DESCRIPTOR
:
4330 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
4331 case MACH_MSG_OOL_PORTS_DESCRIPTOR
:
4333 send_size
-= DESC_SIZE_ADJUSTMENT
;
4335 case MACH_MSG_PORT_DESCRIPTOR
:
4336 send_size
-= DESC_SIZE_ADJUSTMENT
;
4347 * Routine: ipc_kmsg_copyout
4349 * "Copy-out" port rights and out-of-line memory
4354 * MACH_MSG_SUCCESS Copied out all rights and memory.
4355 * MACH_RCV_HEADER_ERROR + special bits
4356 * Rights and memory in the message are intact.
4357 * MACH_RCV_BODY_ERROR + special bits
4358 * The message header was successfully copied out.
4359 * As much of the body was handled as possible.
4367 mach_msg_body_t
*slist
,
4368 mach_msg_option_t option
)
4370 mach_msg_return_t mr
;
4372 mr
= ipc_kmsg_copyout_header(kmsg
, space
, option
);
4373 if (mr
!= MACH_MSG_SUCCESS
) {
4377 if (kmsg
->ikm_header
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
4378 mr
= ipc_kmsg_copyout_body(kmsg
, space
, map
, slist
);
4380 if (mr
!= MACH_MSG_SUCCESS
)
4381 mr
|= MACH_RCV_BODY_ERROR
;
4388 * Routine: ipc_kmsg_copyout_pseudo
4390 * Does a pseudo-copyout of the message.
4391 * This is like a regular copyout, except
4392 * that the ports in the header are handled
4393 * as if they are in the body. They aren't reversed.
4395 * The error codes are a combination of special bits.
4396 * The copyout proceeds despite errors.
4400 * MACH_MSG_SUCCESS Successful copyout.
4401 * MACH_MSG_IPC_SPACE No room for port right in name space.
4402 * MACH_MSG_VM_SPACE No room for memory in address space.
4403 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
4404 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
4408 ipc_kmsg_copyout_pseudo(
4412 mach_msg_body_t
*slist
)
4414 mach_msg_bits_t mbits
= kmsg
->ikm_header
->msgh_bits
;
4415 ipc_object_t dest
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
4416 ipc_object_t reply
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
4417 ipc_object_t voucher
= (ipc_object_t
) kmsg
->ikm_voucher
;
4418 mach_msg_type_name_t dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
4419 mach_msg_type_name_t reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
4420 mach_msg_type_name_t voucher_type
= MACH_MSGH_BITS_VOUCHER(mbits
);
4421 mach_port_name_t voucher_name
= kmsg
->ikm_header
->msgh_voucher_port
;
4422 mach_port_name_t dest_name
, reply_name
;
4423 mach_msg_return_t mr
;
4425 assert(IO_VALID(dest
));
4429 * If we did this here, it looks like we wouldn't need the undo logic
4430 * at the end of ipc_kmsg_send() in the error cases. Not sure which
4431 * would be more elegant to keep.
4433 ipc_importance_clean(kmsg
);
4435 /* just assert it is already clean */
4436 ipc_importance_assert_clean(kmsg
);
4439 mr
= (ipc_kmsg_copyout_object(space
, dest
, dest_type
, &dest_name
) |
4440 ipc_kmsg_copyout_object(space
, reply
, reply_type
, &reply_name
));
4442 kmsg
->ikm_header
->msgh_bits
= mbits
& MACH_MSGH_BITS_USER
;
4443 kmsg
->ikm_header
->msgh_remote_port
= CAST_MACH_NAME_TO_PORT(dest_name
);
4444 kmsg
->ikm_header
->msgh_local_port
= CAST_MACH_NAME_TO_PORT(reply_name
);
4446 if (IO_VALID(voucher
)) {
4447 assert(voucher_type
== MACH_MSG_TYPE_MOVE_SEND
);
4449 kmsg
->ikm_voucher
= IP_NULL
;
4450 mr
|= ipc_kmsg_copyout_object(space
, voucher
, voucher_type
, &voucher_name
);
4451 kmsg
->ikm_header
->msgh_voucher_port
= voucher_name
;
4454 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
4455 mr
|= ipc_kmsg_copyout_body(kmsg
, space
, map
, slist
);
4462 * Routine: ipc_kmsg_copyout_dest
4464 * Copies out the destination port in the message.
4465 * Destroys all other rights and memory in the message.
4471 ipc_kmsg_copyout_dest(
4475 mach_msg_bits_t mbits
;
4478 ipc_object_t voucher
;
4479 mach_msg_type_name_t dest_type
;
4480 mach_msg_type_name_t reply_type
;
4481 mach_msg_type_name_t voucher_type
;
4482 mach_port_name_t dest_name
, reply_name
, voucher_name
;
4484 mbits
= kmsg
->ikm_header
->msgh_bits
;
4485 dest
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
4486 reply
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
4487 voucher
= (ipc_object_t
) kmsg
->ikm_voucher
;
4488 voucher_name
= kmsg
->ikm_header
->msgh_voucher_port
;
4489 dest_type
= MACH_MSGH_BITS_REMOTE(mbits
);
4490 reply_type
= MACH_MSGH_BITS_LOCAL(mbits
);
4491 voucher_type
= MACH_MSGH_BITS_VOUCHER(mbits
);
4493 assert(IO_VALID(dest
));
4495 ipc_importance_assert_clean(kmsg
);
4498 if (io_active(dest
)) {
4499 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
4500 /* dest is unlocked */
4504 dest_name
= MACH_PORT_DEAD
;
4507 if (IO_VALID(reply
)) {
4508 ipc_object_destroy(reply
, reply_type
);
4509 reply_name
= MACH_PORT_NULL
;
4511 reply_name
= CAST_MACH_PORT_TO_NAME(reply
);
4513 if (IO_VALID(voucher
)) {
4514 assert(voucher_type
== MACH_MSG_TYPE_MOVE_SEND
);
4516 kmsg
->ikm_voucher
= IP_NULL
;
4517 ipc_object_destroy((ipc_object_t
)voucher
, voucher_type
);
4518 voucher_name
= MACH_PORT_NULL
;
4521 kmsg
->ikm_header
->msgh_bits
= MACH_MSGH_BITS_SET(reply_type
, dest_type
,
4522 voucher_type
, mbits
);
4523 kmsg
->ikm_header
->msgh_local_port
= CAST_MACH_NAME_TO_PORT(dest_name
);
4524 kmsg
->ikm_header
->msgh_remote_port
= CAST_MACH_NAME_TO_PORT(reply_name
);
4525 kmsg
->ikm_header
->msgh_voucher_port
= voucher_name
;
4527 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
4528 mach_msg_body_t
*body
;
4530 body
= (mach_msg_body_t
*) (kmsg
->ikm_header
+ 1);
4531 ipc_kmsg_clean_body(kmsg
, body
->msgh_descriptor_count
,
4532 (mach_msg_descriptor_t
*)(body
+ 1));
4537 * Routine: ipc_kmsg_copyout_to_kernel
4539 * Copies out the destination and reply ports in the message.
4540 * Leaves all other rights and memory in the message alone.
4544 * Derived from ipc_kmsg_copyout_dest.
4545 * Use by mach_msg_rpc_from_kernel (which used to use copyout_dest).
4546 * We really do want to save rights and memory.
4550 ipc_kmsg_copyout_to_kernel(
4556 mach_msg_type_name_t dest_type
;
4557 mach_msg_type_name_t reply_type
;
4558 mach_port_name_t dest_name
, reply_name
;
4560 dest
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
4561 reply
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
4562 dest_type
= MACH_MSGH_BITS_REMOTE(kmsg
->ikm_header
->msgh_bits
);
4563 reply_type
= MACH_MSGH_BITS_LOCAL(kmsg
->ikm_header
->msgh_bits
);
4565 assert(IO_VALID(dest
));
4568 if (io_active(dest
)) {
4569 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
4570 /* dest is unlocked */
4574 dest_name
= MACH_PORT_DEAD
;
4577 reply_name
= CAST_MACH_PORT_TO_NAME(reply
);
4579 kmsg
->ikm_header
->msgh_bits
=
4580 (MACH_MSGH_BITS_OTHER(kmsg
->ikm_header
->msgh_bits
) |
4581 MACH_MSGH_BITS(reply_type
, dest_type
));
4582 kmsg
->ikm_header
->msgh_local_port
= CAST_MACH_NAME_TO_PORT(dest_name
);
4583 kmsg
->ikm_header
->msgh_remote_port
= CAST_MACH_NAME_TO_PORT(reply_name
);
4586 #if IKM_SUPPORT_LEGACY
4588 ipc_kmsg_copyout_to_kernel_legacy(
4594 mach_msg_type_name_t dest_type
;
4595 mach_msg_type_name_t reply_type
;
4596 mach_port_name_t dest_name
, reply_name
;
4598 dest
= (ipc_object_t
) kmsg
->ikm_header
->msgh_remote_port
;
4599 reply
= (ipc_object_t
) kmsg
->ikm_header
->msgh_local_port
;
4600 dest_type
= MACH_MSGH_BITS_REMOTE(kmsg
->ikm_header
->msgh_bits
);
4601 reply_type
= MACH_MSGH_BITS_LOCAL(kmsg
->ikm_header
->msgh_bits
);
4603 assert(IO_VALID(dest
));
4606 if (io_active(dest
)) {
4607 ipc_object_copyout_dest(space
, dest
, dest_type
, &dest_name
);
4608 /* dest is unlocked */
4612 dest_name
= MACH_PORT_DEAD
;
4615 reply_name
= CAST_MACH_PORT_TO_NAME(reply
);
4617 kmsg
->ikm_header
->msgh_bits
=
4618 (MACH_MSGH_BITS_OTHER(kmsg
->ikm_header
->msgh_bits
) |
4619 MACH_MSGH_BITS(reply_type
, dest_type
));
4620 kmsg
->ikm_header
->msgh_local_port
= CAST_MACH_NAME_TO_PORT(dest_name
);
4621 kmsg
->ikm_header
->msgh_remote_port
= CAST_MACH_NAME_TO_PORT(reply_name
);
4623 mach_msg_descriptor_t
*saddr
;
4624 mach_msg_legacy_descriptor_t
*daddr
;
4625 mach_msg_type_number_t i
, count
= ((mach_msg_base_t
*)kmsg
->ikm_header
)->body
.msgh_descriptor_count
;
4626 saddr
= (mach_msg_descriptor_t
*) (((mach_msg_base_t
*)kmsg
->ikm_header
) + 1);
4627 saddr
= &saddr
[count
-1];
4628 daddr
= (mach_msg_legacy_descriptor_t
*)&saddr
[count
];
4631 vm_offset_t dsc_adjust
= 0;
4633 for (i
= 0; i
< count
; i
++, saddr
--, daddr
--) {
4634 switch (saddr
->type
.type
) {
4635 case MACH_MSG_PORT_DESCRIPTOR
: {
4636 mach_msg_port_descriptor_t
*dsc
= &saddr
->port
;
4637 mach_msg_legacy_port_descriptor_t
*dest_dsc
= &daddr
->port
;
4639 mach_port_t name
= dsc
->name
;
4640 mach_msg_type_name_t disposition
= dsc
->disposition
;
4642 dest_dsc
->name
= CAST_MACH_PORT_TO_NAME(name
);
4643 dest_dsc
->disposition
= disposition
;
4644 dest_dsc
->type
= MACH_MSG_PORT_DESCRIPTOR
;
4647 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
4648 case MACH_MSG_OOL_DESCRIPTOR
: {
4649 /* The sender should supply ready-made memory, i.e. a vm_map_copy_t
4650 * so we don't need to do anything special. */
4652 mach_msg_ool_descriptor_t
*source_dsc
= (typeof(source_dsc
))&saddr
->out_of_line
;
4654 mach_msg_ool_descriptor32_t
*dest_dsc
= &daddr
->out_of_line32
;
4656 vm_offset_t address
= (vm_offset_t
)source_dsc
->address
;
4657 vm_size_t size
= source_dsc
->size
;
4658 boolean_t deallocate
= source_dsc
->deallocate
;
4659 mach_msg_copy_options_t copy
= source_dsc
->copy
;
4660 mach_msg_descriptor_type_t type
= source_dsc
->type
;
4662 dest_dsc
->address
= address
;
4663 dest_dsc
->size
= size
;
4664 dest_dsc
->deallocate
= deallocate
;
4665 dest_dsc
->copy
= copy
;
4666 dest_dsc
->type
= type
;
4669 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
4670 mach_msg_ool_ports_descriptor_t
*source_dsc
= (typeof(source_dsc
))&saddr
->ool_ports
;
4672 mach_msg_ool_ports_descriptor32_t
*dest_dsc
= &daddr
->ool_ports32
;
4674 vm_offset_t address
= (vm_offset_t
)source_dsc
->address
;
4675 vm_size_t port_count
= source_dsc
->count
;
4676 boolean_t deallocate
= source_dsc
->deallocate
;
4677 mach_msg_copy_options_t copy
= source_dsc
->copy
;
4678 mach_msg_descriptor_type_t type
= source_dsc
->type
;
4680 dest_dsc
->address
= address
;
4681 dest_dsc
->count
= port_count
;
4682 dest_dsc
->deallocate
= deallocate
;
4683 dest_dsc
->copy
= copy
;
4684 dest_dsc
->type
= type
;
4689 panic("ipc_kmsg_copyin_from_kernel: bad descriptor");
4690 #endif /* MACH_ASSERT */
4696 dsc_adjust
= 4*count
;
4697 memmove((char *)((vm_offset_t
)kmsg
->ikm_header
+ dsc_adjust
), kmsg
->ikm_header
, sizeof(mach_msg_base_t
));
4698 kmsg
->ikm_header
= (mach_msg_header_t
*)((vm_offset_t
)kmsg
->ikm_header
+ dsc_adjust
);
4699 /* Update the message size for the smaller user representation */
4700 kmsg
->ikm_header
->msgh_size
-= dsc_adjust
;
4703 #endif /* IKM_SUPPORT_LEGACY */
4706 mach_msg_trailer_size_t
4707 ipc_kmsg_add_trailer(ipc_kmsg_t kmsg
, ipc_space_t space __unused
,
4708 mach_msg_option_t option
, thread_t thread
,
4709 mach_port_seqno_t seqno
, boolean_t minimal_trailer
,
4710 mach_vm_offset_t context
)
4712 mach_msg_max_trailer_t
*trailer
;
4715 trailer
= (mach_msg_max_trailer_t
*)
4716 ((vm_offset_t
)kmsg
->ikm_header
+
4717 round_msg(kmsg
->ikm_header
->msgh_size
));
4719 if (!(option
& MACH_RCV_TRAILER_MASK
)) {
4720 return trailer
->msgh_trailer_size
;
4723 trailer
->msgh_seqno
= seqno
;
4724 trailer
->msgh_context
= context
;
4725 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(thread_is_64bit(thread
), option
);
4727 if (minimal_trailer
) {
4731 if (MACH_RCV_TRAILER_ELEMENTS(option
) >=
4732 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AV
)){
4733 trailer
->msgh_ad
= 0;
4737 * The ipc_kmsg_t holds a reference to the label of a label
4738 * handle, not the port. We must get a reference to the port
4739 * and a send right to copyout to the receiver.
4742 if (option
& MACH_RCV_TRAILER_ELEMENTS (MACH_RCV_TRAILER_LABELS
)) {
4743 trailer
->msgh_labels
.sender
= 0;
4748 return trailer
->msgh_trailer_size
;