2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: vm/vm_object.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * Virtual memory object module.
65 #include <mach_pagemap.h>
66 #include <task_swapper.h>
68 #include <mach/mach_types.h>
69 #include <mach/memory_object.h>
70 #include <mach/memory_object_default.h>
71 #include <mach/memory_object_control_server.h>
72 #include <mach/vm_param.h>
74 #include <ipc/ipc_types.h>
75 #include <ipc/ipc_port.h>
77 #include <kern/kern_types.h>
78 #include <kern/assert.h>
79 #include <kern/lock.h>
80 #include <kern/queue.h>
82 #include <kern/zalloc.h>
83 #include <kern/host.h>
84 #include <kern/host_statistics.h>
85 #include <kern/processor.h>
86 #include <kern/misc_protos.h>
88 #include <vm/memory_object.h>
89 #include <vm/vm_fault.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_pageout.h>
94 #include <vm/vm_protos.h>
97 * Virtual memory objects maintain the actual data
98 * associated with allocated virtual memory. A given
99 * page of memory exists within exactly one object.
101 * An object is only deallocated when all "references"
104 * Associated with each object is a list of all resident
105 * memory pages belonging to that object; this list is
106 * maintained by the "vm_page" module, but locked by the object's
109 * Each object also records the memory object reference
110 * that is used by the kernel to request and write
111 * back data (the memory object, field "pager"), etc...
113 * Virtual memory objects are allocated to provide
114 * zero-filled memory (vm_allocate) or map a user-defined
115 * memory object into a virtual address space (vm_map).
117 * Virtual memory objects that refer to a user-defined
118 * memory object are called "permanent", because all changes
119 * made in virtual memory are reflected back to the
120 * memory manager, which may then store it permanently.
121 * Other virtual memory objects are called "temporary",
122 * meaning that changes need be written back only when
123 * necessary to reclaim pages, and that storage associated
124 * with the object can be discarded once it is no longer
127 * A permanent memory object may be mapped into more
128 * than one virtual address space. Moreover, two threads
129 * may attempt to make the first mapping of a memory
130 * object concurrently. Only one thread is allowed to
131 * complete this mapping; all others wait for the
132 * "pager_initialized" field is asserted, indicating
133 * that the first thread has initialized all of the
134 * necessary fields in the virtual memory object structure.
136 * The kernel relies on a *default memory manager* to
137 * provide backing storage for the zero-filled virtual
138 * memory objects. The pager memory objects associated
139 * with these temporary virtual memory objects are only
140 * requested from the default memory manager when it
141 * becomes necessary. Virtual memory objects
142 * that depend on the default memory manager are called
143 * "internal". The "pager_created" field is provided to
144 * indicate whether these ports have ever been allocated.
146 * The kernel may also create virtual memory objects to
147 * hold changed pages after a copy-on-write operation.
148 * In this case, the virtual memory object (and its
149 * backing storage -- its memory object) only contain
150 * those pages that have been changed. The "shadow"
151 * field refers to the virtual memory object that contains
152 * the remainder of the contents. The "shadow_offset"
153 * field indicates where in the "shadow" these contents begin.
154 * The "copy" field refers to a virtual memory object
155 * to which changed pages must be copied before changing
156 * this object, in order to implement another form
157 * of copy-on-write optimization.
159 * The virtual memory object structure also records
160 * the attributes associated with its memory object.
161 * The "pager_ready", "can_persist" and "copy_strategy"
162 * fields represent those attributes. The "cached_list"
163 * field is used in the implementation of the persistence
166 * ZZZ Continue this comment.
169 /* Forward declarations for internal functions. */
170 static kern_return_t
vm_object_terminate(
173 extern void vm_object_remove(
176 static vm_object_t
vm_object_cache_trim(
177 boolean_t called_from_vm_object_deallocate
);
179 static void vm_object_deactivate_all_pages(
182 static kern_return_t
vm_object_copy_call(
183 vm_object_t src_object
,
184 vm_object_offset_t src_offset
,
185 vm_object_size_t size
,
186 vm_object_t
*_result_object
);
188 static void vm_object_do_collapse(
190 vm_object_t backing_object
);
192 static void vm_object_do_bypass(
194 vm_object_t backing_object
);
196 static void vm_object_release_pager(
197 memory_object_t pager
);
199 static zone_t vm_object_zone
; /* vm backing store zone */
202 * All wired-down kernel memory belongs to a single virtual
203 * memory object (kernel_object) to avoid wasting data structures.
205 static struct vm_object kernel_object_store
;
206 __private_extern__ vm_object_t kernel_object
= &kernel_object_store
;
209 * The submap object is used as a placeholder for vm_map_submap
210 * operations. The object is declared in vm_map.c because it
211 * is exported by the vm_map module. The storage is declared
212 * here because it must be initialized here.
214 static struct vm_object vm_submap_object_store
;
217 * Virtual memory objects are initialized from
218 * a template (see vm_object_allocate).
220 * When adding a new field to the virtual memory
221 * object structure, be sure to add initialization
222 * (see _vm_object_allocate()).
224 static struct vm_object vm_object_template
;
227 * Virtual memory objects that are not referenced by
228 * any address maps, but that are allowed to persist
229 * (an attribute specified by the associated memory manager),
230 * are kept in a queue (vm_object_cached_list).
232 * When an object from this queue is referenced again,
233 * for example to make another address space mapping,
234 * it must be removed from the queue. That is, the
235 * queue contains *only* objects with zero references.
237 * The kernel may choose to terminate objects from this
238 * queue in order to reclaim storage. The current policy
239 * is to permit a fixed maximum number of unreferenced
240 * objects (vm_object_cached_max).
242 * A spin lock (accessed by routines
243 * vm_object_cache_{lock,lock_try,unlock}) governs the
244 * object cache. It must be held when objects are
245 * added to or removed from the cache (in vm_object_terminate).
246 * The routines that acquire a reference to a virtual
247 * memory object based on one of the memory object ports
248 * must also lock the cache.
250 * Ideally, the object cache should be more isolated
251 * from the reference mechanism, so that the lock need
252 * not be held to make simple references.
254 static queue_head_t vm_object_cached_list
;
255 static int vm_object_cached_count
=0;
256 static int vm_object_cached_high
; /* highest # cached objects */
257 static int vm_object_cached_max
= 512; /* may be patched*/
259 static decl_mutex_data(,vm_object_cached_lock_data
)
261 #define vm_object_cache_lock() \
262 mutex_lock(&vm_object_cached_lock_data)
263 #define vm_object_cache_lock_try() \
264 mutex_try(&vm_object_cached_lock_data)
265 #define vm_object_cache_unlock() \
266 mutex_unlock(&vm_object_cached_lock_data)
268 #define VM_OBJECT_HASH_COUNT 1024
269 static queue_head_t vm_object_hashtable
[VM_OBJECT_HASH_COUNT
];
270 static struct zone
*vm_object_hash_zone
;
272 struct vm_object_hash_entry
{
273 queue_chain_t hash_link
; /* hash chain link */
274 memory_object_t pager
; /* pager we represent */
275 vm_object_t object
; /* corresponding object */
276 boolean_t waiting
; /* someone waiting for
280 typedef struct vm_object_hash_entry
*vm_object_hash_entry_t
;
281 #define VM_OBJECT_HASH_ENTRY_NULL ((vm_object_hash_entry_t) 0)
283 #define VM_OBJECT_HASH_SHIFT 8
284 #define vm_object_hash(pager) \
285 ((((unsigned)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_COUNT)
287 void vm_object_hash_entry_free(
288 vm_object_hash_entry_t entry
);
290 static void vm_object_reap(vm_object_t object
);
291 static void vm_object_reap_async(vm_object_t object
);
292 static void vm_object_reaper_thread(void);
293 static queue_head_t vm_object_reaper_queue
; /* protected by vm_object_cache_lock() */
294 unsigned int vm_object_reap_count
= 0;
295 unsigned int vm_object_reap_count_async
= 0;
298 * vm_object_hash_lookup looks up a pager in the hashtable
299 * and returns the corresponding entry, with optional removal.
302 static vm_object_hash_entry_t
303 vm_object_hash_lookup(
304 memory_object_t pager
,
305 boolean_t remove_entry
)
307 register queue_t bucket
;
308 register vm_object_hash_entry_t entry
;
310 bucket
= &vm_object_hashtable
[vm_object_hash(pager
)];
312 entry
= (vm_object_hash_entry_t
)queue_first(bucket
);
313 while (!queue_end(bucket
, (queue_entry_t
)entry
)) {
314 if (entry
->pager
== pager
&& !remove_entry
)
316 else if (entry
->pager
== pager
) {
317 queue_remove(bucket
, entry
,
318 vm_object_hash_entry_t
, hash_link
);
322 entry
= (vm_object_hash_entry_t
)queue_next(&entry
->hash_link
);
325 return(VM_OBJECT_HASH_ENTRY_NULL
);
329 * vm_object_hash_enter enters the specified
330 * pager / cache object association in the hashtable.
334 vm_object_hash_insert(
335 vm_object_hash_entry_t entry
)
337 register queue_t bucket
;
339 bucket
= &vm_object_hashtable
[vm_object_hash(entry
->pager
)];
341 queue_enter(bucket
, entry
, vm_object_hash_entry_t
, hash_link
);
344 static vm_object_hash_entry_t
345 vm_object_hash_entry_alloc(
346 memory_object_t pager
)
348 vm_object_hash_entry_t entry
;
350 entry
= (vm_object_hash_entry_t
)zalloc(vm_object_hash_zone
);
351 entry
->pager
= pager
;
352 entry
->object
= VM_OBJECT_NULL
;
353 entry
->waiting
= FALSE
;
359 vm_object_hash_entry_free(
360 vm_object_hash_entry_t entry
)
362 zfree(vm_object_hash_zone
, entry
);
366 * vm_object_allocate:
368 * Returns a new object with the given size.
371 __private_extern__
void
373 vm_object_size_t size
,
377 "vm_object_allocate, object 0x%X size 0x%X\n",
378 (integer_t
)object
, size
, 0,0,0);
380 *object
= vm_object_template
;
381 queue_init(&object
->memq
);
382 queue_init(&object
->msr_q
);
384 queue_init(&object
->uplq
);
385 #endif /* UPL_DEBUG */
386 vm_object_lock_init(object
);
390 __private_extern__ vm_object_t
392 vm_object_size_t size
)
394 register vm_object_t object
;
396 object
= (vm_object_t
) zalloc(vm_object_zone
);
398 // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
400 if (object
!= VM_OBJECT_NULL
)
401 _vm_object_allocate(size
, object
);
407 * vm_object_bootstrap:
409 * Initialize the VM objects module.
411 __private_extern__
void
412 vm_object_bootstrap(void)
416 vm_object_zone
= zinit((vm_size_t
) sizeof(struct vm_object
),
417 round_page_32(512*1024),
418 round_page_32(12*1024),
421 queue_init(&vm_object_cached_list
);
422 mutex_init(&vm_object_cached_lock_data
, 0);
424 vm_object_hash_zone
=
425 zinit((vm_size_t
) sizeof (struct vm_object_hash_entry
),
426 round_page_32(512*1024),
427 round_page_32(12*1024),
428 "vm object hash entries");
430 for (i
= 0; i
< VM_OBJECT_HASH_COUNT
; i
++)
431 queue_init(&vm_object_hashtable
[i
]);
434 * Fill in a template object, for quick initialization
437 /* memq; Lock; init after allocation */
438 vm_object_template
.size
= 0;
439 vm_object_template
.memq_hint
= VM_PAGE_NULL
;
440 vm_object_template
.ref_count
= 1;
442 vm_object_template
.res_count
= 1;
443 #endif /* TASK_SWAPPER */
444 vm_object_template
.resident_page_count
= 0;
445 vm_object_template
.copy
= VM_OBJECT_NULL
;
446 vm_object_template
.shadow
= VM_OBJECT_NULL
;
447 vm_object_template
.shadow_offset
= (vm_object_offset_t
) 0;
448 vm_object_template
.cow_hint
= ~(vm_offset_t
)0;
449 vm_object_template
.true_share
= FALSE
;
451 vm_object_template
.pager
= MEMORY_OBJECT_NULL
;
452 vm_object_template
.paging_offset
= 0;
453 vm_object_template
.pager_control
= MEMORY_OBJECT_CONTROL_NULL
;
454 /* msr_q; init after allocation */
456 vm_object_template
.copy_strategy
= MEMORY_OBJECT_COPY_SYMMETRIC
;
457 vm_object_template
.absent_count
= 0;
458 vm_object_template
.paging_in_progress
= 0;
460 /* Begin bitfields */
461 vm_object_template
.all_wanted
= 0; /* all bits FALSE */
462 vm_object_template
.pager_created
= FALSE
;
463 vm_object_template
.pager_initialized
= FALSE
;
464 vm_object_template
.pager_ready
= FALSE
;
465 vm_object_template
.pager_trusted
= FALSE
;
466 vm_object_template
.can_persist
= FALSE
;
467 vm_object_template
.internal
= TRUE
;
468 vm_object_template
.temporary
= TRUE
;
469 vm_object_template
.private = FALSE
;
470 vm_object_template
.pageout
= FALSE
;
471 vm_object_template
.alive
= TRUE
;
472 vm_object_template
.purgable
= VM_OBJECT_NONPURGABLE
;
473 vm_object_template
.silent_overwrite
= FALSE
;
474 vm_object_template
.advisory_pageout
= FALSE
;
475 vm_object_template
.shadowed
= FALSE
;
476 vm_object_template
.terminating
= FALSE
;
477 vm_object_template
.shadow_severed
= FALSE
;
478 vm_object_template
.phys_contiguous
= FALSE
;
479 vm_object_template
.nophyscache
= FALSE
;
482 /* cache bitfields */
483 vm_object_template
.wimg_bits
= VM_WIMG_DEFAULT
;
485 /* cached_list; init after allocation */
486 vm_object_template
.last_alloc
= (vm_object_offset_t
) 0;
487 vm_object_template
.cluster_size
= 0;
489 vm_object_template
.existence_map
= VM_EXTERNAL_NULL
;
490 #endif /* MACH_PAGEMAP */
492 vm_object_template
.paging_object
= VM_OBJECT_NULL
;
493 #endif /* MACH_ASSERT */
496 * Initialize the "kernel object"
499 kernel_object
= &kernel_object_store
;
502 * Note that in the following size specifications, we need to add 1 because
503 * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size.
507 _vm_object_allocate((vm_last_addr
- VM_MIN_KERNEL_ADDRESS
) + 1,
510 _vm_object_allocate((VM_MAX_KERNEL_ADDRESS
- VM_MIN_KERNEL_ADDRESS
) + 1,
513 kernel_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
516 * Initialize the "submap object". Make it as large as the
517 * kernel object so that no limit is imposed on submap sizes.
520 vm_submap_object
= &vm_submap_object_store
;
522 _vm_object_allocate((vm_last_addr
- VM_MIN_KERNEL_ADDRESS
) + 1,
525 _vm_object_allocate((VM_MAX_KERNEL_ADDRESS
- VM_MIN_KERNEL_ADDRESS
) + 1,
528 vm_submap_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
531 * Create an "extra" reference to this object so that we never
532 * try to deallocate it; zfree doesn't like to be called with
535 vm_object_reference(vm_submap_object
);
538 vm_external_module_initialize();
539 #endif /* MACH_PAGEMAP */
543 vm_object_reaper_init(void)
548 queue_init(&vm_object_reaper_queue
);
549 kr
= kernel_thread_start_priority(
550 (thread_continue_t
) vm_object_reaper_thread
,
554 if (kr
!= KERN_SUCCESS
) {
555 panic("failed to launch vm_object_reaper_thread kr=0x%x", kr
);
557 thread_deallocate(thread
);
560 __private_extern__
void
564 * Finish initializing the kernel object.
568 /* remove the typedef below when emergency work-around is taken out */
569 typedef struct vnode_pager
{
570 memory_object_t pager
;
571 memory_object_t pager_handle
; /* pager */
572 memory_object_control_t control_handle
; /* memory object's control handle */
573 void *vnode_handle
; /* vnode handle */
576 #define MIGHT_NOT_CACHE_SHADOWS 1
577 #if MIGHT_NOT_CACHE_SHADOWS
578 static int cache_shadows
= TRUE
;
579 #endif /* MIGHT_NOT_CACHE_SHADOWS */
582 * vm_object_deallocate:
584 * Release a reference to the specified object,
585 * gained either through a vm_object_allocate
586 * or a vm_object_reference call. When all references
587 * are gone, storage associated with this object
588 * may be relinquished.
590 * No object may be locked.
592 __private_extern__
void
593 vm_object_deallocate(
594 register vm_object_t object
)
596 boolean_t retry_cache_trim
= FALSE
;
597 vm_object_t shadow
= VM_OBJECT_NULL
;
599 // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
600 // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
603 while (object
!= VM_OBJECT_NULL
) {
606 * The cache holds a reference (uncounted) to
607 * the object; we must lock it before removing
611 vm_object_cache_lock();
614 * if we try to take a regular lock here
615 * we risk deadlocking against someone
616 * holding a lock on this object while
617 * trying to vm_object_deallocate a different
620 if (vm_object_lock_try(object
))
622 vm_object_cache_unlock();
623 mutex_pause(); /* wait a bit */
625 assert(object
->ref_count
> 0);
628 * If the object has a named reference, and only
629 * that reference would remain, inform the pager
630 * about the last "mapping" reference going away.
632 if ((object
->ref_count
== 2) && (object
->named
)) {
633 memory_object_t pager
= object
->pager
;
635 /* Notify the Pager that there are no */
636 /* more mappers for this object */
638 if (pager
!= MEMORY_OBJECT_NULL
) {
639 vm_object_unlock(object
);
640 vm_object_cache_unlock();
642 memory_object_unmap(pager
);
645 vm_object_cache_lock();
648 * if we try to take a regular lock here
649 * we risk deadlocking against someone
650 * holding a lock on this object while
651 * trying to vm_object_deallocate a different
654 if (vm_object_lock_try(object
))
656 vm_object_cache_unlock();
657 mutex_pause(); /* wait a bit */
659 assert(object
->ref_count
> 0);
664 * Lose the reference. If other references
665 * remain, then we are done, unless we need
666 * to retry a cache trim.
667 * If it is the last reference, then keep it
668 * until any pending initialization is completed.
671 /* if the object is terminating, it cannot go into */
672 /* the cache and we obviously should not call */
673 /* terminate again. */
675 if ((object
->ref_count
> 1) || object
->terminating
) {
677 vm_object_res_deallocate(object
);
678 vm_object_cache_unlock();
680 if (object
->ref_count
== 1 &&
681 object
->shadow
!= VM_OBJECT_NULL
) {
683 * We don't use this VM object anymore. We
684 * would like to collapse it into its parent(s),
685 * but we don't have any pointers back to these
687 * But we can try and collapse this object with
688 * its own shadows, in case these are useless
691 vm_object_collapse(object
, 0);
694 vm_object_unlock(object
);
695 if (retry_cache_trim
&&
696 ((object
= vm_object_cache_trim(TRUE
)) !=
704 * We have to wait for initialization
705 * before destroying or caching the object.
708 if (object
->pager_created
&& ! object
->pager_initialized
) {
709 assert(! object
->can_persist
);
710 vm_object_assert_wait(object
,
711 VM_OBJECT_EVENT_INITIALIZED
,
713 vm_object_unlock(object
);
714 vm_object_cache_unlock();
715 thread_block(THREAD_CONTINUE_NULL
);
720 * If this object can persist, then enter it in
721 * the cache. Otherwise, terminate it.
723 * NOTE: Only permanent objects are cached, and
724 * permanent objects cannot have shadows. This
725 * affects the residence counting logic in a minor
726 * way (can do it in-line, mostly).
729 if ((object
->can_persist
) && (object
->alive
)) {
731 * Now it is safe to decrement reference count,
732 * and to return if reference count is > 0.
734 if (--object
->ref_count
> 0) {
735 vm_object_res_deallocate(object
);
736 vm_object_unlock(object
);
737 vm_object_cache_unlock();
738 if (retry_cache_trim
&&
739 ((object
= vm_object_cache_trim(TRUE
)) !=
746 #if MIGHT_NOT_CACHE_SHADOWS
748 * Remove shadow now if we don't
749 * want to cache shadows.
751 if (! cache_shadows
) {
752 shadow
= object
->shadow
;
753 object
->shadow
= VM_OBJECT_NULL
;
755 #endif /* MIGHT_NOT_CACHE_SHADOWS */
758 * Enter the object onto the queue of
759 * cached objects, and deactivate
762 assert(object
->shadow
== VM_OBJECT_NULL
);
763 VM_OBJ_RES_DECR(object
);
765 "vm_o_deallocate: adding %x to cache, queue = (%x, %x)\n",
767 (integer_t
)vm_object_cached_list
.next
,
768 (integer_t
)vm_object_cached_list
.prev
,0,0);
770 vm_object_cached_count
++;
771 if (vm_object_cached_count
> vm_object_cached_high
)
772 vm_object_cached_high
= vm_object_cached_count
;
773 queue_enter(&vm_object_cached_list
, object
,
774 vm_object_t
, cached_list
);
775 vm_object_cache_unlock();
776 vm_object_deactivate_all_pages(object
);
777 vm_object_unlock(object
);
779 #if MIGHT_NOT_CACHE_SHADOWS
781 * If we have a shadow that we need
782 * to deallocate, do so now, remembering
783 * to trim the cache later.
785 if (! cache_shadows
&& shadow
!= VM_OBJECT_NULL
) {
787 retry_cache_trim
= TRUE
;
790 #endif /* MIGHT_NOT_CACHE_SHADOWS */
793 * Trim the cache. If the cache trim
794 * returns with a shadow for us to deallocate,
795 * then remember to retry the cache trim
796 * when we are done deallocating the shadow.
797 * Otherwise, we are done.
800 object
= vm_object_cache_trim(TRUE
);
801 if (object
== VM_OBJECT_NULL
) {
804 retry_cache_trim
= TRUE
;
808 * This object is not cachable; terminate it.
811 "vm_o_deallocate: !cacheable 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
812 (integer_t
)object
, object
->resident_page_count
,
813 object
->paging_in_progress
,
814 (void *)current_thread(),object
->ref_count
);
816 VM_OBJ_RES_DECR(object
); /* XXX ? */
818 * Terminate this object. If it had a shadow,
819 * then deallocate it; otherwise, if we need
820 * to retry a cache trim, do so now; otherwise,
821 * we are done. "pageout" objects have a shadow,
822 * but maintain a "paging reference" rather than
823 * a normal reference.
825 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
826 if(vm_object_terminate(object
) != KERN_SUCCESS
) {
829 if (shadow
!= VM_OBJECT_NULL
) {
833 if (retry_cache_trim
&&
834 ((object
= vm_object_cache_trim(TRUE
)) !=
841 assert(! retry_cache_trim
);
845 * Check to see whether we really need to trim
846 * down the cache. If so, remove an object from
847 * the cache, terminate it, and repeat.
849 * Called with, and returns with, cache lock unlocked.
852 vm_object_cache_trim(
853 boolean_t called_from_vm_object_deallocate
)
855 register vm_object_t object
= VM_OBJECT_NULL
;
861 * If we no longer need to trim the cache,
865 vm_object_cache_lock();
866 if (vm_object_cached_count
<= vm_object_cached_max
) {
867 vm_object_cache_unlock();
868 return VM_OBJECT_NULL
;
872 * We must trim down the cache, so remove
873 * the first object in the cache.
876 "vm_object_cache_trim: removing from front of cache (%x, %x)\n",
877 (integer_t
)vm_object_cached_list
.next
,
878 (integer_t
)vm_object_cached_list
.prev
, 0, 0, 0);
880 object
= (vm_object_t
) queue_first(&vm_object_cached_list
);
881 if(object
== (vm_object_t
) &vm_object_cached_list
) {
882 /* something's wrong with the calling parameter or */
883 /* the value of vm_object_cached_count, just fix */
885 if(vm_object_cached_max
< 0)
886 vm_object_cached_max
= 0;
887 vm_object_cached_count
= 0;
888 vm_object_cache_unlock();
889 return VM_OBJECT_NULL
;
891 vm_object_lock(object
);
892 queue_remove(&vm_object_cached_list
, object
, vm_object_t
,
894 vm_object_cached_count
--;
897 * Since this object is in the cache, we know
898 * that it is initialized and has no references.
899 * Take a reference to avoid recursive deallocations.
902 assert(object
->pager_initialized
);
903 assert(object
->ref_count
== 0);
907 * Terminate the object.
908 * If the object had a shadow, we let vm_object_deallocate
909 * deallocate it. "pageout" objects have a shadow, but
910 * maintain a "paging reference" rather than a normal
912 * (We are careful here to limit recursion.)
914 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
915 if(vm_object_terminate(object
) != KERN_SUCCESS
)
917 if (shadow
!= VM_OBJECT_NULL
) {
918 if (called_from_vm_object_deallocate
) {
921 vm_object_deallocate(shadow
);
927 boolean_t vm_object_terminate_remove_all
= FALSE
;
930 * Routine: vm_object_terminate
932 * Free all resources associated with a vm_object.
934 * Upon entry, the object must be locked,
935 * and the object must have exactly one reference.
937 * The shadow object reference is left alone.
939 * The object must be unlocked if its found that pages
940 * must be flushed to a backing object. If someone
941 * manages to map the object while it is being flushed
942 * the object is returned unlocked and unchanged. Otherwise,
943 * upon exit, the cache will be unlocked, and the
944 * object will cease to exist.
948 register vm_object_t object
)
950 register vm_page_t p
;
951 vm_object_t shadow_object
;
953 XPR(XPR_VM_OBJECT
, "vm_object_terminate, object 0x%X ref %d\n",
954 (integer_t
)object
, object
->ref_count
, 0, 0, 0);
956 if (!object
->pageout
&& (!object
->temporary
|| object
->can_persist
)
957 && (object
->pager
!= NULL
|| object
->shadow_severed
)) {
958 vm_object_cache_unlock();
959 while (!queue_empty(&object
->memq
)) {
961 * Clear pager_trusted bit so that the pages get yanked
962 * out of the object instead of cleaned in place. This
963 * prevents a deadlock in XMM and makes more sense anyway.
965 object
->pager_trusted
= FALSE
;
967 p
= (vm_page_t
) queue_first(&object
->memq
);
971 if (p
->busy
|| p
->cleaning
) {
972 if(p
->cleaning
|| p
->absent
) {
973 vm_object_paging_wait(object
, THREAD_UNINT
);
976 panic("vm_object_terminate.3 0x%x 0x%x", object
, p
);
980 vm_page_lock_queues();
982 VM_PAGE_QUEUES_REMOVE(p
);
983 vm_page_unlock_queues();
985 if (p
->absent
|| p
->private) {
988 * For private pages, VM_PAGE_FREE just
989 * leaves the page structure around for
990 * its owner to clean up. For absent
991 * pages, the structure is returned to
992 * the appropriate pool.
999 panic("vm_object_terminate.4 0x%x 0x%x", object
, p
);
1002 p
->dirty
= pmap_is_modified(p
->phys_page
);
1004 if ((p
->dirty
|| p
->precious
) && !p
->error
&& object
->alive
) {
1005 vm_pageout_cluster(p
); /* flush page */
1006 vm_object_paging_wait(object
, THREAD_UNINT
);
1008 "vm_object_terminate restart, object 0x%X ref %d\n",
1009 (integer_t
)object
, object
->ref_count
, 0, 0, 0);
1015 vm_object_unlock(object
);
1016 vm_object_cache_lock();
1017 vm_object_lock(object
);
1021 * Make sure the object isn't already being terminated
1023 if(object
->terminating
) {
1024 object
->ref_count
-= 1;
1025 assert(object
->ref_count
> 0);
1026 vm_object_cache_unlock();
1027 vm_object_unlock(object
);
1028 return KERN_FAILURE
;
1032 * Did somebody get a reference to the object while we were
1035 if(object
->ref_count
!= 1) {
1036 object
->ref_count
-= 1;
1037 assert(object
->ref_count
> 0);
1038 vm_object_res_deallocate(object
);
1039 vm_object_cache_unlock();
1040 vm_object_unlock(object
);
1041 return KERN_FAILURE
;
1045 * Make sure no one can look us up now.
1048 object
->terminating
= TRUE
;
1049 object
->alive
= FALSE
;
1050 vm_object_remove(object
);
1053 * Detach the object from its shadow if we are the shadow's
1054 * copy. The reference we hold on the shadow must be dropped
1057 if (((shadow_object
= object
->shadow
) != VM_OBJECT_NULL
) &&
1058 !(object
->pageout
)) {
1059 vm_object_lock(shadow_object
);
1060 if (shadow_object
->copy
== object
)
1061 shadow_object
->copy
= VM_OBJECT_NULL
;
1062 vm_object_unlock(shadow_object
);
1065 if (FALSE
&& object
->paging_in_progress
!= 0) {
1067 * There are still some paging_in_progress references
1068 * on this object, meaning that there are some paging
1069 * or other I/O operations in progress for this VM object.
1070 * Such operations take some paging_in_progress references
1071 * up front to ensure that the object doesn't go away, but
1072 * they may also need to acquire a reference on the VM object,
1073 * to map it in kernel space, for example. That means that
1074 * they may end up releasing the last reference on the VM
1075 * object, triggering its termination, while still holding
1076 * paging_in_progress references. Waiting for these
1077 * pending paging_in_progress references to go away here would
1080 * To avoid deadlocking, we'll let the vm_object_reaper_thread
1081 * complete the VM object termination if it still holds
1082 * paging_in_progress references at this point.
1084 * No new paging_in_progress should appear now that the
1085 * VM object is "terminating" and not "alive".
1087 vm_object_reap_async(object
);
1088 vm_object_cache_unlock();
1089 vm_object_unlock(object
);
1090 return KERN_SUCCESS
;
1092 /* complete the VM object termination */
1093 vm_object_reap(object
);
1094 object
= VM_OBJECT_NULL
;
1095 /* cache lock and object lock were released by vm_object_reap() */
1097 return KERN_SUCCESS
;
1103 * Complete the termination of a VM object after it's been marked
1104 * as "terminating" and "!alive" by vm_object_terminate().
1106 * The VM object cache and the VM object must be locked by caller.
1107 * The locks will be released on return and the VM object is no longer valid.
1113 memory_object_t pager
;
1117 mutex_assert(&vm_object_cached_lock_data
, MA_OWNED
);
1118 mutex_assert(&object
->Lock
, MA_OWNED
);
1121 vm_object_reap_count
++;
1124 * The pageout daemon might be playing with our pages.
1125 * Now that the object is dead, it won't touch any more
1126 * pages, but some pages might already be on their way out.
1127 * Hence, we wait until the active paging activities have
1128 * ceased before we break the association with the pager
1131 while (object
->paging_in_progress
!= 0) {
1132 vm_object_cache_unlock();
1133 vm_object_wait(object
,
1134 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
1136 vm_object_cache_lock();
1137 vm_object_lock(object
);
1140 assert(object
->paging_in_progress
== 0);
1141 pager
= object
->pager
;
1142 object
->pager
= MEMORY_OBJECT_NULL
;
1144 if (pager
!= MEMORY_OBJECT_NULL
)
1145 memory_object_control_disable(object
->pager_control
);
1146 vm_object_cache_unlock();
1148 object
->ref_count
--;
1150 assert(object
->res_count
== 0);
1151 #endif /* TASK_SWAPPER */
1153 assert (object
->ref_count
== 0);
1156 * Clean or free the pages, as appropriate.
1157 * It is possible for us to find busy/absent pages,
1158 * if some faults on this object were aborted.
1160 if (object
->pageout
) {
1161 assert(object
->shadow
!= VM_OBJECT_NULL
);
1163 vm_pageout_object_terminate(object
);
1165 } else if ((object
->temporary
&& !object
->can_persist
) ||
1166 (pager
== MEMORY_OBJECT_NULL
)) {
1167 while (!queue_empty(&object
->memq
)) {
1168 p
= (vm_page_t
) queue_first(&object
->memq
);
1173 } else if (!queue_empty(&object
->memq
)) {
1174 panic("vm_object_reap: queue just emptied isn't");
1177 assert(object
->paging_in_progress
== 0);
1178 assert(object
->ref_count
== 0);
1181 * If the pager has not already been released by
1182 * vm_object_destroy, we need to terminate it and
1183 * release our reference to it here.
1185 if (pager
!= MEMORY_OBJECT_NULL
) {
1186 vm_object_unlock(object
);
1187 vm_object_release_pager(pager
);
1188 vm_object_lock(object
);
1191 /* kick off anyone waiting on terminating */
1192 object
->terminating
= FALSE
;
1193 vm_object_paging_begin(object
);
1194 vm_object_paging_end(object
);
1195 vm_object_unlock(object
);
1198 vm_external_destroy(object
->existence_map
, object
->size
);
1199 #endif /* MACH_PAGEMAP */
1202 * Free the space for the object.
1204 zfree(vm_object_zone
, object
);
1205 object
= VM_OBJECT_NULL
;
1209 vm_object_reap_async(
1213 mutex_assert(&vm_object_cached_lock_data
, MA_OWNED
);
1214 mutex_assert(&object
->Lock
, MA_OWNED
);
1217 vm_object_reap_count_async
++;
1219 /* enqueue the VM object... */
1220 queue_enter(&vm_object_reaper_queue
, object
,
1221 vm_object_t
, cached_list
);
1222 /* ... and wake up the reaper thread */
1223 thread_wakeup((event_t
) &vm_object_reaper_queue
);
1227 vm_object_reaper_thread(void)
1231 vm_object_cache_lock();
1233 while (!queue_empty(&vm_object_reaper_queue
)) {
1234 queue_remove_first(&vm_object_reaper_queue
,
1238 vm_object_lock(object
);
1239 assert(object
->terminating
);
1240 assert(!object
->alive
);
1242 vm_object_reap(object
);
1243 /* cache is unlocked and object is no longer valid */
1244 object
= VM_OBJECT_NULL
;
1246 vm_object_cache_lock();
1249 /* wait for more work... */
1250 assert_wait((event_t
) &vm_object_reaper_queue
, THREAD_UNINT
);
1251 vm_object_cache_unlock();
1252 thread_block((thread_continue_t
) vm_object_reaper_thread
);
1257 * Routine: vm_object_pager_wakeup
1258 * Purpose: Wake up anyone waiting for termination of a pager.
1262 vm_object_pager_wakeup(
1263 memory_object_t pager
)
1265 vm_object_hash_entry_t entry
;
1266 boolean_t waiting
= FALSE
;
1269 * If anyone was waiting for the memory_object_terminate
1270 * to be queued, wake them up now.
1272 vm_object_cache_lock();
1273 entry
= vm_object_hash_lookup(pager
, TRUE
);
1274 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
1275 waiting
= entry
->waiting
;
1276 vm_object_cache_unlock();
1277 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
) {
1279 thread_wakeup((event_t
) pager
);
1280 vm_object_hash_entry_free(entry
);
1285 * Routine: vm_object_release_pager
1286 * Purpose: Terminate the pager and, upon completion,
1287 * release our last reference to it.
1288 * just like memory_object_terminate, except
1289 * that we wake up anyone blocked in vm_object_enter
1290 * waiting for termination message to be queued
1291 * before calling memory_object_init.
1294 vm_object_release_pager(
1295 memory_object_t pager
)
1299 * Terminate the pager.
1302 (void) memory_object_terminate(pager
);
1305 * Wakeup anyone waiting for this terminate
1307 vm_object_pager_wakeup(pager
);
1310 * Release reference to pager.
1312 memory_object_deallocate(pager
);
1316 * Routine: vm_object_destroy
1318 * Shut down a VM object, despite the
1319 * presence of address map (or other) references
1325 __unused kern_return_t reason
)
1327 memory_object_t old_pager
;
1329 if (object
== VM_OBJECT_NULL
)
1330 return(KERN_SUCCESS
);
1333 * Remove the pager association immediately.
1335 * This will prevent the memory manager from further
1336 * meddling. [If it wanted to flush data or make
1337 * other changes, it should have done so before performing
1338 * the destroy call.]
1341 vm_object_cache_lock();
1342 vm_object_lock(object
);
1343 object
->can_persist
= FALSE
;
1344 object
->named
= FALSE
;
1345 object
->alive
= FALSE
;
1348 * Rip out the pager from the vm_object now...
1351 vm_object_remove(object
);
1352 old_pager
= object
->pager
;
1353 object
->pager
= MEMORY_OBJECT_NULL
;
1354 if (old_pager
!= MEMORY_OBJECT_NULL
)
1355 memory_object_control_disable(object
->pager_control
);
1356 vm_object_cache_unlock();
1359 * Wait for the existing paging activity (that got
1360 * through before we nulled out the pager) to subside.
1363 vm_object_paging_wait(object
, THREAD_UNINT
);
1364 vm_object_unlock(object
);
1367 * Terminate the object now.
1369 if (old_pager
!= MEMORY_OBJECT_NULL
) {
1370 vm_object_release_pager(old_pager
);
1373 * JMM - Release the caller's reference. This assumes the
1374 * caller had a reference to release, which is a big (but
1375 * currently valid) assumption if this is driven from the
1376 * vnode pager (it is holding a named reference when making
1379 vm_object_deallocate(object
);
1382 return(KERN_SUCCESS
);
1386 * vm_object_deactivate_pages
1388 * Deactivate all pages in the specified object. (Keep its pages
1389 * in memory even though it is no longer referenced.)
1391 * The object must be locked.
1394 vm_object_deactivate_all_pages(
1395 register vm_object_t object
)
1397 register vm_page_t p
;
1399 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1400 vm_page_lock_queues();
1402 vm_page_deactivate(p
);
1403 vm_page_unlock_queues();
1407 __private_extern__
void
1408 vm_object_deactivate_pages(
1410 vm_object_offset_t offset
,
1411 vm_object_size_t size
,
1412 boolean_t kill_page
)
1414 vm_object_t orig_object
;
1415 int pages_moved
= 0;
1416 int pages_found
= 0;
1419 * entered with object lock held, acquire a paging reference to
1420 * prevent the memory_object and control ports from
1423 orig_object
= object
;
1426 register vm_page_t m
;
1427 vm_object_offset_t toffset
;
1428 vm_object_size_t tsize
;
1430 vm_object_paging_begin(object
);
1431 vm_page_lock_queues();
1433 for (tsize
= size
, toffset
= offset
; tsize
; tsize
-= PAGE_SIZE
, toffset
+= PAGE_SIZE
) {
1435 if ((m
= vm_page_lookup(object
, toffset
)) != VM_PAGE_NULL
) {
1439 if ((m
->wire_count
== 0) && (!m
->private) && (!m
->gobbled
) && (!m
->busy
)) {
1441 assert(!m
->laundry
);
1443 m
->reference
= FALSE
;
1444 pmap_clear_reference(m
->phys_page
);
1446 if ((kill_page
) && (object
->internal
)) {
1447 m
->precious
= FALSE
;
1449 pmap_clear_modify(m
->phys_page
);
1450 vm_external_state_clr(object
->existence_map
, offset
);
1452 VM_PAGE_QUEUES_REMOVE(m
);
1454 assert(!m
->laundry
);
1455 assert(m
->object
!= kernel_object
);
1456 assert(m
->pageq
.next
== NULL
&&
1457 m
->pageq
.prev
== NULL
);
1461 m
, vm_page_t
, pageq
);
1464 &vm_page_queue_inactive
,
1465 m
, vm_page_t
, pageq
);
1470 vm_page_inactive_count
++;
1476 vm_page_unlock_queues();
1477 vm_object_paging_end(object
);
1479 if (object
->shadow
) {
1480 vm_object_t tmp_object
;
1484 offset
+= object
->shadow_offset
;
1486 tmp_object
= object
->shadow
;
1487 vm_object_lock(tmp_object
);
1489 if (object
!= orig_object
)
1490 vm_object_unlock(object
);
1491 object
= tmp_object
;
1495 if (object
!= orig_object
)
1496 vm_object_unlock(object
);
1500 * Routine: vm_object_pmap_protect
1503 * Reduces the permission for all physical
1504 * pages in the specified object range.
1506 * If removing write permission only, it is
1507 * sufficient to protect only the pages in
1508 * the top-level object; only those pages may
1509 * have write permission.
1511 * If removing all access, we must follow the
1512 * shadow chain from the top-level object to
1513 * remove access to all pages in shadowed objects.
1515 * The object must *not* be locked. The object must
1516 * be temporary/internal.
1518 * If pmap is not NULL, this routine assumes that
1519 * the only mappings for the pages are in that
1523 __private_extern__
void
1524 vm_object_pmap_protect(
1525 register vm_object_t object
,
1526 register vm_object_offset_t offset
,
1527 vm_object_size_t size
,
1529 vm_map_offset_t pmap_start
,
1532 if (object
== VM_OBJECT_NULL
)
1534 size
= vm_object_round_page(size
);
1535 offset
= vm_object_trunc_page(offset
);
1537 vm_object_lock(object
);
1539 assert(object
->internal
);
1542 if (ptoa_64(object
->resident_page_count
) > size
/2 && pmap
!= PMAP_NULL
) {
1543 vm_object_unlock(object
);
1544 pmap_protect(pmap
, pmap_start
, pmap_start
+ size
, prot
);
1548 /* if we are doing large ranges with respect to resident */
1549 /* page count then we should interate over pages otherwise */
1550 /* inverse page look-up will be faster */
1551 if (ptoa_64(object
->resident_page_count
/ 4) < size
) {
1553 vm_object_offset_t end
;
1555 end
= offset
+ size
;
1557 if (pmap
!= PMAP_NULL
) {
1558 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1559 if (!p
->fictitious
&&
1560 (offset
<= p
->offset
) && (p
->offset
< end
)) {
1561 vm_map_offset_t start
;
1563 start
= pmap_start
+ p
->offset
- offset
;
1564 pmap_protect(pmap
, start
, start
+ PAGE_SIZE_64
, prot
);
1568 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1569 if (!p
->fictitious
&&
1570 (offset
<= p
->offset
) && (p
->offset
< end
)) {
1572 pmap_page_protect(p
->phys_page
,
1573 prot
& ~p
->page_lock
);
1579 vm_object_offset_t end
;
1580 vm_object_offset_t target_off
;
1582 end
= offset
+ size
;
1584 if (pmap
!= PMAP_NULL
) {
1585 for(target_off
= offset
;
1587 target_off
+= PAGE_SIZE
) {
1588 p
= vm_page_lookup(object
, target_off
);
1589 if (p
!= VM_PAGE_NULL
) {
1591 start
= pmap_start
+
1592 (vm_offset_t
)(p
->offset
- offset
);
1593 pmap_protect(pmap
, start
,
1594 start
+ PAGE_SIZE
, prot
);
1598 for(target_off
= offset
;
1599 target_off
< end
; target_off
+= PAGE_SIZE
) {
1600 p
= vm_page_lookup(object
, target_off
);
1601 if (p
!= VM_PAGE_NULL
) {
1602 pmap_page_protect(p
->phys_page
,
1603 prot
& ~p
->page_lock
);
1609 if (prot
== VM_PROT_NONE
) {
1611 * Must follow shadow chain to remove access
1612 * to pages in shadowed objects.
1614 register vm_object_t next_object
;
1616 next_object
= object
->shadow
;
1617 if (next_object
!= VM_OBJECT_NULL
) {
1618 offset
+= object
->shadow_offset
;
1619 vm_object_lock(next_object
);
1620 vm_object_unlock(object
);
1621 object
= next_object
;
1625 * End of chain - we are done.
1632 * Pages in shadowed objects may never have
1633 * write permission - we may stop here.
1639 vm_object_unlock(object
);
1643 * Routine: vm_object_copy_slowly
1646 * Copy the specified range of the source
1647 * virtual memory object without using
1648 * protection-based optimizations (such
1649 * as copy-on-write). The pages in the
1650 * region are actually copied.
1652 * In/out conditions:
1653 * The caller must hold a reference and a lock
1654 * for the source virtual memory object. The source
1655 * object will be returned *unlocked*.
1658 * If the copy is completed successfully, KERN_SUCCESS is
1659 * returned. If the caller asserted the interruptible
1660 * argument, and an interruption occurred while waiting
1661 * for a user-generated event, MACH_SEND_INTERRUPTED is
1662 * returned. Other values may be returned to indicate
1663 * hard errors during the copy operation.
1665 * A new virtual memory object is returned in a
1666 * parameter (_result_object). The contents of this
1667 * new object, starting at a zero offset, are a copy
1668 * of the source memory region. In the event of
1669 * an error, this parameter will contain the value
1672 __private_extern__ kern_return_t
1673 vm_object_copy_slowly(
1674 register vm_object_t src_object
,
1675 vm_object_offset_t src_offset
,
1676 vm_object_size_t size
,
1677 boolean_t interruptible
,
1678 vm_object_t
*_result_object
) /* OUT */
1680 vm_object_t new_object
;
1681 vm_object_offset_t new_offset
;
1683 vm_object_offset_t src_lo_offset
= src_offset
;
1684 vm_object_offset_t src_hi_offset
= src_offset
+ size
;
1686 XPR(XPR_VM_OBJECT
, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
1687 src_object
, src_offset
, size
, 0, 0);
1690 vm_object_unlock(src_object
);
1691 *_result_object
= VM_OBJECT_NULL
;
1692 return(KERN_INVALID_ARGUMENT
);
1696 * Prevent destruction of the source object while we copy.
1699 assert(src_object
->ref_count
> 0);
1700 src_object
->ref_count
++;
1701 VM_OBJ_RES_INCR(src_object
);
1702 vm_object_unlock(src_object
);
1705 * Create a new object to hold the copied pages.
1707 * We fill the new object starting at offset 0,
1708 * regardless of the input offset.
1709 * We don't bother to lock the new object within
1710 * this routine, since we have the only reference.
1713 new_object
= vm_object_allocate(size
);
1715 vm_object_lock(new_object
);
1717 assert(size
== trunc_page_64(size
)); /* Will the loop terminate? */
1721 src_offset
+= PAGE_SIZE_64
,
1722 new_offset
+= PAGE_SIZE_64
, size
-= PAGE_SIZE_64
1725 vm_fault_return_t result
;
1727 while ((new_page
= vm_page_alloc(new_object
, new_offset
))
1729 if (!vm_page_wait(interruptible
)) {
1730 vm_object_unlock(new_object
);
1731 vm_object_deallocate(new_object
);
1732 vm_object_deallocate(src_object
);
1733 *_result_object
= VM_OBJECT_NULL
;
1734 return(MACH_SEND_INTERRUPTED
);
1739 vm_prot_t prot
= VM_PROT_READ
;
1740 vm_page_t _result_page
;
1743 vm_page_t result_page
;
1744 kern_return_t error_code
;
1746 vm_object_lock(src_object
);
1747 vm_object_paging_begin(src_object
);
1749 XPR(XPR_VM_FAULT
,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
1750 result
= vm_fault_page(src_object
, src_offset
,
1751 VM_PROT_READ
, FALSE
, interruptible
,
1752 src_lo_offset
, src_hi_offset
,
1753 VM_BEHAVIOR_SEQUENTIAL
,
1754 &prot
, &_result_page
, &top_page
,
1756 &error_code
, FALSE
, FALSE
, NULL
, 0);
1759 case VM_FAULT_SUCCESS
:
1760 result_page
= _result_page
;
1763 * We don't need to hold the object
1764 * lock -- the busy page will be enough.
1765 * [We don't care about picking up any
1766 * new modifications.]
1768 * Copy the page to the new object.
1771 * If result_page is clean,
1772 * we could steal it instead
1776 vm_object_unlock(result_page
->object
);
1777 vm_page_copy(result_page
, new_page
);
1780 * Let go of both pages (make them
1781 * not busy, perform wakeup, activate).
1784 new_page
->busy
= FALSE
;
1785 new_page
->dirty
= TRUE
;
1786 vm_object_lock(result_page
->object
);
1787 PAGE_WAKEUP_DONE(result_page
);
1789 vm_page_lock_queues();
1790 if (!result_page
->active
&&
1791 !result_page
->inactive
)
1792 vm_page_activate(result_page
);
1793 vm_page_activate(new_page
);
1794 vm_page_unlock_queues();
1797 * Release paging references and
1798 * top-level placeholder page, if any.
1801 vm_fault_cleanup(result_page
->object
,
1806 case VM_FAULT_RETRY
:
1809 case VM_FAULT_FICTITIOUS_SHORTAGE
:
1810 vm_page_more_fictitious();
1813 case VM_FAULT_MEMORY_SHORTAGE
:
1814 if (vm_page_wait(interruptible
))
1818 case VM_FAULT_INTERRUPTED
:
1819 vm_page_free(new_page
);
1820 vm_object_unlock(new_object
);
1821 vm_object_deallocate(new_object
);
1822 vm_object_deallocate(src_object
);
1823 *_result_object
= VM_OBJECT_NULL
;
1824 return(MACH_SEND_INTERRUPTED
);
1826 case VM_FAULT_MEMORY_ERROR
:
1829 * (a) ignore pages that we can't
1831 * (b) return the null object if
1832 * any page fails [chosen]
1835 vm_page_lock_queues();
1836 vm_page_free(new_page
);
1837 vm_page_unlock_queues();
1838 vm_object_unlock(new_object
);
1839 vm_object_deallocate(new_object
);
1840 vm_object_deallocate(src_object
);
1841 *_result_object
= VM_OBJECT_NULL
;
1842 return(error_code
? error_code
:
1845 } while (result
!= VM_FAULT_SUCCESS
);
1849 * Lose the extra reference, and return our object.
1852 vm_object_unlock(new_object
);
1853 vm_object_deallocate(src_object
);
1854 *_result_object
= new_object
;
1855 return(KERN_SUCCESS
);
1859 * Routine: vm_object_copy_quickly
1862 * Copy the specified range of the source virtual
1863 * memory object, if it can be done without waiting
1864 * for user-generated events.
1867 * If the copy is successful, the copy is returned in
1868 * the arguments; otherwise, the arguments are not
1871 * In/out conditions:
1872 * The object should be unlocked on entry and exit.
1876 __private_extern__ boolean_t
1877 vm_object_copy_quickly(
1878 vm_object_t
*_object
, /* INOUT */
1879 __unused vm_object_offset_t offset
, /* IN */
1880 __unused vm_object_size_t size
, /* IN */
1881 boolean_t
*_src_needs_copy
, /* OUT */
1882 boolean_t
*_dst_needs_copy
) /* OUT */
1884 vm_object_t object
= *_object
;
1885 memory_object_copy_strategy_t copy_strategy
;
1887 XPR(XPR_VM_OBJECT
, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
1888 *_object
, offset
, size
, 0, 0);
1889 if (object
== VM_OBJECT_NULL
) {
1890 *_src_needs_copy
= FALSE
;
1891 *_dst_needs_copy
= FALSE
;
1895 vm_object_lock(object
);
1897 copy_strategy
= object
->copy_strategy
;
1899 switch (copy_strategy
) {
1900 case MEMORY_OBJECT_COPY_SYMMETRIC
:
1903 * Symmetric copy strategy.
1904 * Make another reference to the object.
1905 * Leave object/offset unchanged.
1908 assert(object
->ref_count
> 0);
1909 object
->ref_count
++;
1910 vm_object_res_reference(object
);
1911 object
->shadowed
= TRUE
;
1912 vm_object_unlock(object
);
1915 * Both source and destination must make
1916 * shadows, and the source must be made
1917 * read-only if not already.
1920 *_src_needs_copy
= TRUE
;
1921 *_dst_needs_copy
= TRUE
;
1925 case MEMORY_OBJECT_COPY_DELAY
:
1926 vm_object_unlock(object
);
1930 vm_object_unlock(object
);
1936 static int copy_call_count
= 0;
1937 static int copy_call_sleep_count
= 0;
1938 static int copy_call_restart_count
= 0;
1941 * Routine: vm_object_copy_call [internal]
1944 * Copy the source object (src_object), using the
1945 * user-managed copy algorithm.
1947 * In/out conditions:
1948 * The source object must be locked on entry. It
1949 * will be *unlocked* on exit.
1952 * If the copy is successful, KERN_SUCCESS is returned.
1953 * A new object that represents the copied virtual
1954 * memory is returned in a parameter (*_result_object).
1955 * If the return value indicates an error, this parameter
1958 static kern_return_t
1959 vm_object_copy_call(
1960 vm_object_t src_object
,
1961 vm_object_offset_t src_offset
,
1962 vm_object_size_t size
,
1963 vm_object_t
*_result_object
) /* OUT */
1967 boolean_t check_ready
= FALSE
;
1970 * If a copy is already in progress, wait and retry.
1973 * Consider making this call interruptable, as Mike
1974 * intended it to be.
1977 * Need a counter or version or something to allow
1978 * us to use the copy that the currently requesting
1979 * thread is obtaining -- is it worth adding to the
1980 * vm object structure? Depends how common this case it.
1983 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
1984 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
1986 copy_call_restart_count
++;
1990 * Indicate (for the benefit of memory_object_create_copy)
1991 * that we want a copy for src_object. (Note that we cannot
1992 * do a real assert_wait before calling memory_object_copy,
1993 * so we simply set the flag.)
1996 vm_object_set_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
);
1997 vm_object_unlock(src_object
);
2000 * Ask the memory manager to give us a memory object
2001 * which represents a copy of the src object.
2002 * The memory manager may give us a memory object
2003 * which we already have, or it may give us a
2004 * new memory object. This memory object will arrive
2005 * via memory_object_create_copy.
2008 kr
= KERN_FAILURE
; /* XXX need to change memory_object.defs */
2009 if (kr
!= KERN_SUCCESS
) {
2014 * Wait for the copy to arrive.
2016 vm_object_lock(src_object
);
2017 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
2018 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
2020 copy_call_sleep_count
++;
2023 assert(src_object
->copy
!= VM_OBJECT_NULL
);
2024 copy
= src_object
->copy
;
2025 if (!vm_object_lock_try(copy
)) {
2026 vm_object_unlock(src_object
);
2027 mutex_pause(); /* wait a bit */
2028 vm_object_lock(src_object
);
2031 if (copy
->size
< src_offset
+size
)
2032 copy
->size
= src_offset
+size
;
2034 if (!copy
->pager_ready
)
2040 *_result_object
= copy
;
2041 vm_object_unlock(copy
);
2042 vm_object_unlock(src_object
);
2044 /* Wait for the copy to be ready. */
2045 if (check_ready
== TRUE
) {
2046 vm_object_lock(copy
);
2047 while (!copy
->pager_ready
) {
2048 vm_object_sleep(copy
, VM_OBJECT_EVENT_PAGER_READY
, THREAD_UNINT
);
2050 vm_object_unlock(copy
);
2053 return KERN_SUCCESS
;
2056 static int copy_delayed_lock_collisions
= 0;
2057 static int copy_delayed_max_collisions
= 0;
2058 static int copy_delayed_lock_contention
= 0;
2059 static int copy_delayed_protect_iterate
= 0;
2062 * Routine: vm_object_copy_delayed [internal]
2065 * Copy the specified virtual memory object, using
2066 * the asymmetric copy-on-write algorithm.
2068 * In/out conditions:
2069 * The src_object must be locked on entry. It will be unlocked
2070 * on exit - so the caller must also hold a reference to it.
2072 * This routine will not block waiting for user-generated
2073 * events. It is not interruptible.
2075 __private_extern__ vm_object_t
2076 vm_object_copy_delayed(
2077 vm_object_t src_object
,
2078 vm_object_offset_t src_offset
,
2079 vm_object_size_t size
)
2081 vm_object_t new_copy
= VM_OBJECT_NULL
;
2082 vm_object_t old_copy
;
2084 vm_object_size_t copy_size
= src_offset
+ size
;
2088 * The user-level memory manager wants to see all of the changes
2089 * to this object, but it has promised not to make any changes on
2092 * Perform an asymmetric copy-on-write, as follows:
2093 * Create a new object, called a "copy object" to hold
2094 * pages modified by the new mapping (i.e., the copy,
2095 * not the original mapping).
2096 * Record the original object as the backing object for
2097 * the copy object. If the original mapping does not
2098 * change a page, it may be used read-only by the copy.
2099 * Record the copy object in the original object.
2100 * When the original mapping causes a page to be modified,
2101 * it must be copied to a new page that is "pushed" to
2103 * Mark the new mapping (the copy object) copy-on-write.
2104 * This makes the copy object itself read-only, allowing
2105 * it to be reused if the original mapping makes no
2106 * changes, and simplifying the synchronization required
2107 * in the "push" operation described above.
2109 * The copy-on-write is said to be assymetric because the original
2110 * object is *not* marked copy-on-write. A copied page is pushed
2111 * to the copy object, regardless which party attempted to modify
2114 * Repeated asymmetric copy operations may be done. If the
2115 * original object has not been changed since the last copy, its
2116 * copy object can be reused. Otherwise, a new copy object can be
2117 * inserted between the original object and its previous copy
2118 * object. Since any copy object is read-only, this cannot affect
2119 * affect the contents of the previous copy object.
2121 * Note that a copy object is higher in the object tree than the
2122 * original object; therefore, use of the copy object recorded in
2123 * the original object must be done carefully, to avoid deadlock.
2129 * Wait for paging in progress.
2131 if (!src_object
->true_share
)
2132 vm_object_paging_wait(src_object
, THREAD_UNINT
);
2135 * See whether we can reuse the result of a previous
2139 old_copy
= src_object
->copy
;
2140 if (old_copy
!= VM_OBJECT_NULL
) {
2142 * Try to get the locks (out of order)
2144 if (!vm_object_lock_try(old_copy
)) {
2145 vm_object_unlock(src_object
);
2148 /* Heisenberg Rules */
2149 copy_delayed_lock_collisions
++;
2150 if (collisions
++ == 0)
2151 copy_delayed_lock_contention
++;
2153 if (collisions
> copy_delayed_max_collisions
)
2154 copy_delayed_max_collisions
= collisions
;
2156 vm_object_lock(src_object
);
2161 * Determine whether the old copy object has
2165 if (old_copy
->resident_page_count
== 0 &&
2166 !old_copy
->pager_created
) {
2168 * It has not been modified.
2170 * Return another reference to
2171 * the existing copy-object if
2172 * we can safely grow it (if
2176 if (old_copy
->size
< copy_size
) {
2178 * We can't perform a delayed copy if any of the
2179 * pages in the extended range are wired (because
2180 * we can't safely take write permission away from
2181 * wired pages). If the pages aren't wired, then
2182 * go ahead and protect them.
2184 copy_delayed_protect_iterate
++;
2185 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
2186 if (!p
->fictitious
&&
2187 p
->offset
>= old_copy
->size
&&
2188 p
->offset
< copy_size
) {
2189 if (p
->wire_count
> 0) {
2190 vm_object_unlock(old_copy
);
2191 vm_object_unlock(src_object
);
2193 if (new_copy
!= VM_OBJECT_NULL
) {
2194 vm_object_unlock(new_copy
);
2195 vm_object_deallocate(new_copy
);
2198 return VM_OBJECT_NULL
;
2200 pmap_page_protect(p
->phys_page
,
2201 (VM_PROT_ALL
& ~VM_PROT_WRITE
&
2206 old_copy
->size
= copy_size
;
2209 vm_object_reference_locked(old_copy
);
2210 vm_object_unlock(old_copy
);
2211 vm_object_unlock(src_object
);
2213 if (new_copy
!= VM_OBJECT_NULL
) {
2214 vm_object_unlock(new_copy
);
2215 vm_object_deallocate(new_copy
);
2222 * Adjust the size argument so that the newly-created
2223 * copy object will be large enough to back either the
2224 * old copy object or the new mapping.
2226 if (old_copy
->size
> copy_size
)
2227 copy_size
= old_copy
->size
;
2229 if (new_copy
== VM_OBJECT_NULL
) {
2230 vm_object_unlock(old_copy
);
2231 vm_object_unlock(src_object
);
2232 new_copy
= vm_object_allocate(copy_size
);
2233 vm_object_lock(src_object
);
2234 vm_object_lock(new_copy
);
2237 new_copy
->size
= copy_size
;
2240 * The copy-object is always made large enough to
2241 * completely shadow the original object, since
2242 * it may have several users who want to shadow
2243 * the original object at different points.
2246 assert((old_copy
->shadow
== src_object
) &&
2247 (old_copy
->shadow_offset
== (vm_object_offset_t
) 0));
2249 } else if (new_copy
== VM_OBJECT_NULL
) {
2250 vm_object_unlock(src_object
);
2251 new_copy
= vm_object_allocate(copy_size
);
2252 vm_object_lock(src_object
);
2253 vm_object_lock(new_copy
);
2258 * We now have the src object locked, and the new copy object
2259 * allocated and locked (and potentially the old copy locked).
2260 * Before we go any further, make sure we can still perform
2261 * a delayed copy, as the situation may have changed.
2263 * Specifically, we can't perform a delayed copy if any of the
2264 * pages in the range are wired (because we can't safely take
2265 * write permission away from wired pages). If the pages aren't
2266 * wired, then go ahead and protect them.
2268 copy_delayed_protect_iterate
++;
2269 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
2270 if (!p
->fictitious
&& p
->offset
< copy_size
) {
2271 if (p
->wire_count
> 0) {
2273 vm_object_unlock(old_copy
);
2274 vm_object_unlock(src_object
);
2275 vm_object_unlock(new_copy
);
2276 vm_object_deallocate(new_copy
);
2277 return VM_OBJECT_NULL
;
2279 pmap_page_protect(p
->phys_page
,
2280 (VM_PROT_ALL
& ~VM_PROT_WRITE
&
2286 if (old_copy
!= VM_OBJECT_NULL
) {
2288 * Make the old copy-object shadow the new one.
2289 * It will receive no more pages from the original
2293 src_object
->ref_count
--; /* remove ref. from old_copy */
2294 assert(src_object
->ref_count
> 0);
2295 old_copy
->shadow
= new_copy
;
2296 assert(new_copy
->ref_count
> 0);
2297 new_copy
->ref_count
++; /* for old_copy->shadow ref. */
2300 if (old_copy
->res_count
) {
2301 VM_OBJ_RES_INCR(new_copy
);
2302 VM_OBJ_RES_DECR(src_object
);
2306 vm_object_unlock(old_copy
); /* done with old_copy */
2310 * Point the new copy at the existing object.
2312 new_copy
->shadow
= src_object
;
2313 new_copy
->shadow_offset
= 0;
2314 new_copy
->shadowed
= TRUE
; /* caller must set needs_copy */
2315 assert(src_object
->ref_count
> 0);
2316 src_object
->ref_count
++;
2317 VM_OBJ_RES_INCR(src_object
);
2318 src_object
->copy
= new_copy
;
2319 vm_object_unlock(src_object
);
2320 vm_object_unlock(new_copy
);
2323 "vm_object_copy_delayed: used copy object %X for source %X\n",
2324 (integer_t
)new_copy
, (integer_t
)src_object
, 0, 0, 0);
2330 * Routine: vm_object_copy_strategically
2333 * Perform a copy according to the source object's
2334 * declared strategy. This operation may block,
2335 * and may be interrupted.
2337 __private_extern__ kern_return_t
2338 vm_object_copy_strategically(
2339 register vm_object_t src_object
,
2340 vm_object_offset_t src_offset
,
2341 vm_object_size_t size
,
2342 vm_object_t
*dst_object
, /* OUT */
2343 vm_object_offset_t
*dst_offset
, /* OUT */
2344 boolean_t
*dst_needs_copy
) /* OUT */
2347 boolean_t interruptible
= THREAD_ABORTSAFE
; /* XXX */
2348 memory_object_copy_strategy_t copy_strategy
;
2350 assert(src_object
!= VM_OBJECT_NULL
);
2352 vm_object_lock(src_object
);
2355 * The copy strategy is only valid if the memory manager
2356 * is "ready". Internal objects are always ready.
2359 while (!src_object
->internal
&& !src_object
->pager_ready
) {
2360 wait_result_t wait_result
;
2362 wait_result
= vm_object_sleep( src_object
,
2363 VM_OBJECT_EVENT_PAGER_READY
,
2365 if (wait_result
!= THREAD_AWAKENED
) {
2366 vm_object_unlock(src_object
);
2367 *dst_object
= VM_OBJECT_NULL
;
2369 *dst_needs_copy
= FALSE
;
2370 return(MACH_SEND_INTERRUPTED
);
2374 copy_strategy
= src_object
->copy_strategy
;
2377 * Use the appropriate copy strategy.
2380 switch (copy_strategy
) {
2381 case MEMORY_OBJECT_COPY_DELAY
:
2382 *dst_object
= vm_object_copy_delayed(src_object
,
2384 if (*dst_object
!= VM_OBJECT_NULL
) {
2385 *dst_offset
= src_offset
;
2386 *dst_needs_copy
= TRUE
;
2387 result
= KERN_SUCCESS
;
2390 vm_object_lock(src_object
);
2391 /* fall thru when delayed copy not allowed */
2393 case MEMORY_OBJECT_COPY_NONE
:
2394 result
= vm_object_copy_slowly(src_object
, src_offset
, size
,
2395 interruptible
, dst_object
);
2396 if (result
== KERN_SUCCESS
) {
2398 *dst_needs_copy
= FALSE
;
2402 case MEMORY_OBJECT_COPY_CALL
:
2403 result
= vm_object_copy_call(src_object
, src_offset
, size
,
2405 if (result
== KERN_SUCCESS
) {
2406 *dst_offset
= src_offset
;
2407 *dst_needs_copy
= TRUE
;
2411 case MEMORY_OBJECT_COPY_SYMMETRIC
:
2412 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);
2413 vm_object_unlock(src_object
);
2414 result
= KERN_MEMORY_RESTART_COPY
;
2418 panic("copy_strategically: bad strategy");
2419 result
= KERN_INVALID_ARGUMENT
;
2427 * Create a new object which is backed by the
2428 * specified existing object range. The source
2429 * object reference is deallocated.
2431 * The new object and offset into that object
2432 * are returned in the source parameters.
2434 boolean_t vm_object_shadow_check
= FALSE
;
2436 __private_extern__ boolean_t
2438 vm_object_t
*object
, /* IN/OUT */
2439 vm_object_offset_t
*offset
, /* IN/OUT */
2440 vm_object_size_t length
)
2442 register vm_object_t source
;
2443 register vm_object_t result
;
2446 assert(source
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
);
2449 * Determine if we really need a shadow.
2452 if (vm_object_shadow_check
&& source
->ref_count
== 1 &&
2453 (source
->shadow
== VM_OBJECT_NULL
||
2454 source
->shadow
->copy
== VM_OBJECT_NULL
))
2456 source
->shadowed
= FALSE
;
2461 * Allocate a new object with the given length
2464 if ((result
= vm_object_allocate(length
)) == VM_OBJECT_NULL
)
2465 panic("vm_object_shadow: no object for shadowing");
2468 * The new object shadows the source object, adding
2469 * a reference to it. Our caller changes his reference
2470 * to point to the new object, removing a reference to
2471 * the source object. Net result: no change of reference
2474 result
->shadow
= source
;
2477 * Store the offset into the source object,
2478 * and fix up the offset into the new object.
2481 result
->shadow_offset
= *offset
;
2484 * Return the new things
2493 * The relationship between vm_object structures and
2494 * the memory_object requires careful synchronization.
2496 * All associations are created by memory_object_create_named
2497 * for external pagers and vm_object_pager_create for internal
2498 * objects as follows:
2500 * pager: the memory_object itself, supplied by
2501 * the user requesting a mapping (or the kernel,
2502 * when initializing internal objects); the
2503 * kernel simulates holding send rights by keeping
2507 * the memory object control port,
2508 * created by the kernel; the kernel holds
2509 * receive (and ownership) rights to this
2510 * port, but no other references.
2512 * When initialization is complete, the "initialized" field
2513 * is asserted. Other mappings using a particular memory object,
2514 * and any references to the vm_object gained through the
2515 * port association must wait for this initialization to occur.
2517 * In order to allow the memory manager to set attributes before
2518 * requests (notably virtual copy operations, but also data or
2519 * unlock requests) are made, a "ready" attribute is made available.
2520 * Only the memory manager may affect the value of this attribute.
2521 * Its value does not affect critical kernel functions, such as
2522 * internal object initialization or destruction. [Furthermore,
2523 * memory objects created by the kernel are assumed to be ready
2524 * immediately; the default memory manager need not explicitly
2525 * set the "ready" attribute.]
2527 * [Both the "initialized" and "ready" attribute wait conditions
2528 * use the "pager" field as the wait event.]
2530 * The port associations can be broken down by any of the
2531 * following routines:
2532 * vm_object_terminate:
2533 * No references to the vm_object remain, and
2534 * the object cannot (or will not) be cached.
2535 * This is the normal case, and is done even
2536 * though one of the other cases has already been
2538 * memory_object_destroy:
2539 * The memory manager has requested that the
2540 * kernel relinquish references to the memory
2541 * object. [The memory manager may not want to
2542 * destroy the memory object, but may wish to
2543 * refuse or tear down existing memory mappings.]
2545 * Each routine that breaks an association must break all of
2546 * them at once. At some later time, that routine must clear
2547 * the pager field and release the memory object references.
2548 * [Furthermore, each routine must cope with the simultaneous
2549 * or previous operations of the others.]
2551 * In addition to the lock on the object, the vm_object_cache_lock
2552 * governs the associations. References gained through the
2553 * association require use of the cache lock.
2555 * Because the pager field may be cleared spontaneously, it
2556 * cannot be used to determine whether a memory object has
2557 * ever been associated with a particular vm_object. [This
2558 * knowledge is important to the shadow object mechanism.]
2559 * For this reason, an additional "created" attribute is
2562 * During various paging operations, the pager reference found in the
2563 * vm_object must be valid. To prevent this from being released,
2564 * (other than being removed, i.e., made null), routines may use
2565 * the vm_object_paging_begin/end routines [actually, macros].
2566 * The implementation uses the "paging_in_progress" and "wanted" fields.
2567 * [Operations that alter the validity of the pager values include the
2568 * termination routines and vm_object_collapse.]
2572 static void vm_object_abort_activity(
2573 vm_object_t object
);
2576 * Routine: vm_object_abort_activity [internal use only]
2578 * Abort paging requests pending on this object.
2579 * In/out conditions:
2580 * The object is locked on entry and exit.
2583 vm_object_abort_activity(
2590 XPR(XPR_VM_OBJECT
, "vm_object_abort_activity, object 0x%X\n",
2591 (integer_t
)object
, 0, 0, 0, 0);
2594 * Abort all activity that would be waiting
2595 * for a result on this memory object.
2597 * We could also choose to destroy all pages
2598 * that we have in memory for this object, but
2602 p
= (vm_page_t
) queue_first(&object
->memq
);
2603 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
2604 next
= (vm_page_t
) queue_next(&p
->listq
);
2607 * If it's being paged in, destroy it.
2608 * If an unlock has been requested, start it again.
2611 if (p
->busy
&& p
->absent
) {
2615 if (p
->unlock_request
!= VM_PROT_NONE
)
2616 p
->unlock_request
= VM_PROT_NONE
;
2624 * Wake up threads waiting for the memory object to
2628 object
->pager_ready
= TRUE
;
2629 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
2633 * Routine: vm_object_pager_dead
2636 * A port is being destroy, and the IPC kobject code
2637 * can't tell if it represents a pager port or not.
2638 * So this function is called each time it sees a port
2640 * THIS IS HORRIBLY INEFFICIENT. We should only call
2641 * this routine if we had requested a notification on
2645 __private_extern__
void
2646 vm_object_pager_dead(
2650 vm_object_hash_entry_t entry
;
2653 * Perform essentially the same operations as in vm_object_lookup,
2654 * except that this time we look up based on the memory_object
2655 * port, not the control port.
2657 vm_object_cache_lock();
2658 entry
= vm_object_hash_lookup(pager
, FALSE
);
2659 if (entry
== VM_OBJECT_HASH_ENTRY_NULL
||
2660 entry
->object
== VM_OBJECT_NULL
) {
2661 vm_object_cache_unlock();
2665 object
= entry
->object
;
2666 entry
->object
= VM_OBJECT_NULL
;
2668 vm_object_lock(object
);
2669 if (object
->ref_count
== 0) {
2670 XPR(XPR_VM_OBJECT_CACHE
,
2671 "vm_object_destroy: removing %x from cache, head (%x, %x)\n",
2673 (integer_t
)vm_object_cached_list
.next
,
2674 (integer_t
)vm_object_cached_list
.prev
, 0,0);
2676 queue_remove(&vm_object_cached_list
, object
,
2677 vm_object_t
, cached_list
);
2678 vm_object_cached_count
--;
2680 object
->ref_count
++;
2681 vm_object_res_reference(object
);
2683 object
->can_persist
= FALSE
;
2685 assert(object
->pager
== pager
);
2688 * Remove the pager association.
2690 * Note that the memory_object itself is dead, so
2691 * we don't bother with it.
2694 object
->pager
= MEMORY_OBJECT_NULL
;
2696 vm_object_unlock(object
);
2697 vm_object_cache_unlock();
2699 vm_object_pager_wakeup(pager
);
2702 * Release the pager reference. Note that there's no
2703 * point in trying the memory_object_terminate call
2704 * because the memory_object itself is dead. Also
2705 * release the memory_object_control reference, since
2706 * the pager didn't do that either.
2709 memory_object_deallocate(pager
);
2710 memory_object_control_deallocate(object
->pager_request
);
2714 * Restart pending page requests
2716 vm_object_lock(object
);
2717 vm_object_abort_activity(object
);
2718 vm_object_unlock(object
);
2721 * Lose the object reference.
2724 vm_object_deallocate(object
);
2729 * Routine: vm_object_enter
2731 * Find a VM object corresponding to the given
2732 * pager; if no such object exists, create one,
2733 * and initialize the pager.
2737 memory_object_t pager
,
2738 vm_object_size_t size
,
2743 register vm_object_t object
;
2744 vm_object_t new_object
;
2745 boolean_t must_init
;
2746 vm_object_hash_entry_t entry
, new_entry
;
2748 if (pager
== MEMORY_OBJECT_NULL
)
2749 return(vm_object_allocate(size
));
2751 new_object
= VM_OBJECT_NULL
;
2752 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
2756 * Look for an object associated with this port.
2759 vm_object_cache_lock();
2761 entry
= vm_object_hash_lookup(pager
, FALSE
);
2763 if (entry
== VM_OBJECT_HASH_ENTRY_NULL
) {
2764 if (new_object
== VM_OBJECT_NULL
) {
2766 * We must unlock to create a new object;
2767 * if we do so, we must try the lookup again.
2769 vm_object_cache_unlock();
2770 assert(new_entry
== VM_OBJECT_HASH_ENTRY_NULL
);
2771 new_entry
= vm_object_hash_entry_alloc(pager
);
2772 new_object
= vm_object_allocate(size
);
2773 vm_object_cache_lock();
2776 * Lookup failed twice, and we have something
2777 * to insert; set the object.
2779 vm_object_hash_insert(new_entry
);
2781 entry
->object
= new_object
;
2782 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
2783 new_object
= VM_OBJECT_NULL
;
2786 } else if (entry
->object
== VM_OBJECT_NULL
) {
2788 * If a previous object is being terminated,
2789 * we must wait for the termination message
2790 * to be queued (and lookup the entry again).
2792 entry
->waiting
= TRUE
;
2793 entry
= VM_OBJECT_HASH_ENTRY_NULL
;
2794 assert_wait((event_t
) pager
, THREAD_UNINT
);
2795 vm_object_cache_unlock();
2796 thread_block(THREAD_CONTINUE_NULL
);
2797 vm_object_cache_lock();
2799 } while (entry
== VM_OBJECT_HASH_ENTRY_NULL
);
2801 object
= entry
->object
;
2802 assert(object
!= VM_OBJECT_NULL
);
2805 vm_object_lock(object
);
2806 assert(!internal
|| object
->internal
);
2808 assert(!object
->named
);
2809 object
->named
= TRUE
;
2811 if (object
->ref_count
== 0) {
2812 XPR(XPR_VM_OBJECT_CACHE
,
2813 "vm_object_enter: removing %x from cache, head (%x, %x)\n",
2815 (integer_t
)vm_object_cached_list
.next
,
2816 (integer_t
)vm_object_cached_list
.prev
, 0,0);
2817 queue_remove(&vm_object_cached_list
, object
,
2818 vm_object_t
, cached_list
);
2819 vm_object_cached_count
--;
2821 object
->ref_count
++;
2822 vm_object_res_reference(object
);
2823 vm_object_unlock(object
);
2827 assert(object
->ref_count
> 0);
2831 vm_object_cache_unlock();
2834 "vm_o_enter: pager 0x%x obj 0x%x must_init %d\n",
2835 (integer_t
)pager
, (integer_t
)object
, must_init
, 0, 0);
2838 * If we raced to create a vm_object but lost, let's
2842 if (new_object
!= VM_OBJECT_NULL
)
2843 vm_object_deallocate(new_object
);
2845 if (new_entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
2846 vm_object_hash_entry_free(new_entry
);
2849 memory_object_control_t control
;
2852 * Allocate request port.
2855 control
= memory_object_control_allocate(object
);
2856 assert (control
!= MEMORY_OBJECT_CONTROL_NULL
);
2858 vm_object_lock(object
);
2859 assert(object
!= kernel_object
);
2862 * Copy the reference we were given.
2865 memory_object_reference(pager
);
2866 object
->pager_created
= TRUE
;
2867 object
->pager
= pager
;
2868 object
->internal
= internal
;
2869 object
->pager_trusted
= internal
;
2871 /* copy strategy invalid until set by memory manager */
2872 object
->copy_strategy
= MEMORY_OBJECT_COPY_INVALID
;
2874 object
->pager_control
= control
;
2875 object
->pager_ready
= FALSE
;
2877 vm_object_unlock(object
);
2880 * Let the pager know we're using it.
2883 (void) memory_object_init(pager
,
2884 object
->pager_control
,
2887 vm_object_lock(object
);
2889 object
->named
= TRUE
;
2891 object
->pager_ready
= TRUE
;
2892 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
2895 object
->pager_initialized
= TRUE
;
2896 vm_object_wakeup(object
, VM_OBJECT_EVENT_INITIALIZED
);
2898 vm_object_lock(object
);
2902 * [At this point, the object must be locked]
2906 * Wait for the work above to be done by the first
2907 * thread to map this object.
2910 while (!object
->pager_initialized
) {
2911 vm_object_sleep(object
,
2912 VM_OBJECT_EVENT_INITIALIZED
,
2915 vm_object_unlock(object
);
2918 "vm_object_enter: vm_object %x, memory_object %x, internal %d\n",
2919 (integer_t
)object
, (integer_t
)object
->pager
, internal
, 0,0);
2924 * Routine: vm_object_pager_create
2926 * Create a memory object for an internal object.
2927 * In/out conditions:
2928 * The object is locked on entry and exit;
2929 * it may be unlocked within this call.
2931 * Only one thread may be performing a
2932 * vm_object_pager_create on an object at
2933 * a time. Presumably, only the pageout
2934 * daemon will be using this routine.
2938 vm_object_pager_create(
2939 register vm_object_t object
)
2941 memory_object_t pager
;
2942 vm_object_hash_entry_t entry
;
2944 vm_object_size_t size
;
2945 vm_external_map_t map
;
2946 #endif /* MACH_PAGEMAP */
2948 XPR(XPR_VM_OBJECT
, "vm_object_pager_create, object 0x%X\n",
2949 (integer_t
)object
, 0,0,0,0);
2951 assert(object
!= kernel_object
);
2953 if (memory_manager_default_check() != KERN_SUCCESS
)
2957 * Prevent collapse or termination by holding a paging reference
2960 vm_object_paging_begin(object
);
2961 if (object
->pager_created
) {
2963 * Someone else got to it first...
2964 * wait for them to finish initializing the ports
2966 while (!object
->pager_initialized
) {
2967 vm_object_sleep(object
,
2968 VM_OBJECT_EVENT_INITIALIZED
,
2971 vm_object_paging_end(object
);
2976 * Indicate that a memory object has been assigned
2977 * before dropping the lock, to prevent a race.
2980 object
->pager_created
= TRUE
;
2981 object
->paging_offset
= 0;
2984 size
= object
->size
;
2985 #endif /* MACH_PAGEMAP */
2986 vm_object_unlock(object
);
2989 map
= vm_external_create(size
);
2990 vm_object_lock(object
);
2991 assert(object
->size
== size
);
2992 object
->existence_map
= map
;
2993 vm_object_unlock(object
);
2994 #endif /* MACH_PAGEMAP */
2997 * Create the [internal] pager, and associate it with this object.
2999 * We make the association here so that vm_object_enter()
3000 * can look up the object to complete initializing it. No
3001 * user will ever map this object.
3004 memory_object_default_t dmm
;
3005 vm_size_t cluster_size
;
3007 /* acquire a reference for the default memory manager */
3008 dmm
= memory_manager_default_reference(&cluster_size
);
3009 assert(cluster_size
>= PAGE_SIZE
);
3011 object
->cluster_size
= cluster_size
; /* XXX ??? */
3012 assert(object
->temporary
);
3014 /* create our new memory object */
3015 (void) memory_object_create(dmm
, object
->size
, &pager
);
3017 memory_object_default_deallocate(dmm
);
3020 entry
= vm_object_hash_entry_alloc(pager
);
3022 vm_object_cache_lock();
3023 vm_object_hash_insert(entry
);
3025 entry
->object
= object
;
3026 vm_object_cache_unlock();
3029 * A reference was returned by
3030 * memory_object_create(), and it is
3031 * copied by vm_object_enter().
3034 if (vm_object_enter(pager
, object
->size
, TRUE
, TRUE
, FALSE
) != object
)
3035 panic("vm_object_pager_create: mismatch");
3038 * Drop the reference we were passed.
3040 memory_object_deallocate(pager
);
3042 vm_object_lock(object
);
3045 * Release the paging reference
3047 vm_object_paging_end(object
);
3051 * Routine: vm_object_remove
3053 * Eliminate the pager/object association
3056 * The object cache must be locked.
3058 __private_extern__
void
3062 memory_object_t pager
;
3064 if ((pager
= object
->pager
) != MEMORY_OBJECT_NULL
) {
3065 vm_object_hash_entry_t entry
;
3067 entry
= vm_object_hash_lookup(pager
, FALSE
);
3068 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
3069 entry
->object
= VM_OBJECT_NULL
;
3075 * Global variables for vm_object_collapse():
3077 * Counts for normal collapses and bypasses.
3078 * Debugging variables, to watch or disable collapse.
3080 static long object_collapses
= 0;
3081 static long object_bypasses
= 0;
3083 static boolean_t vm_object_collapse_allowed
= TRUE
;
3084 static boolean_t vm_object_bypass_allowed
= TRUE
;
3086 static int vm_external_discarded
;
3087 static int vm_external_collapsed
;
3089 unsigned long vm_object_collapse_encrypted
= 0;
3092 * Routine: vm_object_do_collapse
3094 * Collapse an object with the object backing it.
3095 * Pages in the backing object are moved into the
3096 * parent, and the backing object is deallocated.
3098 * Both objects and the cache are locked; the page
3099 * queues are unlocked.
3103 vm_object_do_collapse(
3105 vm_object_t backing_object
)
3108 vm_object_offset_t new_offset
, backing_offset
;
3109 vm_object_size_t size
;
3111 backing_offset
= object
->shadow_offset
;
3112 size
= object
->size
;
3115 * Move all in-memory pages from backing_object
3116 * to the parent. Pages that have been paged out
3117 * will be overwritten by any of the parent's
3118 * pages that shadow them.
3121 while (!queue_empty(&backing_object
->memq
)) {
3123 p
= (vm_page_t
) queue_first(&backing_object
->memq
);
3125 new_offset
= (p
->offset
- backing_offset
);
3127 assert(!p
->busy
|| p
->absent
);
3130 * If the parent has a page here, or if
3131 * this page falls outside the parent,
3134 * Otherwise, move it as planned.
3137 if (p
->offset
< backing_offset
|| new_offset
>= size
) {
3142 * The encryption key includes the "pager" and the
3143 * "paging_offset". These might not be the same in
3144 * the new object, so we can't just move an encrypted
3145 * page from one object to the other. We can't just
3146 * decrypt the page here either, because that would drop
3148 * The caller should check for encrypted pages before
3149 * attempting to collapse.
3151 ASSERT_PAGE_DECRYPTED(p
);
3153 pp
= vm_page_lookup(object
, new_offset
);
3154 if (pp
== VM_PAGE_NULL
) {
3157 * Parent now has no page.
3158 * Move the backing object's page up.
3161 vm_page_rename(p
, object
, new_offset
);
3163 } else if (pp
->absent
) {
3166 * Parent has an absent page...
3167 * it's not being paged in, so
3168 * it must really be missing from
3171 * Throw out the absent page...
3172 * any faults looking for that
3173 * page will restart with the new
3178 vm_page_rename(p
, object
, new_offset
);
3179 #endif /* MACH_PAGEMAP */
3181 assert(! pp
->absent
);
3184 * Parent object has a real page.
3185 * Throw away the backing object's
3194 assert(!object
->pager_created
&& object
->pager
== MEMORY_OBJECT_NULL
3195 || (!backing_object
->pager_created
3196 && backing_object
->pager
== MEMORY_OBJECT_NULL
));
3198 assert(!object
->pager_created
&& object
->pager
== MEMORY_OBJECT_NULL
);
3199 #endif /* !MACH_PAGEMAP */
3201 if (backing_object
->pager
!= MEMORY_OBJECT_NULL
) {
3202 vm_object_hash_entry_t entry
;
3205 * Move the pager from backing_object to object.
3207 * XXX We're only using part of the paging space
3208 * for keeps now... we ought to discard the
3212 assert(!object
->paging_in_progress
);
3213 object
->pager
= backing_object
->pager
;
3214 entry
= vm_object_hash_lookup(object
->pager
, FALSE
);
3215 assert(entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
3216 entry
->object
= object
;
3217 object
->pager_created
= backing_object
->pager_created
;
3218 object
->pager_control
= backing_object
->pager_control
;
3219 object
->pager_ready
= backing_object
->pager_ready
;
3220 object
->pager_initialized
= backing_object
->pager_initialized
;
3221 object
->cluster_size
= backing_object
->cluster_size
;
3222 object
->paging_offset
=
3223 backing_object
->paging_offset
+ backing_offset
;
3224 if (object
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
3225 memory_object_control_collapse(object
->pager_control
,
3230 vm_object_cache_unlock();
3234 * If the shadow offset is 0, the use the existence map from
3235 * the backing object if there is one. If the shadow offset is
3236 * not zero, toss it.
3238 * XXX - If the shadow offset is not 0 then a bit copy is needed
3239 * if the map is to be salvaged. For now, we just just toss the
3240 * old map, giving the collapsed object no map. This means that
3241 * the pager is invoked for zero fill pages. If analysis shows
3242 * that this happens frequently and is a performance hit, then
3243 * this code should be fixed to salvage the map.
3245 assert(object
->existence_map
== VM_EXTERNAL_NULL
);
3246 if (backing_offset
|| (size
!= backing_object
->size
)) {
3247 vm_external_discarded
++;
3248 vm_external_destroy(backing_object
->existence_map
,
3249 backing_object
->size
);
3252 vm_external_collapsed
++;
3253 object
->existence_map
= backing_object
->existence_map
;
3255 backing_object
->existence_map
= VM_EXTERNAL_NULL
;
3256 #endif /* MACH_PAGEMAP */
3259 * Object now shadows whatever backing_object did.
3260 * Note that the reference to backing_object->shadow
3261 * moves from within backing_object to within object.
3264 assert(!object
->phys_contiguous
);
3265 assert(!backing_object
->phys_contiguous
);
3266 object
->shadow
= backing_object
->shadow
;
3267 if (object
->shadow
) {
3268 object
->shadow_offset
+= backing_object
->shadow_offset
;
3270 /* no shadow, therefore no shadow offset... */
3271 object
->shadow_offset
= 0;
3273 assert((object
->shadow
== VM_OBJECT_NULL
) ||
3274 (object
->shadow
->copy
!= backing_object
));
3277 * Discard backing_object.
3279 * Since the backing object has no pages, no
3280 * pager left, and no object references within it,
3281 * all that is necessary is to dispose of it.
3284 assert((backing_object
->ref_count
== 1) &&
3285 (backing_object
->resident_page_count
== 0) &&
3286 (backing_object
->paging_in_progress
== 0));
3288 backing_object
->alive
= FALSE
;
3289 vm_object_unlock(backing_object
);
3291 XPR(XPR_VM_OBJECT
, "vm_object_collapse, collapsed 0x%X\n",
3292 (integer_t
)backing_object
, 0,0,0,0);
3294 zfree(vm_object_zone
, backing_object
);
3300 vm_object_do_bypass(
3302 vm_object_t backing_object
)
3305 * Make the parent shadow the next object
3311 * Do object reference in-line to
3312 * conditionally increment shadow's
3313 * residence count. If object is not
3314 * resident, leave residence count
3317 if (backing_object
->shadow
!= VM_OBJECT_NULL
) {
3318 vm_object_lock(backing_object
->shadow
);
3319 backing_object
->shadow
->ref_count
++;
3320 if (object
->res_count
!= 0)
3321 vm_object_res_reference(backing_object
->shadow
);
3322 vm_object_unlock(backing_object
->shadow
);
3324 #else /* TASK_SWAPPER */
3325 vm_object_reference(backing_object
->shadow
);
3326 #endif /* TASK_SWAPPER */
3328 assert(!object
->phys_contiguous
);
3329 assert(!backing_object
->phys_contiguous
);
3330 object
->shadow
= backing_object
->shadow
;
3331 if (object
->shadow
) {
3332 object
->shadow_offset
+= backing_object
->shadow_offset
;
3334 /* no shadow, therefore no shadow offset... */
3335 object
->shadow_offset
= 0;
3339 * Backing object might have had a copy pointer
3340 * to us. If it did, clear it.
3342 if (backing_object
->copy
== object
) {
3343 backing_object
->copy
= VM_OBJECT_NULL
;
3347 * Drop the reference count on backing_object.
3349 * Since its ref_count was at least 2, it
3350 * will not vanish; so we don't need to call
3351 * vm_object_deallocate.
3352 * [FBDP: that doesn't seem to be true any more]
3354 * The res_count on the backing object is
3355 * conditionally decremented. It's possible
3356 * (via vm_pageout_scan) to get here with
3357 * a "swapped" object, which has a 0 res_count,
3358 * in which case, the backing object res_count
3359 * is already down by one.
3361 * Don't call vm_object_deallocate unless
3362 * ref_count drops to zero.
3364 * The ref_count can drop to zero here if the
3365 * backing object could be bypassed but not
3366 * collapsed, such as when the backing object
3367 * is temporary and cachable.
3370 if (backing_object
->ref_count
> 1) {
3371 backing_object
->ref_count
--;
3373 if (object
->res_count
!= 0)
3374 vm_object_res_deallocate(backing_object
);
3375 assert(backing_object
->ref_count
> 0);
3376 #endif /* TASK_SWAPPER */
3377 vm_object_unlock(backing_object
);
3381 * Drop locks so that we can deallocate
3382 * the backing object.
3386 if (object
->res_count
== 0) {
3387 /* XXX get a reference for the deallocate below */
3388 vm_object_res_reference(backing_object
);
3390 #endif /* TASK_SWAPPER */
3391 vm_object_unlock(object
);
3392 vm_object_unlock(backing_object
);
3393 vm_object_deallocate(backing_object
);
3396 * Relock object. We don't have to reverify
3397 * its state since vm_object_collapse will
3398 * do that for us as it starts at the
3402 vm_object_lock(object
);
3410 * vm_object_collapse:
3412 * Perform an object collapse or an object bypass if appropriate.
3413 * The real work of collapsing and bypassing is performed in
3414 * the routines vm_object_do_collapse and vm_object_do_bypass.
3416 * Requires that the object be locked and the page queues be unlocked.
3419 static unsigned long vm_object_collapse_calls
= 0;
3420 static unsigned long vm_object_collapse_objects
= 0;
3421 static unsigned long vm_object_collapse_do_collapse
= 0;
3422 static unsigned long vm_object_collapse_do_bypass
= 0;
3423 __private_extern__
void
3425 register vm_object_t object
,
3426 register vm_object_offset_t hint_offset
)
3428 register vm_object_t backing_object
;
3429 register unsigned int rcount
;
3430 register unsigned int size
;
3431 vm_object_offset_t collapse_min_offset
;
3432 vm_object_offset_t collapse_max_offset
;
3434 vm_object_t original_object
;
3436 vm_object_collapse_calls
++;
3438 if (! vm_object_collapse_allowed
&& ! vm_object_bypass_allowed
) {
3442 XPR(XPR_VM_OBJECT
, "vm_object_collapse, obj 0x%X\n",
3443 (integer_t
)object
, 0,0,0,0);
3445 if (object
== VM_OBJECT_NULL
)
3448 original_object
= object
;
3451 vm_object_collapse_objects
++;
3453 * Verify that the conditions are right for either
3454 * collapse or bypass:
3458 * There is a backing object, and
3461 backing_object
= object
->shadow
;
3462 if (backing_object
== VM_OBJECT_NULL
) {
3463 if (object
!= original_object
) {
3464 vm_object_unlock(object
);
3470 * No pages in the object are currently
3471 * being paged out, and
3473 if (object
->paging_in_progress
!= 0 ||
3474 object
->absent_count
!= 0) {
3475 /* try and collapse the rest of the shadow chain */
3476 vm_object_lock(backing_object
);
3477 if (object
!= original_object
) {
3478 vm_object_unlock(object
);
3480 object
= backing_object
;
3484 vm_object_lock(backing_object
);
3488 * The backing object is not read_only,
3489 * and no pages in the backing object are
3490 * currently being paged out.
3491 * The backing object is internal.
3495 if (!backing_object
->internal
||
3496 backing_object
->paging_in_progress
!= 0) {
3497 /* try and collapse the rest of the shadow chain */
3498 if (object
!= original_object
) {
3499 vm_object_unlock(object
);
3501 object
= backing_object
;
3506 * The backing object can't be a copy-object:
3507 * the shadow_offset for the copy-object must stay
3508 * as 0. Furthermore (for the 'we have all the
3509 * pages' case), if we bypass backing_object and
3510 * just shadow the next object in the chain, old
3511 * pages from that object would then have to be copied
3512 * BOTH into the (former) backing_object and into the
3515 if (backing_object
->shadow
!= VM_OBJECT_NULL
&&
3516 backing_object
->shadow
->copy
== backing_object
) {
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 * We can now try to either collapse the backing
3527 * object (if the parent is the only reference to
3528 * it) or (perhaps) remove the parent's reference
3531 * If there is exactly one reference to the backing
3532 * object, we may be able to collapse it into the
3535 * If MACH_PAGEMAP is defined:
3536 * The parent must not have a pager created for it,
3537 * since collapsing a backing_object dumps new pages
3538 * into the parent that its pager doesn't know about
3539 * (and the collapse code can't merge the existence
3542 * As long as one of the objects is still not known
3543 * to the pager, we can collapse them.
3545 if (backing_object
->ref_count
== 1 &&
3546 (!object
->pager_created
3548 || !backing_object
->pager_created
3549 #endif /*!MACH_PAGEMAP */
3550 ) && vm_object_collapse_allowed
) {
3553 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
3554 (integer_t
)backing_object
, (integer_t
)object
,
3555 (integer_t
)backing_object
->pager
,
3556 (integer_t
)backing_object
->pager_control
, 0);
3559 * We need the cache lock for collapsing,
3560 * but we must not deadlock.
3563 if (! vm_object_cache_lock_try()) {
3564 if (object
!= original_object
) {
3565 vm_object_unlock(object
);
3567 vm_object_unlock(backing_object
);
3573 * We can't collapse the object if it contains
3574 * any encypted page, because the encryption key
3575 * includes the <object,offset> info. We can't
3576 * drop the object lock in vm_object_do_collapse()
3577 * so we can't decrypt the page there either.
3579 if (vm_pages_encrypted
) {
3580 collapse_min_offset
= object
->shadow_offset
;
3581 collapse_max_offset
=
3582 object
->shadow_offset
+ object
->size
;
3583 queue_iterate(&backing_object
->memq
,
3584 page
, vm_page_t
, listq
) {
3585 if (page
->encrypted
&&
3587 collapse_min_offset
) &&
3589 collapse_max_offset
)) {
3591 * We found an encrypted page
3592 * in the backing object,
3593 * within the range covered
3594 * by the parent object: we can
3595 * not collapse them.
3597 vm_object_collapse_encrypted
++;
3598 vm_object_cache_unlock();
3605 * Collapse the object with its backing
3606 * object, and try again with the object's
3607 * new backing object.
3610 vm_object_do_collapse(object
, backing_object
);
3611 vm_object_collapse_do_collapse
++;
3617 * Collapsing the backing object was not possible
3618 * or permitted, so let's try bypassing it.
3621 if (! vm_object_bypass_allowed
) {
3622 /* try and collapse the rest of the shadow chain */
3623 if (object
!= original_object
) {
3624 vm_object_unlock(object
);
3626 object
= backing_object
;
3632 * If the object doesn't have all its pages present,
3633 * we have to make sure no pages in the backing object
3634 * "show through" before bypassing it.
3636 size
= atop(object
->size
);
3637 rcount
= object
->resident_page_count
;
3638 if (rcount
!= size
) {
3639 vm_object_offset_t offset
;
3640 vm_object_offset_t backing_offset
;
3641 unsigned int backing_rcount
;
3642 unsigned int lookups
= 0;
3645 * If the backing object has a pager but no pagemap,
3646 * then we cannot bypass it, because we don't know
3647 * what pages it has.
3649 if (backing_object
->pager_created
3651 && (backing_object
->existence_map
== VM_EXTERNAL_NULL
)
3652 #endif /* MACH_PAGEMAP */
3654 /* try and collapse the rest of the shadow chain */
3655 if (object
!= original_object
) {
3656 vm_object_unlock(object
);
3658 object
= backing_object
;
3663 * If the object has a pager but no pagemap,
3664 * then we cannot bypass it, because we don't know
3665 * what pages it has.
3667 if (object
->pager_created
3669 && (object
->existence_map
== VM_EXTERNAL_NULL
)
3670 #endif /* MACH_PAGEMAP */
3672 /* try and collapse the rest of the shadow chain */
3673 if (object
!= original_object
) {
3674 vm_object_unlock(object
);
3676 object
= backing_object
;
3681 * If all of the pages in the backing object are
3682 * shadowed by the parent object, the parent
3683 * object no longer has to shadow the backing
3684 * object; it can shadow the next one in the
3687 * If the backing object has existence info,
3688 * we must check examine its existence info
3693 backing_offset
= object
->shadow_offset
;
3694 backing_rcount
= backing_object
->resident_page_count
;
3696 #define EXISTS_IN_OBJECT(obj, off, rc) \
3697 (vm_external_state_get((obj)->existence_map, \
3698 (vm_offset_t)(off)) == VM_EXTERNAL_STATE_EXISTS || \
3699 ((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
3702 * Check the hint location first
3703 * (since it is often the quickest way out of here).
3705 if (object
->cow_hint
!= ~(vm_offset_t
)0)
3706 hint_offset
= (vm_object_offset_t
)object
->cow_hint
;
3708 hint_offset
= (hint_offset
> 8 * PAGE_SIZE_64
) ?
3709 (hint_offset
- 8 * PAGE_SIZE_64
) : 0;
3711 if (EXISTS_IN_OBJECT(backing_object
, hint_offset
+
3712 backing_offset
, backing_rcount
) &&
3713 !EXISTS_IN_OBJECT(object
, hint_offset
, rcount
)) {
3714 /* dependency right at the hint */
3715 object
->cow_hint
= (vm_offset_t
)hint_offset
;
3716 /* try and collapse the rest of the shadow chain */
3717 if (object
!= original_object
) {
3718 vm_object_unlock(object
);
3720 object
= backing_object
;
3725 * If the object's window onto the backing_object
3726 * is large compared to the number of resident
3727 * pages in the backing object, it makes sense to
3728 * walk the backing_object's resident pages first.
3730 * NOTE: Pages may be in both the existence map and
3731 * resident. So, we can't permanently decrement
3732 * the rcount here because the second loop may
3733 * find the same pages in the backing object'
3734 * existence map that we found here and we would
3735 * double-decrement the rcount. We also may or
3736 * may not have found the
3738 if (backing_rcount
&& size
>
3739 ((backing_object
->existence_map
) ?
3740 backing_rcount
: (backing_rcount
>> 1))) {
3741 unsigned int rc
= rcount
;
3744 backing_rcount
= backing_object
->resident_page_count
;
3745 p
= (vm_page_t
)queue_first(&backing_object
->memq
);
3747 /* Until we get more than one lookup lock */
3748 if (lookups
> 256) {
3753 offset
= (p
->offset
- backing_offset
);
3754 if (offset
< object
->size
&&
3755 offset
!= hint_offset
&&
3756 !EXISTS_IN_OBJECT(object
, offset
, rc
)) {
3757 /* found a dependency */
3758 object
->cow_hint
= (vm_offset_t
)offset
;
3761 p
= (vm_page_t
) queue_next(&p
->listq
);
3763 } while (--backing_rcount
);
3764 if (backing_rcount
!= 0 ) {
3765 /* try and collapse the rest of the shadow chain */
3766 if (object
!= original_object
) {
3767 vm_object_unlock(object
);
3769 object
= backing_object
;
3775 * Walk through the offsets looking for pages in the
3776 * backing object that show through to the object.
3778 if (backing_rcount
|| backing_object
->existence_map
) {
3779 offset
= hint_offset
;
3782 (offset
+ PAGE_SIZE_64
< object
->size
) ?
3783 (offset
+ PAGE_SIZE_64
) : 0) != hint_offset
) {
3785 /* Until we get more than one lookup lock */
3786 if (lookups
> 256) {
3791 if (EXISTS_IN_OBJECT(backing_object
, offset
+
3792 backing_offset
, backing_rcount
) &&
3793 !EXISTS_IN_OBJECT(object
, offset
, rcount
)) {
3794 /* found a dependency */
3795 object
->cow_hint
= (vm_offset_t
)offset
;
3799 if (offset
!= hint_offset
) {
3800 /* try and collapse the rest of the shadow chain */
3801 if (object
!= original_object
) {
3802 vm_object_unlock(object
);
3804 object
= backing_object
;
3810 /* reset the offset hint for any objects deeper in the chain */
3811 object
->cow_hint
= (vm_offset_t
)0;
3814 * All interesting pages in the backing object
3815 * already live in the parent or its pager.
3816 * Thus we can bypass the backing object.
3819 vm_object_do_bypass(object
, backing_object
);
3820 vm_object_collapse_do_bypass
++;
3823 * Try again with this object's new backing object.
3829 if (object
!= original_object
) {
3830 vm_object_unlock(object
);
3835 * Routine: vm_object_page_remove: [internal]
3837 * Removes all physical pages in the specified
3838 * object range from the object's list of pages.
3840 * In/out conditions:
3841 * The object must be locked.
3842 * The object must not have paging_in_progress, usually
3843 * guaranteed by not having a pager.
3845 unsigned int vm_object_page_remove_lookup
= 0;
3846 unsigned int vm_object_page_remove_iterate
= 0;
3848 __private_extern__
void
3849 vm_object_page_remove(
3850 register vm_object_t object
,
3851 register vm_object_offset_t start
,
3852 register vm_object_offset_t end
)
3854 register vm_page_t p
, next
;
3857 * One and two page removals are most popular.
3858 * The factor of 16 here is somewhat arbitrary.
3859 * It balances vm_object_lookup vs iteration.
3862 if (atop_64(end
- start
) < (unsigned)object
->resident_page_count
/16) {
3863 vm_object_page_remove_lookup
++;
3865 for (; start
< end
; start
+= PAGE_SIZE_64
) {
3866 p
= vm_page_lookup(object
, start
);
3867 if (p
!= VM_PAGE_NULL
) {
3868 assert(!p
->cleaning
&& !p
->pageout
);
3870 pmap_disconnect(p
->phys_page
);
3875 vm_object_page_remove_iterate
++;
3877 p
= (vm_page_t
) queue_first(&object
->memq
);
3878 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
3879 next
= (vm_page_t
) queue_next(&p
->listq
);
3880 if ((start
<= p
->offset
) && (p
->offset
< end
)) {
3881 assert(!p
->cleaning
&& !p
->pageout
);
3883 pmap_disconnect(p
->phys_page
);
3893 * Routine: vm_object_coalesce
3894 * Function: Coalesces two objects backing up adjoining
3895 * regions of memory into a single object.
3897 * returns TRUE if objects were combined.
3899 * NOTE: Only works at the moment if the second object is NULL -
3900 * if it's not, which object do we lock first?
3903 * prev_object First object to coalesce
3904 * prev_offset Offset into prev_object
3905 * next_object Second object into coalesce
3906 * next_offset Offset into next_object
3908 * prev_size Size of reference to prev_object
3909 * next_size Size of reference to next_object
3912 * The object(s) must *not* be locked. The map must be locked
3913 * to preserve the reference to the object(s).
3915 static int vm_object_coalesce_count
= 0;
3917 __private_extern__ boolean_t
3919 register vm_object_t prev_object
,
3920 vm_object_t next_object
,
3921 vm_object_offset_t prev_offset
,
3922 __unused vm_object_offset_t next_offset
,
3923 vm_object_size_t prev_size
,
3924 vm_object_size_t next_size
)
3926 vm_object_size_t newsize
;
3932 if (next_object
!= VM_OBJECT_NULL
) {
3936 if (prev_object
== VM_OBJECT_NULL
) {
3941 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
3942 (integer_t
)prev_object
, prev_offset
, prev_size
, next_size
, 0);
3944 vm_object_lock(prev_object
);
3947 * Try to collapse the object first
3949 vm_object_collapse(prev_object
, prev_offset
);
3952 * Can't coalesce if pages not mapped to
3953 * prev_entry may be in use any way:
3954 * . more than one reference
3956 * . shadows another object
3957 * . has a copy elsewhere
3959 * . paging references (pages might be in page-list)
3962 if ((prev_object
->ref_count
> 1) ||
3963 prev_object
->pager_created
||
3964 (prev_object
->shadow
!= VM_OBJECT_NULL
) ||
3965 (prev_object
->copy
!= VM_OBJECT_NULL
) ||
3966 (prev_object
->true_share
!= FALSE
) ||
3967 (prev_object
->purgable
!= VM_OBJECT_NONPURGABLE
) ||
3968 (prev_object
->paging_in_progress
!= 0)) {
3969 vm_object_unlock(prev_object
);
3973 vm_object_coalesce_count
++;
3976 * Remove any pages that may still be in the object from
3977 * a previous deallocation.
3979 vm_object_page_remove(prev_object
,
3980 prev_offset
+ prev_size
,
3981 prev_offset
+ prev_size
+ next_size
);
3984 * Extend the object if necessary.
3986 newsize
= prev_offset
+ prev_size
+ next_size
;
3987 if (newsize
> prev_object
->size
) {
3990 * We cannot extend an object that has existence info,
3991 * since the existence info might then fail to cover
3992 * the entire object.
3994 * This assertion must be true because the object
3995 * has no pager, and we only create existence info
3996 * for objects with pagers.
3998 assert(prev_object
->existence_map
== VM_EXTERNAL_NULL
);
3999 #endif /* MACH_PAGEMAP */
4000 prev_object
->size
= newsize
;
4003 vm_object_unlock(prev_object
);
4008 * Attach a set of physical pages to an object, so that they can
4009 * be mapped by mapping the object. Typically used to map IO memory.
4011 * The mapping function and its private data are used to obtain the
4012 * physical addresses for each page to be mapped.
4017 vm_object_offset_t offset
,
4018 vm_object_size_t size
,
4019 vm_object_offset_t (*map_fn
)(void *map_fn_data
,
4020 vm_object_offset_t offset
),
4021 void *map_fn_data
) /* private to map_fn */
4027 vm_object_offset_t addr
;
4029 num_pages
= atop_64(size
);
4031 for (i
= 0; i
< num_pages
; i
++, offset
+= PAGE_SIZE_64
) {
4033 addr
= (*map_fn
)(map_fn_data
, offset
);
4035 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
4036 vm_page_more_fictitious();
4038 vm_object_lock(object
);
4039 if ((old_page
= vm_page_lookup(object
, offset
))
4042 vm_page_lock_queues();
4043 vm_page_free(old_page
);
4044 vm_page_unlock_queues();
4047 vm_page_init(m
, addr
);
4048 /* private normally requires lock_queues but since we */
4049 /* are initializing the page, its not necessary here */
4050 m
->private = TRUE
; /* don`t free page */
4052 vm_page_insert(m
, object
, offset
);
4054 PAGE_WAKEUP_DONE(m
);
4055 vm_object_unlock(object
);
4059 #include <mach_kdb.h>
4062 #include <ddb/db_output.h>
4063 #include <vm/vm_print.h>
4065 #define printf kdbprintf
4067 extern boolean_t
vm_object_cached(
4068 vm_object_t object
);
4070 extern void print_bitstring(
4073 boolean_t vm_object_print_pages
= FALSE
;
4079 printf("%c%c%c%c%c%c%c%c",
4080 ((byte
& (1 << 0)) ? '1' : '0'),
4081 ((byte
& (1 << 1)) ? '1' : '0'),
4082 ((byte
& (1 << 2)) ? '1' : '0'),
4083 ((byte
& (1 << 3)) ? '1' : '0'),
4084 ((byte
& (1 << 4)) ? '1' : '0'),
4085 ((byte
& (1 << 5)) ? '1' : '0'),
4086 ((byte
& (1 << 6)) ? '1' : '0'),
4087 ((byte
& (1 << 7)) ? '1' : '0'));
4092 register vm_object_t object
)
4094 register vm_object_t o
;
4096 queue_iterate(&vm_object_cached_list
, o
, vm_object_t
, cached_list
) {
4106 * vm_external_print: [ debug ]
4110 vm_external_map_t emap
,
4113 if (emap
== VM_EXTERNAL_NULL
) {
4116 vm_size_t existence_size
= stob(size
);
4117 printf("{ size=%d, map=[", existence_size
);
4118 if (existence_size
> 0) {
4119 print_bitstring(emap
[0]);
4121 if (existence_size
> 1) {
4122 print_bitstring(emap
[1]);
4124 if (existence_size
> 2) {
4126 print_bitstring(emap
[existence_size
-1]);
4132 #endif /* MACH_PAGEMAP */
4139 int orig_db_indent
= db_indent
;
4142 if (object
== VM_OBJECT_NULL
) {
4143 db_indent
= orig_db_indent
;
4149 iprintf("object 0x%x", object
);
4150 printf(", shadow=0x%x", object
->shadow
);
4151 printf(", copy=0x%x", object
->copy
);
4152 printf(", pager=0x%x", object
->pager
);
4153 printf(", ref=%d\n", object
->ref_count
);
4156 object
= object
->shadow
;
4162 * vm_object_print: [ debug ]
4167 __unused boolean_t have_addr
,
4168 __unused
int arg_count
,
4169 __unused
char *modif
)
4172 register vm_page_t p
;
4177 object
= (vm_object_t
) (long) db_addr
;
4178 if (object
== VM_OBJECT_NULL
)
4181 iprintf("object 0x%x\n", object
);
4185 iprintf("size=0x%x", object
->size
);
4186 printf(", cluster=0x%x", object
->cluster_size
);
4187 printf(", memq_hint=%p", object
->memq_hint
);
4188 printf(", ref_count=%d\n", object
->ref_count
);
4191 printf("res_count=%d, ", object
->res_count
);
4192 #endif /* TASK_SWAPPER */
4193 printf("resident_page_count=%d\n", object
->resident_page_count
);
4195 iprintf("shadow=0x%x", object
->shadow
);
4196 if (object
->shadow
) {
4198 vm_object_t shadow
= object
;
4199 while((shadow
= shadow
->shadow
))
4201 printf(" (depth %d)", i
);
4203 printf(", copy=0x%x", object
->copy
);
4204 printf(", shadow_offset=0x%x", object
->shadow_offset
);
4205 printf(", last_alloc=0x%x\n", object
->last_alloc
);
4207 iprintf("pager=0x%x", object
->pager
);
4208 printf(", paging_offset=0x%x", object
->paging_offset
);
4209 printf(", pager_control=0x%x\n", object
->pager_control
);
4211 iprintf("copy_strategy=%d[", object
->copy_strategy
);
4212 switch (object
->copy_strategy
) {
4213 case MEMORY_OBJECT_COPY_NONE
:
4214 printf("copy_none");
4217 case MEMORY_OBJECT_COPY_CALL
:
4218 printf("copy_call");
4221 case MEMORY_OBJECT_COPY_DELAY
:
4222 printf("copy_delay");
4225 case MEMORY_OBJECT_COPY_SYMMETRIC
:
4226 printf("copy_symmetric");
4229 case MEMORY_OBJECT_COPY_INVALID
:
4230 printf("copy_invalid");
4237 printf(", absent_count=%d\n", object
->absent_count
);
4239 iprintf("all_wanted=0x%x<", object
->all_wanted
);
4241 if (vm_object_wanted(object
, VM_OBJECT_EVENT_INITIALIZED
)) {
4242 printf("%sinit", s
);
4245 if (vm_object_wanted(object
, VM_OBJECT_EVENT_PAGER_READY
)) {
4246 printf("%sready", s
);
4249 if (vm_object_wanted(object
, VM_OBJECT_EVENT_PAGING_IN_PROGRESS
)) {
4250 printf("%spaging", s
);
4253 if (vm_object_wanted(object
, VM_OBJECT_EVENT_ABSENT_COUNT
)) {
4254 printf("%sabsent", s
);
4257 if (vm_object_wanted(object
, VM_OBJECT_EVENT_LOCK_IN_PROGRESS
)) {
4258 printf("%slock", s
);
4261 if (vm_object_wanted(object
, VM_OBJECT_EVENT_UNCACHING
)) {
4262 printf("%suncaching", s
);
4265 if (vm_object_wanted(object
, VM_OBJECT_EVENT_COPY_CALL
)) {
4266 printf("%scopy_call", s
);
4269 if (vm_object_wanted(object
, VM_OBJECT_EVENT_CACHING
)) {
4270 printf("%scaching", s
);
4274 printf(", paging_in_progress=%d\n", object
->paging_in_progress
);
4276 iprintf("%screated, %sinit, %sready, %spersist, %strusted, %spageout, %s, %s\n",
4277 (object
->pager_created
? "" : "!"),
4278 (object
->pager_initialized
? "" : "!"),
4279 (object
->pager_ready
? "" : "!"),
4280 (object
->can_persist
? "" : "!"),
4281 (object
->pager_trusted
? "" : "!"),
4282 (object
->pageout
? "" : "!"),
4283 (object
->internal
? "internal" : "external"),
4284 (object
->temporary
? "temporary" : "permanent"));
4285 iprintf("%salive, %spurgable, %spurgable_volatile, %spurgable_empty, %sshadowed, %scached, %sprivate\n",
4286 (object
->alive
? "" : "!"),
4287 ((object
->purgable
!= VM_OBJECT_NONPURGABLE
) ? "" : "!"),
4288 ((object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
) ? "" : "!"),
4289 ((object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) ? "" : "!"),
4290 (object
->shadowed
? "" : "!"),
4291 (vm_object_cached(object
) ? "" : "!"),
4292 (object
->private ? "" : "!"));
4293 iprintf("%sadvisory_pageout, %ssilent_overwrite\n",
4294 (object
->advisory_pageout
? "" : "!"),
4295 (object
->silent_overwrite
? "" : "!"));
4298 iprintf("existence_map=");
4299 vm_external_print(object
->existence_map
, object
->size
);
4300 #endif /* MACH_PAGEMAP */
4302 iprintf("paging_object=0x%x\n", object
->paging_object
);
4303 #endif /* MACH_ASSERT */
4305 if (vm_object_print_pages
) {
4307 p
= (vm_page_t
) queue_first(&object
->memq
);
4308 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
4310 iprintf("memory:=");
4311 } else if (count
== 2) {
4320 printf("(off=0x%llX,page=%p)", p
->offset
, p
);
4321 p
= (vm_page_t
) queue_next(&p
->listq
);
4332 * vm_object_find [ debug ]
4334 * Find all tasks which reference the given vm_object.
4337 boolean_t
vm_object_find(vm_object_t object
);
4338 boolean_t vm_object_print_verbose
= FALSE
;
4346 vm_map_entry_t entry
;
4347 processor_set_t pset
= &default_pset
;
4348 boolean_t found
= FALSE
;
4350 queue_iterate(&pset
->tasks
, task
, task_t
, pset_tasks
) {
4352 for (entry
= vm_map_first_entry(map
);
4353 entry
&& entry
!= vm_map_to_entry(map
);
4354 entry
= entry
->vme_next
) {
4359 * For the time being skip submaps,
4360 * only the kernel can have submaps,
4361 * and unless we are interested in
4362 * kernel objects, we can simply skip
4363 * submaps. See sb/dejan/nmk18b7/src/mach_kernel/vm
4364 * for a full solution.
4366 if (entry
->is_sub_map
)
4369 obj
= entry
->object
.vm_object
;
4373 while (obj
!= VM_OBJECT_NULL
) {
4374 if (obj
== object
) {
4376 printf("TASK\t\tMAP\t\tENTRY\n");
4379 printf("0x%x\t0x%x\t0x%x\n",
4390 #endif /* MACH_KDB */
4393 vm_object_populate_with_private(
4395 vm_object_offset_t offset
,
4400 vm_object_offset_t base_offset
;
4403 if(!object
->private)
4404 return KERN_FAILURE
;
4406 base_page
= phys_page
;
4408 vm_object_lock(object
);
4409 if(!object
->phys_contiguous
) {
4411 if((base_offset
= trunc_page_64(offset
)) != offset
) {
4412 vm_object_unlock(object
);
4413 return KERN_FAILURE
;
4415 base_offset
+= object
->paging_offset
;
4417 m
= vm_page_lookup(object
, base_offset
);
4418 if(m
!= VM_PAGE_NULL
) {
4420 vm_page_lock_queues();
4421 m
->fictitious
= FALSE
;
4423 m
->phys_page
= base_page
;
4429 object
->absent_count
++;
4431 m
->list_req_pending
= TRUE
;
4432 vm_page_unlock_queues();
4433 } else if (m
->phys_page
!= base_page
) {
4434 /* pmap call to clear old mapping */
4435 pmap_disconnect(m
->phys_page
);
4436 m
->phys_page
= base_page
;
4441 * We're not pointing to the same
4442 * physical page any longer and the
4443 * contents of the new one are not
4444 * supposed to be encrypted.
4445 * XXX What happens to the original
4446 * physical page. Is it lost ?
4448 m
->encrypted
= FALSE
;
4451 while ((m
= vm_page_grab_fictitious())
4453 vm_page_more_fictitious();
4454 vm_page_lock_queues();
4455 m
->fictitious
= FALSE
;
4457 m
->phys_page
= base_page
;
4458 m
->list_req_pending
= TRUE
;
4461 object
->absent_count
++;
4462 vm_page_unlock_queues();
4463 vm_page_insert(m
, object
, base_offset
);
4465 base_page
++; /* Go to the next physical page */
4466 base_offset
+= PAGE_SIZE
;
4470 /* NOTE: we should check the original settings here */
4471 /* if we have a size > zero a pmap call should be made */
4472 /* to disable the range */
4476 /* shadows on contiguous memory are not allowed */
4477 /* we therefore can use the offset field */
4478 object
->shadow_offset
= (vm_object_offset_t
)(phys_page
<< 12);
4479 object
->size
= size
;
4481 vm_object_unlock(object
);
4482 return KERN_SUCCESS
;
4486 * memory_object_free_from_cache:
4488 * Walk the vm_object cache list, removing and freeing vm_objects
4489 * which are backed by the pager identified by the caller, (pager_id).
4490 * Remove up to "count" objects, if there are that may available
4493 * Walk the list at most once, return the number of vm_objects
4497 __private_extern__ kern_return_t
4498 memory_object_free_from_cache(
4499 __unused host_t host
,
4504 int object_released
= 0;
4506 register vm_object_t object
= VM_OBJECT_NULL
;
4510 if(host == HOST_NULL)
4511 return(KERN_INVALID_ARGUMENT);
4515 vm_object_cache_lock();
4517 queue_iterate(&vm_object_cached_list
, object
,
4518 vm_object_t
, cached_list
) {
4519 if (object
->pager
&& (pager_id
== object
->pager
->pager
)) {
4520 vm_object_lock(object
);
4521 queue_remove(&vm_object_cached_list
, object
,
4522 vm_object_t
, cached_list
);
4523 vm_object_cached_count
--;
4526 * Since this object is in the cache, we know
4527 * that it is initialized and has only a pager's
4528 * (implicit) reference. Take a reference to avoid
4529 * recursive deallocations.
4532 assert(object
->pager_initialized
);
4533 assert(object
->ref_count
== 0);
4534 object
->ref_count
++;
4537 * Terminate the object.
4538 * If the object had a shadow, we let
4539 * vm_object_deallocate deallocate it.
4540 * "pageout" objects have a shadow, but
4541 * maintain a "paging reference" rather
4542 * than a normal reference.
4543 * (We are careful here to limit recursion.)
4545 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
4546 if ((vm_object_terminate(object
) == KERN_SUCCESS
)
4547 && (shadow
!= VM_OBJECT_NULL
)) {
4548 vm_object_deallocate(shadow
);
4551 if(object_released
++ == *count
)
4552 return KERN_SUCCESS
;
4556 vm_object_cache_unlock();
4557 *count
= object_released
;
4558 return KERN_SUCCESS
;
4564 memory_object_create_named(
4565 memory_object_t pager
,
4566 memory_object_offset_t size
,
4567 memory_object_control_t
*control
)
4570 vm_object_hash_entry_t entry
;
4572 *control
= MEMORY_OBJECT_CONTROL_NULL
;
4573 if (pager
== MEMORY_OBJECT_NULL
)
4574 return KERN_INVALID_ARGUMENT
;
4576 vm_object_cache_lock();
4577 entry
= vm_object_hash_lookup(pager
, FALSE
);
4578 if ((entry
!= VM_OBJECT_HASH_ENTRY_NULL
) &&
4579 (entry
->object
!= VM_OBJECT_NULL
)) {
4580 if (entry
->object
->named
== TRUE
)
4581 panic("memory_object_create_named: caller already holds the right"); }
4583 vm_object_cache_unlock();
4584 if ((object
= vm_object_enter(pager
, size
, FALSE
, FALSE
, TRUE
))
4585 == VM_OBJECT_NULL
) {
4586 return(KERN_INVALID_OBJECT
);
4589 /* wait for object (if any) to be ready */
4590 if (object
!= VM_OBJECT_NULL
) {
4591 vm_object_lock(object
);
4592 object
->named
= TRUE
;
4593 while (!object
->pager_ready
) {
4594 vm_object_sleep(object
,
4595 VM_OBJECT_EVENT_PAGER_READY
,
4598 *control
= object
->pager_control
;
4599 vm_object_unlock(object
);
4601 return (KERN_SUCCESS
);
4606 * Routine: memory_object_recover_named [user interface]
4608 * Attempt to recover a named reference for a VM object.
4609 * VM will verify that the object has not already started
4610 * down the termination path, and if it has, will optionally
4611 * wait for that to finish.
4613 * KERN_SUCCESS - we recovered a named reference on the object
4614 * KERN_FAILURE - we could not recover a reference (object dead)
4615 * KERN_INVALID_ARGUMENT - bad memory object control
4618 memory_object_recover_named(
4619 memory_object_control_t control
,
4620 boolean_t wait_on_terminating
)
4624 vm_object_cache_lock();
4625 object
= memory_object_control_to_vm_object(control
);
4626 if (object
== VM_OBJECT_NULL
) {
4627 vm_object_cache_unlock();
4628 return (KERN_INVALID_ARGUMENT
);
4632 vm_object_lock(object
);
4634 if (object
->terminating
&& wait_on_terminating
) {
4635 vm_object_cache_unlock();
4636 vm_object_wait(object
,
4637 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
4639 vm_object_cache_lock();
4643 if (!object
->alive
) {
4644 vm_object_cache_unlock();
4645 vm_object_unlock(object
);
4646 return KERN_FAILURE
;
4649 if (object
->named
== TRUE
) {
4650 vm_object_cache_unlock();
4651 vm_object_unlock(object
);
4652 return KERN_SUCCESS
;
4655 if((object
->ref_count
== 0) && (!object
->terminating
)){
4656 queue_remove(&vm_object_cached_list
, object
,
4657 vm_object_t
, cached_list
);
4658 vm_object_cached_count
--;
4659 XPR(XPR_VM_OBJECT_CACHE
,
4660 "memory_object_recover_named: removing %X, head (%X, %X)\n",
4662 (integer_t
)vm_object_cached_list
.next
,
4663 (integer_t
)vm_object_cached_list
.prev
, 0,0);
4666 vm_object_cache_unlock();
4668 object
->named
= TRUE
;
4669 object
->ref_count
++;
4670 vm_object_res_reference(object
);
4671 while (!object
->pager_ready
) {
4672 vm_object_sleep(object
,
4673 VM_OBJECT_EVENT_PAGER_READY
,
4676 vm_object_unlock(object
);
4677 return (KERN_SUCCESS
);
4682 * vm_object_release_name:
4684 * Enforces name semantic on memory_object reference count decrement
4685 * This routine should not be called unless the caller holds a name
4686 * reference gained through the memory_object_create_named.
4688 * If the TERMINATE_IDLE flag is set, the call will return if the
4689 * reference count is not 1. i.e. idle with the only remaining reference
4691 * If the decision is made to proceed the name field flag is set to
4692 * false and the reference count is decremented. If the RESPECT_CACHE
4693 * flag is set and the reference count has gone to zero, the
4694 * memory_object is checked to see if it is cacheable otherwise when
4695 * the reference count is zero, it is simply terminated.
4698 __private_extern__ kern_return_t
4699 vm_object_release_name(
4704 boolean_t original_object
= TRUE
;
4706 while (object
!= VM_OBJECT_NULL
) {
4709 * The cache holds a reference (uncounted) to
4710 * the object. We must locke it before removing
4715 vm_object_cache_lock();
4716 vm_object_lock(object
);
4717 assert(object
->alive
);
4719 assert(object
->named
);
4720 assert(object
->ref_count
> 0);
4723 * We have to wait for initialization before
4724 * destroying or caching the object.
4727 if (object
->pager_created
&& !object
->pager_initialized
) {
4728 assert(!object
->can_persist
);
4729 vm_object_assert_wait(object
,
4730 VM_OBJECT_EVENT_INITIALIZED
,
4732 vm_object_unlock(object
);
4733 vm_object_cache_unlock();
4734 thread_block(THREAD_CONTINUE_NULL
);
4738 if (((object
->ref_count
> 1)
4739 && (flags
& MEMORY_OBJECT_TERMINATE_IDLE
))
4740 || (object
->terminating
)) {
4741 vm_object_unlock(object
);
4742 vm_object_cache_unlock();
4743 return KERN_FAILURE
;
4745 if (flags
& MEMORY_OBJECT_RELEASE_NO_OP
) {
4746 vm_object_unlock(object
);
4747 vm_object_cache_unlock();
4748 return KERN_SUCCESS
;
4752 if ((flags
& MEMORY_OBJECT_RESPECT_CACHE
) &&
4753 (object
->ref_count
== 1)) {
4755 object
->named
= FALSE
;
4756 vm_object_unlock(object
);
4757 vm_object_cache_unlock();
4758 /* let vm_object_deallocate push this thing into */
4759 /* the cache, if that it is where it is bound */
4760 vm_object_deallocate(object
);
4761 return KERN_SUCCESS
;
4763 VM_OBJ_RES_DECR(object
);
4764 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
4765 if(object
->ref_count
== 1) {
4766 if(vm_object_terminate(object
) != KERN_SUCCESS
) {
4767 if(original_object
) {
4768 return KERN_FAILURE
;
4770 return KERN_SUCCESS
;
4773 if (shadow
!= VM_OBJECT_NULL
) {
4774 original_object
= FALSE
;
4778 return KERN_SUCCESS
;
4780 object
->ref_count
--;
4781 assert(object
->ref_count
> 0);
4783 object
->named
= FALSE
;
4784 vm_object_unlock(object
);
4785 vm_object_cache_unlock();
4786 return KERN_SUCCESS
;
4791 return KERN_FAILURE
;
4795 __private_extern__ kern_return_t
4796 vm_object_lock_request(
4798 vm_object_offset_t offset
,
4799 vm_object_size_t size
,
4800 memory_object_return_t should_return
,
4804 __unused boolean_t should_flush
;
4806 should_flush
= flags
& MEMORY_OBJECT_DATA_FLUSH
;
4808 XPR(XPR_MEMORY_OBJECT
,
4809 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
4810 (integer_t
)object
, offset
, size
,
4811 (((should_return
&1)<<1)|should_flush
), prot
);
4814 * Check for bogus arguments.
4816 if (object
== VM_OBJECT_NULL
)
4817 return (KERN_INVALID_ARGUMENT
);
4819 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
)
4820 return (KERN_INVALID_ARGUMENT
);
4822 size
= round_page_64(size
);
4825 * Lock the object, and acquire a paging reference to
4826 * prevent the memory_object reference from being released.
4828 vm_object_lock(object
);
4829 vm_object_paging_begin(object
);
4831 (void)vm_object_update(object
,
4832 offset
, size
, NULL
, NULL
, should_return
, flags
, prot
);
4834 vm_object_paging_end(object
);
4835 vm_object_unlock(object
);
4837 return (KERN_SUCCESS
);
4841 * Empty a purgable object by grabbing the physical pages assigned to it and
4842 * putting them on the free queue without writing them to backing store, etc.
4843 * When the pages are next touched they will be demand zero-fill pages. We
4844 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
4845 * skip referenced/dirty pages, pages on the active queue, etc. We're more
4846 * than happy to grab these since this is a purgable object. We mark the
4847 * object as "empty" after reaping its pages.
4849 * On entry the object and page queues are locked, the object must be a
4850 * purgable object with no delayed copies pending.
4853 vm_object_purge(vm_object_t object
)
4856 unsigned int num_purged_pages
;
4857 vm_page_t local_freeq
;
4858 unsigned long local_freed
;
4859 int purge_loop_quota
;
4860 /* free pages as soon as we gather PURGE_BATCH_FREE_LIMIT pages to free */
4861 #define PURGE_BATCH_FREE_LIMIT 50
4862 /* release page queues lock every PURGE_LOOP_QUOTA iterations */
4863 #define PURGE_LOOP_QUOTA 100
4865 num_purged_pages
= 0;
4866 if (object
->purgable
== VM_OBJECT_NONPURGABLE
)
4867 return num_purged_pages
;
4869 object
->purgable
= VM_OBJECT_PURGABLE_EMPTY
;
4871 assert(object
->copy
== VM_OBJECT_NULL
);
4872 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
4873 purge_loop_quota
= PURGE_LOOP_QUOTA
;
4875 local_freeq
= VM_PAGE_NULL
;
4879 * Go through the object's resident pages and try and discard them.
4881 next
= (vm_page_t
)queue_first(&object
->memq
);
4882 while (!queue_end(&object
->memq
, (queue_entry_t
)next
)) {
4884 next
= (vm_page_t
)queue_next(&next
->listq
);
4886 if (purge_loop_quota
-- == 0) {
4888 * Avoid holding the page queues lock for too long.
4889 * Let someone else take it for a while if needed.
4890 * Keep holding the object's lock to guarantee that
4891 * the object's page list doesn't change under us
4894 if (local_freeq
!= VM_PAGE_NULL
) {
4896 * Flush our queue of pages to free.
4898 vm_page_free_list(local_freeq
);
4899 local_freeq
= VM_PAGE_NULL
;
4902 vm_page_unlock_queues();
4904 vm_page_lock_queues();
4906 /* resume with the current page and a new quota */
4907 purge_loop_quota
= PURGE_LOOP_QUOTA
;
4911 if (p
->busy
|| p
->cleaning
|| p
->laundry
||
4912 p
->list_req_pending
) {
4913 /* page is being acted upon, so don't mess with it */
4916 if (p
->wire_count
) {
4917 /* don't discard a wired page */
4922 /* clean up the object/offset table */
4926 /* update the object's count of absent pages */
4927 vm_object_absent_release(object
);
4930 /* we can discard this page */
4932 /* advertize that this page is in a transition state */
4935 if (p
->no_isync
== TRUE
) {
4936 /* the page hasn't been mapped yet */
4937 /* (optimization to delay the i-cache sync) */
4939 /* unmap the page */
4942 refmod_state
= pmap_disconnect(p
->phys_page
);
4943 if (refmod_state
& VM_MEM_MODIFIED
) {
4948 if (p
->dirty
|| p
->precious
) {
4949 /* we saved the cost of cleaning this page ! */
4951 vm_page_purged_count
++;
4954 /* remove page from active or inactive queue... */
4955 VM_PAGE_QUEUES_REMOVE(p
);
4957 /* ... and put it on our queue of pages to free */
4958 assert(!p
->laundry
);
4959 assert(p
->object
!= kernel_object
);
4960 assert(p
->pageq
.next
== NULL
&&
4961 p
->pageq
.prev
== NULL
);
4962 p
->pageq
.next
= (queue_entry_t
) local_freeq
;
4964 if (++local_freed
>= PURGE_BATCH_FREE_LIMIT
) {
4965 /* flush our queue of pages to free */
4966 vm_page_free_list(local_freeq
);
4967 local_freeq
= VM_PAGE_NULL
;
4972 /* flush our local queue of pages to free one last time */
4973 if (local_freeq
!= VM_PAGE_NULL
) {
4974 vm_page_free_list(local_freeq
);
4975 local_freeq
= VM_PAGE_NULL
;
4979 return num_purged_pages
;
4983 * vm_object_purgable_control() allows the caller to control and investigate the
4984 * state of a purgable object. A purgable object is created via a call to
4985 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgable object will
4986 * never be coalesced with any other object -- even other purgable objects --
4987 * and will thus always remain a distinct object. A purgable object has
4988 * special semantics when its reference count is exactly 1. If its reference
4989 * count is greater than 1, then a purgable object will behave like a normal
4990 * object and attempts to use this interface will result in an error return
4991 * of KERN_INVALID_ARGUMENT.
4993 * A purgable object may be put into a "volatile" state which will make the
4994 * object's pages elligable for being reclaimed without paging to backing
4995 * store if the system runs low on memory. If the pages in a volatile
4996 * purgable object are reclaimed, the purgable object is said to have been
4997 * "emptied." When a purgable object is emptied the system will reclaim as
4998 * many pages from the object as it can in a convenient manner (pages already
4999 * en route to backing store or busy for other reasons are left as is). When
5000 * a purgable object is made volatile, its pages will generally be reclaimed
5001 * before other pages in the application's working set. This semantic is
5002 * generally used by applications which can recreate the data in the object
5003 * faster than it can be paged in. One such example might be media assets
5004 * which can be reread from a much faster RAID volume.
5006 * A purgable object may be designated as "non-volatile" which means it will
5007 * behave like all other objects in the system with pages being written to and
5008 * read from backing store as needed to satisfy system memory needs. If the
5009 * object was emptied before the object was made non-volatile, that fact will
5010 * be returned as the old state of the purgable object (see
5011 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
5012 * were reclaimed as part of emptying the object will be refaulted in as
5013 * zero-fill on demand. It is up to the application to note that an object
5014 * was emptied and recreate the objects contents if necessary. When a
5015 * purgable object is made non-volatile, its pages will generally not be paged
5016 * out to backing store in the immediate future. A purgable object may also
5017 * be manually emptied.
5019 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
5020 * volatile purgable object may be queried at any time. This information may
5021 * be used as a control input to let the application know when the system is
5022 * experiencing memory pressure and is reclaiming memory.
5024 * The specified address may be any address within the purgable object. If
5025 * the specified address does not represent any object in the target task's
5026 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
5027 * object containing the specified address is not a purgable object, then
5028 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
5031 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
5032 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
5033 * state is used to set the new state of the purgable object and return its
5034 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgable
5035 * object is returned in the parameter state.
5037 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
5038 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
5039 * the non-volatile, volatile and volatile/empty states described above.
5040 * Setting the state of a purgable object to VM_PURGABLE_EMPTY will
5041 * immediately reclaim as many pages in the object as can be conveniently
5042 * collected (some may have already been written to backing store or be
5045 * The process of making a purgable object non-volatile and determining its
5046 * previous state is atomic. Thus, if a purgable object is made
5047 * VM_PURGABLE_NONVOLATILE and the old state is returned as
5048 * VM_PURGABLE_VOLATILE, then the purgable object's previous contents are
5049 * completely intact and will remain so until the object is made volatile
5050 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
5051 * was reclaimed while it was in a volatile state and its previous contents
5055 * The object must be locked.
5058 vm_object_purgable_control(
5060 vm_purgable_t control
,
5066 if (object
== VM_OBJECT_NULL
) {
5068 * Object must already be present or it can't be purgable.
5070 return KERN_INVALID_ARGUMENT
;
5074 * Get current state of the purgable object.
5076 switch (object
->purgable
) {
5077 case VM_OBJECT_NONPURGABLE
:
5078 return KERN_INVALID_ARGUMENT
;
5080 case VM_OBJECT_PURGABLE_NONVOLATILE
:
5081 old_state
= VM_PURGABLE_NONVOLATILE
;
5084 case VM_OBJECT_PURGABLE_VOLATILE
:
5085 old_state
= VM_PURGABLE_VOLATILE
;
5088 case VM_OBJECT_PURGABLE_EMPTY
:
5089 old_state
= VM_PURGABLE_EMPTY
;
5093 old_state
= VM_PURGABLE_NONVOLATILE
;
5094 panic("Bad state (%d) for purgable object!\n",
5099 /* purgable cant have delayed copies - now or in the future */
5100 assert(object
->copy
== VM_OBJECT_NULL
);
5101 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
5104 * Execute the desired operation.
5106 if (control
== VM_PURGABLE_GET_STATE
) {
5108 return KERN_SUCCESS
;
5112 case VM_PURGABLE_NONVOLATILE
:
5113 vm_page_lock_queues();
5114 if (object
->purgable
!= VM_OBJECT_PURGABLE_NONVOLATILE
) {
5115 assert(vm_page_purgeable_count
>=
5116 object
->resident_page_count
);
5117 vm_page_purgeable_count
-= object
->resident_page_count
;
5120 object
->purgable
= VM_OBJECT_PURGABLE_NONVOLATILE
;
5123 * If the object wasn't emptied, then mark all pages of the
5124 * object as referenced in order to give them a complete turn
5125 * of the virtual memory "clock" before becoming candidates
5126 * for paging out (if the system is suffering from memory
5127 * pressure). We don't really need to set the pmap reference
5128 * bits (which would be expensive) since the software copies
5129 * are believed if they're set to true ...
5131 if (old_state
!= VM_PURGABLE_EMPTY
) {
5132 for (p
= (vm_page_t
)queue_first(&object
->memq
);
5133 !queue_end(&object
->memq
, (queue_entry_t
)p
);
5134 p
= (vm_page_t
)queue_next(&p
->listq
))
5135 p
->reference
= TRUE
;
5138 vm_page_unlock_queues();
5142 case VM_PURGABLE_VOLATILE
:
5143 vm_page_lock_queues();
5145 if (object
->purgable
!= VM_OBJECT_PURGABLE_VOLATILE
&&
5146 object
->purgable
!= VM_OBJECT_PURGABLE_EMPTY
) {
5147 vm_page_purgeable_count
+= object
->resident_page_count
;
5150 object
->purgable
= VM_OBJECT_PURGABLE_VOLATILE
;
5153 * We want the newly volatile purgable object to be a
5154 * candidate for the pageout scan before other pages in the
5155 * application if the system is suffering from memory
5156 * pressure. To do this, we move a page of the object from
5157 * the active queue onto the inactive queue in order to
5158 * promote the object for early reclaim. We only need to move
5159 * a single page since the pageout scan will reap the entire
5160 * purgable object if it finds a single page in a volatile
5161 * state. Obviously we don't do this if there are no pages
5162 * associated with the object or we find a page of the object
5163 * already on the inactive queue.
5165 for (p
= (vm_page_t
)queue_first(&object
->memq
);
5166 !queue_end(&object
->memq
, (queue_entry_t
)p
);
5167 p
= (vm_page_t
)queue_next(&p
->listq
)) {
5169 /* already a page on the inactive queue */
5172 if (p
->active
&& !p
->busy
) {
5173 /* found one we can move */
5174 vm_page_deactivate(p
);
5178 vm_page_unlock_queues();
5183 case VM_PURGABLE_EMPTY
:
5184 vm_page_lock_queues();
5185 if (object
->purgable
!= VM_OBJECT_PURGABLE_VOLATILE
&&
5186 object
->purgable
!= VM_OBJECT_PURGABLE_EMPTY
) {
5187 vm_page_purgeable_count
+= object
->resident_page_count
;
5189 (void) vm_object_purge(object
);
5190 vm_page_unlock_queues();
5196 return KERN_SUCCESS
;
5201 * vm_object_res_deallocate
5203 * (recursively) decrement residence counts on vm objects and their shadows.
5204 * Called from vm_object_deallocate and when swapping out an object.
5206 * The object is locked, and remains locked throughout the function,
5207 * even as we iterate down the shadow chain. Locks on intermediate objects
5208 * will be dropped, but not the original object.
5210 * NOTE: this function used to use recursion, rather than iteration.
5213 __private_extern__
void
5214 vm_object_res_deallocate(
5217 vm_object_t orig_object
= object
;
5219 * Object is locked so it can be called directly
5220 * from vm_object_deallocate. Original object is never
5223 assert(object
->res_count
> 0);
5224 while (--object
->res_count
== 0) {
5225 assert(object
->ref_count
>= object
->res_count
);
5226 vm_object_deactivate_all_pages(object
);
5227 /* iterate on shadow, if present */
5228 if (object
->shadow
!= VM_OBJECT_NULL
) {
5229 vm_object_t tmp_object
= object
->shadow
;
5230 vm_object_lock(tmp_object
);
5231 if (object
!= orig_object
)
5232 vm_object_unlock(object
);
5233 object
= tmp_object
;
5234 assert(object
->res_count
> 0);
5238 if (object
!= orig_object
)
5239 vm_object_unlock(object
);
5243 * vm_object_res_reference
5245 * Internal function to increment residence count on a vm object
5246 * and its shadows. It is called only from vm_object_reference, and
5247 * when swapping in a vm object, via vm_map_swap.
5249 * The object is locked, and remains locked throughout the function,
5250 * even as we iterate down the shadow chain. Locks on intermediate objects
5251 * will be dropped, but not the original object.
5253 * NOTE: this function used to use recursion, rather than iteration.
5256 __private_extern__
void
5257 vm_object_res_reference(
5260 vm_object_t orig_object
= object
;
5262 * Object is locked, so this can be called directly
5263 * from vm_object_reference. This lock is never released.
5265 while ((++object
->res_count
== 1) &&
5266 (object
->shadow
!= VM_OBJECT_NULL
)) {
5267 vm_object_t tmp_object
= object
->shadow
;
5269 assert(object
->ref_count
>= object
->res_count
);
5270 vm_object_lock(tmp_object
);
5271 if (object
!= orig_object
)
5272 vm_object_unlock(object
);
5273 object
= tmp_object
;
5275 if (object
!= orig_object
)
5276 vm_object_unlock(object
);
5277 assert(orig_object
->ref_count
>= orig_object
->res_count
);
5279 #endif /* TASK_SWAPPER */
5282 * vm_object_reference:
5284 * Gets another reference to the given object.
5286 #ifdef vm_object_reference
5287 #undef vm_object_reference
5289 __private_extern__
void
5290 vm_object_reference(
5291 register vm_object_t object
)
5293 if (object
== VM_OBJECT_NULL
)
5296 vm_object_lock(object
);
5297 assert(object
->ref_count
> 0);
5298 vm_object_reference_locked(object
);
5299 vm_object_unlock(object
);
5304 * Scale the vm_object_cache
5305 * This is required to make sure that the vm_object_cache is big
5306 * enough to effectively cache the mapped file.
5307 * This is really important with UBC as all the regular file vnodes
5308 * have memory object associated with them. Havving this cache too
5309 * small results in rapid reclaim of vnodes and hurts performance a LOT!
5311 * This is also needed as number of vnodes can be dynamically scaled.
5314 adjust_vm_object_cache(
5315 __unused vm_size_t oval
,
5318 vm_object_cached_max
= nval
;
5319 vm_object_cache_trim(FALSE
);
5320 return (KERN_SUCCESS
);
5322 #endif /* MACH_BSD */
5326 * vm_object_transpose
5328 * This routine takes two VM objects of the same size and exchanges
5329 * their backing store.
5330 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
5331 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
5333 * The VM objects must not be locked by caller.
5336 vm_object_transpose(
5337 vm_object_t object1
,
5338 vm_object_t object2
,
5339 vm_object_size_t transpose_size
)
5341 vm_object_t tmp_object
;
5342 kern_return_t retval
;
5343 boolean_t object1_locked
, object2_locked
;
5344 boolean_t object1_paging
, object2_paging
;
5346 vm_object_offset_t page_offset
;
5348 tmp_object
= VM_OBJECT_NULL
;
5349 object1_locked
= FALSE
; object2_locked
= FALSE
;
5350 object1_paging
= FALSE
; object2_paging
= FALSE
;
5352 if (object1
== object2
||
5353 object1
== VM_OBJECT_NULL
||
5354 object2
== VM_OBJECT_NULL
) {
5356 * If the 2 VM objects are the same, there's
5357 * no point in exchanging their backing store.
5359 retval
= KERN_INVALID_VALUE
;
5363 vm_object_lock(object1
);
5364 object1_locked
= TRUE
;
5365 if (object1
->copy
|| object1
->shadow
|| object1
->shadowed
||
5366 object1
->purgable
!= VM_OBJECT_NONPURGABLE
) {
5368 * We don't deal with copy or shadow objects (yet).
5370 retval
= KERN_INVALID_VALUE
;
5374 * Since we're about to mess with the object's backing store,
5375 * mark it as "paging_in_progress". Note that this is not enough
5376 * to prevent any paging activity on this object, so the caller should
5377 * have "quiesced" the objects beforehand, via a UPL operation with
5378 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
5379 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
5381 vm_object_paging_begin(object1
);
5382 object1_paging
= TRUE
;
5383 vm_object_unlock(object1
);
5384 object1_locked
= FALSE
;
5387 * Same as above for the 2nd object...
5389 vm_object_lock(object2
);
5390 object2_locked
= TRUE
;
5391 if (object2
->copy
|| object2
->shadow
|| object2
->shadowed
||
5392 object2
->purgable
!= VM_OBJECT_NONPURGABLE
) {
5393 retval
= KERN_INVALID_VALUE
;
5396 vm_object_paging_begin(object2
);
5397 object2_paging
= TRUE
;
5398 vm_object_unlock(object2
);
5399 object2_locked
= FALSE
;
5402 * Allocate a temporary VM object to hold object1's contents
5403 * while we copy object2 to object1.
5405 tmp_object
= vm_object_allocate(transpose_size
);
5406 vm_object_lock(tmp_object
);
5407 vm_object_paging_begin(tmp_object
);
5408 tmp_object
->can_persist
= FALSE
;
5411 * Since we need to lock both objects at the same time,
5412 * make sure we always lock them in the same order to
5415 if (object1
< object2
) {
5416 vm_object_lock(object1
);
5417 vm_object_lock(object2
);
5419 vm_object_lock(object2
);
5420 vm_object_lock(object1
);
5422 object1_locked
= TRUE
;
5423 object2_locked
= TRUE
;
5425 if (object1
->size
!= object2
->size
||
5426 object1
->size
!= transpose_size
) {
5428 * If the 2 objects don't have the same size, we can't
5429 * exchange their backing stores or one would overflow.
5430 * If their size doesn't match the caller's
5431 * "transpose_size", we can't do it either because the
5432 * transpose operation will affect the entire span of
5435 retval
= KERN_INVALID_VALUE
;
5441 * Transpose the lists of resident pages.
5443 if (object1
->phys_contiguous
|| queue_empty(&object1
->memq
)) {
5445 * No pages in object1, just transfer pages
5446 * from object2 to object1. No need to go through
5447 * an intermediate object.
5449 while (!queue_empty(&object2
->memq
)) {
5450 page
= (vm_page_t
) queue_first(&object2
->memq
);
5451 vm_page_rename(page
, object1
, page
->offset
);
5453 assert(queue_empty(&object2
->memq
));
5454 } else if (object2
->phys_contiguous
|| queue_empty(&object2
->memq
)) {
5456 * No pages in object2, just transfer pages
5457 * from object1 to object2. No need to go through
5458 * an intermediate object.
5460 while (!queue_empty(&object1
->memq
)) {
5461 page
= (vm_page_t
) queue_first(&object1
->memq
);
5462 vm_page_rename(page
, object2
, page
->offset
);
5464 assert(queue_empty(&object1
->memq
));
5466 /* transfer object1's pages to tmp_object */
5467 vm_page_lock_queues();
5468 while (!queue_empty(&object1
->memq
)) {
5469 page
= (vm_page_t
) queue_first(&object1
->memq
);
5470 page_offset
= page
->offset
;
5471 vm_page_remove(page
);
5472 page
->offset
= page_offset
;
5473 queue_enter(&tmp_object
->memq
, page
, vm_page_t
, listq
);
5475 vm_page_unlock_queues();
5476 assert(queue_empty(&object1
->memq
));
5477 /* transfer object2's pages to object1 */
5478 while (!queue_empty(&object2
->memq
)) {
5479 page
= (vm_page_t
) queue_first(&object2
->memq
);
5480 vm_page_rename(page
, object1
, page
->offset
);
5482 assert(queue_empty(&object2
->memq
));
5483 /* transfer tmp_object's pages to object1 */
5484 while (!queue_empty(&tmp_object
->memq
)) {
5485 page
= (vm_page_t
) queue_first(&tmp_object
->memq
);
5486 queue_remove(&tmp_object
->memq
, page
,
5488 vm_page_insert(page
, object2
, page
->offset
);
5490 assert(queue_empty(&tmp_object
->memq
));
5493 /* no need to transpose the size: they should be identical */
5494 assert(object1
->size
== object2
->size
);
5496 #define __TRANSPOSE_FIELD(field) \
5498 tmp_object->field = object1->field; \
5499 object1->field = object2->field; \
5500 object2->field = tmp_object->field; \
5503 assert(!object1
->copy
);
5504 assert(!object2
->copy
);
5506 assert(!object1
->shadow
);
5507 assert(!object2
->shadow
);
5509 __TRANSPOSE_FIELD(shadow_offset
); /* used by phys_contiguous objects */
5510 __TRANSPOSE_FIELD(pager
);
5511 __TRANSPOSE_FIELD(paging_offset
);
5513 __TRANSPOSE_FIELD(pager_control
);
5514 /* update the memory_objects' pointers back to the VM objects */
5515 if (object1
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
5516 memory_object_control_collapse(object1
->pager_control
,
5519 if (object2
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
5520 memory_object_control_collapse(object2
->pager_control
,
5524 __TRANSPOSE_FIELD(absent_count
);
5526 assert(object1
->paging_in_progress
);
5527 assert(object2
->paging_in_progress
);
5529 __TRANSPOSE_FIELD(pager_created
);
5530 __TRANSPOSE_FIELD(pager_initialized
);
5531 __TRANSPOSE_FIELD(pager_ready
);
5532 __TRANSPOSE_FIELD(pager_trusted
);
5533 __TRANSPOSE_FIELD(internal
);
5534 __TRANSPOSE_FIELD(temporary
);
5535 __TRANSPOSE_FIELD(private);
5536 __TRANSPOSE_FIELD(pageout
);
5537 __TRANSPOSE_FIELD(true_share
);
5538 __TRANSPOSE_FIELD(phys_contiguous
);
5539 __TRANSPOSE_FIELD(nophyscache
);
5540 __TRANSPOSE_FIELD(last_alloc
);
5541 __TRANSPOSE_FIELD(sequential
);
5542 __TRANSPOSE_FIELD(cluster_size
);
5543 __TRANSPOSE_FIELD(existence_map
);
5544 __TRANSPOSE_FIELD(cow_hint
);
5545 __TRANSPOSE_FIELD(wimg_bits
);
5547 #undef __TRANSPOSE_FIELD
5549 retval
= KERN_SUCCESS
;
5555 if (tmp_object
!= VM_OBJECT_NULL
) {
5556 vm_object_paging_end(tmp_object
);
5557 vm_object_unlock(tmp_object
);
5559 * Re-initialize the temporary object to avoid
5560 * deallocating a real pager.
5562 _vm_object_allocate(transpose_size
, tmp_object
);
5563 vm_object_deallocate(tmp_object
);
5564 tmp_object
= VM_OBJECT_NULL
;
5567 if (object1_locked
) {
5568 vm_object_unlock(object1
);
5569 object1_locked
= FALSE
;
5571 if (object2_locked
) {
5572 vm_object_unlock(object2
);
5573 object2_locked
= FALSE
;
5575 if (object1_paging
) {
5576 vm_object_lock(object1
);
5577 vm_object_paging_end(object1
);
5578 vm_object_unlock(object1
);
5579 object1_paging
= FALSE
;
5581 if (object2_paging
) {
5582 vm_object_lock(object2
);
5583 vm_object_paging_end(object2
);
5584 vm_object_unlock(object2
);
5585 object2_paging
= FALSE
;