]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/ipc/mach_port.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / osfmk / ipc / mach_port.c
index 78afb835335233d9af374dc7ed463e13b05173f6..4e1e6f488b2738ea0397b29ba82f5b62629d609a 100644 (file)
@@ -1,21 +1,22 @@
 /*
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
  * 
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
@@ -69,8 +70,8 @@
 #include <mach/vm_map.h>
 #include <kern/task.h>
 #include <kern/counters.h>
 #include <mach/vm_map.h>
 #include <kern/task.h>
 #include <kern/counters.h>
-#include <kern/thread_act.h>
-#include <kern/thread_pool.h>
+#include <kern/thread.h>
+#include <kern/kalloc.h>
 #include <mach/mach_port_server.h>
 #include <vm/vm_map.h>
 #include <vm/vm_kern.h>
 #include <mach/mach_port_server.h>
 #include <vm/vm_map.h>
 #include <vm/vm_kern.h>
@@ -93,8 +94,7 @@ void mach_port_names_helper(
        mach_port_name_t        name,
        mach_port_name_t        *names,
        mach_port_type_t        *types,
        mach_port_name_t        name,
        mach_port_name_t        *names,
        mach_port_type_t        *types,
-       ipc_entry_num_t         *actualp,
-       ipc_space_t             space);
+       ipc_entry_num_t         *actualp);
 
 void mach_port_gst_helper(
        ipc_pset_t              pset,
 
 void mach_port_gst_helper(
        ipc_pset_t              pset,
@@ -121,8 +121,7 @@ mach_port_names_helper(
        mach_port_name_t        name,
        mach_port_name_t        *names,
        mach_port_type_t        *types,
        mach_port_name_t        name,
        mach_port_name_t        *names,
        mach_port_type_t        *types,
-       ipc_entry_num_t         *actualp,
-       ipc_space_t             space)
+       ipc_entry_num_t         *actualp)
 {
        ipc_entry_bits_t bits;
        ipc_port_request_index_t request;
 {
        ipc_entry_bits_t bits;
        ipc_port_request_index_t request;
@@ -196,7 +195,6 @@ mach_port_names(
        mach_port_type_t        **typesp,
        mach_msg_type_number_t  *typesCnt)
 {
        mach_port_type_t        **typesp,
        mach_msg_type_number_t  *typesCnt)
 {
-       ipc_entry_bits_t *capability;
        ipc_tree_entry_t tentry;
        ipc_entry_t table;
        ipc_entry_num_t tsize;
        ipc_tree_entry_t tentry;
        ipc_entry_t table;
        ipc_entry_num_t tsize;
@@ -251,11 +249,11 @@ mach_port_names(
                }
                size = size_needed;
 
                }
                size = size_needed;
 
-               kr = vm_allocate(ipc_kernel_map, &addr1, size, TRUE);
+               kr = vm_allocate(ipc_kernel_map, &addr1, size, VM_FLAGS_ANYWHERE);
                if (kr != KERN_SUCCESS)
                        return KERN_RESOURCE_SHORTAGE;
 
                if (kr != KERN_SUCCESS)
                        return KERN_RESOURCE_SHORTAGE;
 
-               kr = vm_allocate(ipc_kernel_map, &addr2, size, TRUE);
+               kr = vm_allocate(ipc_kernel_map, &addr2, size, VM_FLAGS_ANYWHERE);
                if (kr != KERN_SUCCESS) {
                        kmem_free(ipc_kernel_map, addr1, size);
                        return KERN_RESOURCE_SHORTAGE;
                if (kr != KERN_SUCCESS) {
                        kmem_free(ipc_kernel_map, addr1, size);
                        return KERN_RESOURCE_SHORTAGE;
@@ -263,13 +261,24 @@ mach_port_names(
 
                /* can't fault while we hold locks */
 
 
                /* can't fault while we hold locks */
 
-               kr = vm_map_wire(ipc_kernel_map, addr1, addr1 + size,
-                                    VM_PROT_READ|VM_PROT_WRITE, FALSE);
-               assert(kr == KERN_SUCCESS);
+               kr = vm_map_wire(ipc_kernel_map, vm_map_trunc_page(addr1),
+                                vm_map_round_page(addr1 + size),
+                                VM_PROT_READ|VM_PROT_WRITE, FALSE);
+               if (kr != KERN_SUCCESS) {
+                       kmem_free(ipc_kernel_map, addr1, size);
+                       kmem_free(ipc_kernel_map, addr2, size);
+                       return KERN_RESOURCE_SHORTAGE;
+               }
+
+               kr = vm_map_wire(ipc_kernel_map, vm_map_trunc_page(addr2),
+                                vm_map_round_page(addr2 + size),
+                                VM_PROT_READ|VM_PROT_WRITE, FALSE);
+               if (kr != KERN_SUCCESS) {
+                       kmem_free(ipc_kernel_map, addr1, size);
+                       kmem_free(ipc_kernel_map, addr2, size);
+                       return KERN_RESOURCE_SHORTAGE;
+               }
 
 
-               kr = vm_map_wire(ipc_kernel_map, addr2, addr2 + size,
-                                    VM_PROT_READ|VM_PROT_WRITE, FALSE);
-               assert(kr == KERN_SUCCESS);
        }
        /* space is read-locked and active */
 
        }
        /* space is read-locked and active */
 
@@ -291,7 +300,7 @@ mach_port_names(
 
                        name = MACH_PORT_MAKE(index, IE_BITS_GEN(bits));
                        mach_port_names_helper(timestamp, entry, name, names,
 
                        name = MACH_PORT_MAKE(index, IE_BITS_GEN(bits));
                        mach_port_names_helper(timestamp, entry, name, names,
-                                              types, &actual, space);
+                                              types, &actual);
                }
        }
 
                }
        }
 
@@ -303,7 +312,7 @@ mach_port_names(
 
                assert(IE_BITS_TYPE(tentry->ite_bits) != MACH_PORT_TYPE_NONE);
                mach_port_names_helper(timestamp, entry, name, names,
 
                assert(IE_BITS_TYPE(tentry->ite_bits) != MACH_PORT_TYPE_NONE);
                mach_port_names_helper(timestamp, entry, name, names,
-                                      types, &actual, space);
+                                      types, &actual);
        }
        ipc_splay_traverse_finish(&space->is_tree);
        is_read_unlock(space);
        }
        ipc_splay_traverse_finish(&space->is_tree);
        is_read_unlock(space);
@@ -328,20 +337,20 @@ mach_port_names(
                 *      copied-in form.  Free any unused memory.
                 */
 
                 *      copied-in form.  Free any unused memory.
                 */
 
-               kr = vm_map_unwire(ipc_kernel_map,
-                                    addr1, addr1 + vm_size_used, FALSE);
+               kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr1),
+                                  vm_map_round_page(addr1 + vm_size_used), FALSE);
                assert(kr == KERN_SUCCESS);
 
                assert(kr == KERN_SUCCESS);
 
-               kr = vm_map_unwire(ipc_kernel_map,
-                                    addr2, addr2 + vm_size_used, FALSE);
+               kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr2),
+                                  vm_map_round_page(addr2 + vm_size_used), FALSE);
                assert(kr == KERN_SUCCESS);
 
                assert(kr == KERN_SUCCESS);
 
-               kr = vm_map_copyin(ipc_kernel_map, addr1, size_used,
-                                  TRUE, &memory1);
+               kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr1,
+                                  (vm_map_size_t)size_used, TRUE, &memory1);
                assert(kr == KERN_SUCCESS);
 
                assert(kr == KERN_SUCCESS);
 
-               kr = vm_map_copyin(ipc_kernel_map, addr2, size_used,
-                                  TRUE, &memory2);
+               kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr2,
+                                  (vm_map_size_t)size_used, TRUE, &memory2);
                assert(kr == KERN_SUCCESS);
 
                if (vm_size_used != size) {
                assert(kr == KERN_SUCCESS);
 
                if (vm_size_used != size) {
@@ -499,7 +508,7 @@ mach_port_allocate_name(
        if (!MACH_PORT_VALID(name))
                return KERN_INVALID_VALUE;
 
        if (!MACH_PORT_VALID(name))
                return KERN_INVALID_VALUE;
 
-       kr = mach_port_allocate_full (space, right, SUBSYSTEM_NULL,
+       kr = mach_port_allocate_full (space, right, MACH_PORT_NULL,
                                        &qos, &name);
        return (kr);
 }
                                        &qos, &name);
        return (kr);
 }
@@ -531,7 +540,7 @@ mach_port_allocate(
        kern_return_t           kr;
        mach_port_qos_t         qos = qos_template;
 
        kern_return_t           kr;
        mach_port_qos_t         qos = qos_template;
 
-       kr = mach_port_allocate_full (space, right, SUBSYSTEM_NULL,
+       kr = mach_port_allocate_full (space, right, MACH_PORT_NULL,
                                        &qos, namep);
        return (kr);
 }
                                        &qos, namep);
        return (kr);
 }
@@ -564,50 +573,13 @@ mach_port_allocate_qos(
 {
        kern_return_t           kr;
 
 {
        kern_return_t           kr;
 
-       if (qosp->name == TRUE)
+       if (qosp->name)
                return KERN_INVALID_ARGUMENT;
                return KERN_INVALID_ARGUMENT;
-       kr = mach_port_allocate_full (space, right, SUBSYSTEM_NULL,
+       kr = mach_port_allocate_full (space, right, MACH_PORT_NULL,
                                        qosp, namep);
        return (kr);
 }
 
                                        qosp, namep);
        return (kr);
 }
 
-/*
- *     Routine:        mach_port_allocate_subsystem [kernel call]
- *     Purpose:
- *             Allocates a receive right in a space.  Like
- *             mach_port_allocate, except that the caller specifies an
- *             RPC subsystem that is to be used to implement RPC's to the
- *             port. When possible, allocate rpc subsystem ports without
- *             nms, since within RPC ports are intended to be used for
- *             identity only (i.e. nms is painful in the distributed case
- *             and we don't need or want it for RPC anyway).
- *     Conditions:
- *             Nothing locked.
- *     Returns:
- *             KERN_SUCCESS            The right is allocated.
- *             KERN_INVALID_TASK       The space is null.
- *             KERN_INVALID_TASK       The space is dead.
- *             KERN_RESOURCE_SHORTAGE  Couldn't allocate memory.
- *             KERN_NO_SPACE           No room in space for another right.
- *             KERN_INVALID_ARGUMENT   bogus subsystem
- */
-
-kern_return_t
-mach_port_allocate_subsystem(
-       ipc_space_t             space,
-       subsystem_t             subsystem,
-       mach_port_name_t        *namep)
-{
-       kern_return_t           kr;
-       ipc_port_t              port;
-       mach_port_qos_t         qos = qos_template;
-
-       kr = mach_port_allocate_full (space, 
-                                       MACH_PORT_RIGHT_RECEIVE,
-                                       subsystem, &qos, namep);
-       return (kr);
-}
-
 /*
  *     Routine:        mach_port_allocate_full [kernel call]
  *     Purpose:
 /*
  *     Routine:        mach_port_allocate_full [kernel call]
  *     Purpose:
@@ -631,16 +603,19 @@ kern_return_t
 mach_port_allocate_full(
        ipc_space_t             space,
        mach_port_right_t       right,
 mach_port_allocate_full(
        ipc_space_t             space,
        mach_port_right_t       right,
-       subsystem_t             subsystem,
+       mach_port_t             proto,
        mach_port_qos_t         *qosp,
        mach_port_name_t        *namep)
 {
        mach_port_qos_t         *qosp,
        mach_port_name_t        *namep)
 {
-       ipc_kmsg_t              kmsg;
+       ipc_kmsg_t              kmsg = IKM_NULL;
        kern_return_t           kr;
 
        if (space == IS_NULL)
                return (KERN_INVALID_TASK);
 
        kern_return_t           kr;
 
        if (space == IS_NULL)
                return (KERN_INVALID_TASK);
 
+       if (proto != MACH_PORT_NULL)
+               return (KERN_INVALID_VALUE);
+
        if (qosp->name) {
                if (!MACH_PORT_VALID (*namep))
                        return (KERN_INVALID_VALUE);
        if (qosp->name) {
                if (!MACH_PORT_VALID (*namep))
                        return (KERN_INVALID_VALUE);
@@ -648,19 +623,13 @@ mach_port_allocate_full(
                        return (KERN_FAILURE);
        }
 
                        return (KERN_FAILURE);
        }
 
-       if (subsystem != SUBSYSTEM_NULL) {
-               if (right != MACH_PORT_RIGHT_RECEIVE)
-                       return (KERN_INVALID_VALUE);
-       }
-
        if (qosp->prealloc) {
                mach_msg_size_t size = qosp->len + MAX_TRAILER_SIZE;
                if (right != MACH_PORT_RIGHT_RECEIVE)
                        return (KERN_INVALID_VALUE);
        if (qosp->prealloc) {
                mach_msg_size_t size = qosp->len + MAX_TRAILER_SIZE;
                if (right != MACH_PORT_RIGHT_RECEIVE)
                        return (KERN_INVALID_VALUE);
-               kmsg = (ipc_kmsg_t)kalloc(ikm_plus_overhead(size));
+               kmsg = (ipc_kmsg_t)ipc_kmsg_alloc(size);
                if (kmsg == IKM_NULL)
                        return (KERN_RESOURCE_SHORTAGE);
                if (kmsg == IKM_NULL)
                        return (KERN_RESOURCE_SHORTAGE);
-               ikm_init(kmsg, size);
        }
 
        switch (right) {
        }
 
        switch (right) {
@@ -673,16 +642,12 @@ mach_port_allocate_full(
                else
                        kr = ipc_port_alloc(space, namep, &port);
                if (kr == KERN_SUCCESS) {
                else
                        kr = ipc_port_alloc(space, namep, &port);
                if (kr == KERN_SUCCESS) {
-                       if (qosp->prealloc
+                       if (kmsg != IKM_NULL
                                ipc_kmsg_set_prealloc(kmsg, port);
 
                                ipc_kmsg_set_prealloc(kmsg, port);
 
-                       if (subsystem != SUBSYSTEM_NULL) {
-                               port->ip_subsystem = &subsystem->user;
-                               subsystem_reference (subsystem);
-                       }
                        ip_unlock(port);
 
                        ip_unlock(port);
 
-               } else if (qosp->prealloc)
+               } else if (kmsg != IKM_NULL)
                        ipc_kmsg_free(kmsg);
                break;
            }
                        ipc_kmsg_free(kmsg);
                break;
            }
@@ -1021,7 +986,6 @@ mach_port_gst_helper(
        mach_port_name_t        *names,
        ipc_entry_num_t         *actualp)
 {
        mach_port_name_t        *names,
        ipc_entry_num_t         *actualp)
 {
-       ipc_pset_t ip_pset;
        mach_port_name_t name;
 
        assert(port != IP_NULL);
        mach_port_name_t name;
 
        assert(port != IP_NULL);
@@ -1091,7 +1055,7 @@ mach_port_get_set_status(
                mach_port_name_t *names;
                ipc_pset_t pset;
 
                mach_port_name_t *names;
                ipc_pset_t pset;
 
-               kr = vm_allocate(ipc_kernel_map, &addr, size, TRUE);
+               kr = vm_allocate(ipc_kernel_map, &addr, size, VM_FLAGS_ANYWHERE);
                if (kr != KERN_SUCCESS)
                        return KERN_RESOURCE_SHORTAGE;
 
                if (kr != KERN_SUCCESS)
                        return KERN_RESOURCE_SHORTAGE;
 
@@ -1179,12 +1143,12 @@ mach_port_get_set_status(
                 *      copied-in form.  Free any unused memory.
                 */
 
                 *      copied-in form.  Free any unused memory.
                 */
 
-               kr = vm_map_unwire(ipc_kernel_map,
-                                    addr, addr + vm_size_used, FALSE);
+               kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr), 
+                                  vm_map_round_page(addr + vm_size_used), FALSE);
                assert(kr == KERN_SUCCESS);
 
                assert(kr == KERN_SUCCESS);
 
-               kr = vm_map_copyin(ipc_kernel_map, addr, size_used,
-                                  TRUE, &memory);
+               kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr,
+                                  (vm_map_size_t)size_used, TRUE, &memory);
                assert(kr == KERN_SUCCESS);
 
                if (vm_size_used != size)
                assert(kr == KERN_SUCCESS);
 
                if (vm_size_used != size)
@@ -1269,7 +1233,7 @@ mach_port_move_member(
                assert(nset != IPS_NULL);
        }
        ip_lock(port);
                assert(nset != IPS_NULL);
        }
        ip_lock(port);
-       ipc_pset_remove_all(port);
+       ipc_pset_remove_from_all(port);
 
        if (nset != IPS_NULL) {
                ips_lock(nset);
 
        if (nset != IPS_NULL) {
                ips_lock(nset);
@@ -1335,9 +1299,6 @@ mach_port_request_notification(
        ipc_port_t              *previousp)
 {
        kern_return_t kr;
        ipc_port_t              *previousp)
 {
        kern_return_t kr;
-       ipc_entry_t entry;      
-       ipc_port_t port;
-       
 
        if (space == IS_NULL)
                return KERN_INVALID_TASK;
 
        if (space == IS_NULL)
                return KERN_INVALID_TASK;
@@ -1349,18 +1310,23 @@ mach_port_request_notification(
        /*
         *      Requesting notifications on RPC ports is an error.
         */
        /*
         *      Requesting notifications on RPC ports is an error.
         */
-       kr = ipc_right_lookup_write(space, name, &entry);       
-       if (kr != KERN_SUCCESS)
-               return kr;
+       {
+               ipc_port_t port;
+               ipc_entry_t entry;      
 
 
-       port = (ipc_port_t) entry->ie_object;
+               kr = ipc_right_lookup_write(space, name, &entry);       
+               if (kr != KERN_SUCCESS)
+                       return kr;
 
 
-       if (port->ip_subsystem != NULL) {
+               port = (ipc_port_t) entry->ie_object;
+
+               if (port->ip_subsystem != NULL) {
+                       is_write_unlock(space);
+                       panic("mach_port_request_notification: on RPC port!!"); 
+                       return KERN_INVALID_CAPABILITY;
+               }
                is_write_unlock(space);
                is_write_unlock(space);
-               panic("mach_port_request_notification: on RPC port!!"); 
-               return KERN_INVALID_CAPABILITY;
        }
        }
-       is_write_unlock(space);
 #endif         /* NOTYET */
 
 
 #endif         /* NOTYET */
 
 
@@ -1761,7 +1727,6 @@ mach_port_extract_member(
        mach_port_name_t        name,
        mach_port_name_t        psname)
 {
        mach_port_name_t        name,
        mach_port_name_t        psname)
 {
-       mach_port_name_t oldname;
        ipc_object_t psobj;
        ipc_object_t obj;
        kern_return_t kr;
        ipc_object_t psobj;
        ipc_object_t obj;
        kern_return_t kr;
@@ -1788,3 +1753,22 @@ mach_port_extract_member(
        return kr;
 }
 
        return kr;
 }
 
+/*
+ *     task_set_port_space:
+ *
+ *     Set port name space of task to specified size.
+ */
+kern_return_t
+task_set_port_space(
+       ipc_space_t     space,
+       int             table_entries)
+{
+       kern_return_t kr;
+       
+       is_write_lock(space);
+       kr = ipc_entry_grow_table(space, table_entries);
+       if (kr == KERN_SUCCESS)
+               is_write_unlock(space);
+       return kr;
+}
+