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@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * Virtual memory object module definitions.
66 #ifndef _VM_VM_OBJECT_H_
67 #define _VM_VM_OBJECT_H_
70 #include <mach_assert.h>
71 #include <mach_pagemap.h>
73 #include <mach/kern_return.h>
74 #include <mach/boolean.h>
75 #include <mach/memory_object_types.h>
76 #include <mach/port.h>
77 #include <mach/vm_prot.h>
78 #include <mach/vm_param.h>
79 #include <mach/machine/vm_types.h>
80 #include <kern/queue.h>
81 #include <kern/locks.h>
82 #include <kern/assert.h>
83 #include <kern/misc_protos.h>
84 #include <kern/macro_help.h>
85 #include <ipc/ipc_types.h>
88 #include <vm/vm_external.h>
90 #include <vm/vm_options.h>
91 #include <vm/vm_page.h>
93 #if VM_OBJECT_TRACKING
94 #include <libkern/OSDebug.h>
95 #include <kern/btlog.h>
96 extern void vm_object_tracking_init(void);
97 extern boolean_t vm_object_tracking_inited
;
98 extern btlog_t
*vm_object_tracking_btlog
;
99 #define VM_OBJECT_TRACKING_NUM_RECORDS 50000
100 #define VM_OBJECT_TRACKING_BTDEPTH 7
101 #define VM_OBJECT_TRACKING_OP_CREATED 1
102 #define VM_OBJECT_TRACKING_OP_MODIFIED 2
103 #define VM_OBJECT_TRACKING_OP_TRUESHARE 3
104 #endif /* VM_OBJECT_TRACKING */
111 * vm_object_t Virtual memory object.
112 * vm_object_fault_info_t Used to determine cluster size.
115 struct vm_object_fault_info
{
118 vm_size_t cluster_size
;
119 vm_behavior_t behavior
;
120 vm_object_offset_t lo_offset
;
121 vm_object_offset_t hi_offset
;
123 /* boolean_t */ no_cache
:1,
124 /* boolean_t */ stealth
:1,
125 /* boolean_t */ io_sync
:1,
126 /* boolean_t */ cs_bypass
:1,
127 /* boolean_t */ pmap_cs_associated
:1,
128 /* boolean_t */ mark_zf_absent
:1,
129 /* boolean_t */ batch_pmap_op
:1,
130 /* boolean_t */ resilient_media
:1,
131 /* boolean_t */ no_copy_on_read
:1,
132 __vm_object_fault_info_unused_bits
:23;
137 #define vo_size vo_un1.vou_size
138 #define vo_cache_pages_to_scan vo_un1.vou_cache_pages_to_scan
139 #define vo_shadow_offset vo_un2.vou_shadow_offset
140 #define vo_cache_ts vo_un2.vou_cache_ts
141 #define vo_owner vo_un2.vou_owner
145 * on 64 bit systems we pack the pointers hung off the memq.
146 * those pointers have to be able to point back to the memq.
147 * the packed pointers are required to be on a 64 byte boundary
148 * which means 2 things for the vm_object... (1) the memq
149 * struct has to be the first element of the structure so that
150 * we can control it's alignment... (2) the vm_object must be
151 * aligned on a 64 byte boundary... for static vm_object's
152 * this is accomplished via the 'aligned' attribute... for
153 * vm_object's in the zone pool, this is accomplished by
154 * rounding the size of the vm_object element to the nearest
155 * 64 byte size before creating the zone.
157 vm_page_queue_head_t memq
; /* Resident memory - must be first */
158 lck_rw_t Lock
; /* Synchronization */
160 #if DEVELOPMENT || DEBUG
164 vm_object_size_t vou_size
; /* Object size (only valid if internal) */
165 int vou_cache_pages_to_scan
; /* pages yet to be visited in an
166 * external object in cache
170 struct vm_page
*memq_hint
;
171 int ref_count
; /* Number of references */
172 unsigned int resident_page_count
;
173 /* number of resident pages */
174 unsigned int wired_page_count
; /* number of wired pages
175 * use VM_OBJECT_WIRED_PAGE_UPDATE macros to update */
176 unsigned int reusable_page_count
;
178 struct vm_object
*copy
; /* Object that should receive
179 * a copy of my changed pages,
180 * for copy_delay, or just the
181 * temporary object that
182 * shadows this object, for
185 struct vm_object
*shadow
; /* My shadow */
186 memory_object_t pager
; /* Where to get data */
189 vm_object_offset_t vou_shadow_offset
; /* Offset into shadow */
190 clock_sec_t vou_cache_ts
; /* age of an external object
193 task_t vou_owner
; /* If the object is purgeable
194 * or has a "ledger_tag", this
195 * is the task that owns it.
199 vm_object_offset_t paging_offset
; /* Offset into memory object */
200 memory_object_control_t pager_control
; /* Where data comes back */
202 memory_object_copy_strategy_t
203 copy_strategy
; /* How to handle data copy */
207 * Some user processes (mostly VirtualMachine software) take a large
208 * number of UPLs (via IOMemoryDescriptors) to wire pages in large
209 * VM objects and overflow the 16-bit "activity_in_progress" counter.
210 * Since we never enforced any limit there, let's give them 32 bits
211 * for backwards compatibility's sake.
213 unsigned int paging_in_progress
:16,
214 __object1_unused_bits
:16;
215 unsigned int activity_in_progress
;
218 * On 32-bit platforms, enlarging "activity_in_progress" would increase
219 * the size of "struct vm_object". Since we don't know of any actual
220 * overflow of these counters on these platforms, let's keep the
221 * counters as 16-bit integers.
223 unsigned short paging_in_progress
;
224 unsigned short activity_in_progress
;
225 #endif /* __LP64__ */
226 /* The memory object ports are
227 * being used (e.g., for pagein
228 * or pageout) -- don't change
229 * any of these fields (i.e.,
230 * don't collapse, destroy or
235 /* boolean_t array */ all_wanted
:11, /* Bit array of "want to be
236 * awakened" notations. See
237 * VM_OBJECT_EVENT_* items
239 /* boolean_t */ pager_created
:1, /* Has pager been created? */
240 /* boolean_t */ pager_initialized
:1, /* Are fields ready to use? */
241 /* boolean_t */ pager_ready
:1, /* Will pager take requests? */
243 /* boolean_t */ pager_trusted
:1, /* The pager for this object
244 * is trusted. This is true for
245 * all internal objects (backed
246 * by the default pager)
248 /* boolean_t */ can_persist
:1, /* The kernel may keep the data
249 * for this object (and rights
250 * to the memory object) after
251 * all address map references
254 /* boolean_t */ internal
:1, /* Created by the kernel (and
255 * therefore, managed by the
256 * default memory manger)
258 /* boolean_t */ private:1, /* magic device_pager object,
259 * holds private pages only */
260 /* boolean_t */ pageout
:1, /* pageout object. contains
261 * private pages that refer to
262 * a real memory object. */
263 /* boolean_t */ alive
:1, /* Not yet terminated */
265 /* boolean_t */ purgable
:2, /* Purgable state. See
268 /* boolean_t */ purgeable_only_by_kernel
:1,
269 /* boolean_t */ purgeable_when_ripe
:1, /* Purgeable when a token
272 /* boolean_t */ shadowed
:1, /* Shadow may exist */
273 /* boolean_t */ true_share
:1,
274 /* This object is mapped
275 * in more than one place
276 * and hence cannot be
278 /* boolean_t */ terminating
:1,
279 /* Allows vm_object_lookup
280 * and vm_object_deallocate
281 * to special case their
282 * behavior when they are
283 * called as a result of
284 * page cleaning during
287 /* boolean_t */ named
:1, /* An enforces an internal
288 * naming convention, by
289 * calling the right routines
291 * destruction, UBC references
292 * against the vm_object are
295 /* boolean_t */ shadow_severed
:1,
296 /* When a permanent object
297 * backing a COW goes away
298 * unexpectedly. This bit
299 * allows vm_fault to return
300 * an error rather than a
303 /* boolean_t */ phys_contiguous
:1,
304 /* Memory is wired and
305 * guaranteed physically
306 * contiguous. However
307 * it is not device memory
308 * and obeys normal virtual
309 * memory rules w.r.t pmap
312 /* boolean_t */ nophyscache
:1,
313 /* When mapped at the
314 * pmap level, don't allow
315 * primary caching. (for
318 /* boolean_t */ _object5_unused_bits
:1;
320 queue_chain_t cached_list
; /* Attachment point for the
321 * list of objects cached as a
322 * result of their can_persist
326 * the following fields are not protected by any locks
327 * they are updated via atomic compare and swap
329 vm_object_offset_t last_alloc
; /* last allocation offset */
330 vm_offset_t cow_hint
; /* last page present in */
331 /* shadow but not in object */
332 int sequential
; /* sequential access size */
334 uint32_t pages_created
;
336 /* hold object lock when altering */
338 wimg_bits
:8, /* cache WIMG bits */
339 code_signed
:1, /* pages are signed and should be
340 * validated; the signatures are stored
342 transposed
:1, /* object was transposed with another */
343 mapping_in_progress
:1, /* pager being mapped/unmapped */
350 object_is_shared_cache
:1,
351 purgeable_queue_type
:2,
352 purgeable_queue_group
:3,
354 no_tag_update
:1, /* */
355 #if CONFIG_SECLUDED_MEMORY
356 eligible_for_secluded
:1,
358 #else /* CONFIG_SECLUDED_MEMORY */
359 __object3_unused_bits
:2,
360 #endif /* CONFIG_SECLUDED_MEMORY */
361 #if VM_OBJECT_ACCESS_TRACKING
363 #else /* VM_OBJECT_ACCESS_TRACKING */
364 __unused_access_tracking
:1,
365 #endif /* VM_OBJECT_ACCESS_TRACKING */
369 #if VM_OBJECT_ACCESS_TRACKING
370 uint32_t access_tracking_reads
;
371 uint32_t access_tracking_writes
;
372 #endif /* VM_OBJECT_ACCESS_TRACKING */
374 uint8_t scan_collisions
;
375 uint8_t __object4_unused_bits
[1];
378 #if CONFIG_PHANTOM_CACHE
379 uint32_t phantom_object_id
;
381 #if CONFIG_IOSCHED || UPL_DEBUG
382 queue_head_t uplq
; /* List of outstanding upls */
387 * Keep track of the stack traces for the first holders
388 * of a "paging_in_progress" reference for this VM object.
390 #define VM_PIP_DEBUG_STACK_FRAMES 25 /* depth of each stack trace */
391 #define VM_PIP_DEBUG_MAX_REFS 10 /* track that many references */
392 struct __pip_backtrace
{
393 void *pip_retaddr
[VM_PIP_DEBUG_STACK_FRAMES
];
394 } pip_holders
[VM_PIP_DEBUG_MAX_REFS
];
395 #endif /* VM_PIP_DEBUG */
397 queue_chain_t objq
; /* object queue - currently used for purgable queues */
398 queue_chain_t task_objq
; /* objects owned by task - protected by task lock */
400 #if !VM_TAG_ACTIVE_UPDATE
401 queue_chain_t wired_objq
;
402 #endif /* !VM_TAG_ACTIVE_UPDATE */
405 void *purgeable_owner_bt
[16];
406 task_t vo_purgeable_volatilizer
; /* who made it volatile? */
407 void *purgeable_volatilizer_bt
[16];
411 #define VM_OBJECT_PURGEABLE_FAULT_ERROR(object) \
412 ((object)->volatile_fault && \
413 ((object)->purgable == VM_PURGABLE_VOLATILE || \
414 (object)->purgable == VM_PURGABLE_EMPTY))
416 #if VM_OBJECT_ACCESS_TRACKING
417 extern uint64_t vm_object_access_tracking_reads
;
418 extern uint64_t vm_object_access_tracking_writes
;
419 extern void vm_object_access_tracking(vm_object_t object
,
420 int *access_tracking
,
421 uint32_t *access_tracking_reads
,
422 uint32_t *acess_tracking_writes
);
423 #endif /* VM_OBJECT_ACCESS_TRACKING */
426 vm_object_t kernel_object
; /* the single kernel object */
429 vm_object_t compressor_object
; /* the single compressor object */
432 vm_object_t retired_pages_object
; /* holds VM pages which should never be used */
435 unsigned int vm_object_absent_max
; /* maximum number of absent pages
436 * at a time for each object */
438 # define VM_MSYNC_INITIALIZED 0
439 # define VM_MSYNC_SYNCHRONIZING 1
440 # define VM_MSYNC_DONE 2
443 extern lck_grp_t vm_map_lck_grp
;
444 extern lck_attr_t vm_map_lck_attr
;
446 #ifndef VM_TAG_ACTIVE_UPDATE
447 #error VM_TAG_ACTIVE_UPDATE
450 #if VM_TAG_ACTIVE_UPDATE
451 #define VM_OBJECT_WIRED_ENQUEUE(object) panic("VM_OBJECT_WIRED_ENQUEUE")
452 #define VM_OBJECT_WIRED_DEQUEUE(object) panic("VM_OBJECT_WIRED_DEQUEUE")
453 #else /* VM_TAG_ACTIVE_UPDATE */
454 #define VM_OBJECT_WIRED_ENQUEUE(object) \
456 lck_spin_lock_grp(&vm_objects_wired_lock, &vm_page_lck_grp_bucket); \
457 assert(!(object)->wired_objq.next); \
458 assert(!(object)->wired_objq.prev); \
459 queue_enter(&vm_objects_wired, (object), \
460 vm_object_t, wired_objq); \
461 lck_spin_unlock(&vm_objects_wired_lock); \
463 #define VM_OBJECT_WIRED_DEQUEUE(object) \
465 if ((object)->wired_objq.next) { \
466 lck_spin_lock_grp(&vm_objects_wired_lock, &vm_page_lck_grp_bucket); \
467 queue_remove(&vm_objects_wired, (object), \
468 vm_object_t, wired_objq); \
469 lck_spin_unlock(&vm_objects_wired_lock); \
472 #endif /* VM_TAG_ACTIVE_UPDATE */
474 #define VM_OBJECT_WIRED(object, tag) \
476 assert(VM_KERN_MEMORY_NONE != (tag)); \
477 assert(VM_KERN_MEMORY_NONE == (object)->wire_tag); \
478 (object)->wire_tag = (tag); \
479 if (!VM_TAG_ACTIVE_UPDATE) { \
480 VM_OBJECT_WIRED_ENQUEUE((object)); \
484 #define VM_OBJECT_UNWIRED(object) \
486 if (!VM_TAG_ACTIVE_UPDATE) { \
487 VM_OBJECT_WIRED_DEQUEUE((object)); \
489 if (VM_KERN_MEMORY_NONE != (object)->wire_tag) { \
490 vm_tag_update_size((object)->wire_tag, -ptoa_64((object)->wired_page_count)); \
491 (object)->wire_tag = VM_KERN_MEMORY_NONE; \
495 // These two macros start & end a C block
496 #define VM_OBJECT_WIRED_PAGE_UPDATE_START(object) \
499 int64_t __wireddelta = 0; vm_tag_t __waswired = (object)->wire_tag;
501 #define VM_OBJECT_WIRED_PAGE_UPDATE_END(object, tag) \
502 if (__wireddelta) { \
503 boolean_t __overflow __assert_only = \
504 os_add_overflow((object)->wired_page_count, __wireddelta, \
505 &(object)->wired_page_count); \
506 assert(!__overflow); \
507 if (!(object)->pageout && !(object)->no_tag_update) { \
508 if (__wireddelta > 0) { \
509 assert (VM_KERN_MEMORY_NONE != (tag)); \
510 if (VM_KERN_MEMORY_NONE == __waswired) { \
511 VM_OBJECT_WIRED((object), (tag)); \
513 vm_tag_update_size((object)->wire_tag, ptoa_64(__wireddelta)); \
514 } else if (VM_KERN_MEMORY_NONE != __waswired) { \
515 assert (VM_KERN_MEMORY_NONE != (object)->wire_tag); \
516 vm_tag_update_size((object)->wire_tag, ptoa_64(__wireddelta)); \
517 if (!(object)->wired_page_count) { \
518 VM_OBJECT_UNWIRED((object)); \
526 #define VM_OBJECT_WIRED_PAGE_COUNT(object, delta) \
527 __wireddelta += delta; \
529 #define VM_OBJECT_WIRED_PAGE_ADD(object, m) \
530 if (!(m)->vmp_private && !(m)->vmp_fictitious) __wireddelta++;
532 #define VM_OBJECT_WIRED_PAGE_REMOVE(object, m) \
533 if (!(m)->vmp_private && !(m)->vmp_fictitious) __wireddelta--;
537 #define OBJECT_LOCK_SHARED 0
538 #define OBJECT_LOCK_EXCLUSIVE 1
540 extern lck_grp_t vm_object_lck_grp
;
541 extern lck_attr_t vm_object_lck_attr
;
542 extern lck_attr_t kernel_object_lck_attr
;
543 extern lck_attr_t compressor_object_lck_attr
;
545 extern vm_object_t vm_pageout_scan_wants_object
;
547 extern void vm_object_lock(vm_object_t
);
548 extern bool vm_object_lock_check_contended(vm_object_t
);
549 extern boolean_t
vm_object_lock_try(vm_object_t
);
550 extern boolean_t
_vm_object_lock_try(vm_object_t
);
551 extern boolean_t
vm_object_lock_avoid(vm_object_t
);
552 extern void vm_object_lock_shared(vm_object_t
);
553 extern boolean_t
vm_object_lock_yield_shared(vm_object_t
);
554 extern boolean_t
vm_object_lock_try_shared(vm_object_t
);
555 extern void vm_object_unlock(vm_object_t
);
556 extern boolean_t
vm_object_lock_upgrade(vm_object_t
);
559 * Object locking macros
562 #define vm_object_lock_init(object) \
563 lck_rw_init(&(object)->Lock, &vm_object_lck_grp, \
564 (((object) == kernel_object || \
565 (object) == vm_submap_object) ? \
566 &kernel_object_lck_attr : \
567 (((object) == compressor_object) ? \
568 &compressor_object_lck_attr : \
569 &vm_object_lck_attr)))
570 #define vm_object_lock_destroy(object) lck_rw_destroy(&(object)->Lock, &vm_object_lck_grp)
572 #define vm_object_lock_try_scan(object) _vm_object_lock_try(object)
575 * CAUTION: the following vm_object_lock_assert_held*() macros merely
576 * check if anyone is holding the lock, but the holder may not necessarily
579 #if MACH_ASSERT || DEBUG
580 #define vm_object_lock_assert_held(object) \
581 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_HELD)
582 #define vm_object_lock_assert_shared(object) \
583 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_SHARED)
584 #define vm_object_lock_assert_exclusive(object) \
585 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_EXCLUSIVE)
586 #define vm_object_lock_assert_notheld(object) \
587 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_NOTHELD)
588 #else /* MACH_ASSERT || DEBUG */
589 #define vm_object_lock_assert_held(object)
590 #define vm_object_lock_assert_shared(object)
591 #define vm_object_lock_assert_exclusive(object)
592 #define vm_object_lock_assert_notheld(object)
593 #endif /* MACH_ASSERT || DEBUG */
597 * Declare procedures that operate on VM objects.
600 __private_extern__
void vm_object_bootstrap(void);
602 __private_extern__
void vm_object_reaper_init(void);
604 __private_extern__ vm_object_t
vm_object_allocate(vm_object_size_t size
);
606 __private_extern__
void _vm_object_allocate(vm_object_size_t size
,
609 #define vm_object_reference_locked(object) \
611 vm_object_t RLObject = (object); \
612 vm_object_lock_assert_exclusive(object); \
613 assert((RLObject)->ref_count > 0); \
614 (RLObject)->ref_count++; \
615 assert((RLObject)->ref_count > 1); \
619 #define vm_object_reference_shared(object) \
621 vm_object_t RLObject = (object); \
622 vm_object_lock_assert_shared(object); \
623 assert((RLObject)->ref_count > 0); \
624 OSAddAtomic(1, &(RLObject)->ref_count); \
625 assert((RLObject)->ref_count > 0); \
629 __private_extern__
void vm_object_reference(
634 #define vm_object_reference(object) \
636 vm_object_t RObject = (object); \
638 vm_object_lock_shared(RObject); \
639 vm_object_reference_shared(RObject); \
640 vm_object_unlock(RObject); \
644 #endif /* MACH_ASSERT */
646 __private_extern__
void vm_object_deallocate(
649 __private_extern__ kern_return_t
vm_object_release_name(
653 __private_extern__
void vm_object_pmap_protect(
655 vm_object_offset_t offset
,
656 vm_object_size_t size
,
658 vm_map_size_t pmap_page_size
,
659 vm_map_offset_t pmap_start
,
662 __private_extern__
void vm_object_pmap_protect_options(
664 vm_object_offset_t offset
,
665 vm_object_size_t size
,
667 vm_map_size_t pmap_page_size
,
668 vm_map_offset_t pmap_start
,
672 __private_extern__
void vm_object_page_remove(
674 vm_object_offset_t start
,
675 vm_object_offset_t end
);
677 __private_extern__
void vm_object_deactivate_pages(
679 vm_object_offset_t offset
,
680 vm_object_size_t size
,
682 boolean_t reusable_page
,
684 /* XXX TODO4K: need pmap_page_size here too? */
685 vm_map_offset_t pmap_offset
);
687 __private_extern__
void vm_object_reuse_pages(
689 vm_object_offset_t start_offset
,
690 vm_object_offset_t end_offset
,
691 boolean_t allow_partial_reuse
);
693 __private_extern__
uint64_t vm_object_purge(
697 __private_extern__ kern_return_t
vm_object_purgable_control(
699 vm_purgable_t control
,
702 __private_extern__ kern_return_t
vm_object_get_page_counts(
704 vm_object_offset_t offset
,
705 vm_object_size_t size
,
706 unsigned int *resident_page_count
,
707 unsigned int *dirty_page_count
);
709 __private_extern__ boolean_t
vm_object_coalesce(
710 vm_object_t prev_object
,
711 vm_object_t next_object
,
712 vm_object_offset_t prev_offset
,
713 vm_object_offset_t next_offset
,
714 vm_object_size_t prev_size
,
715 vm_object_size_t next_size
);
717 __private_extern__ boolean_t
vm_object_shadow(
719 vm_object_offset_t
*offset
,
720 vm_object_size_t length
);
722 __private_extern__
void vm_object_collapse(
724 vm_object_offset_t offset
,
725 boolean_t can_bypass
);
727 __private_extern__ boolean_t
vm_object_copy_quickly(
728 vm_object_t
*_object
,
729 vm_object_offset_t src_offset
,
730 vm_object_size_t size
,
731 boolean_t
*_src_needs_copy
,
732 boolean_t
*_dst_needs_copy
);
734 __private_extern__ kern_return_t
vm_object_copy_strategically(
735 vm_object_t src_object
,
736 vm_object_offset_t src_offset
,
737 vm_object_size_t size
,
738 vm_object_t
*dst_object
,
739 vm_object_offset_t
*dst_offset
,
740 boolean_t
*dst_needs_copy
);
742 __private_extern__ kern_return_t
vm_object_copy_slowly(
743 vm_object_t src_object
,
744 vm_object_offset_t src_offset
,
745 vm_object_size_t size
,
746 boolean_t interruptible
,
747 vm_object_t
*_result_object
);
749 __private_extern__ vm_object_t
vm_object_copy_delayed(
750 vm_object_t src_object
,
751 vm_object_offset_t src_offset
,
752 vm_object_size_t size
,
753 boolean_t src_object_shared
);
757 __private_extern__ kern_return_t
vm_object_destroy(
759 kern_return_t reason
);
761 __private_extern__
void vm_object_pager_create(
764 __private_extern__
void vm_object_compressor_pager_create(
767 __private_extern__
void vm_object_page_map(
769 vm_object_offset_t offset
,
770 vm_object_size_t size
,
771 vm_object_offset_t (*map_fn
)
772 (void *, vm_object_offset_t
),
775 __private_extern__ kern_return_t
vm_object_upl_request(
777 vm_object_offset_t offset
,
780 upl_page_info_t
*page_info
,
782 upl_control_flags_t flags
,
785 __private_extern__ kern_return_t
vm_object_transpose(
788 vm_object_size_t transpose_size
);
790 __private_extern__ boolean_t
vm_object_sync(
792 vm_object_offset_t offset
,
793 vm_object_size_t size
,
794 boolean_t should_flush
,
795 boolean_t should_return
,
796 boolean_t should_iosync
);
798 __private_extern__ kern_return_t
vm_object_update(
800 vm_object_offset_t offset
,
801 vm_object_size_t size
,
802 vm_object_offset_t
*error_offset
,
804 memory_object_return_t should_return
,
808 __private_extern__ kern_return_t
vm_object_lock_request(
810 vm_object_offset_t offset
,
811 vm_object_size_t size
,
812 memory_object_return_t should_return
,
818 __private_extern__ vm_object_t
vm_object_memory_object_associate(
819 memory_object_t pager
,
821 vm_object_size_t size
,
822 boolean_t check_named
);
825 __private_extern__
void vm_object_cluster_size(
827 vm_object_offset_t
*start
,
829 vm_object_fault_info_t fault_info
,
830 uint32_t *io_streaming
);
832 __private_extern__ kern_return_t
vm_object_populate_with_private(
834 vm_object_offset_t offset
,
838 __private_extern__
void vm_object_change_wimg_mode(
840 unsigned int wimg_mode
);
842 extern kern_return_t
adjust_vm_object_cache(
846 extern kern_return_t
vm_object_page_op(
848 vm_object_offset_t offset
,
853 extern kern_return_t
vm_object_range_op(
855 vm_object_offset_t offset_beg
,
856 vm_object_offset_t offset_end
,
861 __private_extern__
void vm_object_reap_pages(
865 #define REAP_TERMINATE 1
866 #define REAP_PURGEABLE 2
867 #define REAP_DATA_FLUSH 3
871 __private_extern__
uint32_t
872 vm_object_compressed_freezer_pageout(
873 vm_object_t object
, uint32_t dirty_budget
);
875 __private_extern__
void
876 vm_object_compressed_freezer_done(
879 #endif /* CONFIG_FREEZE */
881 __private_extern__
void
886 struct io_reprioritize_req
{
891 queue_chain_t io_reprioritize_list
;
893 typedef struct io_reprioritize_req
*io_reprioritize_req_t
;
895 extern void vm_io_reprioritize_init(void);
899 * Event waiting handling
902 #define VM_OBJECT_EVENT_INITIALIZED 0
903 #define VM_OBJECT_EVENT_PAGER_READY 1
904 #define VM_OBJECT_EVENT_PAGING_IN_PROGRESS 2
905 #define VM_OBJECT_EVENT_MAPPING_IN_PROGRESS 3
906 #define VM_OBJECT_EVENT_LOCK_IN_PROGRESS 4
907 #define VM_OBJECT_EVENT_UNCACHING 5
908 #define VM_OBJECT_EVENT_COPY_CALL 6
909 #define VM_OBJECT_EVENT_CACHING 7
910 #define VM_OBJECT_EVENT_UNBLOCKED 8
911 #define VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS 9
913 #define VM_OBJECT_EVENT_MAX 10 /* 11 bits in "all_wanted", so 0->10 */
915 static __inline__ wait_result_t
916 vm_object_assert_wait(
919 wait_interrupt_t interruptible
)
923 vm_object_lock_assert_exclusive(object
);
924 assert(event
>= 0 && event
<= VM_OBJECT_EVENT_MAX
);
926 object
->all_wanted
|= 1 << event
;
927 wr
= assert_wait((event_t
)((vm_offset_t
)object
+ (vm_offset_t
)event
),
932 static __inline__ wait_result_t
936 wait_interrupt_t interruptible
)
940 vm_object_assert_wait(object
, event
, interruptible
);
941 vm_object_unlock(object
);
942 wr
= thread_block(THREAD_CONTINUE_NULL
);
946 static __inline__ wait_result_t
947 thread_sleep_vm_object(
950 wait_interrupt_t interruptible
)
954 #if DEVELOPMENT || DEBUG
955 if (object
->Lock_owner
!= current_thread()) {
956 panic("thread_sleep_vm_object: now owner - %p\n", object
);
958 object
->Lock_owner
= 0;
960 wr
= lck_rw_sleep(&object
->Lock
,
961 LCK_SLEEP_PROMOTED_PRI
,
964 #if DEVELOPMENT || DEBUG
965 object
->Lock_owner
= current_thread();
970 static __inline__ wait_result_t
974 wait_interrupt_t interruptible
)
978 vm_object_lock_assert_exclusive(object
);
979 assert(event
>= 0 && event
<= VM_OBJECT_EVENT_MAX
);
981 object
->all_wanted
|= 1 << event
;
982 wr
= thread_sleep_vm_object(object
,
983 (event_t
)((vm_offset_t
)object
+ (vm_offset_t
)event
),
988 static __inline__
void
993 vm_object_lock_assert_exclusive(object
);
994 assert(event
>= 0 && event
<= VM_OBJECT_EVENT_MAX
);
996 if (object
->all_wanted
& (1 << event
)) {
997 thread_wakeup((event_t
)((vm_offset_t
)object
+ (vm_offset_t
)event
));
999 object
->all_wanted
&= ~(1 << event
);
1002 static __inline__
void
1003 vm_object_set_wanted(
1007 vm_object_lock_assert_exclusive(object
);
1008 assert(event
>= 0 && event
<= VM_OBJECT_EVENT_MAX
);
1010 object
->all_wanted
|= (1 << event
);
1013 static __inline__
int
1018 vm_object_lock_assert_held(object
);
1019 assert(event
>= 0 && event
<= VM_OBJECT_EVENT_MAX
);
1021 return object
->all_wanted
& (1 << event
);
1025 * Routines implemented as macros
1028 #include <libkern/OSDebug.h>
1029 #define VM_PIP_DEBUG_BEGIN(object) \
1031 int pip = ((object)->paging_in_progress + \
1032 (object)->activity_in_progress); \
1033 if (pip < VM_PIP_DEBUG_MAX_REFS) { \
1034 (void) OSBacktrace(&(object)->pip_holders[pip].pip_retaddr[0], \
1035 VM_PIP_DEBUG_STACK_FRAMES); \
1038 #else /* VM_PIP_DEBUG */
1039 #define VM_PIP_DEBUG_BEGIN(object)
1040 #endif /* VM_PIP_DEBUG */
1042 #define vm_object_activity_begin(object) \
1044 vm_object_lock_assert_exclusive((object)); \
1045 VM_PIP_DEBUG_BEGIN((object)); \
1046 (object)->activity_in_progress++; \
1047 if ((object)->activity_in_progress == 0) { \
1048 panic("vm_object_activity_begin(%p): overflow\n", (object));\
1052 #define vm_object_activity_end(object) \
1054 vm_object_lock_assert_exclusive((object)); \
1055 if ((object)->activity_in_progress == 0) { \
1056 panic("vm_object_activity_end(%p): underflow\n", (object));\
1058 (object)->activity_in_progress--; \
1059 if ((object)->paging_in_progress == 0 && \
1060 (object)->activity_in_progress == 0) \
1061 vm_object_wakeup((object), \
1062 VM_OBJECT_EVENT_PAGING_IN_PROGRESS); \
1065 #define vm_object_paging_begin(object) \
1067 vm_object_lock_assert_exclusive((object)); \
1068 VM_PIP_DEBUG_BEGIN((object)); \
1069 (object)->paging_in_progress++; \
1070 if ((object)->paging_in_progress == 0) { \
1071 panic("vm_object_paging_begin(%p): overflow\n", (object));\
1075 #define vm_object_paging_end(object) \
1077 vm_object_lock_assert_exclusive((object)); \
1078 if ((object)->paging_in_progress == 0) { \
1079 panic("vm_object_paging_end(%p): underflow\n", (object));\
1081 (object)->paging_in_progress--; \
1082 if ((object)->paging_in_progress == 0) { \
1083 vm_object_wakeup((object), \
1084 VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS); \
1085 if ((object)->activity_in_progress == 0) \
1086 vm_object_wakeup((object), \
1087 VM_OBJECT_EVENT_PAGING_IN_PROGRESS); \
1091 #define vm_object_paging_wait(object, interruptible) \
1093 vm_object_lock_assert_exclusive((object)); \
1094 while ((object)->paging_in_progress != 0 || \
1095 (object)->activity_in_progress != 0) { \
1096 wait_result_t _wr; \
1098 _wr = vm_object_sleep((object), \
1099 VM_OBJECT_EVENT_PAGING_IN_PROGRESS, \
1102 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/ \
1107 #define vm_object_paging_only_wait(object, interruptible) \
1109 vm_object_lock_assert_exclusive((object)); \
1110 while ((object)->paging_in_progress != 0) { \
1111 wait_result_t _wr; \
1113 _wr = vm_object_sleep((object), \
1114 VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS,\
1117 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/ \
1123 #define vm_object_mapping_begin(object) \
1125 vm_object_lock_assert_exclusive((object)); \
1126 assert(! (object)->mapping_in_progress); \
1127 (object)->mapping_in_progress = TRUE; \
1130 #define vm_object_mapping_end(object) \
1132 vm_object_lock_assert_exclusive((object)); \
1133 assert((object)->mapping_in_progress); \
1134 (object)->mapping_in_progress = FALSE; \
1135 vm_object_wakeup((object), \
1136 VM_OBJECT_EVENT_MAPPING_IN_PROGRESS); \
1139 #define vm_object_mapping_wait(object, interruptible) \
1141 vm_object_lock_assert_exclusive((object)); \
1142 while ((object)->mapping_in_progress) { \
1143 wait_result_t _wr; \
1145 _wr = vm_object_sleep((object), \
1146 VM_OBJECT_EVENT_MAPPING_IN_PROGRESS, \
1148 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/ \
1151 assert(!(object)->mapping_in_progress); \
1156 #define vm_object_round_page(x) (((vm_object_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
1157 #define vm_object_trunc_page(x) ((vm_object_offset_t)(x) & ~((signed)PAGE_MASK))
1159 extern void vm_object_cache_add(vm_object_t
);
1160 extern void vm_object_cache_remove(vm_object_t
);
1161 extern int vm_object_cache_evict(int, int);
1163 #define VM_OBJECT_OWNER_DISOWNED ((task_t) -1)
1164 #define VM_OBJECT_OWNER(object) \
1165 ((((object)->purgable == VM_PURGABLE_DENY && \
1166 (object)->vo_ledger_tag == 0) || \
1167 (object)->vo_owner == TASK_NULL) \
1168 ? TASK_NULL /* not owned */ \
1169 : (((object)->vo_owner == VM_OBJECT_OWNER_DISOWNED) \
1170 ? kernel_task /* disowned -> kernel */ \
1171 : (object)->vo_owner)) /* explicit owner */ \
1173 extern void vm_object_ledger_tag_ledgers(
1175 int *ledger_idx_volatile
,
1176 int *ledger_idx_nonvolatile
,
1177 int *ledger_idx_volatile_compressed
,
1178 int *ledger_idx_nonvolatile_compressed
,
1179 boolean_t
*do_footprint
);
1180 extern kern_return_t
vm_object_ownership_change(
1184 int new_ledger_flags
,
1185 boolean_t task_objq_locked
);
1187 // LP64todo: all the current tools are 32bit, obviously never worked for 64b
1188 // so probably should be a real 32b ID vs. ptr.
1189 // Current users just check for equality
1190 #define VM_OBJECT_ID(o) ((uint32_t)(uintptr_t)VM_KERNEL_ADDRPERM((o)))
1192 #endif /* _VM_VM_OBJECT_H_ */