]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/vm/device_vm.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / osfmk / vm / device_vm.c
index a6636ce12a20794be26c6d7020369040a29e6984..c4f953e1caf758292171fa9f03ab8cc64e121563 100644 (file)
@@ -1,16 +1,19 @@
 /*
- * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
- * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * 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.
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ * 
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
 #include <sys/errno.h>
-#include <kern/host.h>
+
 #include <mach/mach_types.h>
-#include <vm/vm_map.h>
-#include <vm/vm_kern.h>
-#include <vm/vm_pageout.h>
 #include <mach/kern_return.h>
+#include <mach/memory_object_control.h>
 #include <mach/memory_object_types.h>
 #include <mach/port.h>
 #include <mach/policy.h>
+#include <mach/upl.h>
+#include <kern/kern_types.h>
+#include <kern/ipc_kobject.h>
+#include <kern/host.h>
+#include <kern/thread.h>
 #include <ipc/ipc_port.h>
 #include <ipc/ipc_space.h>
-#include <kern/thread.h>
 #include <device/device_port.h>
+#include <vm/memory_object.h>
 #include <vm/vm_pageout.h>
+#include <vm/vm_map.h>
+#include <vm/vm_kern.h>
+#include <vm/vm_pageout.h>
+#include <vm/vm_protos.h>
+#include <mach/sdt.h>
+
 
 /* Device VM COMPONENT INTERFACES */
 
 
 
 /* until component support available */
-int    device_pager_workaround;
-
-typedef int device_port_t;
+const struct memory_object_pager_ops device_pager_ops = {
+       device_pager_reference,
+       device_pager_deallocate,
+       device_pager_init,
+       device_pager_terminate,
+       device_pager_data_request,
+       device_pager_data_return,
+       device_pager_data_initialize,
+       device_pager_data_unlock,
+       device_pager_synchronize,
+       device_pager_map,
+       device_pager_last_unmap,
+       NULL, /* data_reclaim */
+       "device pager"
+};
+
+typedef uintptr_t device_port_t;
 
+/*
+ * The start of "struct device_pager" MUST match a "struct memory_object".
+ */
 typedef struct device_pager {
-       int             *pager;         /* pager workaround pointer     */
-       unsigned int    pager_ikot;     /* fake ip_kotype()             */
+       /* mandatory generic header */
+       struct memory_object dev_pgr_hdr;
+
+       /* pager-specific data */
+       lck_mtx_t       lock;
        unsigned int    ref_count;      /* reference count              */
-       memory_object_control_t control_handle; /* mem object's cntrl handle */
        device_port_t   device_handle;  /* device_handle */
        vm_size_t       size;
        int             flags;
+       boolean_t       is_mapped;
 } *device_pager_t;
 
+lck_grp_t      device_pager_lck_grp;
+lck_grp_attr_t device_pager_lck_grp_attr;
+lck_attr_t     device_pager_lck_attr;
 
-
-void 
-device_pager_bootstrap(
-       void);
-
-
-memory_object_t
-device_pager_setup(
-       memory_object_t,
-       int,
-       vm_size_t,
-       int);
+#define device_pager_lock_init(pager)                          \
+       lck_mtx_init(&(pager)->lock,                            \
+                    &device_pager_lck_grp,                     \
+                    &device_pager_lck_attr)
+#define device_pager_lock_destroy(pager)                       \
+       lck_mtx_destroy(&(pager)->lock, &device_pager_lck_grp)
+#define device_pager_lock(pager) lck_mtx_lock(&(pager)->lock)
+#define device_pager_unlock(pager) lck_mtx_unlock(&(pager)->lock)
 
 device_pager_t
-device_pager_lookup(
-       memory_object_t);
-
-kern_return_t
-device_pager_init(
-       memory_object_t, 
-       memory_object_control_t, 
-       vm_size_t);
-
-
-kern_return_t
-device_pager_data_request( 
-       memory_object_t, 
-       memory_object_offset_t,
-       vm_size_t, 
-       vm_prot_t);
-
-kern_return_t
-device_pager_data_return(
-       memory_object_t,
-       memory_object_offset_t,
-       vm_size_t,
-       boolean_t,
-       boolean_t);
-
-void
-device_pager_reference(
-       memory_object_t);
-
-void
-device_pager_deallocate(
-       memory_object_t);
-
-kern_return_t
-device_pager_data_initialize(
-       memory_object_t,
-       memory_object_offset_t,
-       vm_size_t);
-
-kern_return_t
-device_pager_data_unlock(
-       memory_object_t,
-       memory_object_offset_t,
-       vm_size_t,
-       vm_prot_t);
-
-kern_return_t
-device_pager_terminate(
-       memory_object_t);
-
-kern_return_t
-device_pager_synchronize(
-       memory_object_t,
-       memory_object_offset_t,
-       vm_offset_t,
-       vm_sync_t);
-
-kern_return_t
-device_pager_unmap(
+device_pager_lookup(           /* forward */
        memory_object_t);
 
 device_pager_t
-device_object_create(void);
+device_object_create(void);    /* forward */
 
 zone_t device_pager_zone;
 
@@ -163,11 +136,16 @@ zone_t    device_pager_zone;
 void
 device_pager_bootstrap(void)
 {
-       register vm_size_t      size;
+       vm_size_t      size;
 
        size = (vm_size_t) sizeof(struct device_pager);
        device_pager_zone = zinit(size, (vm_size_t) MAX_DNODE*size,
                                PAGE_SIZE, "device node pager structures");
+       zone_change(device_pager_zone, Z_CALLERACCT, FALSE);
+
+       lck_grp_attr_setdefault(&device_pager_lck_grp_attr);
+       lck_grp_init(&device_pager_lck_grp, "device_pager", &device_pager_lck_grp_attr);
+       lck_attr_setdefault(&device_pager_lck_attr);
 
        return;
 }
@@ -177,12 +155,14 @@ device_pager_bootstrap(void)
  */
 memory_object_t
 device_pager_setup(
-       memory_object_t device,
-       int             device_handle,
+       __unused memory_object_t device,
+       uintptr_t               device_handle,
        vm_size_t       size,
        int             flags)
 {
        device_pager_t  device_object;
+       memory_object_control_t control;
+       vm_object_t     object;
 
        device_object = device_object_create();
        if (device_object == DEVICE_PAGER_NULL)
@@ -192,7 +172,20 @@ device_pager_setup(
        device_object->size = size;
        device_object->flags = flags;
 
-       return((memory_object_t)device_object);
+       memory_object_create_named((memory_object_t) device_object,
+                                  size,
+                                  &control);
+       object = memory_object_control_to_vm_object(control);
+
+       assert(object != VM_OBJECT_NULL);
+       vm_object_lock(object);
+       object->true_share = TRUE;
+       if (object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC) {
+               object->copy_strategy = MEMORY_OBJECT_COPY_DELAY;
+       }
+       vm_object_unlock(object);
+
+       return (memory_object_t)device_object;
 }
 
 /*
@@ -202,39 +195,41 @@ kern_return_t
 device_pager_populate_object(
        memory_object_t         device,
        memory_object_offset_t  offset,
-       vm_offset_t             phys_addr,
+       ppnum_t                 page_num,
        vm_size_t               size)
 {
        device_pager_t  device_object;
        vm_object_t     vm_object;
        kern_return_t   kr;
        upl_t           upl;
-       ipc_port_t      previous;
 
        device_object = device_pager_lookup(device);
        if(device_object == DEVICE_PAGER_NULL)
                return KERN_FAILURE;
 
        vm_object = (vm_object_t)memory_object_control_to_vm_object(
-                                       device_object->control_handle);
+                                       device_object->dev_pgr_hdr.mo_control);
        if(vm_object == NULL) 
                return KERN_FAILURE;
 
        kr =  vm_object_populate_with_private(
-                               vm_object, offset, phys_addr, size);
+                               vm_object, offset, page_num, size);
        if(kr != KERN_SUCCESS)
                return kr;
 
        if(!vm_object->phys_contiguous) {
-               int null_size = 0;
+               unsigned int null_size = 0;
+               assert((upl_size_t) size == size);
                kr = vm_object_upl_request(vm_object,
-                       (vm_object_offset_t)offset, size, &upl,  NULL,
-                       &null_size, (UPL_NO_SYNC | UPL_CLEAN_IN_PLACE)); 
-
+                                          (vm_object_offset_t)offset,
+                                          (upl_size_t) size, &upl,  NULL,
+                                          &null_size,
+                                          (UPL_NO_SYNC | UPL_CLEAN_IN_PLACE),
+                                          VM_KERN_MEMORY_NONE);
                if(kr != KERN_SUCCESS)
                        panic("device_pager_populate_object: list_req failed");
 
-               upl_commit(upl, NULL);
+               upl_commit(upl, NULL, 0);
                upl_deallocate(upl);
        }
 
@@ -247,22 +242,24 @@ device_pager_populate_object(
  */
 device_pager_t
 device_pager_lookup(
-       memory_object_t name)
+       memory_object_t mem_obj)
 {
        device_pager_t  device_object;
 
-       device_object = (device_pager_t)name;
-       assert(device_object->pager == &device_pager_workaround);
-       return (device_object);
+       assert(mem_obj->mo_pager_ops == &device_pager_ops);
+       device_object = (device_pager_t)mem_obj;
+       assert(device_object->ref_count > 0);
+       return device_object;
 }
 
 /*
  *
  */
 kern_return_t
-device_pager_init(memory_object_t mem_obj, 
-               memory_object_control_t control, 
-               vm_size_t pg_size)
+device_pager_init(
+       memory_object_t mem_obj, 
+       memory_object_control_t control, 
+       __unused memory_object_cluster_size_t pg_size)
 {
        device_pager_t   device_object;
        kern_return_t   kr;
@@ -277,7 +274,7 @@ device_pager_init(memory_object_t mem_obj,
        device_object = device_pager_lookup(mem_obj);
 
        memory_object_control_reference(control);
-       device_object->control_handle = control;
+       device_object->dev_pgr_hdr.mo_control = control;
 
 
 /* The following settings should be done through an expanded change */
@@ -315,13 +312,17 @@ device_pager_init(memory_object_t mem_obj,
 /*
  *
  */
+/*ARGSUSED6*/
 kern_return_t
 device_pager_data_return(
-        memory_object_t                mem_obj,
-        memory_object_offset_t offset,
-        vm_size_t              data_cnt,
-        boolean_t              dirty,
-        boolean_t              kernel_copy)  
+       memory_object_t                 mem_obj,
+       memory_object_offset_t          offset,
+       memory_object_cluster_size_t                    data_cnt,
+       __unused memory_object_offset_t *resid_offset,
+       __unused int                    *io_error,
+       __unused boolean_t              dirty,
+       __unused boolean_t              kernel_copy,
+       __unused int                    upl_flags)  
 {
        device_pager_t  device_object;
 
@@ -329,8 +330,10 @@ device_pager_data_return(
        if (device_object == DEVICE_PAGER_NULL)
                panic("device_pager_data_return: lookup failed");
 
-       return device_data_action(device_object->device_handle, device_object,
-                       VM_PROT_READ | VM_PROT_WRITE, offset, data_cnt);
+       __IGNORE_WCASTALIGN(return device_data_action(device_object->device_handle,
+                         (ipc_port_t) device_object,
+                         VM_PROT_READ | VM_PROT_WRITE,
+                         offset, data_cnt));
 }
 
 /*
@@ -340,8 +343,9 @@ kern_return_t
 device_pager_data_request(
        memory_object_t         mem_obj,
        memory_object_offset_t  offset,
-       vm_size_t               length,
-       vm_prot_t               protection_required)
+       memory_object_cluster_size_t            length,
+       __unused vm_prot_t      protection_required,
+        __unused memory_object_fault_info_t    fault_info)
 {
        device_pager_t  device_object;
 
@@ -350,8 +354,9 @@ device_pager_data_request(
        if (device_object == DEVICE_PAGER_NULL)
                panic("device_pager_data_request: lookup failed");
 
-       device_data_action(device_object->device_handle, device_object,
-                                       VM_PROT_READ, offset, length);
+       __IGNORE_WCASTALIGN(device_data_action(device_object->device_handle,
+                          (ipc_port_t) device_object,
+                          VM_PROT_READ, offset, length));
        return KERN_SUCCESS;
 }
 
@@ -368,6 +373,10 @@ device_pager_reference(
        device_object = device_pager_lookup(mem_obj);
        new_ref_count = hw_atomic_add(&device_object->ref_count, 1);
        assert(new_ref_count > 1);
+       DTRACE_VM2(device_pager_reference,
+                  device_pager_t, device_object,
+                  unsigned int, device_object->ref_count);
+
 }
 
 /*
@@ -377,40 +386,74 @@ void
 device_pager_deallocate(
        memory_object_t         mem_obj)
 {
-       device_pager_t  device_object;
+       device_pager_t          device_object;
+       memory_object_control_t device_control;
+       unsigned int            ref_count;
 
        device_object = device_pager_lookup(mem_obj);
+       assert(device_object->ref_count > 0);
+
+       DTRACE_VM2(device_pager_deallocate,
+                  device_pager_t, device_object,
+                  unsigned int, device_object->ref_count);
+
+       ref_count = hw_atomic_sub(&device_object->ref_count, 1);
+
+       if (ref_count == 1) {
+               /*
+                * The last reference is our "named" reference.
+                * Close the device and "destroy" the VM object.
+                */
 
-       if (hw_atomic_sub(&device_object->ref_count, 1) == 0) {
+               DTRACE_VM2(device_pager_destroy,
+                          device_pager_t, device_object,
+                          unsigned int, device_object->ref_count);
+
+               assert(device_object->is_mapped == FALSE);
                if (device_object->device_handle != (device_port_t) NULL) {
                        device_close(device_object->device_handle);
+                       device_object->device_handle = (device_port_t) NULL;
                }
-               zfree(device_pager_zone, (vm_offset_t) device_object);
+               device_control = device_object->dev_pgr_hdr.mo_control;
+               memory_object_destroy(device_control, 0);
+       } else if (ref_count == 0) {
+               /*
+                * No more references: free the pager.
+                */
+               DTRACE_VM2(device_pager_free,
+                          device_pager_t, device_object,
+                          unsigned int, device_object->ref_count);
+
+               device_pager_lock_destroy(device_object);
+
+               zfree(device_pager_zone, device_object);
        }
        return;
 }
 
 kern_return_t
 device_pager_data_initialize(
-        memory_object_t                mem_obj,
-        memory_object_offset_t offset,
-        vm_size_t              data_cnt)
+        __unused memory_object_t               mem_obj,
+        __unused memory_object_offset_t        offset,
+        __unused memory_object_cluster_size_t          data_cnt)
 {
+       panic("device_pager_data_initialize");
        return KERN_FAILURE;
 }
 
 kern_return_t
 device_pager_data_unlock(
-       memory_object_t         mem_obj,
-       memory_object_offset_t  offset,
-       vm_size_t               size,
-       vm_prot_t               desired_access)
+       __unused memory_object_t                mem_obj,
+       __unused memory_object_offset_t offset,
+       __unused memory_object_size_t           size,
+       __unused vm_prot_t              desired_access)
 {
        return KERN_FAILURE;
 }
 
+kern_return_t
 device_pager_terminate(
-       memory_object_t mem_obj)
+       __unused memory_object_t        mem_obj)
 {
        return KERN_SUCCESS;
 }
@@ -422,28 +465,66 @@ device_pager_terminate(
  */
 kern_return_t
 device_pager_synchronize(
-       memory_object_t         mem_obj,
-       memory_object_offset_t  offset,
-       vm_offset_t             length,
-       vm_sync_t               sync_flags)
+       __unused memory_object_t        mem_obj,
+       __unused memory_object_offset_t offset,
+       __unused memory_object_size_t   length,
+       __unused vm_sync_t              sync_flags)
 {
-       device_pager_t  device_object;
+       panic("device_pager_synchronize: memory_object_synchronize no longer supported\n");
+       return KERN_FAILURE;
+}
+
+/*
+ *
+ */
+kern_return_t
+device_pager_map(
+       memory_object_t mem_obj,
+       __unused vm_prot_t              prot)
+{
+       device_pager_t          device_object;
 
        device_object = device_pager_lookup(mem_obj);
 
-       memory_object_synchronize_completed(
-                       device_object->control_handle, offset, length);
+       device_pager_lock(device_object);
+       assert(device_object->ref_count > 0);
+       if (device_object->is_mapped == FALSE) {
+               /*
+                * First mapping of this pager: take an extra reference
+                * that will remain until all the mappings of this pager
+                * are removed.
+                */
+               device_object->is_mapped = TRUE;
+               device_pager_reference(mem_obj);
+       }
+       device_pager_unlock(device_object);
 
        return KERN_SUCCESS;
 }
 
-/*
- *
- */
 kern_return_t
-device_pager_unmap(
+device_pager_last_unmap(
        memory_object_t mem_obj)
 {
+       device_pager_t  device_object;
+       boolean_t       drop_ref;
+
+       device_object = device_pager_lookup(mem_obj);
+
+       device_pager_lock(device_object);
+       assert(device_object->ref_count > 0);
+       if (device_object->is_mapped) {
+               device_object->is_mapped = FALSE;
+               drop_ref = TRUE;
+       } else {
+               drop_ref = FALSE;
+       }
+       device_pager_unlock(device_object);
+
+       if (drop_ref) {
+               device_pager_deallocate(mem_obj);
+       }
+
        return KERN_SUCCESS;
 }
 
@@ -453,19 +534,36 @@ device_pager_unmap(
  *
  */
 device_pager_t
-device_object_create()
+device_object_create(void)
 {
-       register device_pager_t  device_object;
+       device_pager_t  device_object;
 
        device_object = (struct device_pager *) zalloc(device_pager_zone);
        if (device_object == DEVICE_PAGER_NULL)
                return(DEVICE_PAGER_NULL);
-       device_object->pager = &device_pager_workaround;
-       device_object->pager_ikot = IKOT_MEMORY_OBJECT;
+
+       bzero(device_object, sizeof (*device_object));
+
+       device_object->dev_pgr_hdr.mo_ikot = IKOT_MEMORY_OBJECT;
+       device_object->dev_pgr_hdr.mo_pager_ops = &device_pager_ops;
+       device_object->dev_pgr_hdr.mo_control = MEMORY_OBJECT_CONTROL_NULL;
+
+       device_pager_lock_init(device_object);
        device_object->ref_count = 1;
-       device_object->control_handle = MEMORY_OBJECT_CONTROL_NULL;
+       device_object->is_mapped = FALSE;
 
+       DTRACE_VM2(device_pager_create,
+                  device_pager_t, device_object,
+                  unsigned int, device_object->ref_count);
 
        return(device_object);
 }
 
+boolean_t
+is_device_pager_ops(const struct memory_object_pager_ops *pager_ops)
+{
+       if (pager_ops == &device_pager_ops) {
+               return TRUE;
+       }
+       return FALSE;
+}