]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/ipc/ipc_kmsg.c
xnu-3248.20.55.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_kmsg.c
index 9adfe5b832f90296e4b60148459a118620099a51..f5737416afa4c434aa8ea783fdbfff673de47e7f 100644 (file)
@@ -70,7 +70,6 @@
  *     Operations on kernel messages.
  */
 
-#include <norma_vm.h>
 
 #include <mach/mach_types.h>
 #include <mach/boolean.h>
 #include <ipc/ipc_right.h>
 #include <ipc/ipc_hash.h>
 #include <ipc/ipc_table.h>
+#include <ipc/ipc_importance.h>
 
 #include <security/mac_mach_internal.h>
 
 #define DEBUG_MSGS_K64 1
 #endif
 
+#include <sys/kdebug.h>
+#include <libkern/OSAtomic.h>
+
 #pragma pack(4)
 
 typedef        struct 
 {
   mach_msg_bits_t      msgh_bits;
   mach_msg_size_t      msgh_size;
-  uint32_t             msgh_remote_port;
-  uint32_t             msgh_local_port;
-  mach_msg_size_t      msgh_reserved;
+  mach_port_name_t     msgh_remote_port;
+  mach_port_name_t     msgh_local_port;
+  mach_port_name_t     msgh_voucher_port;
   mach_msg_id_t                msgh_id;
 } mach_msg_legacy_header_t;
 
@@ -165,6 +168,7 @@ typedef union
 #pragma pack()
 
 #define LEGACY_HEADER_SIZE_DELTA ((mach_msg_size_t)(sizeof(mach_msg_header_t) - sizeof(mach_msg_legacy_header_t)))
+
 // END LP64 fixes
 
 
@@ -393,7 +397,7 @@ mm_copy_options_string64(
                name = "VIRTUAL";
                break;
            case MACH_MSG_OVERWRITE:
-               name = "OVERWRITE";
+               name = "OVERWRITE(DEPRECATED)";
                break;
            case MACH_MSG_ALLOCATE:
                name = "ALLOCATE";
@@ -471,20 +475,27 @@ ipc_msg_print_untyped64(
 }
 
 #define        DEBUG_IPC_KMSG_PRINT(kmsg,string)       \
+       __unreachable_ok_push   \
        if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {    \
                ipc_kmsg_print64(kmsg, string); \
-       }
+       }       \
+       __unreachable_ok_pop
+
 #define        DEBUG_IPC_MSG_BODY_PRINT(body,size)     \
+       __unreachable_ok_push   \
        if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {    \
                ipc_msg_body_print64(body,size);\
-       }
+       }       \
+       __unreachable_ok_pop
 #else /* !DEBUG_MSGS_K64 */
 #define DEBUG_IPC_KMSG_PRINT(kmsg,string)
 #define        DEBUG_IPC_MSG_BODY_PRINT(body,size)
 #endif  /* !DEBUG_MSGS_K64 */
 
 extern vm_map_t                ipc_kernel_copy_map;
+extern vm_size_t       ipc_kmsg_max_space;
 extern vm_size_t       ipc_kmsg_max_vm_space;
+extern vm_size_t       ipc_kmsg_max_body_space;
 extern vm_size_t       msg_ool_size_small;
 
 #define MSG_OOL_SIZE_SMALL     msg_ool_size_small
@@ -591,20 +602,25 @@ ipc_kmsg_alloc(
         * data backwards.
         */
        mach_msg_size_t size = msg_and_trailer_size - MAX_TRAILER_SIZE;
+
+       /* compare against implementation upper limit for the body */
+       if (size > ipc_kmsg_max_body_space)
+               return IKM_NULL;
+
        if (size > sizeof(mach_msg_base_t)) {
                mach_msg_size_t max_desc = (mach_msg_size_t)(((size - sizeof(mach_msg_base_t)) /
                                           sizeof(mach_msg_ool_descriptor32_t)) *
                                           DESC_SIZE_ADJUSTMENT);
-               if (msg_and_trailer_size >= MACH_MSG_SIZE_MAX - max_desc)
+
+               /* make sure expansion won't cause wrap */
+               if (msg_and_trailer_size > MACH_MSG_SIZE_MAX - max_desc)
                        return IKM_NULL;
 
                max_expanded_size = msg_and_trailer_size + max_desc;
        } else
-               max_expanded_size = msg_and_trailer_size;
+         max_expanded_size = msg_and_trailer_size;
 
-       if (max_expanded_size > ikm_less_overhead(MACH_MSG_SIZE_MAX))
-               return IKM_NULL;
-       else if (max_expanded_size < IKM_SAVED_MSG_SIZE)
+       if (max_expanded_size < IKM_SAVED_MSG_SIZE)
                max_expanded_size = IKM_SAVED_MSG_SIZE;         /* round up for ikm_cache */
 
        if (max_expanded_size == IKM_SAVED_MSG_SIZE) {
@@ -617,12 +633,9 @@ ipc_kmsg_alloc(
                        assert(i <= IKM_STASH);
                        kmsg = cache->entries[--i];
                        cache->avail = i;
-                       ikm_check_init(kmsg, max_expanded_size);
                        enable_preemption();
-                       kmsg->ikm_header = (mach_msg_header_t *)
-                                          ((vm_offset_t)(kmsg + 1) +
-                                           max_expanded_size -
-                                           msg_and_trailer_size);
+                       ikm_check_init(kmsg, max_expanded_size);
+                       ikm_set_header(kmsg, msg_and_trailer_size);
                        return (kmsg);
                }
                enable_preemption();
@@ -633,10 +646,7 @@ ipc_kmsg_alloc(
 
        if (kmsg != IKM_NULL) {
                ikm_init(kmsg, max_expanded_size);
-               kmsg->ikm_header = (mach_msg_header_t *)
-                                  ((vm_offset_t)(kmsg + 1) +
-                                   max_expanded_size -
-                                   msg_and_trailer_size);
+               ikm_set_header(kmsg, msg_and_trailer_size);
        }
 
        return(kmsg);
@@ -661,12 +671,11 @@ ipc_kmsg_free(
        mach_msg_size_t size = kmsg->ikm_size;
        ipc_port_t port;
 
-#if CONFIG_MACF_MACH
-       if (kmsg->ikm_sender != NULL) {
-               task_deallocate(kmsg->ikm_sender);
-               kmsg->ikm_sender = NULL;
-       }
-#endif
+       assert(!IP_VALID(kmsg->ikm_voucher));
+
+       KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_FREE) | DBG_FUNC_NONE,
+                             VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
+                             0, 0, 0, 0);
 
        /*
         * Check to see if the message is bound to the port.  If so,
@@ -680,9 +689,11 @@ ipc_kmsg_free(
                if (ip_active(port) && (port->ip_premsg == kmsg)) {
                        assert(IP_PREALLOC(port));
                        ip_unlock(port);
+                       ip_release(port);
                        return;
                }
-               ip_check_unlock(port);  /* May be last reference */
+                ip_unlock(port);
+               ip_release(port); /* May be last reference */
        }
 
        /*
@@ -814,51 +825,59 @@ void
 ipc_kmsg_destroy(
        ipc_kmsg_t      kmsg)
 {
-       ipc_kmsg_queue_t queue;
-       boolean_t empty;
-
        /*
-        *      ipc_kmsg_clean can cause more messages to be destroyed.
-        *      Curtail recursion by queueing messages.  If a message
-        *      is already queued, then this is a recursive call.
+        *      Destroying a message can cause more messages to be destroyed.
+        *      Curtail recursion by putting messages on the deferred
+        *      destruction queue.  If this was the first message on the
+        *      queue, this instance must process the full queue.
         */
+       if (ipc_kmsg_delayed_destroy(kmsg))
+               ipc_kmsg_reap_delayed();
+}
 
-       queue = &(current_thread()->ith_messages);
-       empty = ipc_kmsg_queue_empty(queue);
-       ipc_kmsg_enqueue(queue, kmsg);
+/*
+ *     Routine:        ipc_kmsg_delayed_destroy
+ *     Purpose:
+ *             Enqueues a kernel message for deferred destruction.
+ *     Returns:
+ *             Boolean indicator that the caller is responsible to reap
+ *             deferred messages.
+ */
 
-       if (empty) {
-               /* must leave kmsg in queue while cleaning it */
+boolean_t ipc_kmsg_delayed_destroy(
+       ipc_kmsg_t kmsg)
+{
+       ipc_kmsg_queue_t queue = &(current_thread()->ith_messages);
+       boolean_t first = ipc_kmsg_queue_empty(queue);
 
-               while ((kmsg = ipc_kmsg_queue_first(queue)) != IKM_NULL) {
-                       ipc_kmsg_clean(kmsg);
-                       ipc_kmsg_rmqueue(queue, kmsg);
-                       ipc_kmsg_free(kmsg);
-               }
-       }
+       ipc_kmsg_enqueue(queue, kmsg);
+       return first;
 }
 
 /*
- *     Routine:        ipc_kmsg_destroy_dest
+ *     Routine:        ipc_kmsg_destroy_queue
  *     Purpose:
- *             Destroys a kernel message.  Releases all rights,
- *             references, and memory held by the message (including
- *             the destination port reference.
- *             Frees the message.
+ *             Destroys messages from the per-thread
+ *             deferred reaping queue.
  *     Conditions:
  *             No locks held.
  */
+
 void
-ipc_kmsg_destroy_dest(
-       ipc_kmsg_t kmsg)
+ipc_kmsg_reap_delayed(void)
 {
-    ipc_port_t port;
-       
-    port = kmsg->ikm_header->msgh_remote_port;
+       ipc_kmsg_queue_t queue = &(current_thread()->ith_messages);
+       ipc_kmsg_t kmsg;
 
-    ipc_port_release(port);
-    kmsg->ikm_header->msgh_remote_port = MACH_PORT_NULL;
-    ipc_kmsg_destroy(kmsg);
+       /*
+        * must leave kmsg in queue while cleaning it to assure
+        * no nested calls recurse into here.
+        */
+       while ((kmsg = ipc_kmsg_queue_first(queue)) != IKM_NULL) {
+               ipc_kmsg_clean(kmsg);
+               ipc_kmsg_rmqueue(queue, kmsg);
+               ipc_kmsg_free(kmsg);
+       }
 }
 
 /*
@@ -870,7 +889,7 @@ ipc_kmsg_destroy_dest(
  *     Conditions:
  *             No locks held.
  */
-
+static unsigned int _ipc_kmsg_clean_invalid_desc = 0;
 void
 ipc_kmsg_clean_body(
        __unused ipc_kmsg_t     kmsg,
@@ -949,7 +968,7 @@ ipc_kmsg_clean_body(
                break;
            }
            default : {
-               printf("cleanup: don't understand this type of descriptor\n");
+                   _ipc_kmsg_clean_invalid_desc++; /* don't understand this type of descriptor */
            }
        }
     }
@@ -979,14 +998,24 @@ ipc_kmsg_clean_partial(
        ipc_object_t object;
        mach_msg_bits_t mbits = kmsg->ikm_header->msgh_bits;
 
+       /* deal with importance chain while we still have dest and voucher references */
+       ipc_importance_clean(kmsg);
+
        object = (ipc_object_t) kmsg->ikm_header->msgh_remote_port;
        assert(IO_VALID(object));
-       ipc_object_destroy(object, MACH_MSGH_BITS_REMOTE(mbits));
+       ipc_object_destroy_dest(object, MACH_MSGH_BITS_REMOTE(mbits));
 
        object = (ipc_object_t) kmsg->ikm_header->msgh_local_port;
        if (IO_VALID(object))
                ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
 
+       object = (ipc_object_t) kmsg->ikm_voucher;
+       if (IO_VALID(object)) {
+               assert(MACH_MSGH_BITS_VOUCHER(mbits) == MACH_MSG_TYPE_MOVE_SEND);
+               ipc_object_destroy(object, MACH_MSG_TYPE_PORT_SEND);
+               kmsg->ikm_voucher = IP_NULL;
+       }
+
        if (paddr) {
                (void) vm_deallocate(ipc_kernel_copy_map, paddr, length);
        }
@@ -1010,15 +1039,25 @@ ipc_kmsg_clean(
        ipc_object_t object;
        mach_msg_bits_t mbits;
 
+       /* deal with importance chain while we still have dest and voucher references */
+       ipc_importance_clean(kmsg);
+
        mbits = kmsg->ikm_header->msgh_bits;
        object = (ipc_object_t) kmsg->ikm_header->msgh_remote_port;
        if (IO_VALID(object))
-               ipc_object_destroy(object, MACH_MSGH_BITS_REMOTE(mbits));
+               ipc_object_destroy_dest(object, MACH_MSGH_BITS_REMOTE(mbits));
 
        object = (ipc_object_t) kmsg->ikm_header->msgh_local_port;
        if (IO_VALID(object))
                ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
 
+       object = (ipc_object_t) kmsg->ikm_voucher;
+       if (IO_VALID(object)) {
+               assert(MACH_MSGH_BITS_VOUCHER(mbits) == MACH_MSG_TYPE_MOVE_SEND);
+               ipc_object_destroy(object, MACH_MSG_TYPE_PORT_SEND);
+               kmsg->ikm_voucher = IP_NULL;
+       }
+
        if (mbits & MACH_MSGH_BITS_COMPLEX) {
                mach_msg_body_t *body;
 
@@ -1026,13 +1065,6 @@ ipc_kmsg_clean(
                ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count,
                                    (mach_msg_descriptor_t *)(body + 1));
        }
-
-#if CONFIG_MACF_MACH
-       if (kmsg->ikm_sender != NULL) {
-               task_deallocate(kmsg->ikm_sender);
-               kmsg->ikm_sender = NULL;
-       }
-#endif
 }
 
 /*
@@ -1072,6 +1104,23 @@ ipc_kmsg_clear_prealloc(
        IP_CLEAR_PREALLOC(port, kmsg);
 }
 
+/*
+ *     Routine:        ipc_kmsg_prealloc
+ *     Purpose:
+ *             Wraper to ipc_kmsg_alloc() to account for
+ *             header expansion requirements.
+ */
+ipc_kmsg_t
+ipc_kmsg_prealloc(mach_msg_size_t size)
+{
+#if defined(__LP64__)
+       if (size > MACH_MSG_SIZE_MAX - LEGACY_HEADER_SIZE_DELTA)
+               return IKM_NULL;
+
+       size += LEGACY_HEADER_SIZE_DELTA;
+#endif
+       return ipc_kmsg_alloc(size);
+}
 
 
 /*
@@ -1085,6 +1134,7 @@ ipc_kmsg_clear_prealloc(
  *             MACH_MSG_SUCCESS        Acquired a message buffer.
  *             MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
  *             MACH_SEND_MSG_TOO_SMALL Message size not long-word multiple.
+ *             MACH_SEND_TOO_LARGE     Message too large to ever be sent.
  *             MACH_SEND_NO_BUFFER     Couldn't allocate a message buffer.
  *             MACH_SEND_INVALID_DATA  Couldn't copy message data.
  */
@@ -1105,7 +1155,7 @@ ipc_kmsg_get(
        if ((size < sizeof(mach_msg_legacy_header_t)) || (size & 3))
                return MACH_SEND_MSG_TOO_SMALL;
 
-       if (size > MACH_MSG_SIZE_MAX - MAX_TRAILER_SIZE)
+       if (size > ipc_kmsg_max_body_space)
                return MACH_SEND_TOO_LARGE;
 
        if(size == sizeof(mach_msg_legacy_header_t))
@@ -1120,12 +1170,15 @@ ipc_kmsg_get(
 #if defined(__LP64__)
        size += LEGACY_HEADER_SIZE_DELTA;
 #endif
+       /* unreachable if !DEBUG */
+       __unreachable_ok_push
        if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {
                unsigned int j;
                for (j=0; j<sizeof(legacy_base.header); j++) {
                        kprintf("%02x\n", ((unsigned char*)&legacy_base.header)[j]);
                }
        }
+       __unreachable_ok_pop
 
        msg_and_trailer_size = size + MAX_TRAILER_SIZE;
        kmsg = ipc_kmsg_alloc(msg_and_trailer_size);
@@ -1136,7 +1189,7 @@ ipc_kmsg_get(
        kmsg->ikm_header->msgh_bits                     = legacy_base.header.msgh_bits;
        kmsg->ikm_header->msgh_remote_port      = CAST_MACH_NAME_TO_PORT(legacy_base.header.msgh_remote_port);
        kmsg->ikm_header->msgh_local_port       = CAST_MACH_NAME_TO_PORT(legacy_base.header.msgh_local_port);
-       kmsg->ikm_header->msgh_reserved         = legacy_base.header.msgh_reserved;
+       kmsg->ikm_header->msgh_voucher_port             = legacy_base.header.msgh_voucher_port;
        kmsg->ikm_header->msgh_id                       = legacy_base.header.msgh_id;
 
        DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_get header:\n"
@@ -1144,13 +1197,13 @@ ipc_kmsg_get(
                                                         "  bits:               0x%.8x\n"
                                                         "  remote_port:        %p\n"
                                                         "  local_port: %p\n"
-                                                        "  reserved:   0x%.8x\n"
+                                                        "  voucher_port:       0x%.8x\n"
                                                         "  id:         %.8d\n",
                                                         kmsg->ikm_header->msgh_size,
                                                         kmsg->ikm_header->msgh_bits,
                                                         kmsg->ikm_header->msgh_remote_port,
                                                         kmsg->ikm_header->msgh_local_port,
-                                                        kmsg->ikm_header->msgh_reserved,
+                                                        kmsg->ikm_header->msgh_voucher_port,
                                                         kmsg->ikm_header->msgh_id);
 
        if (copyinmsg(msg_addr, (char *)(kmsg->ikm_header + 1), size - (mach_msg_size_t)sizeof(mach_msg_header_t))) {
@@ -1158,6 +1211,8 @@ ipc_kmsg_get(
                return MACH_SEND_INVALID_DATA;
        }
 
+       /* unreachable if !DEBUG */
+       __unreachable_ok_push
        if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK))
        {
                kprintf("body: size: %lu\n", (size - sizeof(mach_msg_header_t)));
@@ -1167,6 +1222,7 @@ ipc_kmsg_get(
                        kprintf("%.4x\n",((uint32_t *)(kmsg->ikm_header + 1))[i]);
                }
        }
+       __unreachable_ok_pop
        DEBUG_IPC_KMSG_PRINT(kmsg, "ipc_kmsg_get()");
 
        /* 
@@ -1187,18 +1243,7 @@ ipc_kmsg_get(
                (unsigned int)kmsg->ikm_header->msgh_local_port, 0); 
 #endif
 
-#if CONFIG_MACF_MACH
-       /* XXX - why do we zero sender labels here instead of in mach_msg()? */
-       task_t cur = current_task();
-       if (cur) {
-               task_reference(cur);
-               kmsg->ikm_sender = cur;
-       } else
-               trailer->msgh_labels.sender = 0;
-#else
        trailer->msgh_labels.sender = 0;
-#endif
-
        *kmsgp = kmsg;
        return MACH_MSG_SUCCESS;
 }
@@ -1231,9 +1276,8 @@ ipc_kmsg_get_from_kernel(
        ipc_port_t      dest_port;
 
        assert(size >= sizeof(mach_msg_header_t));
-//     assert((size & 3) == 0);
+       assert((size & 3) == 0);
 
-       assert(IP_VALID((ipc_port_t) msg->msgh_remote_port));
        dest_port = (ipc_port_t)msg->msgh_remote_port;
 
        msg_and_trailer_size = size + MAX_TRAILER_SIZE;
@@ -1243,10 +1287,9 @@ ipc_kmsg_get_from_kernel(
         * clients.  These are set up for those kernel clients
         * which cannot afford to wait.
         */
-#ifndef __LP64__
-       /* LP64todo - does the prealloc kmsg need ikm_header padding?
-        */
-       if (IP_PREALLOC(dest_port)) {
+       if (IP_VALID(dest_port) && IP_PREALLOC(dest_port)) {
+               mach_msg_size_t max_desc = 0;
+
                ip_lock(dest_port);
                if (!ip_active(dest_port)) {
                        ip_unlock(dest_port);
@@ -1254,19 +1297,26 @@ ipc_kmsg_get_from_kernel(
                }
                assert(IP_PREALLOC(dest_port));
                kmsg = dest_port->ip_premsg;
-               if (msg_and_trailer_size > kmsg->ikm_size) {
-                       ip_unlock(dest_port);
-                       return MACH_SEND_TOO_LARGE;
-               }
                if (ikm_prealloc_inuse(kmsg)) {
                        ip_unlock(dest_port);
                        return MACH_SEND_NO_BUFFER;
                }
+#if !defined(__LP64__)
+               if (msg->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
+                       assert(size > sizeof(mach_msg_base_t));
+                       max_desc = ((mach_msg_base_t *)msg)->body.msgh_descriptor_count *
+                               DESC_SIZE_ADJUSTMENT;
+               }
+#endif
+               if (msg_and_trailer_size > kmsg->ikm_size - max_desc) {
+                       ip_unlock(dest_port);
+                       return MACH_SEND_TOO_LARGE;
+               }
                ikm_prealloc_set_inuse(kmsg, dest_port);
+               ikm_set_header(kmsg, msg_and_trailer_size);
                ip_unlock(dest_port);
        }
        else
-#endif /* !__LP64__ */
        {
                kmsg = ipc_kmsg_alloc(msg_and_trailer_size);
                if (kmsg == IKM_NULL)
@@ -1292,9 +1342,6 @@ ipc_kmsg_get_from_kernel(
 
        trailer->msgh_labels.sender = 0;
 
-#if CONFIG_MACF_MACH
-       kmsg->ikm_sender = NULL;
-#endif
        *kmsgp = kmsg;
        return MACH_MSG_SUCCESS;
 }
@@ -1315,7 +1362,10 @@ ipc_kmsg_get_from_kernel(
  *             MACH_MSG_SUCCESS        The message was accepted.
  *             MACH_SEND_TIMED_OUT     Caller still has message.
  *             MACH_SEND_INTERRUPTED   Caller still has message.
+ *             MACH_SEND_INVALID_DEST  Caller still has message.
  */
+
+
 mach_msg_return_t
 ipc_kmsg_send(
        ipc_kmsg_t              kmsg,
@@ -1323,17 +1373,54 @@ ipc_kmsg_send(
        mach_msg_timeout_t      send_timeout)
 {
        ipc_port_t port;
+       thread_t th = current_thread();
        mach_msg_return_t error = MACH_MSG_SUCCESS;
+       boolean_t kernel_reply = FALSE;
        spl_t s;
 
-       port = (ipc_port_t) kmsg->ikm_header->msgh_remote_port;
-       assert(IP_VALID(port));
+       /* Check if honor qlimit flag is set on thread. */
+       if ((th->options & TH_OPT_HONOR_QLIMIT) == TH_OPT_HONOR_QLIMIT) {
+               /* Remove the MACH_SEND_ALWAYS flag to honor queue limit. */
+               option &= (~MACH_SEND_ALWAYS);
+               /* Add the timeout flag since the message queue might be full. */
+               option |= MACH_SEND_TIMEOUT;
+               th->options &= (~TH_OPT_HONOR_QLIMIT);
+       }
 
-       if ((option & ~(MACH_SEND_TIMEOUT|MACH_SEND_ALWAYS)) != 0)
-               printf("ipc_kmsg_send: bad option 0x%x\n", option);
+#if IMPORTANCE_INHERITANCE
+       boolean_t did_importance = FALSE;
+#if IMPORTANCE_DEBUG
+       mach_msg_id_t imp_msgh_id = -1;
+       int           sender_pid  = -1;
+#endif /* IMPORTANCE_DEBUG */
+#endif /* IMPORTANCE_INHERITANCE */
 
+       /* don't allow the creation of a circular loop */
+       if (kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_CIRCULAR) {
+               ipc_kmsg_destroy(kmsg);
+               return MACH_MSG_SUCCESS;
+       }
+
+       port = (ipc_port_t) kmsg->ikm_header->msgh_remote_port;
+       assert(IP_VALID(port));
        ip_lock(port);
 
+#if IMPORTANCE_INHERITANCE
+retry:
+#endif /* IMPORTANCE_INHERITANCE */
+       /*
+        *      Can't deliver to a dead port.
+        *      However, we can pretend it got sent
+        *      and was then immediately destroyed.
+        */
+       if (!ip_active(port)) {
+               ip_unlock(port);
+               ip_release(port);  /* JMM - Future: release right, not just ref */
+               kmsg->ikm_header->msgh_remote_port = MACH_PORT_NULL;
+               ipc_kmsg_destroy(kmsg);
+               return MACH_MSG_SUCCESS;
+       }
+
        if (port->ip_receiver == ipc_space_kernel) {
 
                /*
@@ -1359,36 +1446,21 @@ ipc_kmsg_send(
                assert(IP_VALID(port));
                ip_lock(port);
                /* fall thru with reply - same options */
+               kernel_reply = TRUE;
        }
 
+#if IMPORTANCE_INHERITANCE
        /*
-        *      Can't deliver to a dead port.
-        *      However, we can pretend it got sent
-        *      and was then immediately destroyed.
+        * Need to see if this message needs importance donation and/or
+        * propagation.  That routine can drop the port lock temporarily.
+        * If it does we'll have to revalidate the destination.
         */
-       if (!ip_active(port)) {
-               /*
-                *      We can't let ipc_kmsg_destroy deallocate
-                *      the port right, because we might end up
-                *      in an infinite loop trying to deliver
-                *      a send-once notification.
-                */
-
-               ip_release(port);
-               ip_check_unlock(port);
-               kmsg->ikm_header->msgh_remote_port = MACH_PORT_NULL;
-               ipc_kmsg_destroy(kmsg);
-               return MACH_MSG_SUCCESS;
-       }
-
-       if (kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_CIRCULAR) {
-               ip_unlock(port);
-
-               /* don't allow the creation of a circular loop */
-
-               ipc_kmsg_destroy(kmsg);
-               return MACH_MSG_SUCCESS;
+       if (did_importance == FALSE) {
+               did_importance = TRUE;
+               if (ipc_importance_send(kmsg, option))
+                       goto retry;
        }
+#endif /* IMPORTANCE_INHERITANCE */
 
        /*
         * We have a valid message and a valid reference on the port.
@@ -1398,14 +1470,66 @@ ipc_kmsg_send(
        s = splsched();
        imq_lock(&port->ip_messages);
        ip_unlock(port);
+
        error = ipc_mqueue_send(&port->ip_messages, kmsg, option, 
                        send_timeout, s);
 
+#if IMPORTANCE_INHERITANCE
+       if (did_importance == TRUE) {
+               __unused int importance_cleared = 0;
+               switch (error) {
+                       case MACH_SEND_TIMED_OUT:
+                       case MACH_SEND_NO_BUFFER:
+                       case MACH_SEND_INTERRUPTED:
+                       case MACH_SEND_INVALID_DEST:
+                               /*
+                                * We still have the kmsg and its
+                                * reference on the port.  But we
+                                * have to back out the importance
+                                * boost.
+                                *
+                                * The port could have changed hands,
+                                * be inflight to another destination,
+                                * etc...  But in those cases our
+                                * back-out will find the new owner
+                                * (and all the operations that
+                                * transferred the right should have
+                                * applied their own boost adjustments
+                                * to the old owner(s)).
+                                */
+                               importance_cleared = 1;
+                               ipc_importance_clean(kmsg);
+                               break;
+
+                       case MACH_MSG_SUCCESS:
+                       default:
+                               break;
+               }
+#if IMPORTANCE_DEBUG
+               KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_MSG, IMP_MSG_SEND)) | DBG_FUNC_END,
+                                         task_pid(current_task()), sender_pid, imp_msgh_id, importance_cleared, 0);
+#endif /* IMPORTANCE_DEBUG */
+       }
+#endif /* IMPORTANCE_INHERITANCE */
+
        /*
         * If the port has been destroyed while we wait, treat the message
         * as a successful delivery (like we do for an inactive port).
         */
        if (error == MACH_SEND_INVALID_DEST) {
+               ip_release(port); /* JMM - Future: release right, not just ref */
+               kmsg->ikm_header->msgh_remote_port = MACH_PORT_NULL;
+               ipc_kmsg_destroy(kmsg);
+               return MACH_MSG_SUCCESS;
+       }
+
+       if (error != MACH_MSG_SUCCESS && kernel_reply) {
+               /*
+                * Kernel reply messages that fail can't be allowed to
+                * pseudo-receive on error conditions. We need to just treat
+                * the message as a successful delivery.
+                */
+               ip_release(port); /* JMM - Future: release right, not just ref */
                kmsg->ikm_header->msgh_remote_port = MACH_PORT_NULL;
                ipc_kmsg_destroy(kmsg);
                return MACH_MSG_SUCCESS;
@@ -1443,13 +1567,13 @@ ipc_kmsg_put(
                                                         "  bits:               0x%.8x\n"
                                                         "  remote_port:        %p\n"
                                                         "  local_port: %p\n"
-                                                        "  reserved:   0x%.8x\n"
+                                                        "  voucher_port:       0x%.8x\n"
                                                         "  id:         %.8d\n",
                                                         kmsg->ikm_header->msgh_size,
                                                         kmsg->ikm_header->msgh_bits,
                                                         kmsg->ikm_header->msgh_remote_port,
                                                         kmsg->ikm_header->msgh_local_port,
-                                                        kmsg->ikm_header->msgh_reserved,
+                                                        kmsg->ikm_header->msgh_voucher_port,
                                                         kmsg->ikm_header->msgh_id);
 
 #if defined(__LP64__)
@@ -1461,13 +1585,13 @@ ipc_kmsg_put(
                mach_msg_size_t         msg_size        = kmsg->ikm_header->msgh_size;
                mach_port_name_t        remote_port     = CAST_MACH_PORT_TO_NAME(kmsg->ikm_header->msgh_remote_port);
                mach_port_name_t        local_port      = CAST_MACH_PORT_TO_NAME(kmsg->ikm_header->msgh_local_port);
-               mach_msg_size_t         reserved        = kmsg->ikm_header->msgh_reserved;
+               mach_port_name_t        voucher_port    = kmsg->ikm_header->msgh_voucher_port;
                mach_msg_id_t           id                      = kmsg->ikm_header->msgh_id;
 
                legacy_header->msgh_id                  = id;
-               legacy_header->msgh_reserved    = reserved;
-               legacy_header->msgh_local_port  = local_port;
-               legacy_header->msgh_remote_port = remote_port;
+               legacy_header->msgh_local_port = local_port;
+               legacy_header->msgh_remote_port = remote_port;
+               legacy_header->msgh_voucher_port = voucher_port;
                legacy_header->msgh_size                = msg_size - LEGACY_HEADER_SIZE_DELTA;
                legacy_header->msgh_bits                = bits;
 
@@ -1476,6 +1600,8 @@ ipc_kmsg_put(
        }
 #endif
 
+       /* unreachable if !DEBUG */
+       __unreachable_ok_push
        if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {
                kprintf("ipc_kmsg_put header+body: %d\n", (size));
                uint32_t i;
@@ -1485,6 +1611,7 @@ ipc_kmsg_put(
                }
                kprintf("type: %d\n", ((mach_msg_type_descriptor_t *)(((mach_msg_base_t *)kmsg->ikm_header)+1))->type);
        }
+       __unreachable_ok_pop
        if (copyoutmsg((const char *) kmsg->ikm_header, msg_addr, size))
                mr = MACH_RCV_INVALID_DATA;
        else
@@ -1526,16 +1653,6 @@ ipc_kmsg_put_to_kernel(
  *             and the bits field is updated.  The destination port
  *             will be a valid port pointer.
  *
- *             The notify argument implements the MACH_SEND_CANCEL option.
- *             If it is not MACH_PORT_NULL, it should name a receive right.
- *             If the processing of the destination port would generate
- *             a port-deleted notification (because the right for the
- *             destination port is destroyed and it had a request for
- *             a dead-name notification registered), and the port-deleted
- *             notification would be sent to the named receive right,
- *             then it isn't sent and the send-once right for the notify
- *             port is quietly destroyed.
- *
  *     Conditions:
  *             Nothing locked.
  *     Returns:
@@ -1543,9 +1660,6 @@ ipc_kmsg_put_to_kernel(
  *             MACH_SEND_INVALID_HEADER
  *                     Illegal value in the message header bits.
  *             MACH_SEND_INVALID_DEST  The space is dead.
- *             MACH_SEND_INVALID_NOTIFY
- *                     Notify is non-null and doesn't name a receive right.
- *                     (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
  *             MACH_SEND_INVALID_DEST  Can't copyin destination port.
  *                     (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
  *             MACH_SEND_INVALID_REPLY Can't copyin reply port.
@@ -1554,21 +1668,36 @@ ipc_kmsg_put_to_kernel(
 
 mach_msg_return_t
 ipc_kmsg_copyin_header(
-       mach_msg_header_t       *msg,
+       ipc_kmsg_t              kmsg,
        ipc_space_t             space,
-       mach_port_name_t        notify)
+       mach_msg_option_t       *optionp)
 {
+       mach_msg_header_t *msg = kmsg->ikm_header;
        mach_msg_bits_t mbits = msg->msgh_bits & MACH_MSGH_BITS_USER;
        mach_port_name_t dest_name = CAST_MACH_PORT_TO_NAME(msg->msgh_remote_port);
        mach_port_name_t reply_name = CAST_MACH_PORT_TO_NAME(msg->msgh_local_port);
+       mach_port_name_t voucher_name = MACH_PORT_NULL;
        kern_return_t kr;
 
        mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
        mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
-       ipc_object_t dest_port, reply_port;
-       ipc_port_t dest_soright, reply_soright;
-       ipc_port_t notify_port;
-       ipc_entry_t entry;
+       mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
+       ipc_object_t dest_port = IO_NULL;
+       ipc_object_t reply_port = IO_NULL;
+       ipc_port_t dest_soright = IP_NULL;
+       ipc_port_t reply_soright = IP_NULL;
+       ipc_port_t voucher_soright = IP_NULL;
+       ipc_port_t release_port = IP_NULL;
+       ipc_port_t voucher_port = IP_NULL;
+       ipc_port_t voucher_release_port = IP_NULL;
+       ipc_entry_t dest_entry = IE_NULL;
+       ipc_entry_t reply_entry = IE_NULL;
+       ipc_entry_t voucher_entry = IE_NULL;
+
+       int assertcnt = 0;
+#if IMPORTANCE_INHERITANCE
+       boolean_t needboost = FALSE;
+#endif /* IMPORTANCE_INHERITANCE */
 
        if ((mbits != msg->msgh_bits) ||
            (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type)) ||
@@ -1577,353 +1706,381 @@ ipc_kmsg_copyin_header(
             !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type)))
                return MACH_SEND_INVALID_HEADER;
 
-       reply_soright = IP_NULL; /* in case we go to invalid dest early */
+       if (!MACH_PORT_VALID(dest_name))
+               return MACH_SEND_INVALID_DEST;
 
        is_write_lock(space);
-       if (!space->is_active)
-               goto invalid_dest;
-
-       if (!MACH_PORT_VALID(dest_name))
-               goto invalid_dest;
+       if (!is_active(space)) {
+               is_write_unlock(space);
+               return MACH_SEND_INVALID_DEST;
+       }
+       /* space locked and active */
 
-#if CONFIG_MACF_MACH
        /*
-        * We do the port send check here instead of in ipc_kmsg_send()
-        * because copying the header involves copying the port rights too
-        * and we need to do the send check before anything is actually copied.
+        *      If there is a voucher specified, make sure the disposition is
+        *      valid and the entry actually refers to a voucher port.  Don't
+        *      actually copy in until we validate destination and reply.
         */
-       entry = ipc_entry_lookup(space, dest_name);
-       if (entry != IE_NULL) {
-               int error = 0;
-               ipc_port_t port = (ipc_port_t) entry->ie_object;
-               if (port == IP_NULL)
-                       goto invalid_dest;
-               ip_lock(port);
-               if (ip_active(port)) {
-                       task_t self = current_task();
-                       tasklabel_lock(self);
-                       error = mac_port_check_send(&self->maclabel,
-                           &port->ip_label);
-                       tasklabel_unlock(self);
-               }
-               ip_unlock(port);
-               if (error != 0)
-                       goto invalid_dest;
-       }
-#endif
+       if (voucher_type != MACH_MSGH_BITS_ZERO) {
+
+               voucher_name = msg->msgh_voucher_port;
 
-       if (notify != MACH_PORT_NULL) {
-               if ((entry = ipc_entry_lookup(space, notify)) == IE_NULL) {
+               if (voucher_name == MACH_PORT_DEAD ||
+                   (voucher_type != MACH_MSG_TYPE_MOVE_SEND &&
+                    voucher_type != MACH_MSG_TYPE_COPY_SEND)) {
                        is_write_unlock(space);
-                       return MACH_SEND_INVALID_NOTIFY;
+                       return MACH_SEND_INVALID_VOUCHER;
                }
-               if((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0) {
-                       is_write_unlock(space);
-                       return MACH_SEND_INVALID_NOTIFY;
+
+               if (voucher_name != MACH_PORT_NULL) {
+                       voucher_entry = ipc_entry_lookup(space, voucher_name);
+                       if (voucher_entry == IE_NULL || 
+                           (voucher_entry->ie_bits & MACH_PORT_TYPE_SEND) == 0 ||
+                           io_kotype(voucher_entry->ie_object) != IKOT_VOUCHER) {
+                               is_write_unlock(space);
+                               return MACH_SEND_INVALID_VOUCHER;
+                       }
+               } else {
+                       voucher_type = MACH_MSG_TYPE_MOVE_SEND;
                }
+       }
 
-               notify_port = (ipc_port_t) entry->ie_object;
-       } else
-               notify_port = IP_NULL;
+       /*
+        *      Handle combinations of validating destination and reply; along
+        *      with copying in destination, reply, and voucher in an atomic way.
+        */
 
-       if (dest_name == reply_name) {
-               mach_port_name_t name = dest_name;
+       if (dest_name == voucher_name) {
 
                /*
-                *      Destination and reply ports are the same!
-                *      This is a little tedious to make atomic, because
-                *      there are 25 combinations of dest_type/reply_type.
-                *      However, most are easy.  If either is move-sonce,
-                *      then there must be an error.  If either are
-                *      make-send or make-sonce, then we must be looking
-                *      at a receive right so the port can't die.
-                *      The hard cases are the combinations of
-                *      copy-send and make-send.
+                *      If the destination name is the same as the voucher name,
+                *      the voucher_entry must already be known.  Either that or
+                *      the destination name is MACH_PORT_NULL (i.e. invalid).
                 */
-
-               entry = ipc_entry_lookup(space, name);
-               if (entry == IE_NULL)
+               dest_entry = voucher_entry;
+               if (dest_entry == IE_NULL) {
                        goto invalid_dest;
+               }
 
-               assert(reply_type != 0); /* because name not null */
-
-               if (!ipc_right_copyin_check(space, name, entry, reply_type))
-                       goto invalid_reply;
+               /*
+                *      Make sure a future copyin of the reply port will succeed.
+                *      Once we start copying in the dest/voucher pair, we can't
+                *      back out.
+                */
+               if (MACH_PORT_VALID(reply_name)) {
+                       assert(reply_type != 0); /* because reply_name not null */
 
-               if ((dest_type == MACH_MSG_TYPE_MOVE_SEND_ONCE) ||
-                   (reply_type == MACH_MSG_TYPE_MOVE_SEND_ONCE)) {
-                       /*
-                        *      Why must there be an error?  To get a valid
-                        *      destination, this entry must name a live
-                        *      port (not a dead name or dead port).  However
-                        *      a successful move-sonce will destroy a
-                        *      live entry.  Therefore the other copyin,
-                        *      whatever it is, would fail.  We've already
-                        *      checked for reply port errors above,
-                        *      so report a destination error.
-                        */
+                       /* It is just WRONG if dest, voucher, and reply are all the same. */
+                       if (voucher_name == reply_name) {
+                               goto invalid_reply;
+                       }
+                       reply_entry = ipc_entry_lookup(space, reply_name);
+                       if (reply_entry == IE_NULL) {
+                               goto invalid_reply;
+                       }
+                       assert(dest_entry != reply_entry); /* names are not equal */
+                       if (!ipc_right_copyin_check(space, reply_name, reply_entry, reply_type)) {
+                               goto invalid_reply;
+                       }
+               }
 
+               /* 
+                *      Do the joint copyin of the dest disposition and 
+                *      voucher disposition from the one entry/port.  We
+                *      already validated that the voucher copyin would
+                *      succeed (above).  So, any failure in combining
+                *      the copyins can be blamed on the destination.
+                */
+               kr = ipc_right_copyin_two(space, dest_name, dest_entry,
+                                         dest_type, voucher_type,
+                                         &dest_port, &dest_soright,
+                                         &release_port);
+               if (kr != KERN_SUCCESS) {
+                       assert(kr != KERN_INVALID_CAPABILITY);
                        goto invalid_dest;
-               } else if ((dest_type == MACH_MSG_TYPE_MAKE_SEND) ||
-                          (dest_type == MACH_MSG_TYPE_MAKE_SEND_ONCE) ||
-                          (reply_type == MACH_MSG_TYPE_MAKE_SEND) ||
-                          (reply_type == MACH_MSG_TYPE_MAKE_SEND_ONCE)) {
-                       kr = ipc_right_copyin(space, name, entry,
-                                             dest_type, FALSE,
-                                             &dest_port, &dest_soright);
-                       if (kr != KERN_SUCCESS)
-                               goto invalid_dest;
-
-                       /*
-                        *      Either dest or reply needs a receive right.
-                        *      We know the receive right is there, because
-                        *      of the copyin_check and copyin calls.  Hence
-                        *      the port is not in danger of dying.  If dest
-                        *      used the receive right, then the right needed
-                        *      by reply (and verified by copyin_check) will
-                        *      still be there.
-                        */
-
-                       assert(IO_VALID(dest_port));
-                       assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE);
-                       assert(dest_soright == IP_NULL);
+               }
+               voucher_port = (ipc_port_t)dest_port;
 
-                       kr = ipc_right_copyin(space, name, entry,
+               /* 
+                * could not have been one of these dispositions, 
+                * validated the port was a true kernel voucher port above,
+                * AND was successfully able to copyin both dest and voucher.
+                */
+               assert(dest_type != MACH_MSG_TYPE_MAKE_SEND);
+               assert(dest_type != MACH_MSG_TYPE_MAKE_SEND_ONCE);
+               assert(dest_type != MACH_MSG_TYPE_MOVE_SEND_ONCE);
+               
+               /*
+                *      Perform the delayed reply right copyin (guaranteed success).
+                */
+               if (reply_entry != IE_NULL) {
+                       kr = ipc_right_copyin(space, reply_name, reply_entry,
                                              reply_type, TRUE,
-                                             &reply_port, &reply_soright);
-
+                                             &reply_port, &reply_soright,
+                                             &release_port, &assertcnt);
+                       assert(assertcnt == 0);
                        assert(kr == KERN_SUCCESS);
-                       assert(reply_port == dest_port);
-                       assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE);
-                       assert(reply_soright == IP_NULL);
-               } else if ((dest_type == MACH_MSG_TYPE_COPY_SEND) &&
-                          (reply_type == MACH_MSG_TYPE_COPY_SEND)) {
-                       /*
-                        *      To make this atomic, just do one copy-send,
-                        *      and dup the send right we get out.
-                        */
-
-                       kr = ipc_right_copyin(space, name, entry,
-                                             dest_type, FALSE,
-                                             &dest_port, &dest_soright);
-                       if (kr != KERN_SUCCESS)
-                               goto invalid_dest;
-
-                       assert(entry->ie_bits & MACH_PORT_TYPE_SEND);
-                       assert(dest_soright == IP_NULL);
+               }
 
+       } else {
+               if (dest_name == reply_name) {
                        /*
-                        *      It's OK if the port we got is dead now,
-                        *      so reply_port is IP_DEAD, because the msg
-                        *      won't go anywhere anyway.
+                        *      Destination and reply ports are the same!
+                        *      This is very similar to the case where the
+                        *      destination and voucher ports were the same
+                        *      (except the reply port disposition is not
+                        *      previously validated).
                         */
+                       dest_entry = ipc_entry_lookup(space, dest_name);
+                       if (dest_entry == IE_NULL) {
+                               goto invalid_dest;
+                       }
+                       reply_entry = dest_entry;
+                       assert(reply_type != 0); /* because name not null */
 
-                       reply_port = (ipc_object_t)
-                               ipc_port_copy_send((ipc_port_t) dest_port);
-                       reply_soright = IP_NULL;
-               } else if ((dest_type == MACH_MSG_TYPE_MOVE_SEND) &&
-                          (reply_type == MACH_MSG_TYPE_MOVE_SEND)) {
-                       /*
-                        *      This is an easy case.  Just use our
-                        *      handy-dandy special-purpose copyin call
-                        *      to get two send rights for the price of one.
+                       /* 
+                        *      Do the joint copyin of the dest disposition and 
+                        *      reply disposition from the one entry/port.
                         */
-
-                       kr = ipc_right_copyin_two(space, name, entry,
-                                                 &dest_port, &dest_soright);
-                       if (kr != KERN_SUCCESS)
+                       kr = ipc_right_copyin_two(space, dest_name, dest_entry,
+                                                 dest_type, reply_type,
+                                                 &dest_port, &dest_soright,
+                                                 &release_port);
+                       if (kr == KERN_INVALID_CAPABILITY) {
+                               goto invalid_reply;
+                       } else if (kr != KERN_SUCCESS) {
                                goto invalid_dest;
-
-                       /* the entry might need to be deallocated */
-                       if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE)
-                               ipc_entry_dealloc(space, name, entry);
-
+                       }
                        reply_port = dest_port;
-                       reply_soright = IP_NULL;
-               } else {
-                       ipc_port_t soright;
 
-                       assert(((dest_type == MACH_MSG_TYPE_COPY_SEND) &&
-                               (reply_type == MACH_MSG_TYPE_MOVE_SEND)) ||
-                              ((dest_type == MACH_MSG_TYPE_MOVE_SEND) &&
-                               (reply_type == MACH_MSG_TYPE_COPY_SEND)));
 
+               } else {
                        /*
-                        *      To make this atomic, just do a move-send,
-                        *      and dup the send right we get out.
+                        *      Handle destination and reply independently, as
+                        *      they are independent entries (even if the entries
+                        *      refer to the same port).
+                        *
+                        *      This can be the tough case to make atomic.
+                        *
+                        *      The difficult problem is serializing with port death.
+                        *      The bad case is when dest_port dies after its copyin,
+                        *      reply_port dies before its copyin, and dest_port dies before
+                        *      reply_port.  Then the copyins operated as if dest_port was
+                        *      alive and reply_port was dead, which shouldn't have happened
+                        *      because they died in the other order.
+                        *
+                        *      Note that it is easy for a user task to tell if
+                        *      a copyin happened before or after a port died.
+                        *      If a port dies before copyin, a dead-name notification
+                        *      is generated and the dead name's urefs are incremented,
+                        *      and if the copyin happens first, a port-deleted
+                        *      notification is generated.
+                        *
+                        *      Even so, avoiding that potentially detectable race is too
+                        *      expensive - and no known code cares about it.  So, we just
+                        *      do the expedient thing and copy them in one after the other.
                         */
 
-                       kr = ipc_right_copyin(space, name, entry,
-                                             MACH_MSG_TYPE_MOVE_SEND, FALSE,
-                                             &dest_port, &soright);
-                       if (kr != KERN_SUCCESS)
+                       dest_entry = ipc_entry_lookup(space, dest_name);
+                       if (dest_entry == IE_NULL) {
                                goto invalid_dest;
+                       }
+                       assert(dest_entry != voucher_entry);
 
-                       /* the entry might need to be deallocated */
+                       /*
+                        *      Make sure reply port entry is valid before dest copyin.
+                        */
+                       if (MACH_PORT_VALID(reply_name)) {
+                               if (reply_name == voucher_name) {
+                                       goto invalid_reply;
+                               }
+                               reply_entry = ipc_entry_lookup(space, reply_name);
+                               if (reply_entry == IE_NULL) {
+                                       goto invalid_reply;
+                               }
+                               assert(dest_entry != reply_entry); /* names are not equal */
+                               assert(reply_type != 0); /* because reply_name not null */
 
-                       if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE)
-                               ipc_entry_dealloc(space, name, entry);
+                               if (!ipc_right_copyin_check(space, reply_name, reply_entry, reply_type)) {
+                                       goto invalid_reply;
+                               }
+                       }
 
                        /*
-                        *      It's OK if the port we got is dead now,
-                        *      so reply_port is IP_DEAD, because the msg
-                        *      won't go anywhere anyway.
+                        *      copyin the destination.
                         */
+                       kr = ipc_right_copyin(space, dest_name, dest_entry,
+                                             dest_type, FALSE,
+                                             &dest_port, &dest_soright,
+                                             &release_port, &assertcnt);
+                       assert(assertcnt == 0);
+                       if (kr != KERN_SUCCESS) {
+                               goto invalid_dest;
+                       }
+                       assert(IO_VALID(dest_port));
+                       assert(!IP_VALID(release_port));
 
-                       reply_port = (ipc_object_t)
-                               ipc_port_copy_send((ipc_port_t) dest_port);
-
-                       if (dest_type == MACH_MSG_TYPE_MOVE_SEND) {
-                               dest_soright = soright;
-                               reply_soright = IP_NULL;
+                       /*
+                        *      Copyin the pre-validated reply right.
+                        *      It's OK if the reply right has gone dead in the meantime.
+                        */
+                       if (MACH_PORT_VALID(reply_name)) {
+                               kr = ipc_right_copyin(space, reply_name, reply_entry,
+                                                     reply_type, TRUE,
+                                                     &reply_port, &reply_soright,
+                                                     &release_port, &assertcnt);
+                               assert(assertcnt == 0);
+                               assert(kr == KERN_SUCCESS);
                        } else {
-                               dest_soright = IP_NULL;
-                               reply_soright = soright;
+                               /* convert invalid name to equivalent ipc_object type */
+                               reply_port = (ipc_object_t)CAST_MACH_NAME_TO_PORT(reply_name);
                        }
                }
-       } else if (!MACH_PORT_VALID(reply_name)) {
-               /*
-                *      No reply port!  This is an easy case
-                *      to make atomic.  Just copyin the destination.
-                */
-
-               entry = ipc_entry_lookup(space, dest_name);
-               if (entry == IE_NULL)
-                       goto invalid_dest;
-
-               kr = ipc_right_copyin(space, dest_name, entry,
-                                     dest_type, FALSE,
-                                     &dest_port, &dest_soright);
-               if (kr != KERN_SUCCESS)
-                       goto invalid_dest;
-
-               /* the entry might need to be deallocated */
-
-               if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE)
-                       ipc_entry_dealloc(space, dest_name, entry);
-
-               reply_port = (ipc_object_t)CAST_MACH_NAME_TO_PORT(reply_name);
-               reply_soright = IP_NULL;
-       } else {
-               ipc_entry_t dest_entry, reply_entry;
 
                /*
-                *      This is the tough case to make atomic.
-                *      The difficult problem is serializing with port death.
-                *      At the time we copyin dest_port, it must be alive.
-                *      If reply_port is alive when we copyin it, then
-                *      we are OK, because we serialize before the death
-                *      of both ports.  Assume reply_port is dead at copyin.
-                *      Then if dest_port dies/died after reply_port died,
-                *      we are OK, because we serialize between the death
-                *      of the two ports.  So the bad case is when dest_port
-                *      dies after its copyin, reply_port dies before its
-                *      copyin, and dest_port dies before reply_port.  Then
-                *      the copyins operated as if dest_port was alive
-                *      and reply_port was dead, which shouldn't have happened
-                *      because they died in the other order.
-                *
-                *      Note that it is easy for a user task to tell if
-                *      a copyin happened before or after a port died.
-                *      For example, suppose both dest and reply are
-                *      send-once rights (types are both move-sonce) and
-                *      both rights have dead-name requests registered.
-                *      If a port dies before copyin, a dead-name notification
-                *      is generated and the dead name's urefs are incremented,
-                *      and if the copyin happens first, a port-deleted
-                *      notification is generated.
-                *
-                *      Note that although the entries are different,
-                *      dest_port and reply_port might still be the same.
-                *
-                * JMM - The code to handle this was too expensive and, anyway,
-                * we intend to separate the dest lookup from the reply copyin
-                * by a wide margin, so the user will have to learn to deal!
-                * I will be making the change soon!
+                * Finally can copyin the voucher right now that dest and reply
+                * are fully copied in (guaranteed success).
                 */
+               if (IE_NULL != voucher_entry) {
+                       kr = ipc_right_copyin(space, voucher_name, voucher_entry,
+                                             voucher_type, FALSE,
+                                             (ipc_object_t *)&voucher_port,
+                                             &voucher_soright,
+                                             &voucher_release_port,
+                                             &assertcnt);
+                       assert(assertcnt == 0);
+                       assert(KERN_SUCCESS == kr);
+                       assert(IP_VALID(voucher_port));
+                       assert(ip_active(voucher_port));
+               }
+       }
 
-               dest_entry = ipc_entry_lookup(space, dest_name);
-               if (dest_entry == IE_NULL)
-                       goto invalid_dest;
-
-               reply_entry = ipc_entry_lookup(space, reply_name);
-               if (reply_entry == IE_NULL)
-                       goto invalid_reply;
-
-               assert(dest_entry != reply_entry); /* names are not equal */
-               assert(reply_type != 0); /* because reply_name not null */
-
-               if (!ipc_right_copyin_check(space, reply_name, reply_entry,
-                                           reply_type))
-                       goto invalid_reply;
-
-               kr = ipc_right_copyin(space, dest_name, dest_entry,
-                                     dest_type, FALSE,
-                                     &dest_port, &dest_soright);
-               if (kr != KERN_SUCCESS)
-                       goto invalid_dest;
-
-               assert(IO_VALID(dest_port));
-
-               kr = ipc_right_copyin(space, reply_name, reply_entry,
-                                     reply_type, TRUE,
-                                     &reply_port, &reply_soright);
-
-               assert(kr == KERN_SUCCESS);
-
-               /* the entries might need to be deallocated */
-
-               if (IE_BITS_TYPE(reply_entry->ie_bits) == MACH_PORT_TYPE_NONE)
-                       ipc_entry_dealloc(space, reply_name, reply_entry);
-
-               if (IE_BITS_TYPE(dest_entry->ie_bits) == MACH_PORT_TYPE_NONE)
-                       ipc_entry_dealloc(space, dest_name, dest_entry);
+       /* the entry(s) might need to be deallocated */
+       assert(IE_NULL != dest_entry);
+       if (IE_BITS_TYPE(dest_entry->ie_bits) == MACH_PORT_TYPE_NONE) {
+               ipc_entry_dealloc(space, dest_name, dest_entry);
+               dest_entry = IE_NULL;
+       }
+       if (dest_entry != reply_entry && IE_NULL != reply_entry &&
+           IE_BITS_TYPE(reply_entry->ie_bits) == MACH_PORT_TYPE_NONE) {
+               ipc_entry_dealloc(space, reply_name, reply_entry);
+               reply_entry = IE_NULL;
+       }
+       if (dest_entry != voucher_entry && IE_NULL != voucher_entry &&
+           IE_BITS_TYPE(voucher_entry->ie_bits) == MACH_PORT_TYPE_NONE) {
+               ipc_entry_dealloc(space, voucher_name, voucher_entry);
+               voucher_entry = IE_NULL;
        }
 
        /*
-        *      At this point, dest_port, reply_port,
-        *      dest_soright, reply_soright are all initialized.
-        *      Any defunct entries have been deallocated.
-        *      The space is still write-locked, and we need to
-        *      make the MACH_SEND_CANCEL check.  The notify_port pointer
-        *      is still usable, because the copyin code above won't ever
-        *      deallocate a receive right, so its entry still exists
-        *      and holds a ref.  Note notify_port might even equal
-        *      dest_port or reply_port.
+        * No room to store voucher port in in-kernel msg header,
+        * so we store it back in the kmsg itself.
         */
-
-       if ((notify != MACH_PORT_NULL) &&
-           (dest_soright == notify_port)) {
-               ipc_port_release_sonce(dest_soright);
-               dest_soright = IP_NULL;
+       if (IP_VALID(voucher_port)) {
+               assert(ip_active(voucher_port));
+               kmsg->ikm_voucher = voucher_port;
+               voucher_type = MACH_MSG_TYPE_MOVE_SEND;
        }
 
-       is_write_unlock(space);
-
-       if (dest_soright != IP_NULL)
-               ipc_notify_port_deleted(dest_soright, dest_name);
-
-       if (reply_soright != IP_NULL)
-               ipc_notify_port_deleted(reply_soright, reply_name);
-
        dest_type = ipc_object_copyin_type(dest_type);
        reply_type = ipc_object_copyin_type(reply_type);
 
-       msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
-                         MACH_MSGH_BITS(dest_type, reply_type));
-       msg->msgh_remote_port = (ipc_port_t)dest_port;
-       msg->msgh_local_port = (ipc_port_t)reply_port;
-
-       return MACH_MSG_SUCCESS;
+       /*
+        * JMM - Without rdar://problem/6275821, this is the last place we can
+        * re-arm the send-possible notifications.  It may trigger unexpectedly
+        * early (send may NOT have failed), but better than missing.  We assure
+        * we won't miss by forcing MACH_SEND_ALWAYS if we got past arming.
+        */
+       if (((*optionp & MACH_SEND_NOTIFY) != 0) && 
+           dest_type != MACH_MSG_TYPE_PORT_SEND_ONCE &&
+           dest_entry != IE_NULL && dest_entry->ie_request != IE_REQ_NONE) {
+               ipc_port_t dport = (ipc_port_t)dest_port;
+
+               assert(dport != IP_NULL);
+               ip_lock(dport);
+               if (ip_active(dport) && dport->ip_receiver != ipc_space_kernel) {
+                       if (ip_full(dport)) {
+#if IMPORTANCE_INHERITANCE
+                               needboost = ipc_port_request_sparm(dport, dest_name, 
+                                                       dest_entry->ie_request,
+                                                       (*optionp & MACH_SEND_NOIMPORTANCE));
+                               if (needboost == FALSE)
+                                       ip_unlock(dport);
+#else
+                               ipc_port_request_sparm(dport, dest_name, dest_entry->ie_request);
+                               ip_unlock(dport);
+#endif /* IMPORTANCE_INHERITANCE */
+                       } else {
+                               *optionp |= MACH_SEND_ALWAYS;
+                               ip_unlock(dport);
+                       }
+               } else {
+                       ip_unlock(dport);
+               }
+       }
+
+       is_write_unlock(space);
+
+#if IMPORTANCE_INHERITANCE
+       /* 
+        * If our request is the first boosting send-possible
+        * notification this cycle, push the boost down the
+        * destination port.
+        */
+       if (needboost == TRUE) {
+               ipc_port_t dport = (ipc_port_t)dest_port;
+
+               /* dport still locked from above */
+               if (ipc_port_importance_delta(dport, IPID_OPTION_SENDPOSSIBLE, 1) == FALSE) {
+                       ip_unlock(dport);
+               }
+       }
+#endif /* IMPORTANCE_INHERITANCE */
+
+       if (dest_soright != IP_NULL) {
+               ipc_notify_port_deleted(dest_soright, dest_name);
+       }
+       if (reply_soright != IP_NULL) {
+               ipc_notify_port_deleted(reply_soright, reply_name);
+       }
+       if (voucher_soright != IP_NULL) {
+               ipc_notify_port_deleted(voucher_soright, voucher_name);
+       }
+       msg->msgh_bits = MACH_MSGH_BITS_SET(dest_type, reply_type, voucher_type, mbits);
+       msg->msgh_remote_port = (ipc_port_t)dest_port;
+       msg->msgh_local_port = (ipc_port_t)reply_port;
+
+       if (release_port != IP_NULL)
+               ip_release(release_port);
+
+       if (voucher_release_port != IP_NULL)
+               ip_release(voucher_release_port);
+
+       return MACH_MSG_SUCCESS;
 
 invalid_reply:
        is_write_unlock(space);
+
+       if (release_port != IP_NULL)
+               ip_release(release_port);
+
+       assert(voucher_port == IP_NULL);
+       assert(voucher_soright == IP_NULL);
+
        return MACH_SEND_INVALID_REPLY;
 
 invalid_dest:
        is_write_unlock(space);
+
+       if (release_port != IP_NULL)
+               ip_release(release_port);
+
        if (reply_soright != IP_NULL)
                ipc_notify_port_deleted(reply_soright, reply_name);
+
+       assert(voucher_port == IP_NULL);
+       assert(voucher_soright == IP_NULL);
+
        return MACH_SEND_INVALID_DEST;
 }
 
@@ -2296,6 +2453,8 @@ ipc_kmsg_copyin_body(
      */
     daddr = NULL;
     for (i = 0; i < dsc_count; i++) {
+       mach_msg_size_t size;
+
        daddr = naddr;
 
        /* make sure the descriptor fits in the message */
@@ -2325,8 +2484,6 @@ ipc_kmsg_copyin_body(
        }
 
        switch (daddr->type.type) {
-           mach_msg_size_t size;
-
        case MACH_MSG_OOL_DESCRIPTOR:
        case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
         size = (is_task_64bit) ?
@@ -2351,6 +2508,13 @@ ipc_kmsg_copyin_body(
                 * Out-of-line memory descriptor, accumulate kernel
                 * memory requirements
                 */
+               if (space_needed + round_page(size) <= space_needed) {
+                   /* Overflow dectected */
+                   ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
+                   mr = MACH_MSG_VM_KERNEL;
+                   goto out;
+               }                   
+                   
                space_needed += round_page(size);
                if (space_needed > ipc_kmsg_max_vm_space) {
                    
@@ -2372,7 +2536,7 @@ ipc_kmsg_copyin_body(
      */
     if (space_needed) {
         if (vm_allocate(ipc_kernel_copy_map, &paddr, space_needed, 
-                    VM_FLAGS_ANYWHERE) != KERN_SUCCESS) {
+                    VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_KERN_MEMORY_IPC)) != KERN_SUCCESS) {
             ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
             mr = MACH_MSG_VM_KERNEL;
             goto out;
@@ -2381,11 +2545,14 @@ ipc_kmsg_copyin_body(
 
     /* user_addr = just after base as it was copied in */
     user_addr = (mach_msg_descriptor_t *)((vm_offset_t)kmsg->ikm_header + sizeof(mach_msg_base_t));
-    /* Shift the mach_msg_base_t down to make for dsc_count*16bytes of descriptors */
+
+    /* Shift the mach_msg_base_t down to make room for dsc_count*16bytes of descriptors */
     if(descriptor_size != 16*dsc_count) {
         vm_offset_t dsc_adjust = 16*dsc_count - descriptor_size;
+
         memmove((char *)(((vm_offset_t)kmsg->ikm_header) - dsc_adjust), kmsg->ikm_header, sizeof(mach_msg_base_t));
         kmsg->ikm_header = (mach_msg_header_t *)((vm_offset_t)kmsg->ikm_header - dsc_adjust);
+
         /* Update the message size for the larger in-kernel representation */
         kmsg->ikm_header->msgh_size += (mach_msg_size_t)dsc_adjust;
     }
@@ -2455,7 +2622,6 @@ ipc_kmsg_copyin_body(
  *             MACH_MSG_SUCCESS        Successful copyin.
  *             MACH_SEND_INVALID_HEADER
  *                     Illegal value in the message header bits.
- *             MACH_SEND_INVALID_NOTIFY        Bad notify port.
  *             MACH_SEND_INVALID_DEST  Can't copyin destination port.
  *             MACH_SEND_INVALID_REPLY Can't copyin reply port.
  *             MACH_SEND_INVALID_MEMORY        Can't grab out-of-line memory.
@@ -2469,27 +2635,39 @@ ipc_kmsg_copyin(
        ipc_kmsg_t              kmsg,
        ipc_space_t             space,
        vm_map_t                map,
-       mach_port_name_t        notify)
+       mach_msg_option_t       *optionp)
 {
     mach_msg_return_t          mr;
-    
-    mr = ipc_kmsg_copyin_header(kmsg->ikm_header, space, notify);
+
+    kmsg->ikm_header->msgh_bits &= MACH_MSGH_BITS_USER;
+
+    mr = ipc_kmsg_copyin_header(kmsg, space, optionp);
+
     if (mr != MACH_MSG_SUCCESS)
        return mr;
-    
-       DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_copyin header:\n%.8x\n%.8x\n%p\n%p\n%.8x\n%.8x\n",
-                                                        kmsg->ikm_header->msgh_size,
-                                                        kmsg->ikm_header->msgh_bits,
-                                                        kmsg->ikm_header->msgh_remote_port,
-                                                        kmsg->ikm_header->msgh_local_port,
-                                                        kmsg->ikm_header->msgh_reserved,
-                                                        kmsg->ikm_header->msgh_id);
+
+    KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_MSG_SEND) | DBG_FUNC_NONE,
+                         VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
+                         (uintptr_t)kmsg->ikm_header->msgh_bits,
+                         (uintptr_t)kmsg->ikm_header->msgh_id,
+                         VM_KERNEL_ADDRPERM((uintptr_t)unsafe_convert_port_to_voucher(kmsg->ikm_voucher)),
+                         0);
+
+    DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_copyin header:\n%.8x\n%.8x\n%p\n%p\n%p\n%.8x\n",
+                            kmsg->ikm_header->msgh_size,
+                            kmsg->ikm_header->msgh_bits,
+                            kmsg->ikm_header->msgh_remote_port,
+                            kmsg->ikm_header->msgh_local_port,
+                            kmsg->ikm_voucher,
+                            kmsg->ikm_header->msgh_id);
 
     if ((kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) == 0)
        return MACH_MSG_SUCCESS;
     
        mr = ipc_kmsg_copyin_body( kmsg, space, map);
 
+       /* unreachable if !DEBUG */
+       __unreachable_ok_push
        if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK))
        {
                kprintf("body:\n");
@@ -2499,6 +2677,8 @@ ipc_kmsg_copyin(
                        kprintf("%.4x\n",((uint32_t *)(kmsg->ikm_header + 1))[i]);
                }
        }
+       __unreachable_ok_pop
+
        return mr;
 }
 
@@ -2511,14 +2691,11 @@ ipc_kmsg_copyin(
  *             Because the message comes from the kernel,
  *             the implementation assumes there are no errors
  *             or peculiarities in the message.
- *
- *             Returns TRUE if queueing the message
- *             would result in a circularity.
  *     Conditions:
  *             Nothing locked.
  */
 
-void
+mach_msg_return_t
 ipc_kmsg_copyin_from_kernel(
        ipc_kmsg_t      kmsg)
 {
@@ -2529,6 +2706,8 @@ ipc_kmsg_copyin_from_kernel(
        ipc_object_t local = (ipc_object_t) kmsg->ikm_header->msgh_local_port;
 
        /* translate the destination and reply ports */
+       if (!IO_VALID(remote))
+               return MACH_SEND_INVALID_DEST;
 
        ipc_object_copyin_from_kernel(remote, rname);
        if (IO_VALID(local))
@@ -2552,7 +2731,7 @@ ipc_kmsg_copyin_from_kernel(
 
                kmsg->ikm_header->msgh_bits = bits;
                if ((bits & MACH_MSGH_BITS_COMPLEX) == 0)
-                       return;
+                       return MACH_MSG_SUCCESS;
        }
     {
        mach_msg_descriptor_t   *saddr;
@@ -2646,10 +2825,11 @@ ipc_kmsg_copyin_from_kernel(
            }
        }
     }
+    return MACH_MSG_SUCCESS;
 }
 
 #if IKM_SUPPORT_LEGACY
-void
+mach_msg_return_t
 ipc_kmsg_copyin_from_kernel_legacy(
        ipc_kmsg_t      kmsg)
 {
@@ -2660,6 +2840,8 @@ ipc_kmsg_copyin_from_kernel_legacy(
        ipc_object_t local = (ipc_object_t) kmsg->ikm_header->msgh_local_port;
 
        /* translate the destination and reply ports */
+       if (!IO_VALID(remote))
+               return MACH_SEND_INVALID_DEST;
 
        ipc_object_copyin_from_kernel(remote, rname);
        if (IO_VALID(local))
@@ -2683,7 +2865,7 @@ ipc_kmsg_copyin_from_kernel_legacy(
 
                kmsg->ikm_header->msgh_bits = bits;
                if ((bits & MACH_MSGH_BITS_COMPLEX) == 0)
-                       return;
+                       return MACH_MSG_SUCCESS;
        }
     {
        mach_msg_legacy_descriptor_t    *saddr;
@@ -2816,6 +2998,7 @@ ipc_kmsg_copyin_from_kernel_legacy(
            }
        }
     }
+    return MACH_MSG_SUCCESS;
 }
 #endif /* IKM_SUPPORT_LEGACY */
 
@@ -2828,13 +3011,6 @@ ipc_kmsg_copyin_from_kernel_legacy(
  *             If it does succeed the remote/local port fields
  *             contain port names instead of object pointers,
  *             and the bits field is updated.
- *
- *             The notify argument implements the MACH_RCV_NOTIFY option.
- *             If it is not MACH_PORT_NULL, it should name a receive right.
- *             If the process of receiving the reply port creates a
- *             new right in the receiving task, then the new right is
- *             automatically registered for a dead-name notification,
- *             with the notify port supplying the send-once right.
  *     Conditions:
  *             Nothing locked.
  *     Returns:
@@ -2854,228 +3030,198 @@ ipc_kmsg_copyin_from_kernel_legacy(
 
 mach_msg_return_t
 ipc_kmsg_copyout_header(
-       mach_msg_header_t       *msg,
+       ipc_kmsg_t              kmsg,
        ipc_space_t             space,
-       mach_port_name_t        notify)
+       mach_msg_option_t       option)
 {
+       mach_msg_header_t *msg = kmsg->ikm_header;
        mach_msg_bits_t mbits = msg->msgh_bits;
        ipc_port_t dest = (ipc_port_t) msg->msgh_remote_port;
 
        assert(IP_VALID(dest));
 
+       /*
+        * While we still hold a reference on the received-from port,
+        * process all send-possible notfications we received along with
+        * the message.
+        */
+       ipc_port_spnotify(dest);
+
     {
        mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
        mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
-       ipc_port_t reply = (ipc_port_t) msg->msgh_local_port;
+       mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
+       ipc_port_t reply = msg->msgh_local_port;
+       ipc_port_t release_reply_port = IP_NULL;
        mach_port_name_t dest_name, reply_name;
 
+       ipc_port_t voucher = kmsg->ikm_voucher;
+       ipc_port_t release_voucher_port = IP_NULL;
+       mach_port_name_t voucher_name;
+
+       uint32_t entries_held = 0;
+       boolean_t need_write_lock = FALSE;
+       kern_return_t kr;
+
+       /*
+        * Reserve any potentially needed entries in the target space.
+        * We'll free any unused before unlocking the space.
+        */
        if (IP_VALID(reply)) {
-               ipc_port_t notify_port;
-               ipc_entry_t entry;
-               kern_return_t kr;
+               entries_held++;
+               need_write_lock = TRUE;
+       }
+       if (IP_VALID(voucher)) {
+               assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND); 
 
-               /*
-                *      Handling notify (for MACH_RCV_NOTIFY) is tricky.
-                *      The problem is atomically making a send-once right
-                *      from the notify port and installing it for a
-                *      dead-name request in the new entry, because this
-                *      requires two port locks (on the notify port and
-                *      the reply port).  However, we can safely make
-                *      and consume send-once rights for the notify port
-                *      as long as we hold the space locked.  This isn't
-                *      an atomicity problem, because the only way
-                *      to detect that a send-once right has been created
-                *      and then consumed if it wasn't needed is by getting
-                *      at the receive right to look at ip_sorights, and
-                *      because the space is write-locked status calls can't
-                *      lookup the notify port receive right.  When we make
-                *      the send-once right, we lock the notify port,
-                *      so any status calls in progress will be done.
-                */
+               if ((option & MACH_RCV_VOUCHER) != 0)
+                       entries_held++;
+               need_write_lock = TRUE;
+       }
 
-               is_write_lock(space);
+       if (need_write_lock) {
 
-               for (;;) {
-                       ipc_port_request_index_t request;
+               is_write_lock(space);
 
-                       if (!space->is_active) {
+               while(entries_held) {
+                       if (!is_active(space)) {
                                is_write_unlock(space);
                                return (MACH_RCV_HEADER_ERROR|
                                        MACH_MSG_IPC_SPACE);
                        }
-
-                       if (notify != MACH_PORT_NULL) {
-                               notify_port = ipc_port_lookup_notify(space,
-                                                                    notify);
-                               if (notify_port == IP_NULL) {
-                                       printf("ipc_kmsg_copyout_header: no notify port\n");
-                                       is_write_unlock(space);
-                                       return MACH_RCV_INVALID_NOTIFY;
-                               }
-                       } else
-                               notify_port = IP_NULL;
-
-                       if ((reply_type != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
-                           ipc_right_reverse(space, (ipc_object_t) reply,
-                                             &reply_name, &entry)) {
-                               /* reply port is locked and active */
-
-                               /*
-                                *      We don't need the notify_port
-                                *      send-once right, but we can't release
-                                *      it here because reply port is locked.
-                                *      Wait until after the copyout to
-                                *      release the notify port right.
-                                */
-
-                               assert(entry->ie_bits &
-                                      MACH_PORT_TYPE_SEND_RECEIVE);
+                               
+                       kr = ipc_entries_hold(space, entries_held);
+                       if (KERN_SUCCESS == kr)
                                break;
-                       }
-
-                       ip_lock(reply);
-                       if (!ip_active(reply)) {
-                               ip_release(reply);
-                               ip_check_unlock(reply);
 
-                               if (notify_port != IP_NULL)
-                                       ipc_port_release_sonce(notify_port);
-
-                               ip_lock(dest);
-                               is_write_unlock(space);
+                       kr = ipc_entry_grow_table(space, ITS_SIZE_NONE);
+                       if (KERN_SUCCESS != kr)
+                               return(MACH_RCV_HEADER_ERROR|
+                                      MACH_MSG_IPC_SPACE);
+                       /* space was unlocked and relocked - retry */
+               }
 
-                               reply = IP_DEAD;
-                               reply_name = MACH_PORT_DEAD;
-                               goto copyout_dest;
-                       }
+               /* Handle reply port. */
+               if (IP_VALID(reply)) {
+                       ipc_entry_t entry;
 
-                       reply_name = CAST_MACH_PORT_TO_NAME(reply);
-                       kr = ipc_entry_get(space, &reply_name, &entry);
-                       if (kr != KERN_SUCCESS) {
-                               ip_unlock(reply);
-
-                               if (notify_port != IP_NULL)
-                                       ipc_port_release_sonce(notify_port);
-
-                               /* space is locked */
-                               kr = ipc_entry_grow_table(space,
-                                                         ITS_SIZE_NONE);
-                               if (kr != KERN_SUCCESS) {
-                                       /* space is unlocked */
-
-                                       if (kr == KERN_RESOURCE_SHORTAGE) {
-                                               printf("ipc_kmsg_copyout_header: can't grow kernel ipc space\n");
-                                               return (MACH_RCV_HEADER_ERROR|
-                                                       MACH_MSG_IPC_KERNEL);
-                                       } else {
-                                               printf("ipc_kmsg_copyout_header: can't grow user ipc space\n");
-                                               return (MACH_RCV_HEADER_ERROR|
-                                                       MACH_MSG_IPC_SPACE);
-                                       }
+                       /* Is there already an entry we can use? */
+                       if ((reply_type != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
+                           ipc_right_reverse(space, (ipc_object_t) reply, &reply_name, &entry)) {
+                               /* reply port is locked and active */
+                               assert(entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE);
+                       } else {
+                               ip_lock(reply);
+                               if (!ip_active(reply)) {
+                                       ip_unlock(reply);
+                                       
+                                       release_reply_port = reply;
+                                       reply = IP_DEAD;
+                                       reply_name = MACH_PORT_DEAD;
+                                       goto done_with_reply;
                                }
-                               /* space is locked again; start over */
-
-                               continue;
-                       }
-                       assert(IE_BITS_TYPE(entry->ie_bits) ==
-                              MACH_PORT_TYPE_NONE);
-                       assert(entry->ie_object == IO_NULL); 
-
-                       if (notify_port == IP_NULL) {
-                               /* not making a dead-name request */
-
+                               
+                               /* claim a held entry for the reply port */
+                               assert(entries_held > 0);
+                               entries_held--;
+                               ipc_entry_claim(space, &reply_name, &entry);
+                               assert(IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE);
+                               assert(entry->ie_object == IO_NULL); 
                                entry->ie_object = (ipc_object_t) reply;
-                               break;
                        }
 
-                       kr = ipc_port_dnrequest(reply, reply_name,
-                                               notify_port, &request);
-                       if (kr != KERN_SUCCESS) {
-                               ip_unlock(reply);
+                       /* space and reply port are locked and active */
+                       ip_reference(reply);    /* hold onto the reply port */
 
-                               ipc_port_release_sonce(notify_port);
+                       kr = ipc_right_copyout(space, reply_name, entry,
+                                              reply_type, TRUE, (ipc_object_t) reply);
+                       assert(kr == KERN_SUCCESS);
+                       /* reply port is unlocked */
+               } else
+                       reply_name = CAST_MACH_PORT_TO_NAME(reply);
 
-                               ipc_entry_dealloc(space, reply_name, entry);
-                               is_write_unlock(space);
+       done_with_reply:
 
-                               ip_lock(reply);
-                               if (!ip_active(reply)) {
-                                       /* will fail next time around loop */
+               /* Handle voucher port. */
+               if (voucher_type != MACH_MSGH_BITS_ZERO) {
+                       assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
 
-                                       ip_unlock(reply);
-                                       is_write_lock(space);
-                                       continue;
+                       if (!IP_VALID(voucher)) {
+                               if ((option & MACH_RCV_VOUCHER) == 0) {
+                                       voucher_type = MACH_MSGH_BITS_ZERO;
                                }
-
-                               kr = ipc_port_dngrow(reply, ITS_SIZE_NONE);
-                               /* port is unlocked */
-                               if (kr != KERN_SUCCESS) {
-                                       printf("ipc_kmsg_copyout_header: can't grow kernel ipc space2\n");
-                                       return (MACH_RCV_HEADER_ERROR|
-                                               MACH_MSG_IPC_KERNEL);
+                               voucher_name = MACH_PORT_NULL;
+                               goto done_with_voucher;
+                       }
+                       
+                       /* clear voucher from its hiding place back in the kmsg */
+                       kmsg->ikm_voucher = IP_NULL;
+
+                       if ((option & MACH_RCV_VOUCHER) != 0) {
+                               ipc_entry_t entry;
+
+                               if (ipc_right_reverse(space, (ipc_object_t) voucher,
+                                                     &voucher_name, &entry)) {
+                                       /* voucher port locked */
+                                       assert(entry->ie_bits & MACH_PORT_TYPE_SEND);
+                               } else {
+                                       assert(entries_held > 0);
+                                       entries_held--;
+                                       ipc_entry_claim(space, &voucher_name, &entry);
+                                       assert(IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE);
+                                       assert(entry->ie_object == IO_NULL); 
+                                       entry->ie_object = (ipc_object_t) voucher;
+                                       ip_lock(voucher);
                                }
-
-                               is_write_lock(space);
-                               continue;
+                               /* space is locked and active */
+
+                               assert(ip_active(voucher));
+                               assert(ip_kotype(voucher) == IKOT_VOUCHER);
+                               kr = ipc_right_copyout(space, voucher_name, entry,
+                                                      MACH_MSG_TYPE_MOVE_SEND, TRUE, 
+                                                      (ipc_object_t) voucher);
+                               /* voucher port is unlocked */
+                       } else {
+                               voucher_type = MACH_MSGH_BITS_ZERO;
+                               release_voucher_port = voucher;
+                               voucher_name = MACH_PORT_NULL;
                        }
-
-                       notify_port = IP_NULL; /* don't release right below */
-
-                       entry->ie_object = (ipc_object_t) reply;
-                       entry->ie_request = request;
-                       break;
+               } else {
+                       voucher_name = msg->msgh_voucher_port;
                }
 
-               /* space and reply port are locked and active */
-
-               ip_reference(reply);    /* hold onto the reply port */
-
-               kr = ipc_right_copyout(space, reply_name, entry,
-                                      reply_type, TRUE, (ipc_object_t) reply);
-               /* reply port is unlocked */
-               assert(kr == KERN_SUCCESS);
-
-               if (notify_port != IP_NULL)
-                       ipc_port_release_sonce(notify_port);
+       done_with_voucher:
 
                ip_lock(dest);
                is_write_unlock(space);
+
        } else {
                /*
-                *      No reply port!  This is an easy case.
+                *      No reply or voucher port!  This is an easy case.
                 *      We only need to have the space locked
-                *      when checking notify and when locking
-                *      the destination (to ensure atomicity).
+                *      when locking the destination.
                 */
 
                is_read_lock(space);
-               if (!space->is_active) {
+               if (!is_active(space)) {
                        is_read_unlock(space);
                        return MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE;
                }
 
-               if (notify != MACH_PORT_NULL) {
-                       ipc_entry_t entry;
-
-                       /* must check notify even though it won't be used */
-
-                       if ((entry = ipc_entry_lookup(space, notify)) == IE_NULL) {
-                               printf("ipc_kmsg_copyout_header: ipc_entry_lookup failed\n");
-                               is_read_unlock(space);
-                               return MACH_RCV_INVALID_NOTIFY;
-                       }
-       
-                       if ((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0) {
-                               printf("ipc_kmsg_copyout_header: MACH_PORT_TYPE_RECEIVE not set!\n");
-                               is_read_unlock(space);
-                               return MACH_RCV_INVALID_NOTIFY;
-                       }
-               }
-
                ip_lock(dest);
                is_read_unlock(space);
 
                reply_name = CAST_MACH_PORT_TO_NAME(reply);
+
+               if (voucher_type != MACH_MSGH_BITS_ZERO) {
+                       assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
+                       if ((option & MACH_RCV_VOUCHER) == 0) {
+                               voucher_type = MACH_MSGH_BITS_ZERO;
+                       }
+                       voucher_name = MACH_PORT_NULL;
+               } else {
+                       voucher_name = msg->msgh_voucher_port;
+               }
        }
 
        /*
@@ -3125,18 +3271,17 @@ ipc_kmsg_copyout_header(
         *      is done correctly.
         */
 
-    copyout_dest:
-
        if (ip_active(dest)) {
                ipc_object_copyout_dest(space, (ipc_object_t) dest,
                                        dest_type, &dest_name);
                /* dest is unlocked */
+
        } else {
                ipc_port_timestamp_t timestamp;
 
                timestamp = dest->ip_timestamp;
+               ip_unlock(dest);
                ip_release(dest);
-               ip_check_unlock(dest);
 
                if (IP_VALID(reply)) {
                        ip_lock(reply);
@@ -3152,15 +3297,43 @@ ipc_kmsg_copyout_header(
        }
 
        if (IP_VALID(reply))
-               ipc_port_release(reply);
+               ip_release(reply);
+
+       if (IP_VALID(release_reply_port)) {
+               if (reply_type == MACH_MSG_TYPE_PORT_SEND_ONCE)
+                       ipc_port_release_sonce(release_reply_port);
+               else
+                       ipc_port_release_send(release_reply_port);
+       }
+
+       if (IP_VALID(release_voucher_port))
+               ipc_port_release_send(release_voucher_port);
 
-       msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
-                         MACH_MSGH_BITS(reply_type, dest_type));
+
+       if ((option & MACH_RCV_VOUCHER) != 0) {
+           KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_RECV) | DBG_FUNC_NONE,
+                                 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
+                                 (uintptr_t)kmsg->ikm_header->msgh_bits,
+                                 (uintptr_t)kmsg->ikm_header->msgh_id,
+                                 VM_KERNEL_ADDRPERM((uintptr_t)unsafe_convert_port_to_voucher(voucher)),
+                                 0);
+       } else {
+           KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_RECV_VOUCHER_REFUSED) | DBG_FUNC_NONE,
+                                 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
+                                 (uintptr_t)kmsg->ikm_header->msgh_bits,
+                                 (uintptr_t)kmsg->ikm_header->msgh_id,
+                                 VM_KERNEL_ADDRPERM((uintptr_t)unsafe_convert_port_to_voucher(voucher)),
+                                 0);
+       }
+
+       msg->msgh_bits = MACH_MSGH_BITS_SET(reply_type, dest_type,
+                                           voucher_type, mbits);
        msg->msgh_local_port = CAST_MACH_NAME_TO_PORT(dest_name);
        msg->msgh_remote_port = CAST_MACH_NAME_TO_PORT(reply_name);
+       msg->msgh_voucher_port = voucher_name;
     }
 
-       return MACH_MSG_SUCCESS;
+    return MACH_MSG_SUCCESS;
 }
 
 /*
@@ -3263,7 +3436,7 @@ mach_msg_descriptor_t *
 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)
 {
     vm_map_copy_t                      copy;
-    mach_vm_offset_t           rcv_addr;
+    vm_map_address_t                   rcv_addr;
     mach_msg_copy_options_t            copy_options;
     mach_msg_size_t                    size;
     mach_msg_descriptor_type_t dsc_type;
@@ -3275,55 +3448,15 @@ ipc_kmsg_copyout_ool_descriptor(mach_msg_ool_descriptor_t *dsc, mach_msg_descrip
     copy_options = dsc->copy;
     assert(copy_options != MACH_MSG_KALLOC_COPY_T);
     dsc_type = dsc->type;
-    rcv_addr = 0;
 
     if (copy != VM_MAP_COPY_NULL) {
-        /*
-         * Check to see if there is an overwrite descriptor
-         * specified in the scatter list for this ool data.
-         * The descriptor has already been verified.
-         */
-#if 0
-        if (saddr != MACH_MSG_DESCRIPTOR_NULL) {
-            if (differs) {
-                OTHER_OOL_DESCRIPTOR *scatter_dsc;
-
-                scatter_dsc = (OTHER_OOL_DESCRIPTOR *)saddr;
-                if (scatter_dsc->copy == MACH_MSG_OVERWRITE) {
-                    rcv_addr = (mach_vm_offset_t) scatter_dsc->address;
-                    copy_options = MACH_MSG_OVERWRITE;
-                } else {
-                    copy_options = MACH_MSG_VIRTUAL_COPY;
-                }
-            } else {
-                mach_msg_ool_descriptor_t *scatter_dsc;
-
-                scatter_dsc = &saddr->out_of_line;
-                if (scatter_dsc->copy == MACH_MSG_OVERWRITE) {
-                    rcv_addr = CAST_USER_ADDR_T(scatter_dsc->address);
-                    copy_options = MACH_MSG_OVERWRITE;
-                } else {
-                    copy_options = MACH_MSG_VIRTUAL_COPY;
-                }
-            }
-            INCREMENT_SCATTER(saddr, sdsc_count, differs);
-        }
-#endif
-
+       kern_return_t kr;
 
-        /*
-         * Whether the data was virtually or physically
-         * copied we have a vm_map_copy_t for it.
-         * If there's an overwrite region specified
-         * overwrite it, otherwise do a virtual copy out.
-         */
-        kern_return_t kr;
-        if (copy_options == MACH_MSG_OVERWRITE && rcv_addr != 0) {
-            kr = vm_map_copy_overwrite(map, rcv_addr,
-                    copy, TRUE);
-        } else {
-            kr = vm_map_copyout(map, &rcv_addr, copy);
-        }      
+        rcv_addr = 0;
+       if (vm_map_copy_validate_size(map, copy, (vm_map_size_t)size) == FALSE)
+               panic("Inconsistent OOL/copyout size on %p: expected %d, got %lld @%p",
+                     dsc, size, (unsigned long long)copy->size, copy);
+        kr = vm_map_copyout(map, &rcv_addr, copy);
         if (kr != KERN_SUCCESS) {
             if (kr == KERN_RESOURCE_SHORTAGE)
                 *mr |= MACH_MSG_VM_KERNEL;
@@ -3402,7 +3535,7 @@ ipc_kmsg_copyout_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t *dsc,
         ipc_kmsg_t kmsg,
         mach_msg_return_t *mr)
 {
-    mach_vm_offset_t           rcv_addr;
+    mach_vm_offset_t           rcv_addr = 0;
     mach_msg_type_name_t               disp;
     mach_msg_type_number_t             count, i;
     vm_size_t                          ports_length, names_length;
@@ -3446,8 +3579,9 @@ ipc_kmsg_copyout_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t *dsc,
             /*
              * Dynamically allocate the region
              */
-            int anywhere = VM_MAKE_TAG(VM_MEMORY_MACH_MSG)|
-                VM_FLAGS_ANYWHERE;
+            int anywhere = VM_FLAGS_ANYWHERE;
+           if (vm_kernel_map_is_kernel(map)) anywhere |= VM_MAKE_TAG(VM_KERN_MEMORY_IPC);
+           else                              anywhere |= VM_MAKE_TAG(VM_MEMORY_MACH_MSG);
 
             kern_return_t kr;
             if ((kr = mach_vm_allocate(map, &rcv_addr, 
@@ -3687,8 +3821,6 @@ ipc_kmsg_copyout_size(
  *             Nothing locked.
  *     Returns:
  *             MACH_MSG_SUCCESS        Copied out all rights and memory.
- *             MACH_RCV_INVALID_NOTIFY Bad notify port.
- *                     Rights and memory in the message are intact.
  *             MACH_RCV_HEADER_ERROR + special bits
  *                     Rights and memory in the message are intact.
  *             MACH_RCV_BODY_ERROR + special bits
@@ -3701,12 +3833,12 @@ ipc_kmsg_copyout(
        ipc_kmsg_t              kmsg,
        ipc_space_t             space,
        vm_map_t                map,
-       mach_port_name_t        notify,
-       mach_msg_body_t         *slist)
+       mach_msg_body_t         *slist,
+        mach_msg_option_t      option)
 {
        mach_msg_return_t mr;
 
-       mr = ipc_kmsg_copyout_header(kmsg->ikm_header, space, notify);
+       mr = ipc_kmsg_copyout_header(kmsg, space, option);
        if (mr != MACH_MSG_SUCCESS) {
                return mr;
        }
@@ -3751,20 +3883,43 @@ ipc_kmsg_copyout_pseudo(
        mach_msg_bits_t mbits = kmsg->ikm_header->msgh_bits;
        ipc_object_t dest = (ipc_object_t) kmsg->ikm_header->msgh_remote_port;
        ipc_object_t reply = (ipc_object_t) kmsg->ikm_header->msgh_local_port;
+       ipc_object_t voucher = (ipc_object_t) kmsg->ikm_voucher;
        mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
        mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
+       mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
+       mach_port_name_t voucher_name = kmsg->ikm_header->msgh_voucher_port;
        mach_port_name_t dest_name, reply_name;
        mach_msg_return_t mr;
 
        assert(IO_VALID(dest));
 
+#if 0
+       /*
+        * If we did this here, it looks like we wouldn't need the undo logic
+        * at the end of ipc_kmsg_send() in the error cases.  Not sure which
+        * would be more elegant to keep.
+        */
+       ipc_importance_clean(kmsg);
+#else
+       /* just assert it is already clean */
+       ipc_importance_assert_clean(kmsg);
+#endif
+
        mr = (ipc_kmsg_copyout_object(space, dest, dest_type, &dest_name) |
              ipc_kmsg_copyout_object(space, reply, reply_type, &reply_name));
 
-       kmsg->ikm_header->msgh_bits = mbits &~ MACH_MSGH_BITS_CIRCULAR;
+       kmsg->ikm_header->msgh_bits = mbits & MACH_MSGH_BITS_USER;
        kmsg->ikm_header->msgh_remote_port = CAST_MACH_NAME_TO_PORT(dest_name);
        kmsg->ikm_header->msgh_local_port = CAST_MACH_NAME_TO_PORT(reply_name);
 
+       if (IO_VALID(voucher)) {
+               assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
+
+               kmsg->ikm_voucher = IP_NULL;
+               mr |= ipc_kmsg_copyout_object(space, voucher, voucher_type, &voucher_name);
+               kmsg->ikm_header->msgh_voucher_port = voucher_name;
+       }
+               
        if (mbits & MACH_MSGH_BITS_COMPLEX) {
                mr |= ipc_kmsg_copyout_body(kmsg, space, map, slist);
        }
@@ -3789,25 +3944,32 @@ ipc_kmsg_copyout_dest(
        mach_msg_bits_t mbits;
        ipc_object_t dest;
        ipc_object_t reply;
+       ipc_object_t voucher;
        mach_msg_type_name_t dest_type;
        mach_msg_type_name_t reply_type;
-       mach_port_name_t dest_name, reply_name;
+       mach_msg_type_name_t voucher_type;
+       mach_port_name_t dest_name, reply_name, voucher_name;
 
        mbits = kmsg->ikm_header->msgh_bits;
        dest = (ipc_object_t) kmsg->ikm_header->msgh_remote_port;
        reply = (ipc_object_t) kmsg->ikm_header->msgh_local_port;
+       voucher = (ipc_object_t) kmsg->ikm_voucher;
+       voucher_name = kmsg->ikm_header->msgh_voucher_port;
        dest_type = MACH_MSGH_BITS_REMOTE(mbits);
        reply_type = MACH_MSGH_BITS_LOCAL(mbits);
+       voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
 
        assert(IO_VALID(dest));
 
+       ipc_importance_assert_clean(kmsg);
+
        io_lock(dest);
        if (io_active(dest)) {
                ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
                /* dest is unlocked */
        } else {
+               io_unlock(dest);
                io_release(dest);
-               io_check_unlock(dest);
                dest_name = MACH_PORT_DEAD;
        }
 
@@ -3817,10 +3979,19 @@ ipc_kmsg_copyout_dest(
        } else
                reply_name = CAST_MACH_PORT_TO_NAME(reply);
 
-       kmsg->ikm_header->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
-                                     MACH_MSGH_BITS(reply_type, dest_type));
+       if (IO_VALID(voucher)) {
+               assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
+
+               kmsg->ikm_voucher = IP_NULL;
+               ipc_object_destroy((ipc_object_t)voucher, voucher_type);
+               voucher_name = MACH_PORT_NULL;
+       }
+
+       kmsg->ikm_header->msgh_bits = MACH_MSGH_BITS_SET(reply_type, dest_type,
+                                                        voucher_type, mbits);
        kmsg->ikm_header->msgh_local_port = CAST_MACH_NAME_TO_PORT(dest_name);
        kmsg->ikm_header->msgh_remote_port = CAST_MACH_NAME_TO_PORT(reply_name);
+       kmsg->ikm_header->msgh_voucher_port = voucher_name;
 
        if (mbits & MACH_MSGH_BITS_COMPLEX) {
                mach_msg_body_t *body;
@@ -3831,163 +4002,6 @@ ipc_kmsg_copyout_dest(
        }
 }
 
-/*
- *      Routine:        ipc_kmsg_copyin_scatter
- *      Purpose:
- *              allocate and copyin a scatter list
- *      Algorithm:
- *              The gather (kmsg) is valid since it has been copied in.
- *              Gather list descriptors are sequentially paired with scatter
- *              list descriptors, with port descriptors in either list ignored.
- *              Descriptors are consistent if the type fileds match and size
- *              of the scatter descriptor is less than or equal to the
- *              size of the gather descriptor.  A MACH_MSG_ALLOCATE copy
- *              strategy in a scatter descriptor matches any size in the
- *              corresponding gather descriptor assuming they are the same type.
- *              Either list may be larger than the other.  During the
- *              subsequent copy out, excess scatter descriptors are ignored
- *              and excess gather descriptors default to dynamic allocation.
- *
- *              In the case of a size error, the scatter list is released.
- *      Conditions:
- *              Nothing locked.
- *      Returns:
- *              the allocated message body containing the scatter list.
- */
-
-mach_msg_body_t *
-ipc_kmsg_get_scatter(
-       mach_vm_address_t       msg_addr,
-       mach_msg_size_t         slist_size,
-       ipc_kmsg_t              kmsg)
-{
-        mach_msg_body_t         *slist;
-        mach_msg_body_t         *body;
-        mach_msg_descriptor_t   *gstart, *gend;
-        mach_msg_descriptor_t   *sstart, *send;
-
-#if defined(__LP64__)
-        panic("ipc_kmsg_get_scatter called!");
-#endif
-
-        if (slist_size < sizeof(mach_msg_base_t))
-                return MACH_MSG_BODY_NULL;
-
-        slist_size -= (mach_msg_size_t)sizeof(mach_msg_header_t);
-        slist = (mach_msg_body_t *)kalloc(slist_size);
-        if (slist == MACH_MSG_BODY_NULL)
-                return slist;
-
-        if (copyin(msg_addr + sizeof(mach_msg_header_t), (char *)slist, slist_size)) {
-                kfree(slist, slist_size);
-                return MACH_MSG_BODY_NULL;
-        }
-
-        if ((slist->msgh_descriptor_count* sizeof(mach_msg_descriptor_t)
-             + sizeof(mach_msg_size_t)) > slist_size) {
-                kfree(slist, slist_size);
-                return MACH_MSG_BODY_NULL;
-        }
-
-        body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
-        gstart = (mach_msg_descriptor_t *) (body + 1);
-        gend = gstart + body->msgh_descriptor_count;
-
-        sstart = (mach_msg_descriptor_t *) (slist + 1);
-        send = sstart + slist->msgh_descriptor_count;
-
-        while (gstart < gend) {
-            mach_msg_descriptor_type_t  g_type;
-
-            /*
-             * Skip port descriptors in gather list.
-             */
-            g_type = gstart->type.type;
-
-            if (g_type != MACH_MSG_PORT_DESCRIPTOR) {
-
-             /*
-              * A scatter list with a 0 descriptor count is treated as an
-              * automatic size mismatch.
-              */
-             if (slist->msgh_descriptor_count == 0) {
-                        kfree(slist, slist_size);
-                        return MACH_MSG_BODY_NULL;
-             }
-
-             /*
-              * Skip port descriptors in  scatter list.
-              */
-             while (sstart < send) {
-                    if (sstart->type.type != MACH_MSG_PORT_DESCRIPTOR)
-                        break;
-                    sstart++;
-             }
-
-             /*
-              * No more scatter descriptors, we're done
-              */
-             if (sstart >= send) {
-                    break;
-             }
-
-             /*
-              * Check type, copy and size fields
-              */
-                if (g_type == MACH_MSG_OOL_DESCRIPTOR ||
-                    g_type == MACH_MSG_OOL_VOLATILE_DESCRIPTOR) {
-                    if (sstart->type.type != MACH_MSG_OOL_DESCRIPTOR &&
-                        sstart->type.type != MACH_MSG_OOL_VOLATILE_DESCRIPTOR) {
-                        kfree(slist, slist_size);
-                        return MACH_MSG_BODY_NULL;
-                    }
-                    if (sstart->out_of_line.copy == MACH_MSG_OVERWRITE &&
-                        gstart->out_of_line.size > sstart->out_of_line.size) {
-                        kfree(slist, slist_size);
-                        return MACH_MSG_BODY_NULL;
-                    }
-                }
-                else {
-                 if (sstart->type.type != MACH_MSG_OOL_PORTS_DESCRIPTOR) {
-                        kfree(slist, slist_size);
-                        return MACH_MSG_BODY_NULL;
-                 }
-                    if (sstart->ool_ports.copy == MACH_MSG_OVERWRITE &&
-                        gstart->ool_ports.count > sstart->ool_ports.count) {
-                        kfree(slist, slist_size);
-                        return MACH_MSG_BODY_NULL;
-                    }
-                }
-                sstart++;
-            }
-            gstart++;
-        }
-        return slist;
-}
-
-
-/*
- *      Routine:        ipc_kmsg_free_scatter
- *      Purpose:
- *              Deallocate a scatter list.  Since we actually allocated
- *              a body without a header, and since the header was originally
- *              accounted for in slist_size, we have to ajust it down
- *              before freeing the scatter list.
- */
-void
-ipc_kmsg_free_scatter(
-        mach_msg_body_t *slist,
-        mach_msg_size_t slist_size)
-{
-#if defined(__LP64__)
-        panic("%s called; halting!", __func__);
-#endif
-
-        slist_size -= (mach_msg_size_t)sizeof(mach_msg_header_t);
-        kfree(slist, slist_size);
-}
-
-
 /*
  *     Routine:        ipc_kmsg_copyout_to_kernel
  *     Purpose:
@@ -4024,8 +4038,8 @@ ipc_kmsg_copyout_to_kernel(
                ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
                /* dest is unlocked */
        } else {
+               io_unlock(dest);
                io_release(dest);
-               io_check_unlock(dest);
                dest_name = MACH_PORT_DEAD;
        }
 
@@ -4062,8 +4076,8 @@ ipc_kmsg_copyout_to_kernel_legacy(
                ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
                /* dest is unlocked */
        } else {
+               io_unlock(dest);
                io_release(dest);
-               io_check_unlock(dest);
                dest_name = MACH_PORT_DEAD;
        }
 
@@ -4158,271 +4172,47 @@ ipc_kmsg_copyout_to_kernel_legacy(
 #endif /* IKM_SUPPORT_LEGACY */
 
 
-#include <mach_kdb.h>
-#if    MACH_KDB
-
-#include <ddb/db_output.h>
-#include <ipc/ipc_print.h>
-/*
- * Forward declarations
- */
-void ipc_msg_print_untyped(
-       mach_msg_body_t         *body);
-
-const char * ipc_type_name(
-       int             type_name,
-       boolean_t       received);
-
-const char *
-msgh_bit_decode(
-       mach_msg_bits_t bit);
-
-const char *
-mm_copy_options_string(
-       mach_msg_copy_options_t option);
-
-void db_print_msg_uid(mach_msg_header_t *);
-
-
-const char *
-ipc_type_name(
-       int             type_name,
-       boolean_t       received)
-{
-       switch (type_name) {
-               case MACH_MSG_TYPE_PORT_NAME:
-               return "port_name";
-               
-               case MACH_MSG_TYPE_MOVE_RECEIVE:
-               if (received) {
-                       return "port_receive";
-               } else {
-                       return "move_receive";
-               }
-               
-               case MACH_MSG_TYPE_MOVE_SEND:
-               if (received) {
-                       return "port_send";
-               } else {
-                       return "move_send";
-               }
-               
-               case MACH_MSG_TYPE_MOVE_SEND_ONCE:
-               if (received) {
-                       return "port_send_once";
-               } else {
-                       return "move_send_once";
-               }
-               
-               case MACH_MSG_TYPE_COPY_SEND:
-               return "copy_send";
-               
-               case MACH_MSG_TYPE_MAKE_SEND:
-               return "make_send";
-               
-               case MACH_MSG_TYPE_MAKE_SEND_ONCE:
-               return "make_send_once";
-               
-               default:
-               return (char *) 0;
-       }
-}
-               
-void
-ipc_print_type_name(
-       int     type_name)
-{
-       const char *name = ipc_type_name(type_name, TRUE);
-       if (name) {
-               printf("%s", name);
-       } else {
-               printf("type%d", type_name);
-       }
-}
-
-/*
- * ipc_kmsg_print      [ debug ]
- */
-void
-ipc_kmsg_print(
-       ipc_kmsg_t      kmsg)
-{
-       iprintf("kmsg=0x%x\n", kmsg);
-       iprintf("ikm_next=0x%x, prev=0x%x, size=%d",
-               kmsg->ikm_next,
-               kmsg->ikm_prev,
-               kmsg->ikm_size);
-       printf("\n");
-       ipc_msg_print(kmsg->ikm_header);
-}
-
-const char *
-msgh_bit_decode(
-       mach_msg_bits_t bit)
-{
-       switch (bit) {
-           case MACH_MSGH_BITS_COMPLEX:        return "complex";
-           case MACH_MSGH_BITS_CIRCULAR:       return "circular";
-           default:                            return (char *) 0;
-       }
-}
-
-/*
- * ipc_msg_print       [ debug ]
- */
-void
-ipc_msg_print(
-       mach_msg_header_t       *msgh)
+mach_msg_trailer_size_t
+ipc_kmsg_add_trailer(ipc_kmsg_t kmsg, ipc_space_t space __unused, 
+               mach_msg_option_t option, thread_t thread, 
+               mach_port_seqno_t seqno, boolean_t minimal_trailer,
+               mach_vm_offset_t context)
 {
-       mach_msg_bits_t mbits;
-       unsigned int    bit, i;
-       const char      *bit_name;
-       int             needs_comma;
+       mach_msg_max_trailer_t *trailer;
 
-       mbits = msgh->msgh_bits;
-       iprintf("msgh_bits=0x%x:  l=0x%x,r=0x%x\n",
-               mbits,
-               MACH_MSGH_BITS_LOCAL(msgh->msgh_bits),
-               MACH_MSGH_BITS_REMOTE(msgh->msgh_bits));
+       (void)thread;
+       trailer = (mach_msg_max_trailer_t *)
+               ((vm_offset_t)kmsg->ikm_header +
+                round_msg(kmsg->ikm_header->msgh_size));
 
-       mbits = MACH_MSGH_BITS_OTHER(mbits) & MACH_MSGH_BITS_USED;
-       db_indent += 2;
-       if (mbits)
-               iprintf("decoded bits:  ");
-       needs_comma = 0;
-       for (i = 0, bit = 1; i < sizeof(mbits) * 8; ++i, bit <<= 1) {
-               if ((mbits & bit) == 0)
-                       continue;
-               bit_name = msgh_bit_decode((mach_msg_bits_t)bit);
-               if (bit_name)
-                       printf("%s%s", needs_comma ? "," : "", bit_name);
-               else
-                       printf("%sunknown(0x%x),", needs_comma ? "," : "", bit);
-               ++needs_comma;
-       }
-       if (msgh->msgh_bits & ~MACH_MSGH_BITS_USED) {
-               printf("%sunused=0x%x,", needs_comma ? "," : "",
-                      msgh->msgh_bits & ~MACH_MSGH_BITS_USED);
+       if (!(option & MACH_RCV_TRAILER_MASK)) {
+               return trailer->msgh_trailer_size;
        }
-       printf("\n");
-       db_indent -= 2;
 
-       needs_comma = 1;
-       if (msgh->msgh_remote_port) {
-               iprintf("remote=0x%x(", msgh->msgh_remote_port);
-               ipc_print_type_name(MACH_MSGH_BITS_REMOTE(msgh->msgh_bits));
-               printf(")");
-       } else {
-               iprintf("remote=null");
-       }
+       trailer->msgh_seqno = seqno;
+       trailer->msgh_context = context;
+       trailer->msgh_trailer_size = REQUESTED_TRAILER_SIZE(thread_is_64bit(thread), option);
 
-       if (msgh->msgh_local_port) {
-               printf("%slocal=%p(", needs_comma ? "," : "",
-                      msgh->msgh_local_port);
-               ipc_print_type_name(MACH_MSGH_BITS_LOCAL(msgh->msgh_bits));
-               printf(")\n");
-       } else {
-               printf("local=null\n");
+       if (minimal_trailer) { 
+               goto done;
        }
 
-       iprintf("msgh_id=%d, size=%d\n",
-               msgh->msgh_id,
-               msgh->msgh_size);
-
-       if (mbits & MACH_MSGH_BITS_COMPLEX) {   
-               ipc_msg_print_untyped((mach_msg_body_t *) (msgh + 1));
+       if (MACH_RCV_TRAILER_ELEMENTS(option) >= 
+                       MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AV)){
+               trailer->msgh_ad = 0;
        }
-}
 
+       /*
+        * The ipc_kmsg_t holds a reference to the label of a label
+        * handle, not the port. We must get a reference to the port
+        * and a send right to copyout to the receiver.
+        */
 
-const char *
-mm_copy_options_string(
-       mach_msg_copy_options_t option)
-{
-       const char      *name;
-
-       switch (option) {
-           case MACH_MSG_PHYSICAL_COPY:
-               name = "PHYSICAL";
-               break;
-           case MACH_MSG_VIRTUAL_COPY:
-               name = "VIRTUAL";
-               break;
-           case MACH_MSG_OVERWRITE:
-               name = "OVERWRITE";
-               break;
-           case MACH_MSG_ALLOCATE:
-               name = "ALLOCATE";
-               break;
-           case MACH_MSG_KALLOC_COPY_T:
-               name = "KALLOC_COPY_T";
-               break;
-           default:
-               name = "unknown";
-               break;
+       if (option & MACH_RCV_TRAILER_ELEMENTS (MACH_RCV_TRAILER_LABELS)) {
+               trailer->msgh_labels.sender = 0;
        }
-       return name;
-}
-
-void
-ipc_msg_print_untyped(
-       mach_msg_body_t         *body)
-{
-    mach_msg_descriptor_t      *saddr, *send;
-    mach_msg_descriptor_type_t type;
-
-    iprintf("%d descriptors %d: \n", body->msgh_descriptor_count);
-
-    saddr = (mach_msg_descriptor_t *) (body + 1);
-    send = saddr + body->msgh_descriptor_count;
-
-    for ( ; saddr < send; saddr++ ) {
-       
-       type = saddr->type.type;
-
-       switch (type) {
-           
-           case MACH_MSG_PORT_DESCRIPTOR: {
-               mach_msg_port_descriptor_t *dsc;
 
-               dsc = &saddr->port;
-               iprintf("-- PORT name = 0x%x disp = ", dsc->name);
-               ipc_print_type_name(dsc->disposition);
-               printf("\n");
-               break;
-           }
-           case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
-           case MACH_MSG_OOL_DESCRIPTOR: {
-               mach_msg_ool_descriptor_t *dsc;
-               
-               dsc = &saddr->out_of_line;
-               iprintf("-- OOL%s addr = 0x%x size = 0x%x copy = %s %s\n",
-                       type == MACH_MSG_OOL_DESCRIPTOR ? "" : " VOLATILE",
-                       dsc->address, dsc->size,
-                       mm_copy_options_string(dsc->copy),
-                       dsc->deallocate ? "DEALLOC" : "");
-               break;
-           } 
-           case MACH_MSG_OOL_PORTS_DESCRIPTOR : {
-               mach_msg_ool_ports_descriptor_t *dsc;
+done:
 
-               dsc = &saddr->ool_ports;
-
-               iprintf("-- OOL_PORTS addr = 0x%x count = 0x%x ",
-                         dsc->address, dsc->count);
-               printf("disp = ");
-               ipc_print_type_name(dsc->disposition);
-               printf(" copy = %s %s\n",
-                      mm_copy_options_string(dsc->copy),
-                      dsc->deallocate ? "DEALLOC" : "");
-               break;
-           }
-
-           default: {
-               iprintf("-- UNKNOWN DESCRIPTOR 0x%x\n", type);
-               break;
-           }
-       }
-    }
+       return trailer->msgh_trailer_size;
 }
-#endif /* MACH_KDB */