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/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>
40 #include <mach/thread_act.h>
42 #include <kern/assert.h>
43 #include <kern/host.h>
44 #include <kern/thread.h>
46 #include <ipc/ipc_port.h>
47 #include <ipc/ipc_space.h>
49 #include <default_pager/default_pager_types.h>
50 #include <default_pager/default_pager_object_server.h>
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>
60 /* BSD VM COMPONENT INTERFACES */
80 return(map
->hdr
.nentries
);
84 mach_get_vm_start(vm_map_t map
)
86 return( vm_map_first_entry(map
)->vme_start
);
90 mach_get_vm_end(vm_map_t map
)
92 return( vm_map_last_entry(map
)->vme_end
);
99 const struct memory_object_pager_ops vnode_pager_ops
= {
100 vnode_pager_reference
,
101 vnode_pager_deallocate
,
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
,
110 vnode_pager_last_unmap
,
111 NULL
, /* data_reclaim */
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 */
124 #define pager_ikot pager_header.io_bits
127 trigger_name_to_port( /* forward */
131 vnode_pager_cluster_read( /* forward */
139 vnode_pager_cluster_write( /* forward */
143 vm_object_offset_t
*,
149 vnode_object_create( /* forward */
153 vnode_pager_lookup( /* forward */
156 zone_t vnode_pager_zone
;
159 #define VNODE_PAGER_NULL ((vnode_pager_t) 0)
161 /* TODO: Should be set dynamically by vnode_pager_init() */
162 #define CLUSTER_SHIFT 1
164 /* TODO: Should be set dynamically by vnode_pager_bootstrap() */
165 #define MAX_VNODE 10000
171 #define PAGER_ALL 0xffffffff
172 #define PAGER_INIT 0x00000001
173 #define PAGER_PAGEIN 0x00000002
175 #define PAGER_DEBUG(LEVEL, A) {if ((pagerdebug & LEVEL)==LEVEL){printf A;}}
177 #define PAGER_DEBUG(LEVEL, A)
180 extern int proc_resetpcontrol(int);
182 #if DEVELOPMENT || DEBUG
183 extern unsigned long vm_cs_validated_resets
;
187 * Routine: mach_macx_triggers
189 * Syscall interface to set the call backs for low and
194 struct macx_triggers_args
*args
)
196 int hi_water
= args
->hi_water
;
197 int low_water
= args
->low_water
;
198 int flags
= args
->flags
;
199 mach_port_t trigger_name
= args
->alert_port
;
201 memory_object_default_t default_pager
;
202 ipc_port_t trigger_port
;
204 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
205 kr
= host_default_memory_manager(host_priv_self(),
207 if(kr
!= KERN_SUCCESS
) {
211 if (((flags
& SWAP_ENCRYPT_ON
) && (flags
& SWAP_ENCRYPT_OFF
)) ||
212 ((flags
& SWAP_COMPACT_ENABLE
) && (flags
& SWAP_COMPACT_DISABLE
))) {
213 /* can't have it both ways */
217 if (default_pager_init_flag
== 0) {
218 start_def_pager(NULL
);
219 default_pager_init_flag
= 1;
222 if (flags
& SWAP_ENCRYPT_ON
) {
223 /* ENCRYPTED SWAP: tell default_pager to encrypt */
224 default_pager_triggers(default_pager
,
228 } else if (flags
& SWAP_ENCRYPT_OFF
) {
229 /* ENCRYPTED SWAP: tell default_pager not to encrypt */
230 default_pager_triggers(default_pager
,
236 if (flags
& USE_EMERGENCY_SWAP_FILE_FIRST
) {
238 * Time to switch to the emergency segment.
240 return default_pager_triggers(default_pager
,
242 USE_EMERGENCY_SWAP_FILE_FIRST
,
246 if (flags
& SWAP_FILE_CREATION_ERROR
) {
248 * For some reason, the dynamic pager failed to create a swap file.
250 trigger_port
= trigger_name_to_port(trigger_name
);
251 if(trigger_port
== NULL
) {
254 /* trigger_port is locked and active */
255 ipc_port_make_send_locked(trigger_port
);
256 ip_unlock(trigger_port
);
257 default_pager_triggers(default_pager
,
259 SWAP_FILE_CREATION_ERROR
,
263 if (flags
& HI_WAT_ALERT
) {
264 trigger_port
= trigger_name_to_port(trigger_name
);
265 if(trigger_port
== NULL
) {
268 /* trigger_port is locked and active */
269 ipc_port_make_send_locked(trigger_port
);
270 ip_unlock(trigger_port
);
271 default_pager_triggers(default_pager
,
273 HI_WAT_ALERT
, trigger_port
);
276 if (flags
& LO_WAT_ALERT
) {
277 trigger_port
= trigger_name_to_port(trigger_name
);
278 if(trigger_port
== NULL
) {
281 /* trigger_port is locked and active */
282 ipc_port_make_send_locked(trigger_port
);
283 ip_unlock(trigger_port
);
284 default_pager_triggers(default_pager
,
286 LO_WAT_ALERT
, trigger_port
);
290 if (flags
& PROC_RESUME
) {
293 * For this call, hi_water is used to pass in the pid of the process we want to resume
294 * or unthrottle. This is of course restricted to the superuser (checked inside of
295 * proc_resetpcontrol).
298 return proc_resetpcontrol(hi_water
);
302 * Set thread scheduling priority and policy for the current thread
303 * it is assumed for the time being that the thread setting the alert
304 * is the same one which will be servicing it.
306 * XXX This does not belong in the kernel XXX
308 if (flags
& HI_WAT_ALERT
) {
309 thread_precedence_policy_data_t pre
;
310 thread_extended_policy_data_t ext
;
312 ext
.timeshare
= FALSE
;
313 pre
.importance
= INT32_MAX
;
315 thread_policy_set(current_thread(),
316 THREAD_EXTENDED_POLICY
,
317 (thread_policy_t
)&ext
,
318 THREAD_EXTENDED_POLICY_COUNT
);
320 thread_policy_set(current_thread(),
321 THREAD_PRECEDENCE_POLICY
,
322 (thread_policy_t
)&pre
,
323 THREAD_PRECEDENCE_POLICY_COUNT
);
325 current_thread()->options
|= TH_OPT_VMPRIV
;
328 if (flags
& (SWAP_COMPACT_DISABLE
| SWAP_COMPACT_ENABLE
)) {
329 return macx_backing_store_compaction(flags
& (SWAP_COMPACT_DISABLE
| SWAP_COMPACT_ENABLE
));
339 trigger_name_to_port(
340 mach_port_t trigger_name
)
342 ipc_port_t trigger_port
;
345 if (trigger_name
== 0)
348 space
= current_space();
349 if(ipc_port_translate_receive(space
, CAST_MACH_PORT_TO_NAME(trigger_name
),
350 &trigger_port
) != KERN_SUCCESS
)
356 extern int uiomove64(addr64_t
, int, void *);
360 memory_object_control_uiomove(
361 memory_object_control_t control
,
362 memory_object_offset_t offset
,
377 vm_page_t page_run
[MAX_RUN
];
378 int dirty_count
; /* keeps track of number of pages dirtied as part of this uiomove */
380 object
= memory_object_control_to_vm_object(control
);
381 if (object
== VM_OBJECT_NULL
) {
384 assert(!object
->internal
);
386 vm_object_lock(object
);
388 if (mark_dirty
&& object
->copy
!= VM_OBJECT_NULL
) {
390 * We can't modify the pages without honoring
391 * copy-on-write obligations first, so fall off
392 * this optimized path and fall back to the regular
395 vm_object_unlock(object
);
398 orig_offset
= start_offset
;
401 while (io_requested
&& retval
== 0) {
403 cur_needed
= (start_offset
+ io_requested
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
405 if (cur_needed
> MAX_RUN
)
406 cur_needed
= MAX_RUN
;
408 for (cur_run
= 0; cur_run
< cur_needed
; ) {
410 if ((dst_page
= vm_page_lookup(object
, offset
)) == VM_PAGE_NULL
)
414 if (dst_page
->busy
|| dst_page
->cleaning
) {
416 * someone else is playing with the page... if we've
417 * already collected pages into this run, go ahead
418 * and process now, we can't block on this
419 * page while holding other pages in the BUSY state
420 * otherwise we will wait
424 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
427 if (dst_page
->laundry
) {
428 dst_page
->pageout
= FALSE
;
430 vm_pageout_steal_laundry(dst_page
, FALSE
);
433 * this routine is only called when copying
434 * to/from real files... no need to consider
435 * encrypted swap pages
437 assert(!dst_page
->encrypted
);
440 if (dst_page
->dirty
== FALSE
)
442 SET_PAGE_DIRTY(dst_page
, FALSE
);
443 if (dst_page
->cs_validated
&&
444 !dst_page
->cs_tainted
) {
447 * We're modifying a code-signed
448 * page: force revalidate
450 dst_page
->cs_validated
= FALSE
;
451 #if DEVELOPMENT || DEBUG
452 vm_cs_validated_resets
++;
454 pmap_disconnect(dst_page
->phys_page
);
457 dst_page
->busy
= TRUE
;
459 page_run
[cur_run
++] = dst_page
;
461 offset
+= PAGE_SIZE_64
;
465 * we hit a 'hole' in the cache or
466 * a page we don't want to try to handle,
467 * so bail at this point
468 * we'll unlock the object below
471 vm_object_unlock(object
);
473 for (i
= 0; i
< cur_run
; i
++) {
475 dst_page
= page_run
[i
];
477 if ((xsize
= PAGE_SIZE
- start_offset
) > io_requested
)
478 xsize
= io_requested
;
480 if ( (retval
= uiomove64((addr64_t
)(((addr64_t
)(dst_page
->phys_page
) << PAGE_SHIFT
) + start_offset
), xsize
, uio
)) )
483 io_requested
-= xsize
;
486 vm_object_lock(object
);
489 * if we have more than 1 page to work on
490 * in the current run, or the original request
491 * started at offset 0 of the page, or we're
492 * processing multiple batches, we will move
493 * the pages to the tail of the inactive queue
494 * to implement an LRU for read/write accesses
496 * the check for orig_offset == 0 is there to
497 * mitigate the cost of small (< page_size) requests
498 * to the same page (this way we only move it once)
500 if (take_reference
&& (cur_run
> 1 || orig_offset
== 0)) {
502 vm_page_lockspin_queues();
504 for (i
= 0; i
< cur_run
; i
++)
505 vm_page_lru(page_run
[i
]);
507 vm_page_unlock_queues();
509 for (i
= 0; i
< cur_run
; i
++) {
510 dst_page
= page_run
[i
];
513 * someone is explicitly referencing this page...
514 * update clustered and speculative state
517 if (dst_page
->clustered
)
518 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
520 PAGE_WAKEUP_DONE(dst_page
);
524 vm_object_unlock(object
);
525 task_update_logical_writes(current_task(), (dirty_count
* PAGE_SIZE
), TASK_WRITE_DEFERRED
);
534 vnode_pager_bootstrap(void)
536 register vm_size_t size
;
538 size
= (vm_size_t
) sizeof(struct vnode_pager
);
539 vnode_pager_zone
= zinit(size
, (vm_size_t
) MAX_VNODE
*size
,
540 PAGE_SIZE
, "vnode pager structures");
541 zone_change(vnode_pager_zone
, Z_CALLERACCT
, FALSE
);
542 zone_change(vnode_pager_zone
, Z_NOENCRYPT
, TRUE
);
545 #if CONFIG_CODE_DECRYPTION
546 apple_protect_pager_bootstrap();
547 #endif /* CONFIG_CODE_DECRYPTION */
548 swapfile_pager_bootstrap();
558 __unused memory_object_t pager
)
560 vnode_pager_t vnode_object
;
562 vnode_object
= vnode_object_create(vp
);
563 if (vnode_object
== VNODE_PAGER_NULL
)
564 panic("vnode_pager_setup: vnode_object_create() failed");
565 return((memory_object_t
)vnode_object
);
572 vnode_pager_init(memory_object_t mem_obj
,
573 memory_object_control_t control
,
577 memory_object_cluster_size_t pg_size
)
579 vnode_pager_t vnode_object
;
581 memory_object_attr_info_data_t attributes
;
584 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_init: %p, %p, %lx\n", mem_obj
, control
, (unsigned long)pg_size
));
586 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
587 return KERN_INVALID_ARGUMENT
;
589 vnode_object
= vnode_pager_lookup(mem_obj
);
591 memory_object_control_reference(control
);
593 vnode_object
->control_handle
= control
;
595 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
596 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
597 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
598 attributes
.may_cache_object
= TRUE
;
599 attributes
.temporary
= TRUE
;
601 kr
= memory_object_change_attributes(
603 MEMORY_OBJECT_ATTRIBUTE_INFO
,
604 (memory_object_info_t
) &attributes
,
605 MEMORY_OBJECT_ATTR_INFO_COUNT
);
606 if (kr
!= KERN_SUCCESS
)
607 panic("vnode_pager_init: memory_object_change_attributes() failed");
609 return(KERN_SUCCESS
);
616 vnode_pager_data_return(
617 memory_object_t mem_obj
,
618 memory_object_offset_t offset
,
619 memory_object_cluster_size_t data_cnt
,
620 memory_object_offset_t
*resid_offset
,
622 __unused boolean_t dirty
,
623 __unused boolean_t kernel_copy
,
626 register vnode_pager_t vnode_object
;
628 vnode_object
= vnode_pager_lookup(mem_obj
);
630 vnode_pager_cluster_write(vnode_object
, offset
, data_cnt
, resid_offset
, io_error
, upl_flags
);
636 vnode_pager_data_initialize(
637 __unused memory_object_t mem_obj
,
638 __unused memory_object_offset_t offset
,
639 __unused memory_object_cluster_size_t data_cnt
)
641 panic("vnode_pager_data_initialize");
646 vnode_pager_data_unlock(
647 __unused memory_object_t mem_obj
,
648 __unused memory_object_offset_t offset
,
649 __unused memory_object_size_t size
,
650 __unused vm_prot_t desired_access
)
656 vnode_pager_get_isinuse(
657 memory_object_t mem_obj
,
660 vnode_pager_t vnode_object
;
662 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
664 return KERN_INVALID_ARGUMENT
;
667 vnode_object
= vnode_pager_lookup(mem_obj
);
669 *isinuse
= vnode_pager_isinuse(vnode_object
->vnode_handle
);
674 vnode_pager_get_throttle_io_limit(
675 memory_object_t mem_obj
,
678 vnode_pager_t vnode_object
;
680 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
)
681 return KERN_INVALID_ARGUMENT
;
683 vnode_object
= vnode_pager_lookup(mem_obj
);
685 (void)vnode_pager_return_throttle_io_limit(vnode_object
->vnode_handle
, limit
);
690 vnode_pager_get_isSSD(
691 memory_object_t mem_obj
,
694 vnode_pager_t vnode_object
;
696 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
)
697 return KERN_INVALID_ARGUMENT
;
699 vnode_object
= vnode_pager_lookup(mem_obj
);
701 *isSSD
= vnode_pager_isSSD(vnode_object
->vnode_handle
);
706 vnode_pager_get_object_size(
707 memory_object_t mem_obj
,
708 memory_object_offset_t
*length
)
710 vnode_pager_t vnode_object
;
712 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
714 return KERN_INVALID_ARGUMENT
;
717 vnode_object
= vnode_pager_lookup(mem_obj
);
719 *length
= vnode_pager_get_filesize(vnode_object
->vnode_handle
);
724 vnode_pager_get_object_name(
725 memory_object_t mem_obj
,
727 vm_size_t pathname_len
,
729 vm_size_t filename_len
,
730 boolean_t
*truncated_path_p
)
732 vnode_pager_t vnode_object
;
734 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
735 return KERN_INVALID_ARGUMENT
;
738 vnode_object
= vnode_pager_lookup(mem_obj
);
740 return vnode_pager_get_name(vnode_object
->vnode_handle
,
749 vnode_pager_get_object_mtime(
750 memory_object_t mem_obj
,
751 struct timespec
*mtime
,
752 struct timespec
*cs_mtime
)
754 vnode_pager_t vnode_object
;
756 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
757 return KERN_INVALID_ARGUMENT
;
760 vnode_object
= vnode_pager_lookup(mem_obj
);
762 return vnode_pager_get_mtime(vnode_object
->vnode_handle
,
768 vnode_pager_get_object_cs_blobs(
769 memory_object_t mem_obj
,
772 vnode_pager_t vnode_object
;
774 if (mem_obj
== MEMORY_OBJECT_NULL
||
775 mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
776 return KERN_INVALID_ARGUMENT
;
779 vnode_object
= vnode_pager_lookup(mem_obj
);
781 return vnode_pager_get_cs_blobs(vnode_object
->vnode_handle
,
785 #if CHECK_CS_VALIDATION_BITMAP
787 vnode_pager_cs_check_validation_bitmap(
788 memory_object_t mem_obj
,
789 memory_object_offset_t offset
,
792 vnode_pager_t vnode_object
;
794 if (mem_obj
== MEMORY_OBJECT_NULL
||
795 mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
796 return KERN_INVALID_ARGUMENT
;
799 vnode_object
= vnode_pager_lookup(mem_obj
);
800 return ubc_cs_check_validation_bitmap( vnode_object
->vnode_handle
, offset
, optype
);
802 #endif /* CHECK_CS_VALIDATION_BITMAP */
808 vnode_pager_data_request(
809 memory_object_t mem_obj
,
810 memory_object_offset_t offset
,
811 __unused memory_object_cluster_size_t length
,
812 __unused vm_prot_t desired_access
,
813 memory_object_fault_info_t fault_info
)
815 vnode_pager_t vnode_object
;
816 memory_object_offset_t base_offset
;
818 uint32_t io_streaming
= 0;
820 vnode_object
= vnode_pager_lookup(mem_obj
);
822 size
= MAX_UPL_TRANSFER_BYTES
;
823 base_offset
= offset
;
825 if (memory_object_cluster_size(vnode_object
->control_handle
, &base_offset
, &size
, &io_streaming
, fault_info
) != KERN_SUCCESS
)
828 assert(offset
>= base_offset
&&
829 offset
< base_offset
+ size
);
831 return vnode_pager_cluster_read(vnode_object
, base_offset
, offset
, io_streaming
, size
);
838 vnode_pager_reference(
839 memory_object_t mem_obj
)
841 register vnode_pager_t vnode_object
;
842 unsigned int new_ref_count
;
844 vnode_object
= vnode_pager_lookup(mem_obj
);
845 new_ref_count
= hw_atomic_add(&vnode_object
->ref_count
, 1);
846 assert(new_ref_count
> 1);
853 vnode_pager_deallocate(
854 memory_object_t mem_obj
)
856 register vnode_pager_t vnode_object
;
858 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_deallocate: %p\n", mem_obj
));
860 vnode_object
= vnode_pager_lookup(mem_obj
);
862 if (hw_atomic_sub(&vnode_object
->ref_count
, 1) == 0) {
863 if (vnode_object
->vnode_handle
!= NULL
) {
864 vnode_pager_vrele(vnode_object
->vnode_handle
);
866 zfree(vnode_pager_zone
, vnode_object
);
875 vnode_pager_terminate(
879 memory_object_t mem_obj
)
881 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_terminate: %p\n", mem_obj
));
883 return(KERN_SUCCESS
);
890 vnode_pager_synchronize(
891 memory_object_t mem_obj
,
892 memory_object_offset_t offset
,
893 memory_object_size_t length
,
894 __unused vm_sync_t sync_flags
)
896 register vnode_pager_t vnode_object
;
898 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_synchronize: %p\n", mem_obj
));
900 vnode_object
= vnode_pager_lookup(mem_obj
);
902 memory_object_synchronize_completed(vnode_object
->control_handle
, offset
, length
);
904 return (KERN_SUCCESS
);
912 memory_object_t mem_obj
,
915 vnode_pager_t vnode_object
;
919 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_map: %p %x\n", mem_obj
, prot
));
921 vnode_object
= vnode_pager_lookup(mem_obj
);
923 ret
= ubc_map(vnode_object
->vnode_handle
, prot
);
935 vnode_pager_last_unmap(
936 memory_object_t mem_obj
)
938 register vnode_pager_t vnode_object
;
940 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_last_unmap: %p\n", mem_obj
));
942 vnode_object
= vnode_pager_lookup(mem_obj
);
944 ubc_unmap(vnode_object
->vnode_handle
);
954 vnode_pager_cluster_write(
955 vnode_pager_t vnode_object
,
956 vm_object_offset_t offset
,
958 vm_object_offset_t
* resid_offset
,
965 if (upl_flags
& UPL_MSYNC
) {
967 upl_flags
|= UPL_VNODE_PAGER
;
969 if ( (upl_flags
& UPL_IOSYNC
) && io_error
)
970 upl_flags
|= UPL_KEEPCACHED
;
973 size
= (cnt
< MAX_UPL_TRANSFER_BYTES
) ? cnt
: MAX_UPL_TRANSFER_BYTES
; /* effective max */
975 assert((upl_size_t
) size
== size
);
976 vnode_pageout(vnode_object
->vnode_handle
,
977 NULL
, (upl_offset_t
)0, offset
, (upl_size_t
)size
, upl_flags
, &errno
);
979 if ( (upl_flags
& UPL_KEEPCACHED
) ) {
980 if ( (*io_error
= errno
) )
987 *resid_offset
= offset
;
990 vm_object_offset_t vnode_size
;
991 vm_object_offset_t base_offset
;
994 * this is the pageout path
996 vnode_size
= vnode_pager_get_filesize(vnode_object
->vnode_handle
);
998 if (vnode_size
> (offset
+ PAGE_SIZE
)) {
1000 * preset the maximum size of the cluster
1001 * and put us on a nice cluster boundary...
1002 * and then clip the size to insure we
1003 * don't request past the end of the underlying file
1005 size
= MAX_UPL_TRANSFER_BYTES
;
1006 base_offset
= offset
& ~((signed)(size
- 1));
1008 if ((base_offset
+ size
) > vnode_size
)
1009 size
= round_page(((vm_size_t
)(vnode_size
- base_offset
)));
1012 * we've been requested to page out a page beyond the current
1013 * end of the 'file'... don't try to cluster in this case...
1014 * we still need to send this page through because it might
1015 * be marked precious and the underlying filesystem may need
1016 * to do something with it (besides page it out)...
1018 base_offset
= offset
;
1021 assert((upl_size_t
) size
== size
);
1022 vnode_pageout(vnode_object
->vnode_handle
,
1023 NULL
, (upl_offset_t
)(offset
- base_offset
), base_offset
, (upl_size_t
) size
,
1024 (upl_flags
& UPL_IOSYNC
) | UPL_VNODE_PAGER
, NULL
);
1033 vnode_pager_cluster_read(
1034 vnode_pager_t vnode_object
,
1035 vm_object_offset_t base_offset
,
1036 vm_object_offset_t offset
,
1037 uint32_t io_streaming
,
1040 int local_error
= 0;
1044 assert(! (cnt
& PAGE_MASK
));
1047 flags
|= UPL_IOSTREAMING
;
1049 assert((upl_size_t
) cnt
== cnt
);
1050 kret
= vnode_pagein(vnode_object
->vnode_handle
,
1052 (upl_offset_t
) (offset
- base_offset
),
1058 if(kret == PAGER_ABSENT) {
1059 Need to work out the defs here, 1 corresponds to PAGER_ABSENT
1060 defined in bsd/vm/vm_pager.h However, we should not be including
1061 that file here it is a layering violation.
1066 unsigned int count
= 0;
1069 uplflags
= (UPL_NO_SYNC
|
1070 UPL_CLEAN_IN_PLACE
|
1073 assert((upl_size_t
) cnt
== cnt
);
1074 kr
= memory_object_upl_request(vnode_object
->control_handle
,
1075 base_offset
, (upl_size_t
) cnt
,
1076 &upl
, NULL
, &count
, uplflags
);
1077 if (kr
== KERN_SUCCESS
) {
1079 upl_deallocate(upl
);
1082 * We couldn't gather the page list, probably
1083 * because the memory object doesn't have a link
1084 * to a VM object anymore (forced unmount, for
1085 * example). Just return an error to the vm_fault()
1086 * path and let it handle it.
1090 return KERN_FAILURE
;
1093 return KERN_SUCCESS
;
1102 vnode_pager_release_from_cache(
1105 memory_object_free_from_cache(
1106 &realhost
, &vnode_pager_ops
, cnt
);
1113 vnode_object_create(
1116 register vnode_pager_t vnode_object
;
1118 vnode_object
= (struct vnode_pager
*) zalloc(vnode_pager_zone
);
1119 if (vnode_object
== VNODE_PAGER_NULL
)
1120 return(VNODE_PAGER_NULL
);
1123 * The vm_map call takes both named entry ports and raw memory
1124 * objects in the same parameter. We need to make sure that
1125 * vm_map does not see this object as a named entry port. So,
1126 * we reserve the first word in the object for a fake ip_kotype
1127 * setting - that will tell vm_map to use it as a memory object.
1129 vnode_object
->pager_ops
= &vnode_pager_ops
;
1130 vnode_object
->pager_ikot
= IKOT_MEMORY_OBJECT
;
1131 vnode_object
->ref_count
= 1;
1132 vnode_object
->control_handle
= MEMORY_OBJECT_CONTROL_NULL
;
1133 vnode_object
->vnode_handle
= vp
;
1135 return(vnode_object
);
1143 memory_object_t name
)
1145 vnode_pager_t vnode_object
;
1147 vnode_object
= (vnode_pager_t
)name
;
1148 assert(vnode_object
->pager_ops
== &vnode_pager_ops
);
1149 return (vnode_object
);
1153 /*********************** proc_info implementation *************/
1155 #include <sys/bsdtask_info.h>
1157 static int fill_vnodeinfoforaddr( vm_map_entry_t entry
, uintptr_t * vnodeaddr
, uint32_t * vid
);
1161 fill_procregioninfo(task_t task
, uint64_t arg
, struct proc_regioninfo_internal
*pinfo
, uintptr_t *vnodeaddr
, uint32_t *vid
)
1165 vm_map_offset_t address
= (vm_map_offset_t
)arg
;
1166 vm_map_entry_t tmp_entry
;
1167 vm_map_entry_t entry
;
1168 vm_map_offset_t start
;
1169 vm_region_extended_info_data_t extended
;
1170 vm_region_top_info_data_t top
;
1174 if (map
== VM_MAP_NULL
)
1179 vm_map_reference(map
);
1182 vm_map_lock_read(map
);
1185 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
1186 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
1187 vm_map_unlock_read(map
);
1188 vm_map_deallocate(map
);
1195 start
= entry
->vme_start
;
1197 pinfo
->pri_offset
= VME_OFFSET(entry
);
1198 pinfo
->pri_protection
= entry
->protection
;
1199 pinfo
->pri_max_protection
= entry
->max_protection
;
1200 pinfo
->pri_inheritance
= entry
->inheritance
;
1201 pinfo
->pri_behavior
= entry
->behavior
;
1202 pinfo
->pri_user_wired_count
= entry
->user_wired_count
;
1203 pinfo
->pri_user_tag
= VME_ALIAS(entry
);
1205 if (entry
->is_sub_map
) {
1206 pinfo
->pri_flags
|= PROC_REGION_SUBMAP
;
1208 if (entry
->is_shared
)
1209 pinfo
->pri_flags
|= PROC_REGION_SHARED
;
1213 extended
.protection
= entry
->protection
;
1214 extended
.user_tag
= VME_ALIAS(entry
);
1215 extended
.pages_resident
= 0;
1216 extended
.pages_swapped_out
= 0;
1217 extended
.pages_shared_now_private
= 0;
1218 extended
.pages_dirtied
= 0;
1219 extended
.external_pager
= 0;
1220 extended
.shadow_depth
= 0;
1222 vm_map_region_walk(map
, start
, entry
, VME_OFFSET(entry
), entry
->vme_end
- start
, &extended
);
1224 if (extended
.external_pager
&& extended
.ref_count
== 2 && extended
.share_mode
== SM_SHARED
)
1225 extended
.share_mode
= SM_PRIVATE
;
1227 top
.private_pages_resident
= 0;
1228 top
.shared_pages_resident
= 0;
1229 vm_map_region_top_walk(entry
, &top
);
1232 pinfo
->pri_pages_resident
= extended
.pages_resident
;
1233 pinfo
->pri_pages_shared_now_private
= extended
.pages_shared_now_private
;
1234 pinfo
->pri_pages_swapped_out
= extended
.pages_swapped_out
;
1235 pinfo
->pri_pages_dirtied
= extended
.pages_dirtied
;
1236 pinfo
->pri_ref_count
= extended
.ref_count
;
1237 pinfo
->pri_shadow_depth
= extended
.shadow_depth
;
1238 pinfo
->pri_share_mode
= extended
.share_mode
;
1240 pinfo
->pri_private_pages_resident
= top
.private_pages_resident
;
1241 pinfo
->pri_shared_pages_resident
= top
.shared_pages_resident
;
1242 pinfo
->pri_obj_id
= top
.obj_id
;
1244 pinfo
->pri_address
= (uint64_t)start
;
1245 pinfo
->pri_size
= (uint64_t)(entry
->vme_end
- start
);
1246 pinfo
->pri_depth
= 0;
1248 if ((vnodeaddr
!= 0) && (entry
->is_sub_map
== 0)) {
1249 *vnodeaddr
= (uintptr_t)0;
1251 if (fill_vnodeinfoforaddr(entry
, vnodeaddr
, vid
) ==0) {
1252 vm_map_unlock_read(map
);
1253 vm_map_deallocate(map
);
1258 vm_map_unlock_read(map
);
1259 vm_map_deallocate(map
);
1264 fill_procregioninfo_onlymappedvnodes(task_t task
, uint64_t arg
, struct proc_regioninfo_internal
*pinfo
, uintptr_t *vnodeaddr
, uint32_t *vid
)
1268 vm_map_offset_t address
= (vm_map_offset_t
)arg
;
1269 vm_map_entry_t tmp_entry
;
1270 vm_map_entry_t entry
;
1274 if (map
== VM_MAP_NULL
)
1279 vm_map_reference(map
);
1282 vm_map_lock_read(map
);
1284 if (!vm_map_lookup_entry(map
, address
, &tmp_entry
)) {
1285 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
1286 vm_map_unlock_read(map
);
1287 vm_map_deallocate(map
);
1294 while (entry
!= vm_map_to_entry(map
)) {
1298 if (entry
->is_sub_map
== 0) {
1299 if (fill_vnodeinfoforaddr(entry
, vnodeaddr
, vid
)) {
1301 pinfo
->pri_offset
= VME_OFFSET(entry
);
1302 pinfo
->pri_protection
= entry
->protection
;
1303 pinfo
->pri_max_protection
= entry
->max_protection
;
1304 pinfo
->pri_inheritance
= entry
->inheritance
;
1305 pinfo
->pri_behavior
= entry
->behavior
;
1306 pinfo
->pri_user_wired_count
= entry
->user_wired_count
;
1307 pinfo
->pri_user_tag
= VME_ALIAS(entry
);
1309 if (entry
->is_shared
)
1310 pinfo
->pri_flags
|= PROC_REGION_SHARED
;
1312 pinfo
->pri_pages_resident
= 0;
1313 pinfo
->pri_pages_shared_now_private
= 0;
1314 pinfo
->pri_pages_swapped_out
= 0;
1315 pinfo
->pri_pages_dirtied
= 0;
1316 pinfo
->pri_ref_count
= 0;
1317 pinfo
->pri_shadow_depth
= 0;
1318 pinfo
->pri_share_mode
= 0;
1320 pinfo
->pri_private_pages_resident
= 0;
1321 pinfo
->pri_shared_pages_resident
= 0;
1322 pinfo
->pri_obj_id
= 0;
1324 pinfo
->pri_address
= (uint64_t)entry
->vme_start
;
1325 pinfo
->pri_size
= (uint64_t)(entry
->vme_end
- entry
->vme_start
);
1326 pinfo
->pri_depth
= 0;
1328 vm_map_unlock_read(map
);
1329 vm_map_deallocate(map
);
1334 /* Keep searching for a vnode-backed mapping */
1335 entry
= entry
->vme_next
;
1338 vm_map_unlock_read(map
);
1339 vm_map_deallocate(map
);
1344 fill_vnodeinfoforaddr(
1345 vm_map_entry_t entry
,
1346 uintptr_t * vnodeaddr
,
1349 vm_object_t top_object
, object
;
1350 memory_object_t memory_object
;
1351 memory_object_pager_ops_t pager_ops
;
1356 if (entry
->is_sub_map
) {
1360 * The last object in the shadow chain has the
1361 * relevant pager information.
1363 top_object
= VME_OBJECT(entry
);
1364 if (top_object
== VM_OBJECT_NULL
) {
1365 object
= VM_OBJECT_NULL
;
1368 vm_object_lock(top_object
);
1369 for (object
= top_object
, shadow_depth
= 0;
1370 object
->shadow
!= VM_OBJECT_NULL
;
1371 object
= object
->shadow
, shadow_depth
++) {
1372 vm_object_lock(object
->shadow
);
1373 vm_object_unlock(object
);
1378 if (object
== VM_OBJECT_NULL
) {
1380 } else if (object
->internal
) {
1381 vm_object_unlock(object
);
1383 } else if (! object
->pager_ready
||
1384 object
->terminating
||
1386 vm_object_unlock(object
);
1389 memory_object
= object
->pager
;
1390 pager_ops
= memory_object
->mo_pager_ops
;
1391 if (pager_ops
== &vnode_pager_ops
) {
1392 kr
= vnode_pager_get_object_vnode(
1395 if (kr
!= KERN_SUCCESS
) {
1396 vm_object_unlock(object
);
1400 vm_object_unlock(object
);
1404 vm_object_unlock(object
);
1409 vnode_pager_get_object_vnode (
1410 memory_object_t mem_obj
,
1411 uintptr_t * vnodeaddr
,
1414 vnode_pager_t vnode_object
;
1416 vnode_object
= vnode_pager_lookup(mem_obj
);
1417 if (vnode_object
->vnode_handle
) {
1418 *vnodeaddr
= (uintptr_t)vnode_object
->vnode_handle
;
1419 *vid
= (uint32_t)vnode_vid((void *)vnode_object
->vnode_handle
);
1421 return(KERN_SUCCESS
);
1424 return(KERN_FAILURE
);
1429 vnode_pager_get_object_devvp(
1430 memory_object_t mem_obj
,
1436 if(vnode_pager_get_object_vnode(mem_obj
, (uintptr_t *)&vp
, (uint32_t *)&vid
) != KERN_SUCCESS
)
1437 return (KERN_FAILURE
);
1438 *devvp
= (uintptr_t)vnode_mountdevvp(vp
);
1440 return (KERN_SUCCESS
);
1441 return (KERN_FAILURE
);
1446 * Find the underlying vnode object for the given vm_map_entry. If found, return with the
1447 * object locked, otherwise return NULL with nothing locked.
1452 vm_map_entry_t entry
1455 vm_object_t top_object
, object
;
1456 memory_object_t memory_object
;
1457 memory_object_pager_ops_t pager_ops
;
1459 if (!entry
->is_sub_map
) {
1462 * The last object in the shadow chain has the
1463 * relevant pager information.
1466 top_object
= VME_OBJECT(entry
);
1469 vm_object_lock(top_object
);
1471 for (object
= top_object
; object
->shadow
!= VM_OBJECT_NULL
; object
= object
->shadow
) {
1472 vm_object_lock(object
->shadow
);
1473 vm_object_unlock(object
);
1476 if (object
&& !object
->internal
&& object
->pager_ready
&& !object
->terminating
&&
1478 memory_object
= object
->pager
;
1479 pager_ops
= memory_object
->mo_pager_ops
;
1482 * If this object points to the vnode_pager_ops, then we found what we're
1483 * looking for. Otherwise, this vm_map_entry doesn't have an underlying
1484 * vnode and so we fall through to the bottom and return NULL.
1487 if (pager_ops
== &vnode_pager_ops
)
1488 return object
; /* we return with the object locked */
1491 vm_object_unlock(object
);
1496 return(VM_OBJECT_NULL
);