2 * Copyright (c) 2000-2007 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.
59 * File: vm/vm_object.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * Virtual memory object module.
66 #include <mach_pagemap.h>
67 #include <task_swapper.h>
69 #include <mach/mach_types.h>
70 #include <mach/memory_object.h>
71 #include <mach/memory_object_default.h>
72 #include <mach/memory_object_control_server.h>
73 #include <mach/vm_param.h>
75 #include <ipc/ipc_types.h>
76 #include <ipc/ipc_port.h>
78 #include <kern/kern_types.h>
79 #include <kern/assert.h>
80 #include <kern/lock.h>
81 #include <kern/queue.h>
83 #include <kern/zalloc.h>
84 #include <kern/host.h>
85 #include <kern/host_statistics.h>
86 #include <kern/processor.h>
87 #include <kern/misc_protos.h>
89 #include <vm/memory_object.h>
90 #include <vm/vm_fault.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_protos.h>
96 #include <vm/vm_purgeable_internal.h>
99 #include <sys/kern_memorystatus.h>
103 * Virtual memory objects maintain the actual data
104 * associated with allocated virtual memory. A given
105 * page of memory exists within exactly one object.
107 * An object is only deallocated when all "references"
110 * Associated with each object is a list of all resident
111 * memory pages belonging to that object; this list is
112 * maintained by the "vm_page" module, but locked by the object's
115 * Each object also records the memory object reference
116 * that is used by the kernel to request and write
117 * back data (the memory object, field "pager"), etc...
119 * Virtual memory objects are allocated to provide
120 * zero-filled memory (vm_allocate) or map a user-defined
121 * memory object into a virtual address space (vm_map).
123 * Virtual memory objects that refer to a user-defined
124 * memory object are called "permanent", because all changes
125 * made in virtual memory are reflected back to the
126 * memory manager, which may then store it permanently.
127 * Other virtual memory objects are called "temporary",
128 * meaning that changes need be written back only when
129 * necessary to reclaim pages, and that storage associated
130 * with the object can be discarded once it is no longer
133 * A permanent memory object may be mapped into more
134 * than one virtual address space. Moreover, two threads
135 * may attempt to make the first mapping of a memory
136 * object concurrently. Only one thread is allowed to
137 * complete this mapping; all others wait for the
138 * "pager_initialized" field is asserted, indicating
139 * that the first thread has initialized all of the
140 * necessary fields in the virtual memory object structure.
142 * The kernel relies on a *default memory manager* to
143 * provide backing storage for the zero-filled virtual
144 * memory objects. The pager memory objects associated
145 * with these temporary virtual memory objects are only
146 * requested from the default memory manager when it
147 * becomes necessary. Virtual memory objects
148 * that depend on the default memory manager are called
149 * "internal". The "pager_created" field is provided to
150 * indicate whether these ports have ever been allocated.
152 * The kernel may also create virtual memory objects to
153 * hold changed pages after a copy-on-write operation.
154 * In this case, the virtual memory object (and its
155 * backing storage -- its memory object) only contain
156 * those pages that have been changed. The "shadow"
157 * field refers to the virtual memory object that contains
158 * the remainder of the contents. The "shadow_offset"
159 * field indicates where in the "shadow" these contents begin.
160 * The "copy" field refers to a virtual memory object
161 * to which changed pages must be copied before changing
162 * this object, in order to implement another form
163 * of copy-on-write optimization.
165 * The virtual memory object structure also records
166 * the attributes associated with its memory object.
167 * The "pager_ready", "can_persist" and "copy_strategy"
168 * fields represent those attributes. The "cached_list"
169 * field is used in the implementation of the persistence
172 * ZZZ Continue this comment.
175 /* Forward declarations for internal functions. */
176 static kern_return_t
vm_object_terminate(
179 extern void vm_object_remove(
182 static kern_return_t
vm_object_copy_call(
183 vm_object_t src_object
,
184 vm_object_offset_t src_offset
,
185 vm_object_size_t size
,
186 vm_object_t
*_result_object
);
188 static void vm_object_do_collapse(
190 vm_object_t backing_object
);
192 static void vm_object_do_bypass(
194 vm_object_t backing_object
);
196 static void vm_object_release_pager(
197 memory_object_t pager
,
200 static zone_t vm_object_zone
; /* vm backing store zone */
203 * All wired-down kernel memory belongs to a single virtual
204 * memory object (kernel_object) to avoid wasting data structures.
206 static struct vm_object kernel_object_store
;
207 vm_object_t kernel_object
;
211 * The submap object is used as a placeholder for vm_map_submap
212 * operations. The object is declared in vm_map.c because it
213 * is exported by the vm_map module. The storage is declared
214 * here because it must be initialized here.
216 static struct vm_object vm_submap_object_store
;
219 * Virtual memory objects are initialized from
220 * a template (see vm_object_allocate).
222 * When adding a new field to the virtual memory
223 * object structure, be sure to add initialization
224 * (see _vm_object_allocate()).
226 static struct vm_object vm_object_template
;
228 unsigned int vm_page_purged_wired
= 0;
229 unsigned int vm_page_purged_busy
= 0;
230 unsigned int vm_page_purged_others
= 0;
234 * Virtual memory objects that are not referenced by
235 * any address maps, but that are allowed to persist
236 * (an attribute specified by the associated memory manager),
237 * are kept in a queue (vm_object_cached_list).
239 * When an object from this queue is referenced again,
240 * for example to make another address space mapping,
241 * it must be removed from the queue. That is, the
242 * queue contains *only* objects with zero references.
244 * The kernel may choose to terminate objects from this
245 * queue in order to reclaim storage. The current policy
246 * is to permit a fixed maximum number of unreferenced
247 * objects (vm_object_cached_max).
249 * A spin lock (accessed by routines
250 * vm_object_cache_{lock,lock_try,unlock}) governs the
251 * object cache. It must be held when objects are
252 * added to or removed from the cache (in vm_object_terminate).
253 * The routines that acquire a reference to a virtual
254 * memory object based on one of the memory object ports
255 * must also lock the cache.
257 * Ideally, the object cache should be more isolated
258 * from the reference mechanism, so that the lock need
259 * not be held to make simple references.
261 static vm_object_t
vm_object_cache_trim(
262 boolean_t called_from_vm_object_deallocate
);
264 static queue_head_t vm_object_cached_list
;
265 static int vm_object_cached_count
=0;
266 static int vm_object_cached_high
; /* highest # cached objects */
267 static int vm_object_cached_max
= 512; /* may be patched*/
269 static lck_mtx_t vm_object_cached_lock_data
;
270 static lck_mtx_ext_t vm_object_cached_lock_data_ext
;
272 #define vm_object_cache_lock() \
273 lck_mtx_lock(&vm_object_cached_lock_data)
274 #define vm_object_cache_lock_try() \
275 lck_mtx_try_lock(&vm_object_cached_lock_data)
276 #define vm_object_cache_lock_spin() \
277 lck_mtx_lock_spin(&vm_object_cached_lock_data)
278 #define vm_object_cache_unlock() \
279 lck_mtx_unlock(&vm_object_cached_lock_data)
281 #endif /* VM_OBJECT_CACHE */
284 static void vm_object_deactivate_all_pages(
288 #define VM_OBJECT_HASH_COUNT 1024
289 #define VM_OBJECT_HASH_LOCK_COUNT 512
291 static lck_mtx_t vm_object_hashed_lock_data
[VM_OBJECT_HASH_LOCK_COUNT
];
292 static lck_mtx_ext_t vm_object_hashed_lock_data_ext
[VM_OBJECT_HASH_LOCK_COUNT
];
294 static queue_head_t vm_object_hashtable
[VM_OBJECT_HASH_COUNT
];
295 static struct zone
*vm_object_hash_zone
;
297 struct vm_object_hash_entry
{
298 queue_chain_t hash_link
; /* hash chain link */
299 memory_object_t pager
; /* pager we represent */
300 vm_object_t object
; /* corresponding object */
301 boolean_t waiting
; /* someone waiting for
305 typedef struct vm_object_hash_entry
*vm_object_hash_entry_t
;
306 #define VM_OBJECT_HASH_ENTRY_NULL ((vm_object_hash_entry_t) 0)
308 #define VM_OBJECT_HASH_SHIFT 5
309 #define vm_object_hash(pager) \
310 ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_COUNT))
312 #define vm_object_lock_hash(pager) \
313 ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_LOCK_COUNT))
315 void vm_object_hash_entry_free(
316 vm_object_hash_entry_t entry
);
318 static void vm_object_reap(vm_object_t object
);
319 static void vm_object_reap_async(vm_object_t object
);
320 static void vm_object_reaper_thread(void);
322 static lck_mtx_t vm_object_reaper_lock_data
;
323 static lck_mtx_ext_t vm_object_reaper_lock_data_ext
;
325 static queue_head_t vm_object_reaper_queue
; /* protected by vm_object_reaper_lock() */
326 unsigned int vm_object_reap_count
= 0;
327 unsigned int vm_object_reap_count_async
= 0;
329 #define vm_object_reaper_lock() \
330 lck_mtx_lock(&vm_object_reaper_lock_data)
331 #define vm_object_reaper_lock_spin() \
332 lck_mtx_lock_spin(&vm_object_reaper_lock_data)
333 #define vm_object_reaper_unlock() \
334 lck_mtx_unlock(&vm_object_reaper_lock_data)
339 vm_object_hash_lock_spin(
340 memory_object_t pager
)
344 index
= vm_object_lock_hash(pager
);
346 lck_mtx_lock_spin(&vm_object_hashed_lock_data
[index
]);
348 return (&vm_object_hashed_lock_data
[index
]);
352 vm_object_hash_unlock(lck_mtx_t
*lck
)
359 * vm_object_hash_lookup looks up a pager in the hashtable
360 * and returns the corresponding entry, with optional removal.
362 static vm_object_hash_entry_t
363 vm_object_hash_lookup(
364 memory_object_t pager
,
365 boolean_t remove_entry
)
368 vm_object_hash_entry_t entry
;
370 bucket
= &vm_object_hashtable
[vm_object_hash(pager
)];
372 entry
= (vm_object_hash_entry_t
)queue_first(bucket
);
373 while (!queue_end(bucket
, (queue_entry_t
)entry
)) {
374 if (entry
->pager
== pager
) {
376 queue_remove(bucket
, entry
,
377 vm_object_hash_entry_t
, hash_link
);
381 entry
= (vm_object_hash_entry_t
)queue_next(&entry
->hash_link
);
383 return(VM_OBJECT_HASH_ENTRY_NULL
);
387 * vm_object_hash_enter enters the specified
388 * pager / cache object association in the hashtable.
392 vm_object_hash_insert(
393 vm_object_hash_entry_t entry
,
398 bucket
= &vm_object_hashtable
[vm_object_hash(entry
->pager
)];
400 queue_enter(bucket
, entry
, vm_object_hash_entry_t
, hash_link
);
402 entry
->object
= object
;
403 object
->hashed
= TRUE
;
406 static vm_object_hash_entry_t
407 vm_object_hash_entry_alloc(
408 memory_object_t pager
)
410 vm_object_hash_entry_t entry
;
412 entry
= (vm_object_hash_entry_t
)zalloc(vm_object_hash_zone
);
413 entry
->pager
= pager
;
414 entry
->object
= VM_OBJECT_NULL
;
415 entry
->waiting
= FALSE
;
421 vm_object_hash_entry_free(
422 vm_object_hash_entry_t entry
)
424 zfree(vm_object_hash_zone
, entry
);
428 * vm_object_allocate:
430 * Returns a new object with the given size.
433 __private_extern__
void
435 vm_object_size_t size
,
439 "vm_object_allocate, object 0x%X size 0x%X\n",
440 object
, size
, 0,0,0);
442 *object
= vm_object_template
;
443 queue_init(&object
->memq
);
444 queue_init(&object
->msr_q
);
446 queue_init(&object
->uplq
);
447 #endif /* UPL_DEBUG */
448 vm_object_lock_init(object
);
452 __private_extern__ vm_object_t
454 vm_object_size_t size
)
456 register vm_object_t object
;
458 object
= (vm_object_t
) zalloc(vm_object_zone
);
460 // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
462 if (object
!= VM_OBJECT_NULL
)
463 _vm_object_allocate(size
, object
);
469 lck_grp_t vm_object_lck_grp
;
470 lck_grp_attr_t vm_object_lck_grp_attr
;
471 lck_attr_t vm_object_lck_attr
;
472 lck_attr_t kernel_object_lck_attr
;
475 * vm_object_bootstrap:
477 * Initialize the VM objects module.
479 __private_extern__
void
480 vm_object_bootstrap(void)
484 vm_object_zone
= zinit((vm_size_t
) sizeof(struct vm_object
),
485 round_page(512*1024),
489 vm_object_init_lck_grp();
492 queue_init(&vm_object_cached_list
);
494 lck_mtx_init_ext(&vm_object_cached_lock_data
,
495 &vm_object_cached_lock_data_ext
,
497 &vm_object_lck_attr
);
499 queue_init(&vm_object_reaper_queue
);
501 for (i
= 0; i
< VM_OBJECT_HASH_LOCK_COUNT
; i
++) {
502 lck_mtx_init_ext(&vm_object_hashed_lock_data
[i
],
503 &vm_object_hashed_lock_data_ext
[i
],
505 &vm_object_lck_attr
);
507 lck_mtx_init_ext(&vm_object_reaper_lock_data
,
508 &vm_object_reaper_lock_data_ext
,
510 &vm_object_lck_attr
);
512 vm_object_hash_zone
=
513 zinit((vm_size_t
) sizeof (struct vm_object_hash_entry
),
514 round_page(512*1024),
516 "vm object hash entries");
518 for (i
= 0; i
< VM_OBJECT_HASH_COUNT
; i
++)
519 queue_init(&vm_object_hashtable
[i
]);
523 * Fill in a template object, for quick initialization
526 /* memq; Lock; init after allocation */
527 vm_object_template
.memq
.prev
= NULL
;
528 vm_object_template
.memq
.next
= NULL
;
531 * We can't call vm_object_lock_init() here because that will
532 * allocate some memory and VM is not fully initialized yet.
533 * The lock will be initialized for each allocated object in
534 * _vm_object_allocate(), so we don't need to initialize it in
535 * the vm_object_template.
537 vm_object_lock_init(&vm_object_template
);
539 vm_object_template
.size
= 0;
540 vm_object_template
.memq_hint
= VM_PAGE_NULL
;
541 vm_object_template
.ref_count
= 1;
543 vm_object_template
.res_count
= 1;
544 #endif /* TASK_SWAPPER */
545 vm_object_template
.resident_page_count
= 0;
546 vm_object_template
.wired_page_count
= 0;
547 vm_object_template
.reusable_page_count
= 0;
548 vm_object_template
.copy
= VM_OBJECT_NULL
;
549 vm_object_template
.shadow
= VM_OBJECT_NULL
;
550 vm_object_template
.shadow_offset
= (vm_object_offset_t
) 0;
551 vm_object_template
.pager
= MEMORY_OBJECT_NULL
;
552 vm_object_template
.paging_offset
= 0;
553 vm_object_template
.pager_control
= MEMORY_OBJECT_CONTROL_NULL
;
554 vm_object_template
.copy_strategy
= MEMORY_OBJECT_COPY_SYMMETRIC
;
555 vm_object_template
.paging_in_progress
= 0;
556 vm_object_template
.activity_in_progress
= 0;
558 /* Begin bitfields */
559 vm_object_template
.all_wanted
= 0; /* all bits FALSE */
560 vm_object_template
.pager_created
= FALSE
;
561 vm_object_template
.pager_initialized
= FALSE
;
562 vm_object_template
.pager_ready
= FALSE
;
563 vm_object_template
.pager_trusted
= FALSE
;
564 vm_object_template
.can_persist
= FALSE
;
565 vm_object_template
.internal
= TRUE
;
566 vm_object_template
.temporary
= TRUE
;
567 vm_object_template
.private = FALSE
;
568 vm_object_template
.pageout
= FALSE
;
569 vm_object_template
.alive
= TRUE
;
570 vm_object_template
.purgable
= VM_PURGABLE_DENY
;
571 vm_object_template
.shadowed
= FALSE
;
572 vm_object_template
.silent_overwrite
= FALSE
;
573 vm_object_template
.advisory_pageout
= FALSE
;
574 vm_object_template
.true_share
= FALSE
;
575 vm_object_template
.terminating
= FALSE
;
576 vm_object_template
.named
= FALSE
;
577 vm_object_template
.shadow_severed
= FALSE
;
578 vm_object_template
.phys_contiguous
= FALSE
;
579 vm_object_template
.nophyscache
= FALSE
;
582 vm_object_template
.cached_list
.prev
= NULL
;
583 vm_object_template
.cached_list
.next
= NULL
;
584 vm_object_template
.msr_q
.prev
= NULL
;
585 vm_object_template
.msr_q
.next
= NULL
;
587 vm_object_template
.last_alloc
= (vm_object_offset_t
) 0;
588 vm_object_template
.sequential
= (vm_object_offset_t
) 0;
589 vm_object_template
.pages_created
= 0;
590 vm_object_template
.pages_used
= 0;
593 vm_object_template
.existence_map
= VM_EXTERNAL_NULL
;
594 #endif /* MACH_PAGEMAP */
595 vm_object_template
.cow_hint
= ~(vm_offset_t
)0;
597 vm_object_template
.paging_object
= VM_OBJECT_NULL
;
598 #endif /* MACH_ASSERT */
600 /* cache bitfields */
601 vm_object_template
.wimg_bits
= VM_WIMG_DEFAULT
;
602 vm_object_template
.code_signed
= FALSE
;
603 vm_object_template
.hashed
= FALSE
;
604 vm_object_template
.transposed
= FALSE
;
605 vm_object_template
.mapping_in_progress
= FALSE
;
606 vm_object_template
.volatile_empty
= FALSE
;
607 vm_object_template
.volatile_fault
= FALSE
;
608 vm_object_template
.all_reusable
= FALSE
;
609 vm_object_template
.blocked_access
= FALSE
;
610 vm_object_template
.__object2_unused_bits
= 0;
612 vm_object_template
.uplq
.prev
= NULL
;
613 vm_object_template
.uplq
.next
= NULL
;
614 #endif /* UPL_DEBUG */
616 bzero(&vm_object_template
.pip_holders
,
617 sizeof (vm_object_template
.pip_holders
));
618 #endif /* VM_PIP_DEBUG */
620 vm_object_template
.objq
.next
=NULL
;
621 vm_object_template
.objq
.prev
=NULL
;
625 * Initialize the "kernel object"
628 kernel_object
= &kernel_object_store
;
631 * Note that in the following size specifications, we need to add 1 because
632 * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size.
636 _vm_object_allocate(vm_last_addr
+ 1,
639 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
642 kernel_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
645 * Initialize the "submap object". Make it as large as the
646 * kernel object so that no limit is imposed on submap sizes.
649 vm_submap_object
= &vm_submap_object_store
;
651 _vm_object_allocate(vm_last_addr
+ 1,
654 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
657 vm_submap_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
660 * Create an "extra" reference to this object so that we never
661 * try to deallocate it; zfree doesn't like to be called with
664 vm_object_reference(vm_submap_object
);
667 vm_external_module_initialize();
668 #endif /* MACH_PAGEMAP */
672 vm_object_reaper_init(void)
677 kr
= kernel_thread_start_priority(
678 (thread_continue_t
) vm_object_reaper_thread
,
682 if (kr
!= KERN_SUCCESS
) {
683 panic("failed to launch vm_object_reaper_thread kr=0x%x", kr
);
685 thread_deallocate(thread
);
688 __private_extern__
void
692 * Finish initializing the kernel object.
697 __private_extern__
void
698 vm_object_init_lck_grp(void)
701 * initialze the vm_object lock world
703 lck_grp_attr_setdefault(&vm_object_lck_grp_attr
);
704 lck_grp_init(&vm_object_lck_grp
, "vm_object", &vm_object_lck_grp_attr
);
705 lck_attr_setdefault(&vm_object_lck_attr
);
706 lck_attr_setdefault(&kernel_object_lck_attr
);
707 lck_attr_cleardebug(&kernel_object_lck_attr
);
711 #define MIGHT_NOT_CACHE_SHADOWS 1
712 #if MIGHT_NOT_CACHE_SHADOWS
713 static int cache_shadows
= TRUE
;
714 #endif /* MIGHT_NOT_CACHE_SHADOWS */
718 * vm_object_deallocate:
720 * Release a reference to the specified object,
721 * gained either through a vm_object_allocate
722 * or a vm_object_reference call. When all references
723 * are gone, storage associated with this object
724 * may be relinquished.
726 * No object may be locked.
728 unsigned long vm_object_deallocate_shared_successes
= 0;
729 unsigned long vm_object_deallocate_shared_failures
= 0;
730 unsigned long vm_object_deallocate_shared_swap_failures
= 0;
731 __private_extern__
void
732 vm_object_deallocate(
733 register vm_object_t object
)
736 boolean_t retry_cache_trim
= FALSE
;
737 uint32_t try_failed_count
= 0;
739 vm_object_t shadow
= VM_OBJECT_NULL
;
741 // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
742 // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
744 if (object
== VM_OBJECT_NULL
)
747 if (object
== kernel_object
) {
748 vm_object_lock_shared(object
);
750 OSAddAtomic(-1, &object
->ref_count
);
752 if (object
->ref_count
== 0) {
753 panic("vm_object_deallocate: losing kernel_object\n");
755 vm_object_unlock(object
);
759 if (object
->ref_count
> 2 ||
760 (!object
->named
&& object
->ref_count
> 1)) {
761 UInt32 original_ref_count
;
762 volatile UInt32
*ref_count_p
;
766 * The object currently looks like it is not being
767 * kept alive solely by the reference we're about to release.
768 * Let's try and release our reference without taking
769 * all the locks we would need if we had to terminate the
770 * object (cache lock + exclusive object lock).
771 * Lock the object "shared" to make sure we don't race with
772 * anyone holding it "exclusive".
774 vm_object_lock_shared(object
);
775 ref_count_p
= (volatile UInt32
*) &object
->ref_count
;
776 original_ref_count
= object
->ref_count
;
778 * Test again as "ref_count" could have changed.
779 * "named" shouldn't change.
781 if (original_ref_count
> 2 ||
782 (!object
->named
&& original_ref_count
> 1)) {
783 atomic_swap
= OSCompareAndSwap(
785 original_ref_count
- 1,
786 (UInt32
*) &object
->ref_count
);
787 if (atomic_swap
== FALSE
) {
788 vm_object_deallocate_shared_swap_failures
++;
794 vm_object_unlock(object
);
798 * ref_count was updated atomically !
800 vm_object_deallocate_shared_successes
++;
805 * Someone else updated the ref_count at the same
806 * time and we lost the race. Fall back to the usual
807 * slow but safe path...
809 vm_object_deallocate_shared_failures
++;
812 while (object
!= VM_OBJECT_NULL
) {
814 vm_object_lock(object
);
816 assert(object
->ref_count
> 0);
819 * If the object has a named reference, and only
820 * that reference would remain, inform the pager
821 * about the last "mapping" reference going away.
823 if ((object
->ref_count
== 2) && (object
->named
)) {
824 memory_object_t pager
= object
->pager
;
826 /* Notify the Pager that there are no */
827 /* more mappers for this object */
829 if (pager
!= MEMORY_OBJECT_NULL
) {
830 vm_object_mapping_wait(object
, THREAD_UNINT
);
831 vm_object_mapping_begin(object
);
832 vm_object_unlock(object
);
834 memory_object_last_unmap(pager
);
836 vm_object_lock(object
);
837 vm_object_mapping_end(object
);
840 * recheck the ref_count since we dropped the object lock
841 * to call 'memory_object_last_unmap'... it's possible
842 * additional references got taken and we only want
843 * to deactivate the pages if this 'named' object will only
844 * referenced by the backing pager once we drop our reference
847 if (!object
->terminating
&& object
->ref_count
== 2)
848 vm_object_deactivate_all_pages(object
);
850 assert(object
->ref_count
> 0);
854 * Lose the reference. If other references
855 * remain, then we are done, unless we need
856 * to retry a cache trim.
857 * If it is the last reference, then keep it
858 * until any pending initialization is completed.
861 /* if the object is terminating, it cannot go into */
862 /* the cache and we obviously should not call */
863 /* terminate again. */
865 if ((object
->ref_count
> 1) || object
->terminating
) {
866 vm_object_lock_assert_exclusive(object
);
868 vm_object_res_deallocate(object
);
870 if (object
->ref_count
== 1 &&
871 object
->shadow
!= VM_OBJECT_NULL
) {
873 * There's only one reference left on this
874 * VM object. We can't tell if it's a valid
875 * one (from a mapping for example) or if this
876 * object is just part of a possibly stale and
877 * useless shadow chain.
878 * We would like to try and collapse it into
879 * its parent, but we don't have any pointers
880 * back to this parent object.
881 * But we can try and collapse this object with
882 * its own shadows, in case these are useless
884 * We can't bypass this object though, since we
885 * don't know if this last reference on it is
888 vm_object_collapse(object
, 0, FALSE
);
890 vm_object_unlock(object
);
892 if (retry_cache_trim
&&
893 ((object
= vm_object_cache_trim(TRUE
)) !=
902 * We have to wait for initialization
903 * before destroying or caching the object.
906 if (object
->pager_created
&& ! object
->pager_initialized
) {
907 assert(! object
->can_persist
);
908 vm_object_assert_wait(object
,
909 VM_OBJECT_EVENT_INITIALIZED
,
911 vm_object_unlock(object
);
913 thread_block(THREAD_CONTINUE_NULL
);
919 * If this object can persist, then enter it in
920 * the cache. Otherwise, terminate it.
922 * NOTE: Only permanent objects are cached, and
923 * permanent objects cannot have shadows. This
924 * affects the residence counting logic in a minor
925 * way (can do it in-line, mostly).
928 if ((object
->can_persist
) && (object
->alive
)) {
930 * Now it is safe to decrement reference count,
931 * and to return if reference count is > 0.
934 vm_object_lock_assert_exclusive(object
);
935 if (--object
->ref_count
> 0) {
936 vm_object_res_deallocate(object
);
937 vm_object_unlock(object
);
939 if (retry_cache_trim
&&
940 ((object
= vm_object_cache_trim(TRUE
)) !=
947 #if MIGHT_NOT_CACHE_SHADOWS
949 * Remove shadow now if we don't
950 * want to cache shadows.
952 if (! cache_shadows
) {
953 shadow
= object
->shadow
;
954 object
->shadow
= VM_OBJECT_NULL
;
956 #endif /* MIGHT_NOT_CACHE_SHADOWS */
959 * Enter the object onto the queue of
960 * cached objects, and deactivate
963 assert(object
->shadow
== VM_OBJECT_NULL
);
964 VM_OBJ_RES_DECR(object
);
966 "vm_o_deallocate: adding %x to cache, queue = (%x, %x)\n",
968 vm_object_cached_list
.next
,
969 vm_object_cached_list
.prev
,0,0);
972 vm_object_unlock(object
);
974 try_failed_count
= 0;
976 vm_object_cache_lock();
979 * if we try to take a regular lock here
980 * we risk deadlocking against someone
981 * holding a lock on this object while
982 * trying to vm_object_deallocate a different
985 if (vm_object_lock_try(object
))
987 vm_object_cache_unlock();
990 mutex_pause(try_failed_count
); /* wait a bit */
992 vm_object_cached_count
++;
993 if (vm_object_cached_count
> vm_object_cached_high
)
994 vm_object_cached_high
= vm_object_cached_count
;
995 queue_enter(&vm_object_cached_list
, object
,
996 vm_object_t
, cached_list
);
997 vm_object_cache_unlock();
999 vm_object_deactivate_all_pages(object
);
1000 vm_object_unlock(object
);
1002 #if MIGHT_NOT_CACHE_SHADOWS
1004 * If we have a shadow that we need
1005 * to deallocate, do so now, remembering
1006 * to trim the cache later.
1008 if (! cache_shadows
&& shadow
!= VM_OBJECT_NULL
) {
1010 retry_cache_trim
= TRUE
;
1013 #endif /* MIGHT_NOT_CACHE_SHADOWS */
1016 * Trim the cache. If the cache trim
1017 * returns with a shadow for us to deallocate,
1018 * then remember to retry the cache trim
1019 * when we are done deallocating the shadow.
1020 * Otherwise, we are done.
1023 object
= vm_object_cache_trim(TRUE
);
1024 if (object
== VM_OBJECT_NULL
) {
1027 retry_cache_trim
= TRUE
;
1029 #endif /* VM_OBJECT_CACHE */
1032 * This object is not cachable; terminate it.
1035 "vm_o_deallocate: !cacheable 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
1036 object
, object
->resident_page_count
,
1037 object
->paging_in_progress
,
1038 (void *)current_thread(),object
->ref_count
);
1040 VM_OBJ_RES_DECR(object
); /* XXX ? */
1042 * Terminate this object. If it had a shadow,
1043 * then deallocate it; otherwise, if we need
1044 * to retry a cache trim, do so now; otherwise,
1045 * we are done. "pageout" objects have a shadow,
1046 * but maintain a "paging reference" rather than
1047 * a normal reference.
1049 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
1051 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
1054 if (shadow
!= VM_OBJECT_NULL
) {
1059 if (retry_cache_trim
&&
1060 ((object
= vm_object_cache_trim(TRUE
)) !=
1069 assert(! retry_cache_trim
);
1076 * Check to see whether we really need to trim
1077 * down the cache. If so, remove an object from
1078 * the cache, terminate it, and repeat.
1080 * Called with, and returns with, cache lock unlocked.
1083 vm_object_cache_trim(
1084 boolean_t called_from_vm_object_deallocate
)
1086 register vm_object_t object
= VM_OBJECT_NULL
;
1092 * If we no longer need to trim the cache,
1095 if (vm_object_cached_count
<= vm_object_cached_max
)
1096 return VM_OBJECT_NULL
;
1098 vm_object_cache_lock();
1099 if (vm_object_cached_count
<= vm_object_cached_max
) {
1100 vm_object_cache_unlock();
1101 return VM_OBJECT_NULL
;
1105 * We must trim down the cache, so remove
1106 * the first object in the cache.
1109 "vm_object_cache_trim: removing from front of cache (%x, %x)\n",
1110 vm_object_cached_list
.next
,
1111 vm_object_cached_list
.prev
, 0, 0, 0);
1113 object
= (vm_object_t
) queue_first(&vm_object_cached_list
);
1114 if(object
== (vm_object_t
) &vm_object_cached_list
) {
1115 /* something's wrong with the calling parameter or */
1116 /* the value of vm_object_cached_count, just fix */
1118 if(vm_object_cached_max
< 0)
1119 vm_object_cached_max
= 0;
1120 vm_object_cached_count
= 0;
1121 vm_object_cache_unlock();
1122 return VM_OBJECT_NULL
;
1124 vm_object_lock(object
);
1125 queue_remove(&vm_object_cached_list
, object
, vm_object_t
,
1127 vm_object_cached_count
--;
1129 vm_object_cache_unlock();
1131 * Since this object is in the cache, we know
1132 * that it is initialized and has no references.
1133 * Take a reference to avoid recursive deallocations.
1136 assert(object
->pager_initialized
);
1137 assert(object
->ref_count
== 0);
1138 vm_object_lock_assert_exclusive(object
);
1139 object
->ref_count
++;
1142 * Terminate the object.
1143 * If the object had a shadow, we let vm_object_deallocate
1144 * deallocate it. "pageout" objects have a shadow, but
1145 * maintain a "paging reference" rather than a normal
1147 * (We are careful here to limit recursion.)
1149 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
1151 if(vm_object_terminate(object
) != KERN_SUCCESS
)
1154 if (shadow
!= VM_OBJECT_NULL
) {
1155 if (called_from_vm_object_deallocate
) {
1158 vm_object_deallocate(shadow
);
1167 * Routine: vm_object_terminate
1169 * Free all resources associated with a vm_object.
1170 * In/out conditions:
1171 * Upon entry, the object must be locked,
1172 * and the object must have exactly one reference.
1174 * The shadow object reference is left alone.
1176 * The object must be unlocked if its found that pages
1177 * must be flushed to a backing object. If someone
1178 * manages to map the object while it is being flushed
1179 * the object is returned unlocked and unchanged. Otherwise,
1180 * upon exit, the cache will be unlocked, and the
1181 * object will cease to exist.
1183 static kern_return_t
1184 vm_object_terminate(
1187 vm_object_t shadow_object
;
1189 XPR(XPR_VM_OBJECT
, "vm_object_terminate, object 0x%X ref %d\n",
1190 object
, object
->ref_count
, 0, 0, 0);
1192 if (!object
->pageout
&& (!object
->temporary
|| object
->can_persist
) &&
1193 (object
->pager
!= NULL
|| object
->shadow_severed
)) {
1195 * Clear pager_trusted bit so that the pages get yanked
1196 * out of the object instead of cleaned in place. This
1197 * prevents a deadlock in XMM and makes more sense anyway.
1199 object
->pager_trusted
= FALSE
;
1201 vm_object_reap_pages(object
, REAP_TERMINATE
);
1204 * Make sure the object isn't already being terminated
1206 if (object
->terminating
) {
1207 vm_object_lock_assert_exclusive(object
);
1208 object
->ref_count
--;
1209 assert(object
->ref_count
> 0);
1210 vm_object_unlock(object
);
1211 return KERN_FAILURE
;
1215 * Did somebody get a reference to the object while we were
1218 if (object
->ref_count
!= 1) {
1219 vm_object_lock_assert_exclusive(object
);
1220 object
->ref_count
--;
1221 assert(object
->ref_count
> 0);
1222 vm_object_res_deallocate(object
);
1223 vm_object_unlock(object
);
1224 return KERN_FAILURE
;
1228 * Make sure no one can look us up now.
1231 object
->terminating
= TRUE
;
1232 object
->alive
= FALSE
;
1234 if (object
->hashed
) {
1237 lck
= vm_object_hash_lock_spin(object
->pager
);
1238 vm_object_remove(object
);
1239 vm_object_hash_unlock(lck
);
1242 * Detach the object from its shadow if we are the shadow's
1243 * copy. The reference we hold on the shadow must be dropped
1246 if (((shadow_object
= object
->shadow
) != VM_OBJECT_NULL
) &&
1247 !(object
->pageout
)) {
1248 vm_object_lock(shadow_object
);
1249 if (shadow_object
->copy
== object
)
1250 shadow_object
->copy
= VM_OBJECT_NULL
;
1251 vm_object_unlock(shadow_object
);
1254 if (object
->paging_in_progress
!= 0 ||
1255 object
->activity_in_progress
!= 0) {
1257 * There are still some paging_in_progress references
1258 * on this object, meaning that there are some paging
1259 * or other I/O operations in progress for this VM object.
1260 * Such operations take some paging_in_progress references
1261 * up front to ensure that the object doesn't go away, but
1262 * they may also need to acquire a reference on the VM object,
1263 * to map it in kernel space, for example. That means that
1264 * they may end up releasing the last reference on the VM
1265 * object, triggering its termination, while still holding
1266 * paging_in_progress references. Waiting for these
1267 * pending paging_in_progress references to go away here would
1270 * To avoid deadlocking, we'll let the vm_object_reaper_thread
1271 * complete the VM object termination if it still holds
1272 * paging_in_progress references at this point.
1274 * No new paging_in_progress should appear now that the
1275 * VM object is "terminating" and not "alive".
1277 vm_object_reap_async(object
);
1278 vm_object_unlock(object
);
1280 * Return KERN_FAILURE to let the caller know that we
1281 * haven't completed the termination and it can't drop this
1282 * object's reference on its shadow object yet.
1283 * The reaper thread will take care of that once it has
1284 * completed this object's termination.
1286 return KERN_FAILURE
;
1289 * complete the VM object termination
1291 vm_object_reap(object
);
1292 object
= VM_OBJECT_NULL
;
1295 * the object lock was released by vm_object_reap()
1297 * KERN_SUCCESS means that this object has been terminated
1298 * and no longer needs its shadow object but still holds a
1300 * The caller is responsible for dropping that reference.
1301 * We can't call vm_object_deallocate() here because that
1302 * would create a recursion.
1304 return KERN_SUCCESS
;
1311 * Complete the termination of a VM object after it's been marked
1312 * as "terminating" and "!alive" by vm_object_terminate().
1314 * The VM object must be locked by caller.
1315 * The lock will be released on return and the VM object is no longer valid.
1321 memory_object_t pager
;
1323 vm_object_lock_assert_exclusive(object
);
1324 assert(object
->paging_in_progress
== 0);
1325 assert(object
->activity_in_progress
== 0);
1327 vm_object_reap_count
++;
1329 pager
= object
->pager
;
1330 object
->pager
= MEMORY_OBJECT_NULL
;
1332 if (pager
!= MEMORY_OBJECT_NULL
)
1333 memory_object_control_disable(object
->pager_control
);
1335 object
->ref_count
--;
1337 assert(object
->res_count
== 0);
1338 #endif /* TASK_SWAPPER */
1340 assert (object
->ref_count
== 0);
1343 * remove from purgeable queue if it's on
1345 if (object
->objq
.next
|| object
->objq
.prev
) {
1346 purgeable_q_t queue
= vm_purgeable_object_remove(object
);
1349 /* Must take page lock for this - using it to protect token queue */
1350 vm_page_lock_queues();
1351 vm_purgeable_token_delete_first(queue
);
1353 assert(queue
->debug_count_objects
>=0);
1354 vm_page_unlock_queues();
1358 * Clean or free the pages, as appropriate.
1359 * It is possible for us to find busy/absent pages,
1360 * if some faults on this object were aborted.
1362 if (object
->pageout
) {
1363 assert(object
->shadow
!= VM_OBJECT_NULL
);
1365 vm_pageout_object_terminate(object
);
1367 } else if (((object
->temporary
&& !object
->can_persist
) || (pager
== MEMORY_OBJECT_NULL
))) {
1369 vm_object_reap_pages(object
, REAP_REAP
);
1371 assert(queue_empty(&object
->memq
));
1372 assert(object
->paging_in_progress
== 0);
1373 assert(object
->activity_in_progress
== 0);
1374 assert(object
->ref_count
== 0);
1377 * If the pager has not already been released by
1378 * vm_object_destroy, we need to terminate it and
1379 * release our reference to it here.
1381 if (pager
!= MEMORY_OBJECT_NULL
) {
1382 vm_object_unlock(object
);
1383 vm_object_release_pager(pager
, object
->hashed
);
1384 vm_object_lock(object
);
1387 /* kick off anyone waiting on terminating */
1388 object
->terminating
= FALSE
;
1389 vm_object_paging_begin(object
);
1390 vm_object_paging_end(object
);
1391 vm_object_unlock(object
);
1394 vm_external_destroy(object
->existence_map
, object
->size
);
1395 #endif /* MACH_PAGEMAP */
1397 object
->shadow
= VM_OBJECT_NULL
;
1399 vm_object_lock_destroy(object
);
1401 * Free the space for the object.
1403 zfree(vm_object_zone
, object
);
1404 object
= VM_OBJECT_NULL
;
1409 #define V_O_R_MAX_BATCH 128
1412 #define VM_OBJ_REAP_FREELIST(_local_free_q, do_disconnect) \
1414 if (_local_free_q) { \
1415 if (do_disconnect) { \
1417 for (m = _local_free_q; \
1418 m != VM_PAGE_NULL; \
1419 m = (vm_page_t) m->pageq.next) { \
1421 pmap_disconnect(m->phys_page); \
1425 vm_page_free_list(_local_free_q, TRUE); \
1426 _local_free_q = VM_PAGE_NULL; \
1432 vm_object_reap_pages(
1438 vm_page_t local_free_q
= VM_PAGE_NULL
;
1440 boolean_t disconnect_on_release
;
1442 if (reap_type
== REAP_DATA_FLUSH
) {
1444 * We need to disconnect pages from all pmaps before
1445 * releasing them to the free list
1447 disconnect_on_release
= TRUE
;
1450 * Either the caller has already disconnected the pages
1451 * from all pmaps, or we disconnect them here as we add
1452 * them to out local list of pages to be released.
1453 * No need to re-disconnect them when we release the pages
1456 disconnect_on_release
= FALSE
;
1459 restart_after_sleep
:
1460 if (queue_empty(&object
->memq
))
1462 loop_count
= V_O_R_MAX_BATCH
+ 1;
1464 vm_page_lockspin_queues();
1466 next
= (vm_page_t
)queue_first(&object
->memq
);
1468 while (!queue_end(&object
->memq
, (queue_entry_t
)next
)) {
1471 next
= (vm_page_t
)queue_next(&next
->listq
);
1473 if (--loop_count
== 0) {
1475 vm_page_unlock_queues();
1479 * Free the pages we reclaimed so far
1480 * and take a little break to avoid
1481 * hogging the page queue lock too long
1483 VM_OBJ_REAP_FREELIST(local_free_q
,
1484 disconnect_on_release
);
1488 loop_count
= V_O_R_MAX_BATCH
+ 1;
1490 vm_page_lockspin_queues();
1492 if (reap_type
== REAP_DATA_FLUSH
|| reap_type
== REAP_TERMINATE
) {
1494 if (reap_type
== REAP_DATA_FLUSH
&& (p
->pageout
== TRUE
&& p
->list_req_pending
== TRUE
)) {
1495 p
->list_req_pending
= FALSE
;
1496 p
->cleaning
= FALSE
;
1499 * need to drop the laundry count...
1500 * we may also need to remove it
1501 * from the I/O paging queue...
1502 * vm_pageout_throttle_up handles both cases
1504 * the laundry and pageout_queue flags are cleared...
1508 vm_pageout_throttle_up(p
);
1510 vm_pageout_throttle_up(p
);
1514 * toss the wire count we picked up
1515 * when we intially set this page up
1521 } else if (p
->busy
|| p
->cleaning
) {
1523 vm_page_unlock_queues();
1525 * free the pages reclaimed so far
1527 VM_OBJ_REAP_FREELIST(local_free_q
,
1528 disconnect_on_release
);
1530 PAGE_SLEEP(object
, p
, THREAD_UNINT
);
1532 goto restart_after_sleep
;
1535 switch (reap_type
) {
1537 case REAP_DATA_FLUSH
:
1538 if (VM_PAGE_WIRED(p
)) {
1540 * this is an odd case... perhaps we should
1541 * zero-fill this page since we're conceptually
1542 * tossing its data at this point, but leaving
1543 * it on the object to honor the 'wire' contract
1549 case REAP_PURGEABLE
:
1550 if (VM_PAGE_WIRED(p
)) {
1551 /* can't purge a wired page */
1552 vm_page_purged_wired
++;
1558 * We can't reclaim a busy page but we can
1559 * make it pageable (it's not wired) to make
1560 * sure that it gets considered by
1561 * vm_pageout_scan() later.
1563 vm_page_deactivate(p
);
1564 vm_page_purged_busy
++;
1568 if (p
->cleaning
|| p
->laundry
|| p
->list_req_pending
) {
1570 * page is being acted upon,
1571 * so don't mess with it
1573 vm_page_purged_others
++;
1576 assert(p
->object
!= kernel_object
);
1579 * we can discard this page...
1581 if (p
->pmapped
== TRUE
) {
1586 refmod_state
= pmap_disconnect(p
->phys_page
);
1587 if (refmod_state
& VM_MEM_MODIFIED
) {
1591 if (p
->dirty
|| p
->precious
) {
1593 * we saved the cost of cleaning this page !
1595 vm_page_purged_count
++;
1600 case REAP_TERMINATE
:
1601 if (p
->absent
|| p
->private) {
1603 * For private pages, VM_PAGE_FREE just
1604 * leaves the page structure around for
1605 * its owner to clean up. For absent
1606 * pages, the structure is returned to
1607 * the appropriate pool.
1611 if (p
->fictitious
) {
1612 assert (p
->phys_page
== vm_page_guard_addr
);
1615 if (!p
->dirty
&& p
->wpmapped
)
1616 p
->dirty
= pmap_is_modified(p
->phys_page
);
1618 if ((p
->dirty
|| p
->precious
) && !p
->error
&& object
->alive
) {
1622 VM_PAGE_QUEUES_REMOVE(p
);
1624 vm_page_unlock_queues();
1626 * free the pages reclaimed so far
1628 VM_OBJ_REAP_FREELIST(local_free_q
,
1629 disconnect_on_release
);
1632 * flush page... page will be freed
1633 * upon completion of I/O
1635 vm_pageout_cluster(p
);
1636 vm_object_paging_wait(object
, THREAD_UNINT
);
1638 goto restart_after_sleep
;
1645 vm_page_free_prepare_queues(p
);
1646 assert(p
->pageq
.next
== NULL
&& p
->pageq
.prev
== NULL
);
1648 * Add this page to our list of reclaimed pages,
1649 * to be freed later.
1651 p
->pageq
.next
= (queue_entry_t
) local_free_q
;
1654 vm_page_unlock_queues();
1657 * Free the remaining reclaimed pages
1659 VM_OBJ_REAP_FREELIST(local_free_q
,
1660 disconnect_on_release
);
1665 vm_object_reap_async(
1668 vm_object_lock_assert_exclusive(object
);
1670 vm_object_reaper_lock_spin();
1672 vm_object_reap_count_async
++;
1674 /* enqueue the VM object... */
1675 queue_enter(&vm_object_reaper_queue
, object
,
1676 vm_object_t
, cached_list
);
1678 vm_object_reaper_unlock();
1680 /* ... and wake up the reaper thread */
1681 thread_wakeup((event_t
) &vm_object_reaper_queue
);
1686 vm_object_reaper_thread(void)
1688 vm_object_t object
, shadow_object
;
1690 vm_object_reaper_lock_spin();
1692 while (!queue_empty(&vm_object_reaper_queue
)) {
1693 queue_remove_first(&vm_object_reaper_queue
,
1698 vm_object_reaper_unlock();
1699 vm_object_lock(object
);
1701 assert(object
->terminating
);
1702 assert(!object
->alive
);
1705 * The pageout daemon might be playing with our pages.
1706 * Now that the object is dead, it won't touch any more
1707 * pages, but some pages might already be on their way out.
1708 * Hence, we wait until the active paging activities have
1709 * ceased before we break the association with the pager
1712 while (object
->paging_in_progress
!= 0 ||
1713 object
->activity_in_progress
!= 0) {
1714 vm_object_wait(object
,
1715 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
1717 vm_object_lock(object
);
1721 object
->pageout
? VM_OBJECT_NULL
: object
->shadow
;
1723 vm_object_reap(object
);
1724 /* cache is unlocked and object is no longer valid */
1725 object
= VM_OBJECT_NULL
;
1727 if (shadow_object
!= VM_OBJECT_NULL
) {
1729 * Drop the reference "object" was holding on
1730 * its shadow object.
1732 vm_object_deallocate(shadow_object
);
1733 shadow_object
= VM_OBJECT_NULL
;
1735 vm_object_reaper_lock_spin();
1738 /* wait for more work... */
1739 assert_wait((event_t
) &vm_object_reaper_queue
, THREAD_UNINT
);
1741 vm_object_reaper_unlock();
1743 thread_block((thread_continue_t
) vm_object_reaper_thread
);
1748 * Routine: vm_object_pager_wakeup
1749 * Purpose: Wake up anyone waiting for termination of a pager.
1753 vm_object_pager_wakeup(
1754 memory_object_t pager
)
1756 vm_object_hash_entry_t entry
;
1757 boolean_t waiting
= FALSE
;
1761 * If anyone was waiting for the memory_object_terminate
1762 * to be queued, wake them up now.
1764 lck
= vm_object_hash_lock_spin(pager
);
1765 entry
= vm_object_hash_lookup(pager
, TRUE
);
1766 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
1767 waiting
= entry
->waiting
;
1768 vm_object_hash_unlock(lck
);
1770 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
) {
1772 thread_wakeup((event_t
) pager
);
1773 vm_object_hash_entry_free(entry
);
1778 * Routine: vm_object_release_pager
1779 * Purpose: Terminate the pager and, upon completion,
1780 * release our last reference to it.
1781 * just like memory_object_terminate, except
1782 * that we wake up anyone blocked in vm_object_enter
1783 * waiting for termination message to be queued
1784 * before calling memory_object_init.
1787 vm_object_release_pager(
1788 memory_object_t pager
,
1793 * Terminate the pager.
1796 (void) memory_object_terminate(pager
);
1798 if (hashed
== TRUE
) {
1800 * Wakeup anyone waiting for this terminate
1801 * and remove the entry from the hash
1803 vm_object_pager_wakeup(pager
);
1806 * Release reference to pager.
1808 memory_object_deallocate(pager
);
1812 * Routine: vm_object_destroy
1814 * Shut down a VM object, despite the
1815 * presence of address map (or other) references
1821 __unused kern_return_t reason
)
1823 memory_object_t old_pager
;
1825 if (object
== VM_OBJECT_NULL
)
1826 return(KERN_SUCCESS
);
1829 * Remove the pager association immediately.
1831 * This will prevent the memory manager from further
1832 * meddling. [If it wanted to flush data or make
1833 * other changes, it should have done so before performing
1834 * the destroy call.]
1837 vm_object_lock(object
);
1838 object
->can_persist
= FALSE
;
1839 object
->named
= FALSE
;
1840 object
->alive
= FALSE
;
1842 if (object
->hashed
) {
1845 * Rip out the pager from the vm_object now...
1847 lck
= vm_object_hash_lock_spin(object
->pager
);
1848 vm_object_remove(object
);
1849 vm_object_hash_unlock(lck
);
1851 old_pager
= object
->pager
;
1852 object
->pager
= MEMORY_OBJECT_NULL
;
1853 if (old_pager
!= MEMORY_OBJECT_NULL
)
1854 memory_object_control_disable(object
->pager_control
);
1857 * Wait for the existing paging activity (that got
1858 * through before we nulled out the pager) to subside.
1861 vm_object_paging_wait(object
, THREAD_UNINT
);
1862 vm_object_unlock(object
);
1865 * Terminate the object now.
1867 if (old_pager
!= MEMORY_OBJECT_NULL
) {
1868 vm_object_release_pager(old_pager
, object
->hashed
);
1871 * JMM - Release the caller's reference. This assumes the
1872 * caller had a reference to release, which is a big (but
1873 * currently valid) assumption if this is driven from the
1874 * vnode pager (it is holding a named reference when making
1877 vm_object_deallocate(object
);
1880 return(KERN_SUCCESS
);
1884 #define VM_OBJ_DEACT_ALL_STATS DEBUG
1885 #if VM_OBJ_DEACT_ALL_STATS
1886 uint32_t vm_object_deactivate_all_pages_batches
= 0;
1887 uint32_t vm_object_deactivate_all_pages_pages
= 0;
1888 #endif /* VM_OBJ_DEACT_ALL_STATS */
1890 * vm_object_deactivate_all_pages
1892 * Deactivate all pages in the specified object. (Keep its pages
1893 * in memory even though it is no longer referenced.)
1895 * The object must be locked.
1898 vm_object_deactivate_all_pages(
1899 register vm_object_t object
)
1901 register vm_page_t p
;
1903 #if VM_OBJ_DEACT_ALL_STATS
1905 #endif /* VM_OBJ_DEACT_ALL_STATS */
1906 #define V_O_D_A_P_MAX_BATCH 256
1908 loop_count
= V_O_D_A_P_MAX_BATCH
;
1909 #if VM_OBJ_DEACT_ALL_STATS
1911 #endif /* VM_OBJ_DEACT_ALL_STATS */
1912 vm_page_lock_queues();
1913 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1914 if (--loop_count
== 0) {
1915 #if VM_OBJ_DEACT_ALL_STATS
1916 hw_atomic_add(&vm_object_deactivate_all_pages_batches
,
1918 hw_atomic_add(&vm_object_deactivate_all_pages_pages
,
1921 #endif /* VM_OBJ_DEACT_ALL_STATS */
1922 lck_mtx_yield(&vm_page_queue_lock
);
1923 loop_count
= V_O_D_A_P_MAX_BATCH
;
1925 if (!p
->busy
&& !p
->throttled
) {
1926 #if VM_OBJ_DEACT_ALL_STATS
1928 #endif /* VM_OBJ_DEACT_ALL_STATS */
1929 vm_page_deactivate(p
);
1932 #if VM_OBJ_DEACT_ALL_STATS
1934 hw_atomic_add(&vm_object_deactivate_all_pages_batches
, 1);
1935 hw_atomic_add(&vm_object_deactivate_all_pages_pages
,
1939 #endif /* VM_OBJ_DEACT_ALL_STATS */
1940 vm_page_unlock_queues();
1946 * when deallocating pages it is necessary to hold
1947 * the vm_page_queue_lock (a hot global lock) for certain operations
1948 * on the page... however, the majority of the work can be done
1949 * while merely holding the object lock... to mitigate the time spent behind the
1950 * global lock, go to a 2 pass algorithm... collect pages up to DELAYED_WORK_LIMIT
1951 * while doing all of the work that doesn't require the vm_page_queue_lock...
1952 * them call dw_do_work to acquire the vm_page_queue_lock and do the
1953 * necessary work for each page... we will grab the busy bit on the page
1954 * so that dw_do_work can drop the object lock if it can't immediately take the
1955 * vm_page_queue_lock in order to compete for the locks in the same order that
1956 * vm_pageout_scan takes them.
1959 #define DELAYED_WORK_LIMIT 32
1961 #define DW_clear_reference 0x01
1962 #define DW_move_page 0x02
1963 #define DW_clear_busy 0x04
1964 #define DW_PAGE_WAKEUP 0x08
1972 static void dw_do_work(vm_object_t object
, struct dw
*dwp
, int dw_count
);
1985 * pageout_scan takes the vm_page_lock_queues first
1986 * then tries for the object lock... to avoid what
1987 * is effectively a lock inversion, we'll go to the
1988 * trouble of taking them in that same order... otherwise
1989 * if this object contains the majority of the pages resident
1990 * in the UBC (or a small set of large objects actively being
1991 * worked on contain the majority of the pages), we could
1992 * cause the pageout_scan thread to 'starve' in its attempt
1993 * to find pages to move to the free queue, since it has to
1994 * successfully acquire the object lock of any candidate page
1995 * before it can steal/clean it.
1997 if (!vm_page_trylockspin_queues()) {
1998 vm_object_unlock(object
);
2000 vm_page_lockspin_queues();
2002 for (j
= 0; ; j
++) {
2003 if (!vm_object_lock_avoid(object
) &&
2004 _vm_object_lock_try(object
))
2006 vm_page_unlock_queues();
2008 vm_page_lockspin_queues();
2011 for (j
= 0; j
< dw_count
; j
++, dwp
++) {
2015 if (dwp
->dw_mask
& DW_clear_reference
)
2016 m
->reference
= FALSE
;
2018 if (dwp
->dw_mask
& DW_move_page
) {
2019 VM_PAGE_QUEUES_REMOVE(m
);
2021 assert(!m
->laundry
);
2022 assert(m
->object
!= kernel_object
);
2023 assert(m
->pageq
.next
== NULL
&&
2024 m
->pageq
.prev
== NULL
);
2027 queue_enter_first(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
2028 vm_zf_queue_count
++;
2030 queue_enter_first(&vm_page_queue_inactive
, m
, vm_page_t
, pageq
);
2034 if (!m
->fictitious
) {
2035 vm_page_inactive_count
++;
2036 token_new_pagecount
++;
2038 assert(m
->phys_page
== vm_page_fictitious_addr
);
2041 if (dwp
->dw_mask
& DW_clear_busy
)
2042 dwp
->dw_m
->busy
= FALSE
;
2044 if (dwp
->dw_mask
& DW_PAGE_WAKEUP
)
2045 PAGE_WAKEUP(dwp
->dw_m
);
2047 vm_page_unlock_queues();
2054 * Decide if we need to send a memory status notification.
2057 (vm_page_active_count
+ vm_page_inactive_count
+
2058 vm_page_speculative_count
+ vm_page_free_count
+
2059 (IP_VALID(memory_manager_default
)?0:vm_page_purgeable_count
) ) * 100 /
2061 if (percent_avail
>= (kern_memorystatus_level
+ 5) ||
2062 percent_avail
<= (kern_memorystatus_level
- 5)) {
2063 kern_memorystatus_level
= percent_avail
;
2064 thread_wakeup((event_t
)&kern_memorystatus_wakeup
);
2073 * The "chunk" macros are used by routines below when looking for pages to deactivate. These
2074 * exist because of the need to handle shadow chains. When deactivating pages, we only
2075 * want to deactive the ones at the top most level in the object chain. In order to do
2076 * this efficiently, the specified address range is divided up into "chunks" and we use
2077 * a bit map to keep track of which pages have already been processed as we descend down
2078 * the shadow chain. These chunk macros hide the details of the bit map implementation
2079 * as much as we can.
2081 * For convenience, we use a 64-bit data type as the bit map, and therefore a chunk is
2082 * set to 64 pages. The bit map is indexed from the low-order end, so that the lowest
2083 * order bit represents page 0 in the current range and highest order bit represents
2086 * For further convenience, we also use negative logic for the page state in the bit map.
2087 * The bit is set to 1 to indicate it has not yet been seen, and to 0 to indicate it has
2088 * been processed. This way we can simply test the 64-bit long word to see if it's zero
2089 * to easily tell if the whole range has been processed. Therefore, the bit map starts
2090 * out with all the bits set. The macros below hide all these details from the caller.
2093 #define PAGES_IN_A_CHUNK 64 /* The number of pages in the chunk must */
2094 /* be the same as the number of bits in */
2095 /* the chunk_state_t type. We use 64 */
2096 /* just for convenience. */
2098 #define CHUNK_SIZE (PAGES_IN_A_CHUNK * PAGE_SIZE_64) /* Size of a chunk in bytes */
2100 typedef uint64_t chunk_state_t
;
2103 * The bit map uses negative logic, so we start out with all 64 bits set to indicate
2104 * that no pages have been processed yet. Also, if len is less than the full CHUNK_SIZE,
2105 * then we mark pages beyond the len as having been "processed" so that we don't waste time
2106 * looking at pages in that range. This can save us from unnecessarily chasing down the
2110 #define CHUNK_INIT(c, len) \
2114 (c) = 0xffffffffffffffffLL; \
2116 for (p = (len) / PAGE_SIZE_64; p < PAGES_IN_A_CHUNK; p++) \
2117 MARK_PAGE_HANDLED(c, p); \
2121 * Return true if all pages in the chunk have not yet been processed.
2124 #define CHUNK_NOT_COMPLETE(c) ((c) != 0)
2127 * Return true if the page at offset 'p' in the bit map has already been handled
2128 * while processing a higher level object in the shadow chain.
2131 #define PAGE_ALREADY_HANDLED(c, p) (((c) & (1LL << (p))) == 0)
2134 * Mark the page at offset 'p' in the bit map as having been processed.
2137 #define MARK_PAGE_HANDLED(c, p) \
2139 (c) = (c) & ~(1LL << (p)); \
2144 * Return true if the page at the given offset has been paged out. Object is
2145 * locked upon entry and returned locked.
2151 vm_object_offset_t offset
)
2154 memory_object_t pager
;
2157 * Check the existence map for the page if we have one, otherwise
2158 * ask the pager about this page.
2162 if (object
->existence_map
) {
2163 if (vm_external_state_get(object
->existence_map
, offset
)
2164 == VM_EXTERNAL_STATE_EXISTS
) {
2173 if (object
->internal
&&
2175 !object
->terminating
&&
2176 object
->pager_ready
) {
2179 * We're already holding a "paging in progress" reference
2180 * so the object can't disappear when we release the lock.
2183 assert(object
->paging_in_progress
);
2184 pager
= object
->pager
;
2185 vm_object_unlock(object
);
2187 kr
= memory_object_data_request(
2189 offset
+ object
->paging_offset
,
2190 0, /* just poke the pager */
2194 vm_object_lock(object
);
2196 if (kr
== KERN_SUCCESS
) {
2211 * Deactivate the pages in the specified object and range. If kill_page is set, also discard any
2212 * page modified state from the pmap. Update the chunk_state as we go along. The caller must specify
2213 * a size that is less than or equal to the CHUNK_SIZE.
2217 deactivate_pages_in_object(
2219 vm_object_offset_t offset
,
2220 vm_object_size_t size
,
2221 boolean_t kill_page
,
2222 boolean_t reusable_page
,
2226 boolean_t all_reusable
,
2227 chunk_state_t
*chunk_state
)
2231 struct dw dw_array
[DELAYED_WORK_LIMIT
];
2234 unsigned int reusable
= 0;
2238 * Examine each page in the chunk. The variable 'p' is the page number relative to the start of the
2239 * chunk. Since this routine is called once for each level in the shadow chain, the chunk_state may
2240 * have pages marked as having been processed already. We stop the loop early if we find we've handled
2241 * all the pages in the chunk.
2247 for(p
= 0; size
&& CHUNK_NOT_COMPLETE(*chunk_state
); p
++, size
-= PAGE_SIZE_64
, offset
+= PAGE_SIZE_64
) {
2250 * If this offset has already been found and handled in a higher level object, then don't
2251 * do anything with it in the current shadow object.
2254 if (PAGE_ALREADY_HANDLED(*chunk_state
, p
))
2258 * See if the page at this offset is around. First check to see if the page is resident,
2259 * then if not, check the existence map or with the pager.
2262 if ((m
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
2265 * We found a page we were looking for. Mark it as "handled" now in the chunk_state
2266 * so that we won't bother looking for a page at this offset again if there are more
2267 * shadow objects. Then deactivate the page.
2270 MARK_PAGE_HANDLED(*chunk_state
, p
);
2272 if (( !VM_PAGE_WIRED(m
)) && (!m
->private) && (!m
->gobbled
) && (!m
->busy
)) {
2275 assert(!m
->laundry
);
2277 clear_refmod
= VM_MEM_REFERENCED
;
2278 dwp
->dw_mask
= DW_clear_reference
;
2280 if ((kill_page
) && (object
->internal
)) {
2281 m
->precious
= FALSE
;
2284 clear_refmod
|= VM_MEM_MODIFIED
;
2287 * This page is now clean and
2288 * reclaimable. Move it out
2289 * of the throttled queue, so
2290 * that vm_pageout_scan() can
2293 dwp
->dw_mask
|= DW_move_page
;
2296 vm_external_state_clr(object
->existence_map
, offset
);
2297 #endif /* MACH_PAGEMAP */
2299 if (reusable_page
&& !m
->reusable
) {
2300 assert(!all_reusable
);
2301 assert(!object
->all_reusable
);
2303 object
->reusable_page_count
++;
2304 assert(object
->resident_page_count
>= object
->reusable_page_count
);
2309 m
->reusable
= FALSE
;
2310 object
->reusable_page_count
--;
2315 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
2317 if (!m
->throttled
&& !(reusable_page
|| all_reusable
))
2318 dwp
->dw_mask
|= DW_move_page
;
2320 * dw_do_work may need to drop the object lock
2321 * if it does, we need the pages its looking at to
2322 * be held stable via the busy bit.
2325 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
2331 if (dw_count
>= DELAYED_WORK_LIMIT
) {
2333 OSAddAtomic(reusable
,
2334 &vm_page_stats_reusable
.reusable_count
);
2335 vm_page_stats_reusable
.reusable
+= reusable
;
2338 dw_do_work(object
, &dw_array
[0], dw_count
);
2348 * The page at this offset isn't memory resident, check to see if it's
2349 * been paged out. If so, mark it as handled so we don't bother looking
2350 * for it in the shadow chain.
2353 if (page_is_paged_out(object
, offset
)) {
2354 MARK_PAGE_HANDLED(*chunk_state
, p
);
2357 * If we're killing a non-resident page, then clear the page in the existence
2358 * map so we don't bother paging it back in if it's touched again in the future.
2361 if ((kill_page
) && (object
->internal
)) {
2363 vm_external_state_clr(object
->existence_map
, offset
);
2364 #endif /* MACH_PAGEMAP */
2371 OSAddAtomic(reusable
, &vm_page_stats_reusable
.reusable_count
);
2372 vm_page_stats_reusable
.reusable
+= reusable
;
2377 dw_do_work(object
, &dw_array
[0], dw_count
);
2382 * Deactive a "chunk" of the given range of the object starting at offset. A "chunk"
2383 * will always be less than or equal to the given size. The total range is divided up
2384 * into chunks for efficiency and performance related to the locks and handling the shadow
2385 * chain. This routine returns how much of the given "size" it actually processed. It's
2386 * up to the caler to loop and keep calling this routine until the entire range they want
2387 * to process has been done.
2390 static vm_object_size_t
2392 vm_object_t orig_object
,
2393 vm_object_offset_t offset
,
2394 vm_object_size_t size
,
2395 boolean_t kill_page
,
2396 boolean_t reusable_page
,
2397 boolean_t all_reusable
)
2400 vm_object_t tmp_object
;
2401 vm_object_size_t length
;
2402 chunk_state_t chunk_state
;
2406 * Get set to do a chunk. We'll do up to CHUNK_SIZE, but no more than the
2407 * remaining size the caller asked for.
2410 length
= MIN(size
, CHUNK_SIZE
);
2413 * The chunk_state keeps track of which pages we've already processed if there's
2414 * a shadow chain on this object. At this point, we haven't done anything with this
2415 * range of pages yet, so initialize the state to indicate no pages processed yet.
2418 CHUNK_INIT(chunk_state
, length
);
2419 object
= orig_object
;
2422 * Start at the top level object and iterate around the loop once for each object
2423 * in the shadow chain. We stop processing early if we've already found all the pages
2424 * in the range. Otherwise we stop when we run out of shadow objects.
2427 while (object
&& CHUNK_NOT_COMPLETE(chunk_state
)) {
2428 vm_object_paging_begin(object
);
2430 deactivate_pages_in_object(object
, offset
, length
, kill_page
, reusable_page
, all_reusable
, &chunk_state
);
2432 vm_object_paging_end(object
);
2435 * We've finished with this object, see if there's a shadow object. If
2436 * there is, update the offset and lock the new object. We also turn off
2437 * kill_page at this point since we only kill pages in the top most object.
2440 tmp_object
= object
->shadow
;
2444 reusable_page
= FALSE
;
2445 all_reusable
= FALSE
;
2446 offset
+= object
->shadow_offset
;
2447 vm_object_lock(tmp_object
);
2450 if (object
!= orig_object
)
2451 vm_object_unlock(object
);
2453 object
= tmp_object
;
2456 if (object
&& object
!= orig_object
)
2457 vm_object_unlock(object
);
2465 * Move any resident pages in the specified range to the inactive queue. If kill_page is set,
2466 * we also clear the modified status of the page and "forget" any changes that have been made
2470 __private_extern__
void
2471 vm_object_deactivate_pages(
2473 vm_object_offset_t offset
,
2474 vm_object_size_t size
,
2475 boolean_t kill_page
,
2476 boolean_t reusable_page
)
2478 vm_object_size_t length
;
2479 boolean_t all_reusable
;
2482 * We break the range up into chunks and do one chunk at a time. This is for
2483 * efficiency and performance while handling the shadow chains and the locks.
2484 * The deactivate_a_chunk() function returns how much of the range it processed.
2485 * We keep calling this routine until the given size is exhausted.
2489 all_reusable
= FALSE
;
2490 if (reusable_page
&&
2491 object
->size
!= 0 &&
2492 object
->size
== size
&&
2493 object
->reusable_page_count
== 0) {
2494 all_reusable
= TRUE
;
2495 reusable_page
= FALSE
;
2499 if ((reusable_page
|| all_reusable
) && object
->all_reusable
) {
2500 /* This means MADV_FREE_REUSABLE has been called twice, which
2501 * is probably illegal. */
2507 length
= deactivate_a_chunk(object
, offset
, size
, kill_page
, reusable_page
, all_reusable
);
2514 if (!object
->all_reusable
) {
2515 unsigned int reusable
;
2517 object
->all_reusable
= TRUE
;
2518 assert(object
->reusable_page_count
== 0);
2519 /* update global stats */
2520 reusable
= object
->resident_page_count
;
2521 OSAddAtomic(reusable
,
2522 &vm_page_stats_reusable
.reusable_count
);
2523 vm_page_stats_reusable
.reusable
+= reusable
;
2524 vm_page_stats_reusable
.all_reusable_calls
++;
2526 } else if (reusable_page
) {
2527 vm_page_stats_reusable
.partial_reusable_calls
++;
2532 vm_object_reuse_pages(
2534 vm_object_offset_t start_offset
,
2535 vm_object_offset_t end_offset
,
2536 boolean_t allow_partial_reuse
)
2538 vm_object_offset_t cur_offset
;
2540 unsigned int reused
, reusable
;
2542 #define VM_OBJECT_REUSE_PAGE(object, m, reused) \
2544 if ((m) != VM_PAGE_NULL && \
2546 assert((object)->reusable_page_count <= \
2547 (object)->resident_page_count); \
2548 assert((object)->reusable_page_count > 0); \
2549 (object)->reusable_page_count--; \
2550 (m)->reusable = FALSE; \
2558 vm_object_lock_assert_exclusive(object
);
2560 if (object
->all_reusable
) {
2561 assert(object
->reusable_page_count
== 0);
2562 object
->all_reusable
= FALSE
;
2563 if (end_offset
- start_offset
== object
->size
||
2564 !allow_partial_reuse
) {
2565 vm_page_stats_reusable
.all_reuse_calls
++;
2566 reused
= object
->resident_page_count
;
2568 vm_page_stats_reusable
.partial_reuse_calls
++;
2569 queue_iterate(&object
->memq
, m
, vm_page_t
, listq
) {
2570 if (m
->offset
< start_offset
||
2571 m
->offset
>= end_offset
) {
2573 object
->reusable_page_count
++;
2574 assert(object
->resident_page_count
>= object
->reusable_page_count
);
2577 assert(!m
->reusable
);
2582 } else if (object
->resident_page_count
>
2583 ((end_offset
- start_offset
) >> PAGE_SHIFT
)) {
2584 vm_page_stats_reusable
.partial_reuse_calls
++;
2585 for (cur_offset
= start_offset
;
2586 cur_offset
< end_offset
;
2587 cur_offset
+= PAGE_SIZE_64
) {
2588 if (object
->reusable_page_count
== 0) {
2591 m
= vm_page_lookup(object
, cur_offset
);
2592 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
2595 vm_page_stats_reusable
.partial_reuse_calls
++;
2596 queue_iterate(&object
->memq
, m
, vm_page_t
, listq
) {
2597 if (object
->reusable_page_count
== 0) {
2600 if (m
->offset
< start_offset
||
2601 m
->offset
>= end_offset
) {
2604 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
2608 /* update global stats */
2609 OSAddAtomic(reusable
-reused
, &vm_page_stats_reusable
.reusable_count
);
2610 vm_page_stats_reusable
.reused
+= reused
;
2611 vm_page_stats_reusable
.reusable
+= reusable
;
2615 * Routine: vm_object_pmap_protect
2618 * Reduces the permission for all physical
2619 * pages in the specified object range.
2621 * If removing write permission only, it is
2622 * sufficient to protect only the pages in
2623 * the top-level object; only those pages may
2624 * have write permission.
2626 * If removing all access, we must follow the
2627 * shadow chain from the top-level object to
2628 * remove access to all pages in shadowed objects.
2630 * The object must *not* be locked. The object must
2631 * be temporary/internal.
2633 * If pmap is not NULL, this routine assumes that
2634 * the only mappings for the pages are in that
2638 __private_extern__
void
2639 vm_object_pmap_protect(
2640 register vm_object_t object
,
2641 register vm_object_offset_t offset
,
2642 vm_object_size_t size
,
2644 vm_map_offset_t pmap_start
,
2647 if (object
== VM_OBJECT_NULL
)
2649 size
= vm_object_round_page(size
);
2650 offset
= vm_object_trunc_page(offset
);
2652 vm_object_lock(object
);
2654 if (object
->phys_contiguous
) {
2656 vm_object_unlock(object
);
2657 pmap_protect(pmap
, pmap_start
, pmap_start
+ size
, prot
);
2659 vm_object_offset_t phys_start
, phys_end
, phys_addr
;
2661 phys_start
= object
->shadow_offset
+ offset
;
2662 phys_end
= phys_start
+ size
;
2663 assert(phys_start
<= phys_end
);
2664 assert(phys_end
<= object
->shadow_offset
+ object
->size
);
2665 vm_object_unlock(object
);
2667 for (phys_addr
= phys_start
;
2668 phys_addr
< phys_end
;
2669 phys_addr
+= PAGE_SIZE_64
) {
2670 pmap_page_protect((ppnum_t
) (phys_addr
>> PAGE_SHIFT
), prot
);
2676 assert(object
->internal
);
2679 if (ptoa_64(object
->resident_page_count
) > size
/2 && pmap
!= PMAP_NULL
) {
2680 vm_object_unlock(object
);
2681 pmap_protect(pmap
, pmap_start
, pmap_start
+ size
, prot
);
2685 /* if we are doing large ranges with respect to resident */
2686 /* page count then we should interate over pages otherwise */
2687 /* inverse page look-up will be faster */
2688 if (ptoa_64(object
->resident_page_count
/ 4) < size
) {
2690 vm_object_offset_t end
;
2692 end
= offset
+ size
;
2694 if (pmap
!= PMAP_NULL
) {
2695 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2696 if (!p
->fictitious
&&
2697 (offset
<= p
->offset
) && (p
->offset
< end
)) {
2698 vm_map_offset_t start
;
2700 start
= pmap_start
+ p
->offset
- offset
;
2701 pmap_protect(pmap
, start
, start
+ PAGE_SIZE_64
, prot
);
2705 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2706 if (!p
->fictitious
&&
2707 (offset
<= p
->offset
) && (p
->offset
< end
)) {
2709 pmap_page_protect(p
->phys_page
, prot
);
2715 vm_object_offset_t end
;
2716 vm_object_offset_t target_off
;
2718 end
= offset
+ size
;
2720 if (pmap
!= PMAP_NULL
) {
2721 for(target_off
= offset
;
2723 target_off
+= PAGE_SIZE
) {
2724 p
= vm_page_lookup(object
, target_off
);
2725 if (p
!= VM_PAGE_NULL
) {
2726 vm_object_offset_t start
;
2727 start
= pmap_start
+
2728 (p
->offset
- offset
);
2729 pmap_protect(pmap
, start
,
2730 start
+ PAGE_SIZE
, prot
);
2734 for(target_off
= offset
;
2735 target_off
< end
; target_off
+= PAGE_SIZE
) {
2736 p
= vm_page_lookup(object
, target_off
);
2737 if (p
!= VM_PAGE_NULL
) {
2738 pmap_page_protect(p
->phys_page
, prot
);
2744 if (prot
== VM_PROT_NONE
) {
2746 * Must follow shadow chain to remove access
2747 * to pages in shadowed objects.
2749 register vm_object_t next_object
;
2751 next_object
= object
->shadow
;
2752 if (next_object
!= VM_OBJECT_NULL
) {
2753 offset
+= object
->shadow_offset
;
2754 vm_object_lock(next_object
);
2755 vm_object_unlock(object
);
2756 object
= next_object
;
2760 * End of chain - we are done.
2767 * Pages in shadowed objects may never have
2768 * write permission - we may stop here.
2774 vm_object_unlock(object
);
2778 * Routine: vm_object_copy_slowly
2781 * Copy the specified range of the source
2782 * virtual memory object without using
2783 * protection-based optimizations (such
2784 * as copy-on-write). The pages in the
2785 * region are actually copied.
2787 * In/out conditions:
2788 * The caller must hold a reference and a lock
2789 * for the source virtual memory object. The source
2790 * object will be returned *unlocked*.
2793 * If the copy is completed successfully, KERN_SUCCESS is
2794 * returned. If the caller asserted the interruptible
2795 * argument, and an interruption occurred while waiting
2796 * for a user-generated event, MACH_SEND_INTERRUPTED is
2797 * returned. Other values may be returned to indicate
2798 * hard errors during the copy operation.
2800 * A new virtual memory object is returned in a
2801 * parameter (_result_object). The contents of this
2802 * new object, starting at a zero offset, are a copy
2803 * of the source memory region. In the event of
2804 * an error, this parameter will contain the value
2807 __private_extern__ kern_return_t
2808 vm_object_copy_slowly(
2809 register vm_object_t src_object
,
2810 vm_object_offset_t src_offset
,
2811 vm_object_size_t size
,
2812 boolean_t interruptible
,
2813 vm_object_t
*_result_object
) /* OUT */
2815 vm_object_t new_object
;
2816 vm_object_offset_t new_offset
;
2818 struct vm_object_fault_info fault_info
;
2820 XPR(XPR_VM_OBJECT
, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
2821 src_object
, src_offset
, size
, 0, 0);
2824 vm_object_unlock(src_object
);
2825 *_result_object
= VM_OBJECT_NULL
;
2826 return(KERN_INVALID_ARGUMENT
);
2830 * Prevent destruction of the source object while we copy.
2833 vm_object_reference_locked(src_object
);
2834 vm_object_unlock(src_object
);
2837 * Create a new object to hold the copied pages.
2839 * We fill the new object starting at offset 0,
2840 * regardless of the input offset.
2841 * We don't bother to lock the new object within
2842 * this routine, since we have the only reference.
2845 new_object
= vm_object_allocate(size
);
2848 assert(size
== trunc_page_64(size
)); /* Will the loop terminate? */
2850 fault_info
.interruptible
= interruptible
;
2851 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
2852 fault_info
.user_tag
= 0;
2853 fault_info
.lo_offset
= src_offset
;
2854 fault_info
.hi_offset
= src_offset
+ size
;
2855 fault_info
.no_cache
= FALSE
;
2856 fault_info
.stealth
= TRUE
;
2860 src_offset
+= PAGE_SIZE_64
,
2861 new_offset
+= PAGE_SIZE_64
, size
-= PAGE_SIZE_64
2864 vm_fault_return_t result
;
2866 vm_object_lock(new_object
);
2868 while ((new_page
= vm_page_alloc(new_object
, new_offset
))
2871 vm_object_unlock(new_object
);
2873 if (!vm_page_wait(interruptible
)) {
2874 vm_object_deallocate(new_object
);
2875 vm_object_deallocate(src_object
);
2876 *_result_object
= VM_OBJECT_NULL
;
2877 return(MACH_SEND_INTERRUPTED
);
2879 vm_object_lock(new_object
);
2881 vm_object_unlock(new_object
);
2884 vm_prot_t prot
= VM_PROT_READ
;
2885 vm_page_t _result_page
;
2888 vm_page_t result_page
;
2889 kern_return_t error_code
;
2891 vm_object_lock(src_object
);
2892 vm_object_paging_begin(src_object
);
2894 if (size
> (vm_size_t
) -1) {
2895 /* 32-bit overflow */
2896 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
2898 fault_info
.cluster_size
= (vm_size_t
) size
;
2899 assert(fault_info
.cluster_size
== size
);
2902 XPR(XPR_VM_FAULT
,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
2903 result
= vm_fault_page(src_object
, src_offset
,
2904 VM_PROT_READ
, FALSE
,
2905 &prot
, &_result_page
, &top_page
,
2907 &error_code
, FALSE
, FALSE
, &fault_info
);
2910 case VM_FAULT_SUCCESS
:
2911 result_page
= _result_page
;
2914 * We don't need to hold the object
2915 * lock -- the busy page will be enough.
2916 * [We don't care about picking up any
2917 * new modifications.]
2919 * Copy the page to the new object.
2922 * If result_page is clean,
2923 * we could steal it instead
2927 vm_object_unlock(result_page
->object
);
2928 vm_page_copy(result_page
, new_page
);
2931 * Let go of both pages (make them
2932 * not busy, perform wakeup, activate).
2934 vm_object_lock(new_object
);
2935 new_page
->dirty
= TRUE
;
2936 PAGE_WAKEUP_DONE(new_page
);
2937 vm_object_unlock(new_object
);
2939 vm_object_lock(result_page
->object
);
2940 PAGE_WAKEUP_DONE(result_page
);
2942 vm_page_lockspin_queues();
2943 if (!result_page
->active
&&
2944 !result_page
->inactive
&&
2945 !result_page
->throttled
)
2946 vm_page_activate(result_page
);
2947 vm_page_activate(new_page
);
2948 vm_page_unlock_queues();
2951 * Release paging references and
2952 * top-level placeholder page, if any.
2955 vm_fault_cleanup(result_page
->object
,
2960 case VM_FAULT_RETRY
:
2963 case VM_FAULT_FICTITIOUS_SHORTAGE
:
2964 vm_page_more_fictitious();
2967 case VM_FAULT_MEMORY_SHORTAGE
:
2968 if (vm_page_wait(interruptible
))
2972 case VM_FAULT_INTERRUPTED
:
2973 vm_object_lock(new_object
);
2974 VM_PAGE_FREE(new_page
);
2975 vm_object_unlock(new_object
);
2977 vm_object_deallocate(new_object
);
2978 vm_object_deallocate(src_object
);
2979 *_result_object
= VM_OBJECT_NULL
;
2980 return(MACH_SEND_INTERRUPTED
);
2982 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
2983 /* success but no VM page: fail */
2984 vm_object_paging_end(src_object
);
2985 vm_object_unlock(src_object
);
2987 case VM_FAULT_MEMORY_ERROR
:
2990 * (a) ignore pages that we can't
2992 * (b) return the null object if
2993 * any page fails [chosen]
2996 vm_object_lock(new_object
);
2997 VM_PAGE_FREE(new_page
);
2998 vm_object_unlock(new_object
);
3000 vm_object_deallocate(new_object
);
3001 vm_object_deallocate(src_object
);
3002 *_result_object
= VM_OBJECT_NULL
;
3003 return(error_code
? error_code
:
3007 panic("vm_object_copy_slowly: unexpected error"
3008 " 0x%x from vm_fault_page()\n", result
);
3010 } while (result
!= VM_FAULT_SUCCESS
);
3014 * Lose the extra reference, and return our object.
3016 vm_object_deallocate(src_object
);
3017 *_result_object
= new_object
;
3018 return(KERN_SUCCESS
);
3022 * Routine: vm_object_copy_quickly
3025 * Copy the specified range of the source virtual
3026 * memory object, if it can be done without waiting
3027 * for user-generated events.
3030 * If the copy is successful, the copy is returned in
3031 * the arguments; otherwise, the arguments are not
3034 * In/out conditions:
3035 * The object should be unlocked on entry and exit.
3039 __private_extern__ boolean_t
3040 vm_object_copy_quickly(
3041 vm_object_t
*_object
, /* INOUT */
3042 __unused vm_object_offset_t offset
, /* IN */
3043 __unused vm_object_size_t size
, /* IN */
3044 boolean_t
*_src_needs_copy
, /* OUT */
3045 boolean_t
*_dst_needs_copy
) /* OUT */
3047 vm_object_t object
= *_object
;
3048 memory_object_copy_strategy_t copy_strategy
;
3050 XPR(XPR_VM_OBJECT
, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
3051 *_object
, offset
, size
, 0, 0);
3052 if (object
== VM_OBJECT_NULL
) {
3053 *_src_needs_copy
= FALSE
;
3054 *_dst_needs_copy
= FALSE
;
3058 vm_object_lock(object
);
3060 copy_strategy
= object
->copy_strategy
;
3062 switch (copy_strategy
) {
3063 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3066 * Symmetric copy strategy.
3067 * Make another reference to the object.
3068 * Leave object/offset unchanged.
3071 vm_object_reference_locked(object
);
3072 object
->shadowed
= TRUE
;
3073 vm_object_unlock(object
);
3076 * Both source and destination must make
3077 * shadows, and the source must be made
3078 * read-only if not already.
3081 *_src_needs_copy
= TRUE
;
3082 *_dst_needs_copy
= TRUE
;
3086 case MEMORY_OBJECT_COPY_DELAY
:
3087 vm_object_unlock(object
);
3091 vm_object_unlock(object
);
3097 static int copy_call_count
= 0;
3098 static int copy_call_sleep_count
= 0;
3099 static int copy_call_restart_count
= 0;
3102 * Routine: vm_object_copy_call [internal]
3105 * Copy the source object (src_object), using the
3106 * user-managed copy algorithm.
3108 * In/out conditions:
3109 * The source object must be locked on entry. It
3110 * will be *unlocked* on exit.
3113 * If the copy is successful, KERN_SUCCESS is returned.
3114 * A new object that represents the copied virtual
3115 * memory is returned in a parameter (*_result_object).
3116 * If the return value indicates an error, this parameter
3119 static kern_return_t
3120 vm_object_copy_call(
3121 vm_object_t src_object
,
3122 vm_object_offset_t src_offset
,
3123 vm_object_size_t size
,
3124 vm_object_t
*_result_object
) /* OUT */
3128 boolean_t check_ready
= FALSE
;
3129 uint32_t try_failed_count
= 0;
3132 * If a copy is already in progress, wait and retry.
3135 * Consider making this call interruptable, as Mike
3136 * intended it to be.
3139 * Need a counter or version or something to allow
3140 * us to use the copy that the currently requesting
3141 * thread is obtaining -- is it worth adding to the
3142 * vm object structure? Depends how common this case it.
3145 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3146 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3148 copy_call_restart_count
++;
3152 * Indicate (for the benefit of memory_object_create_copy)
3153 * that we want a copy for src_object. (Note that we cannot
3154 * do a real assert_wait before calling memory_object_copy,
3155 * so we simply set the flag.)
3158 vm_object_set_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
);
3159 vm_object_unlock(src_object
);
3162 * Ask the memory manager to give us a memory object
3163 * which represents a copy of the src object.
3164 * The memory manager may give us a memory object
3165 * which we already have, or it may give us a
3166 * new memory object. This memory object will arrive
3167 * via memory_object_create_copy.
3170 kr
= KERN_FAILURE
; /* XXX need to change memory_object.defs */
3171 if (kr
!= KERN_SUCCESS
) {
3176 * Wait for the copy to arrive.
3178 vm_object_lock(src_object
);
3179 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3180 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3182 copy_call_sleep_count
++;
3185 assert(src_object
->copy
!= VM_OBJECT_NULL
);
3186 copy
= src_object
->copy
;
3187 if (!vm_object_lock_try(copy
)) {
3188 vm_object_unlock(src_object
);
3191 mutex_pause(try_failed_count
); /* wait a bit */
3193 vm_object_lock(src_object
);
3196 if (copy
->size
< src_offset
+size
)
3197 copy
->size
= src_offset
+size
;
3199 if (!copy
->pager_ready
)
3205 *_result_object
= copy
;
3206 vm_object_unlock(copy
);
3207 vm_object_unlock(src_object
);
3209 /* Wait for the copy to be ready. */
3210 if (check_ready
== TRUE
) {
3211 vm_object_lock(copy
);
3212 while (!copy
->pager_ready
) {
3213 vm_object_sleep(copy
, VM_OBJECT_EVENT_PAGER_READY
, THREAD_UNINT
);
3215 vm_object_unlock(copy
);
3218 return KERN_SUCCESS
;
3221 static int copy_delayed_lock_collisions
= 0;
3222 static int copy_delayed_max_collisions
= 0;
3223 static int copy_delayed_lock_contention
= 0;
3224 static int copy_delayed_protect_iterate
= 0;
3227 * Routine: vm_object_copy_delayed [internal]
3230 * Copy the specified virtual memory object, using
3231 * the asymmetric copy-on-write algorithm.
3233 * In/out conditions:
3234 * The src_object must be locked on entry. It will be unlocked
3235 * on exit - so the caller must also hold a reference to it.
3237 * This routine will not block waiting for user-generated
3238 * events. It is not interruptible.
3240 __private_extern__ vm_object_t
3241 vm_object_copy_delayed(
3242 vm_object_t src_object
,
3243 vm_object_offset_t src_offset
,
3244 vm_object_size_t size
,
3245 boolean_t src_object_shared
)
3247 vm_object_t new_copy
= VM_OBJECT_NULL
;
3248 vm_object_t old_copy
;
3250 vm_object_size_t copy_size
= src_offset
+ size
;
3255 * The user-level memory manager wants to see all of the changes
3256 * to this object, but it has promised not to make any changes on
3259 * Perform an asymmetric copy-on-write, as follows:
3260 * Create a new object, called a "copy object" to hold
3261 * pages modified by the new mapping (i.e., the copy,
3262 * not the original mapping).
3263 * Record the original object as the backing object for
3264 * the copy object. If the original mapping does not
3265 * change a page, it may be used read-only by the copy.
3266 * Record the copy object in the original object.
3267 * When the original mapping causes a page to be modified,
3268 * it must be copied to a new page that is "pushed" to
3270 * Mark the new mapping (the copy object) copy-on-write.
3271 * This makes the copy object itself read-only, allowing
3272 * it to be reused if the original mapping makes no
3273 * changes, and simplifying the synchronization required
3274 * in the "push" operation described above.
3276 * The copy-on-write is said to be assymetric because the original
3277 * object is *not* marked copy-on-write. A copied page is pushed
3278 * to the copy object, regardless which party attempted to modify
3281 * Repeated asymmetric copy operations may be done. If the
3282 * original object has not been changed since the last copy, its
3283 * copy object can be reused. Otherwise, a new copy object can be
3284 * inserted between the original object and its previous copy
3285 * object. Since any copy object is read-only, this cannot affect
3286 * affect the contents of the previous copy object.
3288 * Note that a copy object is higher in the object tree than the
3289 * original object; therefore, use of the copy object recorded in
3290 * the original object must be done carefully, to avoid deadlock.
3296 * Wait for paging in progress.
3298 if (!src_object
->true_share
&&
3299 (src_object
->paging_in_progress
!= 0 ||
3300 src_object
->activity_in_progress
!= 0)) {
3301 if (src_object_shared
== TRUE
) {
3302 vm_object_unlock(src_object
);
3303 vm_object_lock(src_object
);
3304 src_object_shared
= FALSE
;
3307 vm_object_paging_wait(src_object
, THREAD_UNINT
);
3310 * See whether we can reuse the result of a previous
3314 old_copy
= src_object
->copy
;
3315 if (old_copy
!= VM_OBJECT_NULL
) {
3319 * Try to get the locks (out of order)
3321 if (src_object_shared
== TRUE
)
3322 lock_granted
= vm_object_lock_try_shared(old_copy
);
3324 lock_granted
= vm_object_lock_try(old_copy
);
3326 if (!lock_granted
) {
3327 vm_object_unlock(src_object
);
3329 if (collisions
++ == 0)
3330 copy_delayed_lock_contention
++;
3331 mutex_pause(collisions
);
3333 /* Heisenberg Rules */
3334 copy_delayed_lock_collisions
++;
3336 if (collisions
> copy_delayed_max_collisions
)
3337 copy_delayed_max_collisions
= collisions
;
3339 if (src_object_shared
== TRUE
)
3340 vm_object_lock_shared(src_object
);
3342 vm_object_lock(src_object
);
3348 * Determine whether the old copy object has
3352 if (old_copy
->resident_page_count
== 0 &&
3353 !old_copy
->pager_created
) {
3355 * It has not been modified.
3357 * Return another reference to
3358 * the existing copy-object if
3359 * we can safely grow it (if
3363 if (old_copy
->size
< copy_size
) {
3364 if (src_object_shared
== TRUE
) {
3365 vm_object_unlock(old_copy
);
3366 vm_object_unlock(src_object
);
3368 vm_object_lock(src_object
);
3369 src_object_shared
= FALSE
;
3373 * We can't perform a delayed copy if any of the
3374 * pages in the extended range are wired (because
3375 * we can't safely take write permission away from
3376 * wired pages). If the pages aren't wired, then
3377 * go ahead and protect them.
3379 copy_delayed_protect_iterate
++;
3381 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
3382 if (!p
->fictitious
&&
3383 p
->offset
>= old_copy
->size
&&
3384 p
->offset
< copy_size
) {
3385 if (VM_PAGE_WIRED(p
)) {
3386 vm_object_unlock(old_copy
);
3387 vm_object_unlock(src_object
);
3389 if (new_copy
!= VM_OBJECT_NULL
) {
3390 vm_object_unlock(new_copy
);
3391 vm_object_deallocate(new_copy
);
3394 return VM_OBJECT_NULL
;
3396 pmap_page_protect(p
->phys_page
,
3397 (VM_PROT_ALL
& ~VM_PROT_WRITE
));
3401 old_copy
->size
= copy_size
;
3403 if (src_object_shared
== TRUE
)
3404 vm_object_reference_shared(old_copy
);
3406 vm_object_reference_locked(old_copy
);
3407 vm_object_unlock(old_copy
);
3408 vm_object_unlock(src_object
);
3410 if (new_copy
!= VM_OBJECT_NULL
) {
3411 vm_object_unlock(new_copy
);
3412 vm_object_deallocate(new_copy
);
3420 * Adjust the size argument so that the newly-created
3421 * copy object will be large enough to back either the
3422 * old copy object or the new mapping.
3424 if (old_copy
->size
> copy_size
)
3425 copy_size
= old_copy
->size
;
3427 if (new_copy
== VM_OBJECT_NULL
) {
3428 vm_object_unlock(old_copy
);
3429 vm_object_unlock(src_object
);
3430 new_copy
= vm_object_allocate(copy_size
);
3431 vm_object_lock(src_object
);
3432 vm_object_lock(new_copy
);
3434 src_object_shared
= FALSE
;
3437 new_copy
->size
= copy_size
;
3440 * The copy-object is always made large enough to
3441 * completely shadow the original object, since
3442 * it may have several users who want to shadow
3443 * the original object at different points.
3446 assert((old_copy
->shadow
== src_object
) &&
3447 (old_copy
->shadow_offset
== (vm_object_offset_t
) 0));
3449 } else if (new_copy
== VM_OBJECT_NULL
) {
3450 vm_object_unlock(src_object
);
3451 new_copy
= vm_object_allocate(copy_size
);
3452 vm_object_lock(src_object
);
3453 vm_object_lock(new_copy
);
3455 src_object_shared
= FALSE
;
3460 * We now have the src object locked, and the new copy object
3461 * allocated and locked (and potentially the old copy locked).
3462 * Before we go any further, make sure we can still perform
3463 * a delayed copy, as the situation may have changed.
3465 * Specifically, we can't perform a delayed copy if any of the
3466 * pages in the range are wired (because we can't safely take
3467 * write permission away from wired pages). If the pages aren't
3468 * wired, then go ahead and protect them.
3470 copy_delayed_protect_iterate
++;
3472 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
3473 if (!p
->fictitious
&& p
->offset
< copy_size
) {
3474 if (VM_PAGE_WIRED(p
)) {
3476 vm_object_unlock(old_copy
);
3477 vm_object_unlock(src_object
);
3478 vm_object_unlock(new_copy
);
3479 vm_object_deallocate(new_copy
);
3480 return VM_OBJECT_NULL
;
3482 pmap_page_protect(p
->phys_page
,
3483 (VM_PROT_ALL
& ~VM_PROT_WRITE
));
3487 if (old_copy
!= VM_OBJECT_NULL
) {
3489 * Make the old copy-object shadow the new one.
3490 * It will receive no more pages from the original
3494 /* remove ref. from old_copy */
3495 vm_object_lock_assert_exclusive(src_object
);
3496 src_object
->ref_count
--;
3497 assert(src_object
->ref_count
> 0);
3498 vm_object_lock_assert_exclusive(old_copy
);
3499 old_copy
->shadow
= new_copy
;
3500 vm_object_lock_assert_exclusive(new_copy
);
3501 assert(new_copy
->ref_count
> 0);
3502 new_copy
->ref_count
++; /* for old_copy->shadow ref. */
3505 if (old_copy
->res_count
) {
3506 VM_OBJ_RES_INCR(new_copy
);
3507 VM_OBJ_RES_DECR(src_object
);
3511 vm_object_unlock(old_copy
); /* done with old_copy */
3515 * Point the new copy at the existing object.
3517 vm_object_lock_assert_exclusive(new_copy
);
3518 new_copy
->shadow
= src_object
;
3519 new_copy
->shadow_offset
= 0;
3520 new_copy
->shadowed
= TRUE
; /* caller must set needs_copy */
3522 vm_object_lock_assert_exclusive(src_object
);
3523 vm_object_reference_locked(src_object
);
3524 src_object
->copy
= new_copy
;
3525 vm_object_unlock(src_object
);
3526 vm_object_unlock(new_copy
);
3529 "vm_object_copy_delayed: used copy object %X for source %X\n",
3530 new_copy
, src_object
, 0, 0, 0);
3536 * Routine: vm_object_copy_strategically
3539 * Perform a copy according to the source object's
3540 * declared strategy. This operation may block,
3541 * and may be interrupted.
3543 __private_extern__ kern_return_t
3544 vm_object_copy_strategically(
3545 register vm_object_t src_object
,
3546 vm_object_offset_t src_offset
,
3547 vm_object_size_t size
,
3548 vm_object_t
*dst_object
, /* OUT */
3549 vm_object_offset_t
*dst_offset
, /* OUT */
3550 boolean_t
*dst_needs_copy
) /* OUT */
3553 boolean_t interruptible
= THREAD_ABORTSAFE
; /* XXX */
3554 boolean_t object_lock_shared
= FALSE
;
3555 memory_object_copy_strategy_t copy_strategy
;
3557 assert(src_object
!= VM_OBJECT_NULL
);
3559 copy_strategy
= src_object
->copy_strategy
;
3561 if (copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
3562 vm_object_lock_shared(src_object
);
3563 object_lock_shared
= TRUE
;
3565 vm_object_lock(src_object
);
3568 * The copy strategy is only valid if the memory manager
3569 * is "ready". Internal objects are always ready.
3572 while (!src_object
->internal
&& !src_object
->pager_ready
) {
3573 wait_result_t wait_result
;
3575 if (object_lock_shared
== TRUE
) {
3576 vm_object_unlock(src_object
);
3577 vm_object_lock(src_object
);
3578 object_lock_shared
= FALSE
;
3581 wait_result
= vm_object_sleep( src_object
,
3582 VM_OBJECT_EVENT_PAGER_READY
,
3584 if (wait_result
!= THREAD_AWAKENED
) {
3585 vm_object_unlock(src_object
);
3586 *dst_object
= VM_OBJECT_NULL
;
3588 *dst_needs_copy
= FALSE
;
3589 return(MACH_SEND_INTERRUPTED
);
3594 * Use the appropriate copy strategy.
3597 switch (copy_strategy
) {
3598 case MEMORY_OBJECT_COPY_DELAY
:
3599 *dst_object
= vm_object_copy_delayed(src_object
,
3600 src_offset
, size
, object_lock_shared
);
3601 if (*dst_object
!= VM_OBJECT_NULL
) {
3602 *dst_offset
= src_offset
;
3603 *dst_needs_copy
= TRUE
;
3604 result
= KERN_SUCCESS
;
3607 vm_object_lock(src_object
);
3608 /* fall thru when delayed copy not allowed */
3610 case MEMORY_OBJECT_COPY_NONE
:
3611 result
= vm_object_copy_slowly(src_object
, src_offset
, size
,
3612 interruptible
, dst_object
);
3613 if (result
== KERN_SUCCESS
) {
3615 *dst_needs_copy
= FALSE
;
3619 case MEMORY_OBJECT_COPY_CALL
:
3620 result
= vm_object_copy_call(src_object
, src_offset
, size
,
3622 if (result
== KERN_SUCCESS
) {
3623 *dst_offset
= src_offset
;
3624 *dst_needs_copy
= TRUE
;
3628 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3629 XPR(XPR_VM_OBJECT
, "v_o_c_strategically obj 0x%x off 0x%x size 0x%x\n", src_object
, src_offset
, size
, 0, 0);
3630 vm_object_unlock(src_object
);
3631 result
= KERN_MEMORY_RESTART_COPY
;
3635 panic("copy_strategically: bad strategy");
3636 result
= KERN_INVALID_ARGUMENT
;
3644 * Create a new object which is backed by the
3645 * specified existing object range. The source
3646 * object reference is deallocated.
3648 * The new object and offset into that object
3649 * are returned in the source parameters.
3651 boolean_t vm_object_shadow_check
= FALSE
;
3653 __private_extern__ boolean_t
3655 vm_object_t
*object
, /* IN/OUT */
3656 vm_object_offset_t
*offset
, /* IN/OUT */
3657 vm_object_size_t length
)
3659 register vm_object_t source
;
3660 register vm_object_t result
;
3666 * This assertion is valid but it gets triggered by Rosetta for example
3667 * due to a combination of vm_remap() that changes a VM object's
3668 * copy_strategy from SYMMETRIC to DELAY and vm_protect(VM_PROT_COPY)
3669 * that then sets "needs_copy" on its map entry. This creates a
3670 * mapping situation that VM should never see and doesn't know how to
3672 * It's not clear if this can create any real problem but we should
3673 * look into fixing this, probably by having vm_protect(VM_PROT_COPY)
3674 * do more than just set "needs_copy" to handle the copy-on-write...
3675 * In the meantime, let's disable the assertion.
3677 assert(source
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
);
3681 * Determine if we really need a shadow.
3684 if (vm_object_shadow_check
&& source
->ref_count
== 1 &&
3685 (source
->shadow
== VM_OBJECT_NULL
||
3686 source
->shadow
->copy
== VM_OBJECT_NULL
))
3688 source
->shadowed
= FALSE
;
3693 * Allocate a new object with the given length
3696 if ((result
= vm_object_allocate(length
)) == VM_OBJECT_NULL
)
3697 panic("vm_object_shadow: no object for shadowing");
3700 * The new object shadows the source object, adding
3701 * a reference to it. Our caller changes his reference
3702 * to point to the new object, removing a reference to
3703 * the source object. Net result: no change of reference
3706 result
->shadow
= source
;
3709 * Store the offset into the source object,
3710 * and fix up the offset into the new object.
3713 result
->shadow_offset
= *offset
;
3716 * Return the new things
3725 * The relationship between vm_object structures and
3726 * the memory_object requires careful synchronization.
3728 * All associations are created by memory_object_create_named
3729 * for external pagers and vm_object_pager_create for internal
3730 * objects as follows:
3732 * pager: the memory_object itself, supplied by
3733 * the user requesting a mapping (or the kernel,
3734 * when initializing internal objects); the
3735 * kernel simulates holding send rights by keeping
3739 * the memory object control port,
3740 * created by the kernel; the kernel holds
3741 * receive (and ownership) rights to this
3742 * port, but no other references.
3744 * When initialization is complete, the "initialized" field
3745 * is asserted. Other mappings using a particular memory object,
3746 * and any references to the vm_object gained through the
3747 * port association must wait for this initialization to occur.
3749 * In order to allow the memory manager to set attributes before
3750 * requests (notably virtual copy operations, but also data or
3751 * unlock requests) are made, a "ready" attribute is made available.
3752 * Only the memory manager may affect the value of this attribute.
3753 * Its value does not affect critical kernel functions, such as
3754 * internal object initialization or destruction. [Furthermore,
3755 * memory objects created by the kernel are assumed to be ready
3756 * immediately; the default memory manager need not explicitly
3757 * set the "ready" attribute.]
3759 * [Both the "initialized" and "ready" attribute wait conditions
3760 * use the "pager" field as the wait event.]
3762 * The port associations can be broken down by any of the
3763 * following routines:
3764 * vm_object_terminate:
3765 * No references to the vm_object remain, and
3766 * the object cannot (or will not) be cached.
3767 * This is the normal case, and is done even
3768 * though one of the other cases has already been
3770 * memory_object_destroy:
3771 * The memory manager has requested that the
3772 * kernel relinquish references to the memory
3773 * object. [The memory manager may not want to
3774 * destroy the memory object, but may wish to
3775 * refuse or tear down existing memory mappings.]
3777 * Each routine that breaks an association must break all of
3778 * them at once. At some later time, that routine must clear
3779 * the pager field and release the memory object references.
3780 * [Furthermore, each routine must cope with the simultaneous
3781 * or previous operations of the others.]
3783 * In addition to the lock on the object, the vm_object_hash_lock
3784 * governs the associations. References gained through the
3785 * association require use of the hash lock.
3787 * Because the pager field may be cleared spontaneously, it
3788 * cannot be used to determine whether a memory object has
3789 * ever been associated with a particular vm_object. [This
3790 * knowledge is important to the shadow object mechanism.]
3791 * For this reason, an additional "created" attribute is
3794 * During various paging operations, the pager reference found in the
3795 * vm_object must be valid. To prevent this from being released,
3796 * (other than being removed, i.e., made null), routines may use
3797 * the vm_object_paging_begin/end routines [actually, macros].
3798 * The implementation uses the "paging_in_progress" and "wanted" fields.
3799 * [Operations that alter the validity of the pager values include the
3800 * termination routines and vm_object_collapse.]
3805 * Routine: vm_object_enter
3807 * Find a VM object corresponding to the given
3808 * pager; if no such object exists, create one,
3809 * and initialize the pager.
3813 memory_object_t pager
,
3814 vm_object_size_t size
,
3819 register vm_object_t object
;
3820 vm_object_t new_object
;
3821 boolean_t must_init
;
3822 vm_object_hash_entry_t entry
, new_entry
;
3823 uint32_t try_failed_count
= 0;
3826 if (pager
== MEMORY_OBJECT_NULL
)
3827 return(vm_object_allocate(size
));
3829 new_object
= VM_OBJECT_NULL
;
3830 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
3834 * Look for an object associated with this port.
3837 lck
= vm_object_hash_lock_spin(pager
);
3839 entry
= vm_object_hash_lookup(pager
, FALSE
);
3841 if (entry
== VM_OBJECT_HASH_ENTRY_NULL
) {
3842 if (new_object
== VM_OBJECT_NULL
) {
3844 * We must unlock to create a new object;
3845 * if we do so, we must try the lookup again.
3847 vm_object_hash_unlock(lck
);
3848 assert(new_entry
== VM_OBJECT_HASH_ENTRY_NULL
);
3849 new_entry
= vm_object_hash_entry_alloc(pager
);
3850 new_object
= vm_object_allocate(size
);
3851 lck
= vm_object_hash_lock_spin(pager
);
3854 * Lookup failed twice, and we have something
3855 * to insert; set the object.
3857 vm_object_hash_insert(new_entry
, new_object
);
3859 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
3860 new_object
= VM_OBJECT_NULL
;
3863 } else if (entry
->object
== VM_OBJECT_NULL
) {
3865 * If a previous object is being terminated,
3866 * we must wait for the termination message
3867 * to be queued (and lookup the entry again).
3869 entry
->waiting
= TRUE
;
3870 entry
= VM_OBJECT_HASH_ENTRY_NULL
;
3871 assert_wait((event_t
) pager
, THREAD_UNINT
);
3872 vm_object_hash_unlock(lck
);
3874 thread_block(THREAD_CONTINUE_NULL
);
3875 lck
= vm_object_hash_lock_spin(pager
);
3877 } while (entry
== VM_OBJECT_HASH_ENTRY_NULL
);
3879 object
= entry
->object
;
3880 assert(object
!= VM_OBJECT_NULL
);
3883 if ( !vm_object_lock_try(object
)) {
3885 vm_object_hash_unlock(lck
);
3888 mutex_pause(try_failed_count
); /* wait a bit */
3891 assert(!internal
|| object
->internal
);
3893 if (object
->ref_count
== 0) {
3894 if ( !vm_object_cache_lock_try()) {
3896 vm_object_hash_unlock(lck
);
3897 vm_object_unlock(object
);
3900 mutex_pause(try_failed_count
); /* wait a bit */
3903 XPR(XPR_VM_OBJECT_CACHE
,
3904 "vm_object_enter: removing %x from cache, head (%x, %x)\n",
3906 vm_object_cached_list
.next
,
3907 vm_object_cached_list
.prev
, 0,0);
3908 queue_remove(&vm_object_cached_list
, object
,
3909 vm_object_t
, cached_list
);
3910 vm_object_cached_count
--;
3912 vm_object_cache_unlock();
3916 assert(!object
->named
);
3917 object
->named
= TRUE
;
3919 vm_object_lock_assert_exclusive(object
);
3920 object
->ref_count
++;
3921 vm_object_res_reference(object
);
3923 vm_object_hash_unlock(lck
);
3924 vm_object_unlock(object
);
3928 vm_object_hash_unlock(lck
);
3930 assert(object
->ref_count
> 0);
3932 VM_STAT_INCR(lookups
);
3935 "vm_o_enter: pager 0x%x obj 0x%x must_init %d\n",
3936 pager
, object
, must_init
, 0, 0);
3939 * If we raced to create a vm_object but lost, let's
3943 if (new_object
!= VM_OBJECT_NULL
)
3944 vm_object_deallocate(new_object
);
3946 if (new_entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
3947 vm_object_hash_entry_free(new_entry
);
3950 memory_object_control_t control
;
3953 * Allocate request port.
3956 control
= memory_object_control_allocate(object
);
3957 assert (control
!= MEMORY_OBJECT_CONTROL_NULL
);
3959 vm_object_lock(object
);
3960 assert(object
!= kernel_object
);
3963 * Copy the reference we were given.
3966 memory_object_reference(pager
);
3967 object
->pager_created
= TRUE
;
3968 object
->pager
= pager
;
3969 object
->internal
= internal
;
3970 object
->pager_trusted
= internal
;
3972 /* copy strategy invalid until set by memory manager */
3973 object
->copy_strategy
= MEMORY_OBJECT_COPY_INVALID
;
3975 object
->pager_control
= control
;
3976 object
->pager_ready
= FALSE
;
3978 vm_object_unlock(object
);
3981 * Let the pager know we're using it.
3984 (void) memory_object_init(pager
,
3985 object
->pager_control
,
3988 vm_object_lock(object
);
3990 object
->named
= TRUE
;
3992 object
->pager_ready
= TRUE
;
3993 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
3996 object
->pager_initialized
= TRUE
;
3997 vm_object_wakeup(object
, VM_OBJECT_EVENT_INITIALIZED
);
3999 vm_object_lock(object
);
4003 * [At this point, the object must be locked]
4007 * Wait for the work above to be done by the first
4008 * thread to map this object.
4011 while (!object
->pager_initialized
) {
4012 vm_object_sleep(object
,
4013 VM_OBJECT_EVENT_INITIALIZED
,
4016 vm_object_unlock(object
);
4019 "vm_object_enter: vm_object %x, memory_object %x, internal %d\n",
4020 object
, object
->pager
, internal
, 0,0);
4025 * Routine: vm_object_pager_create
4027 * Create a memory object for an internal object.
4028 * In/out conditions:
4029 * The object is locked on entry and exit;
4030 * it may be unlocked within this call.
4032 * Only one thread may be performing a
4033 * vm_object_pager_create on an object at
4034 * a time. Presumably, only the pageout
4035 * daemon will be using this routine.
4039 vm_object_pager_create(
4040 register vm_object_t object
)
4042 memory_object_t pager
;
4043 vm_object_hash_entry_t entry
;
4046 vm_object_size_t size
;
4047 vm_external_map_t map
;
4048 #endif /* MACH_PAGEMAP */
4050 XPR(XPR_VM_OBJECT
, "vm_object_pager_create, object 0x%X\n",
4053 assert(object
!= kernel_object
);
4055 if (memory_manager_default_check() != KERN_SUCCESS
)
4059 * Prevent collapse or termination by holding a paging reference
4062 vm_object_paging_begin(object
);
4063 if (object
->pager_created
) {
4065 * Someone else got to it first...
4066 * wait for them to finish initializing the ports
4068 while (!object
->pager_initialized
) {
4069 vm_object_sleep(object
,
4070 VM_OBJECT_EVENT_INITIALIZED
,
4073 vm_object_paging_end(object
);
4078 * Indicate that a memory object has been assigned
4079 * before dropping the lock, to prevent a race.
4082 object
->pager_created
= TRUE
;
4083 object
->paging_offset
= 0;
4086 size
= object
->size
;
4087 #endif /* MACH_PAGEMAP */
4088 vm_object_unlock(object
);
4091 map
= vm_external_create(size
);
4092 vm_object_lock(object
);
4093 assert(object
->size
== size
);
4094 object
->existence_map
= map
;
4095 vm_object_unlock(object
);
4096 #endif /* MACH_PAGEMAP */
4098 if ((uint32_t) object
->size
!= object
->size
) {
4099 panic("vm_object_pager_create(): object size 0x%llx >= 4GB\n",
4100 (uint64_t) object
->size
);
4104 * Create the [internal] pager, and associate it with this object.
4106 * We make the association here so that vm_object_enter()
4107 * can look up the object to complete initializing it. No
4108 * user will ever map this object.
4111 memory_object_default_t dmm
;
4113 /* acquire a reference for the default memory manager */
4114 dmm
= memory_manager_default_reference();
4116 assert(object
->temporary
);
4118 /* create our new memory object */
4119 assert((vm_size_t
) object
->size
== object
->size
);
4120 (void) memory_object_create(dmm
, (vm_size_t
) object
->size
,
4123 memory_object_default_deallocate(dmm
);
4126 entry
= vm_object_hash_entry_alloc(pager
);
4128 lck
= vm_object_hash_lock_spin(pager
);
4129 vm_object_hash_insert(entry
, object
);
4130 vm_object_hash_unlock(lck
);
4133 * A reference was returned by
4134 * memory_object_create(), and it is
4135 * copied by vm_object_enter().
4138 if (vm_object_enter(pager
, object
->size
, TRUE
, TRUE
, FALSE
) != object
)
4139 panic("vm_object_pager_create: mismatch");
4142 * Drop the reference we were passed.
4144 memory_object_deallocate(pager
);
4146 vm_object_lock(object
);
4149 * Release the paging reference
4151 vm_object_paging_end(object
);
4155 * Routine: vm_object_remove
4157 * Eliminate the pager/object association
4160 * The object cache must be locked.
4162 __private_extern__
void
4166 memory_object_t pager
;
4168 if ((pager
= object
->pager
) != MEMORY_OBJECT_NULL
) {
4169 vm_object_hash_entry_t entry
;
4171 entry
= vm_object_hash_lookup(pager
, FALSE
);
4172 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
4173 entry
->object
= VM_OBJECT_NULL
;
4179 * Global variables for vm_object_collapse():
4181 * Counts for normal collapses and bypasses.
4182 * Debugging variables, to watch or disable collapse.
4184 static long object_collapses
= 0;
4185 static long object_bypasses
= 0;
4187 static boolean_t vm_object_collapse_allowed
= TRUE
;
4188 static boolean_t vm_object_bypass_allowed
= TRUE
;
4191 static int vm_external_discarded
;
4192 static int vm_external_collapsed
;
4195 unsigned long vm_object_collapse_encrypted
= 0;
4198 * Routine: vm_object_do_collapse
4200 * Collapse an object with the object backing it.
4201 * Pages in the backing object are moved into the
4202 * parent, and the backing object is deallocated.
4204 * Both objects and the cache are locked; the page
4205 * queues are unlocked.
4209 vm_object_do_collapse(
4211 vm_object_t backing_object
)
4214 vm_object_offset_t new_offset
, backing_offset
;
4215 vm_object_size_t size
;
4217 vm_object_lock_assert_exclusive(object
);
4218 vm_object_lock_assert_exclusive(backing_object
);
4220 backing_offset
= object
->shadow_offset
;
4221 size
= object
->size
;
4224 * Move all in-memory pages from backing_object
4225 * to the parent. Pages that have been paged out
4226 * will be overwritten by any of the parent's
4227 * pages that shadow them.
4230 while (!queue_empty(&backing_object
->memq
)) {
4232 p
= (vm_page_t
) queue_first(&backing_object
->memq
);
4234 new_offset
= (p
->offset
- backing_offset
);
4236 assert(!p
->busy
|| p
->absent
);
4239 * If the parent has a page here, or if
4240 * this page falls outside the parent,
4243 * Otherwise, move it as planned.
4246 if (p
->offset
< backing_offset
|| new_offset
>= size
) {
4251 * The encryption key includes the "pager" and the
4252 * "paging_offset". These will not change during the
4253 * object collapse, so we can just move an encrypted
4254 * page from one object to the other in this case.
4255 * We can't decrypt the page here, since we can't drop
4259 vm_object_collapse_encrypted
++;
4261 pp
= vm_page_lookup(object
, new_offset
);
4262 if (pp
== VM_PAGE_NULL
) {
4265 * Parent now has no page.
4266 * Move the backing object's page up.
4269 vm_page_rename(p
, object
, new_offset
, TRUE
);
4271 } else if (pp
->absent
) {
4274 * Parent has an absent page...
4275 * it's not being paged in, so
4276 * it must really be missing from
4279 * Throw out the absent page...
4280 * any faults looking for that
4281 * page will restart with the new
4286 vm_page_rename(p
, object
, new_offset
, TRUE
);
4287 #endif /* MACH_PAGEMAP */
4289 assert(! pp
->absent
);
4292 * Parent object has a real page.
4293 * Throw away the backing object's
4302 assert((!object
->pager_created
&& (object
->pager
== MEMORY_OBJECT_NULL
))
4303 || (!backing_object
->pager_created
4304 && (backing_object
->pager
== MEMORY_OBJECT_NULL
)));
4306 assert(!object
->pager_created
&& object
->pager
== MEMORY_OBJECT_NULL
);
4307 #endif /* !MACH_PAGEMAP */
4309 if (backing_object
->pager
!= MEMORY_OBJECT_NULL
) {
4310 vm_object_hash_entry_t entry
;
4313 * Move the pager from backing_object to object.
4315 * XXX We're only using part of the paging space
4316 * for keeps now... we ought to discard the
4320 assert(!object
->paging_in_progress
);
4321 assert(!object
->activity_in_progress
);
4322 object
->pager
= backing_object
->pager
;
4324 if (backing_object
->hashed
) {
4327 lck
= vm_object_hash_lock_spin(backing_object
->pager
);
4328 entry
= vm_object_hash_lookup(object
->pager
, FALSE
);
4329 assert(entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
4330 entry
->object
= object
;
4331 vm_object_hash_unlock(lck
);
4333 object
->hashed
= TRUE
;
4335 object
->pager_created
= backing_object
->pager_created
;
4336 object
->pager_control
= backing_object
->pager_control
;
4337 object
->pager_ready
= backing_object
->pager_ready
;
4338 object
->pager_initialized
= backing_object
->pager_initialized
;
4339 object
->paging_offset
=
4340 backing_object
->paging_offset
+ backing_offset
;
4341 if (object
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
4342 memory_object_control_collapse(object
->pager_control
,
4349 * If the shadow offset is 0, the use the existence map from
4350 * the backing object if there is one. If the shadow offset is
4351 * not zero, toss it.
4353 * XXX - If the shadow offset is not 0 then a bit copy is needed
4354 * if the map is to be salvaged. For now, we just just toss the
4355 * old map, giving the collapsed object no map. This means that
4356 * the pager is invoked for zero fill pages. If analysis shows
4357 * that this happens frequently and is a performance hit, then
4358 * this code should be fixed to salvage the map.
4360 assert(object
->existence_map
== VM_EXTERNAL_NULL
);
4361 if (backing_offset
|| (size
!= backing_object
->size
)) {
4362 vm_external_discarded
++;
4363 vm_external_destroy(backing_object
->existence_map
,
4364 backing_object
->size
);
4367 vm_external_collapsed
++;
4368 object
->existence_map
= backing_object
->existence_map
;
4370 backing_object
->existence_map
= VM_EXTERNAL_NULL
;
4371 #endif /* MACH_PAGEMAP */
4374 * Object now shadows whatever backing_object did.
4375 * Note that the reference to backing_object->shadow
4376 * moves from within backing_object to within object.
4379 assert(!object
->phys_contiguous
);
4380 assert(!backing_object
->phys_contiguous
);
4381 object
->shadow
= backing_object
->shadow
;
4382 if (object
->shadow
) {
4383 object
->shadow_offset
+= backing_object
->shadow_offset
;
4385 /* no shadow, therefore no shadow offset... */
4386 object
->shadow_offset
= 0;
4388 assert((object
->shadow
== VM_OBJECT_NULL
) ||
4389 (object
->shadow
->copy
!= backing_object
));
4392 * Discard backing_object.
4394 * Since the backing object has no pages, no
4395 * pager left, and no object references within it,
4396 * all that is necessary is to dispose of it.
4399 assert((backing_object
->ref_count
== 1) &&
4400 (backing_object
->resident_page_count
== 0) &&
4401 (backing_object
->paging_in_progress
== 0) &&
4402 (backing_object
->activity_in_progress
== 0));
4404 backing_object
->alive
= FALSE
;
4405 vm_object_unlock(backing_object
);
4407 XPR(XPR_VM_OBJECT
, "vm_object_collapse, collapsed 0x%X\n",
4408 backing_object
, 0,0,0,0);
4410 vm_object_lock_destroy(backing_object
);
4412 zfree(vm_object_zone
, backing_object
);
4418 vm_object_do_bypass(
4420 vm_object_t backing_object
)
4423 * Make the parent shadow the next object
4427 vm_object_lock_assert_exclusive(object
);
4428 vm_object_lock_assert_exclusive(backing_object
);
4432 * Do object reference in-line to
4433 * conditionally increment shadow's
4434 * residence count. If object is not
4435 * resident, leave residence count
4438 if (backing_object
->shadow
!= VM_OBJECT_NULL
) {
4439 vm_object_lock(backing_object
->shadow
);
4440 vm_object_lock_assert_exclusive(backing_object
->shadow
);
4441 backing_object
->shadow
->ref_count
++;
4442 if (object
->res_count
!= 0)
4443 vm_object_res_reference(backing_object
->shadow
);
4444 vm_object_unlock(backing_object
->shadow
);
4446 #else /* TASK_SWAPPER */
4447 vm_object_reference(backing_object
->shadow
);
4448 #endif /* TASK_SWAPPER */
4450 assert(!object
->phys_contiguous
);
4451 assert(!backing_object
->phys_contiguous
);
4452 object
->shadow
= backing_object
->shadow
;
4453 if (object
->shadow
) {
4454 object
->shadow_offset
+= backing_object
->shadow_offset
;
4456 /* no shadow, therefore no shadow offset... */
4457 object
->shadow_offset
= 0;
4461 * Backing object might have had a copy pointer
4462 * to us. If it did, clear it.
4464 if (backing_object
->copy
== object
) {
4465 backing_object
->copy
= VM_OBJECT_NULL
;
4469 * Drop the reference count on backing_object.
4471 * Since its ref_count was at least 2, it
4472 * will not vanish; so we don't need to call
4473 * vm_object_deallocate.
4474 * [with a caveat for "named" objects]
4476 * The res_count on the backing object is
4477 * conditionally decremented. It's possible
4478 * (via vm_pageout_scan) to get here with
4479 * a "swapped" object, which has a 0 res_count,
4480 * in which case, the backing object res_count
4481 * is already down by one.
4483 * Don't call vm_object_deallocate unless
4484 * ref_count drops to zero.
4486 * The ref_count can drop to zero here if the
4487 * backing object could be bypassed but not
4488 * collapsed, such as when the backing object
4489 * is temporary and cachable.
4492 if (backing_object
->ref_count
> 2 ||
4493 (!backing_object
->named
&& backing_object
->ref_count
> 1)) {
4494 vm_object_lock_assert_exclusive(backing_object
);
4495 backing_object
->ref_count
--;
4497 if (object
->res_count
!= 0)
4498 vm_object_res_deallocate(backing_object
);
4499 assert(backing_object
->ref_count
> 0);
4500 #endif /* TASK_SWAPPER */
4501 vm_object_unlock(backing_object
);
4505 * Drop locks so that we can deallocate
4506 * the backing object.
4510 if (object
->res_count
== 0) {
4511 /* XXX get a reference for the deallocate below */
4512 vm_object_res_reference(backing_object
);
4514 #endif /* TASK_SWAPPER */
4515 vm_object_unlock(object
);
4516 vm_object_unlock(backing_object
);
4517 vm_object_deallocate(backing_object
);
4520 * Relock object. We don't have to reverify
4521 * its state since vm_object_collapse will
4522 * do that for us as it starts at the
4526 vm_object_lock(object
);
4534 * vm_object_collapse:
4536 * Perform an object collapse or an object bypass if appropriate.
4537 * The real work of collapsing and bypassing is performed in
4538 * the routines vm_object_do_collapse and vm_object_do_bypass.
4540 * Requires that the object be locked and the page queues be unlocked.
4543 static unsigned long vm_object_collapse_calls
= 0;
4544 static unsigned long vm_object_collapse_objects
= 0;
4545 static unsigned long vm_object_collapse_do_collapse
= 0;
4546 static unsigned long vm_object_collapse_do_bypass
= 0;
4547 static unsigned long vm_object_collapse_delays
= 0;
4548 __private_extern__
void
4550 register vm_object_t object
,
4551 register vm_object_offset_t hint_offset
,
4552 boolean_t can_bypass
)
4554 register vm_object_t backing_object
;
4555 register unsigned int rcount
;
4556 register unsigned int size
;
4557 vm_object_t original_object
;
4558 int object_lock_type
;
4559 int backing_object_lock_type
;
4561 vm_object_collapse_calls
++;
4563 if (! vm_object_collapse_allowed
&&
4564 ! (can_bypass
&& vm_object_bypass_allowed
)) {
4568 XPR(XPR_VM_OBJECT
, "vm_object_collapse, obj 0x%X\n",
4571 if (object
== VM_OBJECT_NULL
)
4574 original_object
= object
;
4577 * The top object was locked "exclusive" by the caller.
4578 * In the first pass, to determine if we can collapse the shadow chain,
4579 * take a "shared" lock on the shadow objects. If we can collapse,
4580 * we'll have to go down the chain again with exclusive locks.
4582 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4583 backing_object_lock_type
= OBJECT_LOCK_SHARED
;
4586 object
= original_object
;
4587 vm_object_lock_assert_exclusive(object
);
4590 vm_object_collapse_objects
++;
4592 * Verify that the conditions are right for either
4593 * collapse or bypass:
4597 * There is a backing object, and
4600 backing_object
= object
->shadow
;
4601 if (backing_object
== VM_OBJECT_NULL
) {
4602 if (object
!= original_object
) {
4603 vm_object_unlock(object
);
4607 if (backing_object_lock_type
== OBJECT_LOCK_SHARED
) {
4608 vm_object_lock_shared(backing_object
);
4610 vm_object_lock(backing_object
);
4614 * No pages in the object are currently
4615 * being paged out, and
4617 if (object
->paging_in_progress
!= 0 ||
4618 object
->activity_in_progress
!= 0) {
4619 /* try and collapse the rest of the shadow chain */
4620 if (object
!= original_object
) {
4621 vm_object_unlock(object
);
4623 object
= backing_object
;
4624 object_lock_type
= backing_object_lock_type
;
4630 * The backing object is not read_only,
4631 * and no pages in the backing object are
4632 * currently being paged out.
4633 * The backing object is internal.
4637 if (!backing_object
->internal
||
4638 backing_object
->paging_in_progress
!= 0 ||
4639 backing_object
->activity_in_progress
!= 0) {
4640 /* try and collapse the rest of the shadow chain */
4641 if (object
!= original_object
) {
4642 vm_object_unlock(object
);
4644 object
= backing_object
;
4645 object_lock_type
= backing_object_lock_type
;
4650 * The backing object can't be a copy-object:
4651 * the shadow_offset for the copy-object must stay
4652 * as 0. Furthermore (for the 'we have all the
4653 * pages' case), if we bypass backing_object and
4654 * just shadow the next object in the chain, old
4655 * pages from that object would then have to be copied
4656 * BOTH into the (former) backing_object and into the
4659 if (backing_object
->shadow
!= VM_OBJECT_NULL
&&
4660 backing_object
->shadow
->copy
== backing_object
) {
4661 /* try and collapse the rest of the shadow chain */
4662 if (object
!= original_object
) {
4663 vm_object_unlock(object
);
4665 object
= backing_object
;
4666 object_lock_type
= backing_object_lock_type
;
4671 * We can now try to either collapse the backing
4672 * object (if the parent is the only reference to
4673 * it) or (perhaps) remove the parent's reference
4676 * If there is exactly one reference to the backing
4677 * object, we may be able to collapse it into the
4680 * If MACH_PAGEMAP is defined:
4681 * The parent must not have a pager created for it,
4682 * since collapsing a backing_object dumps new pages
4683 * into the parent that its pager doesn't know about
4684 * (and the collapse code can't merge the existence
4687 * As long as one of the objects is still not known
4688 * to the pager, we can collapse them.
4690 if (backing_object
->ref_count
== 1 &&
4691 (!object
->pager_created
4693 || !backing_object
->pager_created
4694 #endif /*!MACH_PAGEMAP */
4695 ) && vm_object_collapse_allowed
) {
4698 * We need the exclusive lock on the VM objects.
4700 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
4702 * We have an object and its shadow locked
4703 * "shared". We can't just upgrade the locks
4704 * to "exclusive", as some other thread might
4705 * also have these objects locked "shared" and
4706 * attempt to upgrade one or the other to
4707 * "exclusive". The upgrades would block
4708 * forever waiting for the other "shared" locks
4710 * So we have to release the locks and go
4711 * down the shadow chain again (since it could
4712 * have changed) with "exclusive" locking.
4714 vm_object_unlock(backing_object
);
4715 if (object
!= original_object
)
4716 vm_object_unlock(object
);
4717 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4718 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4723 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
4724 backing_object
, object
,
4725 backing_object
->pager
,
4726 backing_object
->pager_control
, 0);
4729 * Collapse the object with its backing
4730 * object, and try again with the object's
4731 * new backing object.
4734 vm_object_do_collapse(object
, backing_object
);
4735 vm_object_collapse_do_collapse
++;
4740 * Collapsing the backing object was not possible
4741 * or permitted, so let's try bypassing it.
4744 if (! (can_bypass
&& vm_object_bypass_allowed
)) {
4745 /* try and collapse the rest of the shadow chain */
4746 if (object
!= original_object
) {
4747 vm_object_unlock(object
);
4749 object
= backing_object
;
4750 object_lock_type
= backing_object_lock_type
;
4756 * If the object doesn't have all its pages present,
4757 * we have to make sure no pages in the backing object
4758 * "show through" before bypassing it.
4760 size
= atop(object
->size
);
4761 rcount
= object
->resident_page_count
;
4762 if (rcount
!= size
) {
4763 vm_object_offset_t offset
;
4764 vm_object_offset_t backing_offset
;
4765 unsigned int backing_rcount
;
4766 unsigned int lookups
= 0;
4769 * If the backing object has a pager but no pagemap,
4770 * then we cannot bypass it, because we don't know
4771 * what pages it has.
4773 if (backing_object
->pager_created
4775 && (backing_object
->existence_map
== VM_EXTERNAL_NULL
)
4776 #endif /* MACH_PAGEMAP */
4778 /* try and collapse the rest of the shadow chain */
4779 if (object
!= original_object
) {
4780 vm_object_unlock(object
);
4782 object
= backing_object
;
4783 object_lock_type
= backing_object_lock_type
;
4788 * If the object has a pager but no pagemap,
4789 * then we cannot bypass it, because we don't know
4790 * what pages it has.
4792 if (object
->pager_created
4794 && (object
->existence_map
== VM_EXTERNAL_NULL
)
4795 #endif /* MACH_PAGEMAP */
4797 /* try and collapse the rest of the shadow chain */
4798 if (object
!= original_object
) {
4799 vm_object_unlock(object
);
4801 object
= backing_object
;
4802 object_lock_type
= backing_object_lock_type
;
4807 * If all of the pages in the backing object are
4808 * shadowed by the parent object, the parent
4809 * object no longer has to shadow the backing
4810 * object; it can shadow the next one in the
4813 * If the backing object has existence info,
4814 * we must check examine its existence info
4819 backing_offset
= object
->shadow_offset
;
4820 backing_rcount
= backing_object
->resident_page_count
;
4823 #define EXISTS_IN_OBJECT(obj, off, rc) \
4824 (vm_external_state_get((obj)->existence_map, \
4825 (vm_offset_t)(off)) == VM_EXTERNAL_STATE_EXISTS || \
4826 ((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
4828 #define EXISTS_IN_OBJECT(obj, off, rc) \
4829 (((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
4830 #endif /* MACH_PAGEMAP */
4833 * Check the hint location first
4834 * (since it is often the quickest way out of here).
4836 if (object
->cow_hint
!= ~(vm_offset_t
)0)
4837 hint_offset
= (vm_object_offset_t
)object
->cow_hint
;
4839 hint_offset
= (hint_offset
> 8 * PAGE_SIZE_64
) ?
4840 (hint_offset
- 8 * PAGE_SIZE_64
) : 0;
4842 if (EXISTS_IN_OBJECT(backing_object
, hint_offset
+
4843 backing_offset
, backing_rcount
) &&
4844 !EXISTS_IN_OBJECT(object
, hint_offset
, rcount
)) {
4845 /* dependency right at the hint */
4846 object
->cow_hint
= (vm_offset_t
) hint_offset
; /* atomic */
4847 /* try and collapse the rest of the shadow chain */
4848 if (object
!= original_object
) {
4849 vm_object_unlock(object
);
4851 object
= backing_object
;
4852 object_lock_type
= backing_object_lock_type
;
4857 * If the object's window onto the backing_object
4858 * is large compared to the number of resident
4859 * pages in the backing object, it makes sense to
4860 * walk the backing_object's resident pages first.
4862 * NOTE: Pages may be in both the existence map and
4863 * resident. So, we can't permanently decrement
4864 * the rcount here because the second loop may
4865 * find the same pages in the backing object'
4866 * existence map that we found here and we would
4867 * double-decrement the rcount. We also may or
4868 * may not have found the
4870 if (backing_rcount
&&
4872 size
> ((backing_object
->existence_map
) ?
4873 backing_rcount
: (backing_rcount
>> 1))
4875 size
> (backing_rcount
>> 1)
4876 #endif /* MACH_PAGEMAP */
4878 unsigned int rc
= rcount
;
4881 backing_rcount
= backing_object
->resident_page_count
;
4882 p
= (vm_page_t
)queue_first(&backing_object
->memq
);
4884 /* Until we get more than one lookup lock */
4885 if (lookups
> 256) {
4886 vm_object_collapse_delays
++;
4891 offset
= (p
->offset
- backing_offset
);
4892 if (offset
< object
->size
&&
4893 offset
!= hint_offset
&&
4894 !EXISTS_IN_OBJECT(object
, offset
, rc
)) {
4895 /* found a dependency */
4896 object
->cow_hint
= (vm_offset_t
) offset
; /* atomic */
4900 p
= (vm_page_t
) queue_next(&p
->listq
);
4902 } while (--backing_rcount
);
4903 if (backing_rcount
!= 0 ) {
4904 /* try and collapse the rest of the shadow chain */
4905 if (object
!= original_object
) {
4906 vm_object_unlock(object
);
4908 object
= backing_object
;
4909 object_lock_type
= backing_object_lock_type
;
4915 * Walk through the offsets looking for pages in the
4916 * backing object that show through to the object.
4920 || backing_object
->existence_map
4921 #endif /* MACH_PAGEMAP */
4923 offset
= hint_offset
;
4926 (offset
+ PAGE_SIZE_64
< object
->size
) ?
4927 (offset
+ PAGE_SIZE_64
) : 0) != hint_offset
) {
4929 /* Until we get more than one lookup lock */
4930 if (lookups
> 256) {
4931 vm_object_collapse_delays
++;
4936 if (EXISTS_IN_OBJECT(backing_object
, offset
+
4937 backing_offset
, backing_rcount
) &&
4938 !EXISTS_IN_OBJECT(object
, offset
, rcount
)) {
4939 /* found a dependency */
4940 object
->cow_hint
= (vm_offset_t
) offset
; /* atomic */
4944 if (offset
!= hint_offset
) {
4945 /* try and collapse the rest of the shadow chain */
4946 if (object
!= original_object
) {
4947 vm_object_unlock(object
);
4949 object
= backing_object
;
4950 object_lock_type
= backing_object_lock_type
;
4957 * We need "exclusive" locks on the 2 VM objects.
4959 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
4960 vm_object_unlock(backing_object
);
4961 if (object
!= original_object
)
4962 vm_object_unlock(object
);
4963 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4964 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4968 /* reset the offset hint for any objects deeper in the chain */
4969 object
->cow_hint
= (vm_offset_t
)0;
4972 * All interesting pages in the backing object
4973 * already live in the parent or its pager.
4974 * Thus we can bypass the backing object.
4977 vm_object_do_bypass(object
, backing_object
);
4978 vm_object_collapse_do_bypass
++;
4981 * Try again with this object's new backing object.
4987 if (object
!= original_object
) {
4988 vm_object_unlock(object
);
4993 * Routine: vm_object_page_remove: [internal]
4995 * Removes all physical pages in the specified
4996 * object range from the object's list of pages.
4998 * In/out conditions:
4999 * The object must be locked.
5000 * The object must not have paging_in_progress, usually
5001 * guaranteed by not having a pager.
5003 unsigned int vm_object_page_remove_lookup
= 0;
5004 unsigned int vm_object_page_remove_iterate
= 0;
5006 __private_extern__
void
5007 vm_object_page_remove(
5008 register vm_object_t object
,
5009 register vm_object_offset_t start
,
5010 register vm_object_offset_t end
)
5012 register vm_page_t p
, next
;
5015 * One and two page removals are most popular.
5016 * The factor of 16 here is somewhat arbitrary.
5017 * It balances vm_object_lookup vs iteration.
5020 if (atop_64(end
- start
) < (unsigned)object
->resident_page_count
/16) {
5021 vm_object_page_remove_lookup
++;
5023 for (; start
< end
; start
+= PAGE_SIZE_64
) {
5024 p
= vm_page_lookup(object
, start
);
5025 if (p
!= VM_PAGE_NULL
) {
5026 assert(!p
->cleaning
&& !p
->pageout
);
5027 if (!p
->fictitious
&& p
->pmapped
)
5028 pmap_disconnect(p
->phys_page
);
5033 vm_object_page_remove_iterate
++;
5035 p
= (vm_page_t
) queue_first(&object
->memq
);
5036 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
5037 next
= (vm_page_t
) queue_next(&p
->listq
);
5038 if ((start
<= p
->offset
) && (p
->offset
< end
)) {
5039 assert(!p
->cleaning
&& !p
->pageout
);
5040 if (!p
->fictitious
&& p
->pmapped
)
5041 pmap_disconnect(p
->phys_page
);
5051 * Routine: vm_object_coalesce
5052 * Function: Coalesces two objects backing up adjoining
5053 * regions of memory into a single object.
5055 * returns TRUE if objects were combined.
5057 * NOTE: Only works at the moment if the second object is NULL -
5058 * if it's not, which object do we lock first?
5061 * prev_object First object to coalesce
5062 * prev_offset Offset into prev_object
5063 * next_object Second object into coalesce
5064 * next_offset Offset into next_object
5066 * prev_size Size of reference to prev_object
5067 * next_size Size of reference to next_object
5070 * The object(s) must *not* be locked. The map must be locked
5071 * to preserve the reference to the object(s).
5073 static int vm_object_coalesce_count
= 0;
5075 __private_extern__ boolean_t
5077 register vm_object_t prev_object
,
5078 vm_object_t next_object
,
5079 vm_object_offset_t prev_offset
,
5080 __unused vm_object_offset_t next_offset
,
5081 vm_object_size_t prev_size
,
5082 vm_object_size_t next_size
)
5084 vm_object_size_t newsize
;
5090 if (next_object
!= VM_OBJECT_NULL
) {
5094 if (prev_object
== VM_OBJECT_NULL
) {
5099 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
5100 prev_object
, prev_offset
, prev_size
, next_size
, 0);
5102 vm_object_lock(prev_object
);
5105 * Try to collapse the object first
5107 vm_object_collapse(prev_object
, prev_offset
, TRUE
);
5110 * Can't coalesce if pages not mapped to
5111 * prev_entry may be in use any way:
5112 * . more than one reference
5114 * . shadows another object
5115 * . has a copy elsewhere
5117 * . paging references (pages might be in page-list)
5120 if ((prev_object
->ref_count
> 1) ||
5121 prev_object
->pager_created
||
5122 (prev_object
->shadow
!= VM_OBJECT_NULL
) ||
5123 (prev_object
->copy
!= VM_OBJECT_NULL
) ||
5124 (prev_object
->true_share
!= FALSE
) ||
5125 (prev_object
->purgable
!= VM_PURGABLE_DENY
) ||
5126 (prev_object
->paging_in_progress
!= 0) ||
5127 (prev_object
->activity_in_progress
!= 0)) {
5128 vm_object_unlock(prev_object
);
5132 vm_object_coalesce_count
++;
5135 * Remove any pages that may still be in the object from
5136 * a previous deallocation.
5138 vm_object_page_remove(prev_object
,
5139 prev_offset
+ prev_size
,
5140 prev_offset
+ prev_size
+ next_size
);
5143 * Extend the object if necessary.
5145 newsize
= prev_offset
+ prev_size
+ next_size
;
5146 if (newsize
> prev_object
->size
) {
5149 * We cannot extend an object that has existence info,
5150 * since the existence info might then fail to cover
5151 * the entire object.
5153 * This assertion must be true because the object
5154 * has no pager, and we only create existence info
5155 * for objects with pagers.
5157 assert(prev_object
->existence_map
== VM_EXTERNAL_NULL
);
5158 #endif /* MACH_PAGEMAP */
5159 prev_object
->size
= newsize
;
5162 vm_object_unlock(prev_object
);
5167 * Attach a set of physical pages to an object, so that they can
5168 * be mapped by mapping the object. Typically used to map IO memory.
5170 * The mapping function and its private data are used to obtain the
5171 * physical addresses for each page to be mapped.
5176 vm_object_offset_t offset
,
5177 vm_object_size_t size
,
5178 vm_object_offset_t (*map_fn
)(void *map_fn_data
,
5179 vm_object_offset_t offset
),
5180 void *map_fn_data
) /* private to map_fn */
5186 vm_object_offset_t addr
;
5188 num_pages
= atop_64(size
);
5190 for (i
= 0; i
< num_pages
; i
++, offset
+= PAGE_SIZE_64
) {
5192 addr
= (*map_fn
)(map_fn_data
, offset
);
5194 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
5195 vm_page_more_fictitious();
5197 vm_object_lock(object
);
5198 if ((old_page
= vm_page_lookup(object
, offset
))
5201 VM_PAGE_FREE(old_page
);
5204 assert((ppnum_t
) addr
== addr
);
5205 vm_page_init(m
, (ppnum_t
) addr
);
5207 * private normally requires lock_queues but since we
5208 * are initializing the page, its not necessary here
5210 m
->private = TRUE
; /* don`t free page */
5212 vm_page_insert(m
, object
, offset
);
5214 PAGE_WAKEUP_DONE(m
);
5215 vm_object_unlock(object
);
5219 #include <mach_kdb.h>
5222 #include <ddb/db_output.h>
5223 #include <vm/vm_print.h>
5225 #define printf kdbprintf
5227 extern boolean_t
vm_object_cached(
5228 vm_object_t object
);
5230 extern void print_bitstring(
5233 boolean_t vm_object_print_pages
= FALSE
;
5239 printf("%c%c%c%c%c%c%c%c",
5240 ((byte
& (1 << 0)) ? '1' : '0'),
5241 ((byte
& (1 << 1)) ? '1' : '0'),
5242 ((byte
& (1 << 2)) ? '1' : '0'),
5243 ((byte
& (1 << 3)) ? '1' : '0'),
5244 ((byte
& (1 << 4)) ? '1' : '0'),
5245 ((byte
& (1 << 5)) ? '1' : '0'),
5246 ((byte
& (1 << 6)) ? '1' : '0'),
5247 ((byte
& (1 << 7)) ? '1' : '0'));
5252 __unused
register vm_object_t object
)
5255 register vm_object_t o
;
5257 queue_iterate(&vm_object_cached_list
, o
, vm_object_t
, cached_list
) {
5268 * vm_external_print: [ debug ]
5272 vm_external_map_t emap
,
5273 vm_object_size_t size
)
5275 if (emap
== VM_EXTERNAL_NULL
) {
5278 vm_object_size_t existence_size
= stob(size
);
5279 printf("{ size=%lld, map=[", (uint64_t) existence_size
);
5280 if (existence_size
> 0) {
5281 print_bitstring(emap
[0]);
5283 if (existence_size
> 1) {
5284 print_bitstring(emap
[1]);
5286 if (existence_size
> 2) {
5288 print_bitstring(emap
[existence_size
-1]);
5294 #endif /* MACH_PAGEMAP */
5301 int orig_db_indent
= db_indent
;
5304 if (object
== VM_OBJECT_NULL
) {
5305 db_indent
= orig_db_indent
;
5311 iprintf("object 0x%x", object
);
5312 printf(", shadow=0x%x", object
->shadow
);
5313 printf(", copy=0x%x", object
->copy
);
5314 printf(", pager=0x%x", object
->pager
);
5315 printf(", ref=%d\n", object
->ref_count
);
5318 object
= object
->shadow
;
5324 * vm_object_print: [ debug ]
5327 vm_object_print(db_expr_t db_addr
, __unused boolean_t have_addr
,
5328 __unused db_expr_t arg_count
, __unused
char *modif
)
5331 register vm_page_t p
;
5336 object
= (vm_object_t
) (long) db_addr
;
5337 if (object
== VM_OBJECT_NULL
)
5340 iprintf("object 0x%x\n", object
);
5344 iprintf("size=0x%x", object
->size
);
5345 printf(", memq_hint=%p", object
->memq_hint
);
5346 printf(", ref_count=%d\n", object
->ref_count
);
5349 printf("res_count=%d, ", object
->res_count
);
5350 #endif /* TASK_SWAPPER */
5351 printf("resident_page_count=%d\n", object
->resident_page_count
);
5353 iprintf("shadow=0x%x", object
->shadow
);
5354 if (object
->shadow
) {
5356 vm_object_t shadow
= object
;
5357 while((shadow
= shadow
->shadow
))
5359 printf(" (depth %d)", i
);
5361 printf(", copy=0x%x", object
->copy
);
5362 printf(", shadow_offset=0x%x", object
->shadow_offset
);
5363 printf(", last_alloc=0x%x\n", object
->last_alloc
);
5365 iprintf("pager=0x%x", object
->pager
);
5366 printf(", paging_offset=0x%x", object
->paging_offset
);
5367 printf(", pager_control=0x%x\n", object
->pager_control
);
5369 iprintf("copy_strategy=%d[", object
->copy_strategy
);
5370 switch (object
->copy_strategy
) {
5371 case MEMORY_OBJECT_COPY_NONE
:
5372 printf("copy_none");
5375 case MEMORY_OBJECT_COPY_CALL
:
5376 printf("copy_call");
5379 case MEMORY_OBJECT_COPY_DELAY
:
5380 printf("copy_delay");
5383 case MEMORY_OBJECT_COPY_SYMMETRIC
:
5384 printf("copy_symmetric");
5387 case MEMORY_OBJECT_COPY_INVALID
:
5388 printf("copy_invalid");
5396 iprintf("all_wanted=0x%x<", object
->all_wanted
);
5398 if (vm_object_wanted(object
, VM_OBJECT_EVENT_INITIALIZED
)) {
5399 printf("%sinit", s
);
5402 if (vm_object_wanted(object
, VM_OBJECT_EVENT_PAGER_READY
)) {
5403 printf("%sready", s
);
5406 if (vm_object_wanted(object
, VM_OBJECT_EVENT_PAGING_IN_PROGRESS
)) {
5407 printf("%spaging", s
);
5410 if (vm_object_wanted(object
, VM_OBJECT_EVENT_LOCK_IN_PROGRESS
)) {
5411 printf("%slock", s
);
5414 if (vm_object_wanted(object
, VM_OBJECT_EVENT_UNCACHING
)) {
5415 printf("%suncaching", s
);
5418 if (vm_object_wanted(object
, VM_OBJECT_EVENT_COPY_CALL
)) {
5419 printf("%scopy_call", s
);
5422 if (vm_object_wanted(object
, VM_OBJECT_EVENT_CACHING
)) {
5423 printf("%scaching", s
);
5427 printf(", paging_in_progress=%d\n", object
->paging_in_progress
);
5428 printf(", activity_in_progress=%d\n", object
->activity_in_progress
);
5430 iprintf("%screated, %sinit, %sready, %spersist, %strusted, %spageout, %s, %s\n",
5431 (object
->pager_created
? "" : "!"),
5432 (object
->pager_initialized
? "" : "!"),
5433 (object
->pager_ready
? "" : "!"),
5434 (object
->can_persist
? "" : "!"),
5435 (object
->pager_trusted
? "" : "!"),
5436 (object
->pageout
? "" : "!"),
5437 (object
->internal
? "internal" : "external"),
5438 (object
->temporary
? "temporary" : "permanent"));
5439 iprintf("%salive, %spurgeable, %spurgeable_volatile, %spurgeable_empty, %sshadowed, %scached, %sprivate\n",
5440 (object
->alive
? "" : "!"),
5441 ((object
->purgable
!= VM_PURGABLE_DENY
) ? "" : "!"),
5442 ((object
->purgable
== VM_PURGABLE_VOLATILE
) ? "" : "!"),
5443 ((object
->purgable
== VM_PURGABLE_EMPTY
) ? "" : "!"),
5444 (object
->shadowed
? "" : "!"),
5445 (vm_object_cached(object
) ? "" : "!"),
5446 (object
->private ? "" : "!"));
5447 iprintf("%sadvisory_pageout, %ssilent_overwrite\n",
5448 (object
->advisory_pageout
? "" : "!"),
5449 (object
->silent_overwrite
? "" : "!"));
5452 iprintf("existence_map=");
5453 vm_external_print(object
->existence_map
, object
->size
);
5454 #endif /* MACH_PAGEMAP */
5456 iprintf("paging_object=0x%x\n", object
->paging_object
);
5457 #endif /* MACH_ASSERT */
5459 if (vm_object_print_pages
) {
5461 p
= (vm_page_t
) queue_first(&object
->memq
);
5462 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
5464 iprintf("memory:=");
5465 } else if (count
== 2) {
5474 printf("(off=0x%llX,page=%p)", p
->offset
, p
);
5475 p
= (vm_page_t
) queue_next(&p
->listq
);
5486 * vm_object_find [ debug ]
5488 * Find all tasks which reference the given vm_object.
5491 boolean_t
vm_object_find(vm_object_t object
);
5492 boolean_t vm_object_print_verbose
= FALSE
;
5500 vm_map_entry_t entry
;
5501 boolean_t found
= FALSE
;
5503 queue_iterate(&tasks
, task
, task_t
, tasks
) {
5505 for (entry
= vm_map_first_entry(map
);
5506 entry
&& entry
!= vm_map_to_entry(map
);
5507 entry
= entry
->vme_next
) {
5512 * For the time being skip submaps,
5513 * only the kernel can have submaps,
5514 * and unless we are interested in
5515 * kernel objects, we can simply skip
5516 * submaps. See sb/dejan/nmk18b7/src/mach_kernel/vm
5517 * for a full solution.
5519 if (entry
->is_sub_map
)
5522 obj
= entry
->object
.vm_object
;
5526 while (obj
!= VM_OBJECT_NULL
) {
5527 if (obj
== object
) {
5529 printf("TASK\t\tMAP\t\tENTRY\n");
5532 printf("0x%x\t0x%x\t0x%x\n",
5543 #endif /* MACH_KDB */
5546 vm_object_populate_with_private(
5548 vm_object_offset_t offset
,
5553 vm_object_offset_t base_offset
;
5556 if(!object
->private)
5557 return KERN_FAILURE
;
5559 base_page
= phys_page
;
5561 vm_object_lock(object
);
5562 if(!object
->phys_contiguous
) {
5564 if((base_offset
= trunc_page_64(offset
)) != offset
) {
5565 vm_object_unlock(object
);
5566 return KERN_FAILURE
;
5568 base_offset
+= object
->paging_offset
;
5570 m
= vm_page_lookup(object
, base_offset
);
5571 if(m
!= VM_PAGE_NULL
) {
5573 if (m
->phys_page
!= vm_page_guard_addr
) {
5575 vm_page_lockspin_queues();
5577 vm_page_unlock_queues();
5579 m
->fictitious
= FALSE
;
5580 m
->phys_page
= base_page
;
5587 m
->list_req_pending
= TRUE
;
5589 } else if (m
->phys_page
!= base_page
) {
5592 * pmap call to clear old mapping
5594 pmap_disconnect(m
->phys_page
);
5596 m
->phys_page
= base_page
;
5601 * We're not pointing to the same
5602 * physical page any longer and the
5603 * contents of the new one are not
5604 * supposed to be encrypted.
5605 * XXX What happens to the original
5606 * physical page. Is it lost ?
5608 m
->encrypted
= FALSE
;
5611 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
5612 vm_page_more_fictitious();
5615 * private normally requires lock_queues but since we
5616 * are initializing the page, its not necessary here
5619 m
->fictitious
= FALSE
;
5620 m
->phys_page
= base_page
;
5621 m
->list_req_pending
= TRUE
;
5625 vm_page_insert(m
, object
, base_offset
);
5627 base_page
++; /* Go to the next physical page */
5628 base_offset
+= PAGE_SIZE
;
5632 /* NOTE: we should check the original settings here */
5633 /* if we have a size > zero a pmap call should be made */
5634 /* to disable the range */
5638 /* shadows on contiguous memory are not allowed */
5639 /* we therefore can use the offset field */
5640 object
->shadow_offset
= (vm_object_offset_t
)phys_page
<< PAGE_SHIFT
;
5641 object
->size
= size
;
5643 vm_object_unlock(object
);
5644 return KERN_SUCCESS
;
5648 * memory_object_free_from_cache:
5650 * Walk the vm_object cache list, removing and freeing vm_objects
5651 * which are backed by the pager identified by the caller, (pager_ops).
5652 * Remove up to "count" objects, if there are that may available
5655 * Walk the list at most once, return the number of vm_objects
5659 __private_extern__ kern_return_t
5660 memory_object_free_from_cache(
5661 __unused host_t host
,
5662 __unused memory_object_pager_ops_t pager_ops
,
5666 int object_released
= 0;
5668 register vm_object_t object
= VM_OBJECT_NULL
;
5672 if(host == HOST_NULL)
5673 return(KERN_INVALID_ARGUMENT);
5677 vm_object_cache_lock();
5679 queue_iterate(&vm_object_cached_list
, object
,
5680 vm_object_t
, cached_list
) {
5681 if (object
->pager
&&
5682 (pager_ops
== object
->pager
->mo_pager_ops
)) {
5683 vm_object_lock(object
);
5684 queue_remove(&vm_object_cached_list
, object
,
5685 vm_object_t
, cached_list
);
5686 vm_object_cached_count
--;
5688 vm_object_cache_unlock();
5690 * Since this object is in the cache, we know
5691 * that it is initialized and has only a pager's
5692 * (implicit) reference. Take a reference to avoid
5693 * recursive deallocations.
5696 assert(object
->pager_initialized
);
5697 assert(object
->ref_count
== 0);
5698 vm_object_lock_assert_exclusive(object
);
5699 object
->ref_count
++;
5702 * Terminate the object.
5703 * If the object had a shadow, we let
5704 * vm_object_deallocate deallocate it.
5705 * "pageout" objects have a shadow, but
5706 * maintain a "paging reference" rather
5707 * than a normal reference.
5708 * (We are careful here to limit recursion.)
5710 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
5712 if ((vm_object_terminate(object
) == KERN_SUCCESS
)
5713 && (shadow
!= VM_OBJECT_NULL
)) {
5714 vm_object_deallocate(shadow
);
5717 if(object_released
++ == *count
)
5718 return KERN_SUCCESS
;
5722 vm_object_cache_unlock();
5723 *count
= object_released
;
5727 return KERN_SUCCESS
;
5733 memory_object_create_named(
5734 memory_object_t pager
,
5735 memory_object_offset_t size
,
5736 memory_object_control_t
*control
)
5739 vm_object_hash_entry_t entry
;
5742 *control
= MEMORY_OBJECT_CONTROL_NULL
;
5743 if (pager
== MEMORY_OBJECT_NULL
)
5744 return KERN_INVALID_ARGUMENT
;
5746 lck
= vm_object_hash_lock_spin(pager
);
5747 entry
= vm_object_hash_lookup(pager
, FALSE
);
5749 if ((entry
!= VM_OBJECT_HASH_ENTRY_NULL
) &&
5750 (entry
->object
!= VM_OBJECT_NULL
)) {
5751 if (entry
->object
->named
== TRUE
)
5752 panic("memory_object_create_named: caller already holds the right"); }
5753 vm_object_hash_unlock(lck
);
5755 if ((object
= vm_object_enter(pager
, size
, FALSE
, FALSE
, TRUE
)) == VM_OBJECT_NULL
) {
5756 return(KERN_INVALID_OBJECT
);
5759 /* wait for object (if any) to be ready */
5760 if (object
!= VM_OBJECT_NULL
) {
5761 vm_object_lock(object
);
5762 object
->named
= TRUE
;
5763 while (!object
->pager_ready
) {
5764 vm_object_sleep(object
,
5765 VM_OBJECT_EVENT_PAGER_READY
,
5768 *control
= object
->pager_control
;
5769 vm_object_unlock(object
);
5771 return (KERN_SUCCESS
);
5776 * Routine: memory_object_recover_named [user interface]
5778 * Attempt to recover a named reference for a VM object.
5779 * VM will verify that the object has not already started
5780 * down the termination path, and if it has, will optionally
5781 * wait for that to finish.
5783 * KERN_SUCCESS - we recovered a named reference on the object
5784 * KERN_FAILURE - we could not recover a reference (object dead)
5785 * KERN_INVALID_ARGUMENT - bad memory object control
5788 memory_object_recover_named(
5789 memory_object_control_t control
,
5790 boolean_t wait_on_terminating
)
5794 object
= memory_object_control_to_vm_object(control
);
5795 if (object
== VM_OBJECT_NULL
) {
5796 return (KERN_INVALID_ARGUMENT
);
5799 vm_object_lock(object
);
5801 if (object
->terminating
&& wait_on_terminating
) {
5802 vm_object_wait(object
,
5803 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
5808 if (!object
->alive
) {
5809 vm_object_unlock(object
);
5810 return KERN_FAILURE
;
5813 if (object
->named
== TRUE
) {
5814 vm_object_unlock(object
);
5815 return KERN_SUCCESS
;
5818 if ((object
->ref_count
== 0) && (!object
->terminating
)) {
5819 if (!vm_object_cache_lock_try()) {
5820 vm_object_unlock(object
);
5823 queue_remove(&vm_object_cached_list
, object
,
5824 vm_object_t
, cached_list
);
5825 vm_object_cached_count
--;
5826 XPR(XPR_VM_OBJECT_CACHE
,
5827 "memory_object_recover_named: removing %X, head (%X, %X)\n",
5829 vm_object_cached_list
.next
,
5830 vm_object_cached_list
.prev
, 0,0);
5832 vm_object_cache_unlock();
5835 object
->named
= TRUE
;
5836 vm_object_lock_assert_exclusive(object
);
5837 object
->ref_count
++;
5838 vm_object_res_reference(object
);
5839 while (!object
->pager_ready
) {
5840 vm_object_sleep(object
,
5841 VM_OBJECT_EVENT_PAGER_READY
,
5844 vm_object_unlock(object
);
5845 return (KERN_SUCCESS
);
5850 * vm_object_release_name:
5852 * Enforces name semantic on memory_object reference count decrement
5853 * This routine should not be called unless the caller holds a name
5854 * reference gained through the memory_object_create_named.
5856 * If the TERMINATE_IDLE flag is set, the call will return if the
5857 * reference count is not 1. i.e. idle with the only remaining reference
5859 * If the decision is made to proceed the name field flag is set to
5860 * false and the reference count is decremented. If the RESPECT_CACHE
5861 * flag is set and the reference count has gone to zero, the
5862 * memory_object is checked to see if it is cacheable otherwise when
5863 * the reference count is zero, it is simply terminated.
5866 __private_extern__ kern_return_t
5867 vm_object_release_name(
5872 boolean_t original_object
= TRUE
;
5874 while (object
!= VM_OBJECT_NULL
) {
5876 vm_object_lock(object
);
5878 assert(object
->alive
);
5879 if (original_object
)
5880 assert(object
->named
);
5881 assert(object
->ref_count
> 0);
5884 * We have to wait for initialization before
5885 * destroying or caching the object.
5888 if (object
->pager_created
&& !object
->pager_initialized
) {
5889 assert(!object
->can_persist
);
5890 vm_object_assert_wait(object
,
5891 VM_OBJECT_EVENT_INITIALIZED
,
5893 vm_object_unlock(object
);
5894 thread_block(THREAD_CONTINUE_NULL
);
5898 if (((object
->ref_count
> 1)
5899 && (flags
& MEMORY_OBJECT_TERMINATE_IDLE
))
5900 || (object
->terminating
)) {
5901 vm_object_unlock(object
);
5902 return KERN_FAILURE
;
5904 if (flags
& MEMORY_OBJECT_RELEASE_NO_OP
) {
5905 vm_object_unlock(object
);
5906 return KERN_SUCCESS
;
5910 if ((flags
& MEMORY_OBJECT_RESPECT_CACHE
) &&
5911 (object
->ref_count
== 1)) {
5912 if (original_object
)
5913 object
->named
= FALSE
;
5914 vm_object_unlock(object
);
5915 /* let vm_object_deallocate push this thing into */
5916 /* the cache, if that it is where it is bound */
5917 vm_object_deallocate(object
);
5918 return KERN_SUCCESS
;
5920 VM_OBJ_RES_DECR(object
);
5921 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
5923 if (object
->ref_count
== 1) {
5924 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
5925 if (original_object
) {
5926 return KERN_FAILURE
;
5928 return KERN_SUCCESS
;
5931 if (shadow
!= VM_OBJECT_NULL
) {
5932 original_object
= FALSE
;
5936 return KERN_SUCCESS
;
5938 vm_object_lock_assert_exclusive(object
);
5939 object
->ref_count
--;
5940 assert(object
->ref_count
> 0);
5942 object
->named
= FALSE
;
5943 vm_object_unlock(object
);
5944 return KERN_SUCCESS
;
5949 return KERN_FAILURE
;
5953 __private_extern__ kern_return_t
5954 vm_object_lock_request(
5956 vm_object_offset_t offset
,
5957 vm_object_size_t size
,
5958 memory_object_return_t should_return
,
5962 __unused boolean_t should_flush
;
5964 should_flush
= flags
& MEMORY_OBJECT_DATA_FLUSH
;
5966 XPR(XPR_MEMORY_OBJECT
,
5967 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
5968 object
, offset
, size
,
5969 (((should_return
&1)<<1)|should_flush
), prot
);
5972 * Check for bogus arguments.
5974 if (object
== VM_OBJECT_NULL
)
5975 return (KERN_INVALID_ARGUMENT
);
5977 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
)
5978 return (KERN_INVALID_ARGUMENT
);
5980 size
= round_page_64(size
);
5983 * Lock the object, and acquire a paging reference to
5984 * prevent the memory_object reference from being released.
5986 vm_object_lock(object
);
5987 vm_object_paging_begin(object
);
5989 (void)vm_object_update(object
,
5990 offset
, size
, NULL
, NULL
, should_return
, flags
, prot
);
5992 vm_object_paging_end(object
);
5993 vm_object_unlock(object
);
5995 return (KERN_SUCCESS
);
5999 * Empty a purgeable object by grabbing the physical pages assigned to it and
6000 * putting them on the free queue without writing them to backing store, etc.
6001 * When the pages are next touched they will be demand zero-fill pages. We
6002 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
6003 * skip referenced/dirty pages, pages on the active queue, etc. We're more
6004 * than happy to grab these since this is a purgeable object. We mark the
6005 * object as "empty" after reaping its pages.
6007 * On entry the object must be locked and it must be
6008 * purgeable with no delayed copies pending.
6011 vm_object_purge(vm_object_t object
)
6013 vm_object_lock_assert_exclusive(object
);
6015 if (object
->purgable
== VM_PURGABLE_DENY
)
6018 assert(object
->copy
== VM_OBJECT_NULL
);
6019 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
6021 if(object
->purgable
== VM_PURGABLE_VOLATILE
) {
6023 assert(object
->resident_page_count
>=
6024 object
->wired_page_count
);
6025 delta
= (object
->resident_page_count
-
6026 object
->wired_page_count
);
6028 assert(vm_page_purgeable_count
>=
6031 (SInt32
*)&vm_page_purgeable_count
);
6033 if (object
->wired_page_count
!= 0) {
6034 assert(vm_page_purgeable_wired_count
>=
6035 object
->wired_page_count
);
6036 OSAddAtomic(-object
->wired_page_count
,
6037 (SInt32
*)&vm_page_purgeable_wired_count
);
6040 object
->purgable
= VM_PURGABLE_EMPTY
;
6042 vm_object_reap_pages(object
, REAP_PURGEABLE
);
6047 * vm_object_purgeable_control() allows the caller to control and investigate the
6048 * state of a purgeable object. A purgeable object is created via a call to
6049 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgeable object will
6050 * never be coalesced with any other object -- even other purgeable objects --
6051 * and will thus always remain a distinct object. A purgeable object has
6052 * special semantics when its reference count is exactly 1. If its reference
6053 * count is greater than 1, then a purgeable object will behave like a normal
6054 * object and attempts to use this interface will result in an error return
6055 * of KERN_INVALID_ARGUMENT.
6057 * A purgeable object may be put into a "volatile" state which will make the
6058 * object's pages elligable for being reclaimed without paging to backing
6059 * store if the system runs low on memory. If the pages in a volatile
6060 * purgeable object are reclaimed, the purgeable object is said to have been
6061 * "emptied." When a purgeable object is emptied the system will reclaim as
6062 * many pages from the object as it can in a convenient manner (pages already
6063 * en route to backing store or busy for other reasons are left as is). When
6064 * a purgeable object is made volatile, its pages will generally be reclaimed
6065 * before other pages in the application's working set. This semantic is
6066 * generally used by applications which can recreate the data in the object
6067 * faster than it can be paged in. One such example might be media assets
6068 * which can be reread from a much faster RAID volume.
6070 * A purgeable object may be designated as "non-volatile" which means it will
6071 * behave like all other objects in the system with pages being written to and
6072 * read from backing store as needed to satisfy system memory needs. If the
6073 * object was emptied before the object was made non-volatile, that fact will
6074 * be returned as the old state of the purgeable object (see
6075 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
6076 * were reclaimed as part of emptying the object will be refaulted in as
6077 * zero-fill on demand. It is up to the application to note that an object
6078 * was emptied and recreate the objects contents if necessary. When a
6079 * purgeable object is made non-volatile, its pages will generally not be paged
6080 * out to backing store in the immediate future. A purgeable object may also
6081 * be manually emptied.
6083 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
6084 * volatile purgeable object may be queried at any time. This information may
6085 * be used as a control input to let the application know when the system is
6086 * experiencing memory pressure and is reclaiming memory.
6088 * The specified address may be any address within the purgeable object. If
6089 * the specified address does not represent any object in the target task's
6090 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
6091 * object containing the specified address is not a purgeable object, then
6092 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
6095 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
6096 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
6097 * state is used to set the new state of the purgeable object and return its
6098 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgeable
6099 * object is returned in the parameter state.
6101 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
6102 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
6103 * the non-volatile, volatile and volatile/empty states described above.
6104 * Setting the state of a purgeable object to VM_PURGABLE_EMPTY will
6105 * immediately reclaim as many pages in the object as can be conveniently
6106 * collected (some may have already been written to backing store or be
6109 * The process of making a purgeable object non-volatile and determining its
6110 * previous state is atomic. Thus, if a purgeable object is made
6111 * VM_PURGABLE_NONVOLATILE and the old state is returned as
6112 * VM_PURGABLE_VOLATILE, then the purgeable object's previous contents are
6113 * completely intact and will remain so until the object is made volatile
6114 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
6115 * was reclaimed while it was in a volatile state and its previous contents
6119 * The object must be locked.
6122 vm_object_purgable_control(
6124 vm_purgable_t control
,
6130 if (object
== VM_OBJECT_NULL
) {
6132 * Object must already be present or it can't be purgeable.
6134 return KERN_INVALID_ARGUMENT
;
6138 * Get current state of the purgeable object.
6140 old_state
= object
->purgable
;
6141 if (old_state
== VM_PURGABLE_DENY
)
6142 return KERN_INVALID_ARGUMENT
;
6144 /* purgeable cant have delayed copies - now or in the future */
6145 assert(object
->copy
== VM_OBJECT_NULL
);
6146 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
6149 * Execute the desired operation.
6151 if (control
== VM_PURGABLE_GET_STATE
) {
6153 return KERN_SUCCESS
;
6156 if ((*state
) & VM_PURGABLE_DEBUG_EMPTY
) {
6157 object
->volatile_empty
= TRUE
;
6159 if ((*state
) & VM_PURGABLE_DEBUG_FAULT
) {
6160 object
->volatile_fault
= TRUE
;
6163 new_state
= *state
& VM_PURGABLE_STATE_MASK
;
6164 if (new_state
== VM_PURGABLE_VOLATILE
&&
6165 object
->volatile_empty
) {
6166 new_state
= VM_PURGABLE_EMPTY
;
6169 switch (new_state
) {
6170 case VM_PURGABLE_DENY
:
6171 case VM_PURGABLE_NONVOLATILE
:
6172 object
->purgable
= new_state
;
6174 if (old_state
== VM_PURGABLE_VOLATILE
) {
6177 assert(object
->resident_page_count
>=
6178 object
->wired_page_count
);
6179 delta
= (object
->resident_page_count
-
6180 object
->wired_page_count
);
6182 assert(vm_page_purgeable_count
>= delta
);
6186 (SInt32
*)&vm_page_purgeable_count
);
6188 if (object
->wired_page_count
!= 0) {
6189 assert(vm_page_purgeable_wired_count
>=
6190 object
->wired_page_count
);
6191 OSAddAtomic(-object
->wired_page_count
,
6192 (SInt32
*)&vm_page_purgeable_wired_count
);
6195 vm_page_lock_queues();
6197 assert(object
->objq
.next
!= NULL
&& object
->objq
.prev
!= NULL
); /* object should be on a queue */
6198 purgeable_q_t queue
= vm_purgeable_object_remove(object
);
6201 vm_purgeable_token_delete_first(queue
);
6202 assert(queue
->debug_count_objects
>=0);
6204 vm_page_unlock_queues();
6208 case VM_PURGABLE_VOLATILE
:
6209 if (object
->volatile_fault
) {
6213 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
6219 refmod
= pmap_disconnect(p
->phys_page
);
6220 if ((refmod
& VM_MEM_MODIFIED
) &&
6227 if (old_state
== VM_PURGABLE_EMPTY
&&
6228 object
->resident_page_count
== 0)
6231 purgeable_q_t queue
;
6233 /* find the correct queue */
6234 if ((*state
&VM_PURGABLE_ORDERING_MASK
) == VM_PURGABLE_ORDERING_OBSOLETE
)
6235 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_OBSOLETE
];
6237 if ((*state
&VM_PURGABLE_BEHAVIOR_MASK
) == VM_PURGABLE_BEHAVIOR_FIFO
)
6238 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_FIFO
];
6240 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_LIFO
];
6243 if (old_state
== VM_PURGABLE_NONVOLATILE
||
6244 old_state
== VM_PURGABLE_EMPTY
) {
6247 /* try to add token... this can fail */
6248 vm_page_lock_queues();
6250 kern_return_t result
= vm_purgeable_token_add(queue
);
6251 if (result
!= KERN_SUCCESS
) {
6252 vm_page_unlock_queues();
6255 vm_page_unlock_queues();
6257 assert(object
->resident_page_count
>=
6258 object
->wired_page_count
);
6259 delta
= (object
->resident_page_count
-
6260 object
->wired_page_count
);
6264 &vm_page_purgeable_count
);
6266 if (object
->wired_page_count
!= 0) {
6267 OSAddAtomic(object
->wired_page_count
,
6268 &vm_page_purgeable_wired_count
);
6271 object
->purgable
= new_state
;
6273 /* object should not be on a queue */
6274 assert(object
->objq
.next
== NULL
&& object
->objq
.prev
== NULL
);
6276 else if (old_state
== VM_PURGABLE_VOLATILE
) {
6278 * if reassigning priorities / purgeable groups, we don't change the
6279 * token queue. So moving priorities will not make pages stay around longer.
6280 * Reasoning is that the algorithm gives most priority to the most important
6281 * object. If a new token is added, the most important object' priority is boosted.
6282 * This biases the system already for purgeable queues that move a lot.
6283 * It doesn't seem more biasing is neccessary in this case, where no new object is added.
6285 assert(object
->objq
.next
!= NULL
&& object
->objq
.prev
!= NULL
); /* object should be on a queue */
6287 purgeable_q_t old_queue
=vm_purgeable_object_remove(object
);
6290 if (old_queue
!= queue
) {
6291 kern_return_t result
;
6293 /* Changing queue. Have to move token. */
6294 vm_page_lock_queues();
6295 vm_purgeable_token_delete_first(old_queue
);
6296 result
= vm_purgeable_token_add(queue
);
6297 vm_page_unlock_queues();
6299 assert(result
==KERN_SUCCESS
); /* this should never fail since we just freed a token */
6302 vm_purgeable_object_add(object
, queue
, (*state
&VM_VOLATILE_GROUP_MASK
)>>VM_VOLATILE_GROUP_SHIFT
);
6304 assert(queue
->debug_count_objects
>=0);
6309 case VM_PURGABLE_EMPTY
:
6310 if (object
->volatile_fault
) {
6314 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
6320 refmod
= pmap_disconnect(p
->phys_page
);
6321 if ((refmod
& VM_MEM_MODIFIED
) &&
6328 if (old_state
!= new_state
) {
6329 assert(old_state
== VM_PURGABLE_NONVOLATILE
||
6330 old_state
== VM_PURGABLE_VOLATILE
);
6331 if (old_state
== VM_PURGABLE_VOLATILE
) {
6332 purgeable_q_t old_queue
;
6334 /* object should be on a queue */
6335 assert(object
->objq
.next
!= NULL
&&
6336 object
->objq
.prev
!= NULL
);
6337 old_queue
= vm_purgeable_object_remove(object
);
6339 vm_page_lock_queues();
6340 vm_purgeable_token_delete_first(old_queue
);
6341 vm_page_unlock_queues();
6343 (void) vm_object_purge(object
);
6350 return KERN_SUCCESS
;
6355 * vm_object_res_deallocate
6357 * (recursively) decrement residence counts on vm objects and their shadows.
6358 * Called from vm_object_deallocate and when swapping out an object.
6360 * The object is locked, and remains locked throughout the function,
6361 * even as we iterate down the shadow chain. Locks on intermediate objects
6362 * will be dropped, but not the original object.
6364 * NOTE: this function used to use recursion, rather than iteration.
6367 __private_extern__
void
6368 vm_object_res_deallocate(
6371 vm_object_t orig_object
= object
;
6373 * Object is locked so it can be called directly
6374 * from vm_object_deallocate. Original object is never
6377 assert(object
->res_count
> 0);
6378 while (--object
->res_count
== 0) {
6379 assert(object
->ref_count
>= object
->res_count
);
6380 vm_object_deactivate_all_pages(object
);
6381 /* iterate on shadow, if present */
6382 if (object
->shadow
!= VM_OBJECT_NULL
) {
6383 vm_object_t tmp_object
= object
->shadow
;
6384 vm_object_lock(tmp_object
);
6385 if (object
!= orig_object
)
6386 vm_object_unlock(object
);
6387 object
= tmp_object
;
6388 assert(object
->res_count
> 0);
6392 if (object
!= orig_object
)
6393 vm_object_unlock(object
);
6397 * vm_object_res_reference
6399 * Internal function to increment residence count on a vm object
6400 * and its shadows. It is called only from vm_object_reference, and
6401 * when swapping in a vm object, via vm_map_swap.
6403 * The object is locked, and remains locked throughout the function,
6404 * even as we iterate down the shadow chain. Locks on intermediate objects
6405 * will be dropped, but not the original object.
6407 * NOTE: this function used to use recursion, rather than iteration.
6410 __private_extern__
void
6411 vm_object_res_reference(
6414 vm_object_t orig_object
= object
;
6416 * Object is locked, so this can be called directly
6417 * from vm_object_reference. This lock is never released.
6419 while ((++object
->res_count
== 1) &&
6420 (object
->shadow
!= VM_OBJECT_NULL
)) {
6421 vm_object_t tmp_object
= object
->shadow
;
6423 assert(object
->ref_count
>= object
->res_count
);
6424 vm_object_lock(tmp_object
);
6425 if (object
!= orig_object
)
6426 vm_object_unlock(object
);
6427 object
= tmp_object
;
6429 if (object
!= orig_object
)
6430 vm_object_unlock(object
);
6431 assert(orig_object
->ref_count
>= orig_object
->res_count
);
6433 #endif /* TASK_SWAPPER */
6436 * vm_object_reference:
6438 * Gets another reference to the given object.
6440 #ifdef vm_object_reference
6441 #undef vm_object_reference
6443 __private_extern__
void
6444 vm_object_reference(
6445 register vm_object_t object
)
6447 if (object
== VM_OBJECT_NULL
)
6450 vm_object_lock(object
);
6451 assert(object
->ref_count
> 0);
6452 vm_object_reference_locked(object
);
6453 vm_object_unlock(object
);
6458 * Scale the vm_object_cache
6459 * This is required to make sure that the vm_object_cache is big
6460 * enough to effectively cache the mapped file.
6461 * This is really important with UBC as all the regular file vnodes
6462 * have memory object associated with them. Havving this cache too
6463 * small results in rapid reclaim of vnodes and hurts performance a LOT!
6465 * This is also needed as number of vnodes can be dynamically scaled.
6468 adjust_vm_object_cache(
6469 __unused vm_size_t oval
,
6470 __unused vm_size_t nval
)
6473 vm_object_cached_max
= nval
;
6474 vm_object_cache_trim(FALSE
);
6476 return (KERN_SUCCESS
);
6478 #endif /* MACH_BSD */
6482 * vm_object_transpose
6484 * This routine takes two VM objects of the same size and exchanges
6485 * their backing store.
6486 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
6487 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
6489 * The VM objects must not be locked by caller.
6491 unsigned int vm_object_transpose_count
= 0;
6493 vm_object_transpose(
6494 vm_object_t object1
,
6495 vm_object_t object2
,
6496 vm_object_size_t transpose_size
)
6498 vm_object_t tmp_object
;
6499 kern_return_t retval
;
6500 boolean_t object1_locked
, object2_locked
;
6502 vm_object_offset_t page_offset
;
6503 lck_mtx_t
*hash_lck
;
6504 vm_object_hash_entry_t hash_entry
;
6506 tmp_object
= VM_OBJECT_NULL
;
6507 object1_locked
= FALSE
; object2_locked
= FALSE
;
6509 if (object1
== object2
||
6510 object1
== VM_OBJECT_NULL
||
6511 object2
== VM_OBJECT_NULL
) {
6513 * If the 2 VM objects are the same, there's
6514 * no point in exchanging their backing store.
6516 retval
= KERN_INVALID_VALUE
;
6521 * Since we need to lock both objects at the same time,
6522 * make sure we always lock them in the same order to
6525 if (object1
> object2
) {
6526 tmp_object
= object1
;
6528 object2
= tmp_object
;
6532 * Allocate a temporary VM object to hold object1's contents
6533 * while we copy object2 to object1.
6535 tmp_object
= vm_object_allocate(transpose_size
);
6536 vm_object_lock(tmp_object
);
6537 tmp_object
->can_persist
= FALSE
;
6541 * Grab control of the 1st VM object.
6543 vm_object_lock(object1
);
6544 object1_locked
= TRUE
;
6545 if (!object1
->alive
|| object1
->terminating
||
6546 object1
->copy
|| object1
->shadow
|| object1
->shadowed
||
6547 object1
->purgable
!= VM_PURGABLE_DENY
) {
6549 * We don't deal with copy or shadow objects (yet).
6551 retval
= KERN_INVALID_VALUE
;
6555 * We're about to mess with the object's backing store and
6556 * taking a "paging_in_progress" reference wouldn't be enough
6557 * to prevent any paging activity on this object, so the caller should
6558 * have "quiesced" the objects beforehand, via a UPL operation with
6559 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
6560 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
6562 * Wait for any paging operation to complete (but only paging, not
6563 * other kind of activities not linked to the pager). After we're
6564 * statisfied that there's no more paging in progress, we keep the
6565 * object locked, to guarantee that no one tries to access its pager.
6567 vm_object_paging_only_wait(object1
, THREAD_UNINT
);
6570 * Same as above for the 2nd object...
6572 vm_object_lock(object2
);
6573 object2_locked
= TRUE
;
6574 if (! object2
->alive
|| object2
->terminating
||
6575 object2
->copy
|| object2
->shadow
|| object2
->shadowed
||
6576 object2
->purgable
!= VM_PURGABLE_DENY
) {
6577 retval
= KERN_INVALID_VALUE
;
6580 vm_object_paging_only_wait(object2
, THREAD_UNINT
);
6583 if (object1
->size
!= object2
->size
||
6584 object1
->size
!= transpose_size
) {
6586 * If the 2 objects don't have the same size, we can't
6587 * exchange their backing stores or one would overflow.
6588 * If their size doesn't match the caller's
6589 * "transpose_size", we can't do it either because the
6590 * transpose operation will affect the entire span of
6593 retval
= KERN_INVALID_VALUE
;
6599 * Transpose the lists of resident pages.
6600 * This also updates the resident_page_count and the memq_hint.
6602 if (object1
->phys_contiguous
|| queue_empty(&object1
->memq
)) {
6604 * No pages in object1, just transfer pages
6605 * from object2 to object1. No need to go through
6606 * an intermediate object.
6608 while (!queue_empty(&object2
->memq
)) {
6609 page
= (vm_page_t
) queue_first(&object2
->memq
);
6610 vm_page_rename(page
, object1
, page
->offset
, FALSE
);
6612 assert(queue_empty(&object2
->memq
));
6613 } else if (object2
->phys_contiguous
|| queue_empty(&object2
->memq
)) {
6615 * No pages in object2, just transfer pages
6616 * from object1 to object2. No need to go through
6617 * an intermediate object.
6619 while (!queue_empty(&object1
->memq
)) {
6620 page
= (vm_page_t
) queue_first(&object1
->memq
);
6621 vm_page_rename(page
, object2
, page
->offset
, FALSE
);
6623 assert(queue_empty(&object1
->memq
));
6625 /* transfer object1's pages to tmp_object */
6626 while (!queue_empty(&object1
->memq
)) {
6627 page
= (vm_page_t
) queue_first(&object1
->memq
);
6628 page_offset
= page
->offset
;
6629 vm_page_remove(page
, TRUE
);
6630 page
->offset
= page_offset
;
6631 queue_enter(&tmp_object
->memq
, page
, vm_page_t
, listq
);
6633 assert(queue_empty(&object1
->memq
));
6634 /* transfer object2's pages to object1 */
6635 while (!queue_empty(&object2
->memq
)) {
6636 page
= (vm_page_t
) queue_first(&object2
->memq
);
6637 vm_page_rename(page
, object1
, page
->offset
, FALSE
);
6639 assert(queue_empty(&object2
->memq
));
6640 /* transfer tmp_object's pages to object1 */
6641 while (!queue_empty(&tmp_object
->memq
)) {
6642 page
= (vm_page_t
) queue_first(&tmp_object
->memq
);
6643 queue_remove(&tmp_object
->memq
, page
,
6645 vm_page_insert(page
, object2
, page
->offset
);
6647 assert(queue_empty(&tmp_object
->memq
));
6650 #define __TRANSPOSE_FIELD(field) \
6652 tmp_object->field = object1->field; \
6653 object1->field = object2->field; \
6654 object2->field = tmp_object->field; \
6657 /* "Lock" refers to the object not its contents */
6658 /* "size" should be identical */
6659 assert(object1
->size
== object2
->size
);
6660 /* "memq_hint" was updated above when transposing pages */
6661 /* "ref_count" refers to the object not its contents */
6663 /* "res_count" refers to the object not its contents */
6665 /* "resident_page_count" was updated above when transposing pages */
6666 /* "wired_page_count" was updated above when transposing pages */
6667 /* "reusable_page_count" was updated above when transposing pages */
6668 /* there should be no "copy" */
6669 assert(!object1
->copy
);
6670 assert(!object2
->copy
);
6671 /* there should be no "shadow" */
6672 assert(!object1
->shadow
);
6673 assert(!object2
->shadow
);
6674 __TRANSPOSE_FIELD(shadow_offset
); /* used by phys_contiguous objects */
6675 __TRANSPOSE_FIELD(pager
);
6676 __TRANSPOSE_FIELD(paging_offset
);
6677 __TRANSPOSE_FIELD(pager_control
);
6678 /* update the memory_objects' pointers back to the VM objects */
6679 if (object1
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6680 memory_object_control_collapse(object1
->pager_control
,
6683 if (object2
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6684 memory_object_control_collapse(object2
->pager_control
,
6687 __TRANSPOSE_FIELD(copy_strategy
);
6688 /* "paging_in_progress" refers to the object not its contents */
6689 assert(!object1
->paging_in_progress
);
6690 assert(!object2
->paging_in_progress
);
6691 assert(object1
->activity_in_progress
);
6692 assert(object2
->activity_in_progress
);
6693 /* "all_wanted" refers to the object not its contents */
6694 __TRANSPOSE_FIELD(pager_created
);
6695 __TRANSPOSE_FIELD(pager_initialized
);
6696 __TRANSPOSE_FIELD(pager_ready
);
6697 __TRANSPOSE_FIELD(pager_trusted
);
6698 __TRANSPOSE_FIELD(can_persist
);
6699 __TRANSPOSE_FIELD(internal
);
6700 __TRANSPOSE_FIELD(temporary
);
6701 __TRANSPOSE_FIELD(private);
6702 __TRANSPOSE_FIELD(pageout
);
6703 /* "alive" should be set */
6704 assert(object1
->alive
);
6705 assert(object2
->alive
);
6706 /* "purgeable" should be non-purgeable */
6707 assert(object1
->purgable
== VM_PURGABLE_DENY
);
6708 assert(object2
->purgable
== VM_PURGABLE_DENY
);
6709 /* "shadowed" refers to the the object not its contents */
6710 __TRANSPOSE_FIELD(silent_overwrite
);
6711 __TRANSPOSE_FIELD(advisory_pageout
);
6712 __TRANSPOSE_FIELD(true_share
);
6713 /* "terminating" should not be set */
6714 assert(!object1
->terminating
);
6715 assert(!object2
->terminating
);
6716 __TRANSPOSE_FIELD(named
);
6717 /* "shadow_severed" refers to the object not its contents */
6718 __TRANSPOSE_FIELD(phys_contiguous
);
6719 __TRANSPOSE_FIELD(nophyscache
);
6720 /* "cached_list.next" points to transposed object */
6721 object1
->cached_list
.next
= (queue_entry_t
) object2
;
6722 object2
->cached_list
.next
= (queue_entry_t
) object1
;
6723 /* "cached_list.prev" should be NULL */
6724 assert(object1
->cached_list
.prev
== NULL
);
6725 assert(object2
->cached_list
.prev
== NULL
);
6726 /* "msr_q" is linked to the object not its contents */
6727 assert(queue_empty(&object1
->msr_q
));
6728 assert(queue_empty(&object2
->msr_q
));
6729 __TRANSPOSE_FIELD(last_alloc
);
6730 __TRANSPOSE_FIELD(sequential
);
6731 __TRANSPOSE_FIELD(pages_created
);
6732 __TRANSPOSE_FIELD(pages_used
);
6734 __TRANSPOSE_FIELD(existence_map
);
6736 __TRANSPOSE_FIELD(cow_hint
);
6738 __TRANSPOSE_FIELD(paging_object
);
6740 __TRANSPOSE_FIELD(wimg_bits
);
6741 __TRANSPOSE_FIELD(code_signed
);
6742 if (object1
->hashed
) {
6743 hash_lck
= vm_object_hash_lock_spin(object2
->pager
);
6744 hash_entry
= vm_object_hash_lookup(object2
->pager
, FALSE
);
6745 assert(hash_entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
6746 hash_entry
->object
= object2
;
6747 vm_object_hash_unlock(hash_lck
);
6749 if (object2
->hashed
) {
6750 hash_lck
= vm_object_hash_lock_spin(object1
->pager
);
6751 hash_entry
= vm_object_hash_lookup(object1
->pager
, FALSE
);
6752 assert(hash_entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
6753 hash_entry
->object
= object1
;
6754 vm_object_hash_unlock(hash_lck
);
6756 __TRANSPOSE_FIELD(hashed
);
6757 object1
->transposed
= TRUE
;
6758 object2
->transposed
= TRUE
;
6759 __TRANSPOSE_FIELD(mapping_in_progress
);
6760 __TRANSPOSE_FIELD(volatile_empty
);
6761 __TRANSPOSE_FIELD(volatile_fault
);
6762 __TRANSPOSE_FIELD(all_reusable
);
6763 assert(object1
->blocked_access
);
6764 assert(object2
->blocked_access
);
6765 assert(object1
->__object2_unused_bits
== 0);
6766 assert(object2
->__object2_unused_bits
== 0);
6768 /* "uplq" refers to the object not its contents (see upl_transpose()) */
6770 assert(object1
->objq
.next
== NULL
);
6771 assert(object1
->objq
.prev
== NULL
);
6772 assert(object2
->objq
.next
== NULL
);
6773 assert(object2
->objq
.prev
== NULL
);
6775 #undef __TRANSPOSE_FIELD
6777 retval
= KERN_SUCCESS
;
6783 if (tmp_object
!= VM_OBJECT_NULL
) {
6784 vm_object_unlock(tmp_object
);
6786 * Re-initialize the temporary object to avoid
6787 * deallocating a real pager.
6789 _vm_object_allocate(transpose_size
, tmp_object
);
6790 vm_object_deallocate(tmp_object
);
6791 tmp_object
= VM_OBJECT_NULL
;
6794 if (object1_locked
) {
6795 vm_object_unlock(object1
);
6796 object1_locked
= FALSE
;
6798 if (object2_locked
) {
6799 vm_object_unlock(object2
);
6800 object2_locked
= FALSE
;
6803 vm_object_transpose_count
++;
6810 * vm_object_cluster_size
6812 * Determine how big a cluster we should issue an I/O for...
6814 * Inputs: *start == offset of page needed
6815 * *length == maximum cluster pager can handle
6816 * Outputs: *start == beginning offset of cluster
6817 * *length == length of cluster to try
6819 * The original *start will be encompassed by the cluster
6822 extern int speculative_reads_disabled
;
6824 unsigned int preheat_pages_max
= MAX_UPL_TRANSFER
;
6825 unsigned int preheat_pages_min
= 8;
6826 unsigned int preheat_pages_mult
= 4;
6828 unsigned int preheat_pages_max
= MAX_UPL_TRANSFER
;
6829 unsigned int preheat_pages_min
= 8;
6830 unsigned int preheat_pages_mult
= 4;
6833 uint32_t pre_heat_scaling
[MAX_UPL_TRANSFER
+ 1];
6834 uint32_t pre_heat_cluster
[MAX_UPL_TRANSFER
+ 1];
6837 __private_extern__
void
6838 vm_object_cluster_size(vm_object_t object
, vm_object_offset_t
*start
,
6839 vm_size_t
*length
, vm_object_fault_info_t fault_info
, uint32_t *io_streaming
)
6841 vm_size_t pre_heat_size
;
6842 vm_size_t tail_size
;
6843 vm_size_t head_size
;
6844 vm_size_t max_length
;
6845 vm_size_t cluster_size
;
6846 vm_object_offset_t object_size
;
6847 vm_object_offset_t orig_start
;
6848 vm_object_offset_t target_start
;
6849 vm_object_offset_t offset
;
6850 vm_behavior_t behavior
;
6851 boolean_t look_behind
= TRUE
;
6852 boolean_t look_ahead
= TRUE
;
6853 uint32_t throttle_limit
;
6855 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6856 unsigned int max_ph_size
;
6857 unsigned int min_ph_size
;
6858 unsigned int ph_mult
;
6860 assert( !(*length
& PAGE_MASK
));
6861 assert( !(*start
& PAGE_MASK_64
));
6863 if ( (ph_mult
= preheat_pages_mult
) < 1 )
6865 if ( (min_ph_size
= preheat_pages_min
) < 1 )
6867 if ( (max_ph_size
= preheat_pages_max
) > MAX_UPL_TRANSFER
)
6868 max_ph_size
= MAX_UPL_TRANSFER
;
6870 if ( (max_length
= *length
) > (max_ph_size
* PAGE_SIZE
) )
6871 max_length
= (max_ph_size
* PAGE_SIZE
);
6874 * we'll always return a cluster size of at least
6875 * 1 page, since the original fault must always
6878 *length
= PAGE_SIZE
;
6881 if (speculative_reads_disabled
|| fault_info
== NULL
|| max_length
== 0) {
6883 * no cluster... just fault the page in
6887 orig_start
= *start
;
6888 target_start
= orig_start
;
6889 cluster_size
= round_page(fault_info
->cluster_size
);
6890 behavior
= fault_info
->behavior
;
6892 vm_object_lock(object
);
6894 if (object
->internal
)
6895 object_size
= object
->size
;
6896 else if (object
->pager
!= MEMORY_OBJECT_NULL
)
6897 vnode_pager_get_object_size(object
->pager
, &object_size
);
6899 goto out
; /* pager is gone for this object, nothing more to do */
6901 object_size
= round_page_64(object_size
);
6903 if (orig_start
>= object_size
) {
6905 * fault occurred beyond the EOF...
6906 * we need to punt w/o changing the
6911 if (object
->pages_used
> object
->pages_created
) {
6913 * must have wrapped our 32 bit counters
6916 object
->pages_used
= object
->pages_created
= 0;
6918 if ((sequential_run
= object
->sequential
)) {
6919 if (sequential_run
< 0) {
6920 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
6921 sequential_run
= 0 - sequential_run
;
6923 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6930 behavior
= VM_BEHAVIOR_DEFAULT
;
6932 case VM_BEHAVIOR_DEFAULT
:
6933 if (object
->internal
&& fault_info
->user_tag
== VM_MEMORY_STACK
)
6936 if (sequential_run
>= (3 * PAGE_SIZE
)) {
6937 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6939 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
)
6940 look_behind
= FALSE
;
6947 if (object
->pages_created
< 32 * ph_mult
) {
6951 pre_heat_size
= PAGE_SIZE
* 8 * ph_mult
;
6955 * Linear growth in PH size: The maximum size is max_length...
6956 * this cacluation will result in a size that is neither a
6957 * power of 2 nor a multiple of PAGE_SIZE... so round
6958 * it up to the nearest PAGE_SIZE boundary
6960 pre_heat_size
= (ph_mult
* (max_length
* object
->pages_used
) / object
->pages_created
);
6962 if (pre_heat_size
< PAGE_SIZE
* min_ph_size
)
6963 pre_heat_size
= PAGE_SIZE
* min_ph_size
;
6965 pre_heat_size
= round_page(pre_heat_size
);
6969 case VM_BEHAVIOR_RANDOM
:
6970 if ((pre_heat_size
= cluster_size
) <= PAGE_SIZE
)
6974 case VM_BEHAVIOR_SEQUENTIAL
:
6975 if ((pre_heat_size
= cluster_size
) == 0)
6976 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6977 look_behind
= FALSE
;
6982 case VM_BEHAVIOR_RSEQNTL
:
6983 if ((pre_heat_size
= cluster_size
) == 0)
6984 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6991 throttle_limit
= (uint32_t) max_length
;
6992 assert(throttle_limit
== max_length
);
6994 if (vnode_pager_check_hard_throttle(object
->pager
, &throttle_limit
, *io_streaming
) == KERN_SUCCESS
) {
6995 if (max_length
> throttle_limit
)
6996 max_length
= throttle_limit
;
6998 if (pre_heat_size
> max_length
)
6999 pre_heat_size
= max_length
;
7001 if (behavior
== VM_BEHAVIOR_DEFAULT
) {
7002 if (vm_page_free_count
< vm_page_throttle_limit
)
7003 pre_heat_size
= trunc_page(pre_heat_size
/ 8);
7004 else if (vm_page_free_count
< vm_page_free_target
)
7005 pre_heat_size
= trunc_page(pre_heat_size
/ 2);
7007 if (pre_heat_size
<= PAGE_SIZE
)
7010 if (look_ahead
== TRUE
) {
7011 if (look_behind
== TRUE
) {
7013 * if we get here its due to a random access...
7014 * so we want to center the original fault address
7015 * within the cluster we will issue... make sure
7016 * to calculate 'head_size' as a multiple of PAGE_SIZE...
7017 * 'pre_heat_size' is a multiple of PAGE_SIZE but not
7018 * necessarily an even number of pages so we need to truncate
7019 * the result to a PAGE_SIZE boundary
7021 head_size
= trunc_page(pre_heat_size
/ 2);
7023 if (target_start
> head_size
)
7024 target_start
-= head_size
;
7029 * 'target_start' at this point represents the beginning offset
7030 * of the cluster we are considering... 'orig_start' will be in
7031 * the center of this cluster if we didn't have to clip the start
7032 * due to running into the start of the file
7035 if ((target_start
+ pre_heat_size
) > object_size
)
7036 pre_heat_size
= (vm_size_t
)(round_page_64(object_size
- target_start
));
7038 * at this point caclulate the number of pages beyond the original fault
7039 * address that we want to consider... this is guaranteed not to extend beyond
7040 * the current EOF...
7042 assert((vm_size_t
)(orig_start
- target_start
) == (orig_start
- target_start
));
7043 tail_size
= pre_heat_size
- (vm_size_t
)(orig_start
- target_start
) - PAGE_SIZE
;
7045 if (pre_heat_size
> target_start
)
7046 pre_heat_size
= (vm_size_t
) target_start
; /* XXX: 32-bit vs 64-bit ? Joe ? */
7049 assert( !(target_start
& PAGE_MASK_64
));
7050 assert( !(pre_heat_size
& PAGE_MASK
));
7052 pre_heat_scaling
[pre_heat_size
/ PAGE_SIZE
]++;
7054 if (pre_heat_size
<= PAGE_SIZE
)
7057 if (look_behind
== TRUE
) {
7059 * take a look at the pages before the original
7060 * faulting offset... recalculate this in case
7061 * we had to clip 'pre_heat_size' above to keep
7062 * from running past the EOF.
7064 head_size
= pre_heat_size
- tail_size
- PAGE_SIZE
;
7066 for (offset
= orig_start
- PAGE_SIZE_64
; head_size
; offset
-= PAGE_SIZE_64
, head_size
-= PAGE_SIZE
) {
7068 * don't poke below the lowest offset
7070 if (offset
< fault_info
->lo_offset
)
7073 * for external objects and internal objects w/o an existence map
7074 * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN
7077 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
7079 * we know for a fact that the pager can't provide the page
7080 * so don't include it or any pages beyond it in this cluster
7085 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
7087 * don't bridge resident pages
7092 *length
+= PAGE_SIZE
;
7095 if (look_ahead
== TRUE
) {
7096 for (offset
= orig_start
+ PAGE_SIZE_64
; tail_size
; offset
+= PAGE_SIZE_64
, tail_size
-= PAGE_SIZE
) {
7098 * don't poke above the highest offset
7100 if (offset
>= fault_info
->hi_offset
)
7102 assert(offset
< object_size
);
7105 * for external objects and internal objects w/o an existence map
7106 * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN
7109 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
7111 * we know for a fact that the pager can't provide the page
7112 * so don't include it or any pages beyond it in this cluster
7117 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
7119 * don't bridge resident pages
7123 *length
+= PAGE_SIZE
;
7127 if (*length
> max_length
)
7128 *length
= max_length
;
7130 pre_heat_cluster
[*length
/ PAGE_SIZE
]++;
7132 vm_object_unlock(object
);
7137 * Allow manipulation of individual page state. This is actually part of
7138 * the UPL regimen but takes place on the VM object rather than on a UPL
7144 vm_object_offset_t offset
,
7146 ppnum_t
*phys_entry
,
7151 vm_object_lock(object
);
7153 if(ops
& UPL_POP_PHYSICAL
) {
7154 if(object
->phys_contiguous
) {
7156 *phys_entry
= (ppnum_t
)
7157 (object
->shadow_offset
>> PAGE_SHIFT
);
7159 vm_object_unlock(object
);
7160 return KERN_SUCCESS
;
7162 vm_object_unlock(object
);
7163 return KERN_INVALID_OBJECT
;
7166 if(object
->phys_contiguous
) {
7167 vm_object_unlock(object
);
7168 return KERN_INVALID_OBJECT
;
7172 if((dst_page
= vm_page_lookup(object
,offset
)) == VM_PAGE_NULL
) {
7173 vm_object_unlock(object
);
7174 return KERN_FAILURE
;
7177 /* Sync up on getting the busy bit */
7178 if((dst_page
->busy
|| dst_page
->cleaning
) &&
7179 (((ops
& UPL_POP_SET
) &&
7180 (ops
& UPL_POP_BUSY
)) || (ops
& UPL_POP_DUMP
))) {
7181 /* someone else is playing with the page, we will */
7183 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7187 if (ops
& UPL_POP_DUMP
) {
7188 if (dst_page
->pmapped
== TRUE
)
7189 pmap_disconnect(dst_page
->phys_page
);
7191 VM_PAGE_FREE(dst_page
);
7198 /* Get the condition of flags before requested ops */
7199 /* are undertaken */
7201 if(dst_page
->dirty
) *flags
|= UPL_POP_DIRTY
;
7202 if(dst_page
->pageout
) *flags
|= UPL_POP_PAGEOUT
;
7203 if(dst_page
->precious
) *flags
|= UPL_POP_PRECIOUS
;
7204 if(dst_page
->absent
) *flags
|= UPL_POP_ABSENT
;
7205 if(dst_page
->busy
) *flags
|= UPL_POP_BUSY
;
7208 /* The caller should have made a call either contingent with */
7209 /* or prior to this call to set UPL_POP_BUSY */
7210 if(ops
& UPL_POP_SET
) {
7211 /* The protection granted with this assert will */
7212 /* not be complete. If the caller violates the */
7213 /* convention and attempts to change page state */
7214 /* without first setting busy we may not see it */
7215 /* because the page may already be busy. However */
7216 /* if such violations occur we will assert sooner */
7218 assert(dst_page
->busy
|| (ops
& UPL_POP_BUSY
));
7219 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= TRUE
;
7220 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= TRUE
;
7221 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= TRUE
;
7222 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= TRUE
;
7223 if (ops
& UPL_POP_BUSY
) dst_page
->busy
= TRUE
;
7226 if(ops
& UPL_POP_CLR
) {
7227 assert(dst_page
->busy
);
7228 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= FALSE
;
7229 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= FALSE
;
7230 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= FALSE
;
7231 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= FALSE
;
7232 if (ops
& UPL_POP_BUSY
) {
7233 dst_page
->busy
= FALSE
;
7234 PAGE_WAKEUP(dst_page
);
7238 if (dst_page
->encrypted
) {
7241 * We need to decrypt this encrypted page before the
7242 * caller can access its contents.
7243 * But if the caller really wants to access the page's
7244 * contents, they have to keep the page "busy".
7245 * Otherwise, the page could get recycled or re-encrypted
7248 if ((ops
& UPL_POP_SET
) && (ops
& UPL_POP_BUSY
) &&
7251 * The page is stable enough to be accessed by
7252 * the caller, so make sure its contents are
7255 vm_page_decrypt(dst_page
, 0);
7258 * The page is not busy, so don't bother
7259 * decrypting it, since anything could
7260 * happen to it between now and when the
7261 * caller wants to access it.
7262 * We should not give the caller access
7265 assert(!phys_entry
);
7271 * The physical page number will remain valid
7272 * only if the page is kept busy.
7273 * ENCRYPTED SWAP: make sure we don't let the
7274 * caller access an encrypted page.
7276 assert(dst_page
->busy
);
7277 assert(!dst_page
->encrypted
);
7278 *phys_entry
= dst_page
->phys_page
;
7284 vm_object_unlock(object
);
7285 return KERN_SUCCESS
;
7290 * vm_object_range_op offers performance enhancement over
7291 * vm_object_page_op for page_op functions which do not require page
7292 * level state to be returned from the call. Page_op was created to provide
7293 * a low-cost alternative to page manipulation via UPLs when only a single
7294 * page was involved. The range_op call establishes the ability in the _op
7295 * family of functions to work on multiple pages where the lack of page level
7296 * state handling allows the caller to avoid the overhead of the upl structures.
7302 vm_object_offset_t offset_beg
,
7303 vm_object_offset_t offset_end
,
7307 vm_object_offset_t offset
;
7310 if (offset_end
- offset_beg
> (uint32_t) -1) {
7311 /* range is too big and would overflow "*range" */
7312 return KERN_INVALID_ARGUMENT
;
7314 if (object
->resident_page_count
== 0) {
7316 if (ops
& UPL_ROP_PRESENT
) {
7319 *range
= (uint32_t) (offset_end
- offset_beg
);
7320 assert(*range
== (offset_end
- offset_beg
));
7323 return KERN_SUCCESS
;
7325 vm_object_lock(object
);
7327 if (object
->phys_contiguous
) {
7328 vm_object_unlock(object
);
7329 return KERN_INVALID_OBJECT
;
7332 offset
= offset_beg
& ~PAGE_MASK_64
;
7334 while (offset
< offset_end
) {
7335 dst_page
= vm_page_lookup(object
, offset
);
7336 if (dst_page
!= VM_PAGE_NULL
) {
7337 if (ops
& UPL_ROP_DUMP
) {
7338 if (dst_page
->busy
|| dst_page
->cleaning
) {
7340 * someone else is playing with the
7341 * page, we will have to wait
7343 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7345 * need to relook the page up since it's
7346 * state may have changed while we slept
7347 * it might even belong to a different object
7352 if (dst_page
->pmapped
== TRUE
)
7353 pmap_disconnect(dst_page
->phys_page
);
7355 VM_PAGE_FREE(dst_page
);
7357 } else if ((ops
& UPL_ROP_ABSENT
) && !dst_page
->absent
)
7359 } else if (ops
& UPL_ROP_PRESENT
)
7362 offset
+= PAGE_SIZE
;
7364 vm_object_unlock(object
);
7367 if (offset
> offset_end
)
7368 offset
= offset_end
;
7369 if(offset
> offset_beg
) {
7370 *range
= (uint32_t) (offset
- offset_beg
);
7371 assert(*range
== (offset
- offset_beg
));
7376 return KERN_SUCCESS
;
7380 uint32_t scan_object_collision
= 0;
7383 vm_object_lock(vm_object_t object
)
7385 if (object
== vm_pageout_scan_wants_object
) {
7386 scan_object_collision
++;
7389 lck_rw_lock_exclusive(&object
->Lock
);
7393 vm_object_lock_avoid(vm_object_t object
)
7395 if (object
== vm_pageout_scan_wants_object
) {
7396 scan_object_collision
++;
7403 _vm_object_lock_try(vm_object_t object
)
7405 return (lck_rw_try_lock_exclusive(&object
->Lock
));
7409 vm_object_lock_try(vm_object_t object
)
7411 if (vm_object_lock_avoid(object
)) {
7414 return _vm_object_lock_try(object
);
7417 vm_object_lock_shared(vm_object_t object
)
7419 if (vm_object_lock_avoid(object
)) {
7422 lck_rw_lock_shared(&object
->Lock
);
7426 vm_object_lock_try_shared(vm_object_t object
)
7428 if (vm_object_lock_avoid(object
)) {
7431 return (lck_rw_try_lock_shared(&object
->Lock
));