]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/device_vm.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / osfmk / vm / device_vm.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <sys/errno.h>
30
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>
37 #include <mach/upl.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>
51 #include <mach/sdt.h>
52 #include <os/refcnt.h>
53
54
55 /* Device VM COMPONENT INTERFACES */
56
57
58 /*
59 * Device PAGER
60 */
61
62
63 /* until component support available */
64
65
66
67 /* until component support available */
68 const struct memory_object_pager_ops device_pager_ops = {
69 device_pager_reference,
70 device_pager_deallocate,
71 device_pager_init,
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,
78 device_pager_map,
79 device_pager_last_unmap,
80 NULL, /* data_reclaim */
81 "device pager"
82 };
83
84 typedef uintptr_t device_port_t;
85
86 /*
87 * The start of "struct device_pager" MUST match a "struct memory_object".
88 */
89 typedef struct device_pager {
90 /* mandatory generic header */
91 struct memory_object dev_pgr_hdr;
92
93 /* pager-specific data */
94 lck_mtx_t lock;
95 struct os_refcnt ref_count; /* reference count */
96 device_port_t device_handle; /* device_handle */
97 vm_size_t size;
98 int flags;
99 boolean_t is_mapped;
100 } *device_pager_t;
101
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;
105
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)
114
115 device_pager_t
116 device_pager_lookup( /* forward */
117 memory_object_t);
118
119 device_pager_t
120 device_object_create(void); /* forward */
121
122 zone_t device_pager_zone;
123
124
125 #define DEVICE_PAGER_NULL ((device_pager_t) 0)
126
127
128 #define MAX_DNODE 10000
129
130
131
132
133
134 /*
135 *
136 */
137 void
138 device_pager_bootstrap(void)
139 {
140 vm_size_t size;
141
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);
146
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);
150
151 return;
152 }
153
154 /*
155 *
156 */
157 memory_object_t
158 device_pager_setup(
159 __unused memory_object_t device,
160 uintptr_t device_handle,
161 vm_size_t size,
162 int flags)
163 {
164 device_pager_t device_object;
165 memory_object_control_t control;
166 vm_object_t object;
167
168 device_object = device_object_create();
169 if (device_object == DEVICE_PAGER_NULL) {
170 panic("device_pager_setup: device_object_create() failed");
171 }
172
173 device_object->device_handle = device_handle;
174 device_object->size = size;
175 device_object->flags = flags;
176
177 memory_object_create_named((memory_object_t) device_object,
178 size,
179 &control);
180 object = memory_object_control_to_vm_object(control);
181
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;
187 }
188 vm_object_unlock(object);
189
190 return (memory_object_t)device_object;
191 }
192
193 /*
194 *
195 */
196 kern_return_t
197 device_pager_populate_object(
198 memory_object_t device,
199 memory_object_offset_t offset,
200 ppnum_t page_num,
201 vm_size_t size)
202 {
203 device_pager_t device_object;
204 vm_object_t vm_object;
205 kern_return_t kr;
206 upl_t upl;
207
208 device_object = device_pager_lookup(device);
209 if (device_object == DEVICE_PAGER_NULL) {
210 return KERN_FAILURE;
211 }
212
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) {
216 return KERN_FAILURE;
217 }
218
219 kr = vm_object_populate_with_private(
220 vm_object, offset, page_num, size);
221 if (kr != KERN_SUCCESS) {
222 return kr;
223 }
224
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,
231 &null_size,
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");
236 }
237
238 upl_commit(upl, NULL, 0);
239 upl_deallocate(upl);
240 }
241
242
243 return kr;
244 }
245
246 /*
247 *
248 */
249 device_pager_t
250 device_pager_lookup(
251 memory_object_t mem_obj)
252 {
253 device_pager_t device_object;
254
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;
259 }
260
261 /*
262 *
263 */
264 kern_return_t
265 device_pager_init(
266 memory_object_t mem_obj,
267 memory_object_control_t control,
268 __unused memory_object_cluster_size_t pg_size)
269 {
270 device_pager_t device_object;
271 kern_return_t kr;
272 memory_object_attr_info_data_t attributes;
273
274 vm_object_t vm_object;
275
276
277 if (control == MEMORY_OBJECT_CONTROL_NULL) {
278 return KERN_INVALID_ARGUMENT;
279 }
280
281 device_object = device_pager_lookup(mem_obj);
282
283 memory_object_control_reference(control);
284 device_object->dev_pgr_hdr.mo_control = control;
285
286
287 /* The following settings should be done through an expanded change */
288 /* attributes call */
289
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;
295 }
296 if (device_object->flags & DEVICE_PAGER_NOPHYSCACHE) {
297 vm_object->nophyscache = TRUE;
298 }
299
300 vm_object->wimg_bits = device_object->flags & VM_WIMG_MASK;
301 vm_object_unlock(vm_object);
302
303
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;
309
310 kr = memory_object_change_attributes(
311 control,
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");
317 }
318
319 return KERN_SUCCESS;
320 }
321
322 /*
323 *
324 */
325 /*ARGSUSED6*/
326 kern_return_t
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)
336 {
337 device_pager_t device_object;
338
339 device_object = device_pager_lookup(mem_obj);
340 if (device_object == DEVICE_PAGER_NULL) {
341 panic("device_pager_data_return: lookup failed");
342 }
343
344 __IGNORE_WCASTALIGN(return device_data_action(device_object->device_handle,
345 (ipc_port_t) device_object,
346 VM_PROT_READ | VM_PROT_WRITE,
347 offset, data_cnt));
348 }
349
350 /*
351 *
352 */
353 kern_return_t
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)
360 {
361 device_pager_t device_object;
362
363 device_object = device_pager_lookup(mem_obj);
364
365 if (device_object == DEVICE_PAGER_NULL) {
366 panic("device_pager_data_request: lookup failed");
367 }
368
369 __IGNORE_WCASTALIGN(device_data_action(device_object->device_handle,
370 (ipc_port_t) device_object,
371 VM_PROT_READ, offset, length));
372 return KERN_SUCCESS;
373 }
374
375 /*
376 *
377 */
378 void
379 device_pager_reference(
380 memory_object_t mem_obj)
381 {
382 device_pager_t device_object;
383
384 device_object = device_pager_lookup(mem_obj);
385 os_ref_retain(&device_object->ref_count);
386
387 DTRACE_VM2(device_pager_reference,
388 device_pager_t, device_object,
389 unsigned int, os_ref_get_count(&device_object->ref_count));
390 }
391
392 /*
393 *
394 */
395 void
396 device_pager_deallocate(
397 memory_object_t mem_obj)
398 {
399 device_pager_t device_object;
400 memory_object_control_t device_control;
401
402 device_object = device_pager_lookup(mem_obj);
403
404 DTRACE_VM2(device_pager_deallocate,
405 device_pager_t, device_object,
406 unsigned int, os_ref_get_count(&device_object->ref_count));
407
408 os_ref_count_t ref_count = os_ref_release(&device_object->ref_count);
409
410 if (ref_count == 1) {
411 /*
412 * The last reference is our "named" reference.
413 * Close the device and "destroy" the VM object.
414 */
415
416 DTRACE_VM2(device_pager_destroy,
417 device_pager_t, device_object,
418 unsigned int, os_ref_get_count(&device_object->ref_count));
419
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;
424 }
425 device_control = device_object->dev_pgr_hdr.mo_control;
426 memory_object_destroy(device_control, 0);
427 } else if (ref_count == 0) {
428 /*
429 * No more references: free the pager.
430 */
431 DTRACE_VM2(device_pager_free,
432 device_pager_t, device_object,
433 unsigned int, os_ref_get_count(&device_object->ref_count));
434
435 device_pager_lock_destroy(device_object);
436
437 zfree(device_pager_zone, device_object);
438 }
439 return;
440 }
441
442 kern_return_t
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)
447 {
448 panic("device_pager_data_initialize");
449 return KERN_FAILURE;
450 }
451
452 kern_return_t
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)
458 {
459 return KERN_FAILURE;
460 }
461
462 kern_return_t
463 device_pager_terminate(
464 __unused memory_object_t mem_obj)
465 {
466 return KERN_SUCCESS;
467 }
468
469
470
471 /*
472 *
473 */
474 kern_return_t
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)
480 {
481 panic("device_pager_synchronize: memory_object_synchronize no longer supported\n");
482 return KERN_FAILURE;
483 }
484
485 /*
486 *
487 */
488 kern_return_t
489 device_pager_map(
490 memory_object_t mem_obj,
491 __unused vm_prot_t prot)
492 {
493 device_pager_t device_object;
494
495 device_object = device_pager_lookup(mem_obj);
496
497 device_pager_lock(device_object);
498 assert(os_ref_get_count(&device_object->ref_count) > 0);
499 if (device_object->is_mapped == FALSE) {
500 /*
501 * First mapping of this pager: take an extra reference
502 * that will remain until all the mappings of this pager
503 * are removed.
504 */
505 device_object->is_mapped = TRUE;
506 device_pager_reference(mem_obj);
507 }
508 device_pager_unlock(device_object);
509
510 return KERN_SUCCESS;
511 }
512
513 kern_return_t
514 device_pager_last_unmap(
515 memory_object_t mem_obj)
516 {
517 device_pager_t device_object;
518 boolean_t drop_ref;
519
520 device_object = device_pager_lookup(mem_obj);
521
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;
526 drop_ref = TRUE;
527 } else {
528 drop_ref = FALSE;
529 }
530 device_pager_unlock(device_object);
531
532 if (drop_ref) {
533 device_pager_deallocate(mem_obj);
534 }
535
536 return KERN_SUCCESS;
537 }
538
539
540
541 /*
542 *
543 */
544 device_pager_t
545 device_object_create(void)
546 {
547 device_pager_t device_object;
548
549 device_object = (struct device_pager *) zalloc(device_pager_zone);
550 if (device_object == DEVICE_PAGER_NULL) {
551 return DEVICE_PAGER_NULL;
552 }
553
554 bzero(device_object, sizeof(*device_object));
555
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;
559
560 device_pager_lock_init(device_object);
561 os_ref_init(&device_object->ref_count, NULL);
562 device_object->is_mapped = FALSE;
563
564 DTRACE_VM2(device_pager_create,
565 device_pager_t, device_object,
566 unsigned int, os_ref_get_count(&device_object->ref_count));
567
568 return device_object;
569 }
570
571 boolean_t
572 is_device_pager_ops(const struct memory_object_pager_ops *pager_ops)
573 {
574 if (pager_ops == &device_pager_ops) {
575 return TRUE;
576 }
577 return FALSE;
578 }