2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * File: vm/vm_object.c
54 * Author: Avadis Tevanian, Jr., Michael Wayne Young
56 * Virtual memory object module.
59 #include <mach_pagemap.h>
60 #include <task_swapper.h>
62 #include <mach/mach_types.h>
63 #include <mach/memory_object.h>
64 #include <mach/memory_object_default.h>
65 #include <mach/memory_object_control_server.h>
66 #include <mach/vm_param.h>
68 #include <ipc/ipc_types.h>
69 #include <ipc/ipc_port.h>
71 #include <kern/kern_types.h>
72 #include <kern/assert.h>
73 #include <kern/lock.h>
74 #include <kern/queue.h>
76 #include <kern/zalloc.h>
77 #include <kern/host.h>
78 #include <kern/host_statistics.h>
79 #include <kern/processor.h>
80 #include <kern/misc_protos.h>
82 #include <vm/memory_object.h>
83 #include <vm/vm_fault.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_pageout.h>
88 #include <vm/vm_protos.h>
91 * Virtual memory objects maintain the actual data
92 * associated with allocated virtual memory. A given
93 * page of memory exists within exactly one object.
95 * An object is only deallocated when all "references"
98 * Associated with each object is a list of all resident
99 * memory pages belonging to that object; this list is
100 * maintained by the "vm_page" module, but locked by the object's
103 * Each object also records the memory object reference
104 * that is used by the kernel to request and write
105 * back data (the memory object, field "pager"), etc...
107 * Virtual memory objects are allocated to provide
108 * zero-filled memory (vm_allocate) or map a user-defined
109 * memory object into a virtual address space (vm_map).
111 * Virtual memory objects that refer to a user-defined
112 * memory object are called "permanent", because all changes
113 * made in virtual memory are reflected back to the
114 * memory manager, which may then store it permanently.
115 * Other virtual memory objects are called "temporary",
116 * meaning that changes need be written back only when
117 * necessary to reclaim pages, and that storage associated
118 * with the object can be discarded once it is no longer
121 * A permanent memory object may be mapped into more
122 * than one virtual address space. Moreover, two threads
123 * may attempt to make the first mapping of a memory
124 * object concurrently. Only one thread is allowed to
125 * complete this mapping; all others wait for the
126 * "pager_initialized" field is asserted, indicating
127 * that the first thread has initialized all of the
128 * necessary fields in the virtual memory object structure.
130 * The kernel relies on a *default memory manager* to
131 * provide backing storage for the zero-filled virtual
132 * memory objects. The pager memory objects associated
133 * with these temporary virtual memory objects are only
134 * requested from the default memory manager when it
135 * becomes necessary. Virtual memory objects
136 * that depend on the default memory manager are called
137 * "internal". The "pager_created" field is provided to
138 * indicate whether these ports have ever been allocated.
140 * The kernel may also create virtual memory objects to
141 * hold changed pages after a copy-on-write operation.
142 * In this case, the virtual memory object (and its
143 * backing storage -- its memory object) only contain
144 * those pages that have been changed. The "shadow"
145 * field refers to the virtual memory object that contains
146 * the remainder of the contents. The "shadow_offset"
147 * field indicates where in the "shadow" these contents begin.
148 * The "copy" field refers to a virtual memory object
149 * to which changed pages must be copied before changing
150 * this object, in order to implement another form
151 * of copy-on-write optimization.
153 * The virtual memory object structure also records
154 * the attributes associated with its memory object.
155 * The "pager_ready", "can_persist" and "copy_strategy"
156 * fields represent those attributes. The "cached_list"
157 * field is used in the implementation of the persistence
160 * ZZZ Continue this comment.
163 /* Forward declarations for internal functions. */
164 static kern_return_t
vm_object_terminate(
167 extern void vm_object_remove(
170 static vm_object_t
vm_object_cache_trim(
171 boolean_t called_from_vm_object_deallocate
);
173 static void vm_object_deactivate_all_pages(
176 static kern_return_t
vm_object_copy_call(
177 vm_object_t src_object
,
178 vm_object_offset_t src_offset
,
179 vm_object_size_t size
,
180 vm_object_t
*_result_object
);
182 static void vm_object_do_collapse(
184 vm_object_t backing_object
);
186 static void vm_object_do_bypass(
188 vm_object_t backing_object
);
190 static void vm_object_release_pager(
191 memory_object_t pager
);
193 static zone_t vm_object_zone
; /* vm backing store zone */
196 * All wired-down kernel memory belongs to a single virtual
197 * memory object (kernel_object) to avoid wasting data structures.
199 static struct vm_object kernel_object_store
;
200 __private_extern__ vm_object_t kernel_object
= &kernel_object_store
;
203 * The submap object is used as a placeholder for vm_map_submap
204 * operations. The object is declared in vm_map.c because it
205 * is exported by the vm_map module. The storage is declared
206 * here because it must be initialized here.
208 static struct vm_object vm_submap_object_store
;
211 * Virtual memory objects are initialized from
212 * a template (see vm_object_allocate).
214 * When adding a new field to the virtual memory
215 * object structure, be sure to add initialization
216 * (see _vm_object_allocate()).
218 static struct vm_object vm_object_template
;
221 * Virtual memory objects that are not referenced by
222 * any address maps, but that are allowed to persist
223 * (an attribute specified by the associated memory manager),
224 * are kept in a queue (vm_object_cached_list).
226 * When an object from this queue is referenced again,
227 * for example to make another address space mapping,
228 * it must be removed from the queue. That is, the
229 * queue contains *only* objects with zero references.
231 * The kernel may choose to terminate objects from this
232 * queue in order to reclaim storage. The current policy
233 * is to permit a fixed maximum number of unreferenced
234 * objects (vm_object_cached_max).
236 * A spin lock (accessed by routines
237 * vm_object_cache_{lock,lock_try,unlock}) governs the
238 * object cache. It must be held when objects are
239 * added to or removed from the cache (in vm_object_terminate).
240 * The routines that acquire a reference to a virtual
241 * memory object based on one of the memory object ports
242 * must also lock the cache.
244 * Ideally, the object cache should be more isolated
245 * from the reference mechanism, so that the lock need
246 * not be held to make simple references.
248 static queue_head_t vm_object_cached_list
;
249 static int vm_object_cached_count
=0;
250 static int vm_object_cached_high
; /* highest # cached objects */
251 static int vm_object_cached_max
= 512; /* may be patched*/
253 static decl_mutex_data(,vm_object_cached_lock_data
)
255 #define vm_object_cache_lock() \
256 mutex_lock(&vm_object_cached_lock_data)
257 #define vm_object_cache_lock_try() \
258 mutex_try(&vm_object_cached_lock_data)
259 #define vm_object_cache_unlock() \
260 mutex_unlock(&vm_object_cached_lock_data)
262 #define VM_OBJECT_HASH_COUNT 1024
263 static queue_head_t vm_object_hashtable
[VM_OBJECT_HASH_COUNT
];
264 static struct zone
*vm_object_hash_zone
;
266 struct vm_object_hash_entry
{
267 queue_chain_t hash_link
; /* hash chain link */
268 memory_object_t pager
; /* pager we represent */
269 vm_object_t object
; /* corresponding object */
270 boolean_t waiting
; /* someone waiting for
274 typedef struct vm_object_hash_entry
*vm_object_hash_entry_t
;
275 #define VM_OBJECT_HASH_ENTRY_NULL ((vm_object_hash_entry_t) 0)
277 #define VM_OBJECT_HASH_SHIFT 8
278 #define vm_object_hash(pager) \
279 ((((unsigned)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_COUNT)
281 void vm_object_hash_entry_free(
282 vm_object_hash_entry_t entry
);
285 * vm_object_hash_lookup looks up a pager in the hashtable
286 * and returns the corresponding entry, with optional removal.
289 static vm_object_hash_entry_t
290 vm_object_hash_lookup(
291 memory_object_t pager
,
292 boolean_t remove_entry
)
294 register queue_t bucket
;
295 register vm_object_hash_entry_t entry
;
297 bucket
= &vm_object_hashtable
[vm_object_hash(pager
)];
299 entry
= (vm_object_hash_entry_t
)queue_first(bucket
);
300 while (!queue_end(bucket
, (queue_entry_t
)entry
)) {
301 if (entry
->pager
== pager
&& !remove_entry
)
303 else if (entry
->pager
== pager
) {
304 queue_remove(bucket
, entry
,
305 vm_object_hash_entry_t
, hash_link
);
309 entry
= (vm_object_hash_entry_t
)queue_next(&entry
->hash_link
);
312 return(VM_OBJECT_HASH_ENTRY_NULL
);
316 * vm_object_hash_enter enters the specified
317 * pager / cache object association in the hashtable.
321 vm_object_hash_insert(
322 vm_object_hash_entry_t entry
)
324 register queue_t bucket
;
326 bucket
= &vm_object_hashtable
[vm_object_hash(entry
->pager
)];
328 queue_enter(bucket
, entry
, vm_object_hash_entry_t
, hash_link
);
331 static vm_object_hash_entry_t
332 vm_object_hash_entry_alloc(
333 memory_object_t pager
)
335 vm_object_hash_entry_t entry
;
337 entry
= (vm_object_hash_entry_t
)zalloc(vm_object_hash_zone
);
338 entry
->pager
= pager
;
339 entry
->object
= VM_OBJECT_NULL
;
340 entry
->waiting
= FALSE
;
346 vm_object_hash_entry_free(
347 vm_object_hash_entry_t entry
)
349 zfree(vm_object_hash_zone
, entry
);
353 * vm_object_allocate:
355 * Returns a new object with the given size.
358 __private_extern__
void
360 vm_object_size_t size
,
364 "vm_object_allocate, object 0x%X size 0x%X\n",
365 (integer_t
)object
, size
, 0,0,0);
367 *object
= vm_object_template
;
368 queue_init(&object
->memq
);
369 queue_init(&object
->msr_q
);
371 queue_init(&object
->uplq
);
372 #endif /* UPL_DEBUG */
373 vm_object_lock_init(object
);
377 __private_extern__ vm_object_t
379 vm_object_size_t size
)
381 register vm_object_t object
;
383 object
= (vm_object_t
) zalloc(vm_object_zone
);
385 // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
387 if (object
!= VM_OBJECT_NULL
)
388 _vm_object_allocate(size
, object
);
394 * vm_object_bootstrap:
396 * Initialize the VM objects module.
398 __private_extern__
void
399 vm_object_bootstrap(void)
403 vm_object_zone
= zinit((vm_size_t
) sizeof(struct vm_object
),
404 round_page_32(512*1024),
405 round_page_32(12*1024),
408 queue_init(&vm_object_cached_list
);
409 mutex_init(&vm_object_cached_lock_data
, 0);
411 vm_object_hash_zone
=
412 zinit((vm_size_t
) sizeof (struct vm_object_hash_entry
),
413 round_page_32(512*1024),
414 round_page_32(12*1024),
415 "vm object hash entries");
417 for (i
= 0; i
< VM_OBJECT_HASH_COUNT
; i
++)
418 queue_init(&vm_object_hashtable
[i
]);
421 * Fill in a template object, for quick initialization
424 /* memq; Lock; init after allocation */
425 vm_object_template
.size
= 0;
426 vm_object_template
.memq_hint
= VM_PAGE_NULL
;
427 vm_object_template
.ref_count
= 1;
429 vm_object_template
.res_count
= 1;
430 #endif /* TASK_SWAPPER */
431 vm_object_template
.resident_page_count
= 0;
432 vm_object_template
.copy
= VM_OBJECT_NULL
;
433 vm_object_template
.shadow
= VM_OBJECT_NULL
;
434 vm_object_template
.shadow_offset
= (vm_object_offset_t
) 0;
435 vm_object_template
.cow_hint
= ~(vm_offset_t
)0;
436 vm_object_template
.true_share
= FALSE
;
438 vm_object_template
.pager
= MEMORY_OBJECT_NULL
;
439 vm_object_template
.paging_offset
= 0;
440 vm_object_template
.pager_control
= MEMORY_OBJECT_CONTROL_NULL
;
441 /* msr_q; init after allocation */
443 vm_object_template
.copy_strategy
= MEMORY_OBJECT_COPY_SYMMETRIC
;
444 vm_object_template
.absent_count
= 0;
445 vm_object_template
.paging_in_progress
= 0;
447 /* Begin bitfields */
448 vm_object_template
.all_wanted
= 0; /* all bits FALSE */
449 vm_object_template
.pager_created
= FALSE
;
450 vm_object_template
.pager_initialized
= FALSE
;
451 vm_object_template
.pager_ready
= FALSE
;
452 vm_object_template
.pager_trusted
= FALSE
;
453 vm_object_template
.can_persist
= FALSE
;
454 vm_object_template
.internal
= TRUE
;
455 vm_object_template
.temporary
= TRUE
;
456 vm_object_template
.private = FALSE
;
457 vm_object_template
.pageout
= FALSE
;
458 vm_object_template
.alive
= TRUE
;
459 vm_object_template
.purgable
= VM_OBJECT_NONPURGABLE
;
460 vm_object_template
.silent_overwrite
= FALSE
;
461 vm_object_template
.advisory_pageout
= FALSE
;
462 vm_object_template
.shadowed
= FALSE
;
463 vm_object_template
.terminating
= FALSE
;
464 vm_object_template
.shadow_severed
= FALSE
;
465 vm_object_template
.phys_contiguous
= FALSE
;
466 vm_object_template
.nophyscache
= FALSE
;
469 /* cache bitfields */
470 vm_object_template
.wimg_bits
= VM_WIMG_DEFAULT
;
472 /* cached_list; init after allocation */
473 vm_object_template
.last_alloc
= (vm_object_offset_t
) 0;
474 vm_object_template
.cluster_size
= 0;
476 vm_object_template
.existence_map
= VM_EXTERNAL_NULL
;
477 #endif /* MACH_PAGEMAP */
479 vm_object_template
.paging_object
= VM_OBJECT_NULL
;
480 #endif /* MACH_ASSERT */
483 * Initialize the "kernel object"
486 kernel_object
= &kernel_object_store
;
489 * Note that in the following size specifications, we need to add 1 because
490 * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size.
494 _vm_object_allocate((vm_last_addr
- VM_MIN_KERNEL_ADDRESS
) + 1,
497 _vm_object_allocate((VM_MAX_KERNEL_ADDRESS
- VM_MIN_KERNEL_ADDRESS
) + 1,
500 kernel_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
503 * Initialize the "submap object". Make it as large as the
504 * kernel object so that no limit is imposed on submap sizes.
507 vm_submap_object
= &vm_submap_object_store
;
509 _vm_object_allocate((vm_last_addr
- VM_MIN_KERNEL_ADDRESS
) + 1,
512 _vm_object_allocate((VM_MAX_KERNEL_ADDRESS
- VM_MIN_KERNEL_ADDRESS
) + 1,
515 vm_submap_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
518 * Create an "extra" reference to this object so that we never
519 * try to deallocate it; zfree doesn't like to be called with
522 vm_object_reference(vm_submap_object
);
525 vm_external_module_initialize();
526 #endif /* MACH_PAGEMAP */
529 __private_extern__
void
533 * Finish initializing the kernel object.
537 /* remove the typedef below when emergency work-around is taken out */
538 typedef struct vnode_pager
{
539 memory_object_t pager
;
540 memory_object_t pager_handle
; /* pager */
541 memory_object_control_t control_handle
; /* memory object's control handle */
542 void *vnode_handle
; /* vnode handle */
545 #define MIGHT_NOT_CACHE_SHADOWS 1
546 #if MIGHT_NOT_CACHE_SHADOWS
547 static int cache_shadows
= TRUE
;
548 #endif /* MIGHT_NOT_CACHE_SHADOWS */
551 * vm_object_deallocate:
553 * Release a reference to the specified object,
554 * gained either through a vm_object_allocate
555 * or a vm_object_reference call. When all references
556 * are gone, storage associated with this object
557 * may be relinquished.
559 * No object may be locked.
561 __private_extern__
void
562 vm_object_deallocate(
563 register vm_object_t object
)
565 boolean_t retry_cache_trim
= FALSE
;
566 vm_object_t shadow
= VM_OBJECT_NULL
;
568 // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
569 // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
572 while (object
!= VM_OBJECT_NULL
) {
575 * The cache holds a reference (uncounted) to
576 * the object; we must lock it before removing
580 vm_object_cache_lock();
583 * if we try to take a regular lock here
584 * we risk deadlocking against someone
585 * holding a lock on this object while
586 * trying to vm_object_deallocate a different
589 if (vm_object_lock_try(object
))
591 vm_object_cache_unlock();
592 mutex_pause(); /* wait a bit */
594 assert(object
->ref_count
> 0);
597 * If the object has a named reference, and only
598 * that reference would remain, inform the pager
599 * about the last "mapping" reference going away.
601 if ((object
->ref_count
== 2) && (object
->named
)) {
602 memory_object_t pager
= object
->pager
;
604 /* Notify the Pager that there are no */
605 /* more mappers for this object */
607 if (pager
!= MEMORY_OBJECT_NULL
) {
608 vm_object_unlock(object
);
609 vm_object_cache_unlock();
611 memory_object_unmap(pager
);
614 vm_object_cache_lock();
617 * if we try to take a regular lock here
618 * we risk deadlocking against someone
619 * holding a lock on this object while
620 * trying to vm_object_deallocate a different
623 if (vm_object_lock_try(object
))
625 vm_object_cache_unlock();
626 mutex_pause(); /* wait a bit */
628 assert(object
->ref_count
> 0);
633 * Lose the reference. If other references
634 * remain, then we are done, unless we need
635 * to retry a cache trim.
636 * If it is the last reference, then keep it
637 * until any pending initialization is completed.
640 /* if the object is terminating, it cannot go into */
641 /* the cache and we obviously should not call */
642 /* terminate again. */
644 if ((object
->ref_count
> 1) || object
->terminating
) {
646 vm_object_res_deallocate(object
);
647 vm_object_cache_unlock();
649 if (object
->ref_count
== 1 &&
650 object
->shadow
!= VM_OBJECT_NULL
) {
652 * We don't use this VM object anymore. We
653 * would like to collapse it into its parent(s),
654 * but we don't have any pointers back to these
656 * But we can try and collapse this object with
657 * its own shadows, in case these are useless
660 vm_object_collapse(object
, 0);
663 vm_object_unlock(object
);
664 if (retry_cache_trim
&&
665 ((object
= vm_object_cache_trim(TRUE
)) !=
673 * We have to wait for initialization
674 * before destroying or caching the object.
677 if (object
->pager_created
&& ! object
->pager_initialized
) {
678 assert(! object
->can_persist
);
679 vm_object_assert_wait(object
,
680 VM_OBJECT_EVENT_INITIALIZED
,
682 vm_object_unlock(object
);
683 vm_object_cache_unlock();
684 thread_block(THREAD_CONTINUE_NULL
);
689 * If this object can persist, then enter it in
690 * the cache. Otherwise, terminate it.
692 * NOTE: Only permanent objects are cached, and
693 * permanent objects cannot have shadows. This
694 * affects the residence counting logic in a minor
695 * way (can do it in-line, mostly).
698 if ((object
->can_persist
) && (object
->alive
)) {
700 * Now it is safe to decrement reference count,
701 * and to return if reference count is > 0.
703 if (--object
->ref_count
> 0) {
704 vm_object_res_deallocate(object
);
705 vm_object_unlock(object
);
706 vm_object_cache_unlock();
707 if (retry_cache_trim
&&
708 ((object
= vm_object_cache_trim(TRUE
)) !=
715 #if MIGHT_NOT_CACHE_SHADOWS
717 * Remove shadow now if we don't
718 * want to cache shadows.
720 if (! cache_shadows
) {
721 shadow
= object
->shadow
;
722 object
->shadow
= VM_OBJECT_NULL
;
724 #endif /* MIGHT_NOT_CACHE_SHADOWS */
727 * Enter the object onto the queue of
728 * cached objects, and deactivate
731 assert(object
->shadow
== VM_OBJECT_NULL
);
732 VM_OBJ_RES_DECR(object
);
734 "vm_o_deallocate: adding %x to cache, queue = (%x, %x)\n",
736 (integer_t
)vm_object_cached_list
.next
,
737 (integer_t
)vm_object_cached_list
.prev
,0,0);
739 vm_object_cached_count
++;
740 if (vm_object_cached_count
> vm_object_cached_high
)
741 vm_object_cached_high
= vm_object_cached_count
;
742 queue_enter(&vm_object_cached_list
, object
,
743 vm_object_t
, cached_list
);
744 vm_object_cache_unlock();
745 vm_object_deactivate_all_pages(object
);
746 vm_object_unlock(object
);
748 #if MIGHT_NOT_CACHE_SHADOWS
750 * If we have a shadow that we need
751 * to deallocate, do so now, remembering
752 * to trim the cache later.
754 if (! cache_shadows
&& shadow
!= VM_OBJECT_NULL
) {
756 retry_cache_trim
= TRUE
;
759 #endif /* MIGHT_NOT_CACHE_SHADOWS */
762 * Trim the cache. If the cache trim
763 * returns with a shadow for us to deallocate,
764 * then remember to retry the cache trim
765 * when we are done deallocating the shadow.
766 * Otherwise, we are done.
769 object
= vm_object_cache_trim(TRUE
);
770 if (object
== VM_OBJECT_NULL
) {
773 retry_cache_trim
= TRUE
;
777 * This object is not cachable; terminate it.
780 "vm_o_deallocate: !cacheable 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
781 (integer_t
)object
, object
->resident_page_count
,
782 object
->paging_in_progress
,
783 (void *)current_thread(),object
->ref_count
);
785 VM_OBJ_RES_DECR(object
); /* XXX ? */
787 * Terminate this object. If it had a shadow,
788 * then deallocate it; otherwise, if we need
789 * to retry a cache trim, do so now; otherwise,
790 * we are done. "pageout" objects have a shadow,
791 * but maintain a "paging reference" rather than
792 * a normal reference.
794 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
795 if(vm_object_terminate(object
) != KERN_SUCCESS
) {
798 if (shadow
!= VM_OBJECT_NULL
) {
802 if (retry_cache_trim
&&
803 ((object
= vm_object_cache_trim(TRUE
)) !=
810 assert(! retry_cache_trim
);
814 * Check to see whether we really need to trim
815 * down the cache. If so, remove an object from
816 * the cache, terminate it, and repeat.
818 * Called with, and returns with, cache lock unlocked.
821 vm_object_cache_trim(
822 boolean_t called_from_vm_object_deallocate
)
824 register vm_object_t object
= VM_OBJECT_NULL
;
830 * If we no longer need to trim the cache,
834 vm_object_cache_lock();
835 if (vm_object_cached_count
<= vm_object_cached_max
) {
836 vm_object_cache_unlock();
837 return VM_OBJECT_NULL
;
841 * We must trim down the cache, so remove
842 * the first object in the cache.
845 "vm_object_cache_trim: removing from front of cache (%x, %x)\n",
846 (integer_t
)vm_object_cached_list
.next
,
847 (integer_t
)vm_object_cached_list
.prev
, 0, 0, 0);
849 object
= (vm_object_t
) queue_first(&vm_object_cached_list
);
850 if(object
== (vm_object_t
) &vm_object_cached_list
) {
851 /* something's wrong with the calling parameter or */
852 /* the value of vm_object_cached_count, just fix */
854 if(vm_object_cached_max
< 0)
855 vm_object_cached_max
= 0;
856 vm_object_cached_count
= 0;
857 vm_object_cache_unlock();
858 return VM_OBJECT_NULL
;
860 vm_object_lock(object
);
861 queue_remove(&vm_object_cached_list
, object
, vm_object_t
,
863 vm_object_cached_count
--;
866 * Since this object is in the cache, we know
867 * that it is initialized and has no references.
868 * Take a reference to avoid recursive deallocations.
871 assert(object
->pager_initialized
);
872 assert(object
->ref_count
== 0);
876 * Terminate the object.
877 * If the object had a shadow, we let vm_object_deallocate
878 * deallocate it. "pageout" objects have a shadow, but
879 * maintain a "paging reference" rather than a normal
881 * (We are careful here to limit recursion.)
883 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
884 if(vm_object_terminate(object
) != KERN_SUCCESS
)
886 if (shadow
!= VM_OBJECT_NULL
) {
887 if (called_from_vm_object_deallocate
) {
890 vm_object_deallocate(shadow
);
896 boolean_t vm_object_terminate_remove_all
= FALSE
;
899 * Routine: vm_object_terminate
901 * Free all resources associated with a vm_object.
903 * Upon entry, the object must be locked,
904 * and the object must have exactly one reference.
906 * The shadow object reference is left alone.
908 * The object must be unlocked if its found that pages
909 * must be flushed to a backing object. If someone
910 * manages to map the object while it is being flushed
911 * the object is returned unlocked and unchanged. Otherwise,
912 * upon exit, the cache will be unlocked, and the
913 * object will cease to exist.
917 register vm_object_t object
)
919 memory_object_t pager
;
920 register vm_page_t p
;
921 vm_object_t shadow_object
;
923 XPR(XPR_VM_OBJECT
, "vm_object_terminate, object 0x%X ref %d\n",
924 (integer_t
)object
, object
->ref_count
, 0, 0, 0);
926 if (!object
->pageout
&& (!object
->temporary
|| object
->can_persist
)
927 && (object
->pager
!= NULL
|| object
->shadow_severed
)) {
928 vm_object_cache_unlock();
929 while (!queue_empty(&object
->memq
)) {
931 * Clear pager_trusted bit so that the pages get yanked
932 * out of the object instead of cleaned in place. This
933 * prevents a deadlock in XMM and makes more sense anyway.
935 object
->pager_trusted
= FALSE
;
937 p
= (vm_page_t
) queue_first(&object
->memq
);
941 if (p
->busy
|| p
->cleaning
) {
942 if(p
->cleaning
|| p
->absent
) {
943 vm_object_paging_wait(object
, THREAD_UNINT
);
946 panic("vm_object_terminate.3 0x%x 0x%x", object
, p
);
950 vm_page_lock_queues();
952 VM_PAGE_QUEUES_REMOVE(p
);
953 vm_page_unlock_queues();
955 if (p
->absent
|| p
->private) {
958 * For private pages, VM_PAGE_FREE just
959 * leaves the page structure around for
960 * its owner to clean up. For absent
961 * pages, the structure is returned to
962 * the appropriate pool.
969 panic("vm_object_terminate.4 0x%x 0x%x", object
, p
);
972 p
->dirty
= pmap_is_modified(p
->phys_page
);
974 if ((p
->dirty
|| p
->precious
) && !p
->error
&& object
->alive
) {
975 vm_pageout_cluster(p
); /* flush page */
976 vm_object_paging_wait(object
, THREAD_UNINT
);
978 "vm_object_terminate restart, object 0x%X ref %d\n",
979 (integer_t
)object
, object
->ref_count
, 0, 0, 0);
985 vm_object_unlock(object
);
986 vm_object_cache_lock();
987 vm_object_lock(object
);
991 * Make sure the object isn't already being terminated
993 if(object
->terminating
) {
994 object
->ref_count
-= 1;
995 assert(object
->ref_count
> 0);
996 vm_object_cache_unlock();
997 vm_object_unlock(object
);
1002 * Did somebody get a reference to the object while we were
1005 if(object
->ref_count
!= 1) {
1006 object
->ref_count
-= 1;
1007 assert(object
->ref_count
> 0);
1008 vm_object_res_deallocate(object
);
1009 vm_object_cache_unlock();
1010 vm_object_unlock(object
);
1011 return KERN_FAILURE
;
1015 * Make sure no one can look us up now.
1018 object
->terminating
= TRUE
;
1019 object
->alive
= FALSE
;
1020 vm_object_remove(object
);
1023 * Detach the object from its shadow if we are the shadow's
1024 * copy. The reference we hold on the shadow must be dropped
1027 if (((shadow_object
= object
->shadow
) != VM_OBJECT_NULL
) &&
1028 !(object
->pageout
)) {
1029 vm_object_lock(shadow_object
);
1030 if (shadow_object
->copy
== object
)
1031 shadow_object
->copy
= VM_OBJECT_NULL
;
1032 vm_object_unlock(shadow_object
);
1036 * The pageout daemon might be playing with our pages.
1037 * Now that the object is dead, it won't touch any more
1038 * pages, but some pages might already be on their way out.
1039 * Hence, we wait until the active paging activities have ceased
1040 * before we break the association with the pager itself.
1042 while (object
->paging_in_progress
!= 0) {
1043 vm_object_cache_unlock();
1044 vm_object_wait(object
,
1045 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
1047 vm_object_cache_lock();
1048 vm_object_lock(object
);
1051 pager
= object
->pager
;
1052 object
->pager
= MEMORY_OBJECT_NULL
;
1054 if (pager
!= MEMORY_OBJECT_NULL
)
1055 memory_object_control_disable(object
->pager_control
);
1056 vm_object_cache_unlock();
1058 object
->ref_count
--;
1060 assert(object
->res_count
== 0);
1061 #endif /* TASK_SWAPPER */
1063 assert (object
->ref_count
== 0);
1066 * Clean or free the pages, as appropriate.
1067 * It is possible for us to find busy/absent pages,
1068 * if some faults on this object were aborted.
1070 if (object
->pageout
) {
1071 assert(shadow_object
!= VM_OBJECT_NULL
);
1072 assert(shadow_object
== object
->shadow
);
1074 vm_pageout_object_terminate(object
);
1076 } else if ((object
->temporary
&& !object
->can_persist
) ||
1077 (pager
== MEMORY_OBJECT_NULL
)) {
1078 while (!queue_empty(&object
->memq
)) {
1079 p
= (vm_page_t
) queue_first(&object
->memq
);
1084 } else if (!queue_empty(&object
->memq
)) {
1085 panic("vm_object_terminate: queue just emptied isn't");
1088 assert(object
->paging_in_progress
== 0);
1089 assert(object
->ref_count
== 0);
1092 * If the pager has not already been released by
1093 * vm_object_destroy, we need to terminate it and
1094 * release our reference to it here.
1096 if (pager
!= MEMORY_OBJECT_NULL
) {
1097 vm_object_unlock(object
);
1098 vm_object_release_pager(pager
);
1099 vm_object_lock(object
);
1102 /* kick off anyone waiting on terminating */
1103 object
->terminating
= FALSE
;
1104 vm_object_paging_begin(object
);
1105 vm_object_paging_end(object
);
1106 vm_object_unlock(object
);
1109 vm_external_destroy(object
->existence_map
, object
->size
);
1110 #endif /* MACH_PAGEMAP */
1113 * Free the space for the object.
1115 zfree(vm_object_zone
, object
);
1116 return KERN_SUCCESS
;
1120 * Routine: vm_object_pager_wakeup
1121 * Purpose: Wake up anyone waiting for termination of a pager.
1125 vm_object_pager_wakeup(
1126 memory_object_t pager
)
1128 vm_object_hash_entry_t entry
;
1129 boolean_t waiting
= FALSE
;
1132 * If anyone was waiting for the memory_object_terminate
1133 * to be queued, wake them up now.
1135 vm_object_cache_lock();
1136 entry
= vm_object_hash_lookup(pager
, TRUE
);
1137 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
1138 waiting
= entry
->waiting
;
1139 vm_object_cache_unlock();
1140 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
) {
1142 thread_wakeup((event_t
) pager
);
1143 vm_object_hash_entry_free(entry
);
1148 * Routine: vm_object_release_pager
1149 * Purpose: Terminate the pager and, upon completion,
1150 * release our last reference to it.
1151 * just like memory_object_terminate, except
1152 * that we wake up anyone blocked in vm_object_enter
1153 * waiting for termination message to be queued
1154 * before calling memory_object_init.
1157 vm_object_release_pager(
1158 memory_object_t pager
)
1162 * Terminate the pager.
1165 (void) memory_object_terminate(pager
);
1168 * Wakeup anyone waiting for this terminate
1170 vm_object_pager_wakeup(pager
);
1173 * Release reference to pager.
1175 memory_object_deallocate(pager
);
1179 * Routine: vm_object_destroy
1181 * Shut down a VM object, despite the
1182 * presence of address map (or other) references
1188 __unused kern_return_t reason
)
1190 memory_object_t old_pager
;
1192 if (object
== VM_OBJECT_NULL
)
1193 return(KERN_SUCCESS
);
1196 * Remove the pager association immediately.
1198 * This will prevent the memory manager from further
1199 * meddling. [If it wanted to flush data or make
1200 * other changes, it should have done so before performing
1201 * the destroy call.]
1204 vm_object_cache_lock();
1205 vm_object_lock(object
);
1206 object
->can_persist
= FALSE
;
1207 object
->named
= FALSE
;
1208 object
->alive
= FALSE
;
1211 * Rip out the pager from the vm_object now...
1214 vm_object_remove(object
);
1215 old_pager
= object
->pager
;
1216 object
->pager
= MEMORY_OBJECT_NULL
;
1217 if (old_pager
!= MEMORY_OBJECT_NULL
)
1218 memory_object_control_disable(object
->pager_control
);
1219 vm_object_cache_unlock();
1222 * Wait for the existing paging activity (that got
1223 * through before we nulled out the pager) to subside.
1226 vm_object_paging_wait(object
, THREAD_UNINT
);
1227 vm_object_unlock(object
);
1230 * Terminate the object now.
1232 if (old_pager
!= MEMORY_OBJECT_NULL
) {
1233 vm_object_release_pager(old_pager
);
1236 * JMM - Release the caller's reference. This assumes the
1237 * caller had a reference to release, which is a big (but
1238 * currently valid) assumption if this is driven from the
1239 * vnode pager (it is holding a named reference when making
1242 vm_object_deallocate(object
);
1245 return(KERN_SUCCESS
);
1249 * vm_object_deactivate_pages
1251 * Deactivate all pages in the specified object. (Keep its pages
1252 * in memory even though it is no longer referenced.)
1254 * The object must be locked.
1257 vm_object_deactivate_all_pages(
1258 register vm_object_t object
)
1260 register vm_page_t p
;
1262 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1263 vm_page_lock_queues();
1265 vm_page_deactivate(p
);
1266 vm_page_unlock_queues();
1270 __private_extern__
void
1271 vm_object_deactivate_pages(
1273 vm_object_offset_t offset
,
1274 vm_object_size_t size
,
1275 boolean_t kill_page
)
1277 vm_object_t orig_object
;
1278 int pages_moved
= 0;
1279 int pages_found
= 0;
1282 * entered with object lock held, acquire a paging reference to
1283 * prevent the memory_object and control ports from
1286 orig_object
= object
;
1289 register vm_page_t m
;
1290 vm_object_offset_t toffset
;
1291 vm_object_size_t tsize
;
1293 vm_object_paging_begin(object
);
1294 vm_page_lock_queues();
1296 for (tsize
= size
, toffset
= offset
; tsize
; tsize
-= PAGE_SIZE
, toffset
+= PAGE_SIZE
) {
1298 if ((m
= vm_page_lookup(object
, toffset
)) != VM_PAGE_NULL
) {
1302 if ((m
->wire_count
== 0) && (!m
->private) && (!m
->gobbled
) && (!m
->busy
)) {
1304 assert(!m
->laundry
);
1306 m
->reference
= FALSE
;
1307 pmap_clear_reference(m
->phys_page
);
1309 if ((kill_page
) && (object
->internal
)) {
1310 m
->precious
= FALSE
;
1312 pmap_clear_modify(m
->phys_page
);
1313 vm_external_state_clr(object
->existence_map
, offset
);
1315 VM_PAGE_QUEUES_REMOVE(m
);
1317 assert(!m
->laundry
);
1318 assert(m
->object
!= kernel_object
);
1319 assert(m
->pageq
.next
== NULL
&&
1320 m
->pageq
.prev
== NULL
);
1324 m
, vm_page_t
, pageq
);
1327 &vm_page_queue_inactive
,
1328 m
, vm_page_t
, pageq
);
1333 vm_page_inactive_count
++;
1339 vm_page_unlock_queues();
1340 vm_object_paging_end(object
);
1342 if (object
->shadow
) {
1343 vm_object_t tmp_object
;
1347 offset
+= object
->shadow_offset
;
1349 tmp_object
= object
->shadow
;
1350 vm_object_lock(tmp_object
);
1352 if (object
!= orig_object
)
1353 vm_object_unlock(object
);
1354 object
= tmp_object
;
1358 if (object
!= orig_object
)
1359 vm_object_unlock(object
);
1363 * Routine: vm_object_pmap_protect
1366 * Reduces the permission for all physical
1367 * pages in the specified object range.
1369 * If removing write permission only, it is
1370 * sufficient to protect only the pages in
1371 * the top-level object; only those pages may
1372 * have write permission.
1374 * If removing all access, we must follow the
1375 * shadow chain from the top-level object to
1376 * remove access to all pages in shadowed objects.
1378 * The object must *not* be locked. The object must
1379 * be temporary/internal.
1381 * If pmap is not NULL, this routine assumes that
1382 * the only mappings for the pages are in that
1386 __private_extern__
void
1387 vm_object_pmap_protect(
1388 register vm_object_t object
,
1389 register vm_object_offset_t offset
,
1390 vm_object_size_t size
,
1392 vm_map_offset_t pmap_start
,
1395 if (object
== VM_OBJECT_NULL
)
1397 size
= vm_object_round_page(size
);
1398 offset
= vm_object_trunc_page(offset
);
1400 vm_object_lock(object
);
1402 assert(object
->internal
);
1405 if (ptoa_64(object
->resident_page_count
) > size
/2 && pmap
!= PMAP_NULL
) {
1406 vm_object_unlock(object
);
1407 pmap_protect(pmap
, pmap_start
, pmap_start
+ size
, prot
);
1411 /* if we are doing large ranges with respect to resident */
1412 /* page count then we should interate over pages otherwise */
1413 /* inverse page look-up will be faster */
1414 if (ptoa_64(object
->resident_page_count
/ 4) < size
) {
1416 vm_object_offset_t end
;
1418 end
= offset
+ size
;
1420 if (pmap
!= PMAP_NULL
) {
1421 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1422 if (!p
->fictitious
&&
1423 (offset
<= p
->offset
) && (p
->offset
< end
)) {
1424 vm_map_offset_t start
;
1426 start
= pmap_start
+ p
->offset
- offset
;
1427 pmap_protect(pmap
, start
, start
+ PAGE_SIZE_64
, prot
);
1431 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1432 if (!p
->fictitious
&&
1433 (offset
<= p
->offset
) && (p
->offset
< end
)) {
1435 pmap_page_protect(p
->phys_page
,
1436 prot
& ~p
->page_lock
);
1442 vm_object_offset_t end
;
1443 vm_object_offset_t target_off
;
1445 end
= offset
+ size
;
1447 if (pmap
!= PMAP_NULL
) {
1448 for(target_off
= offset
;
1450 target_off
+= PAGE_SIZE
) {
1451 p
= vm_page_lookup(object
, target_off
);
1452 if (p
!= VM_PAGE_NULL
) {
1454 start
= pmap_start
+
1455 (vm_offset_t
)(p
->offset
- offset
);
1456 pmap_protect(pmap
, start
,
1457 start
+ PAGE_SIZE
, prot
);
1461 for(target_off
= offset
;
1462 target_off
< end
; target_off
+= PAGE_SIZE
) {
1463 p
= vm_page_lookup(object
, target_off
);
1464 if (p
!= VM_PAGE_NULL
) {
1465 pmap_page_protect(p
->phys_page
,
1466 prot
& ~p
->page_lock
);
1472 if (prot
== VM_PROT_NONE
) {
1474 * Must follow shadow chain to remove access
1475 * to pages in shadowed objects.
1477 register vm_object_t next_object
;
1479 next_object
= object
->shadow
;
1480 if (next_object
!= VM_OBJECT_NULL
) {
1481 offset
+= object
->shadow_offset
;
1482 vm_object_lock(next_object
);
1483 vm_object_unlock(object
);
1484 object
= next_object
;
1488 * End of chain - we are done.
1495 * Pages in shadowed objects may never have
1496 * write permission - we may stop here.
1502 vm_object_unlock(object
);
1506 * Routine: vm_object_copy_slowly
1509 * Copy the specified range of the source
1510 * virtual memory object without using
1511 * protection-based optimizations (such
1512 * as copy-on-write). The pages in the
1513 * region are actually copied.
1515 * In/out conditions:
1516 * The caller must hold a reference and a lock
1517 * for the source virtual memory object. The source
1518 * object will be returned *unlocked*.
1521 * If the copy is completed successfully, KERN_SUCCESS is
1522 * returned. If the caller asserted the interruptible
1523 * argument, and an interruption occurred while waiting
1524 * for a user-generated event, MACH_SEND_INTERRUPTED is
1525 * returned. Other values may be returned to indicate
1526 * hard errors during the copy operation.
1528 * A new virtual memory object is returned in a
1529 * parameter (_result_object). The contents of this
1530 * new object, starting at a zero offset, are a copy
1531 * of the source memory region. In the event of
1532 * an error, this parameter will contain the value
1535 __private_extern__ kern_return_t
1536 vm_object_copy_slowly(
1537 register vm_object_t src_object
,
1538 vm_object_offset_t src_offset
,
1539 vm_object_size_t size
,
1540 boolean_t interruptible
,
1541 vm_object_t
*_result_object
) /* OUT */
1543 vm_object_t new_object
;
1544 vm_object_offset_t new_offset
;
1546 vm_object_offset_t src_lo_offset
= src_offset
;
1547 vm_object_offset_t src_hi_offset
= src_offset
+ size
;
1549 XPR(XPR_VM_OBJECT
, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
1550 src_object
, src_offset
, size
, 0, 0);
1553 vm_object_unlock(src_object
);
1554 *_result_object
= VM_OBJECT_NULL
;
1555 return(KERN_INVALID_ARGUMENT
);
1559 * Prevent destruction of the source object while we copy.
1562 assert(src_object
->ref_count
> 0);
1563 src_object
->ref_count
++;
1564 VM_OBJ_RES_INCR(src_object
);
1565 vm_object_unlock(src_object
);
1568 * Create a new object to hold the copied pages.
1570 * We fill the new object starting at offset 0,
1571 * regardless of the input offset.
1572 * We don't bother to lock the new object within
1573 * this routine, since we have the only reference.
1576 new_object
= vm_object_allocate(size
);
1578 vm_object_lock(new_object
);
1580 assert(size
== trunc_page_64(size
)); /* Will the loop terminate? */
1584 src_offset
+= PAGE_SIZE_64
,
1585 new_offset
+= PAGE_SIZE_64
, size
-= PAGE_SIZE_64
1588 vm_fault_return_t result
;
1590 while ((new_page
= vm_page_alloc(new_object
, new_offset
))
1592 if (!vm_page_wait(interruptible
)) {
1593 vm_object_unlock(new_object
);
1594 vm_object_deallocate(new_object
);
1595 vm_object_deallocate(src_object
);
1596 *_result_object
= VM_OBJECT_NULL
;
1597 return(MACH_SEND_INTERRUPTED
);
1602 vm_prot_t prot
= VM_PROT_READ
;
1603 vm_page_t _result_page
;
1606 vm_page_t result_page
;
1607 kern_return_t error_code
;
1609 vm_object_lock(src_object
);
1610 vm_object_paging_begin(src_object
);
1612 XPR(XPR_VM_FAULT
,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
1613 result
= vm_fault_page(src_object
, src_offset
,
1614 VM_PROT_READ
, FALSE
, interruptible
,
1615 src_lo_offset
, src_hi_offset
,
1616 VM_BEHAVIOR_SEQUENTIAL
,
1617 &prot
, &_result_page
, &top_page
,
1619 &error_code
, FALSE
, FALSE
, NULL
, 0);
1622 case VM_FAULT_SUCCESS
:
1623 result_page
= _result_page
;
1626 * We don't need to hold the object
1627 * lock -- the busy page will be enough.
1628 * [We don't care about picking up any
1629 * new modifications.]
1631 * Copy the page to the new object.
1634 * If result_page is clean,
1635 * we could steal it instead
1639 vm_object_unlock(result_page
->object
);
1640 vm_page_copy(result_page
, new_page
);
1643 * Let go of both pages (make them
1644 * not busy, perform wakeup, activate).
1647 new_page
->busy
= FALSE
;
1648 new_page
->dirty
= TRUE
;
1649 vm_object_lock(result_page
->object
);
1650 PAGE_WAKEUP_DONE(result_page
);
1652 vm_page_lock_queues();
1653 if (!result_page
->active
&&
1654 !result_page
->inactive
)
1655 vm_page_activate(result_page
);
1656 vm_page_activate(new_page
);
1657 vm_page_unlock_queues();
1660 * Release paging references and
1661 * top-level placeholder page, if any.
1664 vm_fault_cleanup(result_page
->object
,
1669 case VM_FAULT_RETRY
:
1672 case VM_FAULT_FICTITIOUS_SHORTAGE
:
1673 vm_page_more_fictitious();
1676 case VM_FAULT_MEMORY_SHORTAGE
:
1677 if (vm_page_wait(interruptible
))
1681 case VM_FAULT_INTERRUPTED
:
1682 vm_page_free(new_page
);
1683 vm_object_unlock(new_object
);
1684 vm_object_deallocate(new_object
);
1685 vm_object_deallocate(src_object
);
1686 *_result_object
= VM_OBJECT_NULL
;
1687 return(MACH_SEND_INTERRUPTED
);
1689 case VM_FAULT_MEMORY_ERROR
:
1692 * (a) ignore pages that we can't
1694 * (b) return the null object if
1695 * any page fails [chosen]
1698 vm_page_lock_queues();
1699 vm_page_free(new_page
);
1700 vm_page_unlock_queues();
1701 vm_object_unlock(new_object
);
1702 vm_object_deallocate(new_object
);
1703 vm_object_deallocate(src_object
);
1704 *_result_object
= VM_OBJECT_NULL
;
1705 return(error_code
? error_code
:
1708 } while (result
!= VM_FAULT_SUCCESS
);
1712 * Lose the extra reference, and return our object.
1715 vm_object_unlock(new_object
);
1716 vm_object_deallocate(src_object
);
1717 *_result_object
= new_object
;
1718 return(KERN_SUCCESS
);
1722 * Routine: vm_object_copy_quickly
1725 * Copy the specified range of the source virtual
1726 * memory object, if it can be done without waiting
1727 * for user-generated events.
1730 * If the copy is successful, the copy is returned in
1731 * the arguments; otherwise, the arguments are not
1734 * In/out conditions:
1735 * The object should be unlocked on entry and exit.
1739 __private_extern__ boolean_t
1740 vm_object_copy_quickly(
1741 vm_object_t
*_object
, /* INOUT */
1742 __unused vm_object_offset_t offset
, /* IN */
1743 __unused vm_object_size_t size
, /* IN */
1744 boolean_t
*_src_needs_copy
, /* OUT */
1745 boolean_t
*_dst_needs_copy
) /* OUT */
1747 vm_object_t object
= *_object
;
1748 memory_object_copy_strategy_t copy_strategy
;
1750 XPR(XPR_VM_OBJECT
, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
1751 *_object
, offset
, size
, 0, 0);
1752 if (object
== VM_OBJECT_NULL
) {
1753 *_src_needs_copy
= FALSE
;
1754 *_dst_needs_copy
= FALSE
;
1758 vm_object_lock(object
);
1760 copy_strategy
= object
->copy_strategy
;
1762 switch (copy_strategy
) {
1763 case MEMORY_OBJECT_COPY_SYMMETRIC
:
1766 * Symmetric copy strategy.
1767 * Make another reference to the object.
1768 * Leave object/offset unchanged.
1771 assert(object
->ref_count
> 0);
1772 object
->ref_count
++;
1773 vm_object_res_reference(object
);
1774 object
->shadowed
= TRUE
;
1775 vm_object_unlock(object
);
1778 * Both source and destination must make
1779 * shadows, and the source must be made
1780 * read-only if not already.
1783 *_src_needs_copy
= TRUE
;
1784 *_dst_needs_copy
= TRUE
;
1788 case MEMORY_OBJECT_COPY_DELAY
:
1789 vm_object_unlock(object
);
1793 vm_object_unlock(object
);
1799 static int copy_call_count
= 0;
1800 static int copy_call_sleep_count
= 0;
1801 static int copy_call_restart_count
= 0;
1804 * Routine: vm_object_copy_call [internal]
1807 * Copy the source object (src_object), using the
1808 * user-managed copy algorithm.
1810 * In/out conditions:
1811 * The source object must be locked on entry. It
1812 * will be *unlocked* on exit.
1815 * If the copy is successful, KERN_SUCCESS is returned.
1816 * A new object that represents the copied virtual
1817 * memory is returned in a parameter (*_result_object).
1818 * If the return value indicates an error, this parameter
1821 static kern_return_t
1822 vm_object_copy_call(
1823 vm_object_t src_object
,
1824 vm_object_offset_t src_offset
,
1825 vm_object_size_t size
,
1826 vm_object_t
*_result_object
) /* OUT */
1830 boolean_t check_ready
= FALSE
;
1833 * If a copy is already in progress, wait and retry.
1836 * Consider making this call interruptable, as Mike
1837 * intended it to be.
1840 * Need a counter or version or something to allow
1841 * us to use the copy that the currently requesting
1842 * thread is obtaining -- is it worth adding to the
1843 * vm object structure? Depends how common this case it.
1846 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
1847 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
1849 copy_call_restart_count
++;
1853 * Indicate (for the benefit of memory_object_create_copy)
1854 * that we want a copy for src_object. (Note that we cannot
1855 * do a real assert_wait before calling memory_object_copy,
1856 * so we simply set the flag.)
1859 vm_object_set_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
);
1860 vm_object_unlock(src_object
);
1863 * Ask the memory manager to give us a memory object
1864 * which represents a copy of the src object.
1865 * The memory manager may give us a memory object
1866 * which we already have, or it may give us a
1867 * new memory object. This memory object will arrive
1868 * via memory_object_create_copy.
1871 kr
= KERN_FAILURE
; /* XXX need to change memory_object.defs */
1872 if (kr
!= KERN_SUCCESS
) {
1877 * Wait for the copy to arrive.
1879 vm_object_lock(src_object
);
1880 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
1881 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
1883 copy_call_sleep_count
++;
1886 assert(src_object
->copy
!= VM_OBJECT_NULL
);
1887 copy
= src_object
->copy
;
1888 if (!vm_object_lock_try(copy
)) {
1889 vm_object_unlock(src_object
);
1890 mutex_pause(); /* wait a bit */
1891 vm_object_lock(src_object
);
1894 if (copy
->size
< src_offset
+size
)
1895 copy
->size
= src_offset
+size
;
1897 if (!copy
->pager_ready
)
1903 *_result_object
= copy
;
1904 vm_object_unlock(copy
);
1905 vm_object_unlock(src_object
);
1907 /* Wait for the copy to be ready. */
1908 if (check_ready
== TRUE
) {
1909 vm_object_lock(copy
);
1910 while (!copy
->pager_ready
) {
1911 vm_object_sleep(copy
, VM_OBJECT_EVENT_PAGER_READY
, THREAD_UNINT
);
1913 vm_object_unlock(copy
);
1916 return KERN_SUCCESS
;
1919 static int copy_delayed_lock_collisions
= 0;
1920 static int copy_delayed_max_collisions
= 0;
1921 static int copy_delayed_lock_contention
= 0;
1922 static int copy_delayed_protect_iterate
= 0;
1925 * Routine: vm_object_copy_delayed [internal]
1928 * Copy the specified virtual memory object, using
1929 * the asymmetric copy-on-write algorithm.
1931 * In/out conditions:
1932 * The src_object must be locked on entry. It will be unlocked
1933 * on exit - so the caller must also hold a reference to it.
1935 * This routine will not block waiting for user-generated
1936 * events. It is not interruptible.
1938 __private_extern__ vm_object_t
1939 vm_object_copy_delayed(
1940 vm_object_t src_object
,
1941 vm_object_offset_t src_offset
,
1942 vm_object_size_t size
)
1944 vm_object_t new_copy
= VM_OBJECT_NULL
;
1945 vm_object_t old_copy
;
1947 vm_object_size_t copy_size
= src_offset
+ size
;
1951 * The user-level memory manager wants to see all of the changes
1952 * to this object, but it has promised not to make any changes on
1955 * Perform an asymmetric copy-on-write, as follows:
1956 * Create a new object, called a "copy object" to hold
1957 * pages modified by the new mapping (i.e., the copy,
1958 * not the original mapping).
1959 * Record the original object as the backing object for
1960 * the copy object. If the original mapping does not
1961 * change a page, it may be used read-only by the copy.
1962 * Record the copy object in the original object.
1963 * When the original mapping causes a page to be modified,
1964 * it must be copied to a new page that is "pushed" to
1966 * Mark the new mapping (the copy object) copy-on-write.
1967 * This makes the copy object itself read-only, allowing
1968 * it to be reused if the original mapping makes no
1969 * changes, and simplifying the synchronization required
1970 * in the "push" operation described above.
1972 * The copy-on-write is said to be assymetric because the original
1973 * object is *not* marked copy-on-write. A copied page is pushed
1974 * to the copy object, regardless which party attempted to modify
1977 * Repeated asymmetric copy operations may be done. If the
1978 * original object has not been changed since the last copy, its
1979 * copy object can be reused. Otherwise, a new copy object can be
1980 * inserted between the original object and its previous copy
1981 * object. Since any copy object is read-only, this cannot affect
1982 * affect the contents of the previous copy object.
1984 * Note that a copy object is higher in the object tree than the
1985 * original object; therefore, use of the copy object recorded in
1986 * the original object must be done carefully, to avoid deadlock.
1992 * Wait for paging in progress.
1994 if (!src_object
->true_share
)
1995 vm_object_paging_wait(src_object
, THREAD_UNINT
);
1998 * See whether we can reuse the result of a previous
2002 old_copy
= src_object
->copy
;
2003 if (old_copy
!= VM_OBJECT_NULL
) {
2005 * Try to get the locks (out of order)
2007 if (!vm_object_lock_try(old_copy
)) {
2008 vm_object_unlock(src_object
);
2011 /* Heisenberg Rules */
2012 copy_delayed_lock_collisions
++;
2013 if (collisions
++ == 0)
2014 copy_delayed_lock_contention
++;
2016 if (collisions
> copy_delayed_max_collisions
)
2017 copy_delayed_max_collisions
= collisions
;
2019 vm_object_lock(src_object
);
2024 * Determine whether the old copy object has
2028 if (old_copy
->resident_page_count
== 0 &&
2029 !old_copy
->pager_created
) {
2031 * It has not been modified.
2033 * Return another reference to
2034 * the existing copy-object if
2035 * we can safely grow it (if
2039 if (old_copy
->size
< copy_size
) {
2041 * We can't perform a delayed copy if any of the
2042 * pages in the extended range are wired (because
2043 * we can't safely take write permission away from
2044 * wired pages). If the pages aren't wired, then
2045 * go ahead and protect them.
2047 copy_delayed_protect_iterate
++;
2048 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
2049 if (!p
->fictitious
&&
2050 p
->offset
>= old_copy
->size
&&
2051 p
->offset
< copy_size
) {
2052 if (p
->wire_count
> 0) {
2053 vm_object_unlock(old_copy
);
2054 vm_object_unlock(src_object
);
2056 if (new_copy
!= VM_OBJECT_NULL
) {
2057 vm_object_unlock(new_copy
);
2058 vm_object_deallocate(new_copy
);
2061 return VM_OBJECT_NULL
;
2063 pmap_page_protect(p
->phys_page
,
2064 (VM_PROT_ALL
& ~VM_PROT_WRITE
&
2069 old_copy
->size
= copy_size
;
2072 vm_object_reference_locked(old_copy
);
2073 vm_object_unlock(old_copy
);
2074 vm_object_unlock(src_object
);
2076 if (new_copy
!= VM_OBJECT_NULL
) {
2077 vm_object_unlock(new_copy
);
2078 vm_object_deallocate(new_copy
);
2085 * Adjust the size argument so that the newly-created
2086 * copy object will be large enough to back either the
2087 * old copy object or the new mapping.
2089 if (old_copy
->size
> copy_size
)
2090 copy_size
= old_copy
->size
;
2092 if (new_copy
== VM_OBJECT_NULL
) {
2093 vm_object_unlock(old_copy
);
2094 vm_object_unlock(src_object
);
2095 new_copy
= vm_object_allocate(copy_size
);
2096 vm_object_lock(src_object
);
2097 vm_object_lock(new_copy
);
2100 new_copy
->size
= copy_size
;
2103 * The copy-object is always made large enough to
2104 * completely shadow the original object, since
2105 * it may have several users who want to shadow
2106 * the original object at different points.
2109 assert((old_copy
->shadow
== src_object
) &&
2110 (old_copy
->shadow_offset
== (vm_object_offset_t
) 0));
2112 } else if (new_copy
== VM_OBJECT_NULL
) {
2113 vm_object_unlock(src_object
);
2114 new_copy
= vm_object_allocate(copy_size
);
2115 vm_object_lock(src_object
);
2116 vm_object_lock(new_copy
);
2121 * We now have the src object locked, and the new copy object
2122 * allocated and locked (and potentially the old copy locked).
2123 * Before we go any further, make sure we can still perform
2124 * a delayed copy, as the situation may have changed.
2126 * Specifically, we can't perform a delayed copy if any of the
2127 * pages in the range are wired (because we can't safely take
2128 * write permission away from wired pages). If the pages aren't
2129 * wired, then go ahead and protect them.
2131 copy_delayed_protect_iterate
++;
2132 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
2133 if (!p
->fictitious
&& p
->offset
< copy_size
) {
2134 if (p
->wire_count
> 0) {
2136 vm_object_unlock(old_copy
);
2137 vm_object_unlock(src_object
);
2138 vm_object_unlock(new_copy
);
2139 vm_object_deallocate(new_copy
);
2140 return VM_OBJECT_NULL
;
2142 pmap_page_protect(p
->phys_page
,
2143 (VM_PROT_ALL
& ~VM_PROT_WRITE
&
2149 if (old_copy
!= VM_OBJECT_NULL
) {
2151 * Make the old copy-object shadow the new one.
2152 * It will receive no more pages from the original
2156 src_object
->ref_count
--; /* remove ref. from old_copy */
2157 assert(src_object
->ref_count
> 0);
2158 old_copy
->shadow
= new_copy
;
2159 assert(new_copy
->ref_count
> 0);
2160 new_copy
->ref_count
++; /* for old_copy->shadow ref. */
2163 if (old_copy
->res_count
) {
2164 VM_OBJ_RES_INCR(new_copy
);
2165 VM_OBJ_RES_DECR(src_object
);
2169 vm_object_unlock(old_copy
); /* done with old_copy */
2173 * Point the new copy at the existing object.
2175 new_copy
->shadow
= src_object
;
2176 new_copy
->shadow_offset
= 0;
2177 new_copy
->shadowed
= TRUE
; /* caller must set needs_copy */
2178 assert(src_object
->ref_count
> 0);
2179 src_object
->ref_count
++;
2180 VM_OBJ_RES_INCR(src_object
);
2181 src_object
->copy
= new_copy
;
2182 vm_object_unlock(src_object
);
2183 vm_object_unlock(new_copy
);
2186 "vm_object_copy_delayed: used copy object %X for source %X\n",
2187 (integer_t
)new_copy
, (integer_t
)src_object
, 0, 0, 0);
2193 * Routine: vm_object_copy_strategically
2196 * Perform a copy according to the source object's
2197 * declared strategy. This operation may block,
2198 * and may be interrupted.
2200 __private_extern__ kern_return_t
2201 vm_object_copy_strategically(
2202 register vm_object_t src_object
,
2203 vm_object_offset_t src_offset
,
2204 vm_object_size_t size
,
2205 vm_object_t
*dst_object
, /* OUT */
2206 vm_object_offset_t
*dst_offset
, /* OUT */
2207 boolean_t
*dst_needs_copy
) /* OUT */
2210 boolean_t interruptible
= THREAD_ABORTSAFE
; /* XXX */
2211 memory_object_copy_strategy_t copy_strategy
;
2213 assert(src_object
!= VM_OBJECT_NULL
);
2215 vm_object_lock(src_object
);
2218 * The copy strategy is only valid if the memory manager
2219 * is "ready". Internal objects are always ready.
2222 while (!src_object
->internal
&& !src_object
->pager_ready
) {
2223 wait_result_t wait_result
;
2225 wait_result
= vm_object_sleep( src_object
,
2226 VM_OBJECT_EVENT_PAGER_READY
,
2228 if (wait_result
!= THREAD_AWAKENED
) {
2229 vm_object_unlock(src_object
);
2230 *dst_object
= VM_OBJECT_NULL
;
2232 *dst_needs_copy
= FALSE
;
2233 return(MACH_SEND_INTERRUPTED
);
2237 copy_strategy
= src_object
->copy_strategy
;
2240 * Use the appropriate copy strategy.
2243 switch (copy_strategy
) {
2244 case MEMORY_OBJECT_COPY_DELAY
:
2245 *dst_object
= vm_object_copy_delayed(src_object
,
2247 if (*dst_object
!= VM_OBJECT_NULL
) {
2248 *dst_offset
= src_offset
;
2249 *dst_needs_copy
= TRUE
;
2250 result
= KERN_SUCCESS
;
2253 vm_object_lock(src_object
);
2254 /* fall thru when delayed copy not allowed */
2256 case MEMORY_OBJECT_COPY_NONE
:
2257 result
= vm_object_copy_slowly(src_object
, src_offset
, size
,
2258 interruptible
, dst_object
);
2259 if (result
== KERN_SUCCESS
) {
2261 *dst_needs_copy
= FALSE
;
2265 case MEMORY_OBJECT_COPY_CALL
:
2266 result
= vm_object_copy_call(src_object
, src_offset
, size
,
2268 if (result
== KERN_SUCCESS
) {
2269 *dst_offset
= src_offset
;
2270 *dst_needs_copy
= TRUE
;
2274 case MEMORY_OBJECT_COPY_SYMMETRIC
:
2275 XPR(XPR_VM_OBJECT
, "v_o_c_strategically obj 0x%x off 0x%x size 0x%x\n",(natural_t
)src_object
, src_offset
, size
, 0, 0);
2276 vm_object_unlock(src_object
);
2277 result
= KERN_MEMORY_RESTART_COPY
;
2281 panic("copy_strategically: bad strategy");
2282 result
= KERN_INVALID_ARGUMENT
;
2290 * Create a new object which is backed by the
2291 * specified existing object range. The source
2292 * object reference is deallocated.
2294 * The new object and offset into that object
2295 * are returned in the source parameters.
2297 boolean_t vm_object_shadow_check
= FALSE
;
2299 __private_extern__ boolean_t
2301 vm_object_t
*object
, /* IN/OUT */
2302 vm_object_offset_t
*offset
, /* IN/OUT */
2303 vm_object_size_t length
)
2305 register vm_object_t source
;
2306 register vm_object_t result
;
2309 assert(source
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
);
2312 * Determine if we really need a shadow.
2315 if (vm_object_shadow_check
&& source
->ref_count
== 1 &&
2316 (source
->shadow
== VM_OBJECT_NULL
||
2317 source
->shadow
->copy
== VM_OBJECT_NULL
))
2319 source
->shadowed
= FALSE
;
2324 * Allocate a new object with the given length
2327 if ((result
= vm_object_allocate(length
)) == VM_OBJECT_NULL
)
2328 panic("vm_object_shadow: no object for shadowing");
2331 * The new object shadows the source object, adding
2332 * a reference to it. Our caller changes his reference
2333 * to point to the new object, removing a reference to
2334 * the source object. Net result: no change of reference
2337 result
->shadow
= source
;
2340 * Store the offset into the source object,
2341 * and fix up the offset into the new object.
2344 result
->shadow_offset
= *offset
;
2347 * Return the new things
2356 * The relationship between vm_object structures and
2357 * the memory_object requires careful synchronization.
2359 * All associations are created by memory_object_create_named
2360 * for external pagers and vm_object_pager_create for internal
2361 * objects as follows:
2363 * pager: the memory_object itself, supplied by
2364 * the user requesting a mapping (or the kernel,
2365 * when initializing internal objects); the
2366 * kernel simulates holding send rights by keeping
2370 * the memory object control port,
2371 * created by the kernel; the kernel holds
2372 * receive (and ownership) rights to this
2373 * port, but no other references.
2375 * When initialization is complete, the "initialized" field
2376 * is asserted. Other mappings using a particular memory object,
2377 * and any references to the vm_object gained through the
2378 * port association must wait for this initialization to occur.
2380 * In order to allow the memory manager to set attributes before
2381 * requests (notably virtual copy operations, but also data or
2382 * unlock requests) are made, a "ready" attribute is made available.
2383 * Only the memory manager may affect the value of this attribute.
2384 * Its value does not affect critical kernel functions, such as
2385 * internal object initialization or destruction. [Furthermore,
2386 * memory objects created by the kernel are assumed to be ready
2387 * immediately; the default memory manager need not explicitly
2388 * set the "ready" attribute.]
2390 * [Both the "initialized" and "ready" attribute wait conditions
2391 * use the "pager" field as the wait event.]
2393 * The port associations can be broken down by any of the
2394 * following routines:
2395 * vm_object_terminate:
2396 * No references to the vm_object remain, and
2397 * the object cannot (or will not) be cached.
2398 * This is the normal case, and is done even
2399 * though one of the other cases has already been
2401 * memory_object_destroy:
2402 * The memory manager has requested that the
2403 * kernel relinquish references to the memory
2404 * object. [The memory manager may not want to
2405 * destroy the memory object, but may wish to
2406 * refuse or tear down existing memory mappings.]
2408 * Each routine that breaks an association must break all of
2409 * them at once. At some later time, that routine must clear
2410 * the pager field and release the memory object references.
2411 * [Furthermore, each routine must cope with the simultaneous
2412 * or previous operations of the others.]
2414 * In addition to the lock on the object, the vm_object_cache_lock
2415 * governs the associations. References gained through the
2416 * association require use of the cache lock.
2418 * Because the pager field may be cleared spontaneously, it
2419 * cannot be used to determine whether a memory object has
2420 * ever been associated with a particular vm_object. [This
2421 * knowledge is important to the shadow object mechanism.]
2422 * For this reason, an additional "created" attribute is
2425 * During various paging operations, the pager reference found in the
2426 * vm_object must be valid. To prevent this from being released,
2427 * (other than being removed, i.e., made null), routines may use
2428 * the vm_object_paging_begin/end routines [actually, macros].
2429 * The implementation uses the "paging_in_progress" and "wanted" fields.
2430 * [Operations that alter the validity of the pager values include the
2431 * termination routines and vm_object_collapse.]
2435 static void vm_object_abort_activity(
2436 vm_object_t object
);
2439 * Routine: vm_object_abort_activity [internal use only]
2441 * Abort paging requests pending on this object.
2442 * In/out conditions:
2443 * The object is locked on entry and exit.
2446 vm_object_abort_activity(
2453 XPR(XPR_VM_OBJECT
, "vm_object_abort_activity, object 0x%X\n",
2454 (integer_t
)object
, 0, 0, 0, 0);
2457 * Abort all activity that would be waiting
2458 * for a result on this memory object.
2460 * We could also choose to destroy all pages
2461 * that we have in memory for this object, but
2465 p
= (vm_page_t
) queue_first(&object
->memq
);
2466 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
2467 next
= (vm_page_t
) queue_next(&p
->listq
);
2470 * If it's being paged in, destroy it.
2471 * If an unlock has been requested, start it again.
2474 if (p
->busy
&& p
->absent
) {
2478 if (p
->unlock_request
!= VM_PROT_NONE
)
2479 p
->unlock_request
= VM_PROT_NONE
;
2487 * Wake up threads waiting for the memory object to
2491 object
->pager_ready
= TRUE
;
2492 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
2496 * Routine: vm_object_pager_dead
2499 * A port is being destroy, and the IPC kobject code
2500 * can't tell if it represents a pager port or not.
2501 * So this function is called each time it sees a port
2503 * THIS IS HORRIBLY INEFFICIENT. We should only call
2504 * this routine if we had requested a notification on
2508 __private_extern__
void
2509 vm_object_pager_dead(
2513 vm_object_hash_entry_t entry
;
2516 * Perform essentially the same operations as in vm_object_lookup,
2517 * except that this time we look up based on the memory_object
2518 * port, not the control port.
2520 vm_object_cache_lock();
2521 entry
= vm_object_hash_lookup(pager
, FALSE
);
2522 if (entry
== VM_OBJECT_HASH_ENTRY_NULL
||
2523 entry
->object
== VM_OBJECT_NULL
) {
2524 vm_object_cache_unlock();
2528 object
= entry
->object
;
2529 entry
->object
= VM_OBJECT_NULL
;
2531 vm_object_lock(object
);
2532 if (object
->ref_count
== 0) {
2533 XPR(XPR_VM_OBJECT_CACHE
,
2534 "vm_object_destroy: removing %x from cache, head (%x, %x)\n",
2536 (integer_t
)vm_object_cached_list
.next
,
2537 (integer_t
)vm_object_cached_list
.prev
, 0,0);
2539 queue_remove(&vm_object_cached_list
, object
,
2540 vm_object_t
, cached_list
);
2541 vm_object_cached_count
--;
2543 object
->ref_count
++;
2544 vm_object_res_reference(object
);
2546 object
->can_persist
= FALSE
;
2548 assert(object
->pager
== pager
);
2551 * Remove the pager association.
2553 * Note that the memory_object itself is dead, so
2554 * we don't bother with it.
2557 object
->pager
= MEMORY_OBJECT_NULL
;
2559 vm_object_unlock(object
);
2560 vm_object_cache_unlock();
2562 vm_object_pager_wakeup(pager
);
2565 * Release the pager reference. Note that there's no
2566 * point in trying the memory_object_terminate call
2567 * because the memory_object itself is dead. Also
2568 * release the memory_object_control reference, since
2569 * the pager didn't do that either.
2572 memory_object_deallocate(pager
);
2573 memory_object_control_deallocate(object
->pager_request
);
2577 * Restart pending page requests
2579 vm_object_lock(object
);
2580 vm_object_abort_activity(object
);
2581 vm_object_unlock(object
);
2584 * Lose the object reference.
2587 vm_object_deallocate(object
);
2592 * Routine: vm_object_enter
2594 * Find a VM object corresponding to the given
2595 * pager; if no such object exists, create one,
2596 * and initialize the pager.
2600 memory_object_t pager
,
2601 vm_object_size_t size
,
2606 register vm_object_t object
;
2607 vm_object_t new_object
;
2608 boolean_t must_init
;
2609 vm_object_hash_entry_t entry
, new_entry
;
2611 if (pager
== MEMORY_OBJECT_NULL
)
2612 return(vm_object_allocate(size
));
2614 new_object
= VM_OBJECT_NULL
;
2615 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
2619 * Look for an object associated with this port.
2622 vm_object_cache_lock();
2624 entry
= vm_object_hash_lookup(pager
, FALSE
);
2626 if (entry
== VM_OBJECT_HASH_ENTRY_NULL
) {
2627 if (new_object
== VM_OBJECT_NULL
) {
2629 * We must unlock to create a new object;
2630 * if we do so, we must try the lookup again.
2632 vm_object_cache_unlock();
2633 assert(new_entry
== VM_OBJECT_HASH_ENTRY_NULL
);
2634 new_entry
= vm_object_hash_entry_alloc(pager
);
2635 new_object
= vm_object_allocate(size
);
2636 vm_object_cache_lock();
2639 * Lookup failed twice, and we have something
2640 * to insert; set the object.
2642 vm_object_hash_insert(new_entry
);
2644 entry
->object
= new_object
;
2645 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
2646 new_object
= VM_OBJECT_NULL
;
2649 } else if (entry
->object
== VM_OBJECT_NULL
) {
2651 * If a previous object is being terminated,
2652 * we must wait for the termination message
2653 * to be queued (and lookup the entry again).
2655 entry
->waiting
= TRUE
;
2656 entry
= VM_OBJECT_HASH_ENTRY_NULL
;
2657 assert_wait((event_t
) pager
, THREAD_UNINT
);
2658 vm_object_cache_unlock();
2659 thread_block(THREAD_CONTINUE_NULL
);
2660 vm_object_cache_lock();
2662 } while (entry
== VM_OBJECT_HASH_ENTRY_NULL
);
2664 object
= entry
->object
;
2665 assert(object
!= VM_OBJECT_NULL
);
2668 vm_object_lock(object
);
2669 assert(!internal
|| object
->internal
);
2671 assert(!object
->named
);
2672 object
->named
= TRUE
;
2674 if (object
->ref_count
== 0) {
2675 XPR(XPR_VM_OBJECT_CACHE
,
2676 "vm_object_enter: removing %x from cache, head (%x, %x)\n",
2678 (integer_t
)vm_object_cached_list
.next
,
2679 (integer_t
)vm_object_cached_list
.prev
, 0,0);
2680 queue_remove(&vm_object_cached_list
, object
,
2681 vm_object_t
, cached_list
);
2682 vm_object_cached_count
--;
2684 object
->ref_count
++;
2685 vm_object_res_reference(object
);
2686 vm_object_unlock(object
);
2690 assert(object
->ref_count
> 0);
2694 vm_object_cache_unlock();
2697 "vm_o_enter: pager 0x%x obj 0x%x must_init %d\n",
2698 (integer_t
)pager
, (integer_t
)object
, must_init
, 0, 0);
2701 * If we raced to create a vm_object but lost, let's
2705 if (new_object
!= VM_OBJECT_NULL
)
2706 vm_object_deallocate(new_object
);
2708 if (new_entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
2709 vm_object_hash_entry_free(new_entry
);
2712 memory_object_control_t control
;
2715 * Allocate request port.
2718 control
= memory_object_control_allocate(object
);
2719 assert (control
!= MEMORY_OBJECT_CONTROL_NULL
);
2721 vm_object_lock(object
);
2722 assert(object
!= kernel_object
);
2725 * Copy the reference we were given.
2728 memory_object_reference(pager
);
2729 object
->pager_created
= TRUE
;
2730 object
->pager
= pager
;
2731 object
->internal
= internal
;
2732 object
->pager_trusted
= internal
;
2734 /* copy strategy invalid until set by memory manager */
2735 object
->copy_strategy
= MEMORY_OBJECT_COPY_INVALID
;
2737 object
->pager_control
= control
;
2738 object
->pager_ready
= FALSE
;
2740 vm_object_unlock(object
);
2743 * Let the pager know we're using it.
2746 (void) memory_object_init(pager
,
2747 object
->pager_control
,
2750 vm_object_lock(object
);
2752 object
->named
= TRUE
;
2754 object
->pager_ready
= TRUE
;
2755 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
2758 object
->pager_initialized
= TRUE
;
2759 vm_object_wakeup(object
, VM_OBJECT_EVENT_INITIALIZED
);
2761 vm_object_lock(object
);
2765 * [At this point, the object must be locked]
2769 * Wait for the work above to be done by the first
2770 * thread to map this object.
2773 while (!object
->pager_initialized
) {
2774 vm_object_sleep(object
,
2775 VM_OBJECT_EVENT_INITIALIZED
,
2778 vm_object_unlock(object
);
2781 "vm_object_enter: vm_object %x, memory_object %x, internal %d\n",
2782 (integer_t
)object
, (integer_t
)object
->pager
, internal
, 0,0);
2787 * Routine: vm_object_pager_create
2789 * Create a memory object for an internal object.
2790 * In/out conditions:
2791 * The object is locked on entry and exit;
2792 * it may be unlocked within this call.
2794 * Only one thread may be performing a
2795 * vm_object_pager_create on an object at
2796 * a time. Presumably, only the pageout
2797 * daemon will be using this routine.
2801 vm_object_pager_create(
2802 register vm_object_t object
)
2804 memory_object_t pager
;
2805 vm_object_hash_entry_t entry
;
2807 vm_object_size_t size
;
2808 vm_external_map_t map
;
2809 #endif /* MACH_PAGEMAP */
2811 XPR(XPR_VM_OBJECT
, "vm_object_pager_create, object 0x%X\n",
2812 (integer_t
)object
, 0,0,0,0);
2814 assert(object
!= kernel_object
);
2816 if (memory_manager_default_check() != KERN_SUCCESS
)
2820 * Prevent collapse or termination by holding a paging reference
2823 vm_object_paging_begin(object
);
2824 if (object
->pager_created
) {
2826 * Someone else got to it first...
2827 * wait for them to finish initializing the ports
2829 while (!object
->pager_initialized
) {
2830 vm_object_sleep(object
,
2831 VM_OBJECT_EVENT_INITIALIZED
,
2834 vm_object_paging_end(object
);
2839 * Indicate that a memory object has been assigned
2840 * before dropping the lock, to prevent a race.
2843 object
->pager_created
= TRUE
;
2844 object
->paging_offset
= 0;
2847 size
= object
->size
;
2848 #endif /* MACH_PAGEMAP */
2849 vm_object_unlock(object
);
2852 map
= vm_external_create(size
);
2853 vm_object_lock(object
);
2854 assert(object
->size
== size
);
2855 object
->existence_map
= map
;
2856 vm_object_unlock(object
);
2857 #endif /* MACH_PAGEMAP */
2860 * Create the [internal] pager, and associate it with this object.
2862 * We make the association here so that vm_object_enter()
2863 * can look up the object to complete initializing it. No
2864 * user will ever map this object.
2867 memory_object_default_t dmm
;
2868 vm_size_t cluster_size
;
2870 /* acquire a reference for the default memory manager */
2871 dmm
= memory_manager_default_reference(&cluster_size
);
2872 assert(cluster_size
>= PAGE_SIZE
);
2874 object
->cluster_size
= cluster_size
; /* XXX ??? */
2875 assert(object
->temporary
);
2877 /* create our new memory object */
2878 (void) memory_object_create(dmm
, object
->size
, &pager
);
2880 memory_object_default_deallocate(dmm
);
2883 entry
= vm_object_hash_entry_alloc(pager
);
2885 vm_object_cache_lock();
2886 vm_object_hash_insert(entry
);
2888 entry
->object
= object
;
2889 vm_object_cache_unlock();
2892 * A reference was returned by
2893 * memory_object_create(), and it is
2894 * copied by vm_object_enter().
2897 if (vm_object_enter(pager
, object
->size
, TRUE
, TRUE
, FALSE
) != object
)
2898 panic("vm_object_pager_create: mismatch");
2901 * Drop the reference we were passed.
2903 memory_object_deallocate(pager
);
2905 vm_object_lock(object
);
2908 * Release the paging reference
2910 vm_object_paging_end(object
);
2914 * Routine: vm_object_remove
2916 * Eliminate the pager/object association
2919 * The object cache must be locked.
2921 __private_extern__
void
2925 memory_object_t pager
;
2927 if ((pager
= object
->pager
) != MEMORY_OBJECT_NULL
) {
2928 vm_object_hash_entry_t entry
;
2930 entry
= vm_object_hash_lookup(pager
, FALSE
);
2931 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
2932 entry
->object
= VM_OBJECT_NULL
;
2938 * Global variables for vm_object_collapse():
2940 * Counts for normal collapses and bypasses.
2941 * Debugging variables, to watch or disable collapse.
2943 static long object_collapses
= 0;
2944 static long object_bypasses
= 0;
2946 static boolean_t vm_object_collapse_allowed
= TRUE
;
2947 static boolean_t vm_object_bypass_allowed
= TRUE
;
2949 static int vm_external_discarded
;
2950 static int vm_external_collapsed
;
2952 unsigned long vm_object_collapse_encrypted
= 0;
2955 * Routine: vm_object_do_collapse
2957 * Collapse an object with the object backing it.
2958 * Pages in the backing object are moved into the
2959 * parent, and the backing object is deallocated.
2961 * Both objects and the cache are locked; the page
2962 * queues are unlocked.
2966 vm_object_do_collapse(
2968 vm_object_t backing_object
)
2971 vm_object_offset_t new_offset
, backing_offset
;
2972 vm_object_size_t size
;
2974 backing_offset
= object
->shadow_offset
;
2975 size
= object
->size
;
2978 * Move all in-memory pages from backing_object
2979 * to the parent. Pages that have been paged out
2980 * will be overwritten by any of the parent's
2981 * pages that shadow them.
2984 while (!queue_empty(&backing_object
->memq
)) {
2986 p
= (vm_page_t
) queue_first(&backing_object
->memq
);
2988 new_offset
= (p
->offset
- backing_offset
);
2990 assert(!p
->busy
|| p
->absent
);
2993 * If the parent has a page here, or if
2994 * this page falls outside the parent,
2997 * Otherwise, move it as planned.
3000 if (p
->offset
< backing_offset
|| new_offset
>= size
) {
3005 * The encryption key includes the "pager" and the
3006 * "paging_offset". These might not be the same in
3007 * the new object, so we can't just move an encrypted
3008 * page from one object to the other. We can't just
3009 * decrypt the page here either, because that would drop
3011 * The caller should check for encrypted pages before
3012 * attempting to collapse.
3014 ASSERT_PAGE_DECRYPTED(p
);
3016 pp
= vm_page_lookup(object
, new_offset
);
3017 if (pp
== VM_PAGE_NULL
) {
3020 * Parent now has no page.
3021 * Move the backing object's page up.
3024 vm_page_rename(p
, object
, new_offset
);
3026 } else if (pp
->absent
) {
3029 * Parent has an absent page...
3030 * it's not being paged in, so
3031 * it must really be missing from
3034 * Throw out the absent page...
3035 * any faults looking for that
3036 * page will restart with the new
3041 vm_page_rename(p
, object
, new_offset
);
3042 #endif /* MACH_PAGEMAP */
3044 assert(! pp
->absent
);
3047 * Parent object has a real page.
3048 * Throw away the backing object's
3057 assert(!object
->pager_created
&& object
->pager
== MEMORY_OBJECT_NULL
3058 || (!backing_object
->pager_created
3059 && backing_object
->pager
== MEMORY_OBJECT_NULL
));
3061 assert(!object
->pager_created
&& object
->pager
== MEMORY_OBJECT_NULL
);
3062 #endif /* !MACH_PAGEMAP */
3064 if (backing_object
->pager
!= MEMORY_OBJECT_NULL
) {
3065 vm_object_hash_entry_t entry
;
3068 * Move the pager from backing_object to object.
3070 * XXX We're only using part of the paging space
3071 * for keeps now... we ought to discard the
3075 assert(!object
->paging_in_progress
);
3076 object
->pager
= backing_object
->pager
;
3077 entry
= vm_object_hash_lookup(object
->pager
, FALSE
);
3078 assert(entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
3079 entry
->object
= object
;
3080 object
->pager_created
= backing_object
->pager_created
;
3081 object
->pager_control
= backing_object
->pager_control
;
3082 object
->pager_ready
= backing_object
->pager_ready
;
3083 object
->pager_initialized
= backing_object
->pager_initialized
;
3084 object
->cluster_size
= backing_object
->cluster_size
;
3085 object
->paging_offset
=
3086 backing_object
->paging_offset
+ backing_offset
;
3087 if (object
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
3088 memory_object_control_collapse(object
->pager_control
,
3093 vm_object_cache_unlock();
3097 * If the shadow offset is 0, the use the existence map from
3098 * the backing object if there is one. If the shadow offset is
3099 * not zero, toss it.
3101 * XXX - If the shadow offset is not 0 then a bit copy is needed
3102 * if the map is to be salvaged. For now, we just just toss the
3103 * old map, giving the collapsed object no map. This means that
3104 * the pager is invoked for zero fill pages. If analysis shows
3105 * that this happens frequently and is a performance hit, then
3106 * this code should be fixed to salvage the map.
3108 assert(object
->existence_map
== VM_EXTERNAL_NULL
);
3109 if (backing_offset
|| (size
!= backing_object
->size
)) {
3110 vm_external_discarded
++;
3111 vm_external_destroy(backing_object
->existence_map
,
3112 backing_object
->size
);
3115 vm_external_collapsed
++;
3116 object
->existence_map
= backing_object
->existence_map
;
3118 backing_object
->existence_map
= VM_EXTERNAL_NULL
;
3119 #endif /* MACH_PAGEMAP */
3122 * Object now shadows whatever backing_object did.
3123 * Note that the reference to backing_object->shadow
3124 * moves from within backing_object to within object.
3127 assert(!object
->phys_contiguous
);
3128 assert(!backing_object
->phys_contiguous
);
3129 object
->shadow
= backing_object
->shadow
;
3130 if (object
->shadow
) {
3131 object
->shadow_offset
+= backing_object
->shadow_offset
;
3133 /* no shadow, therefore no shadow offset... */
3134 object
->shadow_offset
= 0;
3136 assert((object
->shadow
== VM_OBJECT_NULL
) ||
3137 (object
->shadow
->copy
!= backing_object
));
3140 * Discard backing_object.
3142 * Since the backing object has no pages, no
3143 * pager left, and no object references within it,
3144 * all that is necessary is to dispose of it.
3147 assert((backing_object
->ref_count
== 1) &&
3148 (backing_object
->resident_page_count
== 0) &&
3149 (backing_object
->paging_in_progress
== 0));
3151 backing_object
->alive
= FALSE
;
3152 vm_object_unlock(backing_object
);
3154 XPR(XPR_VM_OBJECT
, "vm_object_collapse, collapsed 0x%X\n",
3155 (integer_t
)backing_object
, 0,0,0,0);
3157 zfree(vm_object_zone
, backing_object
);
3163 vm_object_do_bypass(
3165 vm_object_t backing_object
)
3168 * Make the parent shadow the next object
3174 * Do object reference in-line to
3175 * conditionally increment shadow's
3176 * residence count. If object is not
3177 * resident, leave residence count
3180 if (backing_object
->shadow
!= VM_OBJECT_NULL
) {
3181 vm_object_lock(backing_object
->shadow
);
3182 backing_object
->shadow
->ref_count
++;
3183 if (object
->res_count
!= 0)
3184 vm_object_res_reference(backing_object
->shadow
);
3185 vm_object_unlock(backing_object
->shadow
);
3187 #else /* TASK_SWAPPER */
3188 vm_object_reference(backing_object
->shadow
);
3189 #endif /* TASK_SWAPPER */
3191 assert(!object
->phys_contiguous
);
3192 assert(!backing_object
->phys_contiguous
);
3193 object
->shadow
= backing_object
->shadow
;
3194 if (object
->shadow
) {
3195 object
->shadow_offset
+= backing_object
->shadow_offset
;
3197 /* no shadow, therefore no shadow offset... */
3198 object
->shadow_offset
= 0;
3202 * Backing object might have had a copy pointer
3203 * to us. If it did, clear it.
3205 if (backing_object
->copy
== object
) {
3206 backing_object
->copy
= VM_OBJECT_NULL
;
3210 * Drop the reference count on backing_object.
3212 * Since its ref_count was at least 2, it
3213 * will not vanish; so we don't need to call
3214 * vm_object_deallocate.
3215 * [FBDP: that doesn't seem to be true any more]
3217 * The res_count on the backing object is
3218 * conditionally decremented. It's possible
3219 * (via vm_pageout_scan) to get here with
3220 * a "swapped" object, which has a 0 res_count,
3221 * in which case, the backing object res_count
3222 * is already down by one.
3224 * Don't call vm_object_deallocate unless
3225 * ref_count drops to zero.
3227 * The ref_count can drop to zero here if the
3228 * backing object could be bypassed but not
3229 * collapsed, such as when the backing object
3230 * is temporary and cachable.
3233 if (backing_object
->ref_count
> 1) {
3234 backing_object
->ref_count
--;
3236 if (object
->res_count
!= 0)
3237 vm_object_res_deallocate(backing_object
);
3238 assert(backing_object
->ref_count
> 0);
3239 #endif /* TASK_SWAPPER */
3240 vm_object_unlock(backing_object
);
3244 * Drop locks so that we can deallocate
3245 * the backing object.
3249 if (object
->res_count
== 0) {
3250 /* XXX get a reference for the deallocate below */
3251 vm_object_res_reference(backing_object
);
3253 #endif /* TASK_SWAPPER */
3254 vm_object_unlock(object
);
3255 vm_object_unlock(backing_object
);
3256 vm_object_deallocate(backing_object
);
3259 * Relock object. We don't have to reverify
3260 * its state since vm_object_collapse will
3261 * do that for us as it starts at the
3265 vm_object_lock(object
);
3273 * vm_object_collapse:
3275 * Perform an object collapse or an object bypass if appropriate.
3276 * The real work of collapsing and bypassing is performed in
3277 * the routines vm_object_do_collapse and vm_object_do_bypass.
3279 * Requires that the object be locked and the page queues be unlocked.
3282 static unsigned long vm_object_collapse_calls
= 0;
3283 static unsigned long vm_object_collapse_objects
= 0;
3284 static unsigned long vm_object_collapse_do_collapse
= 0;
3285 static unsigned long vm_object_collapse_do_bypass
= 0;
3286 __private_extern__
void
3288 register vm_object_t object
,
3289 register vm_object_offset_t hint_offset
)
3291 register vm_object_t backing_object
;
3292 register unsigned int rcount
;
3293 register unsigned int size
;
3294 vm_object_offset_t collapse_min_offset
;
3295 vm_object_offset_t collapse_max_offset
;
3297 vm_object_t original_object
;
3299 vm_object_collapse_calls
++;
3301 if (! vm_object_collapse_allowed
&& ! vm_object_bypass_allowed
) {
3305 XPR(XPR_VM_OBJECT
, "vm_object_collapse, obj 0x%X\n",
3306 (integer_t
)object
, 0,0,0,0);
3308 if (object
== VM_OBJECT_NULL
)
3311 original_object
= object
;
3314 vm_object_collapse_objects
++;
3316 * Verify that the conditions are right for either
3317 * collapse or bypass:
3321 * There is a backing object, and
3324 backing_object
= object
->shadow
;
3325 if (backing_object
== VM_OBJECT_NULL
) {
3326 if (object
!= original_object
) {
3327 vm_object_unlock(object
);
3333 * No pages in the object are currently
3334 * being paged out, and
3336 if (object
->paging_in_progress
!= 0 ||
3337 object
->absent_count
!= 0) {
3338 /* try and collapse the rest of the shadow chain */
3339 vm_object_lock(backing_object
);
3340 if (object
!= original_object
) {
3341 vm_object_unlock(object
);
3343 object
= backing_object
;
3347 vm_object_lock(backing_object
);
3351 * The backing object is not read_only,
3352 * and no pages in the backing object are
3353 * currently being paged out.
3354 * The backing object is internal.
3358 if (!backing_object
->internal
||
3359 backing_object
->paging_in_progress
!= 0) {
3360 /* try and collapse the rest of the shadow chain */
3361 if (object
!= original_object
) {
3362 vm_object_unlock(object
);
3364 object
= backing_object
;
3369 * The backing object can't be a copy-object:
3370 * the shadow_offset for the copy-object must stay
3371 * as 0. Furthermore (for the 'we have all the
3372 * pages' case), if we bypass backing_object and
3373 * just shadow the next object in the chain, old
3374 * pages from that object would then have to be copied
3375 * BOTH into the (former) backing_object and into the
3378 if (backing_object
->shadow
!= VM_OBJECT_NULL
&&
3379 backing_object
->shadow
->copy
== backing_object
) {
3380 /* try and collapse the rest of the shadow chain */
3381 if (object
!= original_object
) {
3382 vm_object_unlock(object
);
3384 object
= backing_object
;
3389 * We can now try to either collapse the backing
3390 * object (if the parent is the only reference to
3391 * it) or (perhaps) remove the parent's reference
3394 * If there is exactly one reference to the backing
3395 * object, we may be able to collapse it into the
3398 * If MACH_PAGEMAP is defined:
3399 * The parent must not have a pager created for it,
3400 * since collapsing a backing_object dumps new pages
3401 * into the parent that its pager doesn't know about
3402 * (and the collapse code can't merge the existence
3405 * As long as one of the objects is still not known
3406 * to the pager, we can collapse them.
3408 if (backing_object
->ref_count
== 1 &&
3409 (!object
->pager_created
3411 || !backing_object
->pager_created
3412 #endif /*!MACH_PAGEMAP */
3413 ) && vm_object_collapse_allowed
) {
3416 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
3417 (integer_t
)backing_object
, (integer_t
)object
,
3418 (integer_t
)backing_object
->pager
,
3419 (integer_t
)backing_object
->pager_control
, 0);
3422 * We need the cache lock for collapsing,
3423 * but we must not deadlock.
3426 if (! vm_object_cache_lock_try()) {
3427 if (object
!= original_object
) {
3428 vm_object_unlock(object
);
3430 vm_object_unlock(backing_object
);
3436 * We can't collapse the object if it contains
3437 * any encypted page, because the encryption key
3438 * includes the <object,offset> info. We can't
3439 * drop the object lock in vm_object_do_collapse()
3440 * so we can't decrypt the page there either.
3442 if (vm_pages_encrypted
) {
3443 collapse_min_offset
= object
->shadow_offset
;
3444 collapse_max_offset
=
3445 object
->shadow_offset
+ object
->size
;
3446 queue_iterate(&backing_object
->memq
,
3447 page
, vm_page_t
, listq
) {
3448 if (page
->encrypted
&&
3450 collapse_min_offset
) &&
3452 collapse_max_offset
)) {
3454 * We found an encrypted page
3455 * in the backing object,
3456 * within the range covered
3457 * by the parent object: we can
3458 * not collapse them.
3460 vm_object_collapse_encrypted
++;
3461 vm_object_cache_unlock();
3468 * Collapse the object with its backing
3469 * object, and try again with the object's
3470 * new backing object.
3473 vm_object_do_collapse(object
, backing_object
);
3474 vm_object_collapse_do_collapse
++;
3480 * Collapsing the backing object was not possible
3481 * or permitted, so let's try bypassing it.
3484 if (! vm_object_bypass_allowed
) {
3485 /* try and collapse the rest of the shadow chain */
3486 if (object
!= original_object
) {
3487 vm_object_unlock(object
);
3489 object
= backing_object
;
3495 * If the object doesn't have all its pages present,
3496 * we have to make sure no pages in the backing object
3497 * "show through" before bypassing it.
3499 size
= atop(object
->size
);
3500 rcount
= object
->resident_page_count
;
3501 if (rcount
!= size
) {
3502 vm_object_offset_t offset
;
3503 vm_object_offset_t backing_offset
;
3504 unsigned int backing_rcount
;
3505 unsigned int lookups
= 0;
3508 * If the backing object has a pager but no pagemap,
3509 * then we cannot bypass it, because we don't know
3510 * what pages it has.
3512 if (backing_object
->pager_created
3514 && (backing_object
->existence_map
== VM_EXTERNAL_NULL
)
3515 #endif /* MACH_PAGEMAP */
3517 /* try and collapse the rest of the shadow chain */
3518 if (object
!= original_object
) {
3519 vm_object_unlock(object
);
3521 object
= backing_object
;
3526 * If the object has a pager but no pagemap,
3527 * then we cannot bypass it, because we don't know
3528 * what pages it has.
3530 if (object
->pager_created
3532 && (object
->existence_map
== VM_EXTERNAL_NULL
)
3533 #endif /* MACH_PAGEMAP */
3535 /* try and collapse the rest of the shadow chain */
3536 if (object
!= original_object
) {
3537 vm_object_unlock(object
);
3539 object
= backing_object
;
3544 * If all of the pages in the backing object are
3545 * shadowed by the parent object, the parent
3546 * object no longer has to shadow the backing
3547 * object; it can shadow the next one in the
3550 * If the backing object has existence info,
3551 * we must check examine its existence info
3556 backing_offset
= object
->shadow_offset
;
3557 backing_rcount
= backing_object
->resident_page_count
;
3559 #define EXISTS_IN_OBJECT(obj, off, rc) \
3560 (vm_external_state_get((obj)->existence_map, \
3561 (vm_offset_t)(off)) == VM_EXTERNAL_STATE_EXISTS || \
3562 ((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
3565 * Check the hint location first
3566 * (since it is often the quickest way out of here).
3568 if (object
->cow_hint
!= ~(vm_offset_t
)0)
3569 hint_offset
= (vm_object_offset_t
)object
->cow_hint
;
3571 hint_offset
= (hint_offset
> 8 * PAGE_SIZE_64
) ?
3572 (hint_offset
- 8 * PAGE_SIZE_64
) : 0;
3574 if (EXISTS_IN_OBJECT(backing_object
, hint_offset
+
3575 backing_offset
, backing_rcount
) &&
3576 !EXISTS_IN_OBJECT(object
, hint_offset
, rcount
)) {
3577 /* dependency right at the hint */
3578 object
->cow_hint
= (vm_offset_t
)hint_offset
;
3579 /* try and collapse the rest of the shadow chain */
3580 if (object
!= original_object
) {
3581 vm_object_unlock(object
);
3583 object
= backing_object
;
3588 * If the object's window onto the backing_object
3589 * is large compared to the number of resident
3590 * pages in the backing object, it makes sense to
3591 * walk the backing_object's resident pages first.
3593 * NOTE: Pages may be in both the existence map and
3594 * resident. So, we can't permanently decrement
3595 * the rcount here because the second loop may
3596 * find the same pages in the backing object'
3597 * existence map that we found here and we would
3598 * double-decrement the rcount. We also may or
3599 * may not have found the
3601 if (backing_rcount
&& size
>
3602 ((backing_object
->existence_map
) ?
3603 backing_rcount
: (backing_rcount
>> 1))) {
3604 unsigned int rc
= rcount
;
3607 backing_rcount
= backing_object
->resident_page_count
;
3608 p
= (vm_page_t
)queue_first(&backing_object
->memq
);
3610 /* Until we get more than one lookup lock */
3611 if (lookups
> 256) {
3616 offset
= (p
->offset
- backing_offset
);
3617 if (offset
< object
->size
&&
3618 offset
!= hint_offset
&&
3619 !EXISTS_IN_OBJECT(object
, offset
, rc
)) {
3620 /* found a dependency */
3621 object
->cow_hint
= (vm_offset_t
)offset
;
3624 p
= (vm_page_t
) queue_next(&p
->listq
);
3626 } while (--backing_rcount
);
3627 if (backing_rcount
!= 0 ) {
3628 /* try and collapse the rest of the shadow chain */
3629 if (object
!= original_object
) {
3630 vm_object_unlock(object
);
3632 object
= backing_object
;
3638 * Walk through the offsets looking for pages in the
3639 * backing object that show through to the object.
3641 if (backing_rcount
|| backing_object
->existence_map
) {
3642 offset
= hint_offset
;
3645 (offset
+ PAGE_SIZE_64
< object
->size
) ?
3646 (offset
+ PAGE_SIZE_64
) : 0) != hint_offset
) {
3648 /* Until we get more than one lookup lock */
3649 if (lookups
> 256) {
3654 if (EXISTS_IN_OBJECT(backing_object
, offset
+
3655 backing_offset
, backing_rcount
) &&
3656 !EXISTS_IN_OBJECT(object
, offset
, rcount
)) {
3657 /* found a dependency */
3658 object
->cow_hint
= (vm_offset_t
)offset
;
3662 if (offset
!= hint_offset
) {
3663 /* try and collapse the rest of the shadow chain */
3664 if (object
!= original_object
) {
3665 vm_object_unlock(object
);
3667 object
= backing_object
;
3673 /* reset the offset hint for any objects deeper in the chain */
3674 object
->cow_hint
= (vm_offset_t
)0;
3677 * All interesting pages in the backing object
3678 * already live in the parent or its pager.
3679 * Thus we can bypass the backing object.
3682 vm_object_do_bypass(object
, backing_object
);
3683 vm_object_collapse_do_bypass
++;
3686 * Try again with this object's new backing object.
3692 if (object
!= original_object
) {
3693 vm_object_unlock(object
);
3698 * Routine: vm_object_page_remove: [internal]
3700 * Removes all physical pages in the specified
3701 * object range from the object's list of pages.
3703 * In/out conditions:
3704 * The object must be locked.
3705 * The object must not have paging_in_progress, usually
3706 * guaranteed by not having a pager.
3708 unsigned int vm_object_page_remove_lookup
= 0;
3709 unsigned int vm_object_page_remove_iterate
= 0;
3711 __private_extern__
void
3712 vm_object_page_remove(
3713 register vm_object_t object
,
3714 register vm_object_offset_t start
,
3715 register vm_object_offset_t end
)
3717 register vm_page_t p
, next
;
3720 * One and two page removals are most popular.
3721 * The factor of 16 here is somewhat arbitrary.
3722 * It balances vm_object_lookup vs iteration.
3725 if (atop_64(end
- start
) < (unsigned)object
->resident_page_count
/16) {
3726 vm_object_page_remove_lookup
++;
3728 for (; start
< end
; start
+= PAGE_SIZE_64
) {
3729 p
= vm_page_lookup(object
, start
);
3730 if (p
!= VM_PAGE_NULL
) {
3731 assert(!p
->cleaning
&& !p
->pageout
);
3733 pmap_disconnect(p
->phys_page
);
3738 vm_object_page_remove_iterate
++;
3740 p
= (vm_page_t
) queue_first(&object
->memq
);
3741 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
3742 next
= (vm_page_t
) queue_next(&p
->listq
);
3743 if ((start
<= p
->offset
) && (p
->offset
< end
)) {
3744 assert(!p
->cleaning
&& !p
->pageout
);
3746 pmap_disconnect(p
->phys_page
);
3756 * Routine: vm_object_coalesce
3757 * Function: Coalesces two objects backing up adjoining
3758 * regions of memory into a single object.
3760 * returns TRUE if objects were combined.
3762 * NOTE: Only works at the moment if the second object is NULL -
3763 * if it's not, which object do we lock first?
3766 * prev_object First object to coalesce
3767 * prev_offset Offset into prev_object
3768 * next_object Second object into coalesce
3769 * next_offset Offset into next_object
3771 * prev_size Size of reference to prev_object
3772 * next_size Size of reference to next_object
3775 * The object(s) must *not* be locked. The map must be locked
3776 * to preserve the reference to the object(s).
3778 static int vm_object_coalesce_count
= 0;
3780 __private_extern__ boolean_t
3782 register vm_object_t prev_object
,
3783 vm_object_t next_object
,
3784 vm_object_offset_t prev_offset
,
3785 __unused vm_object_offset_t next_offset
,
3786 vm_object_size_t prev_size
,
3787 vm_object_size_t next_size
)
3789 vm_object_size_t newsize
;
3795 if (next_object
!= VM_OBJECT_NULL
) {
3799 if (prev_object
== VM_OBJECT_NULL
) {
3804 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
3805 (integer_t
)prev_object
, prev_offset
, prev_size
, next_size
, 0);
3807 vm_object_lock(prev_object
);
3810 * Try to collapse the object first
3812 vm_object_collapse(prev_object
, prev_offset
);
3815 * Can't coalesce if pages not mapped to
3816 * prev_entry may be in use any way:
3817 * . more than one reference
3819 * . shadows another object
3820 * . has a copy elsewhere
3822 * . paging references (pages might be in page-list)
3825 if ((prev_object
->ref_count
> 1) ||
3826 prev_object
->pager_created
||
3827 (prev_object
->shadow
!= VM_OBJECT_NULL
) ||
3828 (prev_object
->copy
!= VM_OBJECT_NULL
) ||
3829 (prev_object
->true_share
!= FALSE
) ||
3830 (prev_object
->purgable
!= VM_OBJECT_NONPURGABLE
) ||
3831 (prev_object
->paging_in_progress
!= 0)) {
3832 vm_object_unlock(prev_object
);
3836 vm_object_coalesce_count
++;
3839 * Remove any pages that may still be in the object from
3840 * a previous deallocation.
3842 vm_object_page_remove(prev_object
,
3843 prev_offset
+ prev_size
,
3844 prev_offset
+ prev_size
+ next_size
);
3847 * Extend the object if necessary.
3849 newsize
= prev_offset
+ prev_size
+ next_size
;
3850 if (newsize
> prev_object
->size
) {
3853 * We cannot extend an object that has existence info,
3854 * since the existence info might then fail to cover
3855 * the entire object.
3857 * This assertion must be true because the object
3858 * has no pager, and we only create existence info
3859 * for objects with pagers.
3861 assert(prev_object
->existence_map
== VM_EXTERNAL_NULL
);
3862 #endif /* MACH_PAGEMAP */
3863 prev_object
->size
= newsize
;
3866 vm_object_unlock(prev_object
);
3871 * Attach a set of physical pages to an object, so that they can
3872 * be mapped by mapping the object. Typically used to map IO memory.
3874 * The mapping function and its private data are used to obtain the
3875 * physical addresses for each page to be mapped.
3880 vm_object_offset_t offset
,
3881 vm_object_size_t size
,
3882 vm_object_offset_t (*map_fn
)(void *map_fn_data
,
3883 vm_object_offset_t offset
),
3884 void *map_fn_data
) /* private to map_fn */
3890 vm_object_offset_t addr
;
3892 num_pages
= atop_64(size
);
3894 for (i
= 0; i
< num_pages
; i
++, offset
+= PAGE_SIZE_64
) {
3896 addr
= (*map_fn
)(map_fn_data
, offset
);
3898 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
3899 vm_page_more_fictitious();
3901 vm_object_lock(object
);
3902 if ((old_page
= vm_page_lookup(object
, offset
))
3905 vm_page_lock_queues();
3906 vm_page_free(old_page
);
3907 vm_page_unlock_queues();
3910 vm_page_init(m
, addr
);
3911 /* private normally requires lock_queues but since we */
3912 /* are initializing the page, its not necessary here */
3913 m
->private = TRUE
; /* don`t free page */
3915 vm_page_insert(m
, object
, offset
);
3917 PAGE_WAKEUP_DONE(m
);
3918 vm_object_unlock(object
);
3922 #include <mach_kdb.h>
3925 #include <ddb/db_output.h>
3926 #include <vm/vm_print.h>
3928 #define printf kdbprintf
3930 extern boolean_t
vm_object_cached(
3931 vm_object_t object
);
3933 extern void print_bitstring(
3936 boolean_t vm_object_print_pages
= FALSE
;
3942 printf("%c%c%c%c%c%c%c%c",
3943 ((byte
& (1 << 0)) ? '1' : '0'),
3944 ((byte
& (1 << 1)) ? '1' : '0'),
3945 ((byte
& (1 << 2)) ? '1' : '0'),
3946 ((byte
& (1 << 3)) ? '1' : '0'),
3947 ((byte
& (1 << 4)) ? '1' : '0'),
3948 ((byte
& (1 << 5)) ? '1' : '0'),
3949 ((byte
& (1 << 6)) ? '1' : '0'),
3950 ((byte
& (1 << 7)) ? '1' : '0'));
3955 register vm_object_t object
)
3957 register vm_object_t o
;
3959 queue_iterate(&vm_object_cached_list
, o
, vm_object_t
, cached_list
) {
3969 * vm_external_print: [ debug ]
3973 vm_external_map_t emap
,
3976 if (emap
== VM_EXTERNAL_NULL
) {
3979 vm_size_t existence_size
= stob(size
);
3980 printf("{ size=%d, map=[", existence_size
);
3981 if (existence_size
> 0) {
3982 print_bitstring(emap
[0]);
3984 if (existence_size
> 1) {
3985 print_bitstring(emap
[1]);
3987 if (existence_size
> 2) {
3989 print_bitstring(emap
[existence_size
-1]);
3995 #endif /* MACH_PAGEMAP */
4002 int orig_db_indent
= db_indent
;
4005 if (object
== VM_OBJECT_NULL
) {
4006 db_indent
= orig_db_indent
;
4012 iprintf("object 0x%x", object
);
4013 printf(", shadow=0x%x", object
->shadow
);
4014 printf(", copy=0x%x", object
->copy
);
4015 printf(", pager=0x%x", object
->pager
);
4016 printf(", ref=%d\n", object
->ref_count
);
4019 object
= object
->shadow
;
4025 * vm_object_print: [ debug ]
4030 __unused boolean_t have_addr
,
4031 __unused
int arg_count
,
4032 __unused
char *modif
)
4035 register vm_page_t p
;
4040 object
= (vm_object_t
) (long) db_addr
;
4041 if (object
== VM_OBJECT_NULL
)
4044 iprintf("object 0x%x\n", object
);
4048 iprintf("size=0x%x", object
->size
);
4049 printf(", cluster=0x%x", object
->cluster_size
);
4050 printf(", memq_hint=%p", object
->memq_hint
);
4051 printf(", ref_count=%d\n", object
->ref_count
);
4054 printf("res_count=%d, ", object
->res_count
);
4055 #endif /* TASK_SWAPPER */
4056 printf("resident_page_count=%d\n", object
->resident_page_count
);
4058 iprintf("shadow=0x%x", object
->shadow
);
4059 if (object
->shadow
) {
4061 vm_object_t shadow
= object
;
4062 while((shadow
= shadow
->shadow
))
4064 printf(" (depth %d)", i
);
4066 printf(", copy=0x%x", object
->copy
);
4067 printf(", shadow_offset=0x%x", object
->shadow_offset
);
4068 printf(", last_alloc=0x%x\n", object
->last_alloc
);
4070 iprintf("pager=0x%x", object
->pager
);
4071 printf(", paging_offset=0x%x", object
->paging_offset
);
4072 printf(", pager_control=0x%x\n", object
->pager_control
);
4074 iprintf("copy_strategy=%d[", object
->copy_strategy
);
4075 switch (object
->copy_strategy
) {
4076 case MEMORY_OBJECT_COPY_NONE
:
4077 printf("copy_none");
4080 case MEMORY_OBJECT_COPY_CALL
:
4081 printf("copy_call");
4084 case MEMORY_OBJECT_COPY_DELAY
:
4085 printf("copy_delay");
4088 case MEMORY_OBJECT_COPY_SYMMETRIC
:
4089 printf("copy_symmetric");
4092 case MEMORY_OBJECT_COPY_INVALID
:
4093 printf("copy_invalid");
4100 printf(", absent_count=%d\n", object
->absent_count
);
4102 iprintf("all_wanted=0x%x<", object
->all_wanted
);
4104 if (vm_object_wanted(object
, VM_OBJECT_EVENT_INITIALIZED
)) {
4105 printf("%sinit", s
);
4108 if (vm_object_wanted(object
, VM_OBJECT_EVENT_PAGER_READY
)) {
4109 printf("%sready", s
);
4112 if (vm_object_wanted(object
, VM_OBJECT_EVENT_PAGING_IN_PROGRESS
)) {
4113 printf("%spaging", s
);
4116 if (vm_object_wanted(object
, VM_OBJECT_EVENT_ABSENT_COUNT
)) {
4117 printf("%sabsent", s
);
4120 if (vm_object_wanted(object
, VM_OBJECT_EVENT_LOCK_IN_PROGRESS
)) {
4121 printf("%slock", s
);
4124 if (vm_object_wanted(object
, VM_OBJECT_EVENT_UNCACHING
)) {
4125 printf("%suncaching", s
);
4128 if (vm_object_wanted(object
, VM_OBJECT_EVENT_COPY_CALL
)) {
4129 printf("%scopy_call", s
);
4132 if (vm_object_wanted(object
, VM_OBJECT_EVENT_CACHING
)) {
4133 printf("%scaching", s
);
4137 printf(", paging_in_progress=%d\n", object
->paging_in_progress
);
4139 iprintf("%screated, %sinit, %sready, %spersist, %strusted, %spageout, %s, %s\n",
4140 (object
->pager_created
? "" : "!"),
4141 (object
->pager_initialized
? "" : "!"),
4142 (object
->pager_ready
? "" : "!"),
4143 (object
->can_persist
? "" : "!"),
4144 (object
->pager_trusted
? "" : "!"),
4145 (object
->pageout
? "" : "!"),
4146 (object
->internal
? "internal" : "external"),
4147 (object
->temporary
? "temporary" : "permanent"));
4148 iprintf("%salive, %spurgable, %spurgable_volatile, %spurgable_empty, %sshadowed, %scached, %sprivate\n",
4149 (object
->alive
? "" : "!"),
4150 ((object
->purgable
!= VM_OBJECT_NONPURGABLE
) ? "" : "!"),
4151 ((object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
) ? "" : "!"),
4152 ((object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) ? "" : "!"),
4153 (object
->shadowed
? "" : "!"),
4154 (vm_object_cached(object
) ? "" : "!"),
4155 (object
->private ? "" : "!"));
4156 iprintf("%sadvisory_pageout, %ssilent_overwrite\n",
4157 (object
->advisory_pageout
? "" : "!"),
4158 (object
->silent_overwrite
? "" : "!"));
4161 iprintf("existence_map=");
4162 vm_external_print(object
->existence_map
, object
->size
);
4163 #endif /* MACH_PAGEMAP */
4165 iprintf("paging_object=0x%x\n", object
->paging_object
);
4166 #endif /* MACH_ASSERT */
4168 if (vm_object_print_pages
) {
4170 p
= (vm_page_t
) queue_first(&object
->memq
);
4171 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
4173 iprintf("memory:=");
4174 } else if (count
== 2) {
4183 printf("(off=0x%llX,page=%p)", p
->offset
, p
);
4184 p
= (vm_page_t
) queue_next(&p
->listq
);
4195 * vm_object_find [ debug ]
4197 * Find all tasks which reference the given vm_object.
4200 boolean_t
vm_object_find(vm_object_t object
);
4201 boolean_t vm_object_print_verbose
= FALSE
;
4209 vm_map_entry_t entry
;
4210 processor_set_t pset
= &default_pset
;
4211 boolean_t found
= FALSE
;
4213 queue_iterate(&pset
->tasks
, task
, task_t
, pset_tasks
) {
4215 for (entry
= vm_map_first_entry(map
);
4216 entry
&& entry
!= vm_map_to_entry(map
);
4217 entry
= entry
->vme_next
) {
4222 * For the time being skip submaps,
4223 * only the kernel can have submaps,
4224 * and unless we are interested in
4225 * kernel objects, we can simply skip
4226 * submaps. See sb/dejan/nmk18b7/src/mach_kernel/vm
4227 * for a full solution.
4229 if (entry
->is_sub_map
)
4232 obj
= entry
->object
.vm_object
;
4236 while (obj
!= VM_OBJECT_NULL
) {
4237 if (obj
== object
) {
4239 printf("TASK\t\tMAP\t\tENTRY\n");
4242 printf("0x%x\t0x%x\t0x%x\n",
4253 #endif /* MACH_KDB */
4256 vm_object_populate_with_private(
4258 vm_object_offset_t offset
,
4263 vm_object_offset_t base_offset
;
4266 if(!object
->private)
4267 return KERN_FAILURE
;
4269 base_page
= phys_page
;
4271 vm_object_lock(object
);
4272 if(!object
->phys_contiguous
) {
4274 if((base_offset
= trunc_page_64(offset
)) != offset
) {
4275 vm_object_unlock(object
);
4276 return KERN_FAILURE
;
4278 base_offset
+= object
->paging_offset
;
4280 m
= vm_page_lookup(object
, base_offset
);
4281 if(m
!= VM_PAGE_NULL
) {
4283 vm_page_lock_queues();
4284 m
->fictitious
= FALSE
;
4286 m
->phys_page
= base_page
;
4292 object
->absent_count
++;
4294 m
->list_req_pending
= TRUE
;
4295 vm_page_unlock_queues();
4296 } else if (m
->phys_page
!= base_page
) {
4297 /* pmap call to clear old mapping */
4298 pmap_disconnect(m
->phys_page
);
4299 m
->phys_page
= base_page
;
4304 * We're not pointing to the same
4305 * physical page any longer and the
4306 * contents of the new one are not
4307 * supposed to be encrypted.
4308 * XXX What happens to the original
4309 * physical page. Is it lost ?
4311 m
->encrypted
= FALSE
;
4314 while ((m
= vm_page_grab_fictitious())
4316 vm_page_more_fictitious();
4317 vm_page_lock_queues();
4318 m
->fictitious
= FALSE
;
4320 m
->phys_page
= base_page
;
4321 m
->list_req_pending
= TRUE
;
4324 object
->absent_count
++;
4325 vm_page_unlock_queues();
4326 vm_page_insert(m
, object
, base_offset
);
4328 base_page
++; /* Go to the next physical page */
4329 base_offset
+= PAGE_SIZE
;
4333 /* NOTE: we should check the original settings here */
4334 /* if we have a size > zero a pmap call should be made */
4335 /* to disable the range */
4339 /* shadows on contiguous memory are not allowed */
4340 /* we therefore can use the offset field */
4341 object
->shadow_offset
= (vm_object_offset_t
)(phys_page
<< 12);
4342 object
->size
= size
;
4344 vm_object_unlock(object
);
4345 return KERN_SUCCESS
;
4349 * memory_object_free_from_cache:
4351 * Walk the vm_object cache list, removing and freeing vm_objects
4352 * which are backed by the pager identified by the caller, (pager_id).
4353 * Remove up to "count" objects, if there are that may available
4356 * Walk the list at most once, return the number of vm_objects
4360 __private_extern__ kern_return_t
4361 memory_object_free_from_cache(
4362 __unused host_t host
,
4367 int object_released
= 0;
4369 register vm_object_t object
= VM_OBJECT_NULL
;
4373 if(host == HOST_NULL)
4374 return(KERN_INVALID_ARGUMENT);
4378 vm_object_cache_lock();
4380 queue_iterate(&vm_object_cached_list
, object
,
4381 vm_object_t
, cached_list
) {
4382 if (object
->pager
&& (pager_id
== object
->pager
->pager
)) {
4383 vm_object_lock(object
);
4384 queue_remove(&vm_object_cached_list
, object
,
4385 vm_object_t
, cached_list
);
4386 vm_object_cached_count
--;
4389 * Since this object is in the cache, we know
4390 * that it is initialized and has only a pager's
4391 * (implicit) reference. Take a reference to avoid
4392 * recursive deallocations.
4395 assert(object
->pager_initialized
);
4396 assert(object
->ref_count
== 0);
4397 object
->ref_count
++;
4400 * Terminate the object.
4401 * If the object had a shadow, we let
4402 * vm_object_deallocate deallocate it.
4403 * "pageout" objects have a shadow, but
4404 * maintain a "paging reference" rather
4405 * than a normal reference.
4406 * (We are careful here to limit recursion.)
4408 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
4409 if ((vm_object_terminate(object
) == KERN_SUCCESS
)
4410 && (shadow
!= VM_OBJECT_NULL
)) {
4411 vm_object_deallocate(shadow
);
4414 if(object_released
++ == *count
)
4415 return KERN_SUCCESS
;
4419 vm_object_cache_unlock();
4420 *count
= object_released
;
4421 return KERN_SUCCESS
;
4427 memory_object_create_named(
4428 memory_object_t pager
,
4429 memory_object_offset_t size
,
4430 memory_object_control_t
*control
)
4433 vm_object_hash_entry_t entry
;
4435 *control
= MEMORY_OBJECT_CONTROL_NULL
;
4436 if (pager
== MEMORY_OBJECT_NULL
)
4437 return KERN_INVALID_ARGUMENT
;
4439 vm_object_cache_lock();
4440 entry
= vm_object_hash_lookup(pager
, FALSE
);
4441 if ((entry
!= VM_OBJECT_HASH_ENTRY_NULL
) &&
4442 (entry
->object
!= VM_OBJECT_NULL
)) {
4443 if (entry
->object
->named
== TRUE
)
4444 panic("memory_object_create_named: caller already holds the right"); }
4446 vm_object_cache_unlock();
4447 if ((object
= vm_object_enter(pager
, size
, FALSE
, FALSE
, TRUE
))
4448 == VM_OBJECT_NULL
) {
4449 return(KERN_INVALID_OBJECT
);
4452 /* wait for object (if any) to be ready */
4453 if (object
!= VM_OBJECT_NULL
) {
4454 vm_object_lock(object
);
4455 object
->named
= TRUE
;
4456 while (!object
->pager_ready
) {
4457 vm_object_sleep(object
,
4458 VM_OBJECT_EVENT_PAGER_READY
,
4461 *control
= object
->pager_control
;
4462 vm_object_unlock(object
);
4464 return (KERN_SUCCESS
);
4469 * Routine: memory_object_recover_named [user interface]
4471 * Attempt to recover a named reference for a VM object.
4472 * VM will verify that the object has not already started
4473 * down the termination path, and if it has, will optionally
4474 * wait for that to finish.
4476 * KERN_SUCCESS - we recovered a named reference on the object
4477 * KERN_FAILURE - we could not recover a reference (object dead)
4478 * KERN_INVALID_ARGUMENT - bad memory object control
4481 memory_object_recover_named(
4482 memory_object_control_t control
,
4483 boolean_t wait_on_terminating
)
4487 vm_object_cache_lock();
4488 object
= memory_object_control_to_vm_object(control
);
4489 if (object
== VM_OBJECT_NULL
) {
4490 vm_object_cache_unlock();
4491 return (KERN_INVALID_ARGUMENT
);
4495 vm_object_lock(object
);
4497 if (object
->terminating
&& wait_on_terminating
) {
4498 vm_object_cache_unlock();
4499 vm_object_wait(object
,
4500 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
4502 vm_object_cache_lock();
4506 if (!object
->alive
) {
4507 vm_object_cache_unlock();
4508 vm_object_unlock(object
);
4509 return KERN_FAILURE
;
4512 if (object
->named
== TRUE
) {
4513 vm_object_cache_unlock();
4514 vm_object_unlock(object
);
4515 return KERN_SUCCESS
;
4518 if((object
->ref_count
== 0) && (!object
->terminating
)){
4519 queue_remove(&vm_object_cached_list
, object
,
4520 vm_object_t
, cached_list
);
4521 vm_object_cached_count
--;
4522 XPR(XPR_VM_OBJECT_CACHE
,
4523 "memory_object_recover_named: removing %X, head (%X, %X)\n",
4525 (integer_t
)vm_object_cached_list
.next
,
4526 (integer_t
)vm_object_cached_list
.prev
, 0,0);
4529 vm_object_cache_unlock();
4531 object
->named
= TRUE
;
4532 object
->ref_count
++;
4533 vm_object_res_reference(object
);
4534 while (!object
->pager_ready
) {
4535 vm_object_sleep(object
,
4536 VM_OBJECT_EVENT_PAGER_READY
,
4539 vm_object_unlock(object
);
4540 return (KERN_SUCCESS
);
4545 * vm_object_release_name:
4547 * Enforces name semantic on memory_object reference count decrement
4548 * This routine should not be called unless the caller holds a name
4549 * reference gained through the memory_object_create_named.
4551 * If the TERMINATE_IDLE flag is set, the call will return if the
4552 * reference count is not 1. i.e. idle with the only remaining reference
4554 * If the decision is made to proceed the name field flag is set to
4555 * false and the reference count is decremented. If the RESPECT_CACHE
4556 * flag is set and the reference count has gone to zero, the
4557 * memory_object is checked to see if it is cacheable otherwise when
4558 * the reference count is zero, it is simply terminated.
4561 __private_extern__ kern_return_t
4562 vm_object_release_name(
4567 boolean_t original_object
= TRUE
;
4569 while (object
!= VM_OBJECT_NULL
) {
4572 * The cache holds a reference (uncounted) to
4573 * the object. We must locke it before removing
4578 vm_object_cache_lock();
4579 vm_object_lock(object
);
4580 assert(object
->alive
);
4582 assert(object
->named
);
4583 assert(object
->ref_count
> 0);
4586 * We have to wait for initialization before
4587 * destroying or caching the object.
4590 if (object
->pager_created
&& !object
->pager_initialized
) {
4591 assert(!object
->can_persist
);
4592 vm_object_assert_wait(object
,
4593 VM_OBJECT_EVENT_INITIALIZED
,
4595 vm_object_unlock(object
);
4596 vm_object_cache_unlock();
4597 thread_block(THREAD_CONTINUE_NULL
);
4601 if (((object
->ref_count
> 1)
4602 && (flags
& MEMORY_OBJECT_TERMINATE_IDLE
))
4603 || (object
->terminating
)) {
4604 vm_object_unlock(object
);
4605 vm_object_cache_unlock();
4606 return KERN_FAILURE
;
4608 if (flags
& MEMORY_OBJECT_RELEASE_NO_OP
) {
4609 vm_object_unlock(object
);
4610 vm_object_cache_unlock();
4611 return KERN_SUCCESS
;
4615 if ((flags
& MEMORY_OBJECT_RESPECT_CACHE
) &&
4616 (object
->ref_count
== 1)) {
4618 object
->named
= FALSE
;
4619 vm_object_unlock(object
);
4620 vm_object_cache_unlock();
4621 /* let vm_object_deallocate push this thing into */
4622 /* the cache, if that it is where it is bound */
4623 vm_object_deallocate(object
);
4624 return KERN_SUCCESS
;
4626 VM_OBJ_RES_DECR(object
);
4627 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
4628 if(object
->ref_count
== 1) {
4629 if(vm_object_terminate(object
) != KERN_SUCCESS
) {
4630 if(original_object
) {
4631 return KERN_FAILURE
;
4633 return KERN_SUCCESS
;
4636 if (shadow
!= VM_OBJECT_NULL
) {
4637 original_object
= FALSE
;
4641 return KERN_SUCCESS
;
4643 object
->ref_count
--;
4644 assert(object
->ref_count
> 0);
4646 object
->named
= FALSE
;
4647 vm_object_unlock(object
);
4648 vm_object_cache_unlock();
4649 return KERN_SUCCESS
;
4654 return KERN_FAILURE
;
4658 __private_extern__ kern_return_t
4659 vm_object_lock_request(
4661 vm_object_offset_t offset
,
4662 vm_object_size_t size
,
4663 memory_object_return_t should_return
,
4667 __unused boolean_t should_flush
;
4669 should_flush
= flags
& MEMORY_OBJECT_DATA_FLUSH
;
4671 XPR(XPR_MEMORY_OBJECT
,
4672 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
4673 (integer_t
)object
, offset
, size
,
4674 (((should_return
&1)<<1)|should_flush
), prot
);
4677 * Check for bogus arguments.
4679 if (object
== VM_OBJECT_NULL
)
4680 return (KERN_INVALID_ARGUMENT
);
4682 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
)
4683 return (KERN_INVALID_ARGUMENT
);
4685 size
= round_page_64(size
);
4688 * Lock the object, and acquire a paging reference to
4689 * prevent the memory_object reference from being released.
4691 vm_object_lock(object
);
4692 vm_object_paging_begin(object
);
4694 (void)vm_object_update(object
,
4695 offset
, size
, NULL
, NULL
, should_return
, flags
, prot
);
4697 vm_object_paging_end(object
);
4698 vm_object_unlock(object
);
4700 return (KERN_SUCCESS
);
4704 * Empty a purgable object by grabbing the physical pages assigned to it and
4705 * putting them on the free queue without writing them to backing store, etc.
4706 * When the pages are next touched they will be demand zero-fill pages. We
4707 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
4708 * skip referenced/dirty pages, pages on the active queue, etc. We're more
4709 * than happy to grab these since this is a purgable object. We mark the
4710 * object as "empty" after reaping its pages.
4712 * On entry the object and page queues are locked, the object must be a
4713 * purgable object with no delayed copies pending.
4716 vm_object_purge(vm_object_t object
)
4719 unsigned int num_purged_pages
;
4720 vm_page_t local_freeq
;
4721 unsigned long local_freed
;
4722 int purge_loop_quota
;
4723 /* free pages as soon as we gather PURGE_BATCH_FREE_LIMIT pages to free */
4724 #define PURGE_BATCH_FREE_LIMIT 50
4725 /* release page queues lock every PURGE_LOOP_QUOTA iterations */
4726 #define PURGE_LOOP_QUOTA 100
4728 num_purged_pages
= 0;
4729 if (object
->purgable
== VM_OBJECT_NONPURGABLE
)
4730 return num_purged_pages
;
4732 object
->purgable
= VM_OBJECT_PURGABLE_EMPTY
;
4734 assert(object
->copy
== VM_OBJECT_NULL
);
4735 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
4736 purge_loop_quota
= PURGE_LOOP_QUOTA
;
4738 local_freeq
= VM_PAGE_NULL
;
4742 * Go through the object's resident pages and try and discard them.
4744 next
= (vm_page_t
)queue_first(&object
->memq
);
4745 while (!queue_end(&object
->memq
, (queue_entry_t
)next
)) {
4747 next
= (vm_page_t
)queue_next(&next
->listq
);
4749 if (purge_loop_quota
-- == 0) {
4751 * Avoid holding the page queues lock for too long.
4752 * Let someone else take it for a while if needed.
4753 * Keep holding the object's lock to guarantee that
4754 * the object's page list doesn't change under us
4757 if (local_freeq
!= VM_PAGE_NULL
) {
4759 * Flush our queue of pages to free.
4761 vm_page_free_list(local_freeq
);
4762 local_freeq
= VM_PAGE_NULL
;
4765 vm_page_unlock_queues();
4767 vm_page_lock_queues();
4769 /* resume with the current page and a new quota */
4770 purge_loop_quota
= PURGE_LOOP_QUOTA
;
4774 if (p
->busy
|| p
->cleaning
|| p
->laundry
||
4775 p
->list_req_pending
) {
4776 /* page is being acted upon, so don't mess with it */
4779 if (p
->wire_count
) {
4780 /* don't discard a wired page */
4785 /* clean up the object/offset table */
4789 /* update the object's count of absent pages */
4790 vm_object_absent_release(object
);
4793 /* we can discard this page */
4795 /* advertize that this page is in a transition state */
4798 if (p
->no_isync
== TRUE
) {
4799 /* the page hasn't been mapped yet */
4800 /* (optimization to delay the i-cache sync) */
4802 /* unmap the page */
4805 refmod_state
= pmap_disconnect(p
->phys_page
);
4806 if (refmod_state
& VM_MEM_MODIFIED
) {
4811 if (p
->dirty
|| p
->precious
) {
4812 /* we saved the cost of cleaning this page ! */
4814 vm_page_purged_count
++;
4817 /* remove page from active or inactive queue... */
4818 VM_PAGE_QUEUES_REMOVE(p
);
4820 /* ... and put it on our queue of pages to free */
4821 assert(!p
->laundry
);
4822 assert(p
->object
!= kernel_object
);
4823 assert(p
->pageq
.next
== NULL
&&
4824 p
->pageq
.prev
== NULL
);
4825 p
->pageq
.next
= (queue_entry_t
) local_freeq
;
4827 if (++local_freed
>= PURGE_BATCH_FREE_LIMIT
) {
4828 /* flush our queue of pages to free */
4829 vm_page_free_list(local_freeq
);
4830 local_freeq
= VM_PAGE_NULL
;
4835 /* flush our local queue of pages to free one last time */
4836 if (local_freeq
!= VM_PAGE_NULL
) {
4837 vm_page_free_list(local_freeq
);
4838 local_freeq
= VM_PAGE_NULL
;
4842 return num_purged_pages
;
4846 * vm_object_purgable_control() allows the caller to control and investigate the
4847 * state of a purgable object. A purgable object is created via a call to
4848 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgable object will
4849 * never be coalesced with any other object -- even other purgable objects --
4850 * and will thus always remain a distinct object. A purgable object has
4851 * special semantics when its reference count is exactly 1. If its reference
4852 * count is greater than 1, then a purgable object will behave like a normal
4853 * object and attempts to use this interface will result in an error return
4854 * of KERN_INVALID_ARGUMENT.
4856 * A purgable object may be put into a "volatile" state which will make the
4857 * object's pages elligable for being reclaimed without paging to backing
4858 * store if the system runs low on memory. If the pages in a volatile
4859 * purgable object are reclaimed, the purgable object is said to have been
4860 * "emptied." When a purgable object is emptied the system will reclaim as
4861 * many pages from the object as it can in a convenient manner (pages already
4862 * en route to backing store or busy for other reasons are left as is). When
4863 * a purgable object is made volatile, its pages will generally be reclaimed
4864 * before other pages in the application's working set. This semantic is
4865 * generally used by applications which can recreate the data in the object
4866 * faster than it can be paged in. One such example might be media assets
4867 * which can be reread from a much faster RAID volume.
4869 * A purgable object may be designated as "non-volatile" which means it will
4870 * behave like all other objects in the system with pages being written to and
4871 * read from backing store as needed to satisfy system memory needs. If the
4872 * object was emptied before the object was made non-volatile, that fact will
4873 * be returned as the old state of the purgable object (see
4874 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
4875 * were reclaimed as part of emptying the object will be refaulted in as
4876 * zero-fill on demand. It is up to the application to note that an object
4877 * was emptied and recreate the objects contents if necessary. When a
4878 * purgable object is made non-volatile, its pages will generally not be paged
4879 * out to backing store in the immediate future. A purgable object may also
4880 * be manually emptied.
4882 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
4883 * volatile purgable object may be queried at any time. This information may
4884 * be used as a control input to let the application know when the system is
4885 * experiencing memory pressure and is reclaiming memory.
4887 * The specified address may be any address within the purgable object. If
4888 * the specified address does not represent any object in the target task's
4889 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
4890 * object containing the specified address is not a purgable object, then
4891 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
4894 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
4895 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
4896 * state is used to set the new state of the purgable object and return its
4897 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgable
4898 * object is returned in the parameter state.
4900 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
4901 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
4902 * the non-volatile, volatile and volatile/empty states described above.
4903 * Setting the state of a purgable object to VM_PURGABLE_EMPTY will
4904 * immediately reclaim as many pages in the object as can be conveniently
4905 * collected (some may have already been written to backing store or be
4908 * The process of making a purgable object non-volatile and determining its
4909 * previous state is atomic. Thus, if a purgable object is made
4910 * VM_PURGABLE_NONVOLATILE and the old state is returned as
4911 * VM_PURGABLE_VOLATILE, then the purgable object's previous contents are
4912 * completely intact and will remain so until the object is made volatile
4913 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
4914 * was reclaimed while it was in a volatile state and its previous contents
4918 * The object must be locked.
4921 vm_object_purgable_control(
4923 vm_purgable_t control
,
4929 if (object
== VM_OBJECT_NULL
) {
4931 * Object must already be present or it can't be purgable.
4933 return KERN_INVALID_ARGUMENT
;
4937 * Get current state of the purgable object.
4939 switch (object
->purgable
) {
4940 case VM_OBJECT_NONPURGABLE
:
4941 return KERN_INVALID_ARGUMENT
;
4943 case VM_OBJECT_PURGABLE_NONVOLATILE
:
4944 old_state
= VM_PURGABLE_NONVOLATILE
;
4947 case VM_OBJECT_PURGABLE_VOLATILE
:
4948 old_state
= VM_PURGABLE_VOLATILE
;
4951 case VM_OBJECT_PURGABLE_EMPTY
:
4952 old_state
= VM_PURGABLE_EMPTY
;
4956 old_state
= VM_PURGABLE_NONVOLATILE
;
4957 panic("Bad state (%d) for purgable object!\n",
4962 /* purgable cant have delayed copies - now or in the future */
4963 assert(object
->copy
== VM_OBJECT_NULL
);
4964 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
4967 * Execute the desired operation.
4969 if (control
== VM_PURGABLE_GET_STATE
) {
4971 return KERN_SUCCESS
;
4975 case VM_PURGABLE_NONVOLATILE
:
4976 vm_page_lock_queues();
4977 if (object
->purgable
!= VM_OBJECT_PURGABLE_NONVOLATILE
) {
4978 assert(vm_page_purgeable_count
>=
4979 object
->resident_page_count
);
4980 vm_page_purgeable_count
-= object
->resident_page_count
;
4983 object
->purgable
= VM_OBJECT_PURGABLE_NONVOLATILE
;
4986 * If the object wasn't emptied, then mark all pages of the
4987 * object as referenced in order to give them a complete turn
4988 * of the virtual memory "clock" before becoming candidates
4989 * for paging out (if the system is suffering from memory
4990 * pressure). We don't really need to set the pmap reference
4991 * bits (which would be expensive) since the software copies
4992 * are believed if they're set to true ...
4994 if (old_state
!= VM_PURGABLE_EMPTY
) {
4995 for (p
= (vm_page_t
)queue_first(&object
->memq
);
4996 !queue_end(&object
->memq
, (queue_entry_t
)p
);
4997 p
= (vm_page_t
)queue_next(&p
->listq
))
4998 p
->reference
= TRUE
;
5001 vm_page_unlock_queues();
5005 case VM_PURGABLE_VOLATILE
:
5006 vm_page_lock_queues();
5008 if (object
->purgable
!= VM_OBJECT_PURGABLE_VOLATILE
&&
5009 object
->purgable
!= VM_OBJECT_PURGABLE_EMPTY
) {
5010 vm_page_purgeable_count
+= object
->resident_page_count
;
5013 object
->purgable
= VM_OBJECT_PURGABLE_VOLATILE
;
5016 * We want the newly volatile purgable object to be a
5017 * candidate for the pageout scan before other pages in the
5018 * application if the system is suffering from memory
5019 * pressure. To do this, we move a page of the object from
5020 * the active queue onto the inactive queue in order to
5021 * promote the object for early reclaim. We only need to move
5022 * a single page since the pageout scan will reap the entire
5023 * purgable object if it finds a single page in a volatile
5024 * state. Obviously we don't do this if there are no pages
5025 * associated with the object or we find a page of the object
5026 * already on the inactive queue.
5028 for (p
= (vm_page_t
)queue_first(&object
->memq
);
5029 !queue_end(&object
->memq
, (queue_entry_t
)p
);
5030 p
= (vm_page_t
)queue_next(&p
->listq
)) {
5032 /* already a page on the inactive queue */
5035 if (p
->active
&& !p
->busy
) {
5036 /* found one we can move */
5037 vm_page_deactivate(p
);
5041 vm_page_unlock_queues();
5046 case VM_PURGABLE_EMPTY
:
5047 vm_page_lock_queues();
5048 if (object
->purgable
!= VM_OBJECT_PURGABLE_VOLATILE
&&
5049 object
->purgable
!= VM_OBJECT_PURGABLE_EMPTY
) {
5050 vm_page_purgeable_count
+= object
->resident_page_count
;
5052 (void) vm_object_purge(object
);
5053 vm_page_unlock_queues();
5059 return KERN_SUCCESS
;
5064 * vm_object_res_deallocate
5066 * (recursively) decrement residence counts on vm objects and their shadows.
5067 * Called from vm_object_deallocate and when swapping out an object.
5069 * The object is locked, and remains locked throughout the function,
5070 * even as we iterate down the shadow chain. Locks on intermediate objects
5071 * will be dropped, but not the original object.
5073 * NOTE: this function used to use recursion, rather than iteration.
5076 __private_extern__
void
5077 vm_object_res_deallocate(
5080 vm_object_t orig_object
= object
;
5082 * Object is locked so it can be called directly
5083 * from vm_object_deallocate. Original object is never
5086 assert(object
->res_count
> 0);
5087 while (--object
->res_count
== 0) {
5088 assert(object
->ref_count
>= object
->res_count
);
5089 vm_object_deactivate_all_pages(object
);
5090 /* iterate on shadow, if present */
5091 if (object
->shadow
!= VM_OBJECT_NULL
) {
5092 vm_object_t tmp_object
= object
->shadow
;
5093 vm_object_lock(tmp_object
);
5094 if (object
!= orig_object
)
5095 vm_object_unlock(object
);
5096 object
= tmp_object
;
5097 assert(object
->res_count
> 0);
5101 if (object
!= orig_object
)
5102 vm_object_unlock(object
);
5106 * vm_object_res_reference
5108 * Internal function to increment residence count on a vm object
5109 * and its shadows. It is called only from vm_object_reference, and
5110 * when swapping in a vm object, via vm_map_swap.
5112 * The object is locked, and remains locked throughout the function,
5113 * even as we iterate down the shadow chain. Locks on intermediate objects
5114 * will be dropped, but not the original object.
5116 * NOTE: this function used to use recursion, rather than iteration.
5119 __private_extern__
void
5120 vm_object_res_reference(
5123 vm_object_t orig_object
= object
;
5125 * Object is locked, so this can be called directly
5126 * from vm_object_reference. This lock is never released.
5128 while ((++object
->res_count
== 1) &&
5129 (object
->shadow
!= VM_OBJECT_NULL
)) {
5130 vm_object_t tmp_object
= object
->shadow
;
5132 assert(object
->ref_count
>= object
->res_count
);
5133 vm_object_lock(tmp_object
);
5134 if (object
!= orig_object
)
5135 vm_object_unlock(object
);
5136 object
= tmp_object
;
5138 if (object
!= orig_object
)
5139 vm_object_unlock(object
);
5140 assert(orig_object
->ref_count
>= orig_object
->res_count
);
5142 #endif /* TASK_SWAPPER */
5145 * vm_object_reference:
5147 * Gets another reference to the given object.
5149 #ifdef vm_object_reference
5150 #undef vm_object_reference
5152 __private_extern__
void
5153 vm_object_reference(
5154 register vm_object_t object
)
5156 if (object
== VM_OBJECT_NULL
)
5159 vm_object_lock(object
);
5160 assert(object
->ref_count
> 0);
5161 vm_object_reference_locked(object
);
5162 vm_object_unlock(object
);
5167 * Scale the vm_object_cache
5168 * This is required to make sure that the vm_object_cache is big
5169 * enough to effectively cache the mapped file.
5170 * This is really important with UBC as all the regular file vnodes
5171 * have memory object associated with them. Havving this cache too
5172 * small results in rapid reclaim of vnodes and hurts performance a LOT!
5174 * This is also needed as number of vnodes can be dynamically scaled.
5177 adjust_vm_object_cache(
5178 __unused vm_size_t oval
,
5181 vm_object_cached_max
= nval
;
5182 vm_object_cache_trim(FALSE
);
5183 return (KERN_SUCCESS
);
5185 #endif /* MACH_BSD */
5189 * vm_object_transpose
5191 * This routine takes two VM objects of the same size and exchanges
5192 * their backing store.
5193 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
5194 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
5196 * The VM objects must not be locked by caller.
5199 vm_object_transpose(
5200 vm_object_t object1
,
5201 vm_object_t object2
,
5202 vm_object_size_t transpose_size
)
5204 vm_object_t tmp_object
;
5205 kern_return_t retval
;
5206 boolean_t object1_locked
, object2_locked
;
5207 boolean_t object1_paging
, object2_paging
;
5209 vm_object_offset_t page_offset
;
5211 tmp_object
= VM_OBJECT_NULL
;
5212 object1_locked
= FALSE
; object2_locked
= FALSE
;
5213 object1_paging
= FALSE
; object2_paging
= FALSE
;
5215 if (object1
== object2
||
5216 object1
== VM_OBJECT_NULL
||
5217 object2
== VM_OBJECT_NULL
) {
5219 * If the 2 VM objects are the same, there's
5220 * no point in exchanging their backing store.
5222 retval
= KERN_INVALID_VALUE
;
5226 vm_object_lock(object1
);
5227 object1_locked
= TRUE
;
5228 if (object1
->copy
|| object1
->shadow
|| object1
->shadowed
||
5229 object1
->purgable
!= VM_OBJECT_NONPURGABLE
) {
5231 * We don't deal with copy or shadow objects (yet).
5233 retval
= KERN_INVALID_VALUE
;
5237 * Since we're about to mess with the object's backing store,
5238 * mark it as "paging_in_progress". Note that this is not enough
5239 * to prevent any paging activity on this object, so the caller should
5240 * have "quiesced" the objects beforehand, via a UPL operation with
5241 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
5242 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
5244 vm_object_paging_begin(object1
);
5245 object1_paging
= TRUE
;
5246 vm_object_unlock(object1
);
5247 object1_locked
= FALSE
;
5250 * Same as above for the 2nd object...
5252 vm_object_lock(object2
);
5253 object2_locked
= TRUE
;
5254 if (object2
->copy
|| object2
->shadow
|| object2
->shadowed
||
5255 object2
->purgable
!= VM_OBJECT_NONPURGABLE
) {
5256 retval
= KERN_INVALID_VALUE
;
5259 vm_object_paging_begin(object2
);
5260 object2_paging
= TRUE
;
5261 vm_object_unlock(object2
);
5262 object2_locked
= FALSE
;
5265 * Allocate a temporary VM object to hold object1's contents
5266 * while we copy object2 to object1.
5268 tmp_object
= vm_object_allocate(transpose_size
);
5269 vm_object_lock(tmp_object
);
5270 vm_object_paging_begin(tmp_object
);
5271 tmp_object
->can_persist
= FALSE
;
5274 * Since we need to lock both objects at the same time,
5275 * make sure we always lock them in the same order to
5278 if (object1
< object2
) {
5279 vm_object_lock(object1
);
5280 vm_object_lock(object2
);
5282 vm_object_lock(object2
);
5283 vm_object_lock(object1
);
5285 object1_locked
= TRUE
;
5286 object2_locked
= TRUE
;
5288 if (object1
->size
!= object2
->size
||
5289 object1
->size
!= transpose_size
) {
5291 * If the 2 objects don't have the same size, we can't
5292 * exchange their backing stores or one would overflow.
5293 * If their size doesn't match the caller's
5294 * "transpose_size", we can't do it either because the
5295 * transpose operation will affect the entire span of
5298 retval
= KERN_INVALID_VALUE
;
5304 * Transpose the lists of resident pages.
5306 if (object1
->phys_contiguous
|| queue_empty(&object1
->memq
)) {
5308 * No pages in object1, just transfer pages
5309 * from object2 to object1. No need to go through
5310 * an intermediate object.
5312 while (!queue_empty(&object2
->memq
)) {
5313 page
= (vm_page_t
) queue_first(&object2
->memq
);
5314 vm_page_rename(page
, object1
, page
->offset
);
5316 assert(queue_empty(&object2
->memq
));
5317 } else if (object2
->phys_contiguous
|| queue_empty(&object2
->memq
)) {
5319 * No pages in object2, just transfer pages
5320 * from object1 to object2. No need to go through
5321 * an intermediate object.
5323 while (!queue_empty(&object1
->memq
)) {
5324 page
= (vm_page_t
) queue_first(&object1
->memq
);
5325 vm_page_rename(page
, object2
, page
->offset
);
5327 assert(queue_empty(&object1
->memq
));
5329 /* transfer object1's pages to tmp_object */
5330 vm_page_lock_queues();
5331 while (!queue_empty(&object1
->memq
)) {
5332 page
= (vm_page_t
) queue_first(&object1
->memq
);
5333 page_offset
= page
->offset
;
5334 vm_page_remove(page
);
5335 page
->offset
= page_offset
;
5336 queue_enter(&tmp_object
->memq
, page
, vm_page_t
, listq
);
5338 vm_page_unlock_queues();
5339 assert(queue_empty(&object1
->memq
));
5340 /* transfer object2's pages to object1 */
5341 while (!queue_empty(&object2
->memq
)) {
5342 page
= (vm_page_t
) queue_first(&object2
->memq
);
5343 vm_page_rename(page
, object1
, page
->offset
);
5345 assert(queue_empty(&object2
->memq
));
5346 /* transfer tmp_object's pages to object1 */
5347 while (!queue_empty(&tmp_object
->memq
)) {
5348 page
= (vm_page_t
) queue_first(&tmp_object
->memq
);
5349 queue_remove(&tmp_object
->memq
, page
,
5351 vm_page_insert(page
, object2
, page
->offset
);
5353 assert(queue_empty(&tmp_object
->memq
));
5356 /* no need to transpose the size: they should be identical */
5357 assert(object1
->size
== object2
->size
);
5359 #define __TRANSPOSE_FIELD(field) \
5361 tmp_object->field = object1->field; \
5362 object1->field = object2->field; \
5363 object2->field = tmp_object->field; \
5366 assert(!object1
->copy
);
5367 assert(!object2
->copy
);
5369 assert(!object1
->shadow
);
5370 assert(!object2
->shadow
);
5372 __TRANSPOSE_FIELD(shadow_offset
); /* used by phys_contiguous objects */
5373 __TRANSPOSE_FIELD(pager
);
5374 __TRANSPOSE_FIELD(paging_offset
);
5376 __TRANSPOSE_FIELD(pager_control
);
5377 /* update the memory_objects' pointers back to the VM objects */
5378 if (object1
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
5379 memory_object_control_collapse(object1
->pager_control
,
5382 if (object2
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
5383 memory_object_control_collapse(object2
->pager_control
,
5387 __TRANSPOSE_FIELD(absent_count
);
5389 assert(object1
->paging_in_progress
);
5390 assert(object2
->paging_in_progress
);
5392 __TRANSPOSE_FIELD(pager_created
);
5393 __TRANSPOSE_FIELD(pager_initialized
);
5394 __TRANSPOSE_FIELD(pager_ready
);
5395 __TRANSPOSE_FIELD(pager_trusted
);
5396 __TRANSPOSE_FIELD(internal
);
5397 __TRANSPOSE_FIELD(temporary
);
5398 __TRANSPOSE_FIELD(private);
5399 __TRANSPOSE_FIELD(pageout
);
5400 __TRANSPOSE_FIELD(true_share
);
5401 __TRANSPOSE_FIELD(phys_contiguous
);
5402 __TRANSPOSE_FIELD(nophyscache
);
5403 __TRANSPOSE_FIELD(last_alloc
);
5404 __TRANSPOSE_FIELD(sequential
);
5405 __TRANSPOSE_FIELD(cluster_size
);
5406 __TRANSPOSE_FIELD(existence_map
);
5407 __TRANSPOSE_FIELD(cow_hint
);
5408 __TRANSPOSE_FIELD(wimg_bits
);
5410 #undef __TRANSPOSE_FIELD
5412 retval
= KERN_SUCCESS
;
5418 if (tmp_object
!= VM_OBJECT_NULL
) {
5419 vm_object_paging_end(tmp_object
);
5420 vm_object_unlock(tmp_object
);
5422 * Re-initialize the temporary object to avoid
5423 * deallocating a real pager.
5425 _vm_object_allocate(transpose_size
, tmp_object
);
5426 vm_object_deallocate(tmp_object
);
5427 tmp_object
= VM_OBJECT_NULL
;
5430 if (object1_locked
) {
5431 vm_object_unlock(object1
);
5432 object1_locked
= FALSE
;
5434 if (object2_locked
) {
5435 vm_object_unlock(object2
);
5436 object2_locked
= FALSE
;
5438 if (object1_paging
) {
5439 vm_object_lock(object1
);
5440 vm_object_paging_end(object1
);
5441 vm_object_unlock(object1
);
5442 object1_paging
= FALSE
;
5444 if (object2_paging
) {
5445 vm_object_lock(object2
);
5446 vm_object_paging_end(object2
);
5447 vm_object_unlock(object2
);
5448 object2_paging
= FALSE
;