2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/errno.h>
31 #include <mach/mach_types.h>
32 #include <mach/kern_return.h>
33 #include <mach/memory_object_control.h>
34 #include <mach/memory_object_types.h>
35 #include <mach/port.h>
36 #include <mach/policy.h>
38 #include <kern/kern_types.h>
39 #include <kern/ipc_kobject.h>
40 #include <kern/host.h>
41 #include <kern/thread.h>
42 #include <ipc/ipc_port.h>
43 #include <ipc/ipc_space.h>
44 #include <device/device_port.h>
45 #include <vm/memory_object.h>
46 #include <vm/vm_pageout.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_pageout.h>
50 #include <vm/vm_protos.h>
52 #include <os/refcnt.h>
55 /* Device VM COMPONENT INTERFACES */
63 /* until component support available */
67 /* until component support available */
68 const struct memory_object_pager_ops device_pager_ops
= {
69 device_pager_reference
,
70 device_pager_deallocate
,
72 device_pager_terminate
,
73 device_pager_data_request
,
74 device_pager_data_return
,
75 device_pager_data_initialize
,
76 device_pager_data_unlock
,
77 device_pager_synchronize
,
79 device_pager_last_unmap
,
80 NULL
, /* data_reclaim */
84 typedef uintptr_t device_port_t
;
87 * The start of "struct device_pager" MUST match a "struct memory_object".
89 typedef struct device_pager
{
90 /* mandatory generic header */
91 struct memory_object dev_pgr_hdr
;
93 /* pager-specific data */
95 struct os_refcnt ref_count
; /* reference count */
96 device_port_t device_handle
; /* device_handle */
102 lck_grp_t device_pager_lck_grp
;
103 lck_grp_attr_t device_pager_lck_grp_attr
;
104 lck_attr_t device_pager_lck_attr
;
106 #define device_pager_lock_init(pager) \
107 lck_mtx_init(&(pager)->lock, \
108 &device_pager_lck_grp, \
109 &device_pager_lck_attr)
110 #define device_pager_lock_destroy(pager) \
111 lck_mtx_destroy(&(pager)->lock, &device_pager_lck_grp)
112 #define device_pager_lock(pager) lck_mtx_lock(&(pager)->lock)
113 #define device_pager_unlock(pager) lck_mtx_unlock(&(pager)->lock)
116 device_pager_lookup( /* forward */
120 device_object_create(void); /* forward */
122 zone_t device_pager_zone
;
125 #define DEVICE_PAGER_NULL ((device_pager_t) 0)
128 #define MAX_DNODE 10000
138 device_pager_bootstrap(void)
142 size
= (vm_size_t
) sizeof(struct device_pager
);
143 device_pager_zone
= zinit(size
, (vm_size_t
) MAX_DNODE
* size
,
144 PAGE_SIZE
, "device node pager structures");
145 zone_change(device_pager_zone
, Z_CALLERACCT
, FALSE
);
147 lck_grp_attr_setdefault(&device_pager_lck_grp_attr
);
148 lck_grp_init(&device_pager_lck_grp
, "device_pager", &device_pager_lck_grp_attr
);
149 lck_attr_setdefault(&device_pager_lck_attr
);
159 __unused memory_object_t device
,
160 uintptr_t device_handle
,
164 device_pager_t device_object
;
165 memory_object_control_t control
;
168 device_object
= device_object_create();
169 if (device_object
== DEVICE_PAGER_NULL
) {
170 panic("device_pager_setup: device_object_create() failed");
173 device_object
->device_handle
= device_handle
;
174 device_object
->size
= size
;
175 device_object
->flags
= flags
;
177 memory_object_create_named((memory_object_t
) device_object
,
180 object
= memory_object_control_to_vm_object(control
);
182 assert(object
!= VM_OBJECT_NULL
);
183 vm_object_lock(object
);
184 object
->true_share
= TRUE
;
185 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
) {
186 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
188 vm_object_unlock(object
);
190 return (memory_object_t
)device_object
;
197 device_pager_populate_object(
198 memory_object_t device
,
199 memory_object_offset_t offset
,
203 device_pager_t device_object
;
204 vm_object_t vm_object
;
208 device_object
= device_pager_lookup(device
);
209 if (device_object
== DEVICE_PAGER_NULL
) {
213 vm_object
= (vm_object_t
)memory_object_control_to_vm_object(
214 device_object
->dev_pgr_hdr
.mo_control
);
215 if (vm_object
== NULL
) {
219 kr
= vm_object_populate_with_private(
220 vm_object
, offset
, page_num
, size
);
221 if (kr
!= KERN_SUCCESS
) {
225 if (!vm_object
->phys_contiguous
) {
226 unsigned int null_size
= 0;
227 assert((upl_size_t
) size
== size
);
228 kr
= vm_object_upl_request(vm_object
,
229 (vm_object_offset_t
)offset
,
230 (upl_size_t
) size
, &upl
, NULL
,
232 (UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
),
233 VM_KERN_MEMORY_NONE
);
234 if (kr
!= KERN_SUCCESS
) {
235 panic("device_pager_populate_object: list_req failed");
238 upl_commit(upl
, NULL
, 0);
251 memory_object_t mem_obj
)
253 device_pager_t device_object
;
255 assert(mem_obj
->mo_pager_ops
== &device_pager_ops
);
256 device_object
= (device_pager_t
)mem_obj
;
257 assert(os_ref_get_count(&device_object
->ref_count
) > 0);
258 return device_object
;
266 memory_object_t mem_obj
,
267 memory_object_control_t control
,
268 __unused memory_object_cluster_size_t pg_size
)
270 device_pager_t device_object
;
272 memory_object_attr_info_data_t attributes
;
274 vm_object_t vm_object
;
277 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
278 return KERN_INVALID_ARGUMENT
;
281 device_object
= device_pager_lookup(mem_obj
);
283 memory_object_control_reference(control
);
284 device_object
->dev_pgr_hdr
.mo_control
= control
;
287 /* The following settings should be done through an expanded change */
288 /* attributes call */
290 vm_object
= (vm_object_t
)memory_object_control_to_vm_object(control
);
291 vm_object_lock(vm_object
);
292 vm_object
->private = TRUE
;
293 if (device_object
->flags
& DEVICE_PAGER_CONTIGUOUS
) {
294 vm_object
->phys_contiguous
= TRUE
;
296 if (device_object
->flags
& DEVICE_PAGER_NOPHYSCACHE
) {
297 vm_object
->nophyscache
= TRUE
;
300 vm_object
->wimg_bits
= device_object
->flags
& VM_WIMG_MASK
;
301 vm_object_unlock(vm_object
);
304 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
305 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
306 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
307 attributes
.may_cache_object
= FALSE
;
308 attributes
.temporary
= TRUE
;
310 kr
= memory_object_change_attributes(
312 MEMORY_OBJECT_ATTRIBUTE_INFO
,
313 (memory_object_info_t
) &attributes
,
314 MEMORY_OBJECT_ATTR_INFO_COUNT
);
315 if (kr
!= KERN_SUCCESS
) {
316 panic("device_pager_init: memory_object_change_attributes() failed");
327 device_pager_data_return(
328 memory_object_t mem_obj
,
329 memory_object_offset_t offset
,
330 memory_object_cluster_size_t data_cnt
,
331 __unused memory_object_offset_t
*resid_offset
,
332 __unused
int *io_error
,
333 __unused boolean_t dirty
,
334 __unused boolean_t kernel_copy
,
335 __unused
int upl_flags
)
337 device_pager_t device_object
;
339 device_object
= device_pager_lookup(mem_obj
);
340 if (device_object
== DEVICE_PAGER_NULL
) {
341 panic("device_pager_data_return: lookup failed");
344 __IGNORE_WCASTALIGN(return device_data_action(device_object
->device_handle
,
345 (ipc_port_t
) device_object
,
346 VM_PROT_READ
| VM_PROT_WRITE
,
354 device_pager_data_request(
355 memory_object_t mem_obj
,
356 memory_object_offset_t offset
,
357 memory_object_cluster_size_t length
,
358 __unused vm_prot_t protection_required
,
359 __unused memory_object_fault_info_t fault_info
)
361 device_pager_t device_object
;
363 device_object
= device_pager_lookup(mem_obj
);
365 if (device_object
== DEVICE_PAGER_NULL
) {
366 panic("device_pager_data_request: lookup failed");
369 __IGNORE_WCASTALIGN(device_data_action(device_object
->device_handle
,
370 (ipc_port_t
) device_object
,
371 VM_PROT_READ
, offset
, length
));
379 device_pager_reference(
380 memory_object_t mem_obj
)
382 device_pager_t device_object
;
384 device_object
= device_pager_lookup(mem_obj
);
385 os_ref_retain(&device_object
->ref_count
);
387 DTRACE_VM2(device_pager_reference
,
388 device_pager_t
, device_object
,
389 unsigned int, os_ref_get_count(&device_object
->ref_count
));
396 device_pager_deallocate(
397 memory_object_t mem_obj
)
399 device_pager_t device_object
;
400 memory_object_control_t device_control
;
402 device_object
= device_pager_lookup(mem_obj
);
404 DTRACE_VM2(device_pager_deallocate
,
405 device_pager_t
, device_object
,
406 unsigned int, os_ref_get_count(&device_object
->ref_count
));
408 os_ref_count_t ref_count
= os_ref_release(&device_object
->ref_count
);
410 if (ref_count
== 1) {
412 * The last reference is our "named" reference.
413 * Close the device and "destroy" the VM object.
416 DTRACE_VM2(device_pager_destroy
,
417 device_pager_t
, device_object
,
418 unsigned int, os_ref_get_count(&device_object
->ref_count
));
420 assert(device_object
->is_mapped
== FALSE
);
421 if (device_object
->device_handle
!= (device_port_t
) NULL
) {
422 device_close(device_object
->device_handle
);
423 device_object
->device_handle
= (device_port_t
) NULL
;
425 device_control
= device_object
->dev_pgr_hdr
.mo_control
;
426 memory_object_destroy(device_control
, 0);
427 } else if (ref_count
== 0) {
429 * No more references: free the pager.
431 DTRACE_VM2(device_pager_free
,
432 device_pager_t
, device_object
,
433 unsigned int, os_ref_get_count(&device_object
->ref_count
));
435 device_pager_lock_destroy(device_object
);
437 zfree(device_pager_zone
, device_object
);
443 device_pager_data_initialize(
444 __unused memory_object_t mem_obj
,
445 __unused memory_object_offset_t offset
,
446 __unused memory_object_cluster_size_t data_cnt
)
448 panic("device_pager_data_initialize");
453 device_pager_data_unlock(
454 __unused memory_object_t mem_obj
,
455 __unused memory_object_offset_t offset
,
456 __unused memory_object_size_t size
,
457 __unused vm_prot_t desired_access
)
463 device_pager_terminate(
464 __unused memory_object_t mem_obj
)
475 device_pager_synchronize(
476 __unused memory_object_t mem_obj
,
477 __unused memory_object_offset_t offset
,
478 __unused memory_object_size_t length
,
479 __unused vm_sync_t sync_flags
)
481 panic("device_pager_synchronize: memory_object_synchronize no longer supported\n");
490 memory_object_t mem_obj
,
491 __unused vm_prot_t prot
)
493 device_pager_t device_object
;
495 device_object
= device_pager_lookup(mem_obj
);
497 device_pager_lock(device_object
);
498 assert(os_ref_get_count(&device_object
->ref_count
) > 0);
499 if (device_object
->is_mapped
== FALSE
) {
501 * First mapping of this pager: take an extra reference
502 * that will remain until all the mappings of this pager
505 device_object
->is_mapped
= TRUE
;
506 device_pager_reference(mem_obj
);
508 device_pager_unlock(device_object
);
514 device_pager_last_unmap(
515 memory_object_t mem_obj
)
517 device_pager_t device_object
;
520 device_object
= device_pager_lookup(mem_obj
);
522 device_pager_lock(device_object
);
523 assert(os_ref_get_count(&device_object
->ref_count
) > 0);
524 if (device_object
->is_mapped
) {
525 device_object
->is_mapped
= FALSE
;
530 device_pager_unlock(device_object
);
533 device_pager_deallocate(mem_obj
);
545 device_object_create(void)
547 device_pager_t device_object
;
549 device_object
= (struct device_pager
*) zalloc(device_pager_zone
);
550 if (device_object
== DEVICE_PAGER_NULL
) {
551 return DEVICE_PAGER_NULL
;
554 bzero(device_object
, sizeof(*device_object
));
556 device_object
->dev_pgr_hdr
.mo_ikot
= IKOT_MEMORY_OBJECT
;
557 device_object
->dev_pgr_hdr
.mo_pager_ops
= &device_pager_ops
;
558 device_object
->dev_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
560 device_pager_lock_init(device_object
);
561 os_ref_init(&device_object
->ref_count
, NULL
);
562 device_object
->is_mapped
= FALSE
;
564 DTRACE_VM2(device_pager_create
,
565 device_pager_t
, device_object
,
566 unsigned int, os_ref_get_count(&device_object
->ref_count
));
568 return device_object
;
572 is_device_pager_ops(const struct memory_object_pager_ops
*pager_ops
)
574 if (pager_ops
== &device_pager_ops
) {