]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/bsd_vm.c
xnu-1699.22.81.tar.gz
[apple/xnu.git] / osfmk / vm / bsd_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/mach_traps.h>
33 #include <mach/host_priv.h>
34 #include <mach/kern_return.h>
35 #include <mach/memory_object_control.h>
36 #include <mach/memory_object_types.h>
37 #include <mach/port.h>
38 #include <mach/policy.h>
39 #include <mach/upl.h>
40 #include <mach/thread_act.h>
41
42 #include <kern/assert.h>
43 #include <kern/host.h>
44 #include <kern/thread.h>
45
46 #include <ipc/ipc_port.h>
47 #include <ipc/ipc_space.h>
48
49 #include <default_pager/default_pager_types.h>
50 #include <default_pager/default_pager_object_server.h>
51
52 #include <vm/vm_map.h>
53 #include <vm/vm_pageout.h>
54 #include <vm/memory_object.h>
55 #include <vm/vm_pageout.h>
56 #include <vm/vm_protos.h>
57 #include <vm/vm_purgeable_internal.h>
58
59
60 /* BSD VM COMPONENT INTERFACES */
61 int
62 get_map_nentries(
63 vm_map_t);
64
65 vm_offset_t
66 get_map_start(
67 vm_map_t);
68
69 vm_offset_t
70 get_map_end(
71 vm_map_t);
72
73 /*
74 *
75 */
76 int
77 get_map_nentries(
78 vm_map_t map)
79 {
80 return(map->hdr.nentries);
81 }
82
83 mach_vm_offset_t
84 mach_get_vm_start(vm_map_t map)
85 {
86 return( vm_map_first_entry(map)->vme_start);
87 }
88
89 mach_vm_offset_t
90 mach_get_vm_end(vm_map_t map)
91 {
92 return( vm_map_last_entry(map)->vme_end);
93 }
94
95 /*
96 * BSD VNODE PAGER
97 */
98
99 const struct memory_object_pager_ops vnode_pager_ops = {
100 vnode_pager_reference,
101 vnode_pager_deallocate,
102 vnode_pager_init,
103 vnode_pager_terminate,
104 vnode_pager_data_request,
105 vnode_pager_data_return,
106 vnode_pager_data_initialize,
107 vnode_pager_data_unlock,
108 vnode_pager_synchronize,
109 vnode_pager_map,
110 vnode_pager_last_unmap,
111 NULL, /* data_reclaim */
112 "vnode pager"
113 };
114
115 typedef struct vnode_pager {
116 struct ipc_object_header pager_header; /* fake ip_kotype() */
117 memory_object_pager_ops_t pager_ops; /* == &vnode_pager_ops */
118 unsigned int ref_count; /* reference count */
119 memory_object_control_t control_handle; /* mem object control handle */
120 struct vnode *vnode_handle; /* vnode handle */
121 } *vnode_pager_t;
122
123 #define pager_ikot pager_header.io_bits
124
125 ipc_port_t
126 trigger_name_to_port( /* forward */
127 mach_port_t);
128
129 kern_return_t
130 vnode_pager_cluster_read( /* forward */
131 vnode_pager_t,
132 vm_object_offset_t,
133 vm_object_offset_t,
134 uint32_t,
135 vm_size_t);
136
137 void
138 vnode_pager_cluster_write( /* forward */
139 vnode_pager_t,
140 vm_object_offset_t,
141 vm_size_t,
142 vm_object_offset_t *,
143 int *,
144 int);
145
146
147 vnode_pager_t
148 vnode_object_create( /* forward */
149 struct vnode *);
150
151 vnode_pager_t
152 vnode_pager_lookup( /* forward */
153 memory_object_t);
154
155 zone_t vnode_pager_zone;
156
157
158 #define VNODE_PAGER_NULL ((vnode_pager_t) 0)
159
160 /* TODO: Should be set dynamically by vnode_pager_init() */
161 #define CLUSTER_SHIFT 1
162
163 /* TODO: Should be set dynamically by vnode_pager_bootstrap() */
164 #define MAX_VNODE 10000
165
166
167 #if DEBUG
168 int pagerdebug=0;
169
170 #define PAGER_ALL 0xffffffff
171 #define PAGER_INIT 0x00000001
172 #define PAGER_PAGEIN 0x00000002
173
174 #define PAGER_DEBUG(LEVEL, A) {if ((pagerdebug & LEVEL)==LEVEL){printf A;}}
175 #else
176 #define PAGER_DEBUG(LEVEL, A)
177 #endif
178
179 extern int proc_resetpcontrol(int);
180
181 #if DEVELOPMENT || DEBUG
182 extern unsigned long vm_cs_validated_resets;
183 #endif
184
185 /*
186 * Routine: mach_macx_triggers
187 * Function:
188 * Syscall interface to set the call backs for low and
189 * high water marks.
190 */
191 int
192 mach_macx_triggers(
193 struct macx_triggers_args *args)
194 {
195 int hi_water = args->hi_water;
196 int low_water = args->low_water;
197 int flags = args->flags;
198 mach_port_t trigger_name = args->alert_port;
199 kern_return_t kr;
200 memory_object_default_t default_pager;
201 ipc_port_t trigger_port;
202
203 default_pager = MEMORY_OBJECT_DEFAULT_NULL;
204 kr = host_default_memory_manager(host_priv_self(),
205 &default_pager, 0);
206 if(kr != KERN_SUCCESS) {
207 return EINVAL;
208 }
209
210 if (((flags & SWAP_ENCRYPT_ON) && (flags & SWAP_ENCRYPT_OFF)) ||
211 ((flags & SWAP_COMPACT_ENABLE) && (flags & SWAP_COMPACT_DISABLE))) {
212 /* can't have it both ways */
213 return EINVAL;
214 }
215
216 if (default_pager_init_flag == 0) {
217 start_def_pager(NULL);
218 default_pager_init_flag = 1;
219 }
220
221 if (flags & SWAP_ENCRYPT_ON) {
222 /* ENCRYPTED SWAP: tell default_pager to encrypt */
223 default_pager_triggers(default_pager,
224 0, 0,
225 SWAP_ENCRYPT_ON,
226 IP_NULL);
227 } else if (flags & SWAP_ENCRYPT_OFF) {
228 /* ENCRYPTED SWAP: tell default_pager not to encrypt */
229 default_pager_triggers(default_pager,
230 0, 0,
231 SWAP_ENCRYPT_OFF,
232 IP_NULL);
233 }
234
235 if (flags & USE_EMERGENCY_SWAP_FILE_FIRST) {
236 /*
237 * Time to switch to the emergency segment.
238 */
239 return default_pager_triggers(default_pager,
240 0, 0,
241 USE_EMERGENCY_SWAP_FILE_FIRST,
242 IP_NULL);
243 }
244
245 if (flags & SWAP_FILE_CREATION_ERROR) {
246 /*
247 * For some reason, the dynamic pager failed to create a swap file.
248 */
249 trigger_port = trigger_name_to_port(trigger_name);
250 if(trigger_port == NULL) {
251 return EINVAL;
252 }
253 /* trigger_port is locked and active */
254 ipc_port_make_send_locked(trigger_port);
255 /* now unlocked */
256 default_pager_triggers(default_pager,
257 0, 0,
258 SWAP_FILE_CREATION_ERROR,
259 trigger_port);
260 }
261
262 if (flags & HI_WAT_ALERT) {
263 trigger_port = trigger_name_to_port(trigger_name);
264 if(trigger_port == NULL) {
265 return EINVAL;
266 }
267 /* trigger_port is locked and active */
268 ipc_port_make_send_locked(trigger_port);
269 /* now unlocked */
270 default_pager_triggers(default_pager,
271 hi_water, low_water,
272 HI_WAT_ALERT, trigger_port);
273 }
274
275 if (flags & LO_WAT_ALERT) {
276 trigger_port = trigger_name_to_port(trigger_name);
277 if(trigger_port == NULL) {
278 return EINVAL;
279 }
280 /* trigger_port is locked and active */
281 ipc_port_make_send_locked(trigger_port);
282 /* and now its unlocked */
283 default_pager_triggers(default_pager,
284 hi_water, low_water,
285 LO_WAT_ALERT, trigger_port);
286 }
287
288
289 if (flags & PROC_RESUME) {
290
291 /*
292 * For this call, hi_water is used to pass in the pid of the process we want to resume
293 * or unthrottle. This is of course restricted to the superuser (checked inside of
294 * proc_resetpcontrol).
295 */
296
297 return proc_resetpcontrol(hi_water);
298 }
299
300 /*
301 * Set thread scheduling priority and policy for the current thread
302 * it is assumed for the time being that the thread setting the alert
303 * is the same one which will be servicing it.
304 *
305 * XXX This does not belong in the kernel XXX
306 */
307 if (flags & HI_WAT_ALERT) {
308 thread_precedence_policy_data_t pre;
309 thread_extended_policy_data_t ext;
310
311 ext.timeshare = FALSE;
312 pre.importance = INT32_MAX;
313
314 thread_policy_set(current_thread(),
315 THREAD_EXTENDED_POLICY,
316 (thread_policy_t)&ext,
317 THREAD_EXTENDED_POLICY_COUNT);
318
319 thread_policy_set(current_thread(),
320 THREAD_PRECEDENCE_POLICY,
321 (thread_policy_t)&pre,
322 THREAD_PRECEDENCE_POLICY_COUNT);
323
324 current_thread()->options |= TH_OPT_VMPRIV;
325 }
326
327 if (flags & (SWAP_COMPACT_DISABLE | SWAP_COMPACT_ENABLE)) {
328 return macx_backing_store_compaction(flags & (SWAP_COMPACT_DISABLE | SWAP_COMPACT_ENABLE));
329 }
330
331 return 0;
332 }
333
334 /*
335 *
336 */
337 ipc_port_t
338 trigger_name_to_port(
339 mach_port_t trigger_name)
340 {
341 ipc_port_t trigger_port;
342 ipc_space_t space;
343
344 if (trigger_name == 0)
345 return (NULL);
346
347 space = current_space();
348 if(ipc_port_translate_receive(space, CAST_MACH_PORT_TO_NAME(trigger_name),
349 &trigger_port) != KERN_SUCCESS)
350 return (NULL);
351 return trigger_port;
352 }
353
354
355 extern int uiomove64(addr64_t, int, void *);
356 #define MAX_RUN 32
357
358 int
359 memory_object_control_uiomove(
360 memory_object_control_t control,
361 memory_object_offset_t offset,
362 void * uio,
363 int start_offset,
364 int io_requested,
365 int mark_dirty,
366 int take_reference)
367 {
368 vm_object_t object;
369 vm_page_t dst_page;
370 int xsize;
371 int retval = 0;
372 int cur_run;
373 int cur_needed;
374 int i;
375 int orig_offset;
376 vm_page_t page_run[MAX_RUN];
377
378 object = memory_object_control_to_vm_object(control);
379 if (object == VM_OBJECT_NULL) {
380 return (0);
381 }
382 assert(!object->internal);
383
384 vm_object_lock(object);
385
386 if (mark_dirty && object->copy != VM_OBJECT_NULL) {
387 /*
388 * We can't modify the pages without honoring
389 * copy-on-write obligations first, so fall off
390 * this optimized path and fall back to the regular
391 * path.
392 */
393 vm_object_unlock(object);
394 return 0;
395 }
396 orig_offset = start_offset;
397
398 while (io_requested && retval == 0) {
399
400 cur_needed = (start_offset + io_requested + (PAGE_SIZE - 1)) / PAGE_SIZE;
401
402 if (cur_needed > MAX_RUN)
403 cur_needed = MAX_RUN;
404
405 for (cur_run = 0; cur_run < cur_needed; ) {
406
407 if ((dst_page = vm_page_lookup(object, offset)) == VM_PAGE_NULL)
408 break;
409
410 /*
411 * if we're in this routine, we are inside a filesystem's
412 * locking model, so we don't ever want to wait for pages that have
413 * list_req_pending == TRUE since it means that the
414 * page is a candidate for some type of I/O operation,
415 * but that it has not yet been gathered into a UPL...
416 * this implies that it is still outside the domain
417 * of the filesystem and that whoever is responsible for
418 * grabbing it into a UPL may be stuck behind the filesystem
419 * lock this thread owns, or trying to take a lock exclusively
420 * and waiting for the readers to drain from a rw lock...
421 * if we block in those cases, we will deadlock
422 */
423 if (dst_page->list_req_pending) {
424
425 if (dst_page->absent) {
426 /*
427 * this is the list_req_pending | absent | busy case
428 * which originates from vm_fault_page... we want
429 * to fall out of the fast path and go back
430 * to the caller which will gather this page
431 * into a UPL and issue the I/O if no one
432 * else beats us to it
433 */
434 break;
435 }
436 if (dst_page->pageout || dst_page->cleaning) {
437 /*
438 * this is the list_req_pending | pageout | busy case
439 * or the list_req_pending | cleaning case...
440 * which originate from the pageout_scan and
441 * msync worlds for the pageout case and the hibernate
442 * pre-cleaning world for the cleaning case...
443 * we need to reset the state of this page to indicate
444 * it should stay in the cache marked dirty... nothing else we
445 * can do at this point... we can't block on it, we can't busy
446 * it and we can't clean it from this routine.
447 */
448 vm_page_lockspin_queues();
449
450 vm_pageout_queue_steal(dst_page, TRUE);
451 vm_page_deactivate(dst_page);
452
453 vm_page_unlock_queues();
454 }
455 /*
456 * this is the list_req_pending | cleaning case...
457 * we can go ahead and deal with this page since
458 * its ok for us to mark this page busy... if a UPL
459 * tries to gather this page, it will block until the
460 * busy is cleared, thus allowing us safe use of the page
461 * when we're done with it, we will clear busy and wake
462 * up anyone waiting on it, thus allowing the UPL creation
463 * to finish
464 */
465
466 } else if (dst_page->busy || dst_page->cleaning) {
467 /*
468 * someone else is playing with the page... if we've
469 * already collected pages into this run, go ahead
470 * and process now, we can't block on this
471 * page while holding other pages in the BUSY state
472 * otherwise we will wait
473 */
474 if (cur_run)
475 break;
476 PAGE_SLEEP(object, dst_page, THREAD_UNINT);
477 continue;
478 }
479
480 /*
481 * this routine is only called when copying
482 * to/from real files... no need to consider
483 * encrypted swap pages
484 */
485 assert(!dst_page->encrypted);
486
487 if (mark_dirty) {
488 dst_page->dirty = TRUE;
489 if (dst_page->cs_validated &&
490 !dst_page->cs_tainted) {
491 /*
492 * CODE SIGNING:
493 * We're modifying a code-signed
494 * page: force revalidate
495 */
496 dst_page->cs_validated = FALSE;
497 #if DEVELOPMENT || DEBUG
498 vm_cs_validated_resets++;
499 #endif
500 pmap_disconnect(dst_page->phys_page);
501 }
502 }
503 dst_page->busy = TRUE;
504
505 page_run[cur_run++] = dst_page;
506
507 offset += PAGE_SIZE_64;
508 }
509 if (cur_run == 0)
510 /*
511 * we hit a 'hole' in the cache or
512 * a page we don't want to try to handle,
513 * so bail at this point
514 * we'll unlock the object below
515 */
516 break;
517 vm_object_unlock(object);
518
519 for (i = 0; i < cur_run; i++) {
520
521 dst_page = page_run[i];
522
523 if ((xsize = PAGE_SIZE - start_offset) > io_requested)
524 xsize = io_requested;
525
526 if ( (retval = uiomove64((addr64_t)(((addr64_t)(dst_page->phys_page) << 12) + start_offset), xsize, uio)) )
527 break;
528
529 io_requested -= xsize;
530 start_offset = 0;
531 }
532 vm_object_lock(object);
533
534 /*
535 * if we have more than 1 page to work on
536 * in the current run, or the original request
537 * started at offset 0 of the page, or we're
538 * processing multiple batches, we will move
539 * the pages to the tail of the inactive queue
540 * to implement an LRU for read/write accesses
541 *
542 * the check for orig_offset == 0 is there to
543 * mitigate the cost of small (< page_size) requests
544 * to the same page (this way we only move it once)
545 */
546 if (take_reference && (cur_run > 1 || orig_offset == 0)) {
547
548 vm_page_lockspin_queues();
549
550 for (i = 0; i < cur_run; i++)
551 vm_page_lru(page_run[i]);
552
553 vm_page_unlock_queues();
554 }
555 for (i = 0; i < cur_run; i++) {
556 dst_page = page_run[i];
557
558 /*
559 * someone is explicitly referencing this page...
560 * update clustered and speculative state
561 *
562 */
563 VM_PAGE_CONSUME_CLUSTERED(dst_page);
564
565 PAGE_WAKEUP_DONE(dst_page);
566 }
567 orig_offset = 0;
568 }
569 vm_object_unlock(object);
570
571 return (retval);
572 }
573
574
575 /*
576 *
577 */
578 void
579 vnode_pager_bootstrap(void)
580 {
581 register vm_size_t size;
582
583 size = (vm_size_t) sizeof(struct vnode_pager);
584 vnode_pager_zone = zinit(size, (vm_size_t) MAX_VNODE*size,
585 PAGE_SIZE, "vnode pager structures");
586 zone_change(vnode_pager_zone, Z_CALLERACCT, FALSE);
587 zone_change(vnode_pager_zone, Z_NOENCRYPT, TRUE);
588
589
590 #if CONFIG_CODE_DECRYPTION
591 apple_protect_pager_bootstrap();
592 #endif /* CONFIG_CODE_DECRYPTION */
593 swapfile_pager_bootstrap();
594 return;
595 }
596
597 /*
598 *
599 */
600 memory_object_t
601 vnode_pager_setup(
602 struct vnode *vp,
603 __unused memory_object_t pager)
604 {
605 vnode_pager_t vnode_object;
606
607 vnode_object = vnode_object_create(vp);
608 if (vnode_object == VNODE_PAGER_NULL)
609 panic("vnode_pager_setup: vnode_object_create() failed");
610 return((memory_object_t)vnode_object);
611 }
612
613 /*
614 *
615 */
616 kern_return_t
617 vnode_pager_init(memory_object_t mem_obj,
618 memory_object_control_t control,
619 #if !DEBUG
620 __unused
621 #endif
622 memory_object_cluster_size_t pg_size)
623 {
624 vnode_pager_t vnode_object;
625 kern_return_t kr;
626 memory_object_attr_info_data_t attributes;
627
628
629 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_init: %p, %p, %lx\n", mem_obj, control, (unsigned long)pg_size));
630
631 if (control == MEMORY_OBJECT_CONTROL_NULL)
632 return KERN_INVALID_ARGUMENT;
633
634 vnode_object = vnode_pager_lookup(mem_obj);
635
636 memory_object_control_reference(control);
637
638 vnode_object->control_handle = control;
639
640 attributes.copy_strategy = MEMORY_OBJECT_COPY_DELAY;
641 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
642 attributes.cluster_size = (1 << (PAGE_SHIFT));
643 attributes.may_cache_object = TRUE;
644 attributes.temporary = TRUE;
645
646 kr = memory_object_change_attributes(
647 control,
648 MEMORY_OBJECT_ATTRIBUTE_INFO,
649 (memory_object_info_t) &attributes,
650 MEMORY_OBJECT_ATTR_INFO_COUNT);
651 if (kr != KERN_SUCCESS)
652 panic("vnode_pager_init: memory_object_change_attributes() failed");
653
654 return(KERN_SUCCESS);
655 }
656
657 /*
658 *
659 */
660 kern_return_t
661 vnode_pager_data_return(
662 memory_object_t mem_obj,
663 memory_object_offset_t offset,
664 memory_object_cluster_size_t data_cnt,
665 memory_object_offset_t *resid_offset,
666 int *io_error,
667 __unused boolean_t dirty,
668 __unused boolean_t kernel_copy,
669 int upl_flags)
670 {
671 register vnode_pager_t vnode_object;
672
673 vnode_object = vnode_pager_lookup(mem_obj);
674
675 vnode_pager_cluster_write(vnode_object, offset, data_cnt, resid_offset, io_error, upl_flags);
676
677 return KERN_SUCCESS;
678 }
679
680 kern_return_t
681 vnode_pager_data_initialize(
682 __unused memory_object_t mem_obj,
683 __unused memory_object_offset_t offset,
684 __unused memory_object_cluster_size_t data_cnt)
685 {
686 panic("vnode_pager_data_initialize");
687 return KERN_FAILURE;
688 }
689
690 kern_return_t
691 vnode_pager_data_unlock(
692 __unused memory_object_t mem_obj,
693 __unused memory_object_offset_t offset,
694 __unused memory_object_size_t size,
695 __unused vm_prot_t desired_access)
696 {
697 return KERN_FAILURE;
698 }
699
700 kern_return_t
701 vnode_pager_get_isinuse(
702 memory_object_t mem_obj,
703 uint32_t *isinuse)
704 {
705 vnode_pager_t vnode_object;
706
707 if (mem_obj->mo_pager_ops != &vnode_pager_ops) {
708 *isinuse = 1;
709 return KERN_INVALID_ARGUMENT;
710 }
711
712 vnode_object = vnode_pager_lookup(mem_obj);
713
714 *isinuse = vnode_pager_isinuse(vnode_object->vnode_handle);
715 return KERN_SUCCESS;
716 }
717
718 kern_return_t
719 vnode_pager_check_hard_throttle(
720 memory_object_t mem_obj,
721 uint32_t *limit,
722 uint32_t hard_throttle)
723 {
724 vnode_pager_t vnode_object;
725
726 if (mem_obj->mo_pager_ops != &vnode_pager_ops)
727 return KERN_INVALID_ARGUMENT;
728
729 vnode_object = vnode_pager_lookup(mem_obj);
730
731 (void)vnode_pager_return_hard_throttle_limit(vnode_object->vnode_handle, limit, hard_throttle);
732 return KERN_SUCCESS;
733 }
734
735 kern_return_t
736 vnode_pager_get_isSSD(
737 memory_object_t mem_obj,
738 boolean_t *isSSD)
739 {
740 vnode_pager_t vnode_object;
741
742 if (mem_obj->mo_pager_ops != &vnode_pager_ops)
743 return KERN_INVALID_ARGUMENT;
744
745 vnode_object = vnode_pager_lookup(mem_obj);
746
747 *isSSD = vnode_pager_isSSD(vnode_object->vnode_handle);
748 return KERN_SUCCESS;
749 }
750
751 kern_return_t
752 vnode_pager_get_object_size(
753 memory_object_t mem_obj,
754 memory_object_offset_t *length)
755 {
756 vnode_pager_t vnode_object;
757
758 if (mem_obj->mo_pager_ops != &vnode_pager_ops) {
759 *length = 0;
760 return KERN_INVALID_ARGUMENT;
761 }
762
763 vnode_object = vnode_pager_lookup(mem_obj);
764
765 *length = vnode_pager_get_filesize(vnode_object->vnode_handle);
766 return KERN_SUCCESS;
767 }
768
769 kern_return_t
770 vnode_pager_get_object_pathname(
771 memory_object_t mem_obj,
772 char *pathname,
773 vm_size_t *length_p)
774 {
775 vnode_pager_t vnode_object;
776
777 if (mem_obj->mo_pager_ops != &vnode_pager_ops) {
778 return KERN_INVALID_ARGUMENT;
779 }
780
781 vnode_object = vnode_pager_lookup(mem_obj);
782
783 return vnode_pager_get_pathname(vnode_object->vnode_handle,
784 pathname,
785 length_p);
786 }
787
788 kern_return_t
789 vnode_pager_get_object_filename(
790 memory_object_t mem_obj,
791 const char **filename)
792 {
793 vnode_pager_t vnode_object;
794
795 if (mem_obj->mo_pager_ops != &vnode_pager_ops) {
796 return KERN_INVALID_ARGUMENT;
797 }
798
799 vnode_object = vnode_pager_lookup(mem_obj);
800
801 return vnode_pager_get_filename(vnode_object->vnode_handle,
802 filename);
803 }
804
805 kern_return_t
806 vnode_pager_get_object_cs_blobs(
807 memory_object_t mem_obj,
808 void **blobs)
809 {
810 vnode_pager_t vnode_object;
811
812 if (mem_obj == MEMORY_OBJECT_NULL ||
813 mem_obj->mo_pager_ops != &vnode_pager_ops) {
814 return KERN_INVALID_ARGUMENT;
815 }
816
817 vnode_object = vnode_pager_lookup(mem_obj);
818
819 return vnode_pager_get_cs_blobs(vnode_object->vnode_handle,
820 blobs);
821 }
822
823 #if CHECK_CS_VALIDATION_BITMAP
824 kern_return_t
825 vnode_pager_cs_check_validation_bitmap(
826 memory_object_t mem_obj,
827 memory_object_offset_t offset,
828 int optype )
829 {
830 vnode_pager_t vnode_object;
831
832 if (mem_obj == MEMORY_OBJECT_NULL ||
833 mem_obj->mo_pager_ops != &vnode_pager_ops) {
834 return KERN_INVALID_ARGUMENT;
835 }
836
837 vnode_object = vnode_pager_lookup(mem_obj);
838 return ubc_cs_check_validation_bitmap( vnode_object->vnode_handle, offset, optype );
839 }
840 #endif /* CHECK_CS_VALIDATION_BITMAP */
841
842 /*
843 *
844 */
845 kern_return_t
846 vnode_pager_data_request(
847 memory_object_t mem_obj,
848 memory_object_offset_t offset,
849 __unused memory_object_cluster_size_t length,
850 __unused vm_prot_t desired_access,
851 memory_object_fault_info_t fault_info)
852 {
853 vnode_pager_t vnode_object;
854 memory_object_offset_t base_offset;
855 vm_size_t size;
856 uint32_t io_streaming = 0;
857
858 vnode_object = vnode_pager_lookup(mem_obj);
859
860 size = MAX_UPL_TRANSFER * PAGE_SIZE;
861 base_offset = offset;
862
863 if (memory_object_cluster_size(vnode_object->control_handle, &base_offset, &size, &io_streaming, fault_info) != KERN_SUCCESS)
864 size = PAGE_SIZE;
865
866 assert(offset >= base_offset &&
867 offset < base_offset + size);
868
869 return vnode_pager_cluster_read(vnode_object, base_offset, offset, io_streaming, size);
870 }
871
872 /*
873 *
874 */
875 void
876 vnode_pager_reference(
877 memory_object_t mem_obj)
878 {
879 register vnode_pager_t vnode_object;
880 unsigned int new_ref_count;
881
882 vnode_object = vnode_pager_lookup(mem_obj);
883 new_ref_count = hw_atomic_add(&vnode_object->ref_count, 1);
884 assert(new_ref_count > 1);
885 }
886
887 /*
888 *
889 */
890 void
891 vnode_pager_deallocate(
892 memory_object_t mem_obj)
893 {
894 register vnode_pager_t vnode_object;
895
896 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_deallocate: %p\n", mem_obj));
897
898 vnode_object = vnode_pager_lookup(mem_obj);
899
900 if (hw_atomic_sub(&vnode_object->ref_count, 1) == 0) {
901 if (vnode_object->vnode_handle != NULL) {
902 vnode_pager_vrele(vnode_object->vnode_handle);
903 }
904 zfree(vnode_pager_zone, vnode_object);
905 }
906 return;
907 }
908
909 /*
910 *
911 */
912 kern_return_t
913 vnode_pager_terminate(
914 #if !DEBUG
915 __unused
916 #endif
917 memory_object_t mem_obj)
918 {
919 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_terminate: %p\n", mem_obj));
920
921 return(KERN_SUCCESS);
922 }
923
924 /*
925 *
926 */
927 kern_return_t
928 vnode_pager_synchronize(
929 memory_object_t mem_obj,
930 memory_object_offset_t offset,
931 memory_object_size_t length,
932 __unused vm_sync_t sync_flags)
933 {
934 register vnode_pager_t vnode_object;
935
936 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_synchronize: %p\n", mem_obj));
937
938 vnode_object = vnode_pager_lookup(mem_obj);
939
940 memory_object_synchronize_completed(vnode_object->control_handle, offset, length);
941
942 return (KERN_SUCCESS);
943 }
944
945 /*
946 *
947 */
948 kern_return_t
949 vnode_pager_map(
950 memory_object_t mem_obj,
951 vm_prot_t prot)
952 {
953 vnode_pager_t vnode_object;
954 int ret;
955 kern_return_t kr;
956
957 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_map: %p %x\n", mem_obj, prot));
958
959 vnode_object = vnode_pager_lookup(mem_obj);
960
961 ret = ubc_map(vnode_object->vnode_handle, prot);
962
963 if (ret != 0) {
964 kr = KERN_FAILURE;
965 } else {
966 kr = KERN_SUCCESS;
967 }
968
969 return kr;
970 }
971
972 kern_return_t
973 vnode_pager_last_unmap(
974 memory_object_t mem_obj)
975 {
976 register vnode_pager_t vnode_object;
977
978 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_last_unmap: %p\n", mem_obj));
979
980 vnode_object = vnode_pager_lookup(mem_obj);
981
982 ubc_unmap(vnode_object->vnode_handle);
983 return KERN_SUCCESS;
984 }
985
986
987
988 /*
989 *
990 */
991 void
992 vnode_pager_cluster_write(
993 vnode_pager_t vnode_object,
994 vm_object_offset_t offset,
995 vm_size_t cnt,
996 vm_object_offset_t * resid_offset,
997 int * io_error,
998 int upl_flags)
999 {
1000 vm_size_t size;
1001 int errno;
1002
1003 if (upl_flags & UPL_MSYNC) {
1004
1005 upl_flags |= UPL_VNODE_PAGER;
1006
1007 if ( (upl_flags & UPL_IOSYNC) && io_error)
1008 upl_flags |= UPL_KEEPCACHED;
1009
1010 while (cnt) {
1011 size = (cnt < (PAGE_SIZE * MAX_UPL_TRANSFER)) ? cnt : (PAGE_SIZE * MAX_UPL_TRANSFER); /* effective max */
1012
1013 assert((upl_size_t) size == size);
1014 vnode_pageout(vnode_object->vnode_handle,
1015 NULL, (upl_offset_t)0, offset, (upl_size_t)size, upl_flags, &errno);
1016
1017 if ( (upl_flags & UPL_KEEPCACHED) ) {
1018 if ( (*io_error = errno) )
1019 break;
1020 }
1021 cnt -= size;
1022 offset += size;
1023 }
1024 if (resid_offset)
1025 *resid_offset = offset;
1026
1027 } else {
1028 vm_object_offset_t vnode_size;
1029 vm_object_offset_t base_offset;
1030
1031 /*
1032 * this is the pageout path
1033 */
1034 vnode_size = vnode_pager_get_filesize(vnode_object->vnode_handle);
1035
1036 if (vnode_size > (offset + PAGE_SIZE)) {
1037 /*
1038 * preset the maximum size of the cluster
1039 * and put us on a nice cluster boundary...
1040 * and then clip the size to insure we
1041 * don't request past the end of the underlying file
1042 */
1043 size = PAGE_SIZE * MAX_UPL_TRANSFER;
1044 base_offset = offset & ~((signed)(size - 1));
1045
1046 if ((base_offset + size) > vnode_size)
1047 size = round_page(((vm_size_t)(vnode_size - base_offset)));
1048 } else {
1049 /*
1050 * we've been requested to page out a page beyond the current
1051 * end of the 'file'... don't try to cluster in this case...
1052 * we still need to send this page through because it might
1053 * be marked precious and the underlying filesystem may need
1054 * to do something with it (besides page it out)...
1055 */
1056 base_offset = offset;
1057 size = PAGE_SIZE;
1058 }
1059 assert((upl_size_t) size == size);
1060 vnode_pageout(vnode_object->vnode_handle,
1061 NULL, (upl_offset_t)(offset - base_offset), base_offset, (upl_size_t) size, UPL_VNODE_PAGER, NULL);
1062 }
1063 }
1064
1065
1066 /*
1067 *
1068 */
1069 kern_return_t
1070 vnode_pager_cluster_read(
1071 vnode_pager_t vnode_object,
1072 vm_object_offset_t base_offset,
1073 vm_object_offset_t offset,
1074 uint32_t io_streaming,
1075 vm_size_t cnt)
1076 {
1077 int local_error = 0;
1078 int kret;
1079 int flags = 0;
1080
1081 assert(! (cnt & PAGE_MASK));
1082
1083 if (io_streaming)
1084 flags |= UPL_IOSTREAMING;
1085
1086 assert((upl_size_t) cnt == cnt);
1087 kret = vnode_pagein(vnode_object->vnode_handle,
1088 (upl_t) NULL,
1089 (upl_offset_t) (offset - base_offset),
1090 base_offset,
1091 (upl_size_t) cnt,
1092 flags,
1093 &local_error);
1094 /*
1095 if(kret == PAGER_ABSENT) {
1096 Need to work out the defs here, 1 corresponds to PAGER_ABSENT
1097 defined in bsd/vm/vm_pager.h However, we should not be including
1098 that file here it is a layering violation.
1099 */
1100 if (kret == 1) {
1101 int uplflags;
1102 upl_t upl = NULL;
1103 unsigned int count = 0;
1104 kern_return_t kr;
1105
1106 uplflags = (UPL_NO_SYNC |
1107 UPL_CLEAN_IN_PLACE |
1108 UPL_SET_INTERNAL);
1109 count = 0;
1110 assert((upl_size_t) cnt == cnt);
1111 kr = memory_object_upl_request(vnode_object->control_handle,
1112 base_offset, (upl_size_t) cnt,
1113 &upl, NULL, &count, uplflags);
1114 if (kr == KERN_SUCCESS) {
1115 upl_abort(upl, 0);
1116 upl_deallocate(upl);
1117 } else {
1118 /*
1119 * We couldn't gather the page list, probably
1120 * because the memory object doesn't have a link
1121 * to a VM object anymore (forced unmount, for
1122 * example). Just return an error to the vm_fault()
1123 * path and let it handle it.
1124 */
1125 }
1126
1127 return KERN_FAILURE;
1128 }
1129
1130 return KERN_SUCCESS;
1131
1132 }
1133
1134
1135 /*
1136 *
1137 */
1138 void
1139 vnode_pager_release_from_cache(
1140 int *cnt)
1141 {
1142 memory_object_free_from_cache(
1143 &realhost, &vnode_pager_ops, cnt);
1144 }
1145
1146 /*
1147 *
1148 */
1149 vnode_pager_t
1150 vnode_object_create(
1151 struct vnode *vp)
1152 {
1153 register vnode_pager_t vnode_object;
1154
1155 vnode_object = (struct vnode_pager *) zalloc(vnode_pager_zone);
1156 if (vnode_object == VNODE_PAGER_NULL)
1157 return(VNODE_PAGER_NULL);
1158
1159 /*
1160 * The vm_map call takes both named entry ports and raw memory
1161 * objects in the same parameter. We need to make sure that
1162 * vm_map does not see this object as a named entry port. So,
1163 * we reserve the first word in the object for a fake ip_kotype
1164 * setting - that will tell vm_map to use it as a memory object.
1165 */
1166 vnode_object->pager_ops = &vnode_pager_ops;
1167 vnode_object->pager_ikot = IKOT_MEMORY_OBJECT;
1168 vnode_object->ref_count = 1;
1169 vnode_object->control_handle = MEMORY_OBJECT_CONTROL_NULL;
1170 vnode_object->vnode_handle = vp;
1171
1172 return(vnode_object);
1173 }
1174
1175 /*
1176 *
1177 */
1178 vnode_pager_t
1179 vnode_pager_lookup(
1180 memory_object_t name)
1181 {
1182 vnode_pager_t vnode_object;
1183
1184 vnode_object = (vnode_pager_t)name;
1185 assert(vnode_object->pager_ops == &vnode_pager_ops);
1186 return (vnode_object);
1187 }
1188
1189
1190 /*********************** proc_info implementation *************/
1191
1192 #include <sys/bsdtask_info.h>
1193
1194 static int fill_vnodeinfoforaddr( vm_map_entry_t entry, uintptr_t * vnodeaddr, uint32_t * vid);
1195
1196
1197 int
1198 fill_procregioninfo(task_t task, uint64_t arg, struct proc_regioninfo_internal *pinfo, uintptr_t *vnodeaddr, uint32_t *vid)
1199 {
1200
1201 vm_map_t map;
1202 vm_map_offset_t address = (vm_map_offset_t )arg;
1203 vm_map_entry_t tmp_entry;
1204 vm_map_entry_t entry;
1205 vm_map_offset_t start;
1206 vm_region_extended_info_data_t extended;
1207 vm_region_top_info_data_t top;
1208
1209 task_lock(task);
1210 map = task->map;
1211 if (map == VM_MAP_NULL)
1212 {
1213 task_unlock(task);
1214 return(0);
1215 }
1216 vm_map_reference(map);
1217 task_unlock(task);
1218
1219 vm_map_lock_read(map);
1220
1221 start = address;
1222 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
1223 if ((entry = tmp_entry->vme_next) == vm_map_to_entry(map)) {
1224 vm_map_unlock_read(map);
1225 vm_map_deallocate(map);
1226 return(0);
1227 }
1228 } else {
1229 entry = tmp_entry;
1230 }
1231
1232 start = entry->vme_start;
1233
1234 pinfo->pri_offset = entry->offset;
1235 pinfo->pri_protection = entry->protection;
1236 pinfo->pri_max_protection = entry->max_protection;
1237 pinfo->pri_inheritance = entry->inheritance;
1238 pinfo->pri_behavior = entry->behavior;
1239 pinfo->pri_user_wired_count = entry->user_wired_count;
1240 pinfo->pri_user_tag = entry->alias;
1241
1242 if (entry->is_sub_map) {
1243 pinfo->pri_flags |= PROC_REGION_SUBMAP;
1244 } else {
1245 if (entry->is_shared)
1246 pinfo->pri_flags |= PROC_REGION_SHARED;
1247 }
1248
1249
1250 extended.protection = entry->protection;
1251 extended.user_tag = entry->alias;
1252 extended.pages_resident = 0;
1253 extended.pages_swapped_out = 0;
1254 extended.pages_shared_now_private = 0;
1255 extended.pages_dirtied = 0;
1256 extended.external_pager = 0;
1257 extended.shadow_depth = 0;
1258
1259 vm_map_region_walk(map, start, entry, entry->offset, entry->vme_end - start, &extended);
1260
1261 if (extended.external_pager && extended.ref_count == 2 && extended.share_mode == SM_SHARED)
1262 extended.share_mode = SM_PRIVATE;
1263
1264 top.private_pages_resident = 0;
1265 top.shared_pages_resident = 0;
1266 vm_map_region_top_walk(entry, &top);
1267
1268
1269 pinfo->pri_pages_resident = extended.pages_resident;
1270 pinfo->pri_pages_shared_now_private = extended.pages_shared_now_private;
1271 pinfo->pri_pages_swapped_out = extended.pages_swapped_out;
1272 pinfo->pri_pages_dirtied = extended.pages_dirtied;
1273 pinfo->pri_ref_count = extended.ref_count;
1274 pinfo->pri_shadow_depth = extended.shadow_depth;
1275 pinfo->pri_share_mode = extended.share_mode;
1276
1277 pinfo->pri_private_pages_resident = top.private_pages_resident;
1278 pinfo->pri_shared_pages_resident = top.shared_pages_resident;
1279 pinfo->pri_obj_id = top.obj_id;
1280
1281 pinfo->pri_address = (uint64_t)start;
1282 pinfo->pri_size = (uint64_t)(entry->vme_end - start);
1283 pinfo->pri_depth = 0;
1284
1285 if ((vnodeaddr != 0) && (entry->is_sub_map == 0)) {
1286 *vnodeaddr = (uintptr_t)0;
1287
1288 if (fill_vnodeinfoforaddr(entry, vnodeaddr, vid) ==0) {
1289 vm_map_unlock_read(map);
1290 vm_map_deallocate(map);
1291 return(1);
1292 }
1293 }
1294
1295 vm_map_unlock_read(map);
1296 vm_map_deallocate(map);
1297 return(1);
1298 }
1299
1300 static int
1301 fill_vnodeinfoforaddr(
1302 vm_map_entry_t entry,
1303 uintptr_t * vnodeaddr,
1304 uint32_t * vid)
1305 {
1306 vm_object_t top_object, object;
1307 memory_object_t memory_object;
1308 memory_object_pager_ops_t pager_ops;
1309 kern_return_t kr;
1310 int shadow_depth;
1311
1312
1313 if (entry->is_sub_map) {
1314 return(0);
1315 } else {
1316 /*
1317 * The last object in the shadow chain has the
1318 * relevant pager information.
1319 */
1320 top_object = entry->object.vm_object;
1321 if (top_object == VM_OBJECT_NULL) {
1322 object = VM_OBJECT_NULL;
1323 shadow_depth = 0;
1324 } else {
1325 vm_object_lock(top_object);
1326 for (object = top_object, shadow_depth = 0;
1327 object->shadow != VM_OBJECT_NULL;
1328 object = object->shadow, shadow_depth++) {
1329 vm_object_lock(object->shadow);
1330 vm_object_unlock(object);
1331 }
1332 }
1333 }
1334
1335 if (object == VM_OBJECT_NULL) {
1336 return(0);
1337 } else if (object->internal) {
1338 vm_object_unlock(object);
1339 return(0);
1340 } else if (! object->pager_ready ||
1341 object->terminating ||
1342 ! object->alive) {
1343 vm_object_unlock(object);
1344 return(0);
1345 } else {
1346 memory_object = object->pager;
1347 pager_ops = memory_object->mo_pager_ops;
1348 if (pager_ops == &vnode_pager_ops) {
1349 kr = vnode_pager_get_object_vnode(
1350 memory_object,
1351 vnodeaddr, vid);
1352 if (kr != KERN_SUCCESS) {
1353 vm_object_unlock(object);
1354 return(0);
1355 }
1356 } else {
1357 vm_object_unlock(object);
1358 return(0);
1359 }
1360 }
1361 vm_object_unlock(object);
1362 return(1);
1363 }
1364
1365 kern_return_t
1366 vnode_pager_get_object_vnode (
1367 memory_object_t mem_obj,
1368 uintptr_t * vnodeaddr,
1369 uint32_t * vid)
1370 {
1371 vnode_pager_t vnode_object;
1372
1373 vnode_object = vnode_pager_lookup(mem_obj);
1374 if (vnode_object->vnode_handle) {
1375 *vnodeaddr = (uintptr_t)vnode_object->vnode_handle;
1376 *vid = (uint32_t)vnode_vid((void *)vnode_object->vnode_handle);
1377
1378 return(KERN_SUCCESS);
1379 }
1380
1381 return(KERN_FAILURE);
1382 }
1383
1384
1385 /*
1386 * Find the underlying vnode object for the given vm_map_entry. If found, return with the
1387 * object locked, otherwise return NULL with nothing locked.
1388 */
1389
1390 vm_object_t
1391 find_vnode_object(
1392 vm_map_entry_t entry
1393 )
1394 {
1395 vm_object_t top_object, object;
1396 memory_object_t memory_object;
1397 memory_object_pager_ops_t pager_ops;
1398
1399 if (!entry->is_sub_map) {
1400
1401 /*
1402 * The last object in the shadow chain has the
1403 * relevant pager information.
1404 */
1405
1406 top_object = entry->object.vm_object;
1407
1408 if (top_object) {
1409 vm_object_lock(top_object);
1410
1411 for (object = top_object; object->shadow != VM_OBJECT_NULL; object = object->shadow) {
1412 vm_object_lock(object->shadow);
1413 vm_object_unlock(object);
1414 }
1415
1416 if (object && !object->internal && object->pager_ready && !object->terminating &&
1417 object->alive) {
1418 memory_object = object->pager;
1419 pager_ops = memory_object->mo_pager_ops;
1420
1421 /*
1422 * If this object points to the vnode_pager_ops, then we found what we're
1423 * looking for. Otherwise, this vm_map_entry doesn't have an underlying
1424 * vnode and so we fall through to the bottom and return NULL.
1425 */
1426
1427 if (pager_ops == &vnode_pager_ops)
1428 return object; /* we return with the object locked */
1429 }
1430
1431 vm_object_unlock(object);
1432 }
1433
1434 }
1435
1436 return(VM_OBJECT_NULL);
1437 }