2 * Copyright (c) 2000-2020 Apple 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/ledger.h>
45 #include <kern/thread.h>
46 #include <kern/ipc_kobject.h>
47 #include <os/refcnt.h>
49 #include <ipc/ipc_port.h>
50 #include <ipc/ipc_space.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 .memory_object_reference
= vnode_pager_reference
,
101 .memory_object_deallocate
= vnode_pager_deallocate
,
102 .memory_object_init
= vnode_pager_init
,
103 .memory_object_terminate
= vnode_pager_terminate
,
104 .memory_object_data_request
= vnode_pager_data_request
,
105 .memory_object_data_return
= vnode_pager_data_return
,
106 .memory_object_data_initialize
= vnode_pager_data_initialize
,
107 .memory_object_data_unlock
= vnode_pager_data_unlock
,
108 .memory_object_synchronize
= vnode_pager_synchronize
,
109 .memory_object_map
= vnode_pager_map
,
110 .memory_object_last_unmap
= vnode_pager_last_unmap
,
111 .memory_object_data_reclaim
= NULL
,
112 .memory_object_backing_object
= NULL
,
113 .memory_object_pager_name
= "vnode pager"
116 typedef struct vnode_pager
{
117 /* mandatory generic header */
118 struct memory_object vn_pgr_hdr
;
121 #if MEMORY_OBJECT_HAS_REFCOUNT
122 #define vn_pgr_hdr_ref vn_pgr_hdr.mo_ref
124 os_ref_atomic_t vn_pgr_hdr_ref
;
126 struct vnode
*vnode_handle
; /* vnode handle */
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 */
157 vnode_pager_lookup_vnode( /* forward */
160 ZONE_DECLARE(vnode_pager_zone
, "vnode pager structures",
161 sizeof(struct vnode_pager
), ZC_NOENCRYPT
);
163 #define VNODE_PAGER_NULL ((vnode_pager_t) 0)
165 /* TODO: Should be set dynamically by vnode_pager_init() */
166 #define CLUSTER_SHIFT 1
172 #define PAGER_ALL 0xffffffff
173 #define PAGER_INIT 0x00000001
174 #define PAGER_PAGEIN 0x00000002
176 #define PAGER_DEBUG(LEVEL, A) {if ((pagerdebug & LEVEL)==LEVEL){printf A;}}
178 #define PAGER_DEBUG(LEVEL, A)
181 extern int proc_resetpcontrol(int);
184 extern int uiomove64(addr64_t
, int, void *);
188 memory_object_control_uiomove(
189 memory_object_control_t control
,
190 memory_object_offset_t offset
,
205 vm_page_t page_run
[MAX_RUN
];
206 int dirty_count
; /* keeps track of number of pages dirtied as part of this uiomove */
208 object
= memory_object_control_to_vm_object(control
);
209 if (object
== VM_OBJECT_NULL
) {
212 assert(!object
->internal
);
214 vm_object_lock(object
);
216 if (mark_dirty
&& object
->copy
!= VM_OBJECT_NULL
) {
218 * We can't modify the pages without honoring
219 * copy-on-write obligations first, so fall off
220 * this optimized path and fall back to the regular
223 vm_object_unlock(object
);
226 orig_offset
= start_offset
;
229 while (io_requested
&& retval
== 0) {
230 cur_needed
= (start_offset
+ io_requested
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
232 if (cur_needed
> MAX_RUN
) {
233 cur_needed
= MAX_RUN
;
236 for (cur_run
= 0; cur_run
< cur_needed
;) {
237 if ((dst_page
= vm_page_lookup(object
, offset
)) == VM_PAGE_NULL
) {
242 if (dst_page
->vmp_busy
|| dst_page
->vmp_cleaning
) {
244 * someone else is playing with the page... if we've
245 * already collected pages into this run, go ahead
246 * and process now, we can't block on this
247 * page while holding other pages in the BUSY state
248 * otherwise we will wait
253 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
256 if (dst_page
->vmp_laundry
) {
257 vm_pageout_steal_laundry(dst_page
, FALSE
);
261 if (dst_page
->vmp_dirty
== FALSE
) {
264 SET_PAGE_DIRTY(dst_page
, FALSE
);
265 if (dst_page
->vmp_cs_validated
&&
266 !dst_page
->vmp_cs_tainted
) {
269 * We're modifying a code-signed
270 * page: force revalidate
272 dst_page
->vmp_cs_validated
= VMP_CS_ALL_FALSE
;
274 VM_PAGEOUT_DEBUG(vm_cs_validated_resets
, 1);
276 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(dst_page
));
279 dst_page
->vmp_busy
= TRUE
;
281 page_run
[cur_run
++] = dst_page
;
283 offset
+= PAGE_SIZE_64
;
287 * we hit a 'hole' in the cache or
288 * a page we don't want to try to handle,
289 * so bail at this point
290 * we'll unlock the object below
294 vm_object_unlock(object
);
296 for (i
= 0; i
< cur_run
; i
++) {
297 dst_page
= page_run
[i
];
299 if ((xsize
= PAGE_SIZE
- start_offset
) > io_requested
) {
300 xsize
= io_requested
;
303 if ((retval
= uiomove64((addr64_t
)(((addr64_t
)(VM_PAGE_GET_PHYS_PAGE(dst_page
)) << PAGE_SHIFT
) + start_offset
), xsize
, uio
))) {
307 io_requested
-= xsize
;
310 vm_object_lock(object
);
313 * if we have more than 1 page to work on
314 * in the current run, or the original request
315 * started at offset 0 of the page, or we're
316 * processing multiple batches, we will move
317 * the pages to the tail of the inactive queue
318 * to implement an LRU for read/write accesses
320 * the check for orig_offset == 0 is there to
321 * mitigate the cost of small (< page_size) requests
322 * to the same page (this way we only move it once)
324 if (take_reference
&& (cur_run
> 1 || orig_offset
== 0)) {
325 vm_page_lockspin_queues();
327 for (i
= 0; i
< cur_run
; i
++) {
328 vm_page_lru(page_run
[i
]);
331 vm_page_unlock_queues();
333 for (i
= 0; i
< cur_run
; i
++) {
334 dst_page
= page_run
[i
];
337 * someone is explicitly referencing this page...
338 * update clustered and speculative state
341 if (dst_page
->vmp_clustered
) {
342 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
345 PAGE_WAKEUP_DONE(dst_page
);
350 task_update_logical_writes(current_task(), (dirty_count
* PAGE_SIZE
), TASK_WRITE_DEFERRED
, vnode_pager_lookup_vnode(object
->pager
));
352 vm_object_unlock(object
);
363 __unused memory_object_t pager
)
365 vnode_pager_t vnode_object
;
367 vnode_object
= vnode_object_create(vp
);
368 if (vnode_object
== VNODE_PAGER_NULL
) {
369 panic("vnode_pager_setup: vnode_object_create() failed");
371 return (memory_object_t
)vnode_object
;
378 vnode_pager_init(memory_object_t mem_obj
,
379 memory_object_control_t control
,
383 memory_object_cluster_size_t pg_size
)
385 vnode_pager_t vnode_object
;
387 memory_object_attr_info_data_t attributes
;
390 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_init: %p, %p, %lx\n", mem_obj
, control
, (unsigned long)pg_size
));
392 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
393 return KERN_INVALID_ARGUMENT
;
396 vnode_object
= vnode_pager_lookup(mem_obj
);
398 memory_object_control_reference(control
);
400 vnode_object
->vn_pgr_hdr
.mo_control
= control
;
402 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
403 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
404 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
405 attributes
.may_cache_object
= TRUE
;
406 attributes
.temporary
= TRUE
;
408 kr
= memory_object_change_attributes(
410 MEMORY_OBJECT_ATTRIBUTE_INFO
,
411 (memory_object_info_t
) &attributes
,
412 MEMORY_OBJECT_ATTR_INFO_COUNT
);
413 if (kr
!= KERN_SUCCESS
) {
414 panic("vnode_pager_init: memory_object_change_attributes() failed");
424 vnode_pager_data_return(
425 memory_object_t mem_obj
,
426 memory_object_offset_t offset
,
427 memory_object_cluster_size_t data_cnt
,
428 memory_object_offset_t
*resid_offset
,
430 __unused boolean_t dirty
,
431 __unused boolean_t kernel_copy
,
434 vnode_pager_t vnode_object
;
436 assertf(page_aligned(offset
), "offset 0x%llx\n", offset
);
438 vnode_object
= vnode_pager_lookup(mem_obj
);
440 vnode_pager_cluster_write(vnode_object
, offset
, data_cnt
, resid_offset
, io_error
, upl_flags
);
446 vnode_pager_data_initialize(
447 __unused memory_object_t mem_obj
,
448 __unused memory_object_offset_t offset
,
449 __unused memory_object_cluster_size_t data_cnt
)
451 panic("vnode_pager_data_initialize");
456 vnode_pager_data_unlock(
457 __unused memory_object_t mem_obj
,
458 __unused memory_object_offset_t offset
,
459 __unused memory_object_size_t size
,
460 __unused vm_prot_t desired_access
)
467 memory_object_t mem_obj
,
468 vm_object_offset_t s_offset
,
469 vm_object_offset_t e_offset
)
471 vnode_pager_t vnode_object
;
473 if (mem_obj
&& mem_obj
->mo_pager_ops
== &vnode_pager_ops
) {
474 vnode_object
= vnode_pager_lookup(mem_obj
);
475 vnode_pager_was_dirtied(vnode_object
->vnode_handle
, s_offset
, e_offset
);
480 vnode_pager_get_isinuse(
481 memory_object_t mem_obj
,
484 vnode_pager_t vnode_object
;
486 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
488 return KERN_INVALID_ARGUMENT
;
491 vnode_object
= vnode_pager_lookup(mem_obj
);
493 *isinuse
= vnode_pager_isinuse(vnode_object
->vnode_handle
);
498 vnode_pager_get_throttle_io_limit(
499 memory_object_t mem_obj
,
502 vnode_pager_t vnode_object
;
504 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
505 return KERN_INVALID_ARGUMENT
;
508 vnode_object
= vnode_pager_lookup(mem_obj
);
510 (void)vnode_pager_return_throttle_io_limit(vnode_object
->vnode_handle
, limit
);
515 vnode_pager_get_isSSD(
516 memory_object_t mem_obj
,
519 vnode_pager_t vnode_object
;
521 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
522 return KERN_INVALID_ARGUMENT
;
525 vnode_object
= vnode_pager_lookup(mem_obj
);
527 *isSSD
= vnode_pager_isSSD(vnode_object
->vnode_handle
);
532 vnode_pager_get_object_size(
533 memory_object_t mem_obj
,
534 memory_object_offset_t
*length
)
536 vnode_pager_t vnode_object
;
538 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
540 return KERN_INVALID_ARGUMENT
;
543 vnode_object
= vnode_pager_lookup(mem_obj
);
545 *length
= vnode_pager_get_filesize(vnode_object
->vnode_handle
);
550 vnode_pager_get_object_name(
551 memory_object_t mem_obj
,
553 vm_size_t pathname_len
,
555 vm_size_t filename_len
,
556 boolean_t
*truncated_path_p
)
558 vnode_pager_t vnode_object
;
560 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
561 return KERN_INVALID_ARGUMENT
;
564 vnode_object
= vnode_pager_lookup(mem_obj
);
566 return vnode_pager_get_name(vnode_object
->vnode_handle
,
575 vnode_pager_get_object_mtime(
576 memory_object_t mem_obj
,
577 struct timespec
*mtime
,
578 struct timespec
*cs_mtime
)
580 vnode_pager_t vnode_object
;
582 if (mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
583 return KERN_INVALID_ARGUMENT
;
586 vnode_object
= vnode_pager_lookup(mem_obj
);
588 return vnode_pager_get_mtime(vnode_object
->vnode_handle
,
593 #if CHECK_CS_VALIDATION_BITMAP
595 vnode_pager_cs_check_validation_bitmap(
596 memory_object_t mem_obj
,
597 memory_object_offset_t offset
,
600 vnode_pager_t vnode_object
;
602 if (mem_obj
== MEMORY_OBJECT_NULL
||
603 mem_obj
->mo_pager_ops
!= &vnode_pager_ops
) {
604 return KERN_INVALID_ARGUMENT
;
607 vnode_object
= vnode_pager_lookup(mem_obj
);
608 return ubc_cs_check_validation_bitmap( vnode_object
->vnode_handle
, offset
, optype
);
610 #endif /* CHECK_CS_VALIDATION_BITMAP */
616 vnode_pager_data_request(
617 memory_object_t mem_obj
,
618 memory_object_offset_t offset
,
619 __unused memory_object_cluster_size_t length
,
620 __unused vm_prot_t desired_access
,
621 memory_object_fault_info_t fault_info
)
623 vnode_pager_t vnode_object
;
624 memory_object_offset_t base_offset
;
626 uint32_t io_streaming
= 0;
628 assertf(page_aligned(offset
), "offset 0x%llx\n", offset
);
630 vnode_object
= vnode_pager_lookup(mem_obj
);
632 size
= MAX_UPL_TRANSFER_BYTES
;
633 base_offset
= offset
;
635 if (memory_object_cluster_size(vnode_object
->vn_pgr_hdr
.mo_control
,
636 &base_offset
, &size
, &io_streaming
,
637 fault_info
) != KERN_SUCCESS
) {
641 assert(offset
>= base_offset
&&
642 offset
< base_offset
+ size
);
644 return vnode_pager_cluster_read(vnode_object
, base_offset
, offset
, io_streaming
, size
);
651 vnode_pager_reference(
652 memory_object_t mem_obj
)
654 vnode_pager_t vnode_object
;
656 vnode_object
= vnode_pager_lookup(mem_obj
);
657 os_ref_retain_raw(&vnode_object
->vn_pgr_hdr_ref
, NULL
);
664 vnode_pager_deallocate(
665 memory_object_t mem_obj
)
667 vnode_pager_t vnode_object
;
669 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_deallocate: %p\n", mem_obj
));
671 vnode_object
= vnode_pager_lookup(mem_obj
);
673 if (os_ref_release_raw(&vnode_object
->vn_pgr_hdr_ref
, NULL
) == 0) {
674 if (vnode_object
->vnode_handle
!= NULL
) {
675 vnode_pager_vrele(vnode_object
->vnode_handle
);
677 zfree(vnode_pager_zone
, vnode_object
);
685 vnode_pager_terminate(
689 memory_object_t mem_obj
)
691 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_terminate: %p\n", mem_obj
));
700 vnode_pager_synchronize(
701 __unused memory_object_t mem_obj
,
702 __unused memory_object_offset_t offset
,
703 __unused memory_object_size_t length
,
704 __unused vm_sync_t sync_flags
)
706 panic("vnode_pager_synchronize: memory_object_synchronize no longer supported\n");
715 memory_object_t mem_obj
,
718 vnode_pager_t vnode_object
;
722 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_map: %p %x\n", mem_obj
, prot
));
724 vnode_object
= vnode_pager_lookup(mem_obj
);
726 ret
= ubc_map(vnode_object
->vnode_handle
, prot
);
738 vnode_pager_last_unmap(
739 memory_object_t mem_obj
)
741 vnode_pager_t vnode_object
;
743 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_last_unmap: %p\n", mem_obj
));
745 vnode_object
= vnode_pager_lookup(mem_obj
);
747 ubc_unmap(vnode_object
->vnode_handle
);
757 vnode_pager_cluster_write(
758 vnode_pager_t vnode_object
,
759 vm_object_offset_t offset
,
761 vm_object_offset_t
* resid_offset
,
768 if (upl_flags
& UPL_MSYNC
) {
769 upl_flags
|= UPL_VNODE_PAGER
;
771 if ((upl_flags
& UPL_IOSYNC
) && io_error
) {
772 upl_flags
|= UPL_KEEPCACHED
;
776 size
= (cnt
< MAX_UPL_TRANSFER_BYTES
) ? cnt
: MAX_UPL_TRANSFER_BYTES
; /* effective max */
778 assert((upl_size_t
) size
== size
);
779 vnode_pageout(vnode_object
->vnode_handle
,
780 NULL
, (upl_offset_t
)0, offset
, (upl_size_t
)size
, upl_flags
, &errno
);
782 if ((upl_flags
& UPL_KEEPCACHED
)) {
783 if ((*io_error
= errno
)) {
791 *resid_offset
= offset
;
794 vm_object_offset_t vnode_size
;
795 vm_object_offset_t base_offset
;
798 * this is the pageout path
800 vnode_size
= vnode_pager_get_filesize(vnode_object
->vnode_handle
);
802 if (vnode_size
> (offset
+ PAGE_SIZE
)) {
804 * preset the maximum size of the cluster
805 * and put us on a nice cluster boundary...
806 * and then clip the size to insure we
807 * don't request past the end of the underlying file
809 size
= MAX_UPL_TRANSFER_BYTES
;
810 base_offset
= offset
& ~((signed)(size
- 1));
812 if ((base_offset
+ size
) > vnode_size
) {
813 size
= round_page(((vm_size_t
)(vnode_size
- base_offset
)));
817 * we've been requested to page out a page beyond the current
818 * end of the 'file'... don't try to cluster in this case...
819 * we still need to send this page through because it might
820 * be marked precious and the underlying filesystem may need
821 * to do something with it (besides page it out)...
823 base_offset
= offset
;
826 assert((upl_size_t
) size
== size
);
827 vnode_pageout(vnode_object
->vnode_handle
,
828 NULL
, (upl_offset_t
)(offset
- base_offset
), base_offset
, (upl_size_t
) size
,
829 (upl_flags
& UPL_IOSYNC
) | UPL_VNODE_PAGER
, NULL
);
838 vnode_pager_cluster_read(
839 vnode_pager_t vnode_object
,
840 vm_object_offset_t base_offset
,
841 vm_object_offset_t offset
,
842 uint32_t io_streaming
,
849 assert(!(cnt
& PAGE_MASK
));
852 flags
|= UPL_IOSTREAMING
;
855 assert((upl_size_t
) cnt
== cnt
);
856 kret
= vnode_pagein(vnode_object
->vnode_handle
,
858 (upl_offset_t
) (offset
- base_offset
),
864 * if(kret == PAGER_ABSENT) {
865 * Need to work out the defs here, 1 corresponds to PAGER_ABSENT
866 * defined in bsd/vm/vm_pager.h However, we should not be including
867 * that file here it is a layering violation.
872 unsigned int count
= 0;
875 uplflags
= (UPL_NO_SYNC
|
879 assert((upl_size_t
) cnt
== cnt
);
880 kr
= memory_object_upl_request(vnode_object
->vn_pgr_hdr
.mo_control
,
881 base_offset
, (upl_size_t
) cnt
,
882 &upl
, NULL
, &count
, uplflags
, VM_KERN_MEMORY_NONE
);
883 if (kr
== KERN_SUCCESS
) {
888 * We couldn't gather the page list, probably
889 * because the memory object doesn't have a link
890 * to a VM object anymore (forced unmount, for
891 * example). Just return an error to the vm_fault()
892 * path and let it handle it.
909 vnode_pager_t vnode_object
;
911 vnode_object
= (struct vnode_pager
*) zalloc(vnode_pager_zone
);
912 if (vnode_object
== VNODE_PAGER_NULL
) {
913 return VNODE_PAGER_NULL
;
917 * The vm_map call takes both named entry ports and raw memory
918 * objects in the same parameter. We need to make sure that
919 * vm_map does not see this object as a named entry port. So,
920 * we reserve the first word in the object for a fake ip_kotype
921 * setting - that will tell vm_map to use it as a memory object.
923 vnode_object
->vn_pgr_hdr
.mo_ikot
= IKOT_MEMORY_OBJECT
;
924 vnode_object
->vn_pgr_hdr
.mo_pager_ops
= &vnode_pager_ops
;
925 vnode_object
->vn_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
927 os_ref_init_raw(&vnode_object
->vn_pgr_hdr_ref
, NULL
);
928 vnode_object
->vnode_handle
= vp
;
938 memory_object_t name
)
940 vnode_pager_t vnode_object
;
942 vnode_object
= (vnode_pager_t
)name
;
943 assert(vnode_object
->vn_pgr_hdr
.mo_pager_ops
== &vnode_pager_ops
);
949 vnode_pager_lookup_vnode(
950 memory_object_t name
)
952 vnode_pager_t vnode_object
;
953 vnode_object
= (vnode_pager_t
)name
;
954 if (vnode_object
->vn_pgr_hdr
.mo_pager_ops
== &vnode_pager_ops
) {
955 return vnode_object
->vnode_handle
;
961 /*********************** proc_info implementation *************/
963 #include <sys/bsdtask_info.h>
965 static int fill_vnodeinfoforaddr( vm_map_entry_t entry
, uintptr_t * vnodeaddr
, uint32_t * vid
);
968 fill_procregioninfo(task_t task
, uint64_t arg
, struct proc_regioninfo_internal
*pinfo
, uintptr_t *vnodeaddr
, uint32_t *vid
)
971 vm_map_offset_t address
= (vm_map_offset_t
)arg
;
972 vm_map_entry_t tmp_entry
;
973 vm_map_entry_t entry
;
974 vm_map_offset_t start
;
975 vm_region_extended_info_data_t extended
;
976 vm_region_top_info_data_t top
;
977 boolean_t do_region_footprint
;
978 int effective_page_shift
, effective_page_size
;
982 if (map
== VM_MAP_NULL
) {
987 effective_page_shift
= vm_self_region_page_shift(map
);
988 effective_page_size
= (1 << effective_page_shift
);
990 vm_map_reference(map
);
993 do_region_footprint
= task_self_region_footprint();
995 vm_map_lock_read(map
);
999 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
1000 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
1001 if (do_region_footprint
&&
1002 address
== tmp_entry
->vme_end
) {
1003 ledger_amount_t ledger_resident
;
1004 ledger_amount_t ledger_compressed
;
1007 * This request is right after the last valid
1008 * memory region; instead of reporting the
1009 * end of the address space, report a fake
1010 * memory region to account for non-volatile
1011 * purgeable and/or ledger-tagged memory
1012 * owned by this task.
1014 task_ledgers_footprint(task
->ledger
,
1016 &ledger_compressed
);
1017 if (ledger_resident
+ ledger_compressed
== 0) {
1018 /* nothing to report */
1019 vm_map_unlock_read(map
);
1020 vm_map_deallocate(map
);
1024 /* provide fake region for purgeable */
1025 pinfo
->pri_offset
= address
;
1026 pinfo
->pri_protection
= VM_PROT_DEFAULT
;
1027 pinfo
->pri_max_protection
= VM_PROT_DEFAULT
;
1028 pinfo
->pri_inheritance
= VM_INHERIT_NONE
;
1029 pinfo
->pri_behavior
= VM_BEHAVIOR_DEFAULT
;
1030 pinfo
->pri_user_wired_count
= 0;
1031 pinfo
->pri_user_tag
= -1;
1032 pinfo
->pri_pages_resident
=
1033 (uint32_t) (ledger_resident
/ effective_page_size
);
1034 pinfo
->pri_pages_shared_now_private
= 0;
1035 pinfo
->pri_pages_swapped_out
=
1036 (uint32_t) (ledger_compressed
/ effective_page_size
);
1037 pinfo
->pri_pages_dirtied
=
1038 (uint32_t) (ledger_resident
/ effective_page_size
);
1039 pinfo
->pri_ref_count
= 1;
1040 pinfo
->pri_shadow_depth
= 0;
1041 pinfo
->pri_share_mode
= SM_PRIVATE
;
1042 pinfo
->pri_private_pages_resident
=
1043 (uint32_t) (ledger_resident
/ effective_page_size
);
1044 pinfo
->pri_shared_pages_resident
= 0;
1045 pinfo
->pri_obj_id
= VM_OBJECT_ID_FAKE(map
, task_ledgers
.purgeable_nonvolatile
);
1046 pinfo
->pri_address
= address
;
1048 (uint64_t) (ledger_resident
+ ledger_compressed
);
1049 pinfo
->pri_depth
= 0;
1051 vm_map_unlock_read(map
);
1052 vm_map_deallocate(map
);
1055 vm_map_unlock_read(map
);
1056 vm_map_deallocate(map
);
1063 start
= entry
->vme_start
;
1065 pinfo
->pri_offset
= VME_OFFSET(entry
);
1066 pinfo
->pri_protection
= entry
->protection
;
1067 pinfo
->pri_max_protection
= entry
->max_protection
;
1068 pinfo
->pri_inheritance
= entry
->inheritance
;
1069 pinfo
->pri_behavior
= entry
->behavior
;
1070 pinfo
->pri_user_wired_count
= entry
->user_wired_count
;
1071 pinfo
->pri_user_tag
= VME_ALIAS(entry
);
1073 if (entry
->is_sub_map
) {
1074 pinfo
->pri_flags
|= PROC_REGION_SUBMAP
;
1076 if (entry
->is_shared
) {
1077 pinfo
->pri_flags
|= PROC_REGION_SHARED
;
1082 extended
.protection
= entry
->protection
;
1083 extended
.user_tag
= VME_ALIAS(entry
);
1084 extended
.pages_resident
= 0;
1085 extended
.pages_swapped_out
= 0;
1086 extended
.pages_shared_now_private
= 0;
1087 extended
.pages_dirtied
= 0;
1088 extended
.external_pager
= 0;
1089 extended
.shadow_depth
= 0;
1091 vm_map_region_walk(map
, start
, entry
, VME_OFFSET(entry
), entry
->vme_end
- start
, &extended
, TRUE
, VM_REGION_EXTENDED_INFO_COUNT
);
1093 if (extended
.external_pager
&& extended
.ref_count
== 2 && extended
.share_mode
== SM_SHARED
) {
1094 extended
.share_mode
= SM_PRIVATE
;
1097 top
.private_pages_resident
= 0;
1098 top
.shared_pages_resident
= 0;
1099 vm_map_region_top_walk(entry
, &top
);
1102 pinfo
->pri_pages_resident
= extended
.pages_resident
;
1103 pinfo
->pri_pages_shared_now_private
= extended
.pages_shared_now_private
;
1104 pinfo
->pri_pages_swapped_out
= extended
.pages_swapped_out
;
1105 pinfo
->pri_pages_dirtied
= extended
.pages_dirtied
;
1106 pinfo
->pri_ref_count
= extended
.ref_count
;
1107 pinfo
->pri_shadow_depth
= extended
.shadow_depth
;
1108 pinfo
->pri_share_mode
= extended
.share_mode
;
1110 pinfo
->pri_private_pages_resident
= top
.private_pages_resident
;
1111 pinfo
->pri_shared_pages_resident
= top
.shared_pages_resident
;
1112 pinfo
->pri_obj_id
= top
.obj_id
;
1114 pinfo
->pri_address
= (uint64_t)start
;
1115 pinfo
->pri_size
= (uint64_t)(entry
->vme_end
- start
);
1116 pinfo
->pri_depth
= 0;
1118 if ((vnodeaddr
!= 0) && (entry
->is_sub_map
== 0)) {
1119 *vnodeaddr
= (uintptr_t)0;
1121 if (fill_vnodeinfoforaddr(entry
, vnodeaddr
, vid
) == 0) {
1122 vm_map_unlock_read(map
);
1123 vm_map_deallocate(map
);
1128 vm_map_unlock_read(map
);
1129 vm_map_deallocate(map
);
1134 fill_procregioninfo_onlymappedvnodes(task_t task
, uint64_t arg
, struct proc_regioninfo_internal
*pinfo
, uintptr_t *vnodeaddr
, uint32_t *vid
)
1137 vm_map_offset_t address
= (vm_map_offset_t
)arg
;
1138 vm_map_entry_t tmp_entry
;
1139 vm_map_entry_t entry
;
1143 if (map
== VM_MAP_NULL
) {
1147 vm_map_reference(map
);
1150 vm_map_lock_read(map
);
1152 if (!vm_map_lookup_entry(map
, address
, &tmp_entry
)) {
1153 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
1154 vm_map_unlock_read(map
);
1155 vm_map_deallocate(map
);
1162 while (entry
!= vm_map_to_entry(map
)) {
1166 if (entry
->is_sub_map
== 0) {
1167 if (fill_vnodeinfoforaddr(entry
, vnodeaddr
, vid
)) {
1168 pinfo
->pri_offset
= VME_OFFSET(entry
);
1169 pinfo
->pri_protection
= entry
->protection
;
1170 pinfo
->pri_max_protection
= entry
->max_protection
;
1171 pinfo
->pri_inheritance
= entry
->inheritance
;
1172 pinfo
->pri_behavior
= entry
->behavior
;
1173 pinfo
->pri_user_wired_count
= entry
->user_wired_count
;
1174 pinfo
->pri_user_tag
= VME_ALIAS(entry
);
1176 if (entry
->is_shared
) {
1177 pinfo
->pri_flags
|= PROC_REGION_SHARED
;
1180 pinfo
->pri_pages_resident
= 0;
1181 pinfo
->pri_pages_shared_now_private
= 0;
1182 pinfo
->pri_pages_swapped_out
= 0;
1183 pinfo
->pri_pages_dirtied
= 0;
1184 pinfo
->pri_ref_count
= 0;
1185 pinfo
->pri_shadow_depth
= 0;
1186 pinfo
->pri_share_mode
= 0;
1188 pinfo
->pri_private_pages_resident
= 0;
1189 pinfo
->pri_shared_pages_resident
= 0;
1190 pinfo
->pri_obj_id
= 0;
1192 pinfo
->pri_address
= (uint64_t)entry
->vme_start
;
1193 pinfo
->pri_size
= (uint64_t)(entry
->vme_end
- entry
->vme_start
);
1194 pinfo
->pri_depth
= 0;
1196 vm_map_unlock_read(map
);
1197 vm_map_deallocate(map
);
1202 /* Keep searching for a vnode-backed mapping */
1203 entry
= entry
->vme_next
;
1206 vm_map_unlock_read(map
);
1207 vm_map_deallocate(map
);
1212 find_region_details(task_t task
, vm_map_offset_t offset
,
1213 uintptr_t *vnodeaddr
, uint32_t *vid
,
1214 uint64_t *start
, uint64_t *len
)
1217 vm_map_entry_t tmp_entry
, entry
;
1222 if (map
== VM_MAP_NULL
) {
1226 vm_map_reference(map
);
1229 vm_map_lock_read(map
);
1230 if (!vm_map_lookup_entry(map
, offset
, &tmp_entry
)) {
1231 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
1239 while (entry
!= vm_map_to_entry(map
)) {
1245 if (entry
->is_sub_map
== 0) {
1246 if (fill_vnodeinfoforaddr(entry
, vnodeaddr
, vid
)) {
1247 *start
= entry
->vme_start
;
1248 *len
= entry
->vme_end
- entry
->vme_start
;
1254 entry
= entry
->vme_next
;
1258 vm_map_unlock_read(map
);
1259 vm_map_deallocate(map
);
1264 fill_vnodeinfoforaddr(
1265 vm_map_entry_t entry
,
1266 uintptr_t * vnodeaddr
,
1269 vm_object_t top_object
, object
;
1270 memory_object_t memory_object
;
1271 memory_object_pager_ops_t pager_ops
;
1276 if (entry
->is_sub_map
) {
1280 * The last object in the shadow chain has the
1281 * relevant pager information.
1283 top_object
= VME_OBJECT(entry
);
1284 if (top_object
== VM_OBJECT_NULL
) {
1285 object
= VM_OBJECT_NULL
;
1288 vm_object_lock(top_object
);
1289 for (object
= top_object
, shadow_depth
= 0;
1290 object
->shadow
!= VM_OBJECT_NULL
;
1291 object
= object
->shadow
, shadow_depth
++) {
1292 vm_object_lock(object
->shadow
);
1293 vm_object_unlock(object
);
1298 if (object
== VM_OBJECT_NULL
) {
1300 } else if (object
->internal
) {
1301 vm_object_unlock(object
);
1303 } else if (!object
->pager_ready
||
1304 object
->terminating
||
1306 vm_object_unlock(object
);
1309 memory_object
= object
->pager
;
1310 pager_ops
= memory_object
->mo_pager_ops
;
1311 if (pager_ops
== &vnode_pager_ops
) {
1312 kr
= vnode_pager_get_object_vnode(
1315 if (kr
!= KERN_SUCCESS
) {
1316 vm_object_unlock(object
);
1320 vm_object_unlock(object
);
1324 vm_object_unlock(object
);
1329 vnode_pager_get_object_vnode(
1330 memory_object_t mem_obj
,
1331 uintptr_t * vnodeaddr
,
1334 vnode_pager_t vnode_object
;
1336 vnode_object
= vnode_pager_lookup(mem_obj
);
1337 if (vnode_object
->vnode_handle
) {
1338 *vnodeaddr
= (uintptr_t)vnode_object
->vnode_handle
;
1339 *vid
= (uint32_t)vnode_vid((void *)vnode_object
->vnode_handle
);
1341 return KERN_SUCCESS
;
1344 return KERN_FAILURE
;
1349 vnode_pager_get_object_devvp(
1350 memory_object_t mem_obj
,
1356 if (vnode_pager_get_object_vnode(mem_obj
, (uintptr_t *)&vp
, (uint32_t *)&vid
) != KERN_SUCCESS
) {
1357 return KERN_FAILURE
;
1359 *devvp
= (uintptr_t)vnode_mountdevvp(vp
);
1361 return KERN_SUCCESS
;
1363 return KERN_FAILURE
;
1368 * Find the underlying vnode object for the given vm_map_entry. If found, return with the
1369 * object locked, otherwise return NULL with nothing locked.
1374 vm_map_entry_t entry
1377 vm_object_t top_object
, object
;
1378 memory_object_t memory_object
;
1379 memory_object_pager_ops_t pager_ops
;
1381 if (!entry
->is_sub_map
) {
1383 * The last object in the shadow chain has the
1384 * relevant pager information.
1387 top_object
= VME_OBJECT(entry
);
1390 vm_object_lock(top_object
);
1392 for (object
= top_object
; object
->shadow
!= VM_OBJECT_NULL
; object
= object
->shadow
) {
1393 vm_object_lock(object
->shadow
);
1394 vm_object_unlock(object
);
1397 if (object
&& !object
->internal
&& object
->pager_ready
&& !object
->terminating
&&
1399 memory_object
= object
->pager
;
1400 pager_ops
= memory_object
->mo_pager_ops
;
1403 * If this object points to the vnode_pager_ops, then we found what we're
1404 * looking for. Otherwise, this vm_map_entry doesn't have an underlying
1405 * vnode and so we fall through to the bottom and return NULL.
1408 if (pager_ops
== &vnode_pager_ops
) {
1409 return object
; /* we return with the object locked */
1413 vm_object_unlock(object
);
1417 return VM_OBJECT_NULL
;