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 vm_object_t kernel_object
;
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\n", 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 * There's only one reference left on this
684 * VM object. We can't tell if it's a valid
685 * one (from a mapping for example) or if this
686 * object is just part of a possibly stale and
687 * useless shadow chain.
688 * We would like to try and collapse it into
689 * its parent, but we don't have any pointers
690 * back to this parent object.
691 * But we can try and collapse this object with
692 * its own shadows, in case these are useless
694 * We can't bypass this object though, since we
695 * don't know if this last reference on it is
698 vm_object_collapse(object
, 0, FALSE
);
701 vm_object_unlock(object
);
702 if (retry_cache_trim
&&
703 ((object
= vm_object_cache_trim(TRUE
)) !=
711 * We have to wait for initialization
712 * before destroying or caching the object.
715 if (object
->pager_created
&& ! object
->pager_initialized
) {
716 assert(! object
->can_persist
);
717 vm_object_assert_wait(object
,
718 VM_OBJECT_EVENT_INITIALIZED
,
720 vm_object_unlock(object
);
721 vm_object_cache_unlock();
722 thread_block(THREAD_CONTINUE_NULL
);
727 * If this object can persist, then enter it in
728 * the cache. Otherwise, terminate it.
730 * NOTE: Only permanent objects are cached, and
731 * permanent objects cannot have shadows. This
732 * affects the residence counting logic in a minor
733 * way (can do it in-line, mostly).
736 if ((object
->can_persist
) && (object
->alive
)) {
738 * Now it is safe to decrement reference count,
739 * and to return if reference count is > 0.
741 if (--object
->ref_count
> 0) {
742 vm_object_res_deallocate(object
);
743 vm_object_unlock(object
);
744 vm_object_cache_unlock();
745 if (retry_cache_trim
&&
746 ((object
= vm_object_cache_trim(TRUE
)) !=
753 #if MIGHT_NOT_CACHE_SHADOWS
755 * Remove shadow now if we don't
756 * want to cache shadows.
758 if (! cache_shadows
) {
759 shadow
= object
->shadow
;
760 object
->shadow
= VM_OBJECT_NULL
;
762 #endif /* MIGHT_NOT_CACHE_SHADOWS */
765 * Enter the object onto the queue of
766 * cached objects, and deactivate
769 assert(object
->shadow
== VM_OBJECT_NULL
);
770 VM_OBJ_RES_DECR(object
);
772 "vm_o_deallocate: adding %x to cache, queue = (%x, %x)\n",
774 (integer_t
)vm_object_cached_list
.next
,
775 (integer_t
)vm_object_cached_list
.prev
,0,0);
777 vm_object_cached_count
++;
778 if (vm_object_cached_count
> vm_object_cached_high
)
779 vm_object_cached_high
= vm_object_cached_count
;
780 queue_enter(&vm_object_cached_list
, object
,
781 vm_object_t
, cached_list
);
782 vm_object_cache_unlock();
783 vm_object_deactivate_all_pages(object
);
784 vm_object_unlock(object
);
786 #if MIGHT_NOT_CACHE_SHADOWS
788 * If we have a shadow that we need
789 * to deallocate, do so now, remembering
790 * to trim the cache later.
792 if (! cache_shadows
&& shadow
!= VM_OBJECT_NULL
) {
794 retry_cache_trim
= TRUE
;
797 #endif /* MIGHT_NOT_CACHE_SHADOWS */
800 * Trim the cache. If the cache trim
801 * returns with a shadow for us to deallocate,
802 * then remember to retry the cache trim
803 * when we are done deallocating the shadow.
804 * Otherwise, we are done.
807 object
= vm_object_cache_trim(TRUE
);
808 if (object
== VM_OBJECT_NULL
) {
811 retry_cache_trim
= TRUE
;
815 * This object is not cachable; terminate it.
818 "vm_o_deallocate: !cacheable 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
819 (integer_t
)object
, object
->resident_page_count
,
820 object
->paging_in_progress
,
821 (void *)current_thread(),object
->ref_count
);
823 VM_OBJ_RES_DECR(object
); /* XXX ? */
825 * Terminate this object. If it had a shadow,
826 * then deallocate it; otherwise, if we need
827 * to retry a cache trim, do so now; otherwise,
828 * we are done. "pageout" objects have a shadow,
829 * but maintain a "paging reference" rather than
830 * a normal reference.
832 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
833 if(vm_object_terminate(object
) != KERN_SUCCESS
) {
836 if (shadow
!= VM_OBJECT_NULL
) {
840 if (retry_cache_trim
&&
841 ((object
= vm_object_cache_trim(TRUE
)) !=
848 assert(! retry_cache_trim
);
852 * Check to see whether we really need to trim
853 * down the cache. If so, remove an object from
854 * the cache, terminate it, and repeat.
856 * Called with, and returns with, cache lock unlocked.
859 vm_object_cache_trim(
860 boolean_t called_from_vm_object_deallocate
)
862 register vm_object_t object
= VM_OBJECT_NULL
;
868 * If we no longer need to trim the cache,
872 vm_object_cache_lock();
873 if (vm_object_cached_count
<= vm_object_cached_max
) {
874 vm_object_cache_unlock();
875 return VM_OBJECT_NULL
;
879 * We must trim down the cache, so remove
880 * the first object in the cache.
883 "vm_object_cache_trim: removing from front of cache (%x, %x)\n",
884 (integer_t
)vm_object_cached_list
.next
,
885 (integer_t
)vm_object_cached_list
.prev
, 0, 0, 0);
887 object
= (vm_object_t
) queue_first(&vm_object_cached_list
);
888 if(object
== (vm_object_t
) &vm_object_cached_list
) {
889 /* something's wrong with the calling parameter or */
890 /* the value of vm_object_cached_count, just fix */
892 if(vm_object_cached_max
< 0)
893 vm_object_cached_max
= 0;
894 vm_object_cached_count
= 0;
895 vm_object_cache_unlock();
896 return VM_OBJECT_NULL
;
898 vm_object_lock(object
);
899 queue_remove(&vm_object_cached_list
, object
, vm_object_t
,
901 vm_object_cached_count
--;
904 * Since this object is in the cache, we know
905 * that it is initialized and has no references.
906 * Take a reference to avoid recursive deallocations.
909 assert(object
->pager_initialized
);
910 assert(object
->ref_count
== 0);
914 * Terminate the object.
915 * If the object had a shadow, we let vm_object_deallocate
916 * deallocate it. "pageout" objects have a shadow, but
917 * maintain a "paging reference" rather than a normal
919 * (We are careful here to limit recursion.)
921 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
922 if(vm_object_terminate(object
) != KERN_SUCCESS
)
924 if (shadow
!= VM_OBJECT_NULL
) {
925 if (called_from_vm_object_deallocate
) {
928 vm_object_deallocate(shadow
);
934 boolean_t vm_object_terminate_remove_all
= FALSE
;
937 * Routine: vm_object_terminate
939 * Free all resources associated with a vm_object.
941 * Upon entry, the object must be locked,
942 * and the object must have exactly one reference.
944 * The shadow object reference is left alone.
946 * The object must be unlocked if its found that pages
947 * must be flushed to a backing object. If someone
948 * manages to map the object while it is being flushed
949 * the object is returned unlocked and unchanged. Otherwise,
950 * upon exit, the cache will be unlocked, and the
951 * object will cease to exist.
955 register vm_object_t object
)
957 register vm_page_t p
;
958 vm_object_t shadow_object
;
960 XPR(XPR_VM_OBJECT
, "vm_object_terminate, object 0x%X ref %d\n",
961 (integer_t
)object
, object
->ref_count
, 0, 0, 0);
963 if (!object
->pageout
&& (!object
->temporary
|| object
->can_persist
)
964 && (object
->pager
!= NULL
|| object
->shadow_severed
)) {
965 vm_object_cache_unlock();
966 while (!queue_empty(&object
->memq
)) {
968 * Clear pager_trusted bit so that the pages get yanked
969 * out of the object instead of cleaned in place. This
970 * prevents a deadlock in XMM and makes more sense anyway.
972 object
->pager_trusted
= FALSE
;
974 p
= (vm_page_t
) queue_first(&object
->memq
);
978 if (p
->busy
|| p
->cleaning
) {
979 if(p
->cleaning
|| p
->absent
) {
980 vm_object_paging_wait(object
, THREAD_UNINT
);
983 panic("vm_object_terminate.3 0x%x 0x%x", object
, p
);
987 vm_page_lock_queues();
989 VM_PAGE_QUEUES_REMOVE(p
);
990 vm_page_unlock_queues();
992 if (p
->absent
|| p
->private) {
995 * For private pages, VM_PAGE_FREE just
996 * leaves the page structure around for
997 * its owner to clean up. For absent
998 * pages, the structure is returned to
999 * the appropriate pool.
1006 panic("vm_object_terminate.4 0x%x 0x%x", object
, p
);
1009 p
->dirty
= pmap_is_modified(p
->phys_page
);
1011 if ((p
->dirty
|| p
->precious
) && !p
->error
&& object
->alive
) {
1012 vm_pageout_cluster(p
); /* flush page */
1013 vm_object_paging_wait(object
, THREAD_UNINT
);
1015 "vm_object_terminate restart, object 0x%X ref %d\n",
1016 (integer_t
)object
, object
->ref_count
, 0, 0, 0);
1022 vm_object_unlock(object
);
1023 vm_object_cache_lock();
1024 vm_object_lock(object
);
1028 * Make sure the object isn't already being terminated
1030 if(object
->terminating
) {
1031 object
->ref_count
-= 1;
1032 assert(object
->ref_count
> 0);
1033 vm_object_cache_unlock();
1034 vm_object_unlock(object
);
1035 return KERN_FAILURE
;
1039 * Did somebody get a reference to the object while we were
1042 if(object
->ref_count
!= 1) {
1043 object
->ref_count
-= 1;
1044 assert(object
->ref_count
> 0);
1045 vm_object_res_deallocate(object
);
1046 vm_object_cache_unlock();
1047 vm_object_unlock(object
);
1048 return KERN_FAILURE
;
1052 * Make sure no one can look us up now.
1055 object
->terminating
= TRUE
;
1056 object
->alive
= FALSE
;
1057 vm_object_remove(object
);
1060 * Detach the object from its shadow if we are the shadow's
1061 * copy. The reference we hold on the shadow must be dropped
1064 if (((shadow_object
= object
->shadow
) != VM_OBJECT_NULL
) &&
1065 !(object
->pageout
)) {
1066 vm_object_lock(shadow_object
);
1067 if (shadow_object
->copy
== object
)
1068 shadow_object
->copy
= VM_OBJECT_NULL
;
1069 vm_object_unlock(shadow_object
);
1072 if (FALSE
&& object
->paging_in_progress
!= 0) {
1074 * There are still some paging_in_progress references
1075 * on this object, meaning that there are some paging
1076 * or other I/O operations in progress for this VM object.
1077 * Such operations take some paging_in_progress references
1078 * up front to ensure that the object doesn't go away, but
1079 * they may also need to acquire a reference on the VM object,
1080 * to map it in kernel space, for example. That means that
1081 * they may end up releasing the last reference on the VM
1082 * object, triggering its termination, while still holding
1083 * paging_in_progress references. Waiting for these
1084 * pending paging_in_progress references to go away here would
1087 * To avoid deadlocking, we'll let the vm_object_reaper_thread
1088 * complete the VM object termination if it still holds
1089 * paging_in_progress references at this point.
1091 * No new paging_in_progress should appear now that the
1092 * VM object is "terminating" and not "alive".
1094 vm_object_reap_async(object
);
1095 vm_object_cache_unlock();
1096 vm_object_unlock(object
);
1097 return KERN_SUCCESS
;
1100 /* complete the VM object termination */
1101 vm_object_reap(object
);
1102 object
= VM_OBJECT_NULL
;
1103 /* cache lock and object lock were released by vm_object_reap() */
1105 return KERN_SUCCESS
;
1111 * Complete the termination of a VM object after it's been marked
1112 * as "terminating" and "!alive" by vm_object_terminate().
1114 * The VM object cache and the VM object must be locked by caller.
1115 * The locks will be released on return and the VM object is no longer valid.
1121 memory_object_t pager
;
1125 mutex_assert(&vm_object_cached_lock_data
, MA_OWNED
);
1126 mutex_assert(&object
->Lock
, MA_OWNED
);
1129 vm_object_reap_count
++;
1132 * The pageout daemon might be playing with our pages.
1133 * Now that the object is dead, it won't touch any more
1134 * pages, but some pages might already be on their way out.
1135 * Hence, we wait until the active paging activities have
1136 * ceased before we break the association with the pager
1139 while (object
->paging_in_progress
!= 0) {
1140 vm_object_cache_unlock();
1141 vm_object_wait(object
,
1142 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
1144 vm_object_cache_lock();
1145 vm_object_lock(object
);
1148 assert(object
->paging_in_progress
== 0);
1149 pager
= object
->pager
;
1150 object
->pager
= MEMORY_OBJECT_NULL
;
1152 if (pager
!= MEMORY_OBJECT_NULL
)
1153 memory_object_control_disable(object
->pager_control
);
1154 vm_object_cache_unlock();
1156 object
->ref_count
--;
1158 assert(object
->res_count
== 0);
1159 #endif /* TASK_SWAPPER */
1161 assert (object
->ref_count
== 0);
1164 * Clean or free the pages, as appropriate.
1165 * It is possible for us to find busy/absent pages,
1166 * if some faults on this object were aborted.
1168 if (object
->pageout
) {
1169 assert(object
->shadow
!= VM_OBJECT_NULL
);
1171 vm_pageout_object_terminate(object
);
1173 } else if ((object
->temporary
&& !object
->can_persist
) ||
1174 (pager
== MEMORY_OBJECT_NULL
)) {
1175 while (!queue_empty(&object
->memq
)) {
1176 p
= (vm_page_t
) queue_first(&object
->memq
);
1181 } else if (!queue_empty(&object
->memq
)) {
1182 panic("vm_object_reap: queue just emptied isn't");
1185 assert(object
->paging_in_progress
== 0);
1186 assert(object
->ref_count
== 0);
1189 * If the pager has not already been released by
1190 * vm_object_destroy, we need to terminate it and
1191 * release our reference to it here.
1193 if (pager
!= MEMORY_OBJECT_NULL
) {
1194 vm_object_unlock(object
);
1195 vm_object_release_pager(pager
);
1196 vm_object_lock(object
);
1199 /* kick off anyone waiting on terminating */
1200 object
->terminating
= FALSE
;
1201 vm_object_paging_begin(object
);
1202 vm_object_paging_end(object
);
1203 vm_object_unlock(object
);
1206 vm_external_destroy(object
->existence_map
, object
->size
);
1207 #endif /* MACH_PAGEMAP */
1210 * Free the space for the object.
1212 zfree(vm_object_zone
, object
);
1213 object
= VM_OBJECT_NULL
;
1217 vm_object_reap_async(
1221 mutex_assert(&vm_object_cached_lock_data
, MA_OWNED
);
1222 mutex_assert(&object
->Lock
, MA_OWNED
);
1225 vm_object_reap_count_async
++;
1227 /* enqueue the VM object... */
1228 queue_enter(&vm_object_reaper_queue
, object
,
1229 vm_object_t
, cached_list
);
1230 /* ... and wake up the reaper thread */
1231 thread_wakeup((event_t
) &vm_object_reaper_queue
);
1235 vm_object_reaper_thread(void)
1239 vm_object_cache_lock();
1241 while (!queue_empty(&vm_object_reaper_queue
)) {
1242 queue_remove_first(&vm_object_reaper_queue
,
1246 vm_object_lock(object
);
1247 assert(object
->terminating
);
1248 assert(!object
->alive
);
1250 vm_object_reap(object
);
1251 /* cache is unlocked and object is no longer valid */
1252 object
= VM_OBJECT_NULL
;
1254 vm_object_cache_lock();
1257 /* wait for more work... */
1258 assert_wait((event_t
) &vm_object_reaper_queue
, THREAD_UNINT
);
1259 vm_object_cache_unlock();
1260 thread_block((thread_continue_t
) vm_object_reaper_thread
);
1265 * Routine: vm_object_pager_wakeup
1266 * Purpose: Wake up anyone waiting for termination of a pager.
1270 vm_object_pager_wakeup(
1271 memory_object_t pager
)
1273 vm_object_hash_entry_t entry
;
1274 boolean_t waiting
= FALSE
;
1277 * If anyone was waiting for the memory_object_terminate
1278 * to be queued, wake them up now.
1280 vm_object_cache_lock();
1281 entry
= vm_object_hash_lookup(pager
, TRUE
);
1282 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
1283 waiting
= entry
->waiting
;
1284 vm_object_cache_unlock();
1285 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
) {
1287 thread_wakeup((event_t
) pager
);
1288 vm_object_hash_entry_free(entry
);
1293 * Routine: vm_object_release_pager
1294 * Purpose: Terminate the pager and, upon completion,
1295 * release our last reference to it.
1296 * just like memory_object_terminate, except
1297 * that we wake up anyone blocked in vm_object_enter
1298 * waiting for termination message to be queued
1299 * before calling memory_object_init.
1302 vm_object_release_pager(
1303 memory_object_t pager
)
1307 * Terminate the pager.
1310 (void) memory_object_terminate(pager
);
1313 * Wakeup anyone waiting for this terminate
1315 vm_object_pager_wakeup(pager
);
1318 * Release reference to pager.
1320 memory_object_deallocate(pager
);
1324 * Routine: vm_object_destroy
1326 * Shut down a VM object, despite the
1327 * presence of address map (or other) references
1333 __unused kern_return_t reason
)
1335 memory_object_t old_pager
;
1337 if (object
== VM_OBJECT_NULL
)
1338 return(KERN_SUCCESS
);
1341 * Remove the pager association immediately.
1343 * This will prevent the memory manager from further
1344 * meddling. [If it wanted to flush data or make
1345 * other changes, it should have done so before performing
1346 * the destroy call.]
1349 vm_object_cache_lock();
1350 vm_object_lock(object
);
1351 object
->can_persist
= FALSE
;
1352 object
->named
= FALSE
;
1353 object
->alive
= FALSE
;
1356 * Rip out the pager from the vm_object now...
1359 vm_object_remove(object
);
1360 old_pager
= object
->pager
;
1361 object
->pager
= MEMORY_OBJECT_NULL
;
1362 if (old_pager
!= MEMORY_OBJECT_NULL
)
1363 memory_object_control_disable(object
->pager_control
);
1364 vm_object_cache_unlock();
1367 * Wait for the existing paging activity (that got
1368 * through before we nulled out the pager) to subside.
1371 vm_object_paging_wait(object
, THREAD_UNINT
);
1372 vm_object_unlock(object
);
1375 * Terminate the object now.
1377 if (old_pager
!= MEMORY_OBJECT_NULL
) {
1378 vm_object_release_pager(old_pager
);
1381 * JMM - Release the caller's reference. This assumes the
1382 * caller had a reference to release, which is a big (but
1383 * currently valid) assumption if this is driven from the
1384 * vnode pager (it is holding a named reference when making
1387 vm_object_deallocate(object
);
1390 return(KERN_SUCCESS
);
1394 * vm_object_deactivate_pages
1396 * Deactivate all pages in the specified object. (Keep its pages
1397 * in memory even though it is no longer referenced.)
1399 * The object must be locked.
1402 vm_object_deactivate_all_pages(
1403 register vm_object_t object
)
1405 register vm_page_t p
;
1407 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1408 vm_page_lock_queues();
1410 vm_page_deactivate(p
);
1411 vm_page_unlock_queues();
1415 __private_extern__
void
1416 vm_object_deactivate_pages(
1418 vm_object_offset_t offset
,
1419 vm_object_size_t size
,
1420 boolean_t kill_page
)
1422 vm_object_t orig_object
;
1423 int pages_moved
= 0;
1424 int pages_found
= 0;
1427 * entered with object lock held, acquire a paging reference to
1428 * prevent the memory_object and control ports from
1431 orig_object
= object
;
1434 register vm_page_t m
;
1435 vm_object_offset_t toffset
;
1436 vm_object_size_t tsize
;
1438 vm_object_paging_begin(object
);
1439 vm_page_lock_queues();
1441 for (tsize
= size
, toffset
= offset
; tsize
; tsize
-= PAGE_SIZE
, toffset
+= PAGE_SIZE
) {
1443 if ((m
= vm_page_lookup(object
, toffset
)) != VM_PAGE_NULL
) {
1447 if ((m
->wire_count
== 0) && (!m
->private) && (!m
->gobbled
) && (!m
->busy
)) {
1449 assert(!m
->laundry
);
1451 m
->reference
= FALSE
;
1452 pmap_clear_reference(m
->phys_page
);
1454 if ((kill_page
) && (object
->internal
)) {
1455 m
->precious
= FALSE
;
1457 pmap_clear_modify(m
->phys_page
);
1458 vm_external_state_clr(object
->existence_map
, offset
);
1460 VM_PAGE_QUEUES_REMOVE(m
);
1462 assert(!m
->laundry
);
1463 assert(m
->object
!= kernel_object
);
1464 assert(m
->pageq
.next
== NULL
&&
1465 m
->pageq
.prev
== NULL
);
1469 m
, vm_page_t
, pageq
);
1472 &vm_page_queue_inactive
,
1473 m
, vm_page_t
, pageq
);
1478 vm_page_inactive_count
++;
1484 vm_page_unlock_queues();
1485 vm_object_paging_end(object
);
1487 if (object
->shadow
) {
1488 vm_object_t tmp_object
;
1492 offset
+= object
->shadow_offset
;
1494 tmp_object
= object
->shadow
;
1495 vm_object_lock(tmp_object
);
1497 if (object
!= orig_object
)
1498 vm_object_unlock(object
);
1499 object
= tmp_object
;
1503 if (object
!= orig_object
)
1504 vm_object_unlock(object
);
1508 * Routine: vm_object_pmap_protect
1511 * Reduces the permission for all physical
1512 * pages in the specified object range.
1514 * If removing write permission only, it is
1515 * sufficient to protect only the pages in
1516 * the top-level object; only those pages may
1517 * have write permission.
1519 * If removing all access, we must follow the
1520 * shadow chain from the top-level object to
1521 * remove access to all pages in shadowed objects.
1523 * The object must *not* be locked. The object must
1524 * be temporary/internal.
1526 * If pmap is not NULL, this routine assumes that
1527 * the only mappings for the pages are in that
1531 __private_extern__
void
1532 vm_object_pmap_protect(
1533 register vm_object_t object
,
1534 register vm_object_offset_t offset
,
1535 vm_object_size_t size
,
1537 vm_map_offset_t pmap_start
,
1540 if (object
== VM_OBJECT_NULL
)
1542 size
= vm_object_round_page(size
);
1543 offset
= vm_object_trunc_page(offset
);
1545 vm_object_lock(object
);
1547 assert(object
->internal
);
1550 if (ptoa_64(object
->resident_page_count
) > size
/2 && pmap
!= PMAP_NULL
) {
1551 vm_object_unlock(object
);
1552 pmap_protect(pmap
, pmap_start
, pmap_start
+ size
, prot
);
1556 /* if we are doing large ranges with respect to resident */
1557 /* page count then we should interate over pages otherwise */
1558 /* inverse page look-up will be faster */
1559 if (ptoa_64(object
->resident_page_count
/ 4) < size
) {
1561 vm_object_offset_t end
;
1563 end
= offset
+ size
;
1565 if (pmap
!= PMAP_NULL
) {
1566 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1567 if (!p
->fictitious
&&
1568 (offset
<= p
->offset
) && (p
->offset
< end
)) {
1569 vm_map_offset_t start
;
1571 start
= pmap_start
+ p
->offset
- offset
;
1572 pmap_protect(pmap
, start
, start
+ PAGE_SIZE_64
, prot
);
1576 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1577 if (!p
->fictitious
&&
1578 (offset
<= p
->offset
) && (p
->offset
< end
)) {
1580 pmap_page_protect(p
->phys_page
,
1581 prot
& ~p
->page_lock
);
1587 vm_object_offset_t end
;
1588 vm_object_offset_t target_off
;
1590 end
= offset
+ size
;
1592 if (pmap
!= PMAP_NULL
) {
1593 for(target_off
= offset
;
1595 target_off
+= PAGE_SIZE
) {
1596 p
= vm_page_lookup(object
, target_off
);
1597 if (p
!= VM_PAGE_NULL
) {
1599 start
= pmap_start
+
1600 (vm_offset_t
)(p
->offset
- offset
);
1601 pmap_protect(pmap
, start
,
1602 start
+ PAGE_SIZE
, prot
);
1606 for(target_off
= offset
;
1607 target_off
< end
; target_off
+= PAGE_SIZE
) {
1608 p
= vm_page_lookup(object
, target_off
);
1609 if (p
!= VM_PAGE_NULL
) {
1610 pmap_page_protect(p
->phys_page
,
1611 prot
& ~p
->page_lock
);
1617 if (prot
== VM_PROT_NONE
) {
1619 * Must follow shadow chain to remove access
1620 * to pages in shadowed objects.
1622 register vm_object_t next_object
;
1624 next_object
= object
->shadow
;
1625 if (next_object
!= VM_OBJECT_NULL
) {
1626 offset
+= object
->shadow_offset
;
1627 vm_object_lock(next_object
);
1628 vm_object_unlock(object
);
1629 object
= next_object
;
1633 * End of chain - we are done.
1640 * Pages in shadowed objects may never have
1641 * write permission - we may stop here.
1647 vm_object_unlock(object
);
1651 * Routine: vm_object_copy_slowly
1654 * Copy the specified range of the source
1655 * virtual memory object without using
1656 * protection-based optimizations (such
1657 * as copy-on-write). The pages in the
1658 * region are actually copied.
1660 * In/out conditions:
1661 * The caller must hold a reference and a lock
1662 * for the source virtual memory object. The source
1663 * object will be returned *unlocked*.
1666 * If the copy is completed successfully, KERN_SUCCESS is
1667 * returned. If the caller asserted the interruptible
1668 * argument, and an interruption occurred while waiting
1669 * for a user-generated event, MACH_SEND_INTERRUPTED is
1670 * returned. Other values may be returned to indicate
1671 * hard errors during the copy operation.
1673 * A new virtual memory object is returned in a
1674 * parameter (_result_object). The contents of this
1675 * new object, starting at a zero offset, are a copy
1676 * of the source memory region. In the event of
1677 * an error, this parameter will contain the value
1680 __private_extern__ kern_return_t
1681 vm_object_copy_slowly(
1682 register vm_object_t src_object
,
1683 vm_object_offset_t src_offset
,
1684 vm_object_size_t size
,
1685 boolean_t interruptible
,
1686 vm_object_t
*_result_object
) /* OUT */
1688 vm_object_t new_object
;
1689 vm_object_offset_t new_offset
;
1691 vm_object_offset_t src_lo_offset
= src_offset
;
1692 vm_object_offset_t src_hi_offset
= src_offset
+ size
;
1694 XPR(XPR_VM_OBJECT
, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
1695 src_object
, src_offset
, size
, 0, 0);
1698 vm_object_unlock(src_object
);
1699 *_result_object
= VM_OBJECT_NULL
;
1700 return(KERN_INVALID_ARGUMENT
);
1704 * Prevent destruction of the source object while we copy.
1707 assert(src_object
->ref_count
> 0);
1708 src_object
->ref_count
++;
1709 VM_OBJ_RES_INCR(src_object
);
1710 vm_object_unlock(src_object
);
1713 * Create a new object to hold the copied pages.
1715 * We fill the new object starting at offset 0,
1716 * regardless of the input offset.
1717 * We don't bother to lock the new object within
1718 * this routine, since we have the only reference.
1721 new_object
= vm_object_allocate(size
);
1723 vm_object_lock(new_object
);
1725 assert(size
== trunc_page_64(size
)); /* Will the loop terminate? */
1729 src_offset
+= PAGE_SIZE_64
,
1730 new_offset
+= PAGE_SIZE_64
, size
-= PAGE_SIZE_64
1733 vm_fault_return_t result
;
1735 while ((new_page
= vm_page_alloc(new_object
, new_offset
))
1737 if (!vm_page_wait(interruptible
)) {
1738 vm_object_unlock(new_object
);
1739 vm_object_deallocate(new_object
);
1740 vm_object_deallocate(src_object
);
1741 *_result_object
= VM_OBJECT_NULL
;
1742 return(MACH_SEND_INTERRUPTED
);
1747 vm_prot_t prot
= VM_PROT_READ
;
1748 vm_page_t _result_page
;
1751 vm_page_t result_page
;
1752 kern_return_t error_code
;
1754 vm_object_lock(src_object
);
1755 vm_object_paging_begin(src_object
);
1757 XPR(XPR_VM_FAULT
,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
1758 result
= vm_fault_page(src_object
, src_offset
,
1759 VM_PROT_READ
, FALSE
, interruptible
,
1760 src_lo_offset
, src_hi_offset
,
1761 VM_BEHAVIOR_SEQUENTIAL
,
1762 &prot
, &_result_page
, &top_page
,
1764 &error_code
, FALSE
, FALSE
, NULL
, 0);
1767 case VM_FAULT_SUCCESS
:
1768 result_page
= _result_page
;
1771 * We don't need to hold the object
1772 * lock -- the busy page will be enough.
1773 * [We don't care about picking up any
1774 * new modifications.]
1776 * Copy the page to the new object.
1779 * If result_page is clean,
1780 * we could steal it instead
1784 vm_object_unlock(result_page
->object
);
1785 vm_page_copy(result_page
, new_page
);
1788 * Let go of both pages (make them
1789 * not busy, perform wakeup, activate).
1792 new_page
->busy
= FALSE
;
1793 new_page
->dirty
= TRUE
;
1794 vm_object_lock(result_page
->object
);
1795 PAGE_WAKEUP_DONE(result_page
);
1797 vm_page_lock_queues();
1798 if (!result_page
->active
&&
1799 !result_page
->inactive
)
1800 vm_page_activate(result_page
);
1801 vm_page_activate(new_page
);
1802 vm_page_unlock_queues();
1805 * Release paging references and
1806 * top-level placeholder page, if any.
1809 vm_fault_cleanup(result_page
->object
,
1814 case VM_FAULT_RETRY
:
1817 case VM_FAULT_FICTITIOUS_SHORTAGE
:
1818 vm_page_more_fictitious();
1821 case VM_FAULT_MEMORY_SHORTAGE
:
1822 if (vm_page_wait(interruptible
))
1826 case VM_FAULT_INTERRUPTED
:
1827 vm_page_free(new_page
);
1828 vm_object_unlock(new_object
);
1829 vm_object_deallocate(new_object
);
1830 vm_object_deallocate(src_object
);
1831 *_result_object
= VM_OBJECT_NULL
;
1832 return(MACH_SEND_INTERRUPTED
);
1834 case VM_FAULT_MEMORY_ERROR
:
1837 * (a) ignore pages that we can't
1839 * (b) return the null object if
1840 * any page fails [chosen]
1843 vm_page_lock_queues();
1844 vm_page_free(new_page
);
1845 vm_page_unlock_queues();
1846 vm_object_unlock(new_object
);
1847 vm_object_deallocate(new_object
);
1848 vm_object_deallocate(src_object
);
1849 *_result_object
= VM_OBJECT_NULL
;
1850 return(error_code
? error_code
:
1853 } while (result
!= VM_FAULT_SUCCESS
);
1857 * Lose the extra reference, and return our object.
1860 vm_object_unlock(new_object
);
1861 vm_object_deallocate(src_object
);
1862 *_result_object
= new_object
;
1863 return(KERN_SUCCESS
);
1867 * Routine: vm_object_copy_quickly
1870 * Copy the specified range of the source virtual
1871 * memory object, if it can be done without waiting
1872 * for user-generated events.
1875 * If the copy is successful, the copy is returned in
1876 * the arguments; otherwise, the arguments are not
1879 * In/out conditions:
1880 * The object should be unlocked on entry and exit.
1884 __private_extern__ boolean_t
1885 vm_object_copy_quickly(
1886 vm_object_t
*_object
, /* INOUT */
1887 __unused vm_object_offset_t offset
, /* IN */
1888 __unused vm_object_size_t size
, /* IN */
1889 boolean_t
*_src_needs_copy
, /* OUT */
1890 boolean_t
*_dst_needs_copy
) /* OUT */
1892 vm_object_t object
= *_object
;
1893 memory_object_copy_strategy_t copy_strategy
;
1895 XPR(XPR_VM_OBJECT
, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
1896 *_object
, offset
, size
, 0, 0);
1897 if (object
== VM_OBJECT_NULL
) {
1898 *_src_needs_copy
= FALSE
;
1899 *_dst_needs_copy
= FALSE
;
1903 vm_object_lock(object
);
1905 copy_strategy
= object
->copy_strategy
;
1907 switch (copy_strategy
) {
1908 case MEMORY_OBJECT_COPY_SYMMETRIC
:
1911 * Symmetric copy strategy.
1912 * Make another reference to the object.
1913 * Leave object/offset unchanged.
1916 assert(object
->ref_count
> 0);
1917 object
->ref_count
++;
1918 vm_object_res_reference(object
);
1919 object
->shadowed
= TRUE
;
1920 vm_object_unlock(object
);
1923 * Both source and destination must make
1924 * shadows, and the source must be made
1925 * read-only if not already.
1928 *_src_needs_copy
= TRUE
;
1929 *_dst_needs_copy
= TRUE
;
1933 case MEMORY_OBJECT_COPY_DELAY
:
1934 vm_object_unlock(object
);
1938 vm_object_unlock(object
);
1944 static int copy_call_count
= 0;
1945 static int copy_call_sleep_count
= 0;
1946 static int copy_call_restart_count
= 0;
1949 * Routine: vm_object_copy_call [internal]
1952 * Copy the source object (src_object), using the
1953 * user-managed copy algorithm.
1955 * In/out conditions:
1956 * The source object must be locked on entry. It
1957 * will be *unlocked* on exit.
1960 * If the copy is successful, KERN_SUCCESS is returned.
1961 * A new object that represents the copied virtual
1962 * memory is returned in a parameter (*_result_object).
1963 * If the return value indicates an error, this parameter
1966 static kern_return_t
1967 vm_object_copy_call(
1968 vm_object_t src_object
,
1969 vm_object_offset_t src_offset
,
1970 vm_object_size_t size
,
1971 vm_object_t
*_result_object
) /* OUT */
1975 boolean_t check_ready
= FALSE
;
1978 * If a copy is already in progress, wait and retry.
1981 * Consider making this call interruptable, as Mike
1982 * intended it to be.
1985 * Need a counter or version or something to allow
1986 * us to use the copy that the currently requesting
1987 * thread is obtaining -- is it worth adding to the
1988 * vm object structure? Depends how common this case it.
1991 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
1992 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
1994 copy_call_restart_count
++;
1998 * Indicate (for the benefit of memory_object_create_copy)
1999 * that we want a copy for src_object. (Note that we cannot
2000 * do a real assert_wait before calling memory_object_copy,
2001 * so we simply set the flag.)
2004 vm_object_set_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
);
2005 vm_object_unlock(src_object
);
2008 * Ask the memory manager to give us a memory object
2009 * which represents a copy of the src object.
2010 * The memory manager may give us a memory object
2011 * which we already have, or it may give us a
2012 * new memory object. This memory object will arrive
2013 * via memory_object_create_copy.
2016 kr
= KERN_FAILURE
; /* XXX need to change memory_object.defs */
2017 if (kr
!= KERN_SUCCESS
) {
2022 * Wait for the copy to arrive.
2024 vm_object_lock(src_object
);
2025 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
2026 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
2028 copy_call_sleep_count
++;
2031 assert(src_object
->copy
!= VM_OBJECT_NULL
);
2032 copy
= src_object
->copy
;
2033 if (!vm_object_lock_try(copy
)) {
2034 vm_object_unlock(src_object
);
2035 mutex_pause(); /* wait a bit */
2036 vm_object_lock(src_object
);
2039 if (copy
->size
< src_offset
+size
)
2040 copy
->size
= src_offset
+size
;
2042 if (!copy
->pager_ready
)
2048 *_result_object
= copy
;
2049 vm_object_unlock(copy
);
2050 vm_object_unlock(src_object
);
2052 /* Wait for the copy to be ready. */
2053 if (check_ready
== TRUE
) {
2054 vm_object_lock(copy
);
2055 while (!copy
->pager_ready
) {
2056 vm_object_sleep(copy
, VM_OBJECT_EVENT_PAGER_READY
, THREAD_UNINT
);
2058 vm_object_unlock(copy
);
2061 return KERN_SUCCESS
;
2064 static int copy_delayed_lock_collisions
= 0;
2065 static int copy_delayed_max_collisions
= 0;
2066 static int copy_delayed_lock_contention
= 0;
2067 static int copy_delayed_protect_iterate
= 0;
2070 * Routine: vm_object_copy_delayed [internal]
2073 * Copy the specified virtual memory object, using
2074 * the asymmetric copy-on-write algorithm.
2076 * In/out conditions:
2077 * The src_object must be locked on entry. It will be unlocked
2078 * on exit - so the caller must also hold a reference to it.
2080 * This routine will not block waiting for user-generated
2081 * events. It is not interruptible.
2083 __private_extern__ vm_object_t
2084 vm_object_copy_delayed(
2085 vm_object_t src_object
,
2086 vm_object_offset_t src_offset
,
2087 vm_object_size_t size
)
2089 vm_object_t new_copy
= VM_OBJECT_NULL
;
2090 vm_object_t old_copy
;
2092 vm_object_size_t copy_size
= src_offset
+ size
;
2096 * The user-level memory manager wants to see all of the changes
2097 * to this object, but it has promised not to make any changes on
2100 * Perform an asymmetric copy-on-write, as follows:
2101 * Create a new object, called a "copy object" to hold
2102 * pages modified by the new mapping (i.e., the copy,
2103 * not the original mapping).
2104 * Record the original object as the backing object for
2105 * the copy object. If the original mapping does not
2106 * change a page, it may be used read-only by the copy.
2107 * Record the copy object in the original object.
2108 * When the original mapping causes a page to be modified,
2109 * it must be copied to a new page that is "pushed" to
2111 * Mark the new mapping (the copy object) copy-on-write.
2112 * This makes the copy object itself read-only, allowing
2113 * it to be reused if the original mapping makes no
2114 * changes, and simplifying the synchronization required
2115 * in the "push" operation described above.
2117 * The copy-on-write is said to be assymetric because the original
2118 * object is *not* marked copy-on-write. A copied page is pushed
2119 * to the copy object, regardless which party attempted to modify
2122 * Repeated asymmetric copy operations may be done. If the
2123 * original object has not been changed since the last copy, its
2124 * copy object can be reused. Otherwise, a new copy object can be
2125 * inserted between the original object and its previous copy
2126 * object. Since any copy object is read-only, this cannot affect
2127 * affect the contents of the previous copy object.
2129 * Note that a copy object is higher in the object tree than the
2130 * original object; therefore, use of the copy object recorded in
2131 * the original object must be done carefully, to avoid deadlock.
2137 * Wait for paging in progress.
2139 if (!src_object
->true_share
)
2140 vm_object_paging_wait(src_object
, THREAD_UNINT
);
2143 * See whether we can reuse the result of a previous
2147 old_copy
= src_object
->copy
;
2148 if (old_copy
!= VM_OBJECT_NULL
) {
2150 * Try to get the locks (out of order)
2152 if (!vm_object_lock_try(old_copy
)) {
2153 vm_object_unlock(src_object
);
2156 /* Heisenberg Rules */
2157 copy_delayed_lock_collisions
++;
2158 if (collisions
++ == 0)
2159 copy_delayed_lock_contention
++;
2161 if (collisions
> copy_delayed_max_collisions
)
2162 copy_delayed_max_collisions
= collisions
;
2164 vm_object_lock(src_object
);
2169 * Determine whether the old copy object has
2173 if (old_copy
->resident_page_count
== 0 &&
2174 !old_copy
->pager_created
) {
2176 * It has not been modified.
2178 * Return another reference to
2179 * the existing copy-object if
2180 * we can safely grow it (if
2184 if (old_copy
->size
< copy_size
) {
2186 * We can't perform a delayed copy if any of the
2187 * pages in the extended range are wired (because
2188 * we can't safely take write permission away from
2189 * wired pages). If the pages aren't wired, then
2190 * go ahead and protect them.
2192 copy_delayed_protect_iterate
++;
2193 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
2194 if (!p
->fictitious
&&
2195 p
->offset
>= old_copy
->size
&&
2196 p
->offset
< copy_size
) {
2197 if (p
->wire_count
> 0) {
2198 vm_object_unlock(old_copy
);
2199 vm_object_unlock(src_object
);
2201 if (new_copy
!= VM_OBJECT_NULL
) {
2202 vm_object_unlock(new_copy
);
2203 vm_object_deallocate(new_copy
);
2206 return VM_OBJECT_NULL
;
2208 pmap_page_protect(p
->phys_page
,
2209 (VM_PROT_ALL
& ~VM_PROT_WRITE
&
2214 old_copy
->size
= copy_size
;
2217 vm_object_reference_locked(old_copy
);
2218 vm_object_unlock(old_copy
);
2219 vm_object_unlock(src_object
);
2221 if (new_copy
!= VM_OBJECT_NULL
) {
2222 vm_object_unlock(new_copy
);
2223 vm_object_deallocate(new_copy
);
2230 * Adjust the size argument so that the newly-created
2231 * copy object will be large enough to back either the
2232 * old copy object or the new mapping.
2234 if (old_copy
->size
> copy_size
)
2235 copy_size
= old_copy
->size
;
2237 if (new_copy
== VM_OBJECT_NULL
) {
2238 vm_object_unlock(old_copy
);
2239 vm_object_unlock(src_object
);
2240 new_copy
= vm_object_allocate(copy_size
);
2241 vm_object_lock(src_object
);
2242 vm_object_lock(new_copy
);
2245 new_copy
->size
= copy_size
;
2248 * The copy-object is always made large enough to
2249 * completely shadow the original object, since
2250 * it may have several users who want to shadow
2251 * the original object at different points.
2254 assert((old_copy
->shadow
== src_object
) &&
2255 (old_copy
->shadow_offset
== (vm_object_offset_t
) 0));
2257 } else if (new_copy
== VM_OBJECT_NULL
) {
2258 vm_object_unlock(src_object
);
2259 new_copy
= vm_object_allocate(copy_size
);
2260 vm_object_lock(src_object
);
2261 vm_object_lock(new_copy
);
2266 * We now have the src object locked, and the new copy object
2267 * allocated and locked (and potentially the old copy locked).
2268 * Before we go any further, make sure we can still perform
2269 * a delayed copy, as the situation may have changed.
2271 * Specifically, we can't perform a delayed copy if any of the
2272 * pages in the range are wired (because we can't safely take
2273 * write permission away from wired pages). If the pages aren't
2274 * wired, then go ahead and protect them.
2276 copy_delayed_protect_iterate
++;
2277 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
2278 if (!p
->fictitious
&& p
->offset
< copy_size
) {
2279 if (p
->wire_count
> 0) {
2281 vm_object_unlock(old_copy
);
2282 vm_object_unlock(src_object
);
2283 vm_object_unlock(new_copy
);
2284 vm_object_deallocate(new_copy
);
2285 return VM_OBJECT_NULL
;
2287 pmap_page_protect(p
->phys_page
,
2288 (VM_PROT_ALL
& ~VM_PROT_WRITE
&
2294 if (old_copy
!= VM_OBJECT_NULL
) {
2296 * Make the old copy-object shadow the new one.
2297 * It will receive no more pages from the original
2301 src_object
->ref_count
--; /* remove ref. from old_copy */
2302 assert(src_object
->ref_count
> 0);
2303 old_copy
->shadow
= new_copy
;
2304 assert(new_copy
->ref_count
> 0);
2305 new_copy
->ref_count
++; /* for old_copy->shadow ref. */
2308 if (old_copy
->res_count
) {
2309 VM_OBJ_RES_INCR(new_copy
);
2310 VM_OBJ_RES_DECR(src_object
);
2314 vm_object_unlock(old_copy
); /* done with old_copy */
2318 * Point the new copy at the existing object.
2320 new_copy
->shadow
= src_object
;
2321 new_copy
->shadow_offset
= 0;
2322 new_copy
->shadowed
= TRUE
; /* caller must set needs_copy */
2323 assert(src_object
->ref_count
> 0);
2324 src_object
->ref_count
++;
2325 VM_OBJ_RES_INCR(src_object
);
2326 src_object
->copy
= new_copy
;
2327 vm_object_unlock(src_object
);
2328 vm_object_unlock(new_copy
);
2331 "vm_object_copy_delayed: used copy object %X for source %X\n",
2332 (integer_t
)new_copy
, (integer_t
)src_object
, 0, 0, 0);
2338 * Routine: vm_object_copy_strategically
2341 * Perform a copy according to the source object's
2342 * declared strategy. This operation may block,
2343 * and may be interrupted.
2345 __private_extern__ kern_return_t
2346 vm_object_copy_strategically(
2347 register vm_object_t src_object
,
2348 vm_object_offset_t src_offset
,
2349 vm_object_size_t size
,
2350 vm_object_t
*dst_object
, /* OUT */
2351 vm_object_offset_t
*dst_offset
, /* OUT */
2352 boolean_t
*dst_needs_copy
) /* OUT */
2355 boolean_t interruptible
= THREAD_ABORTSAFE
; /* XXX */
2356 memory_object_copy_strategy_t copy_strategy
;
2358 assert(src_object
!= VM_OBJECT_NULL
);
2360 vm_object_lock(src_object
);
2363 * The copy strategy is only valid if the memory manager
2364 * is "ready". Internal objects are always ready.
2367 while (!src_object
->internal
&& !src_object
->pager_ready
) {
2368 wait_result_t wait_result
;
2370 wait_result
= vm_object_sleep( src_object
,
2371 VM_OBJECT_EVENT_PAGER_READY
,
2373 if (wait_result
!= THREAD_AWAKENED
) {
2374 vm_object_unlock(src_object
);
2375 *dst_object
= VM_OBJECT_NULL
;
2377 *dst_needs_copy
= FALSE
;
2378 return(MACH_SEND_INTERRUPTED
);
2382 copy_strategy
= src_object
->copy_strategy
;
2385 * Use the appropriate copy strategy.
2388 switch (copy_strategy
) {
2389 case MEMORY_OBJECT_COPY_DELAY
:
2390 *dst_object
= vm_object_copy_delayed(src_object
,
2392 if (*dst_object
!= VM_OBJECT_NULL
) {
2393 *dst_offset
= src_offset
;
2394 *dst_needs_copy
= TRUE
;
2395 result
= KERN_SUCCESS
;
2398 vm_object_lock(src_object
);
2399 /* fall thru when delayed copy not allowed */
2401 case MEMORY_OBJECT_COPY_NONE
:
2402 result
= vm_object_copy_slowly(src_object
, src_offset
, size
,
2403 interruptible
, dst_object
);
2404 if (result
== KERN_SUCCESS
) {
2406 *dst_needs_copy
= FALSE
;
2410 case MEMORY_OBJECT_COPY_CALL
:
2411 result
= vm_object_copy_call(src_object
, src_offset
, size
,
2413 if (result
== KERN_SUCCESS
) {
2414 *dst_offset
= src_offset
;
2415 *dst_needs_copy
= TRUE
;
2419 case MEMORY_OBJECT_COPY_SYMMETRIC
:
2420 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);
2421 vm_object_unlock(src_object
);
2422 result
= KERN_MEMORY_RESTART_COPY
;
2426 panic("copy_strategically: bad strategy");
2427 result
= KERN_INVALID_ARGUMENT
;
2435 * Create a new object which is backed by the
2436 * specified existing object range. The source
2437 * object reference is deallocated.
2439 * The new object and offset into that object
2440 * are returned in the source parameters.
2442 boolean_t vm_object_shadow_check
= FALSE
;
2444 __private_extern__ boolean_t
2446 vm_object_t
*object
, /* IN/OUT */
2447 vm_object_offset_t
*offset
, /* IN/OUT */
2448 vm_object_size_t length
)
2450 register vm_object_t source
;
2451 register vm_object_t result
;
2454 assert(source
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
);
2457 * Determine if we really need a shadow.
2460 if (vm_object_shadow_check
&& source
->ref_count
== 1 &&
2461 (source
->shadow
== VM_OBJECT_NULL
||
2462 source
->shadow
->copy
== VM_OBJECT_NULL
))
2464 source
->shadowed
= FALSE
;
2469 * Allocate a new object with the given length
2472 if ((result
= vm_object_allocate(length
)) == VM_OBJECT_NULL
)
2473 panic("vm_object_shadow: no object for shadowing");
2476 * The new object shadows the source object, adding
2477 * a reference to it. Our caller changes his reference
2478 * to point to the new object, removing a reference to
2479 * the source object. Net result: no change of reference
2482 result
->shadow
= source
;
2485 * Store the offset into the source object,
2486 * and fix up the offset into the new object.
2489 result
->shadow_offset
= *offset
;
2492 * Return the new things
2501 * The relationship between vm_object structures and
2502 * the memory_object requires careful synchronization.
2504 * All associations are created by memory_object_create_named
2505 * for external pagers and vm_object_pager_create for internal
2506 * objects as follows:
2508 * pager: the memory_object itself, supplied by
2509 * the user requesting a mapping (or the kernel,
2510 * when initializing internal objects); the
2511 * kernel simulates holding send rights by keeping
2515 * the memory object control port,
2516 * created by the kernel; the kernel holds
2517 * receive (and ownership) rights to this
2518 * port, but no other references.
2520 * When initialization is complete, the "initialized" field
2521 * is asserted. Other mappings using a particular memory object,
2522 * and any references to the vm_object gained through the
2523 * port association must wait for this initialization to occur.
2525 * In order to allow the memory manager to set attributes before
2526 * requests (notably virtual copy operations, but also data or
2527 * unlock requests) are made, a "ready" attribute is made available.
2528 * Only the memory manager may affect the value of this attribute.
2529 * Its value does not affect critical kernel functions, such as
2530 * internal object initialization or destruction. [Furthermore,
2531 * memory objects created by the kernel are assumed to be ready
2532 * immediately; the default memory manager need not explicitly
2533 * set the "ready" attribute.]
2535 * [Both the "initialized" and "ready" attribute wait conditions
2536 * use the "pager" field as the wait event.]
2538 * The port associations can be broken down by any of the
2539 * following routines:
2540 * vm_object_terminate:
2541 * No references to the vm_object remain, and
2542 * the object cannot (or will not) be cached.
2543 * This is the normal case, and is done even
2544 * though one of the other cases has already been
2546 * memory_object_destroy:
2547 * The memory manager has requested that the
2548 * kernel relinquish references to the memory
2549 * object. [The memory manager may not want to
2550 * destroy the memory object, but may wish to
2551 * refuse or tear down existing memory mappings.]
2553 * Each routine that breaks an association must break all of
2554 * them at once. At some later time, that routine must clear
2555 * the pager field and release the memory object references.
2556 * [Furthermore, each routine must cope with the simultaneous
2557 * or previous operations of the others.]
2559 * In addition to the lock on the object, the vm_object_cache_lock
2560 * governs the associations. References gained through the
2561 * association require use of the cache lock.
2563 * Because the pager field may be cleared spontaneously, it
2564 * cannot be used to determine whether a memory object has
2565 * ever been associated with a particular vm_object. [This
2566 * knowledge is important to the shadow object mechanism.]
2567 * For this reason, an additional "created" attribute is
2570 * During various paging operations, the pager reference found in the
2571 * vm_object must be valid. To prevent this from being released,
2572 * (other than being removed, i.e., made null), routines may use
2573 * the vm_object_paging_begin/end routines [actually, macros].
2574 * The implementation uses the "paging_in_progress" and "wanted" fields.
2575 * [Operations that alter the validity of the pager values include the
2576 * termination routines and vm_object_collapse.]
2580 static void vm_object_abort_activity(
2581 vm_object_t object
);
2584 * Routine: vm_object_abort_activity [internal use only]
2586 * Abort paging requests pending on this object.
2587 * In/out conditions:
2588 * The object is locked on entry and exit.
2591 vm_object_abort_activity(
2598 XPR(XPR_VM_OBJECT
, "vm_object_abort_activity, object 0x%X\n",
2599 (integer_t
)object
, 0, 0, 0, 0);
2602 * Abort all activity that would be waiting
2603 * for a result on this memory object.
2605 * We could also choose to destroy all pages
2606 * that we have in memory for this object, but
2610 p
= (vm_page_t
) queue_first(&object
->memq
);
2611 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
2612 next
= (vm_page_t
) queue_next(&p
->listq
);
2615 * If it's being paged in, destroy it.
2616 * If an unlock has been requested, start it again.
2619 if (p
->busy
&& p
->absent
) {
2623 if (p
->unlock_request
!= VM_PROT_NONE
)
2624 p
->unlock_request
= VM_PROT_NONE
;
2632 * Wake up threads waiting for the memory object to
2636 object
->pager_ready
= TRUE
;
2637 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
2641 * Routine: vm_object_pager_dead
2644 * A port is being destroy, and the IPC kobject code
2645 * can't tell if it represents a pager port or not.
2646 * So this function is called each time it sees a port
2648 * THIS IS HORRIBLY INEFFICIENT. We should only call
2649 * this routine if we had requested a notification on
2653 __private_extern__
void
2654 vm_object_pager_dead(
2658 vm_object_hash_entry_t entry
;
2661 * Perform essentially the same operations as in vm_object_lookup,
2662 * except that this time we look up based on the memory_object
2663 * port, not the control port.
2665 vm_object_cache_lock();
2666 entry
= vm_object_hash_lookup(pager
, FALSE
);
2667 if (entry
== VM_OBJECT_HASH_ENTRY_NULL
||
2668 entry
->object
== VM_OBJECT_NULL
) {
2669 vm_object_cache_unlock();
2673 object
= entry
->object
;
2674 entry
->object
= VM_OBJECT_NULL
;
2676 vm_object_lock(object
);
2677 if (object
->ref_count
== 0) {
2678 XPR(XPR_VM_OBJECT_CACHE
,
2679 "vm_object_destroy: removing %x from cache, head (%x, %x)\n",
2681 (integer_t
)vm_object_cached_list
.next
,
2682 (integer_t
)vm_object_cached_list
.prev
, 0,0);
2684 queue_remove(&vm_object_cached_list
, object
,
2685 vm_object_t
, cached_list
);
2686 vm_object_cached_count
--;
2688 object
->ref_count
++;
2689 vm_object_res_reference(object
);
2691 object
->can_persist
= FALSE
;
2693 assert(object
->pager
== pager
);
2696 * Remove the pager association.
2698 * Note that the memory_object itself is dead, so
2699 * we don't bother with it.
2702 object
->pager
= MEMORY_OBJECT_NULL
;
2704 vm_object_unlock(object
);
2705 vm_object_cache_unlock();
2707 vm_object_pager_wakeup(pager
);
2710 * Release the pager reference. Note that there's no
2711 * point in trying the memory_object_terminate call
2712 * because the memory_object itself is dead. Also
2713 * release the memory_object_control reference, since
2714 * the pager didn't do that either.
2717 memory_object_deallocate(pager
);
2718 memory_object_control_deallocate(object
->pager_request
);
2722 * Restart pending page requests
2724 vm_object_lock(object
);
2725 vm_object_abort_activity(object
);
2726 vm_object_unlock(object
);
2729 * Lose the object reference.
2732 vm_object_deallocate(object
);
2737 * Routine: vm_object_enter
2739 * Find a VM object corresponding to the given
2740 * pager; if no such object exists, create one,
2741 * and initialize the pager.
2745 memory_object_t pager
,
2746 vm_object_size_t size
,
2751 register vm_object_t object
;
2752 vm_object_t new_object
;
2753 boolean_t must_init
;
2754 vm_object_hash_entry_t entry
, new_entry
;
2756 if (pager
== MEMORY_OBJECT_NULL
)
2757 return(vm_object_allocate(size
));
2759 new_object
= VM_OBJECT_NULL
;
2760 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
2764 * Look for an object associated with this port.
2767 vm_object_cache_lock();
2769 entry
= vm_object_hash_lookup(pager
, FALSE
);
2771 if (entry
== VM_OBJECT_HASH_ENTRY_NULL
) {
2772 if (new_object
== VM_OBJECT_NULL
) {
2774 * We must unlock to create a new object;
2775 * if we do so, we must try the lookup again.
2777 vm_object_cache_unlock();
2778 assert(new_entry
== VM_OBJECT_HASH_ENTRY_NULL
);
2779 new_entry
= vm_object_hash_entry_alloc(pager
);
2780 new_object
= vm_object_allocate(size
);
2781 vm_object_cache_lock();
2784 * Lookup failed twice, and we have something
2785 * to insert; set the object.
2787 vm_object_hash_insert(new_entry
);
2789 entry
->object
= new_object
;
2790 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
2791 new_object
= VM_OBJECT_NULL
;
2794 } else if (entry
->object
== VM_OBJECT_NULL
) {
2796 * If a previous object is being terminated,
2797 * we must wait for the termination message
2798 * to be queued (and lookup the entry again).
2800 entry
->waiting
= TRUE
;
2801 entry
= VM_OBJECT_HASH_ENTRY_NULL
;
2802 assert_wait((event_t
) pager
, THREAD_UNINT
);
2803 vm_object_cache_unlock();
2804 thread_block(THREAD_CONTINUE_NULL
);
2805 vm_object_cache_lock();
2807 } while (entry
== VM_OBJECT_HASH_ENTRY_NULL
);
2809 object
= entry
->object
;
2810 assert(object
!= VM_OBJECT_NULL
);
2813 vm_object_lock(object
);
2814 assert(!internal
|| object
->internal
);
2816 assert(!object
->named
);
2817 object
->named
= TRUE
;
2819 if (object
->ref_count
== 0) {
2820 XPR(XPR_VM_OBJECT_CACHE
,
2821 "vm_object_enter: removing %x from cache, head (%x, %x)\n",
2823 (integer_t
)vm_object_cached_list
.next
,
2824 (integer_t
)vm_object_cached_list
.prev
, 0,0);
2825 queue_remove(&vm_object_cached_list
, object
,
2826 vm_object_t
, cached_list
);
2827 vm_object_cached_count
--;
2829 object
->ref_count
++;
2830 vm_object_res_reference(object
);
2831 vm_object_unlock(object
);
2835 assert(object
->ref_count
> 0);
2839 vm_object_cache_unlock();
2842 "vm_o_enter: pager 0x%x obj 0x%x must_init %d\n",
2843 (integer_t
)pager
, (integer_t
)object
, must_init
, 0, 0);
2846 * If we raced to create a vm_object but lost, let's
2850 if (new_object
!= VM_OBJECT_NULL
)
2851 vm_object_deallocate(new_object
);
2853 if (new_entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
2854 vm_object_hash_entry_free(new_entry
);
2857 memory_object_control_t control
;
2860 * Allocate request port.
2863 control
= memory_object_control_allocate(object
);
2864 assert (control
!= MEMORY_OBJECT_CONTROL_NULL
);
2866 vm_object_lock(object
);
2867 assert(object
!= kernel_object
);
2870 * Copy the reference we were given.
2873 memory_object_reference(pager
);
2874 object
->pager_created
= TRUE
;
2875 object
->pager
= pager
;
2876 object
->internal
= internal
;
2877 object
->pager_trusted
= internal
;
2879 /* copy strategy invalid until set by memory manager */
2880 object
->copy_strategy
= MEMORY_OBJECT_COPY_INVALID
;
2882 object
->pager_control
= control
;
2883 object
->pager_ready
= FALSE
;
2885 vm_object_unlock(object
);
2888 * Let the pager know we're using it.
2891 (void) memory_object_init(pager
,
2892 object
->pager_control
,
2895 vm_object_lock(object
);
2897 object
->named
= TRUE
;
2899 object
->pager_ready
= TRUE
;
2900 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
2903 object
->pager_initialized
= TRUE
;
2904 vm_object_wakeup(object
, VM_OBJECT_EVENT_INITIALIZED
);
2906 vm_object_lock(object
);
2910 * [At this point, the object must be locked]
2914 * Wait for the work above to be done by the first
2915 * thread to map this object.
2918 while (!object
->pager_initialized
) {
2919 vm_object_sleep(object
,
2920 VM_OBJECT_EVENT_INITIALIZED
,
2923 vm_object_unlock(object
);
2926 "vm_object_enter: vm_object %x, memory_object %x, internal %d\n",
2927 (integer_t
)object
, (integer_t
)object
->pager
, internal
, 0,0);
2932 * Routine: vm_object_pager_create
2934 * Create a memory object for an internal object.
2935 * In/out conditions:
2936 * The object is locked on entry and exit;
2937 * it may be unlocked within this call.
2939 * Only one thread may be performing a
2940 * vm_object_pager_create on an object at
2941 * a time. Presumably, only the pageout
2942 * daemon will be using this routine.
2946 vm_object_pager_create(
2947 register vm_object_t object
)
2949 memory_object_t pager
;
2950 vm_object_hash_entry_t entry
;
2952 vm_object_size_t size
;
2953 vm_external_map_t map
;
2954 #endif /* MACH_PAGEMAP */
2956 XPR(XPR_VM_OBJECT
, "vm_object_pager_create, object 0x%X\n",
2957 (integer_t
)object
, 0,0,0,0);
2959 assert(object
!= kernel_object
);
2961 if (memory_manager_default_check() != KERN_SUCCESS
)
2965 * Prevent collapse or termination by holding a paging reference
2968 vm_object_paging_begin(object
);
2969 if (object
->pager_created
) {
2971 * Someone else got to it first...
2972 * wait for them to finish initializing the ports
2974 while (!object
->pager_initialized
) {
2975 vm_object_sleep(object
,
2976 VM_OBJECT_EVENT_INITIALIZED
,
2979 vm_object_paging_end(object
);
2984 * Indicate that a memory object has been assigned
2985 * before dropping the lock, to prevent a race.
2988 object
->pager_created
= TRUE
;
2989 object
->paging_offset
= 0;
2992 size
= object
->size
;
2993 #endif /* MACH_PAGEMAP */
2994 vm_object_unlock(object
);
2997 map
= vm_external_create(size
);
2998 vm_object_lock(object
);
2999 assert(object
->size
== size
);
3000 object
->existence_map
= map
;
3001 vm_object_unlock(object
);
3002 #endif /* MACH_PAGEMAP */
3005 * Create the [internal] pager, and associate it with this object.
3007 * We make the association here so that vm_object_enter()
3008 * can look up the object to complete initializing it. No
3009 * user will ever map this object.
3012 memory_object_default_t dmm
;
3013 vm_size_t cluster_size
;
3015 /* acquire a reference for the default memory manager */
3016 dmm
= memory_manager_default_reference(&cluster_size
);
3017 assert(cluster_size
>= PAGE_SIZE
);
3019 object
->cluster_size
= cluster_size
; /* XXX ??? */
3020 assert(object
->temporary
);
3022 /* create our new memory object */
3023 (void) memory_object_create(dmm
, object
->size
, &pager
);
3025 memory_object_default_deallocate(dmm
);
3028 entry
= vm_object_hash_entry_alloc(pager
);
3030 vm_object_cache_lock();
3031 vm_object_hash_insert(entry
);
3033 entry
->object
= object
;
3034 vm_object_cache_unlock();
3037 * A reference was returned by
3038 * memory_object_create(), and it is
3039 * copied by vm_object_enter().
3042 if (vm_object_enter(pager
, object
->size
, TRUE
, TRUE
, FALSE
) != object
)
3043 panic("vm_object_pager_create: mismatch");
3046 * Drop the reference we were passed.
3048 memory_object_deallocate(pager
);
3050 vm_object_lock(object
);
3053 * Release the paging reference
3055 vm_object_paging_end(object
);
3059 * Routine: vm_object_remove
3061 * Eliminate the pager/object association
3064 * The object cache must be locked.
3066 __private_extern__
void
3070 memory_object_t pager
;
3072 if ((pager
= object
->pager
) != MEMORY_OBJECT_NULL
) {
3073 vm_object_hash_entry_t entry
;
3075 entry
= vm_object_hash_lookup(pager
, FALSE
);
3076 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
3077 entry
->object
= VM_OBJECT_NULL
;
3083 * Global variables for vm_object_collapse():
3085 * Counts for normal collapses and bypasses.
3086 * Debugging variables, to watch or disable collapse.
3088 static long object_collapses
= 0;
3089 static long object_bypasses
= 0;
3091 static boolean_t vm_object_collapse_allowed
= TRUE
;
3092 static boolean_t vm_object_bypass_allowed
= TRUE
;
3094 static int vm_external_discarded
;
3095 static int vm_external_collapsed
;
3097 unsigned long vm_object_collapse_encrypted
= 0;
3100 * Routine: vm_object_do_collapse
3102 * Collapse an object with the object backing it.
3103 * Pages in the backing object are moved into the
3104 * parent, and the backing object is deallocated.
3106 * Both objects and the cache are locked; the page
3107 * queues are unlocked.
3111 vm_object_do_collapse(
3113 vm_object_t backing_object
)
3116 vm_object_offset_t new_offset
, backing_offset
;
3117 vm_object_size_t size
;
3119 backing_offset
= object
->shadow_offset
;
3120 size
= object
->size
;
3123 * Move all in-memory pages from backing_object
3124 * to the parent. Pages that have been paged out
3125 * will be overwritten by any of the parent's
3126 * pages that shadow them.
3129 while (!queue_empty(&backing_object
->memq
)) {
3131 p
= (vm_page_t
) queue_first(&backing_object
->memq
);
3133 new_offset
= (p
->offset
- backing_offset
);
3135 assert(!p
->busy
|| p
->absent
);
3138 * If the parent has a page here, or if
3139 * this page falls outside the parent,
3142 * Otherwise, move it as planned.
3145 if (p
->offset
< backing_offset
|| new_offset
>= size
) {
3150 * The encryption key includes the "pager" and the
3151 * "paging_offset". These might not be the same in
3152 * the new object, so we can't just move an encrypted
3153 * page from one object to the other. We can't just
3154 * decrypt the page here either, because that would drop
3156 * The caller should check for encrypted pages before
3157 * attempting to collapse.
3159 ASSERT_PAGE_DECRYPTED(p
);
3161 pp
= vm_page_lookup(object
, new_offset
);
3162 if (pp
== VM_PAGE_NULL
) {
3165 * Parent now has no page.
3166 * Move the backing object's page up.
3169 vm_page_rename(p
, object
, new_offset
);
3171 } else if (pp
->absent
) {
3174 * Parent has an absent page...
3175 * it's not being paged in, so
3176 * it must really be missing from
3179 * Throw out the absent page...
3180 * any faults looking for that
3181 * page will restart with the new
3186 vm_page_rename(p
, object
, new_offset
);
3187 #endif /* MACH_PAGEMAP */
3189 assert(! pp
->absent
);
3192 * Parent object has a real page.
3193 * Throw away the backing object's
3202 assert(!object
->pager_created
&& object
->pager
== MEMORY_OBJECT_NULL
3203 || (!backing_object
->pager_created
3204 && backing_object
->pager
== MEMORY_OBJECT_NULL
));
3206 assert(!object
->pager_created
&& object
->pager
== MEMORY_OBJECT_NULL
);
3207 #endif /* !MACH_PAGEMAP */
3209 if (backing_object
->pager
!= MEMORY_OBJECT_NULL
) {
3210 vm_object_hash_entry_t entry
;
3213 * Move the pager from backing_object to object.
3215 * XXX We're only using part of the paging space
3216 * for keeps now... we ought to discard the
3220 assert(!object
->paging_in_progress
);
3221 object
->pager
= backing_object
->pager
;
3222 entry
= vm_object_hash_lookup(object
->pager
, FALSE
);
3223 assert(entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
3224 entry
->object
= object
;
3225 object
->pager_created
= backing_object
->pager_created
;
3226 object
->pager_control
= backing_object
->pager_control
;
3227 object
->pager_ready
= backing_object
->pager_ready
;
3228 object
->pager_initialized
= backing_object
->pager_initialized
;
3229 object
->cluster_size
= backing_object
->cluster_size
;
3230 object
->paging_offset
=
3231 backing_object
->paging_offset
+ backing_offset
;
3232 if (object
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
3233 memory_object_control_collapse(object
->pager_control
,
3238 vm_object_cache_unlock();
3242 * If the shadow offset is 0, the use the existence map from
3243 * the backing object if there is one. If the shadow offset is
3244 * not zero, toss it.
3246 * XXX - If the shadow offset is not 0 then a bit copy is needed
3247 * if the map is to be salvaged. For now, we just just toss the
3248 * old map, giving the collapsed object no map. This means that
3249 * the pager is invoked for zero fill pages. If analysis shows
3250 * that this happens frequently and is a performance hit, then
3251 * this code should be fixed to salvage the map.
3253 assert(object
->existence_map
== VM_EXTERNAL_NULL
);
3254 if (backing_offset
|| (size
!= backing_object
->size
)) {
3255 vm_external_discarded
++;
3256 vm_external_destroy(backing_object
->existence_map
,
3257 backing_object
->size
);
3260 vm_external_collapsed
++;
3261 object
->existence_map
= backing_object
->existence_map
;
3263 backing_object
->existence_map
= VM_EXTERNAL_NULL
;
3264 #endif /* MACH_PAGEMAP */
3267 * Object now shadows whatever backing_object did.
3268 * Note that the reference to backing_object->shadow
3269 * moves from within backing_object to within object.
3272 assert(!object
->phys_contiguous
);
3273 assert(!backing_object
->phys_contiguous
);
3274 object
->shadow
= backing_object
->shadow
;
3275 if (object
->shadow
) {
3276 object
->shadow_offset
+= backing_object
->shadow_offset
;
3278 /* no shadow, therefore no shadow offset... */
3279 object
->shadow_offset
= 0;
3281 assert((object
->shadow
== VM_OBJECT_NULL
) ||
3282 (object
->shadow
->copy
!= backing_object
));
3285 * Discard backing_object.
3287 * Since the backing object has no pages, no
3288 * pager left, and no object references within it,
3289 * all that is necessary is to dispose of it.
3292 assert((backing_object
->ref_count
== 1) &&
3293 (backing_object
->resident_page_count
== 0) &&
3294 (backing_object
->paging_in_progress
== 0));
3296 backing_object
->alive
= FALSE
;
3297 vm_object_unlock(backing_object
);
3299 XPR(XPR_VM_OBJECT
, "vm_object_collapse, collapsed 0x%X\n",
3300 (integer_t
)backing_object
, 0,0,0,0);
3302 zfree(vm_object_zone
, backing_object
);
3308 vm_object_do_bypass(
3310 vm_object_t backing_object
)
3313 * Make the parent shadow the next object
3319 * Do object reference in-line to
3320 * conditionally increment shadow's
3321 * residence count. If object is not
3322 * resident, leave residence count
3325 if (backing_object
->shadow
!= VM_OBJECT_NULL
) {
3326 vm_object_lock(backing_object
->shadow
);
3327 backing_object
->shadow
->ref_count
++;
3328 if (object
->res_count
!= 0)
3329 vm_object_res_reference(backing_object
->shadow
);
3330 vm_object_unlock(backing_object
->shadow
);
3332 #else /* TASK_SWAPPER */
3333 vm_object_reference(backing_object
->shadow
);
3334 #endif /* TASK_SWAPPER */
3336 assert(!object
->phys_contiguous
);
3337 assert(!backing_object
->phys_contiguous
);
3338 object
->shadow
= backing_object
->shadow
;
3339 if (object
->shadow
) {
3340 object
->shadow_offset
+= backing_object
->shadow_offset
;
3342 /* no shadow, therefore no shadow offset... */
3343 object
->shadow_offset
= 0;
3347 * Backing object might have had a copy pointer
3348 * to us. If it did, clear it.
3350 if (backing_object
->copy
== object
) {
3351 backing_object
->copy
= VM_OBJECT_NULL
;
3355 * Drop the reference count on backing_object.
3357 * Since its ref_count was at least 2, it
3358 * will not vanish; so we don't need to call
3359 * vm_object_deallocate.
3360 * [FBDP: that doesn't seem to be true any more]
3362 * The res_count on the backing object is
3363 * conditionally decremented. It's possible
3364 * (via vm_pageout_scan) to get here with
3365 * a "swapped" object, which has a 0 res_count,
3366 * in which case, the backing object res_count
3367 * is already down by one.
3369 * Don't call vm_object_deallocate unless
3370 * ref_count drops to zero.
3372 * The ref_count can drop to zero here if the
3373 * backing object could be bypassed but not
3374 * collapsed, such as when the backing object
3375 * is temporary and cachable.
3378 if (backing_object
->ref_count
> 1) {
3379 backing_object
->ref_count
--;
3381 if (object
->res_count
!= 0)
3382 vm_object_res_deallocate(backing_object
);
3383 assert(backing_object
->ref_count
> 0);
3384 #endif /* TASK_SWAPPER */
3385 vm_object_unlock(backing_object
);
3389 * Drop locks so that we can deallocate
3390 * the backing object.
3394 if (object
->res_count
== 0) {
3395 /* XXX get a reference for the deallocate below */
3396 vm_object_res_reference(backing_object
);
3398 #endif /* TASK_SWAPPER */
3399 vm_object_unlock(object
);
3400 vm_object_unlock(backing_object
);
3401 vm_object_deallocate(backing_object
);
3404 * Relock object. We don't have to reverify
3405 * its state since vm_object_collapse will
3406 * do that for us as it starts at the
3410 vm_object_lock(object
);
3418 * vm_object_collapse:
3420 * Perform an object collapse or an object bypass if appropriate.
3421 * The real work of collapsing and bypassing is performed in
3422 * the routines vm_object_do_collapse and vm_object_do_bypass.
3424 * Requires that the object be locked and the page queues be unlocked.
3427 static unsigned long vm_object_collapse_calls
= 0;
3428 static unsigned long vm_object_collapse_objects
= 0;
3429 static unsigned long vm_object_collapse_do_collapse
= 0;
3430 static unsigned long vm_object_collapse_do_bypass
= 0;
3431 __private_extern__
void
3433 register vm_object_t object
,
3434 register vm_object_offset_t hint_offset
,
3435 boolean_t can_bypass
)
3437 register vm_object_t backing_object
;
3438 register unsigned int rcount
;
3439 register unsigned int size
;
3440 vm_object_offset_t collapse_min_offset
;
3441 vm_object_offset_t collapse_max_offset
;
3443 vm_object_t original_object
;
3445 vm_object_collapse_calls
++;
3447 if (! vm_object_collapse_allowed
&&
3448 ! (can_bypass
&& vm_object_bypass_allowed
)) {
3452 XPR(XPR_VM_OBJECT
, "vm_object_collapse, obj 0x%X\n",
3453 (integer_t
)object
, 0,0,0,0);
3455 if (object
== VM_OBJECT_NULL
)
3458 original_object
= object
;
3461 vm_object_collapse_objects
++;
3463 * Verify that the conditions are right for either
3464 * collapse or bypass:
3468 * There is a backing object, and
3471 backing_object
= object
->shadow
;
3472 if (backing_object
== VM_OBJECT_NULL
) {
3473 if (object
!= original_object
) {
3474 vm_object_unlock(object
);
3480 * No pages in the object are currently
3481 * being paged out, and
3483 if (object
->paging_in_progress
!= 0 ||
3484 object
->absent_count
!= 0) {
3485 /* try and collapse the rest of the shadow chain */
3486 vm_object_lock(backing_object
);
3487 if (object
!= original_object
) {
3488 vm_object_unlock(object
);
3490 object
= backing_object
;
3494 vm_object_lock(backing_object
);
3498 * The backing object is not read_only,
3499 * and no pages in the backing object are
3500 * currently being paged out.
3501 * The backing object is internal.
3505 if (!backing_object
->internal
||
3506 backing_object
->paging_in_progress
!= 0) {
3507 /* try and collapse the rest of the shadow chain */
3508 if (object
!= original_object
) {
3509 vm_object_unlock(object
);
3511 object
= backing_object
;
3516 * The backing object can't be a copy-object:
3517 * the shadow_offset for the copy-object must stay
3518 * as 0. Furthermore (for the 'we have all the
3519 * pages' case), if we bypass backing_object and
3520 * just shadow the next object in the chain, old
3521 * pages from that object would then have to be copied
3522 * BOTH into the (former) backing_object and into the
3525 if (backing_object
->shadow
!= VM_OBJECT_NULL
&&
3526 backing_object
->shadow
->copy
== backing_object
) {
3527 /* try and collapse the rest of the shadow chain */
3528 if (object
!= original_object
) {
3529 vm_object_unlock(object
);
3531 object
= backing_object
;
3536 * We can now try to either collapse the backing
3537 * object (if the parent is the only reference to
3538 * it) or (perhaps) remove the parent's reference
3541 * If there is exactly one reference to the backing
3542 * object, we may be able to collapse it into the
3545 * If MACH_PAGEMAP is defined:
3546 * The parent must not have a pager created for it,
3547 * since collapsing a backing_object dumps new pages
3548 * into the parent that its pager doesn't know about
3549 * (and the collapse code can't merge the existence
3552 * As long as one of the objects is still not known
3553 * to the pager, we can collapse them.
3555 if (backing_object
->ref_count
== 1 &&
3556 (!object
->pager_created
3558 || !backing_object
->pager_created
3559 #endif /*!MACH_PAGEMAP */
3560 ) && vm_object_collapse_allowed
) {
3563 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
3564 (integer_t
)backing_object
, (integer_t
)object
,
3565 (integer_t
)backing_object
->pager
,
3566 (integer_t
)backing_object
->pager_control
, 0);
3569 * We need the cache lock for collapsing,
3570 * but we must not deadlock.
3573 if (! vm_object_cache_lock_try()) {
3574 if (object
!= original_object
) {
3575 vm_object_unlock(object
);
3577 vm_object_unlock(backing_object
);
3583 * We can't collapse the object if it contains
3584 * any encypted page, because the encryption key
3585 * includes the <object,offset> info. We can't
3586 * drop the object lock in vm_object_do_collapse()
3587 * so we can't decrypt the page there either.
3589 if (vm_pages_encrypted
) {
3590 collapse_min_offset
= object
->shadow_offset
;
3591 collapse_max_offset
=
3592 object
->shadow_offset
+ object
->size
;
3593 queue_iterate(&backing_object
->memq
,
3594 page
, vm_page_t
, listq
) {
3595 if (page
->encrypted
&&
3597 collapse_min_offset
) &&
3599 collapse_max_offset
)) {
3601 * We found an encrypted page
3602 * in the backing object,
3603 * within the range covered
3604 * by the parent object: we can
3605 * not collapse them.
3607 vm_object_collapse_encrypted
++;
3608 vm_object_cache_unlock();
3615 * Collapse the object with its backing
3616 * object, and try again with the object's
3617 * new backing object.
3620 vm_object_do_collapse(object
, backing_object
);
3621 vm_object_collapse_do_collapse
++;
3627 * Collapsing the backing object was not possible
3628 * or permitted, so let's try bypassing it.
3631 if (! (can_bypass
&& vm_object_bypass_allowed
)) {
3632 /* try and collapse the rest of the shadow chain */
3633 if (object
!= original_object
) {
3634 vm_object_unlock(object
);
3636 object
= backing_object
;
3642 * If the object doesn't have all its pages present,
3643 * we have to make sure no pages in the backing object
3644 * "show through" before bypassing it.
3646 size
= atop(object
->size
);
3647 rcount
= object
->resident_page_count
;
3648 if (rcount
!= size
) {
3649 vm_object_offset_t offset
;
3650 vm_object_offset_t backing_offset
;
3651 unsigned int backing_rcount
;
3652 unsigned int lookups
= 0;
3655 * If the backing object has a pager but no pagemap,
3656 * then we cannot bypass it, because we don't know
3657 * what pages it has.
3659 if (backing_object
->pager_created
3661 && (backing_object
->existence_map
== VM_EXTERNAL_NULL
)
3662 #endif /* MACH_PAGEMAP */
3664 /* try and collapse the rest of the shadow chain */
3665 if (object
!= original_object
) {
3666 vm_object_unlock(object
);
3668 object
= backing_object
;
3673 * If the object has a pager but no pagemap,
3674 * then we cannot bypass it, because we don't know
3675 * what pages it has.
3677 if (object
->pager_created
3679 && (object
->existence_map
== VM_EXTERNAL_NULL
)
3680 #endif /* MACH_PAGEMAP */
3682 /* try and collapse the rest of the shadow chain */
3683 if (object
!= original_object
) {
3684 vm_object_unlock(object
);
3686 object
= backing_object
;
3691 * If all of the pages in the backing object are
3692 * shadowed by the parent object, the parent
3693 * object no longer has to shadow the backing
3694 * object; it can shadow the next one in the
3697 * If the backing object has existence info,
3698 * we must check examine its existence info
3703 backing_offset
= object
->shadow_offset
;
3704 backing_rcount
= backing_object
->resident_page_count
;
3706 #define EXISTS_IN_OBJECT(obj, off, rc) \
3707 (vm_external_state_get((obj)->existence_map, \
3708 (vm_offset_t)(off)) == VM_EXTERNAL_STATE_EXISTS || \
3709 ((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
3712 * Check the hint location first
3713 * (since it is often the quickest way out of here).
3715 if (object
->cow_hint
!= ~(vm_offset_t
)0)
3716 hint_offset
= (vm_object_offset_t
)object
->cow_hint
;
3718 hint_offset
= (hint_offset
> 8 * PAGE_SIZE_64
) ?
3719 (hint_offset
- 8 * PAGE_SIZE_64
) : 0;
3721 if (EXISTS_IN_OBJECT(backing_object
, hint_offset
+
3722 backing_offset
, backing_rcount
) &&
3723 !EXISTS_IN_OBJECT(object
, hint_offset
, rcount
)) {
3724 /* dependency right at the hint */
3725 object
->cow_hint
= (vm_offset_t
)hint_offset
;
3726 /* try and collapse the rest of the shadow chain */
3727 if (object
!= original_object
) {
3728 vm_object_unlock(object
);
3730 object
= backing_object
;
3735 * If the object's window onto the backing_object
3736 * is large compared to the number of resident
3737 * pages in the backing object, it makes sense to
3738 * walk the backing_object's resident pages first.
3740 * NOTE: Pages may be in both the existence map and
3741 * resident. So, we can't permanently decrement
3742 * the rcount here because the second loop may
3743 * find the same pages in the backing object'
3744 * existence map that we found here and we would
3745 * double-decrement the rcount. We also may or
3746 * may not have found the
3748 if (backing_rcount
&& size
>
3749 ((backing_object
->existence_map
) ?
3750 backing_rcount
: (backing_rcount
>> 1))) {
3751 unsigned int rc
= rcount
;
3754 backing_rcount
= backing_object
->resident_page_count
;
3755 p
= (vm_page_t
)queue_first(&backing_object
->memq
);
3757 /* Until we get more than one lookup lock */
3758 if (lookups
> 256) {
3763 offset
= (p
->offset
- backing_offset
);
3764 if (offset
< object
->size
&&
3765 offset
!= hint_offset
&&
3766 !EXISTS_IN_OBJECT(object
, offset
, rc
)) {
3767 /* found a dependency */
3768 object
->cow_hint
= (vm_offset_t
)offset
;
3771 p
= (vm_page_t
) queue_next(&p
->listq
);
3773 } while (--backing_rcount
);
3774 if (backing_rcount
!= 0 ) {
3775 /* try and collapse the rest of the shadow chain */
3776 if (object
!= original_object
) {
3777 vm_object_unlock(object
);
3779 object
= backing_object
;
3785 * Walk through the offsets looking for pages in the
3786 * backing object that show through to the object.
3788 if (backing_rcount
|| backing_object
->existence_map
) {
3789 offset
= hint_offset
;
3792 (offset
+ PAGE_SIZE_64
< object
->size
) ?
3793 (offset
+ PAGE_SIZE_64
) : 0) != hint_offset
) {
3795 /* Until we get more than one lookup lock */
3796 if (lookups
> 256) {
3801 if (EXISTS_IN_OBJECT(backing_object
, offset
+
3802 backing_offset
, backing_rcount
) &&
3803 !EXISTS_IN_OBJECT(object
, offset
, rcount
)) {
3804 /* found a dependency */
3805 object
->cow_hint
= (vm_offset_t
)offset
;
3809 if (offset
!= hint_offset
) {
3810 /* try and collapse the rest of the shadow chain */
3811 if (object
!= original_object
) {
3812 vm_object_unlock(object
);
3814 object
= backing_object
;
3820 /* reset the offset hint for any objects deeper in the chain */
3821 object
->cow_hint
= (vm_offset_t
)0;
3824 * All interesting pages in the backing object
3825 * already live in the parent or its pager.
3826 * Thus we can bypass the backing object.
3829 vm_object_do_bypass(object
, backing_object
);
3830 vm_object_collapse_do_bypass
++;
3833 * Try again with this object's new backing object.
3839 if (object
!= original_object
) {
3840 vm_object_unlock(object
);
3845 * Routine: vm_object_page_remove: [internal]
3847 * Removes all physical pages in the specified
3848 * object range from the object's list of pages.
3850 * In/out conditions:
3851 * The object must be locked.
3852 * The object must not have paging_in_progress, usually
3853 * guaranteed by not having a pager.
3855 unsigned int vm_object_page_remove_lookup
= 0;
3856 unsigned int vm_object_page_remove_iterate
= 0;
3858 __private_extern__
void
3859 vm_object_page_remove(
3860 register vm_object_t object
,
3861 register vm_object_offset_t start
,
3862 register vm_object_offset_t end
)
3864 register vm_page_t p
, next
;
3867 * One and two page removals are most popular.
3868 * The factor of 16 here is somewhat arbitrary.
3869 * It balances vm_object_lookup vs iteration.
3872 if (atop_64(end
- start
) < (unsigned)object
->resident_page_count
/16) {
3873 vm_object_page_remove_lookup
++;
3875 for (; start
< end
; start
+= PAGE_SIZE_64
) {
3876 p
= vm_page_lookup(object
, start
);
3877 if (p
!= VM_PAGE_NULL
) {
3878 assert(!p
->cleaning
&& !p
->pageout
);
3880 pmap_disconnect(p
->phys_page
);
3885 vm_object_page_remove_iterate
++;
3887 p
= (vm_page_t
) queue_first(&object
->memq
);
3888 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
3889 next
= (vm_page_t
) queue_next(&p
->listq
);
3890 if ((start
<= p
->offset
) && (p
->offset
< end
)) {
3891 assert(!p
->cleaning
&& !p
->pageout
);
3893 pmap_disconnect(p
->phys_page
);
3903 * Routine: vm_object_coalesce
3904 * Function: Coalesces two objects backing up adjoining
3905 * regions of memory into a single object.
3907 * returns TRUE if objects were combined.
3909 * NOTE: Only works at the moment if the second object is NULL -
3910 * if it's not, which object do we lock first?
3913 * prev_object First object to coalesce
3914 * prev_offset Offset into prev_object
3915 * next_object Second object into coalesce
3916 * next_offset Offset into next_object
3918 * prev_size Size of reference to prev_object
3919 * next_size Size of reference to next_object
3922 * The object(s) must *not* be locked. The map must be locked
3923 * to preserve the reference to the object(s).
3925 static int vm_object_coalesce_count
= 0;
3927 __private_extern__ boolean_t
3929 register vm_object_t prev_object
,
3930 vm_object_t next_object
,
3931 vm_object_offset_t prev_offset
,
3932 __unused vm_object_offset_t next_offset
,
3933 vm_object_size_t prev_size
,
3934 vm_object_size_t next_size
)
3936 vm_object_size_t newsize
;
3942 if (next_object
!= VM_OBJECT_NULL
) {
3946 if (prev_object
== VM_OBJECT_NULL
) {
3951 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
3952 (integer_t
)prev_object
, prev_offset
, prev_size
, next_size
, 0);
3954 vm_object_lock(prev_object
);
3957 * Try to collapse the object first
3959 vm_object_collapse(prev_object
, prev_offset
, TRUE
);
3962 * Can't coalesce if pages not mapped to
3963 * prev_entry may be in use any way:
3964 * . more than one reference
3966 * . shadows another object
3967 * . has a copy elsewhere
3969 * . paging references (pages might be in page-list)
3972 if ((prev_object
->ref_count
> 1) ||
3973 prev_object
->pager_created
||
3974 (prev_object
->shadow
!= VM_OBJECT_NULL
) ||
3975 (prev_object
->copy
!= VM_OBJECT_NULL
) ||
3976 (prev_object
->true_share
!= FALSE
) ||
3977 (prev_object
->purgable
!= VM_OBJECT_NONPURGABLE
) ||
3978 (prev_object
->paging_in_progress
!= 0)) {
3979 vm_object_unlock(prev_object
);
3983 vm_object_coalesce_count
++;
3986 * Remove any pages that may still be in the object from
3987 * a previous deallocation.
3989 vm_object_page_remove(prev_object
,
3990 prev_offset
+ prev_size
,
3991 prev_offset
+ prev_size
+ next_size
);
3994 * Extend the object if necessary.
3996 newsize
= prev_offset
+ prev_size
+ next_size
;
3997 if (newsize
> prev_object
->size
) {
4000 * We cannot extend an object that has existence info,
4001 * since the existence info might then fail to cover
4002 * the entire object.
4004 * This assertion must be true because the object
4005 * has no pager, and we only create existence info
4006 * for objects with pagers.
4008 assert(prev_object
->existence_map
== VM_EXTERNAL_NULL
);
4009 #endif /* MACH_PAGEMAP */
4010 prev_object
->size
= newsize
;
4013 vm_object_unlock(prev_object
);
4018 * Attach a set of physical pages to an object, so that they can
4019 * be mapped by mapping the object. Typically used to map IO memory.
4021 * The mapping function and its private data are used to obtain the
4022 * physical addresses for each page to be mapped.
4027 vm_object_offset_t offset
,
4028 vm_object_size_t size
,
4029 vm_object_offset_t (*map_fn
)(void *map_fn_data
,
4030 vm_object_offset_t offset
),
4031 void *map_fn_data
) /* private to map_fn */
4037 vm_object_offset_t addr
;
4039 num_pages
= atop_64(size
);
4041 for (i
= 0; i
< num_pages
; i
++, offset
+= PAGE_SIZE_64
) {
4043 addr
= (*map_fn
)(map_fn_data
, offset
);
4045 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
4046 vm_page_more_fictitious();
4048 vm_object_lock(object
);
4049 if ((old_page
= vm_page_lookup(object
, offset
))
4052 vm_page_lock_queues();
4053 vm_page_free(old_page
);
4054 vm_page_unlock_queues();
4057 vm_page_init(m
, addr
);
4058 /* private normally requires lock_queues but since we */
4059 /* are initializing the page, its not necessary here */
4060 m
->private = TRUE
; /* don`t free page */
4062 vm_page_insert(m
, object
, offset
);
4064 PAGE_WAKEUP_DONE(m
);
4065 vm_object_unlock(object
);
4069 #include <mach_kdb.h>
4072 #include <ddb/db_output.h>
4073 #include <vm/vm_print.h>
4075 #define printf kdbprintf
4077 extern boolean_t
vm_object_cached(
4078 vm_object_t object
);
4080 extern void print_bitstring(
4083 boolean_t vm_object_print_pages
= FALSE
;
4089 printf("%c%c%c%c%c%c%c%c",
4090 ((byte
& (1 << 0)) ? '1' : '0'),
4091 ((byte
& (1 << 1)) ? '1' : '0'),
4092 ((byte
& (1 << 2)) ? '1' : '0'),
4093 ((byte
& (1 << 3)) ? '1' : '0'),
4094 ((byte
& (1 << 4)) ? '1' : '0'),
4095 ((byte
& (1 << 5)) ? '1' : '0'),
4096 ((byte
& (1 << 6)) ? '1' : '0'),
4097 ((byte
& (1 << 7)) ? '1' : '0'));
4102 register vm_object_t object
)
4104 register vm_object_t o
;
4106 queue_iterate(&vm_object_cached_list
, o
, vm_object_t
, cached_list
) {
4116 * vm_external_print: [ debug ]
4120 vm_external_map_t emap
,
4123 if (emap
== VM_EXTERNAL_NULL
) {
4126 vm_size_t existence_size
= stob(size
);
4127 printf("{ size=%d, map=[", existence_size
);
4128 if (existence_size
> 0) {
4129 print_bitstring(emap
[0]);
4131 if (existence_size
> 1) {
4132 print_bitstring(emap
[1]);
4134 if (existence_size
> 2) {
4136 print_bitstring(emap
[existence_size
-1]);
4142 #endif /* MACH_PAGEMAP */
4149 int orig_db_indent
= db_indent
;
4152 if (object
== VM_OBJECT_NULL
) {
4153 db_indent
= orig_db_indent
;
4159 iprintf("object 0x%x", object
);
4160 printf(", shadow=0x%x", object
->shadow
);
4161 printf(", copy=0x%x", object
->copy
);
4162 printf(", pager=0x%x", object
->pager
);
4163 printf(", ref=%d\n", object
->ref_count
);
4166 object
= object
->shadow
;
4172 * vm_object_print: [ debug ]
4177 __unused boolean_t have_addr
,
4178 __unused
int arg_count
,
4179 __unused
char *modif
)
4182 register vm_page_t p
;
4187 object
= (vm_object_t
) (long) db_addr
;
4188 if (object
== VM_OBJECT_NULL
)
4191 iprintf("object 0x%x\n", object
);
4195 iprintf("size=0x%x", object
->size
);
4196 printf(", cluster=0x%x", object
->cluster_size
);
4197 printf(", memq_hint=%p", object
->memq_hint
);
4198 printf(", ref_count=%d\n", object
->ref_count
);
4201 printf("res_count=%d, ", object
->res_count
);
4202 #endif /* TASK_SWAPPER */
4203 printf("resident_page_count=%d\n", object
->resident_page_count
);
4205 iprintf("shadow=0x%x", object
->shadow
);
4206 if (object
->shadow
) {
4208 vm_object_t shadow
= object
;
4209 while((shadow
= shadow
->shadow
))
4211 printf(" (depth %d)", i
);
4213 printf(", copy=0x%x", object
->copy
);
4214 printf(", shadow_offset=0x%x", object
->shadow_offset
);
4215 printf(", last_alloc=0x%x\n", object
->last_alloc
);
4217 iprintf("pager=0x%x", object
->pager
);
4218 printf(", paging_offset=0x%x", object
->paging_offset
);
4219 printf(", pager_control=0x%x\n", object
->pager_control
);
4221 iprintf("copy_strategy=%d[", object
->copy_strategy
);
4222 switch (object
->copy_strategy
) {
4223 case MEMORY_OBJECT_COPY_NONE
:
4224 printf("copy_none");
4227 case MEMORY_OBJECT_COPY_CALL
:
4228 printf("copy_call");
4231 case MEMORY_OBJECT_COPY_DELAY
:
4232 printf("copy_delay");
4235 case MEMORY_OBJECT_COPY_SYMMETRIC
:
4236 printf("copy_symmetric");
4239 case MEMORY_OBJECT_COPY_INVALID
:
4240 printf("copy_invalid");
4247 printf(", absent_count=%d\n", object
->absent_count
);
4249 iprintf("all_wanted=0x%x<", object
->all_wanted
);
4251 if (vm_object_wanted(object
, VM_OBJECT_EVENT_INITIALIZED
)) {
4252 printf("%sinit", s
);
4255 if (vm_object_wanted(object
, VM_OBJECT_EVENT_PAGER_READY
)) {
4256 printf("%sready", s
);
4259 if (vm_object_wanted(object
, VM_OBJECT_EVENT_PAGING_IN_PROGRESS
)) {
4260 printf("%spaging", s
);
4263 if (vm_object_wanted(object
, VM_OBJECT_EVENT_ABSENT_COUNT
)) {
4264 printf("%sabsent", s
);
4267 if (vm_object_wanted(object
, VM_OBJECT_EVENT_LOCK_IN_PROGRESS
)) {
4268 printf("%slock", s
);
4271 if (vm_object_wanted(object
, VM_OBJECT_EVENT_UNCACHING
)) {
4272 printf("%suncaching", s
);
4275 if (vm_object_wanted(object
, VM_OBJECT_EVENT_COPY_CALL
)) {
4276 printf("%scopy_call", s
);
4279 if (vm_object_wanted(object
, VM_OBJECT_EVENT_CACHING
)) {
4280 printf("%scaching", s
);
4284 printf(", paging_in_progress=%d\n", object
->paging_in_progress
);
4286 iprintf("%screated, %sinit, %sready, %spersist, %strusted, %spageout, %s, %s\n",
4287 (object
->pager_created
? "" : "!"),
4288 (object
->pager_initialized
? "" : "!"),
4289 (object
->pager_ready
? "" : "!"),
4290 (object
->can_persist
? "" : "!"),
4291 (object
->pager_trusted
? "" : "!"),
4292 (object
->pageout
? "" : "!"),
4293 (object
->internal
? "internal" : "external"),
4294 (object
->temporary
? "temporary" : "permanent"));
4295 iprintf("%salive, %spurgable, %spurgable_volatile, %spurgable_empty, %sshadowed, %scached, %sprivate\n",
4296 (object
->alive
? "" : "!"),
4297 ((object
->purgable
!= VM_OBJECT_NONPURGABLE
) ? "" : "!"),
4298 ((object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
) ? "" : "!"),
4299 ((object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) ? "" : "!"),
4300 (object
->shadowed
? "" : "!"),
4301 (vm_object_cached(object
) ? "" : "!"),
4302 (object
->private ? "" : "!"));
4303 iprintf("%sadvisory_pageout, %ssilent_overwrite\n",
4304 (object
->advisory_pageout
? "" : "!"),
4305 (object
->silent_overwrite
? "" : "!"));
4308 iprintf("existence_map=");
4309 vm_external_print(object
->existence_map
, object
->size
);
4310 #endif /* MACH_PAGEMAP */
4312 iprintf("paging_object=0x%x\n", object
->paging_object
);
4313 #endif /* MACH_ASSERT */
4315 if (vm_object_print_pages
) {
4317 p
= (vm_page_t
) queue_first(&object
->memq
);
4318 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
4320 iprintf("memory:=");
4321 } else if (count
== 2) {
4330 printf("(off=0x%llX,page=%p)", p
->offset
, p
);
4331 p
= (vm_page_t
) queue_next(&p
->listq
);
4342 * vm_object_find [ debug ]
4344 * Find all tasks which reference the given vm_object.
4347 boolean_t
vm_object_find(vm_object_t object
);
4348 boolean_t vm_object_print_verbose
= FALSE
;
4356 vm_map_entry_t entry
;
4357 processor_set_t pset
= &default_pset
;
4358 boolean_t found
= FALSE
;
4360 queue_iterate(&pset
->tasks
, task
, task_t
, pset_tasks
) {
4362 for (entry
= vm_map_first_entry(map
);
4363 entry
&& entry
!= vm_map_to_entry(map
);
4364 entry
= entry
->vme_next
) {
4369 * For the time being skip submaps,
4370 * only the kernel can have submaps,
4371 * and unless we are interested in
4372 * kernel objects, we can simply skip
4373 * submaps. See sb/dejan/nmk18b7/src/mach_kernel/vm
4374 * for a full solution.
4376 if (entry
->is_sub_map
)
4379 obj
= entry
->object
.vm_object
;
4383 while (obj
!= VM_OBJECT_NULL
) {
4384 if (obj
== object
) {
4386 printf("TASK\t\tMAP\t\tENTRY\n");
4389 printf("0x%x\t0x%x\t0x%x\n",
4400 #endif /* MACH_KDB */
4403 vm_object_populate_with_private(
4405 vm_object_offset_t offset
,
4410 vm_object_offset_t base_offset
;
4413 if(!object
->private)
4414 return KERN_FAILURE
;
4416 base_page
= phys_page
;
4418 vm_object_lock(object
);
4419 if(!object
->phys_contiguous
) {
4421 if((base_offset
= trunc_page_64(offset
)) != offset
) {
4422 vm_object_unlock(object
);
4423 return KERN_FAILURE
;
4425 base_offset
+= object
->paging_offset
;
4427 m
= vm_page_lookup(object
, base_offset
);
4428 if(m
!= VM_PAGE_NULL
) {
4430 vm_page_lock_queues();
4431 m
->fictitious
= FALSE
;
4433 m
->phys_page
= base_page
;
4439 object
->absent_count
++;
4441 m
->list_req_pending
= TRUE
;
4442 vm_page_unlock_queues();
4443 } else if (m
->phys_page
!= base_page
) {
4444 /* pmap call to clear old mapping */
4445 pmap_disconnect(m
->phys_page
);
4446 m
->phys_page
= base_page
;
4451 * We're not pointing to the same
4452 * physical page any longer and the
4453 * contents of the new one are not
4454 * supposed to be encrypted.
4455 * XXX What happens to the original
4456 * physical page. Is it lost ?
4458 m
->encrypted
= FALSE
;
4461 while ((m
= vm_page_grab_fictitious())
4463 vm_page_more_fictitious();
4464 vm_page_lock_queues();
4465 m
->fictitious
= FALSE
;
4467 m
->phys_page
= base_page
;
4468 m
->list_req_pending
= TRUE
;
4471 object
->absent_count
++;
4472 vm_page_unlock_queues();
4473 vm_page_insert(m
, object
, base_offset
);
4475 base_page
++; /* Go to the next physical page */
4476 base_offset
+= PAGE_SIZE
;
4480 /* NOTE: we should check the original settings here */
4481 /* if we have a size > zero a pmap call should be made */
4482 /* to disable the range */
4486 /* shadows on contiguous memory are not allowed */
4487 /* we therefore can use the offset field */
4488 object
->shadow_offset
= (vm_object_offset_t
)(phys_page
<< 12);
4489 object
->size
= size
;
4491 vm_object_unlock(object
);
4492 return KERN_SUCCESS
;
4496 * memory_object_free_from_cache:
4498 * Walk the vm_object cache list, removing and freeing vm_objects
4499 * which are backed by the pager identified by the caller, (pager_ops).
4500 * Remove up to "count" objects, if there are that may available
4503 * Walk the list at most once, return the number of vm_objects
4507 __private_extern__ kern_return_t
4508 memory_object_free_from_cache(
4509 __unused host_t host
,
4510 memory_object_pager_ops_t pager_ops
,
4514 int object_released
= 0;
4516 register vm_object_t object
= VM_OBJECT_NULL
;
4520 if(host == HOST_NULL)
4521 return(KERN_INVALID_ARGUMENT);
4525 vm_object_cache_lock();
4527 queue_iterate(&vm_object_cached_list
, object
,
4528 vm_object_t
, cached_list
) {
4529 if (object
->pager
&&
4530 (pager_ops
== object
->pager
->mo_pager_ops
)) {
4531 vm_object_lock(object
);
4532 queue_remove(&vm_object_cached_list
, object
,
4533 vm_object_t
, cached_list
);
4534 vm_object_cached_count
--;
4537 * Since this object is in the cache, we know
4538 * that it is initialized and has only a pager's
4539 * (implicit) reference. Take a reference to avoid
4540 * recursive deallocations.
4543 assert(object
->pager_initialized
);
4544 assert(object
->ref_count
== 0);
4545 object
->ref_count
++;
4548 * Terminate the object.
4549 * If the object had a shadow, we let
4550 * vm_object_deallocate deallocate it.
4551 * "pageout" objects have a shadow, but
4552 * maintain a "paging reference" rather
4553 * than a normal reference.
4554 * (We are careful here to limit recursion.)
4556 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
4557 if ((vm_object_terminate(object
) == KERN_SUCCESS
)
4558 && (shadow
!= VM_OBJECT_NULL
)) {
4559 vm_object_deallocate(shadow
);
4562 if(object_released
++ == *count
)
4563 return KERN_SUCCESS
;
4567 vm_object_cache_unlock();
4568 *count
= object_released
;
4569 return KERN_SUCCESS
;
4575 memory_object_create_named(
4576 memory_object_t pager
,
4577 memory_object_offset_t size
,
4578 memory_object_control_t
*control
)
4581 vm_object_hash_entry_t entry
;
4583 *control
= MEMORY_OBJECT_CONTROL_NULL
;
4584 if (pager
== MEMORY_OBJECT_NULL
)
4585 return KERN_INVALID_ARGUMENT
;
4587 vm_object_cache_lock();
4588 entry
= vm_object_hash_lookup(pager
, FALSE
);
4589 if ((entry
!= VM_OBJECT_HASH_ENTRY_NULL
) &&
4590 (entry
->object
!= VM_OBJECT_NULL
)) {
4591 if (entry
->object
->named
== TRUE
)
4592 panic("memory_object_create_named: caller already holds the right"); }
4594 vm_object_cache_unlock();
4595 if ((object
= vm_object_enter(pager
, size
, FALSE
, FALSE
, TRUE
))
4596 == VM_OBJECT_NULL
) {
4597 return(KERN_INVALID_OBJECT
);
4600 /* wait for object (if any) to be ready */
4601 if (object
!= VM_OBJECT_NULL
) {
4602 vm_object_lock(object
);
4603 object
->named
= TRUE
;
4604 while (!object
->pager_ready
) {
4605 vm_object_sleep(object
,
4606 VM_OBJECT_EVENT_PAGER_READY
,
4609 *control
= object
->pager_control
;
4610 vm_object_unlock(object
);
4612 return (KERN_SUCCESS
);
4617 * Routine: memory_object_recover_named [user interface]
4619 * Attempt to recover a named reference for a VM object.
4620 * VM will verify that the object has not already started
4621 * down the termination path, and if it has, will optionally
4622 * wait for that to finish.
4624 * KERN_SUCCESS - we recovered a named reference on the object
4625 * KERN_FAILURE - we could not recover a reference (object dead)
4626 * KERN_INVALID_ARGUMENT - bad memory object control
4629 memory_object_recover_named(
4630 memory_object_control_t control
,
4631 boolean_t wait_on_terminating
)
4635 vm_object_cache_lock();
4636 object
= memory_object_control_to_vm_object(control
);
4637 if (object
== VM_OBJECT_NULL
) {
4638 vm_object_cache_unlock();
4639 return (KERN_INVALID_ARGUMENT
);
4643 vm_object_lock(object
);
4645 if (object
->terminating
&& wait_on_terminating
) {
4646 vm_object_cache_unlock();
4647 vm_object_wait(object
,
4648 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
4650 vm_object_cache_lock();
4654 if (!object
->alive
) {
4655 vm_object_cache_unlock();
4656 vm_object_unlock(object
);
4657 return KERN_FAILURE
;
4660 if (object
->named
== TRUE
) {
4661 vm_object_cache_unlock();
4662 vm_object_unlock(object
);
4663 return KERN_SUCCESS
;
4666 if((object
->ref_count
== 0) && (!object
->terminating
)){
4667 queue_remove(&vm_object_cached_list
, object
,
4668 vm_object_t
, cached_list
);
4669 vm_object_cached_count
--;
4670 XPR(XPR_VM_OBJECT_CACHE
,
4671 "memory_object_recover_named: removing %X, head (%X, %X)\n",
4673 (integer_t
)vm_object_cached_list
.next
,
4674 (integer_t
)vm_object_cached_list
.prev
, 0,0);
4677 vm_object_cache_unlock();
4679 object
->named
= TRUE
;
4680 object
->ref_count
++;
4681 vm_object_res_reference(object
);
4682 while (!object
->pager_ready
) {
4683 vm_object_sleep(object
,
4684 VM_OBJECT_EVENT_PAGER_READY
,
4687 vm_object_unlock(object
);
4688 return (KERN_SUCCESS
);
4693 * vm_object_release_name:
4695 * Enforces name semantic on memory_object reference count decrement
4696 * This routine should not be called unless the caller holds a name
4697 * reference gained through the memory_object_create_named.
4699 * If the TERMINATE_IDLE flag is set, the call will return if the
4700 * reference count is not 1. i.e. idle with the only remaining reference
4702 * If the decision is made to proceed the name field flag is set to
4703 * false and the reference count is decremented. If the RESPECT_CACHE
4704 * flag is set and the reference count has gone to zero, the
4705 * memory_object is checked to see if it is cacheable otherwise when
4706 * the reference count is zero, it is simply terminated.
4709 __private_extern__ kern_return_t
4710 vm_object_release_name(
4715 boolean_t original_object
= TRUE
;
4717 while (object
!= VM_OBJECT_NULL
) {
4720 * The cache holds a reference (uncounted) to
4721 * the object. We must locke it before removing
4726 vm_object_cache_lock();
4727 vm_object_lock(object
);
4728 assert(object
->alive
);
4730 assert(object
->named
);
4731 assert(object
->ref_count
> 0);
4734 * We have to wait for initialization before
4735 * destroying or caching the object.
4738 if (object
->pager_created
&& !object
->pager_initialized
) {
4739 assert(!object
->can_persist
);
4740 vm_object_assert_wait(object
,
4741 VM_OBJECT_EVENT_INITIALIZED
,
4743 vm_object_unlock(object
);
4744 vm_object_cache_unlock();
4745 thread_block(THREAD_CONTINUE_NULL
);
4749 if (((object
->ref_count
> 1)
4750 && (flags
& MEMORY_OBJECT_TERMINATE_IDLE
))
4751 || (object
->terminating
)) {
4752 vm_object_unlock(object
);
4753 vm_object_cache_unlock();
4754 return KERN_FAILURE
;
4756 if (flags
& MEMORY_OBJECT_RELEASE_NO_OP
) {
4757 vm_object_unlock(object
);
4758 vm_object_cache_unlock();
4759 return KERN_SUCCESS
;
4763 if ((flags
& MEMORY_OBJECT_RESPECT_CACHE
) &&
4764 (object
->ref_count
== 1)) {
4766 object
->named
= FALSE
;
4767 vm_object_unlock(object
);
4768 vm_object_cache_unlock();
4769 /* let vm_object_deallocate push this thing into */
4770 /* the cache, if that it is where it is bound */
4771 vm_object_deallocate(object
);
4772 return KERN_SUCCESS
;
4774 VM_OBJ_RES_DECR(object
);
4775 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
4776 if(object
->ref_count
== 1) {
4777 if(vm_object_terminate(object
) != KERN_SUCCESS
) {
4778 if(original_object
) {
4779 return KERN_FAILURE
;
4781 return KERN_SUCCESS
;
4784 if (shadow
!= VM_OBJECT_NULL
) {
4785 original_object
= FALSE
;
4789 return KERN_SUCCESS
;
4791 object
->ref_count
--;
4792 assert(object
->ref_count
> 0);
4794 object
->named
= FALSE
;
4795 vm_object_unlock(object
);
4796 vm_object_cache_unlock();
4797 return KERN_SUCCESS
;
4802 return KERN_FAILURE
;
4806 __private_extern__ kern_return_t
4807 vm_object_lock_request(
4809 vm_object_offset_t offset
,
4810 vm_object_size_t size
,
4811 memory_object_return_t should_return
,
4815 __unused boolean_t should_flush
;
4817 should_flush
= flags
& MEMORY_OBJECT_DATA_FLUSH
;
4819 XPR(XPR_MEMORY_OBJECT
,
4820 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
4821 (integer_t
)object
, offset
, size
,
4822 (((should_return
&1)<<1)|should_flush
), prot
);
4825 * Check for bogus arguments.
4827 if (object
== VM_OBJECT_NULL
)
4828 return (KERN_INVALID_ARGUMENT
);
4830 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
)
4831 return (KERN_INVALID_ARGUMENT
);
4833 size
= round_page_64(size
);
4836 * Lock the object, and acquire a paging reference to
4837 * prevent the memory_object reference from being released.
4839 vm_object_lock(object
);
4840 vm_object_paging_begin(object
);
4842 (void)vm_object_update(object
,
4843 offset
, size
, NULL
, NULL
, should_return
, flags
, prot
);
4845 vm_object_paging_end(object
);
4846 vm_object_unlock(object
);
4848 return (KERN_SUCCESS
);
4852 * Empty a purgable object by grabbing the physical pages assigned to it and
4853 * putting them on the free queue without writing them to backing store, etc.
4854 * When the pages are next touched they will be demand zero-fill pages. We
4855 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
4856 * skip referenced/dirty pages, pages on the active queue, etc. We're more
4857 * than happy to grab these since this is a purgable object. We mark the
4858 * object as "empty" after reaping its pages.
4860 * On entry the object and page queues are locked, the object must be a
4861 * purgable object with no delayed copies pending.
4864 vm_object_purge(vm_object_t object
)
4867 unsigned int num_purged_pages
;
4868 vm_page_t local_freeq
;
4869 unsigned long local_freed
;
4870 int purge_loop_quota
;
4871 /* free pages as soon as we gather PURGE_BATCH_FREE_LIMIT pages to free */
4872 #define PURGE_BATCH_FREE_LIMIT 50
4873 /* release page queues lock every PURGE_LOOP_QUOTA iterations */
4874 #define PURGE_LOOP_QUOTA 100
4876 num_purged_pages
= 0;
4877 if (object
->purgable
== VM_OBJECT_NONPURGABLE
)
4878 return num_purged_pages
;
4880 object
->purgable
= VM_OBJECT_PURGABLE_EMPTY
;
4882 assert(object
->copy
== VM_OBJECT_NULL
);
4883 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
4884 purge_loop_quota
= PURGE_LOOP_QUOTA
;
4886 local_freeq
= VM_PAGE_NULL
;
4890 * Go through the object's resident pages and try and discard them.
4892 next
= (vm_page_t
)queue_first(&object
->memq
);
4893 while (!queue_end(&object
->memq
, (queue_entry_t
)next
)) {
4895 next
= (vm_page_t
)queue_next(&next
->listq
);
4897 if (purge_loop_quota
-- == 0) {
4899 * Avoid holding the page queues lock for too long.
4900 * Let someone else take it for a while if needed.
4901 * Keep holding the object's lock to guarantee that
4902 * the object's page list doesn't change under us
4905 if (local_freeq
!= VM_PAGE_NULL
) {
4907 * Flush our queue of pages to free.
4909 vm_page_free_list(local_freeq
);
4910 local_freeq
= VM_PAGE_NULL
;
4913 vm_page_unlock_queues();
4915 vm_page_lock_queues();
4917 /* resume with the current page and a new quota */
4918 purge_loop_quota
= PURGE_LOOP_QUOTA
;
4922 if (p
->busy
|| p
->cleaning
|| p
->laundry
||
4923 p
->list_req_pending
) {
4924 /* page is being acted upon, so don't mess with it */
4927 if (p
->wire_count
) {
4928 /* don't discard a wired page */
4933 /* clean up the object/offset table */
4937 /* update the object's count of absent pages */
4938 vm_object_absent_release(object
);
4941 /* we can discard this page */
4943 /* advertize that this page is in a transition state */
4946 if (p
->no_isync
== TRUE
) {
4947 /* the page hasn't been mapped yet */
4948 /* (optimization to delay the i-cache sync) */
4950 /* unmap the page */
4953 refmod_state
= pmap_disconnect(p
->phys_page
);
4954 if (refmod_state
& VM_MEM_MODIFIED
) {
4959 if (p
->dirty
|| p
->precious
) {
4960 /* we saved the cost of cleaning this page ! */
4962 vm_page_purged_count
++;
4965 /* remove page from active or inactive queue... */
4966 VM_PAGE_QUEUES_REMOVE(p
);
4968 /* ... and put it on our queue of pages to free */
4969 assert(!p
->laundry
);
4970 assert(p
->object
!= kernel_object
);
4971 assert(p
->pageq
.next
== NULL
&&
4972 p
->pageq
.prev
== NULL
);
4973 p
->pageq
.next
= (queue_entry_t
) local_freeq
;
4975 if (++local_freed
>= PURGE_BATCH_FREE_LIMIT
) {
4976 /* flush our queue of pages to free */
4977 vm_page_free_list(local_freeq
);
4978 local_freeq
= VM_PAGE_NULL
;
4983 /* flush our local queue of pages to free one last time */
4984 if (local_freeq
!= VM_PAGE_NULL
) {
4985 vm_page_free_list(local_freeq
);
4986 local_freeq
= VM_PAGE_NULL
;
4990 return num_purged_pages
;
4994 * vm_object_purgable_control() allows the caller to control and investigate the
4995 * state of a purgable object. A purgable object is created via a call to
4996 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgable object will
4997 * never be coalesced with any other object -- even other purgable objects --
4998 * and will thus always remain a distinct object. A purgable object has
4999 * special semantics when its reference count is exactly 1. If its reference
5000 * count is greater than 1, then a purgable object will behave like a normal
5001 * object and attempts to use this interface will result in an error return
5002 * of KERN_INVALID_ARGUMENT.
5004 * A purgable object may be put into a "volatile" state which will make the
5005 * object's pages elligable for being reclaimed without paging to backing
5006 * store if the system runs low on memory. If the pages in a volatile
5007 * purgable object are reclaimed, the purgable object is said to have been
5008 * "emptied." When a purgable object is emptied the system will reclaim as
5009 * many pages from the object as it can in a convenient manner (pages already
5010 * en route to backing store or busy for other reasons are left as is). When
5011 * a purgable object is made volatile, its pages will generally be reclaimed
5012 * before other pages in the application's working set. This semantic is
5013 * generally used by applications which can recreate the data in the object
5014 * faster than it can be paged in. One such example might be media assets
5015 * which can be reread from a much faster RAID volume.
5017 * A purgable object may be designated as "non-volatile" which means it will
5018 * behave like all other objects in the system with pages being written to and
5019 * read from backing store as needed to satisfy system memory needs. If the
5020 * object was emptied before the object was made non-volatile, that fact will
5021 * be returned as the old state of the purgable object (see
5022 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
5023 * were reclaimed as part of emptying the object will be refaulted in as
5024 * zero-fill on demand. It is up to the application to note that an object
5025 * was emptied and recreate the objects contents if necessary. When a
5026 * purgable object is made non-volatile, its pages will generally not be paged
5027 * out to backing store in the immediate future. A purgable object may also
5028 * be manually emptied.
5030 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
5031 * volatile purgable object may be queried at any time. This information may
5032 * be used as a control input to let the application know when the system is
5033 * experiencing memory pressure and is reclaiming memory.
5035 * The specified address may be any address within the purgable object. If
5036 * the specified address does not represent any object in the target task's
5037 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
5038 * object containing the specified address is not a purgable object, then
5039 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
5042 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
5043 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
5044 * state is used to set the new state of the purgable object and return its
5045 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgable
5046 * object is returned in the parameter state.
5048 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
5049 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
5050 * the non-volatile, volatile and volatile/empty states described above.
5051 * Setting the state of a purgable object to VM_PURGABLE_EMPTY will
5052 * immediately reclaim as many pages in the object as can be conveniently
5053 * collected (some may have already been written to backing store or be
5056 * The process of making a purgable object non-volatile and determining its
5057 * previous state is atomic. Thus, if a purgable object is made
5058 * VM_PURGABLE_NONVOLATILE and the old state is returned as
5059 * VM_PURGABLE_VOLATILE, then the purgable object's previous contents are
5060 * completely intact and will remain so until the object is made volatile
5061 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
5062 * was reclaimed while it was in a volatile state and its previous contents
5066 * The object must be locked.
5069 vm_object_purgable_control(
5071 vm_purgable_t control
,
5077 if (object
== VM_OBJECT_NULL
) {
5079 * Object must already be present or it can't be purgable.
5081 return KERN_INVALID_ARGUMENT
;
5085 * Get current state of the purgable object.
5087 switch (object
->purgable
) {
5088 case VM_OBJECT_NONPURGABLE
:
5089 return KERN_INVALID_ARGUMENT
;
5091 case VM_OBJECT_PURGABLE_NONVOLATILE
:
5092 old_state
= VM_PURGABLE_NONVOLATILE
;
5095 case VM_OBJECT_PURGABLE_VOLATILE
:
5096 old_state
= VM_PURGABLE_VOLATILE
;
5099 case VM_OBJECT_PURGABLE_EMPTY
:
5100 old_state
= VM_PURGABLE_EMPTY
;
5104 old_state
= VM_PURGABLE_NONVOLATILE
;
5105 panic("Bad state (%d) for purgable object!\n",
5110 /* purgable cant have delayed copies - now or in the future */
5111 assert(object
->copy
== VM_OBJECT_NULL
);
5112 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
5115 * Execute the desired operation.
5117 if (control
== VM_PURGABLE_GET_STATE
) {
5119 return KERN_SUCCESS
;
5123 case VM_PURGABLE_NONVOLATILE
:
5124 vm_page_lock_queues();
5125 if (object
->purgable
!= VM_OBJECT_PURGABLE_NONVOLATILE
) {
5126 assert(vm_page_purgeable_count
>=
5127 object
->resident_page_count
);
5128 vm_page_purgeable_count
-= object
->resident_page_count
;
5131 object
->purgable
= VM_OBJECT_PURGABLE_NONVOLATILE
;
5134 * If the object wasn't emptied, then mark all pages of the
5135 * object as referenced in order to give them a complete turn
5136 * of the virtual memory "clock" before becoming candidates
5137 * for paging out (if the system is suffering from memory
5138 * pressure). We don't really need to set the pmap reference
5139 * bits (which would be expensive) since the software copies
5140 * are believed if they're set to true ...
5142 if (old_state
!= VM_PURGABLE_EMPTY
) {
5143 for (p
= (vm_page_t
)queue_first(&object
->memq
);
5144 !queue_end(&object
->memq
, (queue_entry_t
)p
);
5145 p
= (vm_page_t
)queue_next(&p
->listq
))
5146 p
->reference
= TRUE
;
5149 vm_page_unlock_queues();
5153 case VM_PURGABLE_VOLATILE
:
5154 vm_page_lock_queues();
5156 if (object
->purgable
!= VM_OBJECT_PURGABLE_VOLATILE
&&
5157 object
->purgable
!= VM_OBJECT_PURGABLE_EMPTY
) {
5158 vm_page_purgeable_count
+= object
->resident_page_count
;
5161 object
->purgable
= VM_OBJECT_PURGABLE_VOLATILE
;
5164 * We want the newly volatile purgable object to be a
5165 * candidate for the pageout scan before other pages in the
5166 * application if the system is suffering from memory
5167 * pressure. To do this, we move a page of the object from
5168 * the active queue onto the inactive queue in order to
5169 * promote the object for early reclaim. We only need to move
5170 * a single page since the pageout scan will reap the entire
5171 * purgable object if it finds a single page in a volatile
5172 * state. Obviously we don't do this if there are no pages
5173 * associated with the object or we find a page of the object
5174 * already on the inactive queue.
5176 for (p
= (vm_page_t
)queue_first(&object
->memq
);
5177 !queue_end(&object
->memq
, (queue_entry_t
)p
);
5178 p
= (vm_page_t
)queue_next(&p
->listq
)) {
5180 /* already a page on the inactive queue */
5183 if (p
->active
&& !p
->busy
) {
5184 /* found one we can move */
5185 vm_page_deactivate(p
);
5189 vm_page_unlock_queues();
5194 case VM_PURGABLE_EMPTY
:
5195 vm_page_lock_queues();
5196 if (object
->purgable
!= VM_OBJECT_PURGABLE_VOLATILE
&&
5197 object
->purgable
!= VM_OBJECT_PURGABLE_EMPTY
) {
5198 vm_page_purgeable_count
+= object
->resident_page_count
;
5200 (void) vm_object_purge(object
);
5201 vm_page_unlock_queues();
5207 return KERN_SUCCESS
;
5212 * vm_object_res_deallocate
5214 * (recursively) decrement residence counts on vm objects and their shadows.
5215 * Called from vm_object_deallocate and when swapping out an object.
5217 * The object is locked, and remains locked throughout the function,
5218 * even as we iterate down the shadow chain. Locks on intermediate objects
5219 * will be dropped, but not the original object.
5221 * NOTE: this function used to use recursion, rather than iteration.
5224 __private_extern__
void
5225 vm_object_res_deallocate(
5228 vm_object_t orig_object
= object
;
5230 * Object is locked so it can be called directly
5231 * from vm_object_deallocate. Original object is never
5234 assert(object
->res_count
> 0);
5235 while (--object
->res_count
== 0) {
5236 assert(object
->ref_count
>= object
->res_count
);
5237 vm_object_deactivate_all_pages(object
);
5238 /* iterate on shadow, if present */
5239 if (object
->shadow
!= VM_OBJECT_NULL
) {
5240 vm_object_t tmp_object
= object
->shadow
;
5241 vm_object_lock(tmp_object
);
5242 if (object
!= orig_object
)
5243 vm_object_unlock(object
);
5244 object
= tmp_object
;
5245 assert(object
->res_count
> 0);
5249 if (object
!= orig_object
)
5250 vm_object_unlock(object
);
5254 * vm_object_res_reference
5256 * Internal function to increment residence count on a vm object
5257 * and its shadows. It is called only from vm_object_reference, and
5258 * when swapping in a vm object, via vm_map_swap.
5260 * The object is locked, and remains locked throughout the function,
5261 * even as we iterate down the shadow chain. Locks on intermediate objects
5262 * will be dropped, but not the original object.
5264 * NOTE: this function used to use recursion, rather than iteration.
5267 __private_extern__
void
5268 vm_object_res_reference(
5271 vm_object_t orig_object
= object
;
5273 * Object is locked, so this can be called directly
5274 * from vm_object_reference. This lock is never released.
5276 while ((++object
->res_count
== 1) &&
5277 (object
->shadow
!= VM_OBJECT_NULL
)) {
5278 vm_object_t tmp_object
= object
->shadow
;
5280 assert(object
->ref_count
>= object
->res_count
);
5281 vm_object_lock(tmp_object
);
5282 if (object
!= orig_object
)
5283 vm_object_unlock(object
);
5284 object
= tmp_object
;
5286 if (object
!= orig_object
)
5287 vm_object_unlock(object
);
5288 assert(orig_object
->ref_count
>= orig_object
->res_count
);
5290 #endif /* TASK_SWAPPER */
5293 * vm_object_reference:
5295 * Gets another reference to the given object.
5297 #ifdef vm_object_reference
5298 #undef vm_object_reference
5300 __private_extern__
void
5301 vm_object_reference(
5302 register vm_object_t object
)
5304 if (object
== VM_OBJECT_NULL
)
5307 vm_object_lock(object
);
5308 assert(object
->ref_count
> 0);
5309 vm_object_reference_locked(object
);
5310 vm_object_unlock(object
);
5315 * Scale the vm_object_cache
5316 * This is required to make sure that the vm_object_cache is big
5317 * enough to effectively cache the mapped file.
5318 * This is really important with UBC as all the regular file vnodes
5319 * have memory object associated with them. Havving this cache too
5320 * small results in rapid reclaim of vnodes and hurts performance a LOT!
5322 * This is also needed as number of vnodes can be dynamically scaled.
5325 adjust_vm_object_cache(
5326 __unused vm_size_t oval
,
5329 vm_object_cached_max
= nval
;
5330 vm_object_cache_trim(FALSE
);
5331 return (KERN_SUCCESS
);
5333 #endif /* MACH_BSD */
5337 * vm_object_transpose
5339 * This routine takes two VM objects of the same size and exchanges
5340 * their backing store.
5341 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
5342 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
5344 * The VM objects must not be locked by caller.
5347 vm_object_transpose(
5348 vm_object_t object1
,
5349 vm_object_t object2
,
5350 vm_object_size_t transpose_size
)
5352 vm_object_t tmp_object
;
5353 kern_return_t retval
;
5354 boolean_t object1_locked
, object2_locked
;
5355 boolean_t object1_paging
, object2_paging
;
5357 vm_object_offset_t page_offset
;
5359 tmp_object
= VM_OBJECT_NULL
;
5360 object1_locked
= FALSE
; object2_locked
= FALSE
;
5361 object1_paging
= FALSE
; object2_paging
= FALSE
;
5363 if (object1
== object2
||
5364 object1
== VM_OBJECT_NULL
||
5365 object2
== VM_OBJECT_NULL
) {
5367 * If the 2 VM objects are the same, there's
5368 * no point in exchanging their backing store.
5370 retval
= KERN_INVALID_VALUE
;
5374 vm_object_lock(object1
);
5375 object1_locked
= TRUE
;
5376 if (object1
->copy
|| object1
->shadow
|| object1
->shadowed
||
5377 object1
->purgable
!= VM_OBJECT_NONPURGABLE
) {
5379 * We don't deal with copy or shadow objects (yet).
5381 retval
= KERN_INVALID_VALUE
;
5385 * Since we're about to mess with the object's backing store,
5386 * mark it as "paging_in_progress". Note that this is not enough
5387 * to prevent any paging activity on this object, so the caller should
5388 * have "quiesced" the objects beforehand, via a UPL operation with
5389 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
5390 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
5392 vm_object_paging_begin(object1
);
5393 object1_paging
= TRUE
;
5394 vm_object_unlock(object1
);
5395 object1_locked
= FALSE
;
5398 * Same as above for the 2nd object...
5400 vm_object_lock(object2
);
5401 object2_locked
= TRUE
;
5402 if (object2
->copy
|| object2
->shadow
|| object2
->shadowed
||
5403 object2
->purgable
!= VM_OBJECT_NONPURGABLE
) {
5404 retval
= KERN_INVALID_VALUE
;
5407 vm_object_paging_begin(object2
);
5408 object2_paging
= TRUE
;
5409 vm_object_unlock(object2
);
5410 object2_locked
= FALSE
;
5413 * Allocate a temporary VM object to hold object1's contents
5414 * while we copy object2 to object1.
5416 tmp_object
= vm_object_allocate(transpose_size
);
5417 vm_object_lock(tmp_object
);
5418 vm_object_paging_begin(tmp_object
);
5419 tmp_object
->can_persist
= FALSE
;
5422 * Since we need to lock both objects at the same time,
5423 * make sure we always lock them in the same order to
5426 if (object1
< object2
) {
5427 vm_object_lock(object1
);
5428 vm_object_lock(object2
);
5430 vm_object_lock(object2
);
5431 vm_object_lock(object1
);
5433 object1_locked
= TRUE
;
5434 object2_locked
= TRUE
;
5436 if (object1
->size
!= object2
->size
||
5437 object1
->size
!= transpose_size
) {
5439 * If the 2 objects don't have the same size, we can't
5440 * exchange their backing stores or one would overflow.
5441 * If their size doesn't match the caller's
5442 * "transpose_size", we can't do it either because the
5443 * transpose operation will affect the entire span of
5446 retval
= KERN_INVALID_VALUE
;
5452 * Transpose the lists of resident pages.
5454 if (object1
->phys_contiguous
|| queue_empty(&object1
->memq
)) {
5456 * No pages in object1, just transfer pages
5457 * from object2 to object1. No need to go through
5458 * an intermediate object.
5460 while (!queue_empty(&object2
->memq
)) {
5461 page
= (vm_page_t
) queue_first(&object2
->memq
);
5462 vm_page_rename(page
, object1
, page
->offset
);
5464 assert(queue_empty(&object2
->memq
));
5465 } else if (object2
->phys_contiguous
|| queue_empty(&object2
->memq
)) {
5467 * No pages in object2, just transfer pages
5468 * from object1 to object2. No need to go through
5469 * an intermediate object.
5471 while (!queue_empty(&object1
->memq
)) {
5472 page
= (vm_page_t
) queue_first(&object1
->memq
);
5473 vm_page_rename(page
, object2
, page
->offset
);
5475 assert(queue_empty(&object1
->memq
));
5477 /* transfer object1's pages to tmp_object */
5478 vm_page_lock_queues();
5479 while (!queue_empty(&object1
->memq
)) {
5480 page
= (vm_page_t
) queue_first(&object1
->memq
);
5481 page_offset
= page
->offset
;
5482 vm_page_remove(page
);
5483 page
->offset
= page_offset
;
5484 queue_enter(&tmp_object
->memq
, page
, vm_page_t
, listq
);
5486 vm_page_unlock_queues();
5487 assert(queue_empty(&object1
->memq
));
5488 /* transfer object2's pages to object1 */
5489 while (!queue_empty(&object2
->memq
)) {
5490 page
= (vm_page_t
) queue_first(&object2
->memq
);
5491 vm_page_rename(page
, object1
, page
->offset
);
5493 assert(queue_empty(&object2
->memq
));
5494 /* transfer tmp_object's pages to object1 */
5495 while (!queue_empty(&tmp_object
->memq
)) {
5496 page
= (vm_page_t
) queue_first(&tmp_object
->memq
);
5497 queue_remove(&tmp_object
->memq
, page
,
5499 vm_page_insert(page
, object2
, page
->offset
);
5501 assert(queue_empty(&tmp_object
->memq
));
5504 /* no need to transpose the size: they should be identical */
5505 assert(object1
->size
== object2
->size
);
5507 #define __TRANSPOSE_FIELD(field) \
5509 tmp_object->field = object1->field; \
5510 object1->field = object2->field; \
5511 object2->field = tmp_object->field; \
5514 assert(!object1
->copy
);
5515 assert(!object2
->copy
);
5517 assert(!object1
->shadow
);
5518 assert(!object2
->shadow
);
5520 __TRANSPOSE_FIELD(shadow_offset
); /* used by phys_contiguous objects */
5521 __TRANSPOSE_FIELD(pager
);
5522 __TRANSPOSE_FIELD(paging_offset
);
5524 __TRANSPOSE_FIELD(pager_control
);
5525 /* update the memory_objects' pointers back to the VM objects */
5526 if (object1
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
5527 memory_object_control_collapse(object1
->pager_control
,
5530 if (object2
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
5531 memory_object_control_collapse(object2
->pager_control
,
5535 __TRANSPOSE_FIELD(absent_count
);
5537 assert(object1
->paging_in_progress
);
5538 assert(object2
->paging_in_progress
);
5540 __TRANSPOSE_FIELD(pager_created
);
5541 __TRANSPOSE_FIELD(pager_initialized
);
5542 __TRANSPOSE_FIELD(pager_ready
);
5543 __TRANSPOSE_FIELD(pager_trusted
);
5544 __TRANSPOSE_FIELD(internal
);
5545 __TRANSPOSE_FIELD(temporary
);
5546 __TRANSPOSE_FIELD(private);
5547 __TRANSPOSE_FIELD(pageout
);
5548 __TRANSPOSE_FIELD(true_share
);
5549 __TRANSPOSE_FIELD(phys_contiguous
);
5550 __TRANSPOSE_FIELD(nophyscache
);
5551 __TRANSPOSE_FIELD(last_alloc
);
5552 __TRANSPOSE_FIELD(sequential
);
5553 __TRANSPOSE_FIELD(cluster_size
);
5554 __TRANSPOSE_FIELD(existence_map
);
5555 __TRANSPOSE_FIELD(cow_hint
);
5556 __TRANSPOSE_FIELD(wimg_bits
);
5558 #undef __TRANSPOSE_FIELD
5560 retval
= KERN_SUCCESS
;
5566 if (tmp_object
!= VM_OBJECT_NULL
) {
5567 vm_object_paging_end(tmp_object
);
5568 vm_object_unlock(tmp_object
);
5570 * Re-initialize the temporary object to avoid
5571 * deallocating a real pager.
5573 _vm_object_allocate(transpose_size
, tmp_object
);
5574 vm_object_deallocate(tmp_object
);
5575 tmp_object
= VM_OBJECT_NULL
;
5578 if (object1_locked
) {
5579 vm_object_unlock(object1
);
5580 object1_locked
= FALSE
;
5582 if (object2_locked
) {
5583 vm_object_unlock(object2
);
5584 object2_locked
= FALSE
;
5586 if (object1_paging
) {
5587 vm_object_lock(object1
);
5588 vm_object_paging_end(object1
);
5589 vm_object_unlock(object1
);
5590 object1_paging
= FALSE
;
5592 if (object2_paging
) {
5593 vm_object_lock(object2
);
5594 vm_object_paging_end(object2
);
5595 vm_object_unlock(object2
);
5596 object2_paging
= FALSE
;
5603 /* Allow manipulation of individual page state. This is actually part of */
5604 /* the UPL regimen but takes place on the VM object rather than on a UPL */
5609 vm_object_offset_t offset
,
5611 ppnum_t
*phys_entry
,
5616 vm_object_lock(object
);
5618 if(ops
& UPL_POP_PHYSICAL
) {
5619 if(object
->phys_contiguous
) {
5621 *phys_entry
= (ppnum_t
)
5622 (object
->shadow_offset
>> 12);
5624 vm_object_unlock(object
);
5625 return KERN_SUCCESS
;
5627 vm_object_unlock(object
);
5628 return KERN_INVALID_OBJECT
;
5631 if(object
->phys_contiguous
) {
5632 vm_object_unlock(object
);
5633 return KERN_INVALID_OBJECT
;
5637 if((dst_page
= vm_page_lookup(object
,offset
)) == VM_PAGE_NULL
) {
5638 vm_object_unlock(object
);
5639 return KERN_FAILURE
;
5642 /* Sync up on getting the busy bit */
5643 if((dst_page
->busy
|| dst_page
->cleaning
) &&
5644 (((ops
& UPL_POP_SET
) &&
5645 (ops
& UPL_POP_BUSY
)) || (ops
& UPL_POP_DUMP
))) {
5646 /* someone else is playing with the page, we will */
5648 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
5652 if (ops
& UPL_POP_DUMP
) {
5653 vm_page_lock_queues();
5655 if (dst_page
->no_isync
== FALSE
)
5656 pmap_disconnect(dst_page
->phys_page
);
5657 vm_page_free(dst_page
);
5659 vm_page_unlock_queues();
5666 /* Get the condition of flags before requested ops */
5667 /* are undertaken */
5669 if(dst_page
->dirty
) *flags
|= UPL_POP_DIRTY
;
5670 if(dst_page
->pageout
) *flags
|= UPL_POP_PAGEOUT
;
5671 if(dst_page
->precious
) *flags
|= UPL_POP_PRECIOUS
;
5672 if(dst_page
->absent
) *flags
|= UPL_POP_ABSENT
;
5673 if(dst_page
->busy
) *flags
|= UPL_POP_BUSY
;
5676 /* The caller should have made a call either contingent with */
5677 /* or prior to this call to set UPL_POP_BUSY */
5678 if(ops
& UPL_POP_SET
) {
5679 /* The protection granted with this assert will */
5680 /* not be complete. If the caller violates the */
5681 /* convention and attempts to change page state */
5682 /* without first setting busy we may not see it */
5683 /* because the page may already be busy. However */
5684 /* if such violations occur we will assert sooner */
5686 assert(dst_page
->busy
|| (ops
& UPL_POP_BUSY
));
5687 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= TRUE
;
5688 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= TRUE
;
5689 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= TRUE
;
5690 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= TRUE
;
5691 if (ops
& UPL_POP_BUSY
) dst_page
->busy
= TRUE
;
5694 if(ops
& UPL_POP_CLR
) {
5695 assert(dst_page
->busy
);
5696 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= FALSE
;
5697 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= FALSE
;
5698 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= FALSE
;
5699 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= FALSE
;
5700 if (ops
& UPL_POP_BUSY
) {
5701 dst_page
->busy
= FALSE
;
5702 PAGE_WAKEUP(dst_page
);
5706 if (dst_page
->encrypted
) {
5709 * We need to decrypt this encrypted page before the
5710 * caller can access its contents.
5711 * But if the caller really wants to access the page's
5712 * contents, they have to keep the page "busy".
5713 * Otherwise, the page could get recycled or re-encrypted
5716 if ((ops
& UPL_POP_SET
) && (ops
& UPL_POP_BUSY
) &&
5719 * The page is stable enough to be accessed by
5720 * the caller, so make sure its contents are
5723 vm_page_decrypt(dst_page
, 0);
5726 * The page is not busy, so don't bother
5727 * decrypting it, since anything could
5728 * happen to it between now and when the
5729 * caller wants to access it.
5730 * We should not give the caller access
5733 assert(!phys_entry
);
5739 * The physical page number will remain valid
5740 * only if the page is kept busy.
5741 * ENCRYPTED SWAP: make sure we don't let the
5742 * caller access an encrypted page.
5744 assert(dst_page
->busy
);
5745 assert(!dst_page
->encrypted
);
5746 *phys_entry
= dst_page
->phys_page
;
5752 vm_object_unlock(object
);
5753 return KERN_SUCCESS
;
5758 * vm_object_range_op offers performance enhancement over
5759 * vm_object_page_op for page_op functions which do not require page
5760 * level state to be returned from the call. Page_op was created to provide
5761 * a low-cost alternative to page manipulation via UPLs when only a single
5762 * page was involved. The range_op call establishes the ability in the _op
5763 * family of functions to work on multiple pages where the lack of page level
5764 * state handling allows the caller to avoid the overhead of the upl structures.
5770 vm_object_offset_t offset_beg
,
5771 vm_object_offset_t offset_end
,
5775 vm_object_offset_t offset
;
5778 if (object
->resident_page_count
== 0) {
5780 if (ops
& UPL_ROP_PRESENT
)
5783 *range
= offset_end
- offset_beg
;
5785 return KERN_SUCCESS
;
5787 vm_object_lock(object
);
5789 if (object
->phys_contiguous
) {
5790 vm_object_unlock(object
);
5791 return KERN_INVALID_OBJECT
;
5794 offset
= offset_beg
;
5796 while (offset
< offset_end
) {
5797 dst_page
= vm_page_lookup(object
, offset
);
5798 if (dst_page
!= VM_PAGE_NULL
) {
5799 if (ops
& UPL_ROP_DUMP
) {
5800 if (dst_page
->busy
|| dst_page
->cleaning
) {
5802 * someone else is playing with the
5803 * page, we will have to wait
5806 dst_page
, THREAD_UNINT
);
5808 * need to relook the page up since it's
5809 * state may have changed while we slept
5810 * it might even belong to a different object
5815 vm_page_lock_queues();
5817 if (dst_page
->no_isync
== FALSE
)
5818 pmap_disconnect(dst_page
->phys_page
);
5819 vm_page_free(dst_page
);
5821 vm_page_unlock_queues();
5822 } else if (ops
& UPL_ROP_ABSENT
)
5824 } else if (ops
& UPL_ROP_PRESENT
)
5827 offset
+= PAGE_SIZE
;
5829 vm_object_unlock(object
);
5832 *range
= offset
- offset_beg
;
5834 return KERN_SUCCESS
;