2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: vm/vm_object.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * Virtual memory object module.
66 #include <mach_pagemap.h>
67 #include <task_swapper.h>
69 #include <mach/mach_types.h>
70 #include <mach/memory_object.h>
71 #include <mach/memory_object_default.h>
72 #include <mach/memory_object_control_server.h>
73 #include <mach/vm_param.h>
75 #include <ipc/ipc_types.h>
76 #include <ipc/ipc_port.h>
78 #include <kern/kern_types.h>
79 #include <kern/assert.h>
80 #include <kern/lock.h>
81 #include <kern/queue.h>
83 #include <kern/zalloc.h>
84 #include <kern/host.h>
85 #include <kern/host_statistics.h>
86 #include <kern/processor.h>
87 #include <kern/misc_protos.h>
89 #include <vm/memory_object.h>
90 #include <vm/vm_fault.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_protos.h>
96 #include <vm/vm_purgeable_internal.h>
99 #include <sys/kern_memorystatus.h>
103 * Virtual memory objects maintain the actual data
104 * associated with allocated virtual memory. A given
105 * page of memory exists within exactly one object.
107 * An object is only deallocated when all "references"
110 * Associated with each object is a list of all resident
111 * memory pages belonging to that object; this list is
112 * maintained by the "vm_page" module, but locked by the object's
115 * Each object also records the memory object reference
116 * that is used by the kernel to request and write
117 * back data (the memory object, field "pager"), etc...
119 * Virtual memory objects are allocated to provide
120 * zero-filled memory (vm_allocate) or map a user-defined
121 * memory object into a virtual address space (vm_map).
123 * Virtual memory objects that refer to a user-defined
124 * memory object are called "permanent", because all changes
125 * made in virtual memory are reflected back to the
126 * memory manager, which may then store it permanently.
127 * Other virtual memory objects are called "temporary",
128 * meaning that changes need be written back only when
129 * necessary to reclaim pages, and that storage associated
130 * with the object can be discarded once it is no longer
133 * A permanent memory object may be mapped into more
134 * than one virtual address space. Moreover, two threads
135 * may attempt to make the first mapping of a memory
136 * object concurrently. Only one thread is allowed to
137 * complete this mapping; all others wait for the
138 * "pager_initialized" field is asserted, indicating
139 * that the first thread has initialized all of the
140 * necessary fields in the virtual memory object structure.
142 * The kernel relies on a *default memory manager* to
143 * provide backing storage for the zero-filled virtual
144 * memory objects. The pager memory objects associated
145 * with these temporary virtual memory objects are only
146 * requested from the default memory manager when it
147 * becomes necessary. Virtual memory objects
148 * that depend on the default memory manager are called
149 * "internal". The "pager_created" field is provided to
150 * indicate whether these ports have ever been allocated.
152 * The kernel may also create virtual memory objects to
153 * hold changed pages after a copy-on-write operation.
154 * In this case, the virtual memory object (and its
155 * backing storage -- its memory object) only contain
156 * those pages that have been changed. The "shadow"
157 * field refers to the virtual memory object that contains
158 * the remainder of the contents. The "shadow_offset"
159 * field indicates where in the "shadow" these contents begin.
160 * The "copy" field refers to a virtual memory object
161 * to which changed pages must be copied before changing
162 * this object, in order to implement another form
163 * of copy-on-write optimization.
165 * The virtual memory object structure also records
166 * the attributes associated with its memory object.
167 * The "pager_ready", "can_persist" and "copy_strategy"
168 * fields represent those attributes. The "cached_list"
169 * field is used in the implementation of the persistence
172 * ZZZ Continue this comment.
175 /* Forward declarations for internal functions. */
176 static kern_return_t
vm_object_terminate(
179 extern void vm_object_remove(
182 static kern_return_t
vm_object_copy_call(
183 vm_object_t src_object
,
184 vm_object_offset_t src_offset
,
185 vm_object_size_t size
,
186 vm_object_t
*_result_object
);
188 static void vm_object_do_collapse(
190 vm_object_t backing_object
);
192 static void vm_object_do_bypass(
194 vm_object_t backing_object
);
196 static void vm_object_release_pager(
197 memory_object_t pager
,
200 static zone_t vm_object_zone
; /* vm backing store zone */
203 * All wired-down kernel memory belongs to a single virtual
204 * memory object (kernel_object) to avoid wasting data structures.
206 static struct vm_object kernel_object_store
;
207 vm_object_t kernel_object
;
211 * The submap object is used as a placeholder for vm_map_submap
212 * operations. The object is declared in vm_map.c because it
213 * is exported by the vm_map module. The storage is declared
214 * here because it must be initialized here.
216 static struct vm_object vm_submap_object_store
;
219 * Virtual memory objects are initialized from
220 * a template (see vm_object_allocate).
222 * When adding a new field to the virtual memory
223 * object structure, be sure to add initialization
224 * (see _vm_object_allocate()).
226 static struct vm_object vm_object_template
;
228 unsigned int vm_page_purged_wired
= 0;
229 unsigned int vm_page_purged_busy
= 0;
230 unsigned int vm_page_purged_others
= 0;
234 * Virtual memory objects that are not referenced by
235 * any address maps, but that are allowed to persist
236 * (an attribute specified by the associated memory manager),
237 * are kept in a queue (vm_object_cached_list).
239 * When an object from this queue is referenced again,
240 * for example to make another address space mapping,
241 * it must be removed from the queue. That is, the
242 * queue contains *only* objects with zero references.
244 * The kernel may choose to terminate objects from this
245 * queue in order to reclaim storage. The current policy
246 * is to permit a fixed maximum number of unreferenced
247 * objects (vm_object_cached_max).
249 * A spin lock (accessed by routines
250 * vm_object_cache_{lock,lock_try,unlock}) governs the
251 * object cache. It must be held when objects are
252 * added to or removed from the cache (in vm_object_terminate).
253 * The routines that acquire a reference to a virtual
254 * memory object based on one of the memory object ports
255 * must also lock the cache.
257 * Ideally, the object cache should be more isolated
258 * from the reference mechanism, so that the lock need
259 * not be held to make simple references.
261 static vm_object_t
vm_object_cache_trim(
262 boolean_t called_from_vm_object_deallocate
);
264 static queue_head_t vm_object_cached_list
;
265 static int vm_object_cached_count
=0;
266 static int vm_object_cached_high
; /* highest # cached objects */
267 static int vm_object_cached_max
= 512; /* may be patched*/
269 static lck_mtx_t vm_object_cached_lock_data
;
270 static lck_mtx_ext_t vm_object_cached_lock_data_ext
;
272 #define vm_object_cache_lock() \
273 lck_mtx_lock(&vm_object_cached_lock_data)
274 #define vm_object_cache_lock_try() \
275 lck_mtx_try_lock(&vm_object_cached_lock_data)
276 #define vm_object_cache_lock_spin() \
277 lck_mtx_lock_spin(&vm_object_cached_lock_data)
278 #define vm_object_cache_unlock() \
279 lck_mtx_unlock(&vm_object_cached_lock_data)
281 #endif /* VM_OBJECT_CACHE */
284 static void vm_object_deactivate_all_pages(
288 #define VM_OBJECT_HASH_COUNT 1024
289 #define VM_OBJECT_HASH_LOCK_COUNT 512
291 static lck_mtx_t vm_object_hashed_lock_data
[VM_OBJECT_HASH_LOCK_COUNT
];
292 static lck_mtx_ext_t vm_object_hashed_lock_data_ext
[VM_OBJECT_HASH_LOCK_COUNT
];
294 static queue_head_t vm_object_hashtable
[VM_OBJECT_HASH_COUNT
];
295 static struct zone
*vm_object_hash_zone
;
297 struct vm_object_hash_entry
{
298 queue_chain_t hash_link
; /* hash chain link */
299 memory_object_t pager
; /* pager we represent */
300 vm_object_t object
; /* corresponding object */
301 boolean_t waiting
; /* someone waiting for
305 typedef struct vm_object_hash_entry
*vm_object_hash_entry_t
;
306 #define VM_OBJECT_HASH_ENTRY_NULL ((vm_object_hash_entry_t) 0)
308 #define VM_OBJECT_HASH_SHIFT 5
309 #define vm_object_hash(pager) \
310 ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_COUNT))
312 #define vm_object_lock_hash(pager) \
313 ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_LOCK_COUNT))
315 void vm_object_hash_entry_free(
316 vm_object_hash_entry_t entry
);
318 static void vm_object_reap(vm_object_t object
);
319 static void vm_object_reap_async(vm_object_t object
);
320 static void vm_object_reaper_thread(void);
322 static lck_mtx_t vm_object_reaper_lock_data
;
323 static lck_mtx_ext_t vm_object_reaper_lock_data_ext
;
325 static queue_head_t vm_object_reaper_queue
; /* protected by vm_object_reaper_lock() */
326 unsigned int vm_object_reap_count
= 0;
327 unsigned int vm_object_reap_count_async
= 0;
329 #define vm_object_reaper_lock() \
330 lck_mtx_lock(&vm_object_reaper_lock_data)
331 #define vm_object_reaper_lock_spin() \
332 lck_mtx_lock_spin(&vm_object_reaper_lock_data)
333 #define vm_object_reaper_unlock() \
334 lck_mtx_unlock(&vm_object_reaper_lock_data)
339 vm_object_hash_lock_spin(
340 memory_object_t pager
)
344 index
= vm_object_lock_hash(pager
);
346 lck_mtx_lock_spin(&vm_object_hashed_lock_data
[index
]);
348 return (&vm_object_hashed_lock_data
[index
]);
352 vm_object_hash_unlock(lck_mtx_t
*lck
)
359 * vm_object_hash_lookup looks up a pager in the hashtable
360 * and returns the corresponding entry, with optional removal.
362 static vm_object_hash_entry_t
363 vm_object_hash_lookup(
364 memory_object_t pager
,
365 boolean_t remove_entry
)
368 vm_object_hash_entry_t entry
;
370 bucket
= &vm_object_hashtable
[vm_object_hash(pager
)];
372 entry
= (vm_object_hash_entry_t
)queue_first(bucket
);
373 while (!queue_end(bucket
, (queue_entry_t
)entry
)) {
374 if (entry
->pager
== pager
) {
376 queue_remove(bucket
, entry
,
377 vm_object_hash_entry_t
, hash_link
);
381 entry
= (vm_object_hash_entry_t
)queue_next(&entry
->hash_link
);
383 return(VM_OBJECT_HASH_ENTRY_NULL
);
387 * vm_object_hash_enter enters the specified
388 * pager / cache object association in the hashtable.
392 vm_object_hash_insert(
393 vm_object_hash_entry_t entry
,
398 bucket
= &vm_object_hashtable
[vm_object_hash(entry
->pager
)];
400 queue_enter(bucket
, entry
, vm_object_hash_entry_t
, hash_link
);
402 entry
->object
= object
;
403 object
->hashed
= TRUE
;
406 static vm_object_hash_entry_t
407 vm_object_hash_entry_alloc(
408 memory_object_t pager
)
410 vm_object_hash_entry_t entry
;
412 entry
= (vm_object_hash_entry_t
)zalloc(vm_object_hash_zone
);
413 entry
->pager
= pager
;
414 entry
->object
= VM_OBJECT_NULL
;
415 entry
->waiting
= FALSE
;
421 vm_object_hash_entry_free(
422 vm_object_hash_entry_t entry
)
424 zfree(vm_object_hash_zone
, entry
);
428 * vm_object_allocate:
430 * Returns a new object with the given size.
433 __private_extern__
void
435 vm_object_size_t size
,
439 "vm_object_allocate, object 0x%X size 0x%X\n",
440 object
, size
, 0,0,0);
442 *object
= vm_object_template
;
443 queue_init(&object
->memq
);
444 queue_init(&object
->msr_q
);
446 queue_init(&object
->uplq
);
447 #endif /* UPL_DEBUG */
448 vm_object_lock_init(object
);
452 __private_extern__ vm_object_t
454 vm_object_size_t size
)
456 register vm_object_t object
;
458 object
= (vm_object_t
) zalloc(vm_object_zone
);
460 // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
462 if (object
!= VM_OBJECT_NULL
)
463 _vm_object_allocate(size
, object
);
469 lck_grp_t vm_object_lck_grp
;
470 lck_grp_attr_t vm_object_lck_grp_attr
;
471 lck_attr_t vm_object_lck_attr
;
472 lck_attr_t kernel_object_lck_attr
;
475 * vm_object_bootstrap:
477 * Initialize the VM objects module.
479 __private_extern__
void
480 vm_object_bootstrap(void)
484 vm_object_zone
= zinit((vm_size_t
) sizeof(struct vm_object
),
485 round_page(512*1024),
488 zone_change(vm_object_zone
, Z_NOENCRYPT
, TRUE
);
490 vm_object_init_lck_grp();
493 queue_init(&vm_object_cached_list
);
495 lck_mtx_init_ext(&vm_object_cached_lock_data
,
496 &vm_object_cached_lock_data_ext
,
498 &vm_object_lck_attr
);
500 queue_init(&vm_object_reaper_queue
);
502 for (i
= 0; i
< VM_OBJECT_HASH_LOCK_COUNT
; i
++) {
503 lck_mtx_init_ext(&vm_object_hashed_lock_data
[i
],
504 &vm_object_hashed_lock_data_ext
[i
],
506 &vm_object_lck_attr
);
508 lck_mtx_init_ext(&vm_object_reaper_lock_data
,
509 &vm_object_reaper_lock_data_ext
,
511 &vm_object_lck_attr
);
513 vm_object_hash_zone
=
514 zinit((vm_size_t
) sizeof (struct vm_object_hash_entry
),
515 round_page(512*1024),
517 "vm object hash entries");
518 zone_change(vm_object_hash_zone
, Z_NOENCRYPT
, TRUE
);
520 for (i
= 0; i
< VM_OBJECT_HASH_COUNT
; i
++)
521 queue_init(&vm_object_hashtable
[i
]);
525 * Fill in a template object, for quick initialization
528 /* memq; Lock; init after allocation */
529 vm_object_template
.memq
.prev
= NULL
;
530 vm_object_template
.memq
.next
= NULL
;
533 * We can't call vm_object_lock_init() here because that will
534 * allocate some memory and VM is not fully initialized yet.
535 * The lock will be initialized for each allocated object in
536 * _vm_object_allocate(), so we don't need to initialize it in
537 * the vm_object_template.
539 vm_object_lock_init(&vm_object_template
);
541 vm_object_template
.size
= 0;
542 vm_object_template
.memq_hint
= VM_PAGE_NULL
;
543 vm_object_template
.ref_count
= 1;
545 vm_object_template
.res_count
= 1;
546 #endif /* TASK_SWAPPER */
547 vm_object_template
.resident_page_count
= 0;
548 vm_object_template
.wired_page_count
= 0;
549 vm_object_template
.reusable_page_count
= 0;
550 vm_object_template
.copy
= VM_OBJECT_NULL
;
551 vm_object_template
.shadow
= VM_OBJECT_NULL
;
552 vm_object_template
.shadow_offset
= (vm_object_offset_t
) 0;
553 vm_object_template
.pager
= MEMORY_OBJECT_NULL
;
554 vm_object_template
.paging_offset
= 0;
555 vm_object_template
.pager_control
= MEMORY_OBJECT_CONTROL_NULL
;
556 vm_object_template
.copy_strategy
= MEMORY_OBJECT_COPY_SYMMETRIC
;
557 vm_object_template
.paging_in_progress
= 0;
558 vm_object_template
.activity_in_progress
= 0;
560 /* Begin bitfields */
561 vm_object_template
.all_wanted
= 0; /* all bits FALSE */
562 vm_object_template
.pager_created
= FALSE
;
563 vm_object_template
.pager_initialized
= FALSE
;
564 vm_object_template
.pager_ready
= FALSE
;
565 vm_object_template
.pager_trusted
= FALSE
;
566 vm_object_template
.can_persist
= FALSE
;
567 vm_object_template
.internal
= TRUE
;
568 vm_object_template
.temporary
= TRUE
;
569 vm_object_template
.private = FALSE
;
570 vm_object_template
.pageout
= FALSE
;
571 vm_object_template
.alive
= TRUE
;
572 vm_object_template
.purgable
= VM_PURGABLE_DENY
;
573 vm_object_template
.shadowed
= FALSE
;
574 vm_object_template
.silent_overwrite
= FALSE
;
575 vm_object_template
.advisory_pageout
= FALSE
;
576 vm_object_template
.true_share
= FALSE
;
577 vm_object_template
.terminating
= FALSE
;
578 vm_object_template
.named
= FALSE
;
579 vm_object_template
.shadow_severed
= FALSE
;
580 vm_object_template
.phys_contiguous
= FALSE
;
581 vm_object_template
.nophyscache
= FALSE
;
584 vm_object_template
.cached_list
.prev
= NULL
;
585 vm_object_template
.cached_list
.next
= NULL
;
586 vm_object_template
.msr_q
.prev
= NULL
;
587 vm_object_template
.msr_q
.next
= NULL
;
589 vm_object_template
.last_alloc
= (vm_object_offset_t
) 0;
590 vm_object_template
.sequential
= (vm_object_offset_t
) 0;
591 vm_object_template
.pages_created
= 0;
592 vm_object_template
.pages_used
= 0;
595 vm_object_template
.existence_map
= VM_EXTERNAL_NULL
;
596 #endif /* MACH_PAGEMAP */
597 vm_object_template
.cow_hint
= ~(vm_offset_t
)0;
599 vm_object_template
.paging_object
= VM_OBJECT_NULL
;
600 #endif /* MACH_ASSERT */
602 /* cache bitfields */
603 vm_object_template
.wimg_bits
= VM_WIMG_DEFAULT
;
604 vm_object_template
.code_signed
= FALSE
;
605 vm_object_template
.hashed
= FALSE
;
606 vm_object_template
.transposed
= FALSE
;
607 vm_object_template
.mapping_in_progress
= FALSE
;
608 vm_object_template
.volatile_empty
= FALSE
;
609 vm_object_template
.volatile_fault
= FALSE
;
610 vm_object_template
.all_reusable
= FALSE
;
611 vm_object_template
.blocked_access
= FALSE
;
612 vm_object_template
.__object2_unused_bits
= 0;
614 vm_object_template
.uplq
.prev
= NULL
;
615 vm_object_template
.uplq
.next
= NULL
;
616 #endif /* UPL_DEBUG */
618 bzero(&vm_object_template
.pip_holders
,
619 sizeof (vm_object_template
.pip_holders
));
620 #endif /* VM_PIP_DEBUG */
622 vm_object_template
.objq
.next
=NULL
;
623 vm_object_template
.objq
.prev
=NULL
;
627 * Initialize the "kernel object"
630 kernel_object
= &kernel_object_store
;
633 * Note that in the following size specifications, we need to add 1 because
634 * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size.
638 _vm_object_allocate(vm_last_addr
+ 1,
641 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
644 kernel_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
647 * Initialize the "submap object". Make it as large as the
648 * kernel object so that no limit is imposed on submap sizes.
651 vm_submap_object
= &vm_submap_object_store
;
653 _vm_object_allocate(vm_last_addr
+ 1,
656 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
659 vm_submap_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
662 * Create an "extra" reference to this object so that we never
663 * try to deallocate it; zfree doesn't like to be called with
666 vm_object_reference(vm_submap_object
);
669 vm_external_module_initialize();
670 #endif /* MACH_PAGEMAP */
674 vm_object_reaper_init(void)
679 kr
= kernel_thread_start_priority(
680 (thread_continue_t
) vm_object_reaper_thread
,
684 if (kr
!= KERN_SUCCESS
) {
685 panic("failed to launch vm_object_reaper_thread kr=0x%x", kr
);
687 thread_deallocate(thread
);
690 __private_extern__
void
694 * Finish initializing the kernel object.
699 __private_extern__
void
700 vm_object_init_lck_grp(void)
703 * initialze the vm_object lock world
705 lck_grp_attr_setdefault(&vm_object_lck_grp_attr
);
706 lck_grp_init(&vm_object_lck_grp
, "vm_object", &vm_object_lck_grp_attr
);
707 lck_attr_setdefault(&vm_object_lck_attr
);
708 lck_attr_setdefault(&kernel_object_lck_attr
);
709 lck_attr_cleardebug(&kernel_object_lck_attr
);
713 #define MIGHT_NOT_CACHE_SHADOWS 1
714 #if MIGHT_NOT_CACHE_SHADOWS
715 static int cache_shadows
= TRUE
;
716 #endif /* MIGHT_NOT_CACHE_SHADOWS */
720 * vm_object_deallocate:
722 * Release a reference to the specified object,
723 * gained either through a vm_object_allocate
724 * or a vm_object_reference call. When all references
725 * are gone, storage associated with this object
726 * may be relinquished.
728 * No object may be locked.
730 unsigned long vm_object_deallocate_shared_successes
= 0;
731 unsigned long vm_object_deallocate_shared_failures
= 0;
732 unsigned long vm_object_deallocate_shared_swap_failures
= 0;
733 __private_extern__
void
734 vm_object_deallocate(
735 register vm_object_t object
)
738 boolean_t retry_cache_trim
= FALSE
;
739 uint32_t try_failed_count
= 0;
741 vm_object_t shadow
= VM_OBJECT_NULL
;
743 // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
744 // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
746 if (object
== VM_OBJECT_NULL
)
749 if (object
== kernel_object
) {
750 vm_object_lock_shared(object
);
752 OSAddAtomic(-1, &object
->ref_count
);
754 if (object
->ref_count
== 0) {
755 panic("vm_object_deallocate: losing kernel_object\n");
757 vm_object_unlock(object
);
761 if (object
->ref_count
> 2 ||
762 (!object
->named
&& object
->ref_count
> 1)) {
763 UInt32 original_ref_count
;
764 volatile UInt32
*ref_count_p
;
768 * The object currently looks like it is not being
769 * kept alive solely by the reference we're about to release.
770 * Let's try and release our reference without taking
771 * all the locks we would need if we had to terminate the
772 * object (cache lock + exclusive object lock).
773 * Lock the object "shared" to make sure we don't race with
774 * anyone holding it "exclusive".
776 vm_object_lock_shared(object
);
777 ref_count_p
= (volatile UInt32
*) &object
->ref_count
;
778 original_ref_count
= object
->ref_count
;
780 * Test again as "ref_count" could have changed.
781 * "named" shouldn't change.
783 if (original_ref_count
> 2 ||
784 (!object
->named
&& original_ref_count
> 1)) {
785 atomic_swap
= OSCompareAndSwap(
787 original_ref_count
- 1,
788 (UInt32
*) &object
->ref_count
);
789 if (atomic_swap
== FALSE
) {
790 vm_object_deallocate_shared_swap_failures
++;
796 vm_object_unlock(object
);
800 * ref_count was updated atomically !
802 vm_object_deallocate_shared_successes
++;
807 * Someone else updated the ref_count at the same
808 * time and we lost the race. Fall back to the usual
809 * slow but safe path...
811 vm_object_deallocate_shared_failures
++;
814 while (object
!= VM_OBJECT_NULL
) {
816 vm_object_lock(object
);
818 assert(object
->ref_count
> 0);
821 * If the object has a named reference, and only
822 * that reference would remain, inform the pager
823 * about the last "mapping" reference going away.
825 if ((object
->ref_count
== 2) && (object
->named
)) {
826 memory_object_t pager
= object
->pager
;
828 /* Notify the Pager that there are no */
829 /* more mappers for this object */
831 if (pager
!= MEMORY_OBJECT_NULL
) {
832 vm_object_mapping_wait(object
, THREAD_UNINT
);
833 vm_object_mapping_begin(object
);
834 vm_object_unlock(object
);
836 memory_object_last_unmap(pager
);
838 vm_object_lock(object
);
839 vm_object_mapping_end(object
);
842 * recheck the ref_count since we dropped the object lock
843 * to call 'memory_object_last_unmap'... it's possible
844 * additional references got taken and we only want
845 * to deactivate the pages if this 'named' object will only
846 * referenced by the backing pager once we drop our reference
849 if (!object
->terminating
&& object
->ref_count
== 2)
850 vm_object_deactivate_all_pages(object
);
852 assert(object
->ref_count
> 0);
856 * Lose the reference. If other references
857 * remain, then we are done, unless we need
858 * to retry a cache trim.
859 * If it is the last reference, then keep it
860 * until any pending initialization is completed.
863 /* if the object is terminating, it cannot go into */
864 /* the cache and we obviously should not call */
865 /* terminate again. */
867 if ((object
->ref_count
> 1) || object
->terminating
) {
868 vm_object_lock_assert_exclusive(object
);
870 vm_object_res_deallocate(object
);
872 if (object
->ref_count
== 1 &&
873 object
->shadow
!= VM_OBJECT_NULL
) {
875 * There's only one reference left on this
876 * VM object. We can't tell if it's a valid
877 * one (from a mapping for example) or if this
878 * object is just part of a possibly stale and
879 * useless shadow chain.
880 * We would like to try and collapse it into
881 * its parent, but we don't have any pointers
882 * back to this parent object.
883 * But we can try and collapse this object with
884 * its own shadows, in case these are useless
886 * We can't bypass this object though, since we
887 * don't know if this last reference on it is
890 vm_object_collapse(object
, 0, FALSE
);
892 vm_object_unlock(object
);
894 if (retry_cache_trim
&&
895 ((object
= vm_object_cache_trim(TRUE
)) !=
904 * We have to wait for initialization
905 * before destroying or caching the object.
908 if (object
->pager_created
&& ! object
->pager_initialized
) {
909 assert(! object
->can_persist
);
910 vm_object_assert_wait(object
,
911 VM_OBJECT_EVENT_INITIALIZED
,
913 vm_object_unlock(object
);
915 thread_block(THREAD_CONTINUE_NULL
);
921 * If this object can persist, then enter it in
922 * the cache. Otherwise, terminate it.
924 * NOTE: Only permanent objects are cached, and
925 * permanent objects cannot have shadows. This
926 * affects the residence counting logic in a minor
927 * way (can do it in-line, mostly).
930 if ((object
->can_persist
) && (object
->alive
)) {
932 * Now it is safe to decrement reference count,
933 * and to return if reference count is > 0.
936 vm_object_lock_assert_exclusive(object
);
937 if (--object
->ref_count
> 0) {
938 vm_object_res_deallocate(object
);
939 vm_object_unlock(object
);
941 if (retry_cache_trim
&&
942 ((object
= vm_object_cache_trim(TRUE
)) !=
949 #if MIGHT_NOT_CACHE_SHADOWS
951 * Remove shadow now if we don't
952 * want to cache shadows.
954 if (! cache_shadows
) {
955 shadow
= object
->shadow
;
956 object
->shadow
= VM_OBJECT_NULL
;
958 #endif /* MIGHT_NOT_CACHE_SHADOWS */
961 * Enter the object onto the queue of
962 * cached objects, and deactivate
965 assert(object
->shadow
== VM_OBJECT_NULL
);
966 VM_OBJ_RES_DECR(object
);
968 "vm_o_deallocate: adding %x to cache, queue = (%x, %x)\n",
970 vm_object_cached_list
.next
,
971 vm_object_cached_list
.prev
,0,0);
974 vm_object_unlock(object
);
976 try_failed_count
= 0;
978 vm_object_cache_lock();
981 * if we try to take a regular lock here
982 * we risk deadlocking against someone
983 * holding a lock on this object while
984 * trying to vm_object_deallocate a different
987 if (vm_object_lock_try(object
))
989 vm_object_cache_unlock();
992 mutex_pause(try_failed_count
); /* wait a bit */
994 vm_object_cached_count
++;
995 if (vm_object_cached_count
> vm_object_cached_high
)
996 vm_object_cached_high
= vm_object_cached_count
;
997 queue_enter(&vm_object_cached_list
, object
,
998 vm_object_t
, cached_list
);
999 vm_object_cache_unlock();
1001 vm_object_deactivate_all_pages(object
);
1002 vm_object_unlock(object
);
1004 #if MIGHT_NOT_CACHE_SHADOWS
1006 * If we have a shadow that we need
1007 * to deallocate, do so now, remembering
1008 * to trim the cache later.
1010 if (! cache_shadows
&& shadow
!= VM_OBJECT_NULL
) {
1012 retry_cache_trim
= TRUE
;
1015 #endif /* MIGHT_NOT_CACHE_SHADOWS */
1018 * Trim the cache. If the cache trim
1019 * returns with a shadow for us to deallocate,
1020 * then remember to retry the cache trim
1021 * when we are done deallocating the shadow.
1022 * Otherwise, we are done.
1025 object
= vm_object_cache_trim(TRUE
);
1026 if (object
== VM_OBJECT_NULL
) {
1029 retry_cache_trim
= TRUE
;
1031 #endif /* VM_OBJECT_CACHE */
1034 * This object is not cachable; terminate it.
1037 "vm_o_deallocate: !cacheable 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
1038 object
, object
->resident_page_count
,
1039 object
->paging_in_progress
,
1040 (void *)current_thread(),object
->ref_count
);
1042 VM_OBJ_RES_DECR(object
); /* XXX ? */
1044 * Terminate this object. If it had a shadow,
1045 * then deallocate it; otherwise, if we need
1046 * to retry a cache trim, do so now; otherwise,
1047 * we are done. "pageout" objects have a shadow,
1048 * but maintain a "paging reference" rather than
1049 * a normal reference.
1051 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
1053 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
1056 if (shadow
!= VM_OBJECT_NULL
) {
1061 if (retry_cache_trim
&&
1062 ((object
= vm_object_cache_trim(TRUE
)) !=
1071 assert(! retry_cache_trim
);
1078 * Check to see whether we really need to trim
1079 * down the cache. If so, remove an object from
1080 * the cache, terminate it, and repeat.
1082 * Called with, and returns with, cache lock unlocked.
1085 vm_object_cache_trim(
1086 boolean_t called_from_vm_object_deallocate
)
1088 register vm_object_t object
= VM_OBJECT_NULL
;
1094 * If we no longer need to trim the cache,
1097 if (vm_object_cached_count
<= vm_object_cached_max
)
1098 return VM_OBJECT_NULL
;
1100 vm_object_cache_lock();
1101 if (vm_object_cached_count
<= vm_object_cached_max
) {
1102 vm_object_cache_unlock();
1103 return VM_OBJECT_NULL
;
1107 * We must trim down the cache, so remove
1108 * the first object in the cache.
1111 "vm_object_cache_trim: removing from front of cache (%x, %x)\n",
1112 vm_object_cached_list
.next
,
1113 vm_object_cached_list
.prev
, 0, 0, 0);
1115 object
= (vm_object_t
) queue_first(&vm_object_cached_list
);
1116 if(object
== (vm_object_t
) &vm_object_cached_list
) {
1117 /* something's wrong with the calling parameter or */
1118 /* the value of vm_object_cached_count, just fix */
1120 if(vm_object_cached_max
< 0)
1121 vm_object_cached_max
= 0;
1122 vm_object_cached_count
= 0;
1123 vm_object_cache_unlock();
1124 return VM_OBJECT_NULL
;
1126 vm_object_lock(object
);
1127 queue_remove(&vm_object_cached_list
, object
, vm_object_t
,
1129 vm_object_cached_count
--;
1131 vm_object_cache_unlock();
1133 * Since this object is in the cache, we know
1134 * that it is initialized and has no references.
1135 * Take a reference to avoid recursive deallocations.
1138 assert(object
->pager_initialized
);
1139 assert(object
->ref_count
== 0);
1140 vm_object_lock_assert_exclusive(object
);
1141 object
->ref_count
++;
1144 * Terminate the object.
1145 * If the object had a shadow, we let vm_object_deallocate
1146 * deallocate it. "pageout" objects have a shadow, but
1147 * maintain a "paging reference" rather than a normal
1149 * (We are careful here to limit recursion.)
1151 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
1153 if(vm_object_terminate(object
) != KERN_SUCCESS
)
1156 if (shadow
!= VM_OBJECT_NULL
) {
1157 if (called_from_vm_object_deallocate
) {
1160 vm_object_deallocate(shadow
);
1169 * Routine: vm_object_terminate
1171 * Free all resources associated with a vm_object.
1172 * In/out conditions:
1173 * Upon entry, the object must be locked,
1174 * and the object must have exactly one reference.
1176 * The shadow object reference is left alone.
1178 * The object must be unlocked if its found that pages
1179 * must be flushed to a backing object. If someone
1180 * manages to map the object while it is being flushed
1181 * the object is returned unlocked and unchanged. Otherwise,
1182 * upon exit, the cache will be unlocked, and the
1183 * object will cease to exist.
1185 static kern_return_t
1186 vm_object_terminate(
1189 vm_object_t shadow_object
;
1191 XPR(XPR_VM_OBJECT
, "vm_object_terminate, object 0x%X ref %d\n",
1192 object
, object
->ref_count
, 0, 0, 0);
1194 if (!object
->pageout
&& (!object
->temporary
|| object
->can_persist
) &&
1195 (object
->pager
!= NULL
|| object
->shadow_severed
)) {
1197 * Clear pager_trusted bit so that the pages get yanked
1198 * out of the object instead of cleaned in place. This
1199 * prevents a deadlock in XMM and makes more sense anyway.
1201 object
->pager_trusted
= FALSE
;
1203 vm_object_reap_pages(object
, REAP_TERMINATE
);
1206 * Make sure the object isn't already being terminated
1208 if (object
->terminating
) {
1209 vm_object_lock_assert_exclusive(object
);
1210 object
->ref_count
--;
1211 assert(object
->ref_count
> 0);
1212 vm_object_unlock(object
);
1213 return KERN_FAILURE
;
1217 * Did somebody get a reference to the object while we were
1220 if (object
->ref_count
!= 1) {
1221 vm_object_lock_assert_exclusive(object
);
1222 object
->ref_count
--;
1223 assert(object
->ref_count
> 0);
1224 vm_object_res_deallocate(object
);
1225 vm_object_unlock(object
);
1226 return KERN_FAILURE
;
1230 * Make sure no one can look us up now.
1233 object
->terminating
= TRUE
;
1234 object
->alive
= FALSE
;
1236 if (object
->hashed
) {
1239 lck
= vm_object_hash_lock_spin(object
->pager
);
1240 vm_object_remove(object
);
1241 vm_object_hash_unlock(lck
);
1244 * Detach the object from its shadow if we are the shadow's
1245 * copy. The reference we hold on the shadow must be dropped
1248 if (((shadow_object
= object
->shadow
) != VM_OBJECT_NULL
) &&
1249 !(object
->pageout
)) {
1250 vm_object_lock(shadow_object
);
1251 if (shadow_object
->copy
== object
)
1252 shadow_object
->copy
= VM_OBJECT_NULL
;
1253 vm_object_unlock(shadow_object
);
1256 if (object
->paging_in_progress
!= 0 ||
1257 object
->activity_in_progress
!= 0) {
1259 * There are still some paging_in_progress references
1260 * on this object, meaning that there are some paging
1261 * or other I/O operations in progress for this VM object.
1262 * Such operations take some paging_in_progress references
1263 * up front to ensure that the object doesn't go away, but
1264 * they may also need to acquire a reference on the VM object,
1265 * to map it in kernel space, for example. That means that
1266 * they may end up releasing the last reference on the VM
1267 * object, triggering its termination, while still holding
1268 * paging_in_progress references. Waiting for these
1269 * pending paging_in_progress references to go away here would
1272 * To avoid deadlocking, we'll let the vm_object_reaper_thread
1273 * complete the VM object termination if it still holds
1274 * paging_in_progress references at this point.
1276 * No new paging_in_progress should appear now that the
1277 * VM object is "terminating" and not "alive".
1279 vm_object_reap_async(object
);
1280 vm_object_unlock(object
);
1282 * Return KERN_FAILURE to let the caller know that we
1283 * haven't completed the termination and it can't drop this
1284 * object's reference on its shadow object yet.
1285 * The reaper thread will take care of that once it has
1286 * completed this object's termination.
1288 return KERN_FAILURE
;
1291 * complete the VM object termination
1293 vm_object_reap(object
);
1294 object
= VM_OBJECT_NULL
;
1297 * the object lock was released by vm_object_reap()
1299 * KERN_SUCCESS means that this object has been terminated
1300 * and no longer needs its shadow object but still holds a
1302 * The caller is responsible for dropping that reference.
1303 * We can't call vm_object_deallocate() here because that
1304 * would create a recursion.
1306 return KERN_SUCCESS
;
1313 * Complete the termination of a VM object after it's been marked
1314 * as "terminating" and "!alive" by vm_object_terminate().
1316 * The VM object must be locked by caller.
1317 * The lock will be released on return and the VM object is no longer valid.
1323 memory_object_t pager
;
1325 vm_object_lock_assert_exclusive(object
);
1326 assert(object
->paging_in_progress
== 0);
1327 assert(object
->activity_in_progress
== 0);
1329 vm_object_reap_count
++;
1331 pager
= object
->pager
;
1332 object
->pager
= MEMORY_OBJECT_NULL
;
1334 if (pager
!= MEMORY_OBJECT_NULL
)
1335 memory_object_control_disable(object
->pager_control
);
1337 object
->ref_count
--;
1339 assert(object
->res_count
== 0);
1340 #endif /* TASK_SWAPPER */
1342 assert (object
->ref_count
== 0);
1345 * remove from purgeable queue if it's on
1347 if (object
->objq
.next
|| object
->objq
.prev
) {
1348 purgeable_q_t queue
= vm_purgeable_object_remove(object
);
1351 /* Must take page lock for this - using it to protect token queue */
1352 vm_page_lock_queues();
1353 vm_purgeable_token_delete_first(queue
);
1355 assert(queue
->debug_count_objects
>=0);
1356 vm_page_unlock_queues();
1360 * Clean or free the pages, as appropriate.
1361 * It is possible for us to find busy/absent pages,
1362 * if some faults on this object were aborted.
1364 if (object
->pageout
) {
1365 assert(object
->shadow
!= VM_OBJECT_NULL
);
1367 vm_pageout_object_terminate(object
);
1369 } else if (((object
->temporary
&& !object
->can_persist
) || (pager
== MEMORY_OBJECT_NULL
))) {
1371 vm_object_reap_pages(object
, REAP_REAP
);
1373 assert(queue_empty(&object
->memq
));
1374 assert(object
->paging_in_progress
== 0);
1375 assert(object
->activity_in_progress
== 0);
1376 assert(object
->ref_count
== 0);
1379 * If the pager has not already been released by
1380 * vm_object_destroy, we need to terminate it and
1381 * release our reference to it here.
1383 if (pager
!= MEMORY_OBJECT_NULL
) {
1384 vm_object_unlock(object
);
1385 vm_object_release_pager(pager
, object
->hashed
);
1386 vm_object_lock(object
);
1389 /* kick off anyone waiting on terminating */
1390 object
->terminating
= FALSE
;
1391 vm_object_paging_begin(object
);
1392 vm_object_paging_end(object
);
1393 vm_object_unlock(object
);
1396 vm_external_destroy(object
->existence_map
, object
->size
);
1397 #endif /* MACH_PAGEMAP */
1399 object
->shadow
= VM_OBJECT_NULL
;
1401 vm_object_lock_destroy(object
);
1403 * Free the space for the object.
1405 zfree(vm_object_zone
, object
);
1406 object
= VM_OBJECT_NULL
;
1411 #define V_O_R_MAX_BATCH 128
1414 #define VM_OBJ_REAP_FREELIST(_local_free_q, do_disconnect) \
1416 if (_local_free_q) { \
1417 if (do_disconnect) { \
1419 for (m = _local_free_q; \
1420 m != VM_PAGE_NULL; \
1421 m = (vm_page_t) m->pageq.next) { \
1423 pmap_disconnect(m->phys_page); \
1427 vm_page_free_list(_local_free_q, TRUE); \
1428 _local_free_q = VM_PAGE_NULL; \
1434 vm_object_reap_pages(
1440 vm_page_t local_free_q
= VM_PAGE_NULL
;
1442 boolean_t disconnect_on_release
;
1444 if (reap_type
== REAP_DATA_FLUSH
) {
1446 * We need to disconnect pages from all pmaps before
1447 * releasing them to the free list
1449 disconnect_on_release
= TRUE
;
1452 * Either the caller has already disconnected the pages
1453 * from all pmaps, or we disconnect them here as we add
1454 * them to out local list of pages to be released.
1455 * No need to re-disconnect them when we release the pages
1458 disconnect_on_release
= FALSE
;
1461 restart_after_sleep
:
1462 if (queue_empty(&object
->memq
))
1464 loop_count
= V_O_R_MAX_BATCH
+ 1;
1466 vm_page_lockspin_queues();
1468 next
= (vm_page_t
)queue_first(&object
->memq
);
1470 while (!queue_end(&object
->memq
, (queue_entry_t
)next
)) {
1473 next
= (vm_page_t
)queue_next(&next
->listq
);
1475 if (--loop_count
== 0) {
1477 vm_page_unlock_queues();
1481 * Free the pages we reclaimed so far
1482 * and take a little break to avoid
1483 * hogging the page queue lock too long
1485 VM_OBJ_REAP_FREELIST(local_free_q
,
1486 disconnect_on_release
);
1490 loop_count
= V_O_R_MAX_BATCH
+ 1;
1492 vm_page_lockspin_queues();
1494 if (reap_type
== REAP_DATA_FLUSH
|| reap_type
== REAP_TERMINATE
) {
1496 if (reap_type
== REAP_DATA_FLUSH
&&
1497 ((p
->pageout
== TRUE
|| p
->cleaning
== TRUE
) && p
->list_req_pending
== TRUE
)) {
1498 p
->list_req_pending
= FALSE
;
1499 p
->cleaning
= FALSE
;
1501 * need to drop the laundry count...
1502 * we may also need to remove it
1503 * from the I/O paging queue...
1504 * vm_pageout_throttle_up handles both cases
1506 * the laundry and pageout_queue flags are cleared...
1510 vm_pageout_throttle_up(p
);
1512 vm_pageout_throttle_up(p
);
1514 if (p
->pageout
== TRUE
) {
1516 * toss the wire count we picked up
1517 * when we initially set this page up
1518 * to be cleaned and stolen...
1520 vm_page_unwire(p
, TRUE
);
1525 } else if (p
->busy
|| p
->cleaning
) {
1527 vm_page_unlock_queues();
1529 * free the pages reclaimed so far
1531 VM_OBJ_REAP_FREELIST(local_free_q
,
1532 disconnect_on_release
);
1534 PAGE_SLEEP(object
, p
, THREAD_UNINT
);
1536 goto restart_after_sleep
;
1539 switch (reap_type
) {
1541 case REAP_DATA_FLUSH
:
1542 if (VM_PAGE_WIRED(p
)) {
1544 * this is an odd case... perhaps we should
1545 * zero-fill this page since we're conceptually
1546 * tossing its data at this point, but leaving
1547 * it on the object to honor the 'wire' contract
1553 case REAP_PURGEABLE
:
1554 if (VM_PAGE_WIRED(p
)) {
1555 /* can't purge a wired page */
1556 vm_page_purged_wired
++;
1562 * We can't reclaim a busy page but we can
1563 * make it pageable (it's not wired) to make
1564 * sure that it gets considered by
1565 * vm_pageout_scan() later.
1567 vm_page_deactivate(p
);
1568 vm_page_purged_busy
++;
1572 if (p
->cleaning
|| p
->laundry
|| p
->list_req_pending
) {
1574 * page is being acted upon,
1575 * so don't mess with it
1577 vm_page_purged_others
++;
1580 assert(p
->object
!= kernel_object
);
1583 * we can discard this page...
1585 if (p
->pmapped
== TRUE
) {
1590 refmod_state
= pmap_disconnect(p
->phys_page
);
1591 if (refmod_state
& VM_MEM_MODIFIED
) {
1595 if (p
->dirty
|| p
->precious
) {
1597 * we saved the cost of cleaning this page !
1599 vm_page_purged_count
++;
1604 case REAP_TERMINATE
:
1605 if (p
->absent
|| p
->private) {
1607 * For private pages, VM_PAGE_FREE just
1608 * leaves the page structure around for
1609 * its owner to clean up. For absent
1610 * pages, the structure is returned to
1611 * the appropriate pool.
1615 if (p
->fictitious
) {
1616 assert (p
->phys_page
== vm_page_guard_addr
);
1619 if (!p
->dirty
&& p
->wpmapped
)
1620 p
->dirty
= pmap_is_modified(p
->phys_page
);
1622 if ((p
->dirty
|| p
->precious
) && !p
->error
&& object
->alive
) {
1626 VM_PAGE_QUEUES_REMOVE(p
);
1628 vm_page_unlock_queues();
1630 * free the pages reclaimed so far
1632 VM_OBJ_REAP_FREELIST(local_free_q
,
1633 disconnect_on_release
);
1636 * flush page... page will be freed
1637 * upon completion of I/O
1639 vm_pageout_cluster(p
);
1640 vm_object_paging_wait(object
, THREAD_UNINT
);
1642 goto restart_after_sleep
;
1649 vm_page_free_prepare_queues(p
);
1650 assert(p
->pageq
.next
== NULL
&& p
->pageq
.prev
== NULL
);
1652 * Add this page to our list of reclaimed pages,
1653 * to be freed later.
1655 p
->pageq
.next
= (queue_entry_t
) local_free_q
;
1658 vm_page_unlock_queues();
1661 * Free the remaining reclaimed pages
1663 VM_OBJ_REAP_FREELIST(local_free_q
,
1664 disconnect_on_release
);
1669 vm_object_reap_async(
1672 vm_object_lock_assert_exclusive(object
);
1674 vm_object_reaper_lock_spin();
1676 vm_object_reap_count_async
++;
1678 /* enqueue the VM object... */
1679 queue_enter(&vm_object_reaper_queue
, object
,
1680 vm_object_t
, cached_list
);
1682 vm_object_reaper_unlock();
1684 /* ... and wake up the reaper thread */
1685 thread_wakeup((event_t
) &vm_object_reaper_queue
);
1690 vm_object_reaper_thread(void)
1692 vm_object_t object
, shadow_object
;
1694 vm_object_reaper_lock_spin();
1696 while (!queue_empty(&vm_object_reaper_queue
)) {
1697 queue_remove_first(&vm_object_reaper_queue
,
1702 vm_object_reaper_unlock();
1703 vm_object_lock(object
);
1705 assert(object
->terminating
);
1706 assert(!object
->alive
);
1709 * The pageout daemon might be playing with our pages.
1710 * Now that the object is dead, it won't touch any more
1711 * pages, but some pages might already be on their way out.
1712 * Hence, we wait until the active paging activities have
1713 * ceased before we break the association with the pager
1716 while (object
->paging_in_progress
!= 0 ||
1717 object
->activity_in_progress
!= 0) {
1718 vm_object_wait(object
,
1719 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
1721 vm_object_lock(object
);
1725 object
->pageout
? VM_OBJECT_NULL
: object
->shadow
;
1727 vm_object_reap(object
);
1728 /* cache is unlocked and object is no longer valid */
1729 object
= VM_OBJECT_NULL
;
1731 if (shadow_object
!= VM_OBJECT_NULL
) {
1733 * Drop the reference "object" was holding on
1734 * its shadow object.
1736 vm_object_deallocate(shadow_object
);
1737 shadow_object
= VM_OBJECT_NULL
;
1739 vm_object_reaper_lock_spin();
1742 /* wait for more work... */
1743 assert_wait((event_t
) &vm_object_reaper_queue
, THREAD_UNINT
);
1745 vm_object_reaper_unlock();
1747 thread_block((thread_continue_t
) vm_object_reaper_thread
);
1752 * Routine: vm_object_pager_wakeup
1753 * Purpose: Wake up anyone waiting for termination of a pager.
1757 vm_object_pager_wakeup(
1758 memory_object_t pager
)
1760 vm_object_hash_entry_t entry
;
1761 boolean_t waiting
= FALSE
;
1765 * If anyone was waiting for the memory_object_terminate
1766 * to be queued, wake them up now.
1768 lck
= vm_object_hash_lock_spin(pager
);
1769 entry
= vm_object_hash_lookup(pager
, TRUE
);
1770 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
1771 waiting
= entry
->waiting
;
1772 vm_object_hash_unlock(lck
);
1774 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
) {
1776 thread_wakeup((event_t
) pager
);
1777 vm_object_hash_entry_free(entry
);
1782 * Routine: vm_object_release_pager
1783 * Purpose: Terminate the pager and, upon completion,
1784 * release our last reference to it.
1785 * just like memory_object_terminate, except
1786 * that we wake up anyone blocked in vm_object_enter
1787 * waiting for termination message to be queued
1788 * before calling memory_object_init.
1791 vm_object_release_pager(
1792 memory_object_t pager
,
1797 * Terminate the pager.
1800 (void) memory_object_terminate(pager
);
1802 if (hashed
== TRUE
) {
1804 * Wakeup anyone waiting for this terminate
1805 * and remove the entry from the hash
1807 vm_object_pager_wakeup(pager
);
1810 * Release reference to pager.
1812 memory_object_deallocate(pager
);
1816 * Routine: vm_object_destroy
1818 * Shut down a VM object, despite the
1819 * presence of address map (or other) references
1825 __unused kern_return_t reason
)
1827 memory_object_t old_pager
;
1829 if (object
== VM_OBJECT_NULL
)
1830 return(KERN_SUCCESS
);
1833 * Remove the pager association immediately.
1835 * This will prevent the memory manager from further
1836 * meddling. [If it wanted to flush data or make
1837 * other changes, it should have done so before performing
1838 * the destroy call.]
1841 vm_object_lock(object
);
1842 object
->can_persist
= FALSE
;
1843 object
->named
= FALSE
;
1844 object
->alive
= FALSE
;
1846 if (object
->hashed
) {
1849 * Rip out the pager from the vm_object now...
1851 lck
= vm_object_hash_lock_spin(object
->pager
);
1852 vm_object_remove(object
);
1853 vm_object_hash_unlock(lck
);
1855 old_pager
= object
->pager
;
1856 object
->pager
= MEMORY_OBJECT_NULL
;
1857 if (old_pager
!= MEMORY_OBJECT_NULL
)
1858 memory_object_control_disable(object
->pager_control
);
1861 * Wait for the existing paging activity (that got
1862 * through before we nulled out the pager) to subside.
1865 vm_object_paging_wait(object
, THREAD_UNINT
);
1866 vm_object_unlock(object
);
1869 * Terminate the object now.
1871 if (old_pager
!= MEMORY_OBJECT_NULL
) {
1872 vm_object_release_pager(old_pager
, object
->hashed
);
1875 * JMM - Release the caller's reference. This assumes the
1876 * caller had a reference to release, which is a big (but
1877 * currently valid) assumption if this is driven from the
1878 * vnode pager (it is holding a named reference when making
1881 vm_object_deallocate(object
);
1884 return(KERN_SUCCESS
);
1888 #define VM_OBJ_DEACT_ALL_STATS DEBUG
1889 #if VM_OBJ_DEACT_ALL_STATS
1890 uint32_t vm_object_deactivate_all_pages_batches
= 0;
1891 uint32_t vm_object_deactivate_all_pages_pages
= 0;
1892 #endif /* VM_OBJ_DEACT_ALL_STATS */
1894 * vm_object_deactivate_all_pages
1896 * Deactivate all pages in the specified object. (Keep its pages
1897 * in memory even though it is no longer referenced.)
1899 * The object must be locked.
1902 vm_object_deactivate_all_pages(
1903 register vm_object_t object
)
1905 register vm_page_t p
;
1907 #if VM_OBJ_DEACT_ALL_STATS
1909 #endif /* VM_OBJ_DEACT_ALL_STATS */
1910 #define V_O_D_A_P_MAX_BATCH 256
1912 loop_count
= V_O_D_A_P_MAX_BATCH
;
1913 #if VM_OBJ_DEACT_ALL_STATS
1915 #endif /* VM_OBJ_DEACT_ALL_STATS */
1916 vm_page_lock_queues();
1917 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
1918 if (--loop_count
== 0) {
1919 #if VM_OBJ_DEACT_ALL_STATS
1920 hw_atomic_add(&vm_object_deactivate_all_pages_batches
,
1922 hw_atomic_add(&vm_object_deactivate_all_pages_pages
,
1925 #endif /* VM_OBJ_DEACT_ALL_STATS */
1926 lck_mtx_yield(&vm_page_queue_lock
);
1927 loop_count
= V_O_D_A_P_MAX_BATCH
;
1929 if (!p
->busy
&& !p
->throttled
) {
1930 #if VM_OBJ_DEACT_ALL_STATS
1932 #endif /* VM_OBJ_DEACT_ALL_STATS */
1933 vm_page_deactivate(p
);
1936 #if VM_OBJ_DEACT_ALL_STATS
1938 hw_atomic_add(&vm_object_deactivate_all_pages_batches
, 1);
1939 hw_atomic_add(&vm_object_deactivate_all_pages_pages
,
1943 #endif /* VM_OBJ_DEACT_ALL_STATS */
1944 vm_page_unlock_queues();
1950 * when deallocating pages it is necessary to hold
1951 * the vm_page_queue_lock (a hot global lock) for certain operations
1952 * on the page... however, the majority of the work can be done
1953 * while merely holding the object lock... to mitigate the time spent behind the
1954 * global lock, go to a 2 pass algorithm... collect pages up to DELAYED_WORK_LIMIT
1955 * while doing all of the work that doesn't require the vm_page_queue_lock...
1956 * them call dw_do_work to acquire the vm_page_queue_lock and do the
1957 * necessary work for each page... we will grab the busy bit on the page
1958 * so that dw_do_work can drop the object lock if it can't immediately take the
1959 * vm_page_queue_lock in order to compete for the locks in the same order that
1960 * vm_pageout_scan takes them.
1963 #define DELAYED_WORK_LIMIT 32
1965 #define DW_clear_reference 0x01
1966 #define DW_move_page 0x02
1967 #define DW_clear_busy 0x04
1968 #define DW_PAGE_WAKEUP 0x08
1976 static void dw_do_work(vm_object_t object
, struct dw
*dwp
, int dw_count
);
1989 * pageout_scan takes the vm_page_lock_queues first
1990 * then tries for the object lock... to avoid what
1991 * is effectively a lock inversion, we'll go to the
1992 * trouble of taking them in that same order... otherwise
1993 * if this object contains the majority of the pages resident
1994 * in the UBC (or a small set of large objects actively being
1995 * worked on contain the majority of the pages), we could
1996 * cause the pageout_scan thread to 'starve' in its attempt
1997 * to find pages to move to the free queue, since it has to
1998 * successfully acquire the object lock of any candidate page
1999 * before it can steal/clean it.
2001 if (!vm_page_trylockspin_queues()) {
2002 vm_object_unlock(object
);
2004 vm_page_lockspin_queues();
2006 for (j
= 0; ; j
++) {
2007 if (!vm_object_lock_avoid(object
) &&
2008 _vm_object_lock_try(object
))
2010 vm_page_unlock_queues();
2012 vm_page_lockspin_queues();
2015 for (j
= 0; j
< dw_count
; j
++, dwp
++) {
2019 if (dwp
->dw_mask
& DW_clear_reference
)
2020 m
->reference
= FALSE
;
2022 if (dwp
->dw_mask
& DW_move_page
) {
2023 VM_PAGE_QUEUES_REMOVE(m
);
2025 assert(!m
->laundry
);
2026 assert(m
->object
!= kernel_object
);
2027 assert(m
->pageq
.next
== NULL
&&
2028 m
->pageq
.prev
== NULL
);
2031 queue_enter_first(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
2032 vm_zf_queue_count
++;
2034 queue_enter_first(&vm_page_queue_inactive
, m
, vm_page_t
, pageq
);
2038 if (!m
->fictitious
) {
2039 vm_page_inactive_count
++;
2040 token_new_pagecount
++;
2042 assert(m
->phys_page
== vm_page_fictitious_addr
);
2045 if (dwp
->dw_mask
& DW_clear_busy
)
2046 dwp
->dw_m
->busy
= FALSE
;
2048 if (dwp
->dw_mask
& DW_PAGE_WAKEUP
)
2049 PAGE_WAKEUP(dwp
->dw_m
);
2051 vm_page_unlock_queues();
2058 * Decide if we need to send a memory status notification.
2061 (vm_page_active_count
+ vm_page_inactive_count
+
2062 vm_page_speculative_count
+ vm_page_free_count
+
2063 (IP_VALID(memory_manager_default
)?0:vm_page_purgeable_count
) ) * 100 /
2065 if (percent_avail
>= (kern_memorystatus_level
+ 5) ||
2066 percent_avail
<= (kern_memorystatus_level
- 5)) {
2067 kern_memorystatus_level
= percent_avail
;
2068 thread_wakeup((event_t
)&kern_memorystatus_wakeup
);
2077 * The "chunk" macros are used by routines below when looking for pages to deactivate. These
2078 * exist because of the need to handle shadow chains. When deactivating pages, we only
2079 * want to deactive the ones at the top most level in the object chain. In order to do
2080 * this efficiently, the specified address range is divided up into "chunks" and we use
2081 * a bit map to keep track of which pages have already been processed as we descend down
2082 * the shadow chain. These chunk macros hide the details of the bit map implementation
2083 * as much as we can.
2085 * For convenience, we use a 64-bit data type as the bit map, and therefore a chunk is
2086 * set to 64 pages. The bit map is indexed from the low-order end, so that the lowest
2087 * order bit represents page 0 in the current range and highest order bit represents
2090 * For further convenience, we also use negative logic for the page state in the bit map.
2091 * The bit is set to 1 to indicate it has not yet been seen, and to 0 to indicate it has
2092 * been processed. This way we can simply test the 64-bit long word to see if it's zero
2093 * to easily tell if the whole range has been processed. Therefore, the bit map starts
2094 * out with all the bits set. The macros below hide all these details from the caller.
2097 #define PAGES_IN_A_CHUNK 64 /* The number of pages in the chunk must */
2098 /* be the same as the number of bits in */
2099 /* the chunk_state_t type. We use 64 */
2100 /* just for convenience. */
2102 #define CHUNK_SIZE (PAGES_IN_A_CHUNK * PAGE_SIZE_64) /* Size of a chunk in bytes */
2104 typedef uint64_t chunk_state_t
;
2107 * The bit map uses negative logic, so we start out with all 64 bits set to indicate
2108 * that no pages have been processed yet. Also, if len is less than the full CHUNK_SIZE,
2109 * then we mark pages beyond the len as having been "processed" so that we don't waste time
2110 * looking at pages in that range. This can save us from unnecessarily chasing down the
2114 #define CHUNK_INIT(c, len) \
2118 (c) = 0xffffffffffffffffLL; \
2120 for (p = (len) / PAGE_SIZE_64; p < PAGES_IN_A_CHUNK; p++) \
2121 MARK_PAGE_HANDLED(c, p); \
2125 * Return true if all pages in the chunk have not yet been processed.
2128 #define CHUNK_NOT_COMPLETE(c) ((c) != 0)
2131 * Return true if the page at offset 'p' in the bit map has already been handled
2132 * while processing a higher level object in the shadow chain.
2135 #define PAGE_ALREADY_HANDLED(c, p) (((c) & (1LL << (p))) == 0)
2138 * Mark the page at offset 'p' in the bit map as having been processed.
2141 #define MARK_PAGE_HANDLED(c, p) \
2143 (c) = (c) & ~(1LL << (p)); \
2148 * Return true if the page at the given offset has been paged out. Object is
2149 * locked upon entry and returned locked.
2155 vm_object_offset_t offset
)
2158 memory_object_t pager
;
2161 * Check the existence map for the page if we have one, otherwise
2162 * ask the pager about this page.
2166 if (object
->existence_map
) {
2167 if (vm_external_state_get(object
->existence_map
, offset
)
2168 == VM_EXTERNAL_STATE_EXISTS
) {
2177 if (object
->internal
&&
2179 !object
->terminating
&&
2180 object
->pager_ready
) {
2183 * We're already holding a "paging in progress" reference
2184 * so the object can't disappear when we release the lock.
2187 assert(object
->paging_in_progress
);
2188 pager
= object
->pager
;
2189 vm_object_unlock(object
);
2191 kr
= memory_object_data_request(
2193 offset
+ object
->paging_offset
,
2194 0, /* just poke the pager */
2198 vm_object_lock(object
);
2200 if (kr
== KERN_SUCCESS
) {
2215 * Deactivate the pages in the specified object and range. If kill_page is set, also discard any
2216 * page modified state from the pmap. Update the chunk_state as we go along. The caller must specify
2217 * a size that is less than or equal to the CHUNK_SIZE.
2221 deactivate_pages_in_object(
2223 vm_object_offset_t offset
,
2224 vm_object_size_t size
,
2225 boolean_t kill_page
,
2226 boolean_t reusable_page
,
2230 boolean_t all_reusable
,
2231 chunk_state_t
*chunk_state
)
2235 struct dw dw_array
[DELAYED_WORK_LIMIT
];
2238 unsigned int reusable
= 0;
2242 * Examine each page in the chunk. The variable 'p' is the page number relative to the start of the
2243 * chunk. Since this routine is called once for each level in the shadow chain, the chunk_state may
2244 * have pages marked as having been processed already. We stop the loop early if we find we've handled
2245 * all the pages in the chunk.
2251 for(p
= 0; size
&& CHUNK_NOT_COMPLETE(*chunk_state
); p
++, size
-= PAGE_SIZE_64
, offset
+= PAGE_SIZE_64
) {
2254 * If this offset has already been found and handled in a higher level object, then don't
2255 * do anything with it in the current shadow object.
2258 if (PAGE_ALREADY_HANDLED(*chunk_state
, p
))
2262 * See if the page at this offset is around. First check to see if the page is resident,
2263 * then if not, check the existence map or with the pager.
2266 if ((m
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
2269 * We found a page we were looking for. Mark it as "handled" now in the chunk_state
2270 * so that we won't bother looking for a page at this offset again if there are more
2271 * shadow objects. Then deactivate the page.
2274 MARK_PAGE_HANDLED(*chunk_state
, p
);
2276 if (( !VM_PAGE_WIRED(m
)) && (!m
->private) && (!m
->gobbled
) && (!m
->busy
)) {
2279 assert(!m
->laundry
);
2281 clear_refmod
= VM_MEM_REFERENCED
;
2282 dwp
->dw_mask
= DW_clear_reference
;
2284 if ((kill_page
) && (object
->internal
)) {
2285 m
->precious
= FALSE
;
2288 clear_refmod
|= VM_MEM_MODIFIED
;
2291 * This page is now clean and
2292 * reclaimable. Move it out
2293 * of the throttled queue, so
2294 * that vm_pageout_scan() can
2297 dwp
->dw_mask
|= DW_move_page
;
2300 vm_external_state_clr(object
->existence_map
, offset
);
2301 #endif /* MACH_PAGEMAP */
2303 if (reusable_page
&& !m
->reusable
) {
2304 assert(!all_reusable
);
2305 assert(!object
->all_reusable
);
2307 object
->reusable_page_count
++;
2308 assert(object
->resident_page_count
>= object
->reusable_page_count
);
2313 m
->reusable
= FALSE
;
2314 object
->reusable_page_count
--;
2319 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
2321 if (!m
->throttled
&& !(reusable_page
|| all_reusable
))
2322 dwp
->dw_mask
|= DW_move_page
;
2324 * dw_do_work may need to drop the object lock
2325 * if it does, we need the pages its looking at to
2326 * be held stable via the busy bit.
2329 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
2335 if (dw_count
>= DELAYED_WORK_LIMIT
) {
2337 OSAddAtomic(reusable
,
2338 &vm_page_stats_reusable
.reusable_count
);
2339 vm_page_stats_reusable
.reusable
+= reusable
;
2342 dw_do_work(object
, &dw_array
[0], dw_count
);
2352 * The page at this offset isn't memory resident, check to see if it's
2353 * been paged out. If so, mark it as handled so we don't bother looking
2354 * for it in the shadow chain.
2357 if (page_is_paged_out(object
, offset
)) {
2358 MARK_PAGE_HANDLED(*chunk_state
, p
);
2361 * If we're killing a non-resident page, then clear the page in the existence
2362 * map so we don't bother paging it back in if it's touched again in the future.
2365 if ((kill_page
) && (object
->internal
)) {
2367 vm_external_state_clr(object
->existence_map
, offset
);
2368 #endif /* MACH_PAGEMAP */
2375 OSAddAtomic(reusable
, &vm_page_stats_reusable
.reusable_count
);
2376 vm_page_stats_reusable
.reusable
+= reusable
;
2381 dw_do_work(object
, &dw_array
[0], dw_count
);
2386 * Deactive a "chunk" of the given range of the object starting at offset. A "chunk"
2387 * will always be less than or equal to the given size. The total range is divided up
2388 * into chunks for efficiency and performance related to the locks and handling the shadow
2389 * chain. This routine returns how much of the given "size" it actually processed. It's
2390 * up to the caler to loop and keep calling this routine until the entire range they want
2391 * to process has been done.
2394 static vm_object_size_t
2396 vm_object_t orig_object
,
2397 vm_object_offset_t offset
,
2398 vm_object_size_t size
,
2399 boolean_t kill_page
,
2400 boolean_t reusable_page
,
2401 boolean_t all_reusable
)
2404 vm_object_t tmp_object
;
2405 vm_object_size_t length
;
2406 chunk_state_t chunk_state
;
2410 * Get set to do a chunk. We'll do up to CHUNK_SIZE, but no more than the
2411 * remaining size the caller asked for.
2414 length
= MIN(size
, CHUNK_SIZE
);
2417 * The chunk_state keeps track of which pages we've already processed if there's
2418 * a shadow chain on this object. At this point, we haven't done anything with this
2419 * range of pages yet, so initialize the state to indicate no pages processed yet.
2422 CHUNK_INIT(chunk_state
, length
);
2423 object
= orig_object
;
2426 * Start at the top level object and iterate around the loop once for each object
2427 * in the shadow chain. We stop processing early if we've already found all the pages
2428 * in the range. Otherwise we stop when we run out of shadow objects.
2431 while (object
&& CHUNK_NOT_COMPLETE(chunk_state
)) {
2432 vm_object_paging_begin(object
);
2434 deactivate_pages_in_object(object
, offset
, length
, kill_page
, reusable_page
, all_reusable
, &chunk_state
);
2436 vm_object_paging_end(object
);
2439 * We've finished with this object, see if there's a shadow object. If
2440 * there is, update the offset and lock the new object. We also turn off
2441 * kill_page at this point since we only kill pages in the top most object.
2444 tmp_object
= object
->shadow
;
2448 reusable_page
= FALSE
;
2449 all_reusable
= FALSE
;
2450 offset
+= object
->shadow_offset
;
2451 vm_object_lock(tmp_object
);
2454 if (object
!= orig_object
)
2455 vm_object_unlock(object
);
2457 object
= tmp_object
;
2460 if (object
&& object
!= orig_object
)
2461 vm_object_unlock(object
);
2469 * Move any resident pages in the specified range to the inactive queue. If kill_page is set,
2470 * we also clear the modified status of the page and "forget" any changes that have been made
2474 __private_extern__
void
2475 vm_object_deactivate_pages(
2477 vm_object_offset_t offset
,
2478 vm_object_size_t size
,
2479 boolean_t kill_page
,
2480 boolean_t reusable_page
)
2482 vm_object_size_t length
;
2483 boolean_t all_reusable
;
2486 * We break the range up into chunks and do one chunk at a time. This is for
2487 * efficiency and performance while handling the shadow chains and the locks.
2488 * The deactivate_a_chunk() function returns how much of the range it processed.
2489 * We keep calling this routine until the given size is exhausted.
2493 all_reusable
= FALSE
;
2494 if (reusable_page
&&
2495 object
->size
!= 0 &&
2496 object
->size
== size
&&
2497 object
->reusable_page_count
== 0) {
2498 all_reusable
= TRUE
;
2499 reusable_page
= FALSE
;
2503 if ((reusable_page
|| all_reusable
) && object
->all_reusable
) {
2504 /* This means MADV_FREE_REUSABLE has been called twice, which
2505 * is probably illegal. */
2511 length
= deactivate_a_chunk(object
, offset
, size
, kill_page
, reusable_page
, all_reusable
);
2518 if (!object
->all_reusable
) {
2519 unsigned int reusable
;
2521 object
->all_reusable
= TRUE
;
2522 assert(object
->reusable_page_count
== 0);
2523 /* update global stats */
2524 reusable
= object
->resident_page_count
;
2525 OSAddAtomic(reusable
,
2526 &vm_page_stats_reusable
.reusable_count
);
2527 vm_page_stats_reusable
.reusable
+= reusable
;
2528 vm_page_stats_reusable
.all_reusable_calls
++;
2530 } else if (reusable_page
) {
2531 vm_page_stats_reusable
.partial_reusable_calls
++;
2536 vm_object_reuse_pages(
2538 vm_object_offset_t start_offset
,
2539 vm_object_offset_t end_offset
,
2540 boolean_t allow_partial_reuse
)
2542 vm_object_offset_t cur_offset
;
2544 unsigned int reused
, reusable
;
2546 #define VM_OBJECT_REUSE_PAGE(object, m, reused) \
2548 if ((m) != VM_PAGE_NULL && \
2550 assert((object)->reusable_page_count <= \
2551 (object)->resident_page_count); \
2552 assert((object)->reusable_page_count > 0); \
2553 (object)->reusable_page_count--; \
2554 (m)->reusable = FALSE; \
2562 vm_object_lock_assert_exclusive(object
);
2564 if (object
->all_reusable
) {
2565 assert(object
->reusable_page_count
== 0);
2566 object
->all_reusable
= FALSE
;
2567 if (end_offset
- start_offset
== object
->size
||
2568 !allow_partial_reuse
) {
2569 vm_page_stats_reusable
.all_reuse_calls
++;
2570 reused
= object
->resident_page_count
;
2572 vm_page_stats_reusable
.partial_reuse_calls
++;
2573 queue_iterate(&object
->memq
, m
, vm_page_t
, listq
) {
2574 if (m
->offset
< start_offset
||
2575 m
->offset
>= end_offset
) {
2577 object
->reusable_page_count
++;
2578 assert(object
->resident_page_count
>= object
->reusable_page_count
);
2581 assert(!m
->reusable
);
2586 } else if (object
->resident_page_count
>
2587 ((end_offset
- start_offset
) >> PAGE_SHIFT
)) {
2588 vm_page_stats_reusable
.partial_reuse_calls
++;
2589 for (cur_offset
= start_offset
;
2590 cur_offset
< end_offset
;
2591 cur_offset
+= PAGE_SIZE_64
) {
2592 if (object
->reusable_page_count
== 0) {
2595 m
= vm_page_lookup(object
, cur_offset
);
2596 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
2599 vm_page_stats_reusable
.partial_reuse_calls
++;
2600 queue_iterate(&object
->memq
, m
, vm_page_t
, listq
) {
2601 if (object
->reusable_page_count
== 0) {
2604 if (m
->offset
< start_offset
||
2605 m
->offset
>= end_offset
) {
2608 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
2612 /* update global stats */
2613 OSAddAtomic(reusable
-reused
, &vm_page_stats_reusable
.reusable_count
);
2614 vm_page_stats_reusable
.reused
+= reused
;
2615 vm_page_stats_reusable
.reusable
+= reusable
;
2619 * Routine: vm_object_pmap_protect
2622 * Reduces the permission for all physical
2623 * pages in the specified object range.
2625 * If removing write permission only, it is
2626 * sufficient to protect only the pages in
2627 * the top-level object; only those pages may
2628 * have write permission.
2630 * If removing all access, we must follow the
2631 * shadow chain from the top-level object to
2632 * remove access to all pages in shadowed objects.
2634 * The object must *not* be locked. The object must
2635 * be temporary/internal.
2637 * If pmap is not NULL, this routine assumes that
2638 * the only mappings for the pages are in that
2642 __private_extern__
void
2643 vm_object_pmap_protect(
2644 register vm_object_t object
,
2645 register vm_object_offset_t offset
,
2646 vm_object_size_t size
,
2648 vm_map_offset_t pmap_start
,
2651 if (object
== VM_OBJECT_NULL
)
2653 size
= vm_object_round_page(size
);
2654 offset
= vm_object_trunc_page(offset
);
2656 vm_object_lock(object
);
2658 if (object
->phys_contiguous
) {
2660 vm_object_unlock(object
);
2661 pmap_protect(pmap
, pmap_start
, pmap_start
+ size
, prot
);
2663 vm_object_offset_t phys_start
, phys_end
, phys_addr
;
2665 phys_start
= object
->shadow_offset
+ offset
;
2666 phys_end
= phys_start
+ size
;
2667 assert(phys_start
<= phys_end
);
2668 assert(phys_end
<= object
->shadow_offset
+ object
->size
);
2669 vm_object_unlock(object
);
2671 for (phys_addr
= phys_start
;
2672 phys_addr
< phys_end
;
2673 phys_addr
+= PAGE_SIZE_64
) {
2674 pmap_page_protect((ppnum_t
) (phys_addr
>> PAGE_SHIFT
), prot
);
2680 assert(object
->internal
);
2683 if (ptoa_64(object
->resident_page_count
) > size
/2 && pmap
!= PMAP_NULL
) {
2684 vm_object_unlock(object
);
2685 pmap_protect(pmap
, pmap_start
, pmap_start
+ size
, prot
);
2689 /* if we are doing large ranges with respect to resident */
2690 /* page count then we should interate over pages otherwise */
2691 /* inverse page look-up will be faster */
2692 if (ptoa_64(object
->resident_page_count
/ 4) < size
) {
2694 vm_object_offset_t end
;
2696 end
= offset
+ size
;
2698 if (pmap
!= PMAP_NULL
) {
2699 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2700 if (!p
->fictitious
&&
2701 (offset
<= p
->offset
) && (p
->offset
< end
)) {
2702 vm_map_offset_t start
;
2704 start
= pmap_start
+ p
->offset
- offset
;
2705 pmap_protect(pmap
, start
, start
+ PAGE_SIZE_64
, prot
);
2709 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2710 if (!p
->fictitious
&&
2711 (offset
<= p
->offset
) && (p
->offset
< end
)) {
2713 pmap_page_protect(p
->phys_page
, prot
);
2719 vm_object_offset_t end
;
2720 vm_object_offset_t target_off
;
2722 end
= offset
+ size
;
2724 if (pmap
!= PMAP_NULL
) {
2725 for(target_off
= offset
;
2727 target_off
+= PAGE_SIZE
) {
2728 p
= vm_page_lookup(object
, target_off
);
2729 if (p
!= VM_PAGE_NULL
) {
2730 vm_object_offset_t start
;
2731 start
= pmap_start
+
2732 (p
->offset
- offset
);
2733 pmap_protect(pmap
, start
,
2734 start
+ PAGE_SIZE
, prot
);
2738 for(target_off
= offset
;
2739 target_off
< end
; target_off
+= PAGE_SIZE
) {
2740 p
= vm_page_lookup(object
, target_off
);
2741 if (p
!= VM_PAGE_NULL
) {
2742 pmap_page_protect(p
->phys_page
, prot
);
2748 if (prot
== VM_PROT_NONE
) {
2750 * Must follow shadow chain to remove access
2751 * to pages in shadowed objects.
2753 register vm_object_t next_object
;
2755 next_object
= object
->shadow
;
2756 if (next_object
!= VM_OBJECT_NULL
) {
2757 offset
+= object
->shadow_offset
;
2758 vm_object_lock(next_object
);
2759 vm_object_unlock(object
);
2760 object
= next_object
;
2764 * End of chain - we are done.
2771 * Pages in shadowed objects may never have
2772 * write permission - we may stop here.
2778 vm_object_unlock(object
);
2782 * Routine: vm_object_copy_slowly
2785 * Copy the specified range of the source
2786 * virtual memory object without using
2787 * protection-based optimizations (such
2788 * as copy-on-write). The pages in the
2789 * region are actually copied.
2791 * In/out conditions:
2792 * The caller must hold a reference and a lock
2793 * for the source virtual memory object. The source
2794 * object will be returned *unlocked*.
2797 * If the copy is completed successfully, KERN_SUCCESS is
2798 * returned. If the caller asserted the interruptible
2799 * argument, and an interruption occurred while waiting
2800 * for a user-generated event, MACH_SEND_INTERRUPTED is
2801 * returned. Other values may be returned to indicate
2802 * hard errors during the copy operation.
2804 * A new virtual memory object is returned in a
2805 * parameter (_result_object). The contents of this
2806 * new object, starting at a zero offset, are a copy
2807 * of the source memory region. In the event of
2808 * an error, this parameter will contain the value
2811 __private_extern__ kern_return_t
2812 vm_object_copy_slowly(
2813 register vm_object_t src_object
,
2814 vm_object_offset_t src_offset
,
2815 vm_object_size_t size
,
2816 boolean_t interruptible
,
2817 vm_object_t
*_result_object
) /* OUT */
2819 vm_object_t new_object
;
2820 vm_object_offset_t new_offset
;
2822 struct vm_object_fault_info fault_info
;
2824 XPR(XPR_VM_OBJECT
, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
2825 src_object
, src_offset
, size
, 0, 0);
2828 vm_object_unlock(src_object
);
2829 *_result_object
= VM_OBJECT_NULL
;
2830 return(KERN_INVALID_ARGUMENT
);
2834 * Prevent destruction of the source object while we copy.
2837 vm_object_reference_locked(src_object
);
2838 vm_object_unlock(src_object
);
2841 * Create a new object to hold the copied pages.
2843 * We fill the new object starting at offset 0,
2844 * regardless of the input offset.
2845 * We don't bother to lock the new object within
2846 * this routine, since we have the only reference.
2849 new_object
= vm_object_allocate(size
);
2852 assert(size
== trunc_page_64(size
)); /* Will the loop terminate? */
2854 fault_info
.interruptible
= interruptible
;
2855 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
2856 fault_info
.user_tag
= 0;
2857 fault_info
.lo_offset
= src_offset
;
2858 fault_info
.hi_offset
= src_offset
+ size
;
2859 fault_info
.no_cache
= FALSE
;
2860 fault_info
.stealth
= TRUE
;
2861 fault_info
.mark_zf_absent
= FALSE
;
2865 src_offset
+= PAGE_SIZE_64
,
2866 new_offset
+= PAGE_SIZE_64
, size
-= PAGE_SIZE_64
2869 vm_fault_return_t result
;
2871 vm_object_lock(new_object
);
2873 while ((new_page
= vm_page_alloc(new_object
, new_offset
))
2876 vm_object_unlock(new_object
);
2878 if (!vm_page_wait(interruptible
)) {
2879 vm_object_deallocate(new_object
);
2880 vm_object_deallocate(src_object
);
2881 *_result_object
= VM_OBJECT_NULL
;
2882 return(MACH_SEND_INTERRUPTED
);
2884 vm_object_lock(new_object
);
2886 vm_object_unlock(new_object
);
2889 vm_prot_t prot
= VM_PROT_READ
;
2890 vm_page_t _result_page
;
2893 vm_page_t result_page
;
2894 kern_return_t error_code
;
2896 vm_object_lock(src_object
);
2897 vm_object_paging_begin(src_object
);
2899 if (size
> (vm_size_t
) -1) {
2900 /* 32-bit overflow */
2901 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
2903 fault_info
.cluster_size
= (vm_size_t
) size
;
2904 assert(fault_info
.cluster_size
== size
);
2907 XPR(XPR_VM_FAULT
,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
2908 result
= vm_fault_page(src_object
, src_offset
,
2909 VM_PROT_READ
, FALSE
,
2910 &prot
, &_result_page
, &top_page
,
2912 &error_code
, FALSE
, FALSE
, &fault_info
);
2915 case VM_FAULT_SUCCESS
:
2916 result_page
= _result_page
;
2919 * We don't need to hold the object
2920 * lock -- the busy page will be enough.
2921 * [We don't care about picking up any
2922 * new modifications.]
2924 * Copy the page to the new object.
2927 * If result_page is clean,
2928 * we could steal it instead
2932 vm_object_unlock(result_page
->object
);
2933 vm_page_copy(result_page
, new_page
);
2936 * Let go of both pages (make them
2937 * not busy, perform wakeup, activate).
2939 vm_object_lock(new_object
);
2940 new_page
->dirty
= TRUE
;
2941 PAGE_WAKEUP_DONE(new_page
);
2942 vm_object_unlock(new_object
);
2944 vm_object_lock(result_page
->object
);
2945 PAGE_WAKEUP_DONE(result_page
);
2947 vm_page_lockspin_queues();
2948 if (!result_page
->active
&&
2949 !result_page
->inactive
&&
2950 !result_page
->throttled
)
2951 vm_page_activate(result_page
);
2952 vm_page_activate(new_page
);
2953 vm_page_unlock_queues();
2956 * Release paging references and
2957 * top-level placeholder page, if any.
2960 vm_fault_cleanup(result_page
->object
,
2965 case VM_FAULT_RETRY
:
2968 case VM_FAULT_FICTITIOUS_SHORTAGE
:
2969 vm_page_more_fictitious();
2972 case VM_FAULT_MEMORY_SHORTAGE
:
2973 if (vm_page_wait(interruptible
))
2977 case VM_FAULT_INTERRUPTED
:
2978 vm_object_lock(new_object
);
2979 VM_PAGE_FREE(new_page
);
2980 vm_object_unlock(new_object
);
2982 vm_object_deallocate(new_object
);
2983 vm_object_deallocate(src_object
);
2984 *_result_object
= VM_OBJECT_NULL
;
2985 return(MACH_SEND_INTERRUPTED
);
2987 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
2988 /* success but no VM page: fail */
2989 vm_object_paging_end(src_object
);
2990 vm_object_unlock(src_object
);
2992 case VM_FAULT_MEMORY_ERROR
:
2995 * (a) ignore pages that we can't
2997 * (b) return the null object if
2998 * any page fails [chosen]
3001 vm_object_lock(new_object
);
3002 VM_PAGE_FREE(new_page
);
3003 vm_object_unlock(new_object
);
3005 vm_object_deallocate(new_object
);
3006 vm_object_deallocate(src_object
);
3007 *_result_object
= VM_OBJECT_NULL
;
3008 return(error_code
? error_code
:
3012 panic("vm_object_copy_slowly: unexpected error"
3013 " 0x%x from vm_fault_page()\n", result
);
3015 } while (result
!= VM_FAULT_SUCCESS
);
3019 * Lose the extra reference, and return our object.
3021 vm_object_deallocate(src_object
);
3022 *_result_object
= new_object
;
3023 return(KERN_SUCCESS
);
3027 * Routine: vm_object_copy_quickly
3030 * Copy the specified range of the source virtual
3031 * memory object, if it can be done without waiting
3032 * for user-generated events.
3035 * If the copy is successful, the copy is returned in
3036 * the arguments; otherwise, the arguments are not
3039 * In/out conditions:
3040 * The object should be unlocked on entry and exit.
3044 __private_extern__ boolean_t
3045 vm_object_copy_quickly(
3046 vm_object_t
*_object
, /* INOUT */
3047 __unused vm_object_offset_t offset
, /* IN */
3048 __unused vm_object_size_t size
, /* IN */
3049 boolean_t
*_src_needs_copy
, /* OUT */
3050 boolean_t
*_dst_needs_copy
) /* OUT */
3052 vm_object_t object
= *_object
;
3053 memory_object_copy_strategy_t copy_strategy
;
3055 XPR(XPR_VM_OBJECT
, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
3056 *_object
, offset
, size
, 0, 0);
3057 if (object
== VM_OBJECT_NULL
) {
3058 *_src_needs_copy
= FALSE
;
3059 *_dst_needs_copy
= FALSE
;
3063 vm_object_lock(object
);
3065 copy_strategy
= object
->copy_strategy
;
3067 switch (copy_strategy
) {
3068 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3071 * Symmetric copy strategy.
3072 * Make another reference to the object.
3073 * Leave object/offset unchanged.
3076 vm_object_reference_locked(object
);
3077 object
->shadowed
= TRUE
;
3078 vm_object_unlock(object
);
3081 * Both source and destination must make
3082 * shadows, and the source must be made
3083 * read-only if not already.
3086 *_src_needs_copy
= TRUE
;
3087 *_dst_needs_copy
= TRUE
;
3091 case MEMORY_OBJECT_COPY_DELAY
:
3092 vm_object_unlock(object
);
3096 vm_object_unlock(object
);
3102 static int copy_call_count
= 0;
3103 static int copy_call_sleep_count
= 0;
3104 static int copy_call_restart_count
= 0;
3107 * Routine: vm_object_copy_call [internal]
3110 * Copy the source object (src_object), using the
3111 * user-managed copy algorithm.
3113 * In/out conditions:
3114 * The source object must be locked on entry. It
3115 * will be *unlocked* on exit.
3118 * If the copy is successful, KERN_SUCCESS is returned.
3119 * A new object that represents the copied virtual
3120 * memory is returned in a parameter (*_result_object).
3121 * If the return value indicates an error, this parameter
3124 static kern_return_t
3125 vm_object_copy_call(
3126 vm_object_t src_object
,
3127 vm_object_offset_t src_offset
,
3128 vm_object_size_t size
,
3129 vm_object_t
*_result_object
) /* OUT */
3133 boolean_t check_ready
= FALSE
;
3134 uint32_t try_failed_count
= 0;
3137 * If a copy is already in progress, wait and retry.
3140 * Consider making this call interruptable, as Mike
3141 * intended it to be.
3144 * Need a counter or version or something to allow
3145 * us to use the copy that the currently requesting
3146 * thread is obtaining -- is it worth adding to the
3147 * vm object structure? Depends how common this case it.
3150 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3151 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3153 copy_call_restart_count
++;
3157 * Indicate (for the benefit of memory_object_create_copy)
3158 * that we want a copy for src_object. (Note that we cannot
3159 * do a real assert_wait before calling memory_object_copy,
3160 * so we simply set the flag.)
3163 vm_object_set_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
);
3164 vm_object_unlock(src_object
);
3167 * Ask the memory manager to give us a memory object
3168 * which represents a copy of the src object.
3169 * The memory manager may give us a memory object
3170 * which we already have, or it may give us a
3171 * new memory object. This memory object will arrive
3172 * via memory_object_create_copy.
3175 kr
= KERN_FAILURE
; /* XXX need to change memory_object.defs */
3176 if (kr
!= KERN_SUCCESS
) {
3181 * Wait for the copy to arrive.
3183 vm_object_lock(src_object
);
3184 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3185 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3187 copy_call_sleep_count
++;
3190 assert(src_object
->copy
!= VM_OBJECT_NULL
);
3191 copy
= src_object
->copy
;
3192 if (!vm_object_lock_try(copy
)) {
3193 vm_object_unlock(src_object
);
3196 mutex_pause(try_failed_count
); /* wait a bit */
3198 vm_object_lock(src_object
);
3201 if (copy
->size
< src_offset
+size
)
3202 copy
->size
= src_offset
+size
;
3204 if (!copy
->pager_ready
)
3210 *_result_object
= copy
;
3211 vm_object_unlock(copy
);
3212 vm_object_unlock(src_object
);
3214 /* Wait for the copy to be ready. */
3215 if (check_ready
== TRUE
) {
3216 vm_object_lock(copy
);
3217 while (!copy
->pager_ready
) {
3218 vm_object_sleep(copy
, VM_OBJECT_EVENT_PAGER_READY
, THREAD_UNINT
);
3220 vm_object_unlock(copy
);
3223 return KERN_SUCCESS
;
3226 static int copy_delayed_lock_collisions
= 0;
3227 static int copy_delayed_max_collisions
= 0;
3228 static int copy_delayed_lock_contention
= 0;
3229 static int copy_delayed_protect_iterate
= 0;
3232 * Routine: vm_object_copy_delayed [internal]
3235 * Copy the specified virtual memory object, using
3236 * the asymmetric copy-on-write algorithm.
3238 * In/out conditions:
3239 * The src_object must be locked on entry. It will be unlocked
3240 * on exit - so the caller must also hold a reference to it.
3242 * This routine will not block waiting for user-generated
3243 * events. It is not interruptible.
3245 __private_extern__ vm_object_t
3246 vm_object_copy_delayed(
3247 vm_object_t src_object
,
3248 vm_object_offset_t src_offset
,
3249 vm_object_size_t size
,
3250 boolean_t src_object_shared
)
3252 vm_object_t new_copy
= VM_OBJECT_NULL
;
3253 vm_object_t old_copy
;
3255 vm_object_size_t copy_size
= src_offset
+ size
;
3260 * The user-level memory manager wants to see all of the changes
3261 * to this object, but it has promised not to make any changes on
3264 * Perform an asymmetric copy-on-write, as follows:
3265 * Create a new object, called a "copy object" to hold
3266 * pages modified by the new mapping (i.e., the copy,
3267 * not the original mapping).
3268 * Record the original object as the backing object for
3269 * the copy object. If the original mapping does not
3270 * change a page, it may be used read-only by the copy.
3271 * Record the copy object in the original object.
3272 * When the original mapping causes a page to be modified,
3273 * it must be copied to a new page that is "pushed" to
3275 * Mark the new mapping (the copy object) copy-on-write.
3276 * This makes the copy object itself read-only, allowing
3277 * it to be reused if the original mapping makes no
3278 * changes, and simplifying the synchronization required
3279 * in the "push" operation described above.
3281 * The copy-on-write is said to be assymetric because the original
3282 * object is *not* marked copy-on-write. A copied page is pushed
3283 * to the copy object, regardless which party attempted to modify
3286 * Repeated asymmetric copy operations may be done. If the
3287 * original object has not been changed since the last copy, its
3288 * copy object can be reused. Otherwise, a new copy object can be
3289 * inserted between the original object and its previous copy
3290 * object. Since any copy object is read-only, this cannot affect
3291 * affect the contents of the previous copy object.
3293 * Note that a copy object is higher in the object tree than the
3294 * original object; therefore, use of the copy object recorded in
3295 * the original object must be done carefully, to avoid deadlock.
3301 * Wait for paging in progress.
3303 if (!src_object
->true_share
&&
3304 (src_object
->paging_in_progress
!= 0 ||
3305 src_object
->activity_in_progress
!= 0)) {
3306 if (src_object_shared
== TRUE
) {
3307 vm_object_unlock(src_object
);
3308 vm_object_lock(src_object
);
3309 src_object_shared
= FALSE
;
3312 vm_object_paging_wait(src_object
, THREAD_UNINT
);
3315 * See whether we can reuse the result of a previous
3319 old_copy
= src_object
->copy
;
3320 if (old_copy
!= VM_OBJECT_NULL
) {
3324 * Try to get the locks (out of order)
3326 if (src_object_shared
== TRUE
)
3327 lock_granted
= vm_object_lock_try_shared(old_copy
);
3329 lock_granted
= vm_object_lock_try(old_copy
);
3331 if (!lock_granted
) {
3332 vm_object_unlock(src_object
);
3334 if (collisions
++ == 0)
3335 copy_delayed_lock_contention
++;
3336 mutex_pause(collisions
);
3338 /* Heisenberg Rules */
3339 copy_delayed_lock_collisions
++;
3341 if (collisions
> copy_delayed_max_collisions
)
3342 copy_delayed_max_collisions
= collisions
;
3344 if (src_object_shared
== TRUE
)
3345 vm_object_lock_shared(src_object
);
3347 vm_object_lock(src_object
);
3353 * Determine whether the old copy object has
3357 if (old_copy
->resident_page_count
== 0 &&
3358 !old_copy
->pager_created
) {
3360 * It has not been modified.
3362 * Return another reference to
3363 * the existing copy-object if
3364 * we can safely grow it (if
3368 if (old_copy
->size
< copy_size
) {
3369 if (src_object_shared
== TRUE
) {
3370 vm_object_unlock(old_copy
);
3371 vm_object_unlock(src_object
);
3373 vm_object_lock(src_object
);
3374 src_object_shared
= FALSE
;
3378 * We can't perform a delayed copy if any of the
3379 * pages in the extended range are wired (because
3380 * we can't safely take write permission away from
3381 * wired pages). If the pages aren't wired, then
3382 * go ahead and protect them.
3384 copy_delayed_protect_iterate
++;
3386 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
3387 if (!p
->fictitious
&&
3388 p
->offset
>= old_copy
->size
&&
3389 p
->offset
< copy_size
) {
3390 if (VM_PAGE_WIRED(p
)) {
3391 vm_object_unlock(old_copy
);
3392 vm_object_unlock(src_object
);
3394 if (new_copy
!= VM_OBJECT_NULL
) {
3395 vm_object_unlock(new_copy
);
3396 vm_object_deallocate(new_copy
);
3399 return VM_OBJECT_NULL
;
3401 pmap_page_protect(p
->phys_page
,
3402 (VM_PROT_ALL
& ~VM_PROT_WRITE
));
3406 old_copy
->size
= copy_size
;
3408 if (src_object_shared
== TRUE
)
3409 vm_object_reference_shared(old_copy
);
3411 vm_object_reference_locked(old_copy
);
3412 vm_object_unlock(old_copy
);
3413 vm_object_unlock(src_object
);
3415 if (new_copy
!= VM_OBJECT_NULL
) {
3416 vm_object_unlock(new_copy
);
3417 vm_object_deallocate(new_copy
);
3425 * Adjust the size argument so that the newly-created
3426 * copy object will be large enough to back either the
3427 * old copy object or the new mapping.
3429 if (old_copy
->size
> copy_size
)
3430 copy_size
= old_copy
->size
;
3432 if (new_copy
== VM_OBJECT_NULL
) {
3433 vm_object_unlock(old_copy
);
3434 vm_object_unlock(src_object
);
3435 new_copy
= vm_object_allocate(copy_size
);
3436 vm_object_lock(src_object
);
3437 vm_object_lock(new_copy
);
3439 src_object_shared
= FALSE
;
3442 new_copy
->size
= copy_size
;
3445 * The copy-object is always made large enough to
3446 * completely shadow the original object, since
3447 * it may have several users who want to shadow
3448 * the original object at different points.
3451 assert((old_copy
->shadow
== src_object
) &&
3452 (old_copy
->shadow_offset
== (vm_object_offset_t
) 0));
3454 } else if (new_copy
== VM_OBJECT_NULL
) {
3455 vm_object_unlock(src_object
);
3456 new_copy
= vm_object_allocate(copy_size
);
3457 vm_object_lock(src_object
);
3458 vm_object_lock(new_copy
);
3460 src_object_shared
= FALSE
;
3465 * We now have the src object locked, and the new copy object
3466 * allocated and locked (and potentially the old copy locked).
3467 * Before we go any further, make sure we can still perform
3468 * a delayed copy, as the situation may have changed.
3470 * Specifically, we can't perform a delayed copy if any of the
3471 * pages in the range are wired (because we can't safely take
3472 * write permission away from wired pages). If the pages aren't
3473 * wired, then go ahead and protect them.
3475 copy_delayed_protect_iterate
++;
3477 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
3478 if (!p
->fictitious
&& p
->offset
< copy_size
) {
3479 if (VM_PAGE_WIRED(p
)) {
3481 vm_object_unlock(old_copy
);
3482 vm_object_unlock(src_object
);
3483 vm_object_unlock(new_copy
);
3484 vm_object_deallocate(new_copy
);
3485 return VM_OBJECT_NULL
;
3487 pmap_page_protect(p
->phys_page
,
3488 (VM_PROT_ALL
& ~VM_PROT_WRITE
));
3492 if (old_copy
!= VM_OBJECT_NULL
) {
3494 * Make the old copy-object shadow the new one.
3495 * It will receive no more pages from the original
3499 /* remove ref. from old_copy */
3500 vm_object_lock_assert_exclusive(src_object
);
3501 src_object
->ref_count
--;
3502 assert(src_object
->ref_count
> 0);
3503 vm_object_lock_assert_exclusive(old_copy
);
3504 old_copy
->shadow
= new_copy
;
3505 vm_object_lock_assert_exclusive(new_copy
);
3506 assert(new_copy
->ref_count
> 0);
3507 new_copy
->ref_count
++; /* for old_copy->shadow ref. */
3510 if (old_copy
->res_count
) {
3511 VM_OBJ_RES_INCR(new_copy
);
3512 VM_OBJ_RES_DECR(src_object
);
3516 vm_object_unlock(old_copy
); /* done with old_copy */
3520 * Point the new copy at the existing object.
3522 vm_object_lock_assert_exclusive(new_copy
);
3523 new_copy
->shadow
= src_object
;
3524 new_copy
->shadow_offset
= 0;
3525 new_copy
->shadowed
= TRUE
; /* caller must set needs_copy */
3527 vm_object_lock_assert_exclusive(src_object
);
3528 vm_object_reference_locked(src_object
);
3529 src_object
->copy
= new_copy
;
3530 vm_object_unlock(src_object
);
3531 vm_object_unlock(new_copy
);
3534 "vm_object_copy_delayed: used copy object %X for source %X\n",
3535 new_copy
, src_object
, 0, 0, 0);
3541 * Routine: vm_object_copy_strategically
3544 * Perform a copy according to the source object's
3545 * declared strategy. This operation may block,
3546 * and may be interrupted.
3548 __private_extern__ kern_return_t
3549 vm_object_copy_strategically(
3550 register vm_object_t src_object
,
3551 vm_object_offset_t src_offset
,
3552 vm_object_size_t size
,
3553 vm_object_t
*dst_object
, /* OUT */
3554 vm_object_offset_t
*dst_offset
, /* OUT */
3555 boolean_t
*dst_needs_copy
) /* OUT */
3558 boolean_t interruptible
= THREAD_ABORTSAFE
; /* XXX */
3559 boolean_t object_lock_shared
= FALSE
;
3560 memory_object_copy_strategy_t copy_strategy
;
3562 assert(src_object
!= VM_OBJECT_NULL
);
3564 copy_strategy
= src_object
->copy_strategy
;
3566 if (copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
3567 vm_object_lock_shared(src_object
);
3568 object_lock_shared
= TRUE
;
3570 vm_object_lock(src_object
);
3573 * The copy strategy is only valid if the memory manager
3574 * is "ready". Internal objects are always ready.
3577 while (!src_object
->internal
&& !src_object
->pager_ready
) {
3578 wait_result_t wait_result
;
3580 if (object_lock_shared
== TRUE
) {
3581 vm_object_unlock(src_object
);
3582 vm_object_lock(src_object
);
3583 object_lock_shared
= FALSE
;
3586 wait_result
= vm_object_sleep( src_object
,
3587 VM_OBJECT_EVENT_PAGER_READY
,
3589 if (wait_result
!= THREAD_AWAKENED
) {
3590 vm_object_unlock(src_object
);
3591 *dst_object
= VM_OBJECT_NULL
;
3593 *dst_needs_copy
= FALSE
;
3594 return(MACH_SEND_INTERRUPTED
);
3599 * Use the appropriate copy strategy.
3602 switch (copy_strategy
) {
3603 case MEMORY_OBJECT_COPY_DELAY
:
3604 *dst_object
= vm_object_copy_delayed(src_object
,
3605 src_offset
, size
, object_lock_shared
);
3606 if (*dst_object
!= VM_OBJECT_NULL
) {
3607 *dst_offset
= src_offset
;
3608 *dst_needs_copy
= TRUE
;
3609 result
= KERN_SUCCESS
;
3612 vm_object_lock(src_object
);
3613 /* fall thru when delayed copy not allowed */
3615 case MEMORY_OBJECT_COPY_NONE
:
3616 result
= vm_object_copy_slowly(src_object
, src_offset
, size
,
3617 interruptible
, dst_object
);
3618 if (result
== KERN_SUCCESS
) {
3620 *dst_needs_copy
= FALSE
;
3624 case MEMORY_OBJECT_COPY_CALL
:
3625 result
= vm_object_copy_call(src_object
, src_offset
, size
,
3627 if (result
== KERN_SUCCESS
) {
3628 *dst_offset
= src_offset
;
3629 *dst_needs_copy
= TRUE
;
3633 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3634 XPR(XPR_VM_OBJECT
, "v_o_c_strategically obj 0x%x off 0x%x size 0x%x\n", src_object
, src_offset
, size
, 0, 0);
3635 vm_object_unlock(src_object
);
3636 result
= KERN_MEMORY_RESTART_COPY
;
3640 panic("copy_strategically: bad strategy");
3641 result
= KERN_INVALID_ARGUMENT
;
3649 * Create a new object which is backed by the
3650 * specified existing object range. The source
3651 * object reference is deallocated.
3653 * The new object and offset into that object
3654 * are returned in the source parameters.
3656 boolean_t vm_object_shadow_check
= FALSE
;
3658 __private_extern__ boolean_t
3660 vm_object_t
*object
, /* IN/OUT */
3661 vm_object_offset_t
*offset
, /* IN/OUT */
3662 vm_object_size_t length
)
3664 register vm_object_t source
;
3665 register vm_object_t result
;
3671 * This assertion is valid but it gets triggered by Rosetta for example
3672 * due to a combination of vm_remap() that changes a VM object's
3673 * copy_strategy from SYMMETRIC to DELAY and vm_protect(VM_PROT_COPY)
3674 * that then sets "needs_copy" on its map entry. This creates a
3675 * mapping situation that VM should never see and doesn't know how to
3677 * It's not clear if this can create any real problem but we should
3678 * look into fixing this, probably by having vm_protect(VM_PROT_COPY)
3679 * do more than just set "needs_copy" to handle the copy-on-write...
3680 * In the meantime, let's disable the assertion.
3682 assert(source
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
);
3686 * Determine if we really need a shadow.
3689 if (vm_object_shadow_check
&& source
->ref_count
== 1 &&
3690 (source
->shadow
== VM_OBJECT_NULL
||
3691 source
->shadow
->copy
== VM_OBJECT_NULL
))
3693 source
->shadowed
= FALSE
;
3698 * Allocate a new object with the given length
3701 if ((result
= vm_object_allocate(length
)) == VM_OBJECT_NULL
)
3702 panic("vm_object_shadow: no object for shadowing");
3705 * The new object shadows the source object, adding
3706 * a reference to it. Our caller changes his reference
3707 * to point to the new object, removing a reference to
3708 * the source object. Net result: no change of reference
3711 result
->shadow
= source
;
3714 * Store the offset into the source object,
3715 * and fix up the offset into the new object.
3718 result
->shadow_offset
= *offset
;
3721 * Return the new things
3730 * The relationship between vm_object structures and
3731 * the memory_object requires careful synchronization.
3733 * All associations are created by memory_object_create_named
3734 * for external pagers and vm_object_pager_create for internal
3735 * objects as follows:
3737 * pager: the memory_object itself, supplied by
3738 * the user requesting a mapping (or the kernel,
3739 * when initializing internal objects); the
3740 * kernel simulates holding send rights by keeping
3744 * the memory object control port,
3745 * created by the kernel; the kernel holds
3746 * receive (and ownership) rights to this
3747 * port, but no other references.
3749 * When initialization is complete, the "initialized" field
3750 * is asserted. Other mappings using a particular memory object,
3751 * and any references to the vm_object gained through the
3752 * port association must wait for this initialization to occur.
3754 * In order to allow the memory manager to set attributes before
3755 * requests (notably virtual copy operations, but also data or
3756 * unlock requests) are made, a "ready" attribute is made available.
3757 * Only the memory manager may affect the value of this attribute.
3758 * Its value does not affect critical kernel functions, such as
3759 * internal object initialization or destruction. [Furthermore,
3760 * memory objects created by the kernel are assumed to be ready
3761 * immediately; the default memory manager need not explicitly
3762 * set the "ready" attribute.]
3764 * [Both the "initialized" and "ready" attribute wait conditions
3765 * use the "pager" field as the wait event.]
3767 * The port associations can be broken down by any of the
3768 * following routines:
3769 * vm_object_terminate:
3770 * No references to the vm_object remain, and
3771 * the object cannot (or will not) be cached.
3772 * This is the normal case, and is done even
3773 * though one of the other cases has already been
3775 * memory_object_destroy:
3776 * The memory manager has requested that the
3777 * kernel relinquish references to the memory
3778 * object. [The memory manager may not want to
3779 * destroy the memory object, but may wish to
3780 * refuse or tear down existing memory mappings.]
3782 * Each routine that breaks an association must break all of
3783 * them at once. At some later time, that routine must clear
3784 * the pager field and release the memory object references.
3785 * [Furthermore, each routine must cope with the simultaneous
3786 * or previous operations of the others.]
3788 * In addition to the lock on the object, the vm_object_hash_lock
3789 * governs the associations. References gained through the
3790 * association require use of the hash lock.
3792 * Because the pager field may be cleared spontaneously, it
3793 * cannot be used to determine whether a memory object has
3794 * ever been associated with a particular vm_object. [This
3795 * knowledge is important to the shadow object mechanism.]
3796 * For this reason, an additional "created" attribute is
3799 * During various paging operations, the pager reference found in the
3800 * vm_object must be valid. To prevent this from being released,
3801 * (other than being removed, i.e., made null), routines may use
3802 * the vm_object_paging_begin/end routines [actually, macros].
3803 * The implementation uses the "paging_in_progress" and "wanted" fields.
3804 * [Operations that alter the validity of the pager values include the
3805 * termination routines and vm_object_collapse.]
3810 * Routine: vm_object_enter
3812 * Find a VM object corresponding to the given
3813 * pager; if no such object exists, create one,
3814 * and initialize the pager.
3818 memory_object_t pager
,
3819 vm_object_size_t size
,
3824 register vm_object_t object
;
3825 vm_object_t new_object
;
3826 boolean_t must_init
;
3827 vm_object_hash_entry_t entry
, new_entry
;
3828 uint32_t try_failed_count
= 0;
3831 if (pager
== MEMORY_OBJECT_NULL
)
3832 return(vm_object_allocate(size
));
3834 new_object
= VM_OBJECT_NULL
;
3835 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
3839 * Look for an object associated with this port.
3842 lck
= vm_object_hash_lock_spin(pager
);
3844 entry
= vm_object_hash_lookup(pager
, FALSE
);
3846 if (entry
== VM_OBJECT_HASH_ENTRY_NULL
) {
3847 if (new_object
== VM_OBJECT_NULL
) {
3849 * We must unlock to create a new object;
3850 * if we do so, we must try the lookup again.
3852 vm_object_hash_unlock(lck
);
3853 assert(new_entry
== VM_OBJECT_HASH_ENTRY_NULL
);
3854 new_entry
= vm_object_hash_entry_alloc(pager
);
3855 new_object
= vm_object_allocate(size
);
3856 lck
= vm_object_hash_lock_spin(pager
);
3859 * Lookup failed twice, and we have something
3860 * to insert; set the object.
3862 vm_object_hash_insert(new_entry
, new_object
);
3864 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
3865 new_object
= VM_OBJECT_NULL
;
3868 } else if (entry
->object
== VM_OBJECT_NULL
) {
3870 * If a previous object is being terminated,
3871 * we must wait for the termination message
3872 * to be queued (and lookup the entry again).
3874 entry
->waiting
= TRUE
;
3875 entry
= VM_OBJECT_HASH_ENTRY_NULL
;
3876 assert_wait((event_t
) pager
, THREAD_UNINT
);
3877 vm_object_hash_unlock(lck
);
3879 thread_block(THREAD_CONTINUE_NULL
);
3880 lck
= vm_object_hash_lock_spin(pager
);
3882 } while (entry
== VM_OBJECT_HASH_ENTRY_NULL
);
3884 object
= entry
->object
;
3885 assert(object
!= VM_OBJECT_NULL
);
3888 if ( !vm_object_lock_try(object
)) {
3890 vm_object_hash_unlock(lck
);
3893 mutex_pause(try_failed_count
); /* wait a bit */
3896 assert(!internal
|| object
->internal
);
3898 if (object
->ref_count
== 0) {
3899 if ( !vm_object_cache_lock_try()) {
3901 vm_object_hash_unlock(lck
);
3902 vm_object_unlock(object
);
3905 mutex_pause(try_failed_count
); /* wait a bit */
3908 XPR(XPR_VM_OBJECT_CACHE
,
3909 "vm_object_enter: removing %x from cache, head (%x, %x)\n",
3911 vm_object_cached_list
.next
,
3912 vm_object_cached_list
.prev
, 0,0);
3913 queue_remove(&vm_object_cached_list
, object
,
3914 vm_object_t
, cached_list
);
3915 vm_object_cached_count
--;
3917 vm_object_cache_unlock();
3921 assert(!object
->named
);
3922 object
->named
= TRUE
;
3924 vm_object_lock_assert_exclusive(object
);
3925 object
->ref_count
++;
3926 vm_object_res_reference(object
);
3928 vm_object_hash_unlock(lck
);
3929 vm_object_unlock(object
);
3933 vm_object_hash_unlock(lck
);
3935 assert(object
->ref_count
> 0);
3937 VM_STAT_INCR(lookups
);
3940 "vm_o_enter: pager 0x%x obj 0x%x must_init %d\n",
3941 pager
, object
, must_init
, 0, 0);
3944 * If we raced to create a vm_object but lost, let's
3948 if (new_object
!= VM_OBJECT_NULL
)
3949 vm_object_deallocate(new_object
);
3951 if (new_entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
3952 vm_object_hash_entry_free(new_entry
);
3955 memory_object_control_t control
;
3958 * Allocate request port.
3961 control
= memory_object_control_allocate(object
);
3962 assert (control
!= MEMORY_OBJECT_CONTROL_NULL
);
3964 vm_object_lock(object
);
3965 assert(object
!= kernel_object
);
3968 * Copy the reference we were given.
3971 memory_object_reference(pager
);
3972 object
->pager_created
= TRUE
;
3973 object
->pager
= pager
;
3974 object
->internal
= internal
;
3975 object
->pager_trusted
= internal
;
3977 /* copy strategy invalid until set by memory manager */
3978 object
->copy_strategy
= MEMORY_OBJECT_COPY_INVALID
;
3980 object
->pager_control
= control
;
3981 object
->pager_ready
= FALSE
;
3983 vm_object_unlock(object
);
3986 * Let the pager know we're using it.
3989 (void) memory_object_init(pager
,
3990 object
->pager_control
,
3993 vm_object_lock(object
);
3995 object
->named
= TRUE
;
3997 object
->pager_ready
= TRUE
;
3998 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
4001 object
->pager_initialized
= TRUE
;
4002 vm_object_wakeup(object
, VM_OBJECT_EVENT_INITIALIZED
);
4004 vm_object_lock(object
);
4008 * [At this point, the object must be locked]
4012 * Wait for the work above to be done by the first
4013 * thread to map this object.
4016 while (!object
->pager_initialized
) {
4017 vm_object_sleep(object
,
4018 VM_OBJECT_EVENT_INITIALIZED
,
4021 vm_object_unlock(object
);
4024 "vm_object_enter: vm_object %x, memory_object %x, internal %d\n",
4025 object
, object
->pager
, internal
, 0,0);
4030 * Routine: vm_object_pager_create
4032 * Create a memory object for an internal object.
4033 * In/out conditions:
4034 * The object is locked on entry and exit;
4035 * it may be unlocked within this call.
4037 * Only one thread may be performing a
4038 * vm_object_pager_create on an object at
4039 * a time. Presumably, only the pageout
4040 * daemon will be using this routine.
4044 vm_object_pager_create(
4045 register vm_object_t object
)
4047 memory_object_t pager
;
4048 vm_object_hash_entry_t entry
;
4051 vm_object_size_t size
;
4052 vm_external_map_t map
;
4053 #endif /* MACH_PAGEMAP */
4055 XPR(XPR_VM_OBJECT
, "vm_object_pager_create, object 0x%X\n",
4058 assert(object
!= kernel_object
);
4060 if (memory_manager_default_check() != KERN_SUCCESS
)
4064 * Prevent collapse or termination by holding a paging reference
4067 vm_object_paging_begin(object
);
4068 if (object
->pager_created
) {
4070 * Someone else got to it first...
4071 * wait for them to finish initializing the ports
4073 while (!object
->pager_initialized
) {
4074 vm_object_sleep(object
,
4075 VM_OBJECT_EVENT_INITIALIZED
,
4078 vm_object_paging_end(object
);
4083 * Indicate that a memory object has been assigned
4084 * before dropping the lock, to prevent a race.
4087 object
->pager_created
= TRUE
;
4088 object
->paging_offset
= 0;
4091 size
= object
->size
;
4092 #endif /* MACH_PAGEMAP */
4093 vm_object_unlock(object
);
4096 map
= vm_external_create(size
);
4097 vm_object_lock(object
);
4098 assert(object
->size
== size
);
4099 object
->existence_map
= map
;
4100 vm_object_unlock(object
);
4101 #endif /* MACH_PAGEMAP */
4103 if ((uint32_t) object
->size
!= object
->size
) {
4104 panic("vm_object_pager_create(): object size 0x%llx >= 4GB\n",
4105 (uint64_t) object
->size
);
4109 * Create the [internal] pager, and associate it with this object.
4111 * We make the association here so that vm_object_enter()
4112 * can look up the object to complete initializing it. No
4113 * user will ever map this object.
4116 memory_object_default_t dmm
;
4118 /* acquire a reference for the default memory manager */
4119 dmm
= memory_manager_default_reference();
4121 assert(object
->temporary
);
4123 /* create our new memory object */
4124 assert((vm_size_t
) object
->size
== object
->size
);
4125 (void) memory_object_create(dmm
, (vm_size_t
) object
->size
,
4128 memory_object_default_deallocate(dmm
);
4131 entry
= vm_object_hash_entry_alloc(pager
);
4133 lck
= vm_object_hash_lock_spin(pager
);
4134 vm_object_hash_insert(entry
, object
);
4135 vm_object_hash_unlock(lck
);
4138 * A reference was returned by
4139 * memory_object_create(), and it is
4140 * copied by vm_object_enter().
4143 if (vm_object_enter(pager
, object
->size
, TRUE
, TRUE
, FALSE
) != object
)
4144 panic("vm_object_pager_create: mismatch");
4147 * Drop the reference we were passed.
4149 memory_object_deallocate(pager
);
4151 vm_object_lock(object
);
4154 * Release the paging reference
4156 vm_object_paging_end(object
);
4160 * Routine: vm_object_remove
4162 * Eliminate the pager/object association
4165 * The object cache must be locked.
4167 __private_extern__
void
4171 memory_object_t pager
;
4173 if ((pager
= object
->pager
) != MEMORY_OBJECT_NULL
) {
4174 vm_object_hash_entry_t entry
;
4176 entry
= vm_object_hash_lookup(pager
, FALSE
);
4177 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
4178 entry
->object
= VM_OBJECT_NULL
;
4184 * Global variables for vm_object_collapse():
4186 * Counts for normal collapses and bypasses.
4187 * Debugging variables, to watch or disable collapse.
4189 static long object_collapses
= 0;
4190 static long object_bypasses
= 0;
4192 static boolean_t vm_object_collapse_allowed
= TRUE
;
4193 static boolean_t vm_object_bypass_allowed
= TRUE
;
4196 static int vm_external_discarded
;
4197 static int vm_external_collapsed
;
4200 unsigned long vm_object_collapse_encrypted
= 0;
4203 * Routine: vm_object_do_collapse
4205 * Collapse an object with the object backing it.
4206 * Pages in the backing object are moved into the
4207 * parent, and the backing object is deallocated.
4209 * Both objects and the cache are locked; the page
4210 * queues are unlocked.
4214 vm_object_do_collapse(
4216 vm_object_t backing_object
)
4219 vm_object_offset_t new_offset
, backing_offset
;
4220 vm_object_size_t size
;
4222 vm_object_lock_assert_exclusive(object
);
4223 vm_object_lock_assert_exclusive(backing_object
);
4225 backing_offset
= object
->shadow_offset
;
4226 size
= object
->size
;
4229 * Move all in-memory pages from backing_object
4230 * to the parent. Pages that have been paged out
4231 * will be overwritten by any of the parent's
4232 * pages that shadow them.
4235 while (!queue_empty(&backing_object
->memq
)) {
4237 p
= (vm_page_t
) queue_first(&backing_object
->memq
);
4239 new_offset
= (p
->offset
- backing_offset
);
4241 assert(!p
->busy
|| p
->absent
);
4244 * If the parent has a page here, or if
4245 * this page falls outside the parent,
4248 * Otherwise, move it as planned.
4251 if (p
->offset
< backing_offset
|| new_offset
>= size
) {
4256 * The encryption key includes the "pager" and the
4257 * "paging_offset". These will not change during the
4258 * object collapse, so we can just move an encrypted
4259 * page from one object to the other in this case.
4260 * We can't decrypt the page here, since we can't drop
4264 vm_object_collapse_encrypted
++;
4266 pp
= vm_page_lookup(object
, new_offset
);
4267 if (pp
== VM_PAGE_NULL
) {
4270 * Parent now has no page.
4271 * Move the backing object's page up.
4274 vm_page_rename(p
, object
, new_offset
, TRUE
);
4276 } else if (pp
->absent
) {
4279 * Parent has an absent page...
4280 * it's not being paged in, so
4281 * it must really be missing from
4284 * Throw out the absent page...
4285 * any faults looking for that
4286 * page will restart with the new
4291 vm_page_rename(p
, object
, new_offset
, TRUE
);
4292 #endif /* MACH_PAGEMAP */
4294 assert(! pp
->absent
);
4297 * Parent object has a real page.
4298 * Throw away the backing object's
4307 assert((!object
->pager_created
&& (object
->pager
== MEMORY_OBJECT_NULL
))
4308 || (!backing_object
->pager_created
4309 && (backing_object
->pager
== MEMORY_OBJECT_NULL
)));
4311 assert(!object
->pager_created
&& object
->pager
== MEMORY_OBJECT_NULL
);
4312 #endif /* !MACH_PAGEMAP */
4314 if (backing_object
->pager
!= MEMORY_OBJECT_NULL
) {
4315 vm_object_hash_entry_t entry
;
4318 * Move the pager from backing_object to object.
4320 * XXX We're only using part of the paging space
4321 * for keeps now... we ought to discard the
4325 assert(!object
->paging_in_progress
);
4326 assert(!object
->activity_in_progress
);
4327 object
->pager
= backing_object
->pager
;
4329 if (backing_object
->hashed
) {
4332 lck
= vm_object_hash_lock_spin(backing_object
->pager
);
4333 entry
= vm_object_hash_lookup(object
->pager
, FALSE
);
4334 assert(entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
4335 entry
->object
= object
;
4336 vm_object_hash_unlock(lck
);
4338 object
->hashed
= TRUE
;
4340 object
->pager_created
= backing_object
->pager_created
;
4341 object
->pager_control
= backing_object
->pager_control
;
4342 object
->pager_ready
= backing_object
->pager_ready
;
4343 object
->pager_initialized
= backing_object
->pager_initialized
;
4344 object
->paging_offset
=
4345 backing_object
->paging_offset
+ backing_offset
;
4346 if (object
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
4347 memory_object_control_collapse(object
->pager_control
,
4354 * If the shadow offset is 0, the use the existence map from
4355 * the backing object if there is one. If the shadow offset is
4356 * not zero, toss it.
4358 * XXX - If the shadow offset is not 0 then a bit copy is needed
4359 * if the map is to be salvaged. For now, we just just toss the
4360 * old map, giving the collapsed object no map. This means that
4361 * the pager is invoked for zero fill pages. If analysis shows
4362 * that this happens frequently and is a performance hit, then
4363 * this code should be fixed to salvage the map.
4365 assert(object
->existence_map
== VM_EXTERNAL_NULL
);
4366 if (backing_offset
|| (size
!= backing_object
->size
)) {
4367 vm_external_discarded
++;
4368 vm_external_destroy(backing_object
->existence_map
,
4369 backing_object
->size
);
4372 vm_external_collapsed
++;
4373 object
->existence_map
= backing_object
->existence_map
;
4375 backing_object
->existence_map
= VM_EXTERNAL_NULL
;
4376 #endif /* MACH_PAGEMAP */
4379 * Object now shadows whatever backing_object did.
4380 * Note that the reference to backing_object->shadow
4381 * moves from within backing_object to within object.
4384 assert(!object
->phys_contiguous
);
4385 assert(!backing_object
->phys_contiguous
);
4386 object
->shadow
= backing_object
->shadow
;
4387 if (object
->shadow
) {
4388 object
->shadow_offset
+= backing_object
->shadow_offset
;
4390 /* no shadow, therefore no shadow offset... */
4391 object
->shadow_offset
= 0;
4393 assert((object
->shadow
== VM_OBJECT_NULL
) ||
4394 (object
->shadow
->copy
!= backing_object
));
4397 * Discard backing_object.
4399 * Since the backing object has no pages, no
4400 * pager left, and no object references within it,
4401 * all that is necessary is to dispose of it.
4404 assert((backing_object
->ref_count
== 1) &&
4405 (backing_object
->resident_page_count
== 0) &&
4406 (backing_object
->paging_in_progress
== 0) &&
4407 (backing_object
->activity_in_progress
== 0));
4409 backing_object
->alive
= FALSE
;
4410 vm_object_unlock(backing_object
);
4412 XPR(XPR_VM_OBJECT
, "vm_object_collapse, collapsed 0x%X\n",
4413 backing_object
, 0,0,0,0);
4415 vm_object_lock_destroy(backing_object
);
4417 zfree(vm_object_zone
, backing_object
);
4423 vm_object_do_bypass(
4425 vm_object_t backing_object
)
4428 * Make the parent shadow the next object
4432 vm_object_lock_assert_exclusive(object
);
4433 vm_object_lock_assert_exclusive(backing_object
);
4437 * Do object reference in-line to
4438 * conditionally increment shadow's
4439 * residence count. If object is not
4440 * resident, leave residence count
4443 if (backing_object
->shadow
!= VM_OBJECT_NULL
) {
4444 vm_object_lock(backing_object
->shadow
);
4445 vm_object_lock_assert_exclusive(backing_object
->shadow
);
4446 backing_object
->shadow
->ref_count
++;
4447 if (object
->res_count
!= 0)
4448 vm_object_res_reference(backing_object
->shadow
);
4449 vm_object_unlock(backing_object
->shadow
);
4451 #else /* TASK_SWAPPER */
4452 vm_object_reference(backing_object
->shadow
);
4453 #endif /* TASK_SWAPPER */
4455 assert(!object
->phys_contiguous
);
4456 assert(!backing_object
->phys_contiguous
);
4457 object
->shadow
= backing_object
->shadow
;
4458 if (object
->shadow
) {
4459 object
->shadow_offset
+= backing_object
->shadow_offset
;
4461 /* no shadow, therefore no shadow offset... */
4462 object
->shadow_offset
= 0;
4466 * Backing object might have had a copy pointer
4467 * to us. If it did, clear it.
4469 if (backing_object
->copy
== object
) {
4470 backing_object
->copy
= VM_OBJECT_NULL
;
4474 * Drop the reference count on backing_object.
4476 * Since its ref_count was at least 2, it
4477 * will not vanish; so we don't need to call
4478 * vm_object_deallocate.
4479 * [with a caveat for "named" objects]
4481 * The res_count on the backing object is
4482 * conditionally decremented. It's possible
4483 * (via vm_pageout_scan) to get here with
4484 * a "swapped" object, which has a 0 res_count,
4485 * in which case, the backing object res_count
4486 * is already down by one.
4488 * Don't call vm_object_deallocate unless
4489 * ref_count drops to zero.
4491 * The ref_count can drop to zero here if the
4492 * backing object could be bypassed but not
4493 * collapsed, such as when the backing object
4494 * is temporary and cachable.
4497 if (backing_object
->ref_count
> 2 ||
4498 (!backing_object
->named
&& backing_object
->ref_count
> 1)) {
4499 vm_object_lock_assert_exclusive(backing_object
);
4500 backing_object
->ref_count
--;
4502 if (object
->res_count
!= 0)
4503 vm_object_res_deallocate(backing_object
);
4504 assert(backing_object
->ref_count
> 0);
4505 #endif /* TASK_SWAPPER */
4506 vm_object_unlock(backing_object
);
4510 * Drop locks so that we can deallocate
4511 * the backing object.
4515 if (object
->res_count
== 0) {
4516 /* XXX get a reference for the deallocate below */
4517 vm_object_res_reference(backing_object
);
4519 #endif /* TASK_SWAPPER */
4520 vm_object_unlock(object
);
4521 vm_object_unlock(backing_object
);
4522 vm_object_deallocate(backing_object
);
4525 * Relock object. We don't have to reverify
4526 * its state since vm_object_collapse will
4527 * do that for us as it starts at the
4531 vm_object_lock(object
);
4539 * vm_object_collapse:
4541 * Perform an object collapse or an object bypass if appropriate.
4542 * The real work of collapsing and bypassing is performed in
4543 * the routines vm_object_do_collapse and vm_object_do_bypass.
4545 * Requires that the object be locked and the page queues be unlocked.
4548 static unsigned long vm_object_collapse_calls
= 0;
4549 static unsigned long vm_object_collapse_objects
= 0;
4550 static unsigned long vm_object_collapse_do_collapse
= 0;
4551 static unsigned long vm_object_collapse_do_bypass
= 0;
4552 static unsigned long vm_object_collapse_delays
= 0;
4553 __private_extern__
void
4555 register vm_object_t object
,
4556 register vm_object_offset_t hint_offset
,
4557 boolean_t can_bypass
)
4559 register vm_object_t backing_object
;
4560 register unsigned int rcount
;
4561 register unsigned int size
;
4562 vm_object_t original_object
;
4563 int object_lock_type
;
4564 int backing_object_lock_type
;
4566 vm_object_collapse_calls
++;
4568 if (! vm_object_collapse_allowed
&&
4569 ! (can_bypass
&& vm_object_bypass_allowed
)) {
4573 XPR(XPR_VM_OBJECT
, "vm_object_collapse, obj 0x%X\n",
4576 if (object
== VM_OBJECT_NULL
)
4579 original_object
= object
;
4582 * The top object was locked "exclusive" by the caller.
4583 * In the first pass, to determine if we can collapse the shadow chain,
4584 * take a "shared" lock on the shadow objects. If we can collapse,
4585 * we'll have to go down the chain again with exclusive locks.
4587 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4588 backing_object_lock_type
= OBJECT_LOCK_SHARED
;
4591 object
= original_object
;
4592 vm_object_lock_assert_exclusive(object
);
4595 vm_object_collapse_objects
++;
4597 * Verify that the conditions are right for either
4598 * collapse or bypass:
4602 * There is a backing object, and
4605 backing_object
= object
->shadow
;
4606 if (backing_object
== VM_OBJECT_NULL
) {
4607 if (object
!= original_object
) {
4608 vm_object_unlock(object
);
4612 if (backing_object_lock_type
== OBJECT_LOCK_SHARED
) {
4613 vm_object_lock_shared(backing_object
);
4615 vm_object_lock(backing_object
);
4619 * No pages in the object are currently
4620 * being paged out, and
4622 if (object
->paging_in_progress
!= 0 ||
4623 object
->activity_in_progress
!= 0) {
4624 /* try and collapse the rest of the shadow chain */
4625 if (object
!= original_object
) {
4626 vm_object_unlock(object
);
4628 object
= backing_object
;
4629 object_lock_type
= backing_object_lock_type
;
4635 * The backing object is not read_only,
4636 * and no pages in the backing object are
4637 * currently being paged out.
4638 * The backing object is internal.
4642 if (!backing_object
->internal
||
4643 backing_object
->paging_in_progress
!= 0 ||
4644 backing_object
->activity_in_progress
!= 0) {
4645 /* try and collapse the rest of the shadow chain */
4646 if (object
!= original_object
) {
4647 vm_object_unlock(object
);
4649 object
= backing_object
;
4650 object_lock_type
= backing_object_lock_type
;
4655 * The backing object can't be a copy-object:
4656 * the shadow_offset for the copy-object must stay
4657 * as 0. Furthermore (for the 'we have all the
4658 * pages' case), if we bypass backing_object and
4659 * just shadow the next object in the chain, old
4660 * pages from that object would then have to be copied
4661 * BOTH into the (former) backing_object and into the
4664 if (backing_object
->shadow
!= VM_OBJECT_NULL
&&
4665 backing_object
->shadow
->copy
== backing_object
) {
4666 /* try and collapse the rest of the shadow chain */
4667 if (object
!= original_object
) {
4668 vm_object_unlock(object
);
4670 object
= backing_object
;
4671 object_lock_type
= backing_object_lock_type
;
4676 * We can now try to either collapse the backing
4677 * object (if the parent is the only reference to
4678 * it) or (perhaps) remove the parent's reference
4681 * If there is exactly one reference to the backing
4682 * object, we may be able to collapse it into the
4685 * If MACH_PAGEMAP is defined:
4686 * The parent must not have a pager created for it,
4687 * since collapsing a backing_object dumps new pages
4688 * into the parent that its pager doesn't know about
4689 * (and the collapse code can't merge the existence
4692 * As long as one of the objects is still not known
4693 * to the pager, we can collapse them.
4695 if (backing_object
->ref_count
== 1 &&
4696 (!object
->pager_created
4698 || !backing_object
->pager_created
4699 #endif /*!MACH_PAGEMAP */
4700 ) && vm_object_collapse_allowed
) {
4703 * We need the exclusive lock on the VM objects.
4705 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
4707 * We have an object and its shadow locked
4708 * "shared". We can't just upgrade the locks
4709 * to "exclusive", as some other thread might
4710 * also have these objects locked "shared" and
4711 * attempt to upgrade one or the other to
4712 * "exclusive". The upgrades would block
4713 * forever waiting for the other "shared" locks
4715 * So we have to release the locks and go
4716 * down the shadow chain again (since it could
4717 * have changed) with "exclusive" locking.
4719 vm_object_unlock(backing_object
);
4720 if (object
!= original_object
)
4721 vm_object_unlock(object
);
4722 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4723 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4728 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
4729 backing_object
, object
,
4730 backing_object
->pager
,
4731 backing_object
->pager_control
, 0);
4734 * Collapse the object with its backing
4735 * object, and try again with the object's
4736 * new backing object.
4739 vm_object_do_collapse(object
, backing_object
);
4740 vm_object_collapse_do_collapse
++;
4745 * Collapsing the backing object was not possible
4746 * or permitted, so let's try bypassing it.
4749 if (! (can_bypass
&& vm_object_bypass_allowed
)) {
4750 /* try and collapse the rest of the shadow chain */
4751 if (object
!= original_object
) {
4752 vm_object_unlock(object
);
4754 object
= backing_object
;
4755 object_lock_type
= backing_object_lock_type
;
4761 * If the object doesn't have all its pages present,
4762 * we have to make sure no pages in the backing object
4763 * "show through" before bypassing it.
4765 size
= atop(object
->size
);
4766 rcount
= object
->resident_page_count
;
4767 if (rcount
!= size
) {
4768 vm_object_offset_t offset
;
4769 vm_object_offset_t backing_offset
;
4770 unsigned int backing_rcount
;
4771 unsigned int lookups
= 0;
4774 * If the backing object has a pager but no pagemap,
4775 * then we cannot bypass it, because we don't know
4776 * what pages it has.
4778 if (backing_object
->pager_created
4780 && (backing_object
->existence_map
== VM_EXTERNAL_NULL
)
4781 #endif /* MACH_PAGEMAP */
4783 /* try and collapse the rest of the shadow chain */
4784 if (object
!= original_object
) {
4785 vm_object_unlock(object
);
4787 object
= backing_object
;
4788 object_lock_type
= backing_object_lock_type
;
4793 * If the object has a pager but no pagemap,
4794 * then we cannot bypass it, because we don't know
4795 * what pages it has.
4797 if (object
->pager_created
4799 && (object
->existence_map
== VM_EXTERNAL_NULL
)
4800 #endif /* MACH_PAGEMAP */
4802 /* try and collapse the rest of the shadow chain */
4803 if (object
!= original_object
) {
4804 vm_object_unlock(object
);
4806 object
= backing_object
;
4807 object_lock_type
= backing_object_lock_type
;
4812 * If all of the pages in the backing object are
4813 * shadowed by the parent object, the parent
4814 * object no longer has to shadow the backing
4815 * object; it can shadow the next one in the
4818 * If the backing object has existence info,
4819 * we must check examine its existence info
4824 backing_offset
= object
->shadow_offset
;
4825 backing_rcount
= backing_object
->resident_page_count
;
4828 #define EXISTS_IN_OBJECT(obj, off, rc) \
4829 (vm_external_state_get((obj)->existence_map, \
4830 (vm_offset_t)(off)) == VM_EXTERNAL_STATE_EXISTS || \
4831 ((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
4833 #define EXISTS_IN_OBJECT(obj, off, rc) \
4834 (((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
4835 #endif /* MACH_PAGEMAP */
4838 * Check the hint location first
4839 * (since it is often the quickest way out of here).
4841 if (object
->cow_hint
!= ~(vm_offset_t
)0)
4842 hint_offset
= (vm_object_offset_t
)object
->cow_hint
;
4844 hint_offset
= (hint_offset
> 8 * PAGE_SIZE_64
) ?
4845 (hint_offset
- 8 * PAGE_SIZE_64
) : 0;
4847 if (EXISTS_IN_OBJECT(backing_object
, hint_offset
+
4848 backing_offset
, backing_rcount
) &&
4849 !EXISTS_IN_OBJECT(object
, hint_offset
, rcount
)) {
4850 /* dependency right at the hint */
4851 object
->cow_hint
= (vm_offset_t
) hint_offset
; /* atomic */
4852 /* try and collapse the rest of the shadow chain */
4853 if (object
!= original_object
) {
4854 vm_object_unlock(object
);
4856 object
= backing_object
;
4857 object_lock_type
= backing_object_lock_type
;
4862 * If the object's window onto the backing_object
4863 * is large compared to the number of resident
4864 * pages in the backing object, it makes sense to
4865 * walk the backing_object's resident pages first.
4867 * NOTE: Pages may be in both the existence map and
4868 * resident. So, we can't permanently decrement
4869 * the rcount here because the second loop may
4870 * find the same pages in the backing object'
4871 * existence map that we found here and we would
4872 * double-decrement the rcount. We also may or
4873 * may not have found the
4875 if (backing_rcount
&&
4877 size
> ((backing_object
->existence_map
) ?
4878 backing_rcount
: (backing_rcount
>> 1))
4880 size
> (backing_rcount
>> 1)
4881 #endif /* MACH_PAGEMAP */
4883 unsigned int rc
= rcount
;
4886 backing_rcount
= backing_object
->resident_page_count
;
4887 p
= (vm_page_t
)queue_first(&backing_object
->memq
);
4889 /* Until we get more than one lookup lock */
4890 if (lookups
> 256) {
4891 vm_object_collapse_delays
++;
4896 offset
= (p
->offset
- backing_offset
);
4897 if (offset
< object
->size
&&
4898 offset
!= hint_offset
&&
4899 !EXISTS_IN_OBJECT(object
, offset
, rc
)) {
4900 /* found a dependency */
4901 object
->cow_hint
= (vm_offset_t
) offset
; /* atomic */
4905 p
= (vm_page_t
) queue_next(&p
->listq
);
4907 } while (--backing_rcount
);
4908 if (backing_rcount
!= 0 ) {
4909 /* try and collapse the rest of the shadow chain */
4910 if (object
!= original_object
) {
4911 vm_object_unlock(object
);
4913 object
= backing_object
;
4914 object_lock_type
= backing_object_lock_type
;
4920 * Walk through the offsets looking for pages in the
4921 * backing object that show through to the object.
4925 || backing_object
->existence_map
4926 #endif /* MACH_PAGEMAP */
4928 offset
= hint_offset
;
4931 (offset
+ PAGE_SIZE_64
< object
->size
) ?
4932 (offset
+ PAGE_SIZE_64
) : 0) != hint_offset
) {
4934 /* Until we get more than one lookup lock */
4935 if (lookups
> 256) {
4936 vm_object_collapse_delays
++;
4941 if (EXISTS_IN_OBJECT(backing_object
, offset
+
4942 backing_offset
, backing_rcount
) &&
4943 !EXISTS_IN_OBJECT(object
, offset
, rcount
)) {
4944 /* found a dependency */
4945 object
->cow_hint
= (vm_offset_t
) offset
; /* atomic */
4949 if (offset
!= hint_offset
) {
4950 /* try and collapse the rest of the shadow chain */
4951 if (object
!= original_object
) {
4952 vm_object_unlock(object
);
4954 object
= backing_object
;
4955 object_lock_type
= backing_object_lock_type
;
4962 * We need "exclusive" locks on the 2 VM objects.
4964 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
4965 vm_object_unlock(backing_object
);
4966 if (object
!= original_object
)
4967 vm_object_unlock(object
);
4968 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4969 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4973 /* reset the offset hint for any objects deeper in the chain */
4974 object
->cow_hint
= (vm_offset_t
)0;
4977 * All interesting pages in the backing object
4978 * already live in the parent or its pager.
4979 * Thus we can bypass the backing object.
4982 vm_object_do_bypass(object
, backing_object
);
4983 vm_object_collapse_do_bypass
++;
4986 * Try again with this object's new backing object.
4992 if (object
!= original_object
) {
4993 vm_object_unlock(object
);
4998 * Routine: vm_object_page_remove: [internal]
5000 * Removes all physical pages in the specified
5001 * object range from the object's list of pages.
5003 * In/out conditions:
5004 * The object must be locked.
5005 * The object must not have paging_in_progress, usually
5006 * guaranteed by not having a pager.
5008 unsigned int vm_object_page_remove_lookup
= 0;
5009 unsigned int vm_object_page_remove_iterate
= 0;
5011 __private_extern__
void
5012 vm_object_page_remove(
5013 register vm_object_t object
,
5014 register vm_object_offset_t start
,
5015 register vm_object_offset_t end
)
5017 register vm_page_t p
, next
;
5020 * One and two page removals are most popular.
5021 * The factor of 16 here is somewhat arbitrary.
5022 * It balances vm_object_lookup vs iteration.
5025 if (atop_64(end
- start
) < (unsigned)object
->resident_page_count
/16) {
5026 vm_object_page_remove_lookup
++;
5028 for (; start
< end
; start
+= PAGE_SIZE_64
) {
5029 p
= vm_page_lookup(object
, start
);
5030 if (p
!= VM_PAGE_NULL
) {
5031 assert(!p
->cleaning
&& !p
->pageout
);
5032 if (!p
->fictitious
&& p
->pmapped
)
5033 pmap_disconnect(p
->phys_page
);
5038 vm_object_page_remove_iterate
++;
5040 p
= (vm_page_t
) queue_first(&object
->memq
);
5041 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
5042 next
= (vm_page_t
) queue_next(&p
->listq
);
5043 if ((start
<= p
->offset
) && (p
->offset
< end
)) {
5044 assert(!p
->cleaning
&& !p
->pageout
);
5045 if (!p
->fictitious
&& p
->pmapped
)
5046 pmap_disconnect(p
->phys_page
);
5056 * Routine: vm_object_coalesce
5057 * Function: Coalesces two objects backing up adjoining
5058 * regions of memory into a single object.
5060 * returns TRUE if objects were combined.
5062 * NOTE: Only works at the moment if the second object is NULL -
5063 * if it's not, which object do we lock first?
5066 * prev_object First object to coalesce
5067 * prev_offset Offset into prev_object
5068 * next_object Second object into coalesce
5069 * next_offset Offset into next_object
5071 * prev_size Size of reference to prev_object
5072 * next_size Size of reference to next_object
5075 * The object(s) must *not* be locked. The map must be locked
5076 * to preserve the reference to the object(s).
5078 static int vm_object_coalesce_count
= 0;
5080 __private_extern__ boolean_t
5082 register vm_object_t prev_object
,
5083 vm_object_t next_object
,
5084 vm_object_offset_t prev_offset
,
5085 __unused vm_object_offset_t next_offset
,
5086 vm_object_size_t prev_size
,
5087 vm_object_size_t next_size
)
5089 vm_object_size_t newsize
;
5095 if (next_object
!= VM_OBJECT_NULL
) {
5099 if (prev_object
== VM_OBJECT_NULL
) {
5104 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
5105 prev_object
, prev_offset
, prev_size
, next_size
, 0);
5107 vm_object_lock(prev_object
);
5110 * Try to collapse the object first
5112 vm_object_collapse(prev_object
, prev_offset
, TRUE
);
5115 * Can't coalesce if pages not mapped to
5116 * prev_entry may be in use any way:
5117 * . more than one reference
5119 * . shadows another object
5120 * . has a copy elsewhere
5122 * . paging references (pages might be in page-list)
5125 if ((prev_object
->ref_count
> 1) ||
5126 prev_object
->pager_created
||
5127 (prev_object
->shadow
!= VM_OBJECT_NULL
) ||
5128 (prev_object
->copy
!= VM_OBJECT_NULL
) ||
5129 (prev_object
->true_share
!= FALSE
) ||
5130 (prev_object
->purgable
!= VM_PURGABLE_DENY
) ||
5131 (prev_object
->paging_in_progress
!= 0) ||
5132 (prev_object
->activity_in_progress
!= 0)) {
5133 vm_object_unlock(prev_object
);
5137 vm_object_coalesce_count
++;
5140 * Remove any pages that may still be in the object from
5141 * a previous deallocation.
5143 vm_object_page_remove(prev_object
,
5144 prev_offset
+ prev_size
,
5145 prev_offset
+ prev_size
+ next_size
);
5148 * Extend the object if necessary.
5150 newsize
= prev_offset
+ prev_size
+ next_size
;
5151 if (newsize
> prev_object
->size
) {
5154 * We cannot extend an object that has existence info,
5155 * since the existence info might then fail to cover
5156 * the entire object.
5158 * This assertion must be true because the object
5159 * has no pager, and we only create existence info
5160 * for objects with pagers.
5162 assert(prev_object
->existence_map
== VM_EXTERNAL_NULL
);
5163 #endif /* MACH_PAGEMAP */
5164 prev_object
->size
= newsize
;
5167 vm_object_unlock(prev_object
);
5172 * Attach a set of physical pages to an object, so that they can
5173 * be mapped by mapping the object. Typically used to map IO memory.
5175 * The mapping function and its private data are used to obtain the
5176 * physical addresses for each page to be mapped.
5181 vm_object_offset_t offset
,
5182 vm_object_size_t size
,
5183 vm_object_offset_t (*map_fn
)(void *map_fn_data
,
5184 vm_object_offset_t offset
),
5185 void *map_fn_data
) /* private to map_fn */
5191 vm_object_offset_t addr
;
5193 num_pages
= atop_64(size
);
5195 for (i
= 0; i
< num_pages
; i
++, offset
+= PAGE_SIZE_64
) {
5197 addr
= (*map_fn
)(map_fn_data
, offset
);
5199 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
5200 vm_page_more_fictitious();
5202 vm_object_lock(object
);
5203 if ((old_page
= vm_page_lookup(object
, offset
))
5206 VM_PAGE_FREE(old_page
);
5209 assert((ppnum_t
) addr
== addr
);
5210 vm_page_init(m
, (ppnum_t
) addr
, FALSE
);
5212 * private normally requires lock_queues but since we
5213 * are initializing the page, its not necessary here
5215 m
->private = TRUE
; /* don`t free page */
5217 vm_page_insert(m
, object
, offset
);
5219 PAGE_WAKEUP_DONE(m
);
5220 vm_object_unlock(object
);
5224 #include <mach_kdb.h>
5227 #include <ddb/db_output.h>
5228 #include <vm/vm_print.h>
5230 #define printf kdbprintf
5232 extern boolean_t
vm_object_cached(
5233 vm_object_t object
);
5235 extern void print_bitstring(
5238 boolean_t vm_object_print_pages
= FALSE
;
5244 printf("%c%c%c%c%c%c%c%c",
5245 ((byte
& (1 << 0)) ? '1' : '0'),
5246 ((byte
& (1 << 1)) ? '1' : '0'),
5247 ((byte
& (1 << 2)) ? '1' : '0'),
5248 ((byte
& (1 << 3)) ? '1' : '0'),
5249 ((byte
& (1 << 4)) ? '1' : '0'),
5250 ((byte
& (1 << 5)) ? '1' : '0'),
5251 ((byte
& (1 << 6)) ? '1' : '0'),
5252 ((byte
& (1 << 7)) ? '1' : '0'));
5257 __unused
register vm_object_t object
)
5260 register vm_object_t o
;
5262 queue_iterate(&vm_object_cached_list
, o
, vm_object_t
, cached_list
) {
5273 * vm_external_print: [ debug ]
5277 vm_external_map_t emap
,
5278 vm_object_size_t size
)
5280 if (emap
== VM_EXTERNAL_NULL
) {
5283 vm_object_size_t existence_size
= stob(size
);
5284 printf("{ size=%lld, map=[", (uint64_t) existence_size
);
5285 if (existence_size
> 0) {
5286 print_bitstring(emap
[0]);
5288 if (existence_size
> 1) {
5289 print_bitstring(emap
[1]);
5291 if (existence_size
> 2) {
5293 print_bitstring(emap
[existence_size
-1]);
5299 #endif /* MACH_PAGEMAP */
5306 int orig_db_indent
= db_indent
;
5309 if (object
== VM_OBJECT_NULL
) {
5310 db_indent
= orig_db_indent
;
5316 iprintf("object 0x%x", object
);
5317 printf(", shadow=0x%x", object
->shadow
);
5318 printf(", copy=0x%x", object
->copy
);
5319 printf(", pager=0x%x", object
->pager
);
5320 printf(", ref=%d\n", object
->ref_count
);
5323 object
= object
->shadow
;
5329 * vm_object_print: [ debug ]
5332 vm_object_print(db_expr_t db_addr
, __unused boolean_t have_addr
,
5333 __unused db_expr_t arg_count
, __unused
char *modif
)
5336 register vm_page_t p
;
5341 object
= (vm_object_t
) (long) db_addr
;
5342 if (object
== VM_OBJECT_NULL
)
5345 iprintf("object 0x%x\n", object
);
5349 iprintf("size=0x%x", object
->size
);
5350 printf(", memq_hint=%p", object
->memq_hint
);
5351 printf(", ref_count=%d\n", object
->ref_count
);
5354 printf("res_count=%d, ", object
->res_count
);
5355 #endif /* TASK_SWAPPER */
5356 printf("resident_page_count=%d\n", object
->resident_page_count
);
5358 iprintf("shadow=0x%x", object
->shadow
);
5359 if (object
->shadow
) {
5361 vm_object_t shadow
= object
;
5362 while((shadow
= shadow
->shadow
))
5364 printf(" (depth %d)", i
);
5366 printf(", copy=0x%x", object
->copy
);
5367 printf(", shadow_offset=0x%x", object
->shadow_offset
);
5368 printf(", last_alloc=0x%x\n", object
->last_alloc
);
5370 iprintf("pager=0x%x", object
->pager
);
5371 printf(", paging_offset=0x%x", object
->paging_offset
);
5372 printf(", pager_control=0x%x\n", object
->pager_control
);
5374 iprintf("copy_strategy=%d[", object
->copy_strategy
);
5375 switch (object
->copy_strategy
) {
5376 case MEMORY_OBJECT_COPY_NONE
:
5377 printf("copy_none");
5380 case MEMORY_OBJECT_COPY_CALL
:
5381 printf("copy_call");
5384 case MEMORY_OBJECT_COPY_DELAY
:
5385 printf("copy_delay");
5388 case MEMORY_OBJECT_COPY_SYMMETRIC
:
5389 printf("copy_symmetric");
5392 case MEMORY_OBJECT_COPY_INVALID
:
5393 printf("copy_invalid");
5401 iprintf("all_wanted=0x%x<", object
->all_wanted
);
5403 if (vm_object_wanted(object
, VM_OBJECT_EVENT_INITIALIZED
)) {
5404 printf("%sinit", s
);
5407 if (vm_object_wanted(object
, VM_OBJECT_EVENT_PAGER_READY
)) {
5408 printf("%sready", s
);
5411 if (vm_object_wanted(object
, VM_OBJECT_EVENT_PAGING_IN_PROGRESS
)) {
5412 printf("%spaging", s
);
5415 if (vm_object_wanted(object
, VM_OBJECT_EVENT_LOCK_IN_PROGRESS
)) {
5416 printf("%slock", s
);
5419 if (vm_object_wanted(object
, VM_OBJECT_EVENT_UNCACHING
)) {
5420 printf("%suncaching", s
);
5423 if (vm_object_wanted(object
, VM_OBJECT_EVENT_COPY_CALL
)) {
5424 printf("%scopy_call", s
);
5427 if (vm_object_wanted(object
, VM_OBJECT_EVENT_CACHING
)) {
5428 printf("%scaching", s
);
5432 printf(", paging_in_progress=%d\n", object
->paging_in_progress
);
5433 printf(", activity_in_progress=%d\n", object
->activity_in_progress
);
5435 iprintf("%screated, %sinit, %sready, %spersist, %strusted, %spageout, %s, %s\n",
5436 (object
->pager_created
? "" : "!"),
5437 (object
->pager_initialized
? "" : "!"),
5438 (object
->pager_ready
? "" : "!"),
5439 (object
->can_persist
? "" : "!"),
5440 (object
->pager_trusted
? "" : "!"),
5441 (object
->pageout
? "" : "!"),
5442 (object
->internal
? "internal" : "external"),
5443 (object
->temporary
? "temporary" : "permanent"));
5444 iprintf("%salive, %spurgeable, %spurgeable_volatile, %spurgeable_empty, %sshadowed, %scached, %sprivate\n",
5445 (object
->alive
? "" : "!"),
5446 ((object
->purgable
!= VM_PURGABLE_DENY
) ? "" : "!"),
5447 ((object
->purgable
== VM_PURGABLE_VOLATILE
) ? "" : "!"),
5448 ((object
->purgable
== VM_PURGABLE_EMPTY
) ? "" : "!"),
5449 (object
->shadowed
? "" : "!"),
5450 (vm_object_cached(object
) ? "" : "!"),
5451 (object
->private ? "" : "!"));
5452 iprintf("%sadvisory_pageout, %ssilent_overwrite\n",
5453 (object
->advisory_pageout
? "" : "!"),
5454 (object
->silent_overwrite
? "" : "!"));
5457 iprintf("existence_map=");
5458 vm_external_print(object
->existence_map
, object
->size
);
5459 #endif /* MACH_PAGEMAP */
5461 iprintf("paging_object=0x%x\n", object
->paging_object
);
5462 #endif /* MACH_ASSERT */
5464 if (vm_object_print_pages
) {
5466 p
= (vm_page_t
) queue_first(&object
->memq
);
5467 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
5469 iprintf("memory:=");
5470 } else if (count
== 2) {
5479 printf("(off=0x%llX,page=%p)", p
->offset
, p
);
5480 p
= (vm_page_t
) queue_next(&p
->listq
);
5491 * vm_object_find [ debug ]
5493 * Find all tasks which reference the given vm_object.
5496 boolean_t
vm_object_find(vm_object_t object
);
5497 boolean_t vm_object_print_verbose
= FALSE
;
5505 vm_map_entry_t entry
;
5506 boolean_t found
= FALSE
;
5508 queue_iterate(&tasks
, task
, task_t
, tasks
) {
5510 for (entry
= vm_map_first_entry(map
);
5511 entry
&& entry
!= vm_map_to_entry(map
);
5512 entry
= entry
->vme_next
) {
5517 * For the time being skip submaps,
5518 * only the kernel can have submaps,
5519 * and unless we are interested in
5520 * kernel objects, we can simply skip
5521 * submaps. See sb/dejan/nmk18b7/src/mach_kernel/vm
5522 * for a full solution.
5524 if (entry
->is_sub_map
)
5527 obj
= entry
->object
.vm_object
;
5531 while (obj
!= VM_OBJECT_NULL
) {
5532 if (obj
== object
) {
5534 printf("TASK\t\tMAP\t\tENTRY\n");
5537 printf("0x%x\t0x%x\t0x%x\n",
5548 #endif /* MACH_KDB */
5551 vm_object_populate_with_private(
5553 vm_object_offset_t offset
,
5558 vm_object_offset_t base_offset
;
5561 if(!object
->private)
5562 return KERN_FAILURE
;
5564 base_page
= phys_page
;
5566 vm_object_lock(object
);
5567 if(!object
->phys_contiguous
) {
5569 if((base_offset
= trunc_page_64(offset
)) != offset
) {
5570 vm_object_unlock(object
);
5571 return KERN_FAILURE
;
5573 base_offset
+= object
->paging_offset
;
5575 m
= vm_page_lookup(object
, base_offset
);
5576 if(m
!= VM_PAGE_NULL
) {
5578 if (m
->phys_page
!= vm_page_guard_addr
) {
5580 vm_page_lockspin_queues();
5582 vm_page_unlock_queues();
5584 m
->fictitious
= FALSE
;
5585 m
->phys_page
= base_page
;
5592 m
->list_req_pending
= TRUE
;
5594 } else if (m
->phys_page
!= base_page
) {
5597 * pmap call to clear old mapping
5599 pmap_disconnect(m
->phys_page
);
5601 m
->phys_page
= base_page
;
5606 * We're not pointing to the same
5607 * physical page any longer and the
5608 * contents of the new one are not
5609 * supposed to be encrypted.
5610 * XXX What happens to the original
5611 * physical page. Is it lost ?
5613 m
->encrypted
= FALSE
;
5616 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
5617 vm_page_more_fictitious();
5620 * private normally requires lock_queues but since we
5621 * are initializing the page, its not necessary here
5624 m
->fictitious
= FALSE
;
5625 m
->phys_page
= base_page
;
5626 m
->list_req_pending
= TRUE
;
5630 vm_page_insert(m
, object
, base_offset
);
5632 base_page
++; /* Go to the next physical page */
5633 base_offset
+= PAGE_SIZE
;
5637 /* NOTE: we should check the original settings here */
5638 /* if we have a size > zero a pmap call should be made */
5639 /* to disable the range */
5643 /* shadows on contiguous memory are not allowed */
5644 /* we therefore can use the offset field */
5645 object
->shadow_offset
= (vm_object_offset_t
)phys_page
<< PAGE_SHIFT
;
5646 object
->size
= size
;
5648 vm_object_unlock(object
);
5649 return KERN_SUCCESS
;
5653 * memory_object_free_from_cache:
5655 * Walk the vm_object cache list, removing and freeing vm_objects
5656 * which are backed by the pager identified by the caller, (pager_ops).
5657 * Remove up to "count" objects, if there are that may available
5660 * Walk the list at most once, return the number of vm_objects
5664 __private_extern__ kern_return_t
5665 memory_object_free_from_cache(
5666 __unused host_t host
,
5667 __unused memory_object_pager_ops_t pager_ops
,
5671 int object_released
= 0;
5673 register vm_object_t object
= VM_OBJECT_NULL
;
5677 if(host == HOST_NULL)
5678 return(KERN_INVALID_ARGUMENT);
5682 vm_object_cache_lock();
5684 queue_iterate(&vm_object_cached_list
, object
,
5685 vm_object_t
, cached_list
) {
5686 if (object
->pager
&&
5687 (pager_ops
== object
->pager
->mo_pager_ops
)) {
5688 vm_object_lock(object
);
5689 queue_remove(&vm_object_cached_list
, object
,
5690 vm_object_t
, cached_list
);
5691 vm_object_cached_count
--;
5693 vm_object_cache_unlock();
5695 * Since this object is in the cache, we know
5696 * that it is initialized and has only a pager's
5697 * (implicit) reference. Take a reference to avoid
5698 * recursive deallocations.
5701 assert(object
->pager_initialized
);
5702 assert(object
->ref_count
== 0);
5703 vm_object_lock_assert_exclusive(object
);
5704 object
->ref_count
++;
5707 * Terminate the object.
5708 * If the object had a shadow, we let
5709 * vm_object_deallocate deallocate it.
5710 * "pageout" objects have a shadow, but
5711 * maintain a "paging reference" rather
5712 * than a normal reference.
5713 * (We are careful here to limit recursion.)
5715 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
5717 if ((vm_object_terminate(object
) == KERN_SUCCESS
)
5718 && (shadow
!= VM_OBJECT_NULL
)) {
5719 vm_object_deallocate(shadow
);
5722 if(object_released
++ == *count
)
5723 return KERN_SUCCESS
;
5727 vm_object_cache_unlock();
5728 *count
= object_released
;
5732 return KERN_SUCCESS
;
5738 memory_object_create_named(
5739 memory_object_t pager
,
5740 memory_object_offset_t size
,
5741 memory_object_control_t
*control
)
5744 vm_object_hash_entry_t entry
;
5747 *control
= MEMORY_OBJECT_CONTROL_NULL
;
5748 if (pager
== MEMORY_OBJECT_NULL
)
5749 return KERN_INVALID_ARGUMENT
;
5751 lck
= vm_object_hash_lock_spin(pager
);
5752 entry
= vm_object_hash_lookup(pager
, FALSE
);
5754 if ((entry
!= VM_OBJECT_HASH_ENTRY_NULL
) &&
5755 (entry
->object
!= VM_OBJECT_NULL
)) {
5756 if (entry
->object
->named
== TRUE
)
5757 panic("memory_object_create_named: caller already holds the right"); }
5758 vm_object_hash_unlock(lck
);
5760 if ((object
= vm_object_enter(pager
, size
, FALSE
, FALSE
, TRUE
)) == VM_OBJECT_NULL
) {
5761 return(KERN_INVALID_OBJECT
);
5764 /* wait for object (if any) to be ready */
5765 if (object
!= VM_OBJECT_NULL
) {
5766 vm_object_lock(object
);
5767 object
->named
= TRUE
;
5768 while (!object
->pager_ready
) {
5769 vm_object_sleep(object
,
5770 VM_OBJECT_EVENT_PAGER_READY
,
5773 *control
= object
->pager_control
;
5774 vm_object_unlock(object
);
5776 return (KERN_SUCCESS
);
5781 * Routine: memory_object_recover_named [user interface]
5783 * Attempt to recover a named reference for a VM object.
5784 * VM will verify that the object has not already started
5785 * down the termination path, and if it has, will optionally
5786 * wait for that to finish.
5788 * KERN_SUCCESS - we recovered a named reference on the object
5789 * KERN_FAILURE - we could not recover a reference (object dead)
5790 * KERN_INVALID_ARGUMENT - bad memory object control
5793 memory_object_recover_named(
5794 memory_object_control_t control
,
5795 boolean_t wait_on_terminating
)
5799 object
= memory_object_control_to_vm_object(control
);
5800 if (object
== VM_OBJECT_NULL
) {
5801 return (KERN_INVALID_ARGUMENT
);
5804 vm_object_lock(object
);
5806 if (object
->terminating
&& wait_on_terminating
) {
5807 vm_object_wait(object
,
5808 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
5813 if (!object
->alive
) {
5814 vm_object_unlock(object
);
5815 return KERN_FAILURE
;
5818 if (object
->named
== TRUE
) {
5819 vm_object_unlock(object
);
5820 return KERN_SUCCESS
;
5823 if ((object
->ref_count
== 0) && (!object
->terminating
)) {
5824 if (!vm_object_cache_lock_try()) {
5825 vm_object_unlock(object
);
5828 queue_remove(&vm_object_cached_list
, object
,
5829 vm_object_t
, cached_list
);
5830 vm_object_cached_count
--;
5831 XPR(XPR_VM_OBJECT_CACHE
,
5832 "memory_object_recover_named: removing %X, head (%X, %X)\n",
5834 vm_object_cached_list
.next
,
5835 vm_object_cached_list
.prev
, 0,0);
5837 vm_object_cache_unlock();
5840 object
->named
= TRUE
;
5841 vm_object_lock_assert_exclusive(object
);
5842 object
->ref_count
++;
5843 vm_object_res_reference(object
);
5844 while (!object
->pager_ready
) {
5845 vm_object_sleep(object
,
5846 VM_OBJECT_EVENT_PAGER_READY
,
5849 vm_object_unlock(object
);
5850 return (KERN_SUCCESS
);
5855 * vm_object_release_name:
5857 * Enforces name semantic on memory_object reference count decrement
5858 * This routine should not be called unless the caller holds a name
5859 * reference gained through the memory_object_create_named.
5861 * If the TERMINATE_IDLE flag is set, the call will return if the
5862 * reference count is not 1. i.e. idle with the only remaining reference
5864 * If the decision is made to proceed the name field flag is set to
5865 * false and the reference count is decremented. If the RESPECT_CACHE
5866 * flag is set and the reference count has gone to zero, the
5867 * memory_object is checked to see if it is cacheable otherwise when
5868 * the reference count is zero, it is simply terminated.
5871 __private_extern__ kern_return_t
5872 vm_object_release_name(
5877 boolean_t original_object
= TRUE
;
5879 while (object
!= VM_OBJECT_NULL
) {
5881 vm_object_lock(object
);
5883 assert(object
->alive
);
5884 if (original_object
)
5885 assert(object
->named
);
5886 assert(object
->ref_count
> 0);
5889 * We have to wait for initialization before
5890 * destroying or caching the object.
5893 if (object
->pager_created
&& !object
->pager_initialized
) {
5894 assert(!object
->can_persist
);
5895 vm_object_assert_wait(object
,
5896 VM_OBJECT_EVENT_INITIALIZED
,
5898 vm_object_unlock(object
);
5899 thread_block(THREAD_CONTINUE_NULL
);
5903 if (((object
->ref_count
> 1)
5904 && (flags
& MEMORY_OBJECT_TERMINATE_IDLE
))
5905 || (object
->terminating
)) {
5906 vm_object_unlock(object
);
5907 return KERN_FAILURE
;
5909 if (flags
& MEMORY_OBJECT_RELEASE_NO_OP
) {
5910 vm_object_unlock(object
);
5911 return KERN_SUCCESS
;
5915 if ((flags
& MEMORY_OBJECT_RESPECT_CACHE
) &&
5916 (object
->ref_count
== 1)) {
5917 if (original_object
)
5918 object
->named
= FALSE
;
5919 vm_object_unlock(object
);
5920 /* let vm_object_deallocate push this thing into */
5921 /* the cache, if that it is where it is bound */
5922 vm_object_deallocate(object
);
5923 return KERN_SUCCESS
;
5925 VM_OBJ_RES_DECR(object
);
5926 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
5928 if (object
->ref_count
== 1) {
5929 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
5930 if (original_object
) {
5931 return KERN_FAILURE
;
5933 return KERN_SUCCESS
;
5936 if (shadow
!= VM_OBJECT_NULL
) {
5937 original_object
= FALSE
;
5941 return KERN_SUCCESS
;
5943 vm_object_lock_assert_exclusive(object
);
5944 object
->ref_count
--;
5945 assert(object
->ref_count
> 0);
5947 object
->named
= FALSE
;
5948 vm_object_unlock(object
);
5949 return KERN_SUCCESS
;
5954 return KERN_FAILURE
;
5958 __private_extern__ kern_return_t
5959 vm_object_lock_request(
5961 vm_object_offset_t offset
,
5962 vm_object_size_t size
,
5963 memory_object_return_t should_return
,
5967 __unused boolean_t should_flush
;
5969 should_flush
= flags
& MEMORY_OBJECT_DATA_FLUSH
;
5971 XPR(XPR_MEMORY_OBJECT
,
5972 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
5973 object
, offset
, size
,
5974 (((should_return
&1)<<1)|should_flush
), prot
);
5977 * Check for bogus arguments.
5979 if (object
== VM_OBJECT_NULL
)
5980 return (KERN_INVALID_ARGUMENT
);
5982 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
)
5983 return (KERN_INVALID_ARGUMENT
);
5985 size
= round_page_64(size
);
5988 * Lock the object, and acquire a paging reference to
5989 * prevent the memory_object reference from being released.
5991 vm_object_lock(object
);
5992 vm_object_paging_begin(object
);
5994 (void)vm_object_update(object
,
5995 offset
, size
, NULL
, NULL
, should_return
, flags
, prot
);
5997 vm_object_paging_end(object
);
5998 vm_object_unlock(object
);
6000 return (KERN_SUCCESS
);
6004 * Empty a purgeable object by grabbing the physical pages assigned to it and
6005 * putting them on the free queue without writing them to backing store, etc.
6006 * When the pages are next touched they will be demand zero-fill pages. We
6007 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
6008 * skip referenced/dirty pages, pages on the active queue, etc. We're more
6009 * than happy to grab these since this is a purgeable object. We mark the
6010 * object as "empty" after reaping its pages.
6012 * On entry the object must be locked and it must be
6013 * purgeable with no delayed copies pending.
6016 vm_object_purge(vm_object_t object
)
6018 vm_object_lock_assert_exclusive(object
);
6020 if (object
->purgable
== VM_PURGABLE_DENY
)
6023 assert(object
->copy
== VM_OBJECT_NULL
);
6024 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
6026 if(object
->purgable
== VM_PURGABLE_VOLATILE
) {
6028 assert(object
->resident_page_count
>=
6029 object
->wired_page_count
);
6030 delta
= (object
->resident_page_count
-
6031 object
->wired_page_count
);
6033 assert(vm_page_purgeable_count
>=
6036 (SInt32
*)&vm_page_purgeable_count
);
6038 if (object
->wired_page_count
!= 0) {
6039 assert(vm_page_purgeable_wired_count
>=
6040 object
->wired_page_count
);
6041 OSAddAtomic(-object
->wired_page_count
,
6042 (SInt32
*)&vm_page_purgeable_wired_count
);
6045 object
->purgable
= VM_PURGABLE_EMPTY
;
6047 vm_object_reap_pages(object
, REAP_PURGEABLE
);
6052 * vm_object_purgeable_control() allows the caller to control and investigate the
6053 * state of a purgeable object. A purgeable object is created via a call to
6054 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgeable object will
6055 * never be coalesced with any other object -- even other purgeable objects --
6056 * and will thus always remain a distinct object. A purgeable object has
6057 * special semantics when its reference count is exactly 1. If its reference
6058 * count is greater than 1, then a purgeable object will behave like a normal
6059 * object and attempts to use this interface will result in an error return
6060 * of KERN_INVALID_ARGUMENT.
6062 * A purgeable object may be put into a "volatile" state which will make the
6063 * object's pages elligable for being reclaimed without paging to backing
6064 * store if the system runs low on memory. If the pages in a volatile
6065 * purgeable object are reclaimed, the purgeable object is said to have been
6066 * "emptied." When a purgeable object is emptied the system will reclaim as
6067 * many pages from the object as it can in a convenient manner (pages already
6068 * en route to backing store or busy for other reasons are left as is). When
6069 * a purgeable object is made volatile, its pages will generally be reclaimed
6070 * before other pages in the application's working set. This semantic is
6071 * generally used by applications which can recreate the data in the object
6072 * faster than it can be paged in. One such example might be media assets
6073 * which can be reread from a much faster RAID volume.
6075 * A purgeable object may be designated as "non-volatile" which means it will
6076 * behave like all other objects in the system with pages being written to and
6077 * read from backing store as needed to satisfy system memory needs. If the
6078 * object was emptied before the object was made non-volatile, that fact will
6079 * be returned as the old state of the purgeable object (see
6080 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
6081 * were reclaimed as part of emptying the object will be refaulted in as
6082 * zero-fill on demand. It is up to the application to note that an object
6083 * was emptied and recreate the objects contents if necessary. When a
6084 * purgeable object is made non-volatile, its pages will generally not be paged
6085 * out to backing store in the immediate future. A purgeable object may also
6086 * be manually emptied.
6088 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
6089 * volatile purgeable object may be queried at any time. This information may
6090 * be used as a control input to let the application know when the system is
6091 * experiencing memory pressure and is reclaiming memory.
6093 * The specified address may be any address within the purgeable object. If
6094 * the specified address does not represent any object in the target task's
6095 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
6096 * object containing the specified address is not a purgeable object, then
6097 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
6100 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
6101 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
6102 * state is used to set the new state of the purgeable object and return its
6103 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgeable
6104 * object is returned in the parameter state.
6106 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
6107 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
6108 * the non-volatile, volatile and volatile/empty states described above.
6109 * Setting the state of a purgeable object to VM_PURGABLE_EMPTY will
6110 * immediately reclaim as many pages in the object as can be conveniently
6111 * collected (some may have already been written to backing store or be
6114 * The process of making a purgeable object non-volatile and determining its
6115 * previous state is atomic. Thus, if a purgeable object is made
6116 * VM_PURGABLE_NONVOLATILE and the old state is returned as
6117 * VM_PURGABLE_VOLATILE, then the purgeable object's previous contents are
6118 * completely intact and will remain so until the object is made volatile
6119 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
6120 * was reclaimed while it was in a volatile state and its previous contents
6124 * The object must be locked.
6127 vm_object_purgable_control(
6129 vm_purgable_t control
,
6135 if (object
== VM_OBJECT_NULL
) {
6137 * Object must already be present or it can't be purgeable.
6139 return KERN_INVALID_ARGUMENT
;
6143 * Get current state of the purgeable object.
6145 old_state
= object
->purgable
;
6146 if (old_state
== VM_PURGABLE_DENY
)
6147 return KERN_INVALID_ARGUMENT
;
6149 /* purgeable cant have delayed copies - now or in the future */
6150 assert(object
->copy
== VM_OBJECT_NULL
);
6151 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
6154 * Execute the desired operation.
6156 if (control
== VM_PURGABLE_GET_STATE
) {
6158 return KERN_SUCCESS
;
6161 if ((*state
) & VM_PURGABLE_DEBUG_EMPTY
) {
6162 object
->volatile_empty
= TRUE
;
6164 if ((*state
) & VM_PURGABLE_DEBUG_FAULT
) {
6165 object
->volatile_fault
= TRUE
;
6168 new_state
= *state
& VM_PURGABLE_STATE_MASK
;
6169 if (new_state
== VM_PURGABLE_VOLATILE
&&
6170 object
->volatile_empty
) {
6171 new_state
= VM_PURGABLE_EMPTY
;
6174 switch (new_state
) {
6175 case VM_PURGABLE_DENY
:
6176 case VM_PURGABLE_NONVOLATILE
:
6177 object
->purgable
= new_state
;
6179 if (old_state
== VM_PURGABLE_VOLATILE
) {
6182 assert(object
->resident_page_count
>=
6183 object
->wired_page_count
);
6184 delta
= (object
->resident_page_count
-
6185 object
->wired_page_count
);
6187 assert(vm_page_purgeable_count
>= delta
);
6191 (SInt32
*)&vm_page_purgeable_count
);
6193 if (object
->wired_page_count
!= 0) {
6194 assert(vm_page_purgeable_wired_count
>=
6195 object
->wired_page_count
);
6196 OSAddAtomic(-object
->wired_page_count
,
6197 (SInt32
*)&vm_page_purgeable_wired_count
);
6200 vm_page_lock_queues();
6202 assert(object
->objq
.next
!= NULL
&& object
->objq
.prev
!= NULL
); /* object should be on a queue */
6203 purgeable_q_t queue
= vm_purgeable_object_remove(object
);
6206 vm_purgeable_token_delete_first(queue
);
6207 assert(queue
->debug_count_objects
>=0);
6209 vm_page_unlock_queues();
6213 case VM_PURGABLE_VOLATILE
:
6214 if (object
->volatile_fault
) {
6218 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
6224 refmod
= pmap_disconnect(p
->phys_page
);
6225 if ((refmod
& VM_MEM_MODIFIED
) &&
6232 if (old_state
== VM_PURGABLE_EMPTY
&&
6233 object
->resident_page_count
== 0)
6236 purgeable_q_t queue
;
6238 /* find the correct queue */
6239 if ((*state
&VM_PURGABLE_ORDERING_MASK
) == VM_PURGABLE_ORDERING_OBSOLETE
)
6240 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_OBSOLETE
];
6242 if ((*state
&VM_PURGABLE_BEHAVIOR_MASK
) == VM_PURGABLE_BEHAVIOR_FIFO
)
6243 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_FIFO
];
6245 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_LIFO
];
6248 if (old_state
== VM_PURGABLE_NONVOLATILE
||
6249 old_state
== VM_PURGABLE_EMPTY
) {
6252 /* try to add token... this can fail */
6253 vm_page_lock_queues();
6255 kern_return_t result
= vm_purgeable_token_add(queue
);
6256 if (result
!= KERN_SUCCESS
) {
6257 vm_page_unlock_queues();
6260 vm_page_unlock_queues();
6262 assert(object
->resident_page_count
>=
6263 object
->wired_page_count
);
6264 delta
= (object
->resident_page_count
-
6265 object
->wired_page_count
);
6269 &vm_page_purgeable_count
);
6271 if (object
->wired_page_count
!= 0) {
6272 OSAddAtomic(object
->wired_page_count
,
6273 &vm_page_purgeable_wired_count
);
6276 object
->purgable
= new_state
;
6278 /* object should not be on a queue */
6279 assert(object
->objq
.next
== NULL
&& object
->objq
.prev
== NULL
);
6281 else if (old_state
== VM_PURGABLE_VOLATILE
) {
6283 * if reassigning priorities / purgeable groups, we don't change the
6284 * token queue. So moving priorities will not make pages stay around longer.
6285 * Reasoning is that the algorithm gives most priority to the most important
6286 * object. If a new token is added, the most important object' priority is boosted.
6287 * This biases the system already for purgeable queues that move a lot.
6288 * It doesn't seem more biasing is neccessary in this case, where no new object is added.
6290 assert(object
->objq
.next
!= NULL
&& object
->objq
.prev
!= NULL
); /* object should be on a queue */
6292 purgeable_q_t old_queue
=vm_purgeable_object_remove(object
);
6295 if (old_queue
!= queue
) {
6296 kern_return_t result
;
6298 /* Changing queue. Have to move token. */
6299 vm_page_lock_queues();
6300 vm_purgeable_token_delete_first(old_queue
);
6301 result
= vm_purgeable_token_add(queue
);
6302 vm_page_unlock_queues();
6304 assert(result
==KERN_SUCCESS
); /* this should never fail since we just freed a token */
6307 vm_purgeable_object_add(object
, queue
, (*state
&VM_VOLATILE_GROUP_MASK
)>>VM_VOLATILE_GROUP_SHIFT
);
6309 assert(queue
->debug_count_objects
>=0);
6314 case VM_PURGABLE_EMPTY
:
6315 if (object
->volatile_fault
) {
6319 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
6325 refmod
= pmap_disconnect(p
->phys_page
);
6326 if ((refmod
& VM_MEM_MODIFIED
) &&
6333 if (old_state
!= new_state
) {
6334 assert(old_state
== VM_PURGABLE_NONVOLATILE
||
6335 old_state
== VM_PURGABLE_VOLATILE
);
6336 if (old_state
== VM_PURGABLE_VOLATILE
) {
6337 purgeable_q_t old_queue
;
6339 /* object should be on a queue */
6340 assert(object
->objq
.next
!= NULL
&&
6341 object
->objq
.prev
!= NULL
);
6342 old_queue
= vm_purgeable_object_remove(object
);
6344 vm_page_lock_queues();
6345 vm_purgeable_token_delete_first(old_queue
);
6346 vm_page_unlock_queues();
6348 (void) vm_object_purge(object
);
6355 return KERN_SUCCESS
;
6360 * vm_object_res_deallocate
6362 * (recursively) decrement residence counts on vm objects and their shadows.
6363 * Called from vm_object_deallocate and when swapping out an object.
6365 * The object is locked, and remains locked throughout the function,
6366 * even as we iterate down the shadow chain. Locks on intermediate objects
6367 * will be dropped, but not the original object.
6369 * NOTE: this function used to use recursion, rather than iteration.
6372 __private_extern__
void
6373 vm_object_res_deallocate(
6376 vm_object_t orig_object
= object
;
6378 * Object is locked so it can be called directly
6379 * from vm_object_deallocate. Original object is never
6382 assert(object
->res_count
> 0);
6383 while (--object
->res_count
== 0) {
6384 assert(object
->ref_count
>= object
->res_count
);
6385 vm_object_deactivate_all_pages(object
);
6386 /* iterate on shadow, if present */
6387 if (object
->shadow
!= VM_OBJECT_NULL
) {
6388 vm_object_t tmp_object
= object
->shadow
;
6389 vm_object_lock(tmp_object
);
6390 if (object
!= orig_object
)
6391 vm_object_unlock(object
);
6392 object
= tmp_object
;
6393 assert(object
->res_count
> 0);
6397 if (object
!= orig_object
)
6398 vm_object_unlock(object
);
6402 * vm_object_res_reference
6404 * Internal function to increment residence count on a vm object
6405 * and its shadows. It is called only from vm_object_reference, and
6406 * when swapping in a vm object, via vm_map_swap.
6408 * The object is locked, and remains locked throughout the function,
6409 * even as we iterate down the shadow chain. Locks on intermediate objects
6410 * will be dropped, but not the original object.
6412 * NOTE: this function used to use recursion, rather than iteration.
6415 __private_extern__
void
6416 vm_object_res_reference(
6419 vm_object_t orig_object
= object
;
6421 * Object is locked, so this can be called directly
6422 * from vm_object_reference. This lock is never released.
6424 while ((++object
->res_count
== 1) &&
6425 (object
->shadow
!= VM_OBJECT_NULL
)) {
6426 vm_object_t tmp_object
= object
->shadow
;
6428 assert(object
->ref_count
>= object
->res_count
);
6429 vm_object_lock(tmp_object
);
6430 if (object
!= orig_object
)
6431 vm_object_unlock(object
);
6432 object
= tmp_object
;
6434 if (object
!= orig_object
)
6435 vm_object_unlock(object
);
6436 assert(orig_object
->ref_count
>= orig_object
->res_count
);
6438 #endif /* TASK_SWAPPER */
6441 * vm_object_reference:
6443 * Gets another reference to the given object.
6445 #ifdef vm_object_reference
6446 #undef vm_object_reference
6448 __private_extern__
void
6449 vm_object_reference(
6450 register vm_object_t object
)
6452 if (object
== VM_OBJECT_NULL
)
6455 vm_object_lock(object
);
6456 assert(object
->ref_count
> 0);
6457 vm_object_reference_locked(object
);
6458 vm_object_unlock(object
);
6463 * Scale the vm_object_cache
6464 * This is required to make sure that the vm_object_cache is big
6465 * enough to effectively cache the mapped file.
6466 * This is really important with UBC as all the regular file vnodes
6467 * have memory object associated with them. Havving this cache too
6468 * small results in rapid reclaim of vnodes and hurts performance a LOT!
6470 * This is also needed as number of vnodes can be dynamically scaled.
6473 adjust_vm_object_cache(
6474 __unused vm_size_t oval
,
6475 __unused vm_size_t nval
)
6478 vm_object_cached_max
= nval
;
6479 vm_object_cache_trim(FALSE
);
6481 return (KERN_SUCCESS
);
6483 #endif /* MACH_BSD */
6487 * vm_object_transpose
6489 * This routine takes two VM objects of the same size and exchanges
6490 * their backing store.
6491 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
6492 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
6494 * The VM objects must not be locked by caller.
6496 unsigned int vm_object_transpose_count
= 0;
6498 vm_object_transpose(
6499 vm_object_t object1
,
6500 vm_object_t object2
,
6501 vm_object_size_t transpose_size
)
6503 vm_object_t tmp_object
;
6504 kern_return_t retval
;
6505 boolean_t object1_locked
, object2_locked
;
6507 vm_object_offset_t page_offset
;
6508 lck_mtx_t
*hash_lck
;
6509 vm_object_hash_entry_t hash_entry
;
6511 tmp_object
= VM_OBJECT_NULL
;
6512 object1_locked
= FALSE
; object2_locked
= FALSE
;
6514 if (object1
== object2
||
6515 object1
== VM_OBJECT_NULL
||
6516 object2
== VM_OBJECT_NULL
) {
6518 * If the 2 VM objects are the same, there's
6519 * no point in exchanging their backing store.
6521 retval
= KERN_INVALID_VALUE
;
6526 * Since we need to lock both objects at the same time,
6527 * make sure we always lock them in the same order to
6530 if (object1
> object2
) {
6531 tmp_object
= object1
;
6533 object2
= tmp_object
;
6537 * Allocate a temporary VM object to hold object1's contents
6538 * while we copy object2 to object1.
6540 tmp_object
= vm_object_allocate(transpose_size
);
6541 vm_object_lock(tmp_object
);
6542 tmp_object
->can_persist
= FALSE
;
6546 * Grab control of the 1st VM object.
6548 vm_object_lock(object1
);
6549 object1_locked
= TRUE
;
6550 if (!object1
->alive
|| object1
->terminating
||
6551 object1
->copy
|| object1
->shadow
|| object1
->shadowed
||
6552 object1
->purgable
!= VM_PURGABLE_DENY
) {
6554 * We don't deal with copy or shadow objects (yet).
6556 retval
= KERN_INVALID_VALUE
;
6560 * We're about to mess with the object's backing store and
6561 * taking a "paging_in_progress" reference wouldn't be enough
6562 * to prevent any paging activity on this object, so the caller should
6563 * have "quiesced" the objects beforehand, via a UPL operation with
6564 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
6565 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
6567 * Wait for any paging operation to complete (but only paging, not
6568 * other kind of activities not linked to the pager). After we're
6569 * statisfied that there's no more paging in progress, we keep the
6570 * object locked, to guarantee that no one tries to access its pager.
6572 vm_object_paging_only_wait(object1
, THREAD_UNINT
);
6575 * Same as above for the 2nd object...
6577 vm_object_lock(object2
);
6578 object2_locked
= TRUE
;
6579 if (! object2
->alive
|| object2
->terminating
||
6580 object2
->copy
|| object2
->shadow
|| object2
->shadowed
||
6581 object2
->purgable
!= VM_PURGABLE_DENY
) {
6582 retval
= KERN_INVALID_VALUE
;
6585 vm_object_paging_only_wait(object2
, THREAD_UNINT
);
6588 if (object1
->size
!= object2
->size
||
6589 object1
->size
!= transpose_size
) {
6591 * If the 2 objects don't have the same size, we can't
6592 * exchange their backing stores or one would overflow.
6593 * If their size doesn't match the caller's
6594 * "transpose_size", we can't do it either because the
6595 * transpose operation will affect the entire span of
6598 retval
= KERN_INVALID_VALUE
;
6604 * Transpose the lists of resident pages.
6605 * This also updates the resident_page_count and the memq_hint.
6607 if (object1
->phys_contiguous
|| queue_empty(&object1
->memq
)) {
6609 * No pages in object1, just transfer pages
6610 * from object2 to object1. No need to go through
6611 * an intermediate object.
6613 while (!queue_empty(&object2
->memq
)) {
6614 page
= (vm_page_t
) queue_first(&object2
->memq
);
6615 vm_page_rename(page
, object1
, page
->offset
, FALSE
);
6617 assert(queue_empty(&object2
->memq
));
6618 } else if (object2
->phys_contiguous
|| queue_empty(&object2
->memq
)) {
6620 * No pages in object2, just transfer pages
6621 * from object1 to object2. No need to go through
6622 * an intermediate object.
6624 while (!queue_empty(&object1
->memq
)) {
6625 page
= (vm_page_t
) queue_first(&object1
->memq
);
6626 vm_page_rename(page
, object2
, page
->offset
, FALSE
);
6628 assert(queue_empty(&object1
->memq
));
6630 /* transfer object1's pages to tmp_object */
6631 while (!queue_empty(&object1
->memq
)) {
6632 page
= (vm_page_t
) queue_first(&object1
->memq
);
6633 page_offset
= page
->offset
;
6634 vm_page_remove(page
, TRUE
);
6635 page
->offset
= page_offset
;
6636 queue_enter(&tmp_object
->memq
, page
, vm_page_t
, listq
);
6638 assert(queue_empty(&object1
->memq
));
6639 /* transfer object2's pages to object1 */
6640 while (!queue_empty(&object2
->memq
)) {
6641 page
= (vm_page_t
) queue_first(&object2
->memq
);
6642 vm_page_rename(page
, object1
, page
->offset
, FALSE
);
6644 assert(queue_empty(&object2
->memq
));
6645 /* transfer tmp_object's pages to object1 */
6646 while (!queue_empty(&tmp_object
->memq
)) {
6647 page
= (vm_page_t
) queue_first(&tmp_object
->memq
);
6648 queue_remove(&tmp_object
->memq
, page
,
6650 vm_page_insert(page
, object2
, page
->offset
);
6652 assert(queue_empty(&tmp_object
->memq
));
6655 #define __TRANSPOSE_FIELD(field) \
6657 tmp_object->field = object1->field; \
6658 object1->field = object2->field; \
6659 object2->field = tmp_object->field; \
6662 /* "Lock" refers to the object not its contents */
6663 /* "size" should be identical */
6664 assert(object1
->size
== object2
->size
);
6665 /* "memq_hint" was updated above when transposing pages */
6666 /* "ref_count" refers to the object not its contents */
6668 /* "res_count" refers to the object not its contents */
6670 /* "resident_page_count" was updated above when transposing pages */
6671 /* "wired_page_count" was updated above when transposing pages */
6672 /* "reusable_page_count" was updated above when transposing pages */
6673 /* there should be no "copy" */
6674 assert(!object1
->copy
);
6675 assert(!object2
->copy
);
6676 /* there should be no "shadow" */
6677 assert(!object1
->shadow
);
6678 assert(!object2
->shadow
);
6679 __TRANSPOSE_FIELD(shadow_offset
); /* used by phys_contiguous objects */
6680 __TRANSPOSE_FIELD(pager
);
6681 __TRANSPOSE_FIELD(paging_offset
);
6682 __TRANSPOSE_FIELD(pager_control
);
6683 /* update the memory_objects' pointers back to the VM objects */
6684 if (object1
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6685 memory_object_control_collapse(object1
->pager_control
,
6688 if (object2
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6689 memory_object_control_collapse(object2
->pager_control
,
6692 __TRANSPOSE_FIELD(copy_strategy
);
6693 /* "paging_in_progress" refers to the object not its contents */
6694 assert(!object1
->paging_in_progress
);
6695 assert(!object2
->paging_in_progress
);
6696 assert(object1
->activity_in_progress
);
6697 assert(object2
->activity_in_progress
);
6698 /* "all_wanted" refers to the object not its contents */
6699 __TRANSPOSE_FIELD(pager_created
);
6700 __TRANSPOSE_FIELD(pager_initialized
);
6701 __TRANSPOSE_FIELD(pager_ready
);
6702 __TRANSPOSE_FIELD(pager_trusted
);
6703 __TRANSPOSE_FIELD(can_persist
);
6704 __TRANSPOSE_FIELD(internal
);
6705 __TRANSPOSE_FIELD(temporary
);
6706 __TRANSPOSE_FIELD(private);
6707 __TRANSPOSE_FIELD(pageout
);
6708 /* "alive" should be set */
6709 assert(object1
->alive
);
6710 assert(object2
->alive
);
6711 /* "purgeable" should be non-purgeable */
6712 assert(object1
->purgable
== VM_PURGABLE_DENY
);
6713 assert(object2
->purgable
== VM_PURGABLE_DENY
);
6714 /* "shadowed" refers to the the object not its contents */
6715 __TRANSPOSE_FIELD(silent_overwrite
);
6716 __TRANSPOSE_FIELD(advisory_pageout
);
6717 __TRANSPOSE_FIELD(true_share
);
6718 /* "terminating" should not be set */
6719 assert(!object1
->terminating
);
6720 assert(!object2
->terminating
);
6721 __TRANSPOSE_FIELD(named
);
6722 /* "shadow_severed" refers to the object not its contents */
6723 __TRANSPOSE_FIELD(phys_contiguous
);
6724 __TRANSPOSE_FIELD(nophyscache
);
6725 /* "cached_list.next" points to transposed object */
6726 object1
->cached_list
.next
= (queue_entry_t
) object2
;
6727 object2
->cached_list
.next
= (queue_entry_t
) object1
;
6728 /* "cached_list.prev" should be NULL */
6729 assert(object1
->cached_list
.prev
== NULL
);
6730 assert(object2
->cached_list
.prev
== NULL
);
6731 /* "msr_q" is linked to the object not its contents */
6732 assert(queue_empty(&object1
->msr_q
));
6733 assert(queue_empty(&object2
->msr_q
));
6734 __TRANSPOSE_FIELD(last_alloc
);
6735 __TRANSPOSE_FIELD(sequential
);
6736 __TRANSPOSE_FIELD(pages_created
);
6737 __TRANSPOSE_FIELD(pages_used
);
6739 __TRANSPOSE_FIELD(existence_map
);
6741 __TRANSPOSE_FIELD(cow_hint
);
6743 __TRANSPOSE_FIELD(paging_object
);
6745 __TRANSPOSE_FIELD(wimg_bits
);
6746 __TRANSPOSE_FIELD(code_signed
);
6747 if (object1
->hashed
) {
6748 hash_lck
= vm_object_hash_lock_spin(object2
->pager
);
6749 hash_entry
= vm_object_hash_lookup(object2
->pager
, FALSE
);
6750 assert(hash_entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
6751 hash_entry
->object
= object2
;
6752 vm_object_hash_unlock(hash_lck
);
6754 if (object2
->hashed
) {
6755 hash_lck
= vm_object_hash_lock_spin(object1
->pager
);
6756 hash_entry
= vm_object_hash_lookup(object1
->pager
, FALSE
);
6757 assert(hash_entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
6758 hash_entry
->object
= object1
;
6759 vm_object_hash_unlock(hash_lck
);
6761 __TRANSPOSE_FIELD(hashed
);
6762 object1
->transposed
= TRUE
;
6763 object2
->transposed
= TRUE
;
6764 __TRANSPOSE_FIELD(mapping_in_progress
);
6765 __TRANSPOSE_FIELD(volatile_empty
);
6766 __TRANSPOSE_FIELD(volatile_fault
);
6767 __TRANSPOSE_FIELD(all_reusable
);
6768 assert(object1
->blocked_access
);
6769 assert(object2
->blocked_access
);
6770 assert(object1
->__object2_unused_bits
== 0);
6771 assert(object2
->__object2_unused_bits
== 0);
6773 /* "uplq" refers to the object not its contents (see upl_transpose()) */
6775 assert(object1
->objq
.next
== NULL
);
6776 assert(object1
->objq
.prev
== NULL
);
6777 assert(object2
->objq
.next
== NULL
);
6778 assert(object2
->objq
.prev
== NULL
);
6780 #undef __TRANSPOSE_FIELD
6782 retval
= KERN_SUCCESS
;
6788 if (tmp_object
!= VM_OBJECT_NULL
) {
6789 vm_object_unlock(tmp_object
);
6791 * Re-initialize the temporary object to avoid
6792 * deallocating a real pager.
6794 _vm_object_allocate(transpose_size
, tmp_object
);
6795 vm_object_deallocate(tmp_object
);
6796 tmp_object
= VM_OBJECT_NULL
;
6799 if (object1_locked
) {
6800 vm_object_unlock(object1
);
6801 object1_locked
= FALSE
;
6803 if (object2_locked
) {
6804 vm_object_unlock(object2
);
6805 object2_locked
= FALSE
;
6808 vm_object_transpose_count
++;
6815 * vm_object_cluster_size
6817 * Determine how big a cluster we should issue an I/O for...
6819 * Inputs: *start == offset of page needed
6820 * *length == maximum cluster pager can handle
6821 * Outputs: *start == beginning offset of cluster
6822 * *length == length of cluster to try
6824 * The original *start will be encompassed by the cluster
6827 extern int speculative_reads_disabled
;
6829 unsigned int preheat_pages_max
= MAX_UPL_TRANSFER
;
6830 unsigned int preheat_pages_min
= 8;
6831 unsigned int preheat_pages_mult
= 4;
6833 unsigned int preheat_pages_max
= MAX_UPL_TRANSFER
;
6834 unsigned int preheat_pages_min
= 8;
6835 unsigned int preheat_pages_mult
= 4;
6838 uint32_t pre_heat_scaling
[MAX_UPL_TRANSFER
+ 1];
6839 uint32_t pre_heat_cluster
[MAX_UPL_TRANSFER
+ 1];
6842 __private_extern__
void
6843 vm_object_cluster_size(vm_object_t object
, vm_object_offset_t
*start
,
6844 vm_size_t
*length
, vm_object_fault_info_t fault_info
, uint32_t *io_streaming
)
6846 vm_size_t pre_heat_size
;
6847 vm_size_t tail_size
;
6848 vm_size_t head_size
;
6849 vm_size_t max_length
;
6850 vm_size_t cluster_size
;
6851 vm_object_offset_t object_size
;
6852 vm_object_offset_t orig_start
;
6853 vm_object_offset_t target_start
;
6854 vm_object_offset_t offset
;
6855 vm_behavior_t behavior
;
6856 boolean_t look_behind
= TRUE
;
6857 boolean_t look_ahead
= TRUE
;
6858 uint32_t throttle_limit
;
6860 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6861 unsigned int max_ph_size
;
6862 unsigned int min_ph_size
;
6863 unsigned int ph_mult
;
6865 assert( !(*length
& PAGE_MASK
));
6866 assert( !(*start
& PAGE_MASK_64
));
6868 if ( (ph_mult
= preheat_pages_mult
) < 1 )
6870 if ( (min_ph_size
= preheat_pages_min
) < 1 )
6872 if ( (max_ph_size
= preheat_pages_max
) > MAX_UPL_TRANSFER
)
6873 max_ph_size
= MAX_UPL_TRANSFER
;
6875 if ( (max_length
= *length
) > (max_ph_size
* PAGE_SIZE
) )
6876 max_length
= (max_ph_size
* PAGE_SIZE
);
6879 * we'll always return a cluster size of at least
6880 * 1 page, since the original fault must always
6883 *length
= PAGE_SIZE
;
6886 if (speculative_reads_disabled
|| fault_info
== NULL
|| max_length
== 0) {
6888 * no cluster... just fault the page in
6892 orig_start
= *start
;
6893 target_start
= orig_start
;
6894 cluster_size
= round_page(fault_info
->cluster_size
);
6895 behavior
= fault_info
->behavior
;
6897 vm_object_lock(object
);
6899 if (object
->internal
)
6900 object_size
= object
->size
;
6901 else if (object
->pager
!= MEMORY_OBJECT_NULL
)
6902 vnode_pager_get_object_size(object
->pager
, &object_size
);
6904 goto out
; /* pager is gone for this object, nothing more to do */
6906 object_size
= round_page_64(object_size
);
6908 if (orig_start
>= object_size
) {
6910 * fault occurred beyond the EOF...
6911 * we need to punt w/o changing the
6916 if (object
->pages_used
> object
->pages_created
) {
6918 * must have wrapped our 32 bit counters
6921 object
->pages_used
= object
->pages_created
= 0;
6923 if ((sequential_run
= object
->sequential
)) {
6924 if (sequential_run
< 0) {
6925 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
6926 sequential_run
= 0 - sequential_run
;
6928 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6935 behavior
= VM_BEHAVIOR_DEFAULT
;
6937 case VM_BEHAVIOR_DEFAULT
:
6938 if (object
->internal
&& fault_info
->user_tag
== VM_MEMORY_STACK
)
6941 if (sequential_run
>= (3 * PAGE_SIZE
)) {
6942 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6944 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
)
6945 look_behind
= FALSE
;
6952 if (object
->pages_created
< 32 * ph_mult
) {
6956 pre_heat_size
= PAGE_SIZE
* 8 * ph_mult
;
6960 * Linear growth in PH size: The maximum size is max_length...
6961 * this cacluation will result in a size that is neither a
6962 * power of 2 nor a multiple of PAGE_SIZE... so round
6963 * it up to the nearest PAGE_SIZE boundary
6965 pre_heat_size
= (ph_mult
* (max_length
* object
->pages_used
) / object
->pages_created
);
6967 if (pre_heat_size
< PAGE_SIZE
* min_ph_size
)
6968 pre_heat_size
= PAGE_SIZE
* min_ph_size
;
6970 pre_heat_size
= round_page(pre_heat_size
);
6974 case VM_BEHAVIOR_RANDOM
:
6975 if ((pre_heat_size
= cluster_size
) <= PAGE_SIZE
)
6979 case VM_BEHAVIOR_SEQUENTIAL
:
6980 if ((pre_heat_size
= cluster_size
) == 0)
6981 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6982 look_behind
= FALSE
;
6987 case VM_BEHAVIOR_RSEQNTL
:
6988 if ((pre_heat_size
= cluster_size
) == 0)
6989 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6996 throttle_limit
= (uint32_t) max_length
;
6997 assert(throttle_limit
== max_length
);
6999 if (vnode_pager_check_hard_throttle(object
->pager
, &throttle_limit
, *io_streaming
) == KERN_SUCCESS
) {
7000 if (max_length
> throttle_limit
)
7001 max_length
= throttle_limit
;
7003 if (pre_heat_size
> max_length
)
7004 pre_heat_size
= max_length
;
7006 if (behavior
== VM_BEHAVIOR_DEFAULT
) {
7007 if (vm_page_free_count
< vm_page_throttle_limit
)
7008 pre_heat_size
= trunc_page(pre_heat_size
/ 8);
7009 else if (vm_page_free_count
< vm_page_free_target
)
7010 pre_heat_size
= trunc_page(pre_heat_size
/ 2);
7012 if (pre_heat_size
<= PAGE_SIZE
)
7015 if (look_ahead
== TRUE
) {
7016 if (look_behind
== TRUE
) {
7018 * if we get here its due to a random access...
7019 * so we want to center the original fault address
7020 * within the cluster we will issue... make sure
7021 * to calculate 'head_size' as a multiple of PAGE_SIZE...
7022 * 'pre_heat_size' is a multiple of PAGE_SIZE but not
7023 * necessarily an even number of pages so we need to truncate
7024 * the result to a PAGE_SIZE boundary
7026 head_size
= trunc_page(pre_heat_size
/ 2);
7028 if (target_start
> head_size
)
7029 target_start
-= head_size
;
7034 * 'target_start' at this point represents the beginning offset
7035 * of the cluster we are considering... 'orig_start' will be in
7036 * the center of this cluster if we didn't have to clip the start
7037 * due to running into the start of the file
7040 if ((target_start
+ pre_heat_size
) > object_size
)
7041 pre_heat_size
= (vm_size_t
)(round_page_64(object_size
- target_start
));
7043 * at this point caclulate the number of pages beyond the original fault
7044 * address that we want to consider... this is guaranteed not to extend beyond
7045 * the current EOF...
7047 assert((vm_size_t
)(orig_start
- target_start
) == (orig_start
- target_start
));
7048 tail_size
= pre_heat_size
- (vm_size_t
)(orig_start
- target_start
) - PAGE_SIZE
;
7050 if (pre_heat_size
> target_start
)
7051 pre_heat_size
= (vm_size_t
) target_start
; /* XXX: 32-bit vs 64-bit ? Joe ? */
7054 assert( !(target_start
& PAGE_MASK_64
));
7055 assert( !(pre_heat_size
& PAGE_MASK
));
7057 pre_heat_scaling
[pre_heat_size
/ PAGE_SIZE
]++;
7059 if (pre_heat_size
<= PAGE_SIZE
)
7062 if (look_behind
== TRUE
) {
7064 * take a look at the pages before the original
7065 * faulting offset... recalculate this in case
7066 * we had to clip 'pre_heat_size' above to keep
7067 * from running past the EOF.
7069 head_size
= pre_heat_size
- tail_size
- PAGE_SIZE
;
7071 for (offset
= orig_start
- PAGE_SIZE_64
; head_size
; offset
-= PAGE_SIZE_64
, head_size
-= PAGE_SIZE
) {
7073 * don't poke below the lowest offset
7075 if (offset
< fault_info
->lo_offset
)
7078 * for external objects and internal objects w/o an existence map
7079 * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN
7082 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
7084 * we know for a fact that the pager can't provide the page
7085 * so don't include it or any pages beyond it in this cluster
7090 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
7092 * don't bridge resident pages
7097 *length
+= PAGE_SIZE
;
7100 if (look_ahead
== TRUE
) {
7101 for (offset
= orig_start
+ PAGE_SIZE_64
; tail_size
; offset
+= PAGE_SIZE_64
, tail_size
-= PAGE_SIZE
) {
7103 * don't poke above the highest offset
7105 if (offset
>= fault_info
->hi_offset
)
7107 assert(offset
< object_size
);
7110 * for external objects and internal objects w/o an existence map
7111 * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN
7114 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
7116 * we know for a fact that the pager can't provide the page
7117 * so don't include it or any pages beyond it in this cluster
7122 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
7124 * don't bridge resident pages
7128 *length
+= PAGE_SIZE
;
7132 if (*length
> max_length
)
7133 *length
= max_length
;
7135 pre_heat_cluster
[*length
/ PAGE_SIZE
]++;
7137 vm_object_unlock(object
);
7142 * Allow manipulation of individual page state. This is actually part of
7143 * the UPL regimen but takes place on the VM object rather than on a UPL
7149 vm_object_offset_t offset
,
7151 ppnum_t
*phys_entry
,
7156 vm_object_lock(object
);
7158 if(ops
& UPL_POP_PHYSICAL
) {
7159 if(object
->phys_contiguous
) {
7161 *phys_entry
= (ppnum_t
)
7162 (object
->shadow_offset
>> PAGE_SHIFT
);
7164 vm_object_unlock(object
);
7165 return KERN_SUCCESS
;
7167 vm_object_unlock(object
);
7168 return KERN_INVALID_OBJECT
;
7171 if(object
->phys_contiguous
) {
7172 vm_object_unlock(object
);
7173 return KERN_INVALID_OBJECT
;
7177 if((dst_page
= vm_page_lookup(object
,offset
)) == VM_PAGE_NULL
) {
7178 vm_object_unlock(object
);
7179 return KERN_FAILURE
;
7182 /* Sync up on getting the busy bit */
7183 if((dst_page
->busy
|| dst_page
->cleaning
) &&
7184 (((ops
& UPL_POP_SET
) &&
7185 (ops
& UPL_POP_BUSY
)) || (ops
& UPL_POP_DUMP
))) {
7186 /* someone else is playing with the page, we will */
7188 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7192 if (ops
& UPL_POP_DUMP
) {
7193 if (dst_page
->pmapped
== TRUE
)
7194 pmap_disconnect(dst_page
->phys_page
);
7196 VM_PAGE_FREE(dst_page
);
7203 /* Get the condition of flags before requested ops */
7204 /* are undertaken */
7206 if(dst_page
->dirty
) *flags
|= UPL_POP_DIRTY
;
7207 if(dst_page
->pageout
) *flags
|= UPL_POP_PAGEOUT
;
7208 if(dst_page
->precious
) *flags
|= UPL_POP_PRECIOUS
;
7209 if(dst_page
->absent
) *flags
|= UPL_POP_ABSENT
;
7210 if(dst_page
->busy
) *flags
|= UPL_POP_BUSY
;
7213 /* The caller should have made a call either contingent with */
7214 /* or prior to this call to set UPL_POP_BUSY */
7215 if(ops
& UPL_POP_SET
) {
7216 /* The protection granted with this assert will */
7217 /* not be complete. If the caller violates the */
7218 /* convention and attempts to change page state */
7219 /* without first setting busy we may not see it */
7220 /* because the page may already be busy. However */
7221 /* if such violations occur we will assert sooner */
7223 assert(dst_page
->busy
|| (ops
& UPL_POP_BUSY
));
7224 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= TRUE
;
7225 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= TRUE
;
7226 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= TRUE
;
7227 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= TRUE
;
7228 if (ops
& UPL_POP_BUSY
) dst_page
->busy
= TRUE
;
7231 if(ops
& UPL_POP_CLR
) {
7232 assert(dst_page
->busy
);
7233 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= FALSE
;
7234 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= FALSE
;
7235 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= FALSE
;
7236 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= FALSE
;
7237 if (ops
& UPL_POP_BUSY
) {
7238 dst_page
->busy
= FALSE
;
7239 PAGE_WAKEUP(dst_page
);
7243 if (dst_page
->encrypted
) {
7246 * We need to decrypt this encrypted page before the
7247 * caller can access its contents.
7248 * But if the caller really wants to access the page's
7249 * contents, they have to keep the page "busy".
7250 * Otherwise, the page could get recycled or re-encrypted
7253 if ((ops
& UPL_POP_SET
) && (ops
& UPL_POP_BUSY
) &&
7256 * The page is stable enough to be accessed by
7257 * the caller, so make sure its contents are
7260 vm_page_decrypt(dst_page
, 0);
7263 * The page is not busy, so don't bother
7264 * decrypting it, since anything could
7265 * happen to it between now and when the
7266 * caller wants to access it.
7267 * We should not give the caller access
7270 assert(!phys_entry
);
7276 * The physical page number will remain valid
7277 * only if the page is kept busy.
7278 * ENCRYPTED SWAP: make sure we don't let the
7279 * caller access an encrypted page.
7281 assert(dst_page
->busy
);
7282 assert(!dst_page
->encrypted
);
7283 *phys_entry
= dst_page
->phys_page
;
7289 vm_object_unlock(object
);
7290 return KERN_SUCCESS
;
7295 * vm_object_range_op offers performance enhancement over
7296 * vm_object_page_op for page_op functions which do not require page
7297 * level state to be returned from the call. Page_op was created to provide
7298 * a low-cost alternative to page manipulation via UPLs when only a single
7299 * page was involved. The range_op call establishes the ability in the _op
7300 * family of functions to work on multiple pages where the lack of page level
7301 * state handling allows the caller to avoid the overhead of the upl structures.
7307 vm_object_offset_t offset_beg
,
7308 vm_object_offset_t offset_end
,
7312 vm_object_offset_t offset
;
7315 if (offset_end
- offset_beg
> (uint32_t) -1) {
7316 /* range is too big and would overflow "*range" */
7317 return KERN_INVALID_ARGUMENT
;
7319 if (object
->resident_page_count
== 0) {
7321 if (ops
& UPL_ROP_PRESENT
) {
7324 *range
= (uint32_t) (offset_end
- offset_beg
);
7325 assert(*range
== (offset_end
- offset_beg
));
7328 return KERN_SUCCESS
;
7330 vm_object_lock(object
);
7332 if (object
->phys_contiguous
) {
7333 vm_object_unlock(object
);
7334 return KERN_INVALID_OBJECT
;
7337 offset
= offset_beg
& ~PAGE_MASK_64
;
7339 while (offset
< offset_end
) {
7340 dst_page
= vm_page_lookup(object
, offset
);
7341 if (dst_page
!= VM_PAGE_NULL
) {
7342 if (ops
& UPL_ROP_DUMP
) {
7343 if (dst_page
->busy
|| dst_page
->cleaning
) {
7345 * someone else is playing with the
7346 * page, we will have to wait
7348 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7350 * need to relook the page up since it's
7351 * state may have changed while we slept
7352 * it might even belong to a different object
7357 if (dst_page
->pmapped
== TRUE
)
7358 pmap_disconnect(dst_page
->phys_page
);
7360 VM_PAGE_FREE(dst_page
);
7362 } else if ((ops
& UPL_ROP_ABSENT
) && !dst_page
->absent
)
7364 } else if (ops
& UPL_ROP_PRESENT
)
7367 offset
+= PAGE_SIZE
;
7369 vm_object_unlock(object
);
7372 if (offset
> offset_end
)
7373 offset
= offset_end
;
7374 if(offset
> offset_beg
) {
7375 *range
= (uint32_t) (offset
- offset_beg
);
7376 assert(*range
== (offset
- offset_beg
));
7381 return KERN_SUCCESS
;
7385 uint32_t scan_object_collision
= 0;
7388 vm_object_lock(vm_object_t object
)
7390 if (object
== vm_pageout_scan_wants_object
) {
7391 scan_object_collision
++;
7394 lck_rw_lock_exclusive(&object
->Lock
);
7398 vm_object_lock_avoid(vm_object_t object
)
7400 if (object
== vm_pageout_scan_wants_object
) {
7401 scan_object_collision
++;
7408 _vm_object_lock_try(vm_object_t object
)
7410 return (lck_rw_try_lock_exclusive(&object
->Lock
));
7414 vm_object_lock_try(vm_object_t object
)
7416 // called from hibernate path so check before blocking
7417 if (vm_object_lock_avoid(object
) && ml_get_interrupts_enabled()) {
7420 return _vm_object_lock_try(object
);
7423 vm_object_lock_shared(vm_object_t object
)
7425 if (vm_object_lock_avoid(object
)) {
7428 lck_rw_lock_shared(&object
->Lock
);
7432 vm_object_lock_try_shared(vm_object_t object
)
7434 if (vm_object_lock_avoid(object
)) {
7437 return (lck_rw_try_lock_shared(&object
->Lock
));