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 */
123 #define pager_ikot pager_header.io_bits
126 trigger_name_to_port( /* forward */
130 vnode_pager_cluster_read( /* forward */
138 vnode_pager_cluster_write( /* forward */
142 vm_object_offset_t
*,
148 vnode_object_create( /* forward */
152 vnode_pager_lookup( /* forward */
155 zone_t vnode_pager_zone
;
158 #define VNODE_PAGER_NULL ((vnode_pager_t) 0)
160 /* TODO: Should be set dynamically by vnode_pager_init() */
161 #define CLUSTER_SHIFT 1
163 /* TODO: Should be set dynamically by vnode_pager_bootstrap() */
164 #define MAX_VNODE 10000
170 #define PAGER_ALL 0xffffffff
171 #define PAGER_INIT 0x00000001
172 #define PAGER_PAGEIN 0x00000002
174 #define PAGER_DEBUG(LEVEL, A) {if ((pagerdebug & LEVEL)==LEVEL){printf A;}}
176 #define PAGER_DEBUG(LEVEL, A)
179 extern int proc_resetpcontrol(int);
181 #if DEVELOPMENT || DEBUG
182 extern unsigned long vm_cs_validated_resets
;
186 * Routine: mach_macx_triggers
188 * Syscall interface to set the call backs for low and
193 struct macx_triggers_args
*args
)
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
;
200 memory_object_default_t default_pager
;
201 ipc_port_t trigger_port
;
203 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
204 kr
= host_default_memory_manager(host_priv_self(),
206 if(kr
!= KERN_SUCCESS
) {
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 */
216 if (default_pager_init_flag
== 0) {
217 start_def_pager(NULL
);
218 default_pager_init_flag
= 1;
221 if (flags
& SWAP_ENCRYPT_ON
) {
222 /* ENCRYPTED SWAP: tell default_pager to encrypt */
223 default_pager_triggers(default_pager
,
227 } else if (flags
& SWAP_ENCRYPT_OFF
) {
228 /* ENCRYPTED SWAP: tell default_pager not to encrypt */
229 default_pager_triggers(default_pager
,
235 if (flags
& USE_EMERGENCY_SWAP_FILE_FIRST
) {
237 * Time to switch to the emergency segment.
239 return default_pager_triggers(default_pager
,
241 USE_EMERGENCY_SWAP_FILE_FIRST
,
245 if (flags
& SWAP_FILE_CREATION_ERROR
) {
247 * For some reason, the dynamic pager failed to create a swap file.
249 trigger_port
= trigger_name_to_port(trigger_name
);
250 if(trigger_port
== NULL
) {
253 /* trigger_port is locked and active */
254 ipc_port_make_send_locked(trigger_port
);
256 default_pager_triggers(default_pager
,
258 SWAP_FILE_CREATION_ERROR
,
262 if (flags
& HI_WAT_ALERT
) {
263 trigger_port
= trigger_name_to_port(trigger_name
);
264 if(trigger_port
== NULL
) {
267 /* trigger_port is locked and active */
268 ipc_port_make_send_locked(trigger_port
);
270 default_pager_triggers(default_pager
,
272 HI_WAT_ALERT
, trigger_port
);
275 if (flags
& LO_WAT_ALERT
) {
276 trigger_port
= trigger_name_to_port(trigger_name
);
277 if(trigger_port
== NULL
) {
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
,
285 LO_WAT_ALERT
, trigger_port
);
289 if (flags
& PROC_RESUME
) {
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).
297 return proc_resetpcontrol(hi_water
);
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.
305 * XXX This does not belong in the kernel XXX
307 if (flags
& HI_WAT_ALERT
) {
308 thread_precedence_policy_data_t pre
;
309 thread_extended_policy_data_t ext
;
311 ext
.timeshare
= FALSE
;
312 pre
.importance
= INT32_MAX
;
314 thread_policy_set(current_thread(),
315 THREAD_EXTENDED_POLICY
,
316 (thread_policy_t
)&ext
,
317 THREAD_EXTENDED_POLICY_COUNT
);
319 thread_policy_set(current_thread(),
320 THREAD_PRECEDENCE_POLICY
,
321 (thread_policy_t
)&pre
,
322 THREAD_PRECEDENCE_POLICY_COUNT
);
324 current_thread()->options
|= TH_OPT_VMPRIV
;
327 if (flags
& (SWAP_COMPACT_DISABLE
| SWAP_COMPACT_ENABLE
)) {
328 return macx_backing_store_compaction(flags
& (SWAP_COMPACT_DISABLE
| SWAP_COMPACT_ENABLE
));
338 trigger_name_to_port(
339 mach_port_t trigger_name
)
341 ipc_port_t trigger_port
;
344 if (trigger_name
== 0)
347 space
= current_space();
348 if(ipc_port_translate_receive(space
, CAST_MACH_PORT_TO_NAME(trigger_name
),
349 &trigger_port
) != KERN_SUCCESS
)
355 extern int uiomove64(addr64_t
, int, void *);
359 memory_object_control_uiomove(
360 memory_object_control_t control
,
361 memory_object_offset_t offset
,
376 vm_page_t page_run
[MAX_RUN
];
378 object
= memory_object_control_to_vm_object(control
);
379 if (object
== VM_OBJECT_NULL
) {
382 assert(!object
->internal
);
384 vm_object_lock(object
);
386 if (mark_dirty
&& object
->copy
!= VM_OBJECT_NULL
) {
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
393 vm_object_unlock(object
);
396 orig_offset
= start_offset
;
398 while (io_requested
&& retval
== 0) {
400 cur_needed
= (start_offset
+ io_requested
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
402 if (cur_needed
> MAX_RUN
)
403 cur_needed
= MAX_RUN
;
405 for (cur_run
= 0; cur_run
< cur_needed
; ) {
407 if ((dst_page
= vm_page_lookup(object
, offset
)) == VM_PAGE_NULL
)
411 if (dst_page
->busy
|| dst_page
->cleaning
) {
413 * someone else is playing with the page... if we've
414 * already collected pages into this run, go ahead
415 * and process now, we can't block on this
416 * page while holding other pages in the BUSY state
417 * otherwise we will wait
421 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
424 if (dst_page
->laundry
) {
425 dst_page
->pageout
= FALSE
;
427 vm_pageout_steal_laundry(dst_page
, FALSE
);
430 * this routine is only called when copying
431 * to/from real files... no need to consider
432 * encrypted swap pages
434 assert(!dst_page
->encrypted
);
437 SET_PAGE_DIRTY(dst_page
, FALSE
);
438 if (dst_page
->cs_validated
&&
439 !dst_page
->cs_tainted
) {
442 * We're modifying a code-signed
443 * page: force revalidate
445 dst_page
->cs_validated
= FALSE
;
446 #if DEVELOPMENT || DEBUG
447 vm_cs_validated_resets
++;
449 pmap_disconnect(dst_page
->phys_page
);
452 dst_page
->busy
= TRUE
;
454 page_run
[cur_run
++] = dst_page
;
456 offset
+= PAGE_SIZE_64
;
460 * we hit a 'hole' in the cache or
461 * a page we don't want to try to handle,
462 * so bail at this point
463 * we'll unlock the object below
466 vm_object_unlock(object
);
468 for (i
= 0; i
< cur_run
; i
++) {
470 dst_page
= page_run
[i
];
472 if ((xsize
= PAGE_SIZE
- start_offset
) > io_requested
)
473 xsize
= io_requested
;
475 if ( (retval
= uiomove64((addr64_t
)(((addr64_t
)(dst_page
->phys_page
) << 12) + start_offset
), xsize
, uio
)) )
478 io_requested
-= xsize
;
481 vm_object_lock(object
);
484 * if we have more than 1 page to work on
485 * in the current run, or the original request
486 * started at offset 0 of the page, or we're
487 * processing multiple batches, we will move
488 * the pages to the tail of the inactive queue
489 * to implement an LRU for read/write accesses
491 * the check for orig_offset == 0 is there to
492 * mitigate the cost of small (< page_size) requests
493 * to the same page (this way we only move it once)
495 if (take_reference
&& (cur_run
> 1 || orig_offset
== 0)) {
497 vm_page_lockspin_queues();
499 for (i
= 0; i
< cur_run
; i
++)
500 vm_page_lru(page_run
[i
]);
502 vm_page_unlock_queues();
504 for (i
= 0; i
< cur_run
; i
++) {
505 dst_page
= page_run
[i
];
508 * someone is explicitly referencing this page...
509 * update clustered and speculative state
512 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
514 PAGE_WAKEUP_DONE(dst_page
);
518 vm_object_unlock(object
);
528 vnode_pager_bootstrap(void)
530 register vm_size_t size
;
532 size
= (vm_size_t
) sizeof(struct vnode_pager
);
533 vnode_pager_zone
= zinit(size
, (vm_size_t
) MAX_VNODE
*size
,
534 PAGE_SIZE
, "vnode pager structures");
535 zone_change(vnode_pager_zone
, Z_CALLERACCT
, FALSE
);
536 zone_change(vnode_pager_zone
, Z_NOENCRYPT
, TRUE
);
539 #if CONFIG_CODE_DECRYPTION
540 apple_protect_pager_bootstrap();
541 #endif /* CONFIG_CODE_DECRYPTION */
542 swapfile_pager_bootstrap();
552 __unused memory_object_t pager
)
554 vnode_pager_t vnode_object
;
556 vnode_object
= vnode_object_create(vp
);
557 if (vnode_object
== VNODE_PAGER_NULL
)
558 panic("vnode_pager_setup: vnode_object_create() failed");
559 return((memory_object_t
)vnode_object
);
566 vnode_pager_init(memory_object_t mem_obj
,
567 memory_object_control_t control
,
571 memory_object_cluster_size_t pg_size
)
573 vnode_pager_t vnode_object
;
575 memory_object_attr_info_data_t attributes
;
578 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_init: %p, %p, %lx\n", mem_obj
, control
, (unsigned long)pg_size
));
580 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
581 return KERN_INVALID_ARGUMENT
;
583 vnode_object
= vnode_pager_lookup(mem_obj
);
585 memory_object_control_reference(control
);
587 vnode_object
->control_handle
= control
;
589 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
590 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
591 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
592 attributes
.may_cache_object
= TRUE
;
593 attributes
.temporary
= TRUE
;
595 kr
= memory_object_change_attributes(
597 MEMORY_OBJECT_ATTRIBUTE_INFO
,
598 (memory_object_info_t
) &attributes
,
599 MEMORY_OBJECT_ATTR_INFO_COUNT
);
600 if (kr
!= KERN_SUCCESS
)
601 panic("vnode_pager_init: memory_object_change_attributes() failed");
603 return(KERN_SUCCESS
);
610 vnode_pager_data_return(
611 memory_object_t mem_obj
,
612 memory_object_offset_t offset
,
613 memory_object_cluster_size_t data_cnt
,
614 memory_object_offset_t
*resid_offset
,
616 __unused boolean_t dirty
,
617 __unused boolean_t kernel_copy
,
620 register vnode_pager_t vnode_object
;
622 vnode_object
= vnode_pager_lookup(mem_obj
);
624 vnode_pager_cluster_write(vnode_object
, offset
, data_cnt
, resid_offset
, io_error
, upl_flags
);
630 vnode_pager_data_initialize(
631 __unused memory_object_t mem_obj
,
632 __unused memory_object_offset_t offset
,
633 __unused memory_object_cluster_size_t data_cnt
)
635 panic("vnode_pager_data_initialize");
640 vnode_pager_data_unlock(
641 __unused memory_object_t mem_obj
,
642 __unused memory_object_offset_t offset
,
643 __unused memory_object_size_t size
,
644 __unused vm_prot_t desired_access
)
650 vnode_pager_get_isinuse(
651 memory_object_t mem_obj
,
654 vnode_pager_t vnode_object
;
656 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
658 return KERN_INVALID_ARGUMENT
;
661 vnode_object
= vnode_pager_lookup(mem_obj
);
663 *isinuse
= vnode_pager_isinuse(vnode_object
->vnode_handle
);
668 vnode_pager_check_hard_throttle(
669 memory_object_t mem_obj
,
671 uint32_t hard_throttle
)
673 vnode_pager_t vnode_object
;
675 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
)
676 return KERN_INVALID_ARGUMENT
;
678 vnode_object
= vnode_pager_lookup(mem_obj
);
680 (void)vnode_pager_return_hard_throttle_limit(vnode_object
->vnode_handle
, limit
, hard_throttle
);
685 vnode_pager_get_isSSD(
686 memory_object_t mem_obj
,
689 vnode_pager_t vnode_object
;
691 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
)
692 return KERN_INVALID_ARGUMENT
;
694 vnode_object
= vnode_pager_lookup(mem_obj
);
696 *isSSD
= vnode_pager_isSSD(vnode_object
->vnode_handle
);
701 vnode_pager_get_object_size(
702 memory_object_t mem_obj
,
703 memory_object_offset_t
*length
)
705 vnode_pager_t vnode_object
;
707 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
709 return KERN_INVALID_ARGUMENT
;
712 vnode_object
= vnode_pager_lookup(mem_obj
);
714 *length
= vnode_pager_get_filesize(vnode_object
->vnode_handle
);
719 vnode_pager_get_object_pathname(
720 memory_object_t mem_obj
,
724 vnode_pager_t vnode_object
;
726 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
727 return KERN_INVALID_ARGUMENT
;
730 vnode_object
= vnode_pager_lookup(mem_obj
);
732 return vnode_pager_get_pathname(vnode_object
->vnode_handle
,
738 vnode_pager_get_object_filename(
739 memory_object_t mem_obj
,
740 const char **filename
)
742 vnode_pager_t vnode_object
;
744 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
745 return KERN_INVALID_ARGUMENT
;
748 vnode_object
= vnode_pager_lookup(mem_obj
);
750 return vnode_pager_get_filename(vnode_object
->vnode_handle
,
755 vnode_pager_get_object_cs_blobs(
756 memory_object_t mem_obj
,
759 vnode_pager_t vnode_object
;
761 if (mem_obj
== MEMORY_OBJECT_NULL
||
762 mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
763 return KERN_INVALID_ARGUMENT
;
766 vnode_object
= vnode_pager_lookup(mem_obj
);
768 return vnode_pager_get_cs_blobs(vnode_object
->vnode_handle
,
772 #if CHECK_CS_VALIDATION_BITMAP
774 vnode_pager_cs_check_validation_bitmap(
775 memory_object_t mem_obj
,
776 memory_object_offset_t offset
,
779 vnode_pager_t vnode_object
;
781 if (mem_obj
== MEMORY_OBJECT_NULL
||
782 mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
783 return KERN_INVALID_ARGUMENT
;
786 vnode_object
= vnode_pager_lookup(mem_obj
);
787 return ubc_cs_check_validation_bitmap( vnode_object
->vnode_handle
, offset
, optype
);
789 #endif /* CHECK_CS_VALIDATION_BITMAP */
795 vnode_pager_data_request(
796 memory_object_t mem_obj
,
797 memory_object_offset_t offset
,
798 __unused memory_object_cluster_size_t length
,
799 __unused vm_prot_t desired_access
,
800 memory_object_fault_info_t fault_info
)
802 vnode_pager_t vnode_object
;
803 memory_object_offset_t base_offset
;
805 uint32_t io_streaming
= 0;
807 vnode_object
= vnode_pager_lookup(mem_obj
);
809 size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
810 base_offset
= offset
;
812 if (memory_object_cluster_size(vnode_object
->control_handle
, &base_offset
, &size
, &io_streaming
, fault_info
) != KERN_SUCCESS
)
815 assert(offset
>= base_offset
&&
816 offset
< base_offset
+ size
);
818 return vnode_pager_cluster_read(vnode_object
, base_offset
, offset
, io_streaming
, size
);
825 vnode_pager_reference(
826 memory_object_t mem_obj
)
828 register vnode_pager_t vnode_object
;
829 unsigned int new_ref_count
;
831 vnode_object
= vnode_pager_lookup(mem_obj
);
832 new_ref_count
= hw_atomic_add(&vnode_object
->ref_count
, 1);
833 assert(new_ref_count
> 1);
840 vnode_pager_deallocate(
841 memory_object_t mem_obj
)
843 register vnode_pager_t vnode_object
;
845 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_deallocate: %p\n", mem_obj
));
847 vnode_object
= vnode_pager_lookup(mem_obj
);
849 if (hw_atomic_sub(&vnode_object
->ref_count
, 1) == 0) {
850 if (vnode_object
->vnode_handle
!= NULL
) {
851 vnode_pager_vrele(vnode_object
->vnode_handle
);
853 zfree(vnode_pager_zone
, vnode_object
);
862 vnode_pager_terminate(
866 memory_object_t mem_obj
)
868 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_terminate: %p\n", mem_obj
));
870 return(KERN_SUCCESS
);
877 vnode_pager_synchronize(
878 memory_object_t mem_obj
,
879 memory_object_offset_t offset
,
880 memory_object_size_t length
,
881 __unused vm_sync_t sync_flags
)
883 register vnode_pager_t vnode_object
;
885 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_synchronize: %p\n", mem_obj
));
887 vnode_object
= vnode_pager_lookup(mem_obj
);
889 memory_object_synchronize_completed(vnode_object
->control_handle
, offset
, length
);
891 return (KERN_SUCCESS
);
899 memory_object_t mem_obj
,
902 vnode_pager_t vnode_object
;
906 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_map: %p %x\n", mem_obj
, prot
));
908 vnode_object
= vnode_pager_lookup(mem_obj
);
910 ret
= ubc_map(vnode_object
->vnode_handle
, prot
);
922 vnode_pager_last_unmap(
923 memory_object_t mem_obj
)
925 register vnode_pager_t vnode_object
;
927 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_last_unmap: %p\n", mem_obj
));
929 vnode_object
= vnode_pager_lookup(mem_obj
);
931 ubc_unmap(vnode_object
->vnode_handle
);
941 vnode_pager_cluster_write(
942 vnode_pager_t vnode_object
,
943 vm_object_offset_t offset
,
945 vm_object_offset_t
* resid_offset
,
952 if (upl_flags
& UPL_MSYNC
) {
954 upl_flags
|= UPL_VNODE_PAGER
;
956 if ( (upl_flags
& UPL_IOSYNC
) && io_error
)
957 upl_flags
|= UPL_KEEPCACHED
;
960 size
= (cnt
< (PAGE_SIZE
* MAX_UPL_TRANSFER
)) ? cnt
: (PAGE_SIZE
* MAX_UPL_TRANSFER
); /* effective max */
962 assert((upl_size_t
) size
== size
);
963 vnode_pageout(vnode_object
->vnode_handle
,
964 NULL
, (upl_offset_t
)0, offset
, (upl_size_t
)size
, upl_flags
, &errno
);
966 if ( (upl_flags
& UPL_KEEPCACHED
) ) {
967 if ( (*io_error
= errno
) )
974 *resid_offset
= offset
;
977 vm_object_offset_t vnode_size
;
978 vm_object_offset_t base_offset
;
981 * this is the pageout path
983 vnode_size
= vnode_pager_get_filesize(vnode_object
->vnode_handle
);
985 if (vnode_size
> (offset
+ PAGE_SIZE
)) {
987 * preset the maximum size of the cluster
988 * and put us on a nice cluster boundary...
989 * and then clip the size to insure we
990 * don't request past the end of the underlying file
992 size
= PAGE_SIZE
* MAX_UPL_TRANSFER
;
993 base_offset
= offset
& ~((signed)(size
- 1));
995 if ((base_offset
+ size
) > vnode_size
)
996 size
= round_page(((vm_size_t
)(vnode_size
- base_offset
)));
999 * we've been requested to page out a page beyond the current
1000 * end of the 'file'... don't try to cluster in this case...
1001 * we still need to send this page through because it might
1002 * be marked precious and the underlying filesystem may need
1003 * to do something with it (besides page it out)...
1005 base_offset
= offset
;
1008 assert((upl_size_t
) size
== size
);
1009 vnode_pageout(vnode_object
->vnode_handle
,
1010 NULL
, (upl_offset_t
)(offset
- base_offset
), base_offset
, (upl_size_t
) size
, UPL_VNODE_PAGER
, NULL
);
1019 vnode_pager_cluster_read(
1020 vnode_pager_t vnode_object
,
1021 vm_object_offset_t base_offset
,
1022 vm_object_offset_t offset
,
1023 uint32_t io_streaming
,
1026 int local_error
= 0;
1030 assert(! (cnt
& PAGE_MASK
));
1033 flags
|= UPL_IOSTREAMING
;
1035 assert((upl_size_t
) cnt
== cnt
);
1036 kret
= vnode_pagein(vnode_object
->vnode_handle
,
1038 (upl_offset_t
) (offset
- base_offset
),
1044 if(kret == PAGER_ABSENT) {
1045 Need to work out the defs here, 1 corresponds to PAGER_ABSENT
1046 defined in bsd/vm/vm_pager.h However, we should not be including
1047 that file here it is a layering violation.
1052 unsigned int count
= 0;
1055 uplflags
= (UPL_NO_SYNC
|
1056 UPL_CLEAN_IN_PLACE
|
1059 assert((upl_size_t
) cnt
== cnt
);
1060 kr
= memory_object_upl_request(vnode_object
->control_handle
,
1061 base_offset
, (upl_size_t
) cnt
,
1062 &upl
, NULL
, &count
, uplflags
);
1063 if (kr
== KERN_SUCCESS
) {
1065 upl_deallocate(upl
);
1068 * We couldn't gather the page list, probably
1069 * because the memory object doesn't have a link
1070 * to a VM object anymore (forced unmount, for
1071 * example). Just return an error to the vm_fault()
1072 * path and let it handle it.
1076 return KERN_FAILURE
;
1079 return KERN_SUCCESS
;
1088 vnode_pager_release_from_cache(
1091 memory_object_free_from_cache(
1092 &realhost
, &vnode_pager_ops
, cnt
);
1099 vnode_object_create(
1102 register vnode_pager_t vnode_object
;
1104 vnode_object
= (struct vnode_pager
*) zalloc(vnode_pager_zone
);
1105 if (vnode_object
== VNODE_PAGER_NULL
)
1106 return(VNODE_PAGER_NULL
);
1109 * The vm_map call takes both named entry ports and raw memory
1110 * objects in the same parameter. We need to make sure that
1111 * vm_map does not see this object as a named entry port. So,
1112 * we reserve the first word in the object for a fake ip_kotype
1113 * setting - that will tell vm_map to use it as a memory object.
1115 vnode_object
->pager_ops
= &vnode_pager_ops
;
1116 vnode_object
->pager_ikot
= IKOT_MEMORY_OBJECT
;
1117 vnode_object
->ref_count
= 1;
1118 vnode_object
->control_handle
= MEMORY_OBJECT_CONTROL_NULL
;
1119 vnode_object
->vnode_handle
= vp
;
1121 return(vnode_object
);
1129 memory_object_t name
)
1131 vnode_pager_t vnode_object
;
1133 vnode_object
= (vnode_pager_t
)name
;
1134 assert(vnode_object
->pager_ops
== &vnode_pager_ops
);
1135 return (vnode_object
);
1139 /*********************** proc_info implementation *************/
1141 #include <sys/bsdtask_info.h>
1143 static int fill_vnodeinfoforaddr( vm_map_entry_t entry
, uintptr_t * vnodeaddr
, uint32_t * vid
);
1147 fill_procregioninfo(task_t task
, uint64_t arg
, struct proc_regioninfo_internal
*pinfo
, uintptr_t *vnodeaddr
, uint32_t *vid
)
1151 vm_map_offset_t address
= (vm_map_offset_t
)arg
;
1152 vm_map_entry_t tmp_entry
;
1153 vm_map_entry_t entry
;
1154 vm_map_offset_t start
;
1155 vm_region_extended_info_data_t extended
;
1156 vm_region_top_info_data_t top
;
1160 if (map
== VM_MAP_NULL
)
1165 vm_map_reference(map
);
1168 vm_map_lock_read(map
);
1171 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
1172 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
1173 vm_map_unlock_read(map
);
1174 vm_map_deallocate(map
);
1181 start
= entry
->vme_start
;
1183 pinfo
->pri_offset
= entry
->offset
;
1184 pinfo
->pri_protection
= entry
->protection
;
1185 pinfo
->pri_max_protection
= entry
->max_protection
;
1186 pinfo
->pri_inheritance
= entry
->inheritance
;
1187 pinfo
->pri_behavior
= entry
->behavior
;
1188 pinfo
->pri_user_wired_count
= entry
->user_wired_count
;
1189 pinfo
->pri_user_tag
= entry
->alias
;
1191 if (entry
->is_sub_map
) {
1192 pinfo
->pri_flags
|= PROC_REGION_SUBMAP
;
1194 if (entry
->is_shared
)
1195 pinfo
->pri_flags
|= PROC_REGION_SHARED
;
1199 extended
.protection
= entry
->protection
;
1200 extended
.user_tag
= entry
->alias
;
1201 extended
.pages_resident
= 0;
1202 extended
.pages_swapped_out
= 0;
1203 extended
.pages_shared_now_private
= 0;
1204 extended
.pages_dirtied
= 0;
1205 extended
.external_pager
= 0;
1206 extended
.shadow_depth
= 0;
1208 vm_map_region_walk(map
, start
, entry
, entry
->offset
, entry
->vme_end
- start
, &extended
);
1210 if (extended
.external_pager
&& extended
.ref_count
== 2 && extended
.share_mode
== SM_SHARED
)
1211 extended
.share_mode
= SM_PRIVATE
;
1213 top
.private_pages_resident
= 0;
1214 top
.shared_pages_resident
= 0;
1215 vm_map_region_top_walk(entry
, &top
);
1218 pinfo
->pri_pages_resident
= extended
.pages_resident
;
1219 pinfo
->pri_pages_shared_now_private
= extended
.pages_shared_now_private
;
1220 pinfo
->pri_pages_swapped_out
= extended
.pages_swapped_out
;
1221 pinfo
->pri_pages_dirtied
= extended
.pages_dirtied
;
1222 pinfo
->pri_ref_count
= extended
.ref_count
;
1223 pinfo
->pri_shadow_depth
= extended
.shadow_depth
;
1224 pinfo
->pri_share_mode
= extended
.share_mode
;
1226 pinfo
->pri_private_pages_resident
= top
.private_pages_resident
;
1227 pinfo
->pri_shared_pages_resident
= top
.shared_pages_resident
;
1228 pinfo
->pri_obj_id
= top
.obj_id
;
1230 pinfo
->pri_address
= (uint64_t)start
;
1231 pinfo
->pri_size
= (uint64_t)(entry
->vme_end
- start
);
1232 pinfo
->pri_depth
= 0;
1234 if ((vnodeaddr
!= 0) && (entry
->is_sub_map
== 0)) {
1235 *vnodeaddr
= (uintptr_t)0;
1237 if (fill_vnodeinfoforaddr(entry
, vnodeaddr
, vid
) ==0) {
1238 vm_map_unlock_read(map
);
1239 vm_map_deallocate(map
);
1244 vm_map_unlock_read(map
);
1245 vm_map_deallocate(map
);
1250 fill_vnodeinfoforaddr(
1251 vm_map_entry_t entry
,
1252 uintptr_t * vnodeaddr
,
1255 vm_object_t top_object
, object
;
1256 memory_object_t memory_object
;
1257 memory_object_pager_ops_t pager_ops
;
1262 if (entry
->is_sub_map
) {
1266 * The last object in the shadow chain has the
1267 * relevant pager information.
1269 top_object
= entry
->object
.vm_object
;
1270 if (top_object
== VM_OBJECT_NULL
) {
1271 object
= VM_OBJECT_NULL
;
1274 vm_object_lock(top_object
);
1275 for (object
= top_object
, shadow_depth
= 0;
1276 object
->shadow
!= VM_OBJECT_NULL
;
1277 object
= object
->shadow
, shadow_depth
++) {
1278 vm_object_lock(object
->shadow
);
1279 vm_object_unlock(object
);
1284 if (object
== VM_OBJECT_NULL
) {
1286 } else if (object
->internal
) {
1287 vm_object_unlock(object
);
1289 } else if (! object
->pager_ready
||
1290 object
->terminating
||
1292 vm_object_unlock(object
);
1295 memory_object
= object
->pager
;
1296 pager_ops
= memory_object
->mo_pager_ops
;
1297 if (pager_ops
== &vnode_pager_ops
) {
1298 kr
= vnode_pager_get_object_vnode(
1301 if (kr
!= KERN_SUCCESS
) {
1302 vm_object_unlock(object
);
1306 vm_object_unlock(object
);
1310 vm_object_unlock(object
);
1315 vnode_pager_get_object_vnode (
1316 memory_object_t mem_obj
,
1317 uintptr_t * vnodeaddr
,
1320 vnode_pager_t vnode_object
;
1322 vnode_object
= vnode_pager_lookup(mem_obj
);
1323 if (vnode_object
->vnode_handle
) {
1324 *vnodeaddr
= (uintptr_t)vnode_object
->vnode_handle
;
1325 *vid
= (uint32_t)vnode_vid((void *)vnode_object
->vnode_handle
);
1327 return(KERN_SUCCESS
);
1330 return(KERN_FAILURE
);
1335 * Find the underlying vnode object for the given vm_map_entry. If found, return with the
1336 * object locked, otherwise return NULL with nothing locked.
1341 vm_map_entry_t entry
1344 vm_object_t top_object
, object
;
1345 memory_object_t memory_object
;
1346 memory_object_pager_ops_t pager_ops
;
1348 if (!entry
->is_sub_map
) {
1351 * The last object in the shadow chain has the
1352 * relevant pager information.
1355 top_object
= entry
->object
.vm_object
;
1358 vm_object_lock(top_object
);
1360 for (object
= top_object
; object
->shadow
!= VM_OBJECT_NULL
; object
= object
->shadow
) {
1361 vm_object_lock(object
->shadow
);
1362 vm_object_unlock(object
);
1365 if (object
&& !object
->internal
&& object
->pager_ready
&& !object
->terminating
&&
1367 memory_object
= object
->pager
;
1368 pager_ops
= memory_object
->mo_pager_ops
;
1371 * If this object points to the vnode_pager_ops, then we found what we're
1372 * looking for. Otherwise, this vm_map_entry doesn't have an underlying
1373 * vnode and so we fall through to the bottom and return NULL.
1376 if (pager_ops
== &vnode_pager_ops
)
1377 return object
; /* we return with the object locked */
1380 vm_object_unlock(object
);
1385 return(VM_OBJECT_NULL
);