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>
53 /* Device VM COMPONENT INTERFACES */
61 /* until component support available */
65 /* until component support available */
66 const struct memory_object_pager_ops device_pager_ops
= {
67 device_pager_reference
,
68 device_pager_deallocate
,
70 device_pager_terminate
,
71 device_pager_data_request
,
72 device_pager_data_return
,
73 device_pager_data_initialize
,
74 device_pager_data_unlock
,
75 device_pager_synchronize
,
80 typedef int device_port_t
;
83 * The start of "struct device_pager" MUST match a "struct memory_object".
85 typedef struct device_pager
{
86 memory_object_pager_ops_t pager_ops
; /* == &device_pager_ops */
87 unsigned int pager_ikot
; /* fake ip_kotype() */
88 unsigned int ref_count
; /* reference count */
89 memory_object_control_t control_handle
; /* mem object's cntrl handle */
90 device_port_t device_handle
; /* device_handle */
99 device_pager_lookup( /* forward */
103 device_object_create(void); /* forward */
105 zone_t device_pager_zone
;
108 #define DEVICE_PAGER_NULL ((device_pager_t) 0)
111 #define MAX_DNODE 10000
121 device_pager_bootstrap(void)
123 register vm_size_t size
;
125 size
= (vm_size_t
) sizeof(struct device_pager
);
126 device_pager_zone
= zinit(size
, (vm_size_t
) MAX_DNODE
*size
,
127 PAGE_SIZE
, "device node pager structures");
137 __unused memory_object_t device
,
142 device_pager_t device_object
;
144 device_object
= device_object_create();
145 if (device_object
== DEVICE_PAGER_NULL
)
146 panic("device_pager_setup: device_object_create() failed");
148 device_object
->device_handle
= device_handle
;
149 device_object
->size
= size
;
150 device_object
->flags
= flags
;
152 return((memory_object_t
)device_object
);
159 device_pager_populate_object(
160 memory_object_t device
,
161 memory_object_offset_t offset
,
165 device_pager_t device_object
;
166 vm_object_t vm_object
;
170 device_object
= device_pager_lookup(device
);
171 if(device_object
== DEVICE_PAGER_NULL
)
174 vm_object
= (vm_object_t
)memory_object_control_to_vm_object(
175 device_object
->control_handle
);
176 if(vm_object
== NULL
)
179 kr
= vm_object_populate_with_private(
180 vm_object
, offset
, page_num
, size
);
181 if(kr
!= KERN_SUCCESS
)
184 if(!vm_object
->phys_contiguous
) {
185 unsigned int null_size
= 0;
186 kr
= vm_object_upl_request(vm_object
,
187 (vm_object_offset_t
)offset
, size
, &upl
, NULL
,
188 &null_size
, (UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
));
190 if(kr
!= KERN_SUCCESS
)
191 panic("device_pager_populate_object: list_req failed");
193 upl_commit(upl
, NULL
, 0);
206 memory_object_t name
)
208 device_pager_t device_object
;
210 device_object
= (device_pager_t
)name
;
211 assert(device_object
->pager_ops
== &device_pager_ops
);
212 return (device_object
);
220 memory_object_t mem_obj
,
221 memory_object_control_t control
,
222 __unused vm_size_t pg_size
)
224 device_pager_t device_object
;
226 memory_object_attr_info_data_t attributes
;
228 vm_object_t vm_object
;
231 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
232 return KERN_INVALID_ARGUMENT
;
234 device_object
= device_pager_lookup(mem_obj
);
236 memory_object_control_reference(control
);
237 device_object
->control_handle
= control
;
240 /* The following settings should be done through an expanded change */
241 /* attributes call */
243 vm_object
= (vm_object_t
)memory_object_control_to_vm_object(control
);
244 vm_object_lock(vm_object
);
245 vm_object
->private = TRUE
;
246 if(device_object
->flags
& DEVICE_PAGER_CONTIGUOUS
)
247 vm_object
->phys_contiguous
= TRUE
;
248 if(device_object
->flags
& DEVICE_PAGER_NOPHYSCACHE
)
249 vm_object
->nophyscache
= TRUE
;
251 vm_object
->wimg_bits
= device_object
->flags
& VM_WIMG_MASK
;
252 vm_object_unlock(vm_object
);
255 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
256 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
257 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
258 attributes
.may_cache_object
= FALSE
;
259 attributes
.temporary
= TRUE
;
261 kr
= memory_object_change_attributes(
263 MEMORY_OBJECT_ATTRIBUTE_INFO
,
264 (memory_object_info_t
) &attributes
,
265 MEMORY_OBJECT_ATTR_INFO_COUNT
);
266 if (kr
!= KERN_SUCCESS
)
267 panic("device_pager_init: memory_object_change_attributes() failed");
269 return(KERN_SUCCESS
);
277 device_pager_data_return(
278 memory_object_t mem_obj
,
279 memory_object_offset_t offset
,
281 __unused memory_object_offset_t
*resid_offset
,
282 __unused
int *io_error
,
283 __unused boolean_t dirty
,
284 __unused boolean_t kernel_copy
,
285 __unused
int upl_flags
)
287 device_pager_t device_object
;
289 device_object
= device_pager_lookup(mem_obj
);
290 if (device_object
== DEVICE_PAGER_NULL
)
291 panic("device_pager_data_return: lookup failed");
293 return device_data_action(device_object
->device_handle
,
294 (ipc_port_t
) device_object
,
295 VM_PROT_READ
| VM_PROT_WRITE
,
303 device_pager_data_request(
304 memory_object_t mem_obj
,
305 memory_object_offset_t offset
,
307 __unused vm_prot_t protection_required
,
308 __unused memory_object_fault_info_t fault_info
)
310 device_pager_t device_object
;
312 device_object
= device_pager_lookup(mem_obj
);
314 if (device_object
== DEVICE_PAGER_NULL
)
315 panic("device_pager_data_request: lookup failed");
317 device_data_action(device_object
->device_handle
,
318 (ipc_port_t
) device_object
,
319 VM_PROT_READ
, offset
, length
);
327 device_pager_reference(
328 memory_object_t mem_obj
)
330 device_pager_t device_object
;
331 unsigned int new_ref_count
;
333 device_object
= device_pager_lookup(mem_obj
);
334 new_ref_count
= hw_atomic_add(&device_object
->ref_count
, 1);
335 assert(new_ref_count
> 1);
342 device_pager_deallocate(
343 memory_object_t mem_obj
)
345 device_pager_t device_object
;
346 memory_object_control_t device_control
;
348 device_object
= device_pager_lookup(mem_obj
);
350 if (hw_atomic_sub(&device_object
->ref_count
, 1) == 0) {
351 if (device_object
->device_handle
!= (device_port_t
) NULL
) {
352 device_close(device_object
->device_handle
);
353 device_object
->device_handle
= (device_port_t
) NULL
;
355 device_control
= device_object
->control_handle
;
356 if (device_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
358 * The VM object should already have been disconnected
359 * from the pager at this point.
360 * We still have to release the "memory object control"
363 assert(device_control
->moc_object
== VM_OBJECT_NULL
);
364 memory_object_control_deallocate(device_control
);
365 device_object
->control_handle
=
366 MEMORY_OBJECT_CONTROL_NULL
;
369 zfree(device_pager_zone
, device_object
);
375 device_pager_data_initialize(
376 __unused memory_object_t mem_obj
,
377 __unused memory_object_offset_t offset
,
378 __unused vm_size_t data_cnt
)
380 panic("device_pager_data_initialize");
385 device_pager_data_unlock(
386 __unused memory_object_t mem_obj
,
387 __unused memory_object_offset_t offset
,
388 __unused vm_size_t size
,
389 __unused vm_prot_t desired_access
)
395 device_pager_terminate(
396 __unused memory_object_t mem_obj
)
407 device_pager_synchronize(
408 memory_object_t mem_obj
,
409 memory_object_offset_t offset
,
411 __unused vm_sync_t sync_flags
)
413 device_pager_t device_object
;
415 device_object
= device_pager_lookup(mem_obj
);
417 memory_object_synchronize_completed(
418 device_object
->control_handle
, offset
, length
);
428 __unused memory_object_t mem_obj
)
439 device_object_create(void)
441 register device_pager_t device_object
;
443 device_object
= (struct device_pager
*) zalloc(device_pager_zone
);
444 if (device_object
== DEVICE_PAGER_NULL
)
445 return(DEVICE_PAGER_NULL
);
446 device_object
->pager_ops
= &device_pager_ops
;
447 device_object
->pager_ikot
= IKOT_MEMORY_OBJECT
;
448 device_object
->ref_count
= 1;
449 device_object
->control_handle
= MEMORY_OBJECT_CONTROL_NULL
;
452 return(device_object
);