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>
77 #include <ipc/ipc_types.h>
78 #include <ipc/ipc_port.h>
80 #include <kern/kern_types.h>
81 #include <kern/assert.h>
82 #include <kern/lock.h>
83 #include <kern/queue.h>
85 #include <kern/kalloc.h>
86 #include <kern/zalloc.h>
87 #include <kern/host.h>
88 #include <kern/host_statistics.h>
89 #include <kern/processor.h>
90 #include <kern/misc_protos.h>
92 #include <vm/memory_object.h>
93 #include <vm/vm_fault.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_page.h>
97 #include <vm/vm_pageout.h>
98 #include <vm/vm_protos.h>
99 #include <vm/vm_purgeable_internal.h>
102 * Virtual memory objects maintain the actual data
103 * associated with allocated virtual memory. A given
104 * page of memory exists within exactly one object.
106 * An object is only deallocated when all "references"
109 * Associated with each object is a list of all resident
110 * memory pages belonging to that object; this list is
111 * maintained by the "vm_page" module, but locked by the object's
114 * Each object also records the memory object reference
115 * that is used by the kernel to request and write
116 * back data (the memory object, field "pager"), etc...
118 * Virtual memory objects are allocated to provide
119 * zero-filled memory (vm_allocate) or map a user-defined
120 * memory object into a virtual address space (vm_map).
122 * Virtual memory objects that refer to a user-defined
123 * memory object are called "permanent", because all changes
124 * made in virtual memory are reflected back to the
125 * memory manager, which may then store it permanently.
126 * Other virtual memory objects are called "temporary",
127 * meaning that changes need be written back only when
128 * necessary to reclaim pages, and that storage associated
129 * with the object can be discarded once it is no longer
132 * A permanent memory object may be mapped into more
133 * than one virtual address space. Moreover, two threads
134 * may attempt to make the first mapping of a memory
135 * object concurrently. Only one thread is allowed to
136 * complete this mapping; all others wait for the
137 * "pager_initialized" field is asserted, indicating
138 * that the first thread has initialized all of the
139 * necessary fields in the virtual memory object structure.
141 * The kernel relies on a *default memory manager* to
142 * provide backing storage for the zero-filled virtual
143 * memory objects. The pager memory objects associated
144 * with these temporary virtual memory objects are only
145 * requested from the default memory manager when it
146 * becomes necessary. Virtual memory objects
147 * that depend on the default memory manager are called
148 * "internal". The "pager_created" field is provided to
149 * indicate whether these ports have ever been allocated.
151 * The kernel may also create virtual memory objects to
152 * hold changed pages after a copy-on-write operation.
153 * In this case, the virtual memory object (and its
154 * backing storage -- its memory object) only contain
155 * those pages that have been changed. The "shadow"
156 * field refers to the virtual memory object that contains
157 * the remainder of the contents. The "shadow_offset"
158 * field indicates where in the "shadow" these contents begin.
159 * The "copy" field refers to a virtual memory object
160 * to which changed pages must be copied before changing
161 * this object, in order to implement another form
162 * of copy-on-write optimization.
164 * The virtual memory object structure also records
165 * the attributes associated with its memory object.
166 * The "pager_ready", "can_persist" and "copy_strategy"
167 * fields represent those attributes. The "cached_list"
168 * field is used in the implementation of the persistence
171 * ZZZ Continue this comment.
174 /* Forward declarations for internal functions. */
175 static kern_return_t
vm_object_terminate(
178 extern void vm_object_remove(
181 static kern_return_t
vm_object_copy_call(
182 vm_object_t src_object
,
183 vm_object_offset_t src_offset
,
184 vm_object_size_t size
,
185 vm_object_t
*_result_object
);
187 static void vm_object_do_collapse(
189 vm_object_t backing_object
);
191 static void vm_object_do_bypass(
193 vm_object_t backing_object
);
195 static void vm_object_release_pager(
196 memory_object_t pager
,
199 static zone_t vm_object_zone
; /* vm backing store zone */
202 * All wired-down kernel memory belongs to a single virtual
203 * memory object (kernel_object) to avoid wasting data structures.
205 static struct vm_object kernel_object_store
;
206 vm_object_t kernel_object
;
210 * The submap object is used as a placeholder for vm_map_submap
211 * operations. The object is declared in vm_map.c because it
212 * is exported by the vm_map module. The storage is declared
213 * here because it must be initialized here.
215 static struct vm_object vm_submap_object_store
;
218 * Virtual memory objects are initialized from
219 * a template (see vm_object_allocate).
221 * When adding a new field to the virtual memory
222 * object structure, be sure to add initialization
223 * (see _vm_object_allocate()).
225 static struct vm_object vm_object_template
;
227 unsigned int vm_page_purged_wired
= 0;
228 unsigned int vm_page_purged_busy
= 0;
229 unsigned int vm_page_purged_others
= 0;
233 * Virtual memory objects that are not referenced by
234 * any address maps, but that are allowed to persist
235 * (an attribute specified by the associated memory manager),
236 * are kept in a queue (vm_object_cached_list).
238 * When an object from this queue is referenced again,
239 * for example to make another address space mapping,
240 * it must be removed from the queue. That is, the
241 * queue contains *only* objects with zero references.
243 * The kernel may choose to terminate objects from this
244 * queue in order to reclaim storage. The current policy
245 * is to permit a fixed maximum number of unreferenced
246 * objects (vm_object_cached_max).
248 * A spin lock (accessed by routines
249 * vm_object_cache_{lock,lock_try,unlock}) governs the
250 * object cache. It must be held when objects are
251 * added to or removed from the cache (in vm_object_terminate).
252 * The routines that acquire a reference to a virtual
253 * memory object based on one of the memory object ports
254 * must also lock the cache.
256 * Ideally, the object cache should be more isolated
257 * from the reference mechanism, so that the lock need
258 * not be held to make simple references.
260 static vm_object_t
vm_object_cache_trim(
261 boolean_t called_from_vm_object_deallocate
);
263 static void vm_object_deactivate_all_pages(
266 static int vm_object_cached_high
; /* highest # cached objects */
267 static int vm_object_cached_max
= 512; /* may be patched*/
269 #define vm_object_cache_lock() \
270 lck_mtx_lock(&vm_object_cached_lock_data)
271 #define vm_object_cache_lock_try() \
272 lck_mtx_try_lock(&vm_object_cached_lock_data)
274 #endif /* VM_OBJECT_CACHE */
276 static queue_head_t vm_object_cached_list
;
277 static uint32_t vm_object_cache_pages_freed
= 0;
278 static uint32_t vm_object_cache_pages_moved
= 0;
279 static uint32_t vm_object_cache_pages_skipped
= 0;
280 static uint32_t vm_object_cache_adds
= 0;
281 static uint32_t vm_object_cached_count
= 0;
282 static lck_mtx_t vm_object_cached_lock_data
;
283 static lck_mtx_ext_t vm_object_cached_lock_data_ext
;
285 static uint32_t vm_object_page_grab_failed
= 0;
286 static uint32_t vm_object_page_grab_skipped
= 0;
287 static uint32_t vm_object_page_grab_returned
= 0;
288 static uint32_t vm_object_page_grab_pmapped
= 0;
289 static uint32_t vm_object_page_grab_reactivations
= 0;
291 #define vm_object_cache_lock_spin() \
292 lck_mtx_lock_spin(&vm_object_cached_lock_data)
293 #define vm_object_cache_unlock() \
294 lck_mtx_unlock(&vm_object_cached_lock_data)
296 static void vm_object_cache_remove_locked(vm_object_t
);
299 #define VM_OBJECT_HASH_COUNT 1024
300 #define VM_OBJECT_HASH_LOCK_COUNT 512
302 static lck_mtx_t vm_object_hashed_lock_data
[VM_OBJECT_HASH_LOCK_COUNT
];
303 static lck_mtx_ext_t vm_object_hashed_lock_data_ext
[VM_OBJECT_HASH_LOCK_COUNT
];
305 static queue_head_t vm_object_hashtable
[VM_OBJECT_HASH_COUNT
];
306 static struct zone
*vm_object_hash_zone
;
308 struct vm_object_hash_entry
{
309 queue_chain_t hash_link
; /* hash chain link */
310 memory_object_t pager
; /* pager we represent */
311 vm_object_t object
; /* corresponding object */
312 boolean_t waiting
; /* someone waiting for
316 typedef struct vm_object_hash_entry
*vm_object_hash_entry_t
;
317 #define VM_OBJECT_HASH_ENTRY_NULL ((vm_object_hash_entry_t) 0)
319 #define VM_OBJECT_HASH_SHIFT 5
320 #define vm_object_hash(pager) \
321 ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_COUNT))
323 #define vm_object_lock_hash(pager) \
324 ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_LOCK_COUNT))
326 void vm_object_hash_entry_free(
327 vm_object_hash_entry_t entry
);
329 static void vm_object_reap(vm_object_t object
);
330 static void vm_object_reap_async(vm_object_t object
);
331 static void vm_object_reaper_thread(void);
333 static lck_mtx_t vm_object_reaper_lock_data
;
334 static lck_mtx_ext_t vm_object_reaper_lock_data_ext
;
336 static queue_head_t vm_object_reaper_queue
; /* protected by vm_object_reaper_lock() */
337 unsigned int vm_object_reap_count
= 0;
338 unsigned int vm_object_reap_count_async
= 0;
340 #define vm_object_reaper_lock() \
341 lck_mtx_lock(&vm_object_reaper_lock_data)
342 #define vm_object_reaper_lock_spin() \
343 lck_mtx_lock_spin(&vm_object_reaper_lock_data)
344 #define vm_object_reaper_unlock() \
345 lck_mtx_unlock(&vm_object_reaper_lock_data)
349 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
354 vm_object_hash_lock_spin(
355 memory_object_t pager
)
359 index
= vm_object_lock_hash(pager
);
361 lck_mtx_lock_spin(&vm_object_hashed_lock_data
[index
]);
363 return (&vm_object_hashed_lock_data
[index
]);
367 vm_object_hash_unlock(lck_mtx_t
*lck
)
374 * vm_object_hash_lookup looks up a pager in the hashtable
375 * and returns the corresponding entry, with optional removal.
377 static vm_object_hash_entry_t
378 vm_object_hash_lookup(
379 memory_object_t pager
,
380 boolean_t remove_entry
)
383 vm_object_hash_entry_t entry
;
385 bucket
= &vm_object_hashtable
[vm_object_hash(pager
)];
387 entry
= (vm_object_hash_entry_t
)queue_first(bucket
);
388 while (!queue_end(bucket
, (queue_entry_t
)entry
)) {
389 if (entry
->pager
== pager
) {
391 queue_remove(bucket
, entry
,
392 vm_object_hash_entry_t
, hash_link
);
396 entry
= (vm_object_hash_entry_t
)queue_next(&entry
->hash_link
);
398 return(VM_OBJECT_HASH_ENTRY_NULL
);
402 * vm_object_hash_enter enters the specified
403 * pager / cache object association in the hashtable.
407 vm_object_hash_insert(
408 vm_object_hash_entry_t entry
,
413 bucket
= &vm_object_hashtable
[vm_object_hash(entry
->pager
)];
415 queue_enter(bucket
, entry
, vm_object_hash_entry_t
, hash_link
);
417 entry
->object
= object
;
418 object
->hashed
= TRUE
;
421 static vm_object_hash_entry_t
422 vm_object_hash_entry_alloc(
423 memory_object_t pager
)
425 vm_object_hash_entry_t entry
;
427 entry
= (vm_object_hash_entry_t
)zalloc(vm_object_hash_zone
);
428 entry
->pager
= pager
;
429 entry
->object
= VM_OBJECT_NULL
;
430 entry
->waiting
= FALSE
;
436 vm_object_hash_entry_free(
437 vm_object_hash_entry_t entry
)
439 zfree(vm_object_hash_zone
, entry
);
443 * vm_object_allocate:
445 * Returns a new object with the given size.
448 __private_extern__
void
450 vm_object_size_t size
,
454 "vm_object_allocate, object 0x%X size 0x%X\n",
455 object
, size
, 0,0,0);
457 *object
= vm_object_template
;
458 queue_init(&object
->memq
);
459 queue_init(&object
->msr_q
);
461 queue_init(&object
->uplq
);
462 #endif /* UPL_DEBUG */
463 vm_object_lock_init(object
);
464 object
->vo_size
= size
;
467 __private_extern__ vm_object_t
469 vm_object_size_t size
)
471 register vm_object_t object
;
473 object
= (vm_object_t
) zalloc(vm_object_zone
);
475 // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
477 if (object
!= VM_OBJECT_NULL
)
478 _vm_object_allocate(size
, object
);
484 lck_grp_t vm_object_lck_grp
;
485 lck_grp_t vm_object_cache_lck_grp
;
486 lck_grp_attr_t vm_object_lck_grp_attr
;
487 lck_attr_t vm_object_lck_attr
;
488 lck_attr_t kernel_object_lck_attr
;
491 * vm_object_bootstrap:
493 * Initialize the VM objects module.
495 __private_extern__
void
496 vm_object_bootstrap(void)
500 vm_object_zone
= zinit((vm_size_t
) sizeof(struct vm_object
),
501 round_page(512*1024),
504 zone_change(vm_object_zone
, Z_CALLERACCT
, FALSE
); /* don't charge caller */
505 zone_change(vm_object_zone
, Z_NOENCRYPT
, TRUE
);
507 vm_object_init_lck_grp();
509 queue_init(&vm_object_cached_list
);
511 lck_mtx_init_ext(&vm_object_cached_lock_data
,
512 &vm_object_cached_lock_data_ext
,
513 &vm_object_cache_lck_grp
,
514 &vm_object_lck_attr
);
516 queue_init(&vm_object_reaper_queue
);
518 for (i
= 0; i
< VM_OBJECT_HASH_LOCK_COUNT
; i
++) {
519 lck_mtx_init_ext(&vm_object_hashed_lock_data
[i
],
520 &vm_object_hashed_lock_data_ext
[i
],
522 &vm_object_lck_attr
);
524 lck_mtx_init_ext(&vm_object_reaper_lock_data
,
525 &vm_object_reaper_lock_data_ext
,
527 &vm_object_lck_attr
);
529 vm_object_hash_zone
=
530 zinit((vm_size_t
) sizeof (struct vm_object_hash_entry
),
531 round_page(512*1024),
533 "vm object hash entries");
534 zone_change(vm_object_hash_zone
, Z_CALLERACCT
, FALSE
);
535 zone_change(vm_object_hash_zone
, Z_NOENCRYPT
, TRUE
);
537 for (i
= 0; i
< VM_OBJECT_HASH_COUNT
; i
++)
538 queue_init(&vm_object_hashtable
[i
]);
542 * Fill in a template object, for quick initialization
545 /* memq; Lock; init after allocation */
546 vm_object_template
.memq
.prev
= NULL
;
547 vm_object_template
.memq
.next
= NULL
;
550 * We can't call vm_object_lock_init() here because that will
551 * allocate some memory and VM is not fully initialized yet.
552 * The lock will be initialized for each allocated object in
553 * _vm_object_allocate(), so we don't need to initialize it in
554 * the vm_object_template.
556 vm_object_lock_init(&vm_object_template
);
558 vm_object_template
.vo_size
= 0;
559 vm_object_template
.memq_hint
= VM_PAGE_NULL
;
560 vm_object_template
.ref_count
= 1;
562 vm_object_template
.res_count
= 1;
563 #endif /* TASK_SWAPPER */
564 vm_object_template
.resident_page_count
= 0;
565 vm_object_template
.wired_page_count
= 0;
566 vm_object_template
.reusable_page_count
= 0;
567 vm_object_template
.copy
= VM_OBJECT_NULL
;
568 vm_object_template
.shadow
= VM_OBJECT_NULL
;
569 vm_object_template
.vo_shadow_offset
= (vm_object_offset_t
) 0;
570 vm_object_template
.pager
= MEMORY_OBJECT_NULL
;
571 vm_object_template
.paging_offset
= 0;
572 vm_object_template
.pager_control
= MEMORY_OBJECT_CONTROL_NULL
;
573 vm_object_template
.copy_strategy
= MEMORY_OBJECT_COPY_SYMMETRIC
;
574 vm_object_template
.paging_in_progress
= 0;
575 vm_object_template
.activity_in_progress
= 0;
577 /* Begin bitfields */
578 vm_object_template
.all_wanted
= 0; /* all bits FALSE */
579 vm_object_template
.pager_created
= FALSE
;
580 vm_object_template
.pager_initialized
= FALSE
;
581 vm_object_template
.pager_ready
= FALSE
;
582 vm_object_template
.pager_trusted
= FALSE
;
583 vm_object_template
.can_persist
= FALSE
;
584 vm_object_template
.internal
= TRUE
;
585 vm_object_template
.temporary
= TRUE
;
586 vm_object_template
.private = FALSE
;
587 vm_object_template
.pageout
= FALSE
;
588 vm_object_template
.alive
= TRUE
;
589 vm_object_template
.purgable
= VM_PURGABLE_DENY
;
590 vm_object_template
.shadowed
= FALSE
;
591 vm_object_template
.silent_overwrite
= FALSE
;
592 vm_object_template
.advisory_pageout
= FALSE
;
593 vm_object_template
.true_share
= FALSE
;
594 vm_object_template
.terminating
= FALSE
;
595 vm_object_template
.named
= FALSE
;
596 vm_object_template
.shadow_severed
= FALSE
;
597 vm_object_template
.phys_contiguous
= FALSE
;
598 vm_object_template
.nophyscache
= FALSE
;
601 vm_object_template
.cached_list
.prev
= NULL
;
602 vm_object_template
.cached_list
.next
= NULL
;
603 vm_object_template
.msr_q
.prev
= NULL
;
604 vm_object_template
.msr_q
.next
= NULL
;
606 vm_object_template
.last_alloc
= (vm_object_offset_t
) 0;
607 vm_object_template
.sequential
= (vm_object_offset_t
) 0;
608 vm_object_template
.pages_created
= 0;
609 vm_object_template
.pages_used
= 0;
610 vm_object_template
.scan_collisions
= 0;
613 vm_object_template
.existence_map
= VM_EXTERNAL_NULL
;
614 #endif /* MACH_PAGEMAP */
615 vm_object_template
.cow_hint
= ~(vm_offset_t
)0;
617 vm_object_template
.paging_object
= VM_OBJECT_NULL
;
618 #endif /* MACH_ASSERT */
620 /* cache bitfields */
621 vm_object_template
.wimg_bits
= VM_WIMG_USE_DEFAULT
;
622 vm_object_template
.set_cache_attr
= FALSE
;
623 vm_object_template
.code_signed
= FALSE
;
624 vm_object_template
.hashed
= FALSE
;
625 vm_object_template
.transposed
= FALSE
;
626 vm_object_template
.mapping_in_progress
= FALSE
;
627 vm_object_template
.volatile_empty
= FALSE
;
628 vm_object_template
.volatile_fault
= FALSE
;
629 vm_object_template
.all_reusable
= FALSE
;
630 vm_object_template
.blocked_access
= FALSE
;
631 vm_object_template
.__object2_unused_bits
= 0;
633 vm_object_template
.uplq
.prev
= NULL
;
634 vm_object_template
.uplq
.next
= NULL
;
635 #endif /* UPL_DEBUG */
637 bzero(&vm_object_template
.pip_holders
,
638 sizeof (vm_object_template
.pip_holders
));
639 #endif /* VM_PIP_DEBUG */
641 vm_object_template
.objq
.next
=NULL
;
642 vm_object_template
.objq
.prev
=NULL
;
644 vm_object_template
.vo_cache_ts
= 0;
647 * Initialize the "kernel object"
650 kernel_object
= &kernel_object_store
;
653 * Note that in the following size specifications, we need to add 1 because
654 * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size.
658 _vm_object_allocate(vm_last_addr
+ 1,
661 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
664 kernel_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
667 * Initialize the "submap object". Make it as large as the
668 * kernel object so that no limit is imposed on submap sizes.
671 vm_submap_object
= &vm_submap_object_store
;
673 _vm_object_allocate(vm_last_addr
+ 1,
676 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
679 vm_submap_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
682 * Create an "extra" reference to this object so that we never
683 * try to deallocate it; zfree doesn't like to be called with
686 vm_object_reference(vm_submap_object
);
689 vm_external_module_initialize();
690 #endif /* MACH_PAGEMAP */
694 vm_object_reaper_init(void)
699 kr
= kernel_thread_start_priority(
700 (thread_continue_t
) vm_object_reaper_thread
,
704 if (kr
!= KERN_SUCCESS
) {
705 panic("failed to launch vm_object_reaper_thread kr=0x%x", kr
);
707 thread_deallocate(thread
);
710 __private_extern__
void
714 * Finish initializing the kernel object.
719 __private_extern__
void
720 vm_object_init_lck_grp(void)
723 * initialze the vm_object lock world
725 lck_grp_attr_setdefault(&vm_object_lck_grp_attr
);
726 lck_grp_init(&vm_object_lck_grp
, "vm_object", &vm_object_lck_grp_attr
);
727 lck_grp_init(&vm_object_cache_lck_grp
, "vm_object_cache", &vm_object_lck_grp_attr
);
728 lck_attr_setdefault(&vm_object_lck_attr
);
729 lck_attr_setdefault(&kernel_object_lck_attr
);
730 lck_attr_cleardebug(&kernel_object_lck_attr
);
734 #define MIGHT_NOT_CACHE_SHADOWS 1
735 #if MIGHT_NOT_CACHE_SHADOWS
736 static int cache_shadows
= TRUE
;
737 #endif /* MIGHT_NOT_CACHE_SHADOWS */
741 * vm_object_deallocate:
743 * Release a reference to the specified object,
744 * gained either through a vm_object_allocate
745 * or a vm_object_reference call. When all references
746 * are gone, storage associated with this object
747 * may be relinquished.
749 * No object may be locked.
751 unsigned long vm_object_deallocate_shared_successes
= 0;
752 unsigned long vm_object_deallocate_shared_failures
= 0;
753 unsigned long vm_object_deallocate_shared_swap_failures
= 0;
754 __private_extern__
void
755 vm_object_deallocate(
756 register vm_object_t object
)
759 boolean_t retry_cache_trim
= FALSE
;
760 uint32_t try_failed_count
= 0;
762 vm_object_t shadow
= VM_OBJECT_NULL
;
764 // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
765 // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
767 if (object
== VM_OBJECT_NULL
)
770 if (object
== kernel_object
) {
771 vm_object_lock_shared(object
);
773 OSAddAtomic(-1, &object
->ref_count
);
775 if (object
->ref_count
== 0) {
776 panic("vm_object_deallocate: losing kernel_object\n");
778 vm_object_unlock(object
);
782 if (object
->ref_count
> 2 ||
783 (!object
->named
&& object
->ref_count
> 1)) {
784 UInt32 original_ref_count
;
785 volatile UInt32
*ref_count_p
;
789 * The object currently looks like it is not being
790 * kept alive solely by the reference we're about to release.
791 * Let's try and release our reference without taking
792 * all the locks we would need if we had to terminate the
793 * object (cache lock + exclusive object lock).
794 * Lock the object "shared" to make sure we don't race with
795 * anyone holding it "exclusive".
797 vm_object_lock_shared(object
);
798 ref_count_p
= (volatile UInt32
*) &object
->ref_count
;
799 original_ref_count
= object
->ref_count
;
801 * Test again as "ref_count" could have changed.
802 * "named" shouldn't change.
804 if (original_ref_count
> 2 ||
805 (!object
->named
&& original_ref_count
> 1)) {
806 atomic_swap
= OSCompareAndSwap(
808 original_ref_count
- 1,
809 (UInt32
*) &object
->ref_count
);
810 if (atomic_swap
== FALSE
) {
811 vm_object_deallocate_shared_swap_failures
++;
817 vm_object_unlock(object
);
821 * ref_count was updated atomically !
823 vm_object_deallocate_shared_successes
++;
828 * Someone else updated the ref_count at the same
829 * time and we lost the race. Fall back to the usual
830 * slow but safe path...
832 vm_object_deallocate_shared_failures
++;
835 while (object
!= VM_OBJECT_NULL
) {
837 vm_object_lock(object
);
839 assert(object
->ref_count
> 0);
842 * If the object has a named reference, and only
843 * that reference would remain, inform the pager
844 * about the last "mapping" reference going away.
846 if ((object
->ref_count
== 2) && (object
->named
)) {
847 memory_object_t pager
= object
->pager
;
849 /* Notify the Pager that there are no */
850 /* more mappers for this object */
852 if (pager
!= MEMORY_OBJECT_NULL
) {
853 vm_object_mapping_wait(object
, THREAD_UNINT
);
854 vm_object_mapping_begin(object
);
855 vm_object_unlock(object
);
857 memory_object_last_unmap(pager
);
859 vm_object_lock(object
);
860 vm_object_mapping_end(object
);
862 assert(object
->ref_count
> 0);
866 * Lose the reference. If other references
867 * remain, then we are done, unless we need
868 * to retry a cache trim.
869 * If it is the last reference, then keep it
870 * until any pending initialization is completed.
873 /* if the object is terminating, it cannot go into */
874 /* the cache and we obviously should not call */
875 /* terminate again. */
877 if ((object
->ref_count
> 1) || object
->terminating
) {
878 vm_object_lock_assert_exclusive(object
);
880 vm_object_res_deallocate(object
);
882 if (object
->ref_count
== 1 &&
883 object
->shadow
!= VM_OBJECT_NULL
) {
885 * There's only one reference left on this
886 * VM object. We can't tell if it's a valid
887 * one (from a mapping for example) or if this
888 * object is just part of a possibly stale and
889 * useless shadow chain.
890 * We would like to try and collapse it into
891 * its parent, but we don't have any pointers
892 * back to this parent object.
893 * But we can try and collapse this object with
894 * its own shadows, in case these are useless
896 * We can't bypass this object though, since we
897 * don't know if this last reference on it is
900 vm_object_collapse(object
, 0, FALSE
);
902 vm_object_unlock(object
);
904 if (retry_cache_trim
&&
905 ((object
= vm_object_cache_trim(TRUE
)) !=
914 * We have to wait for initialization
915 * before destroying or caching the object.
918 if (object
->pager_created
&& ! object
->pager_initialized
) {
919 assert(! object
->can_persist
);
920 vm_object_assert_wait(object
,
921 VM_OBJECT_EVENT_INITIALIZED
,
923 vm_object_unlock(object
);
925 thread_block(THREAD_CONTINUE_NULL
);
931 * If this object can persist, then enter it in
932 * the cache. Otherwise, terminate it.
934 * NOTE: Only permanent objects are cached, and
935 * permanent objects cannot have shadows. This
936 * affects the residence counting logic in a minor
937 * way (can do it in-line, mostly).
940 if ((object
->can_persist
) && (object
->alive
)) {
942 * Now it is safe to decrement reference count,
943 * and to return if reference count is > 0.
946 vm_object_lock_assert_exclusive(object
);
947 if (--object
->ref_count
> 0) {
948 vm_object_res_deallocate(object
);
949 vm_object_unlock(object
);
951 if (retry_cache_trim
&&
952 ((object
= vm_object_cache_trim(TRUE
)) !=
959 #if MIGHT_NOT_CACHE_SHADOWS
961 * Remove shadow now if we don't
962 * want to cache shadows.
964 if (! cache_shadows
) {
965 shadow
= object
->shadow
;
966 object
->shadow
= VM_OBJECT_NULL
;
968 #endif /* MIGHT_NOT_CACHE_SHADOWS */
971 * Enter the object onto the queue of
972 * cached objects, and deactivate
975 assert(object
->shadow
== VM_OBJECT_NULL
);
976 VM_OBJ_RES_DECR(object
);
978 "vm_o_deallocate: adding %x to cache, queue = (%x, %x)\n",
980 vm_object_cached_list
.next
,
981 vm_object_cached_list
.prev
,0,0);
984 vm_object_unlock(object
);
986 try_failed_count
= 0;
988 vm_object_cache_lock();
991 * if we try to take a regular lock here
992 * we risk deadlocking against someone
993 * holding a lock on this object while
994 * trying to vm_object_deallocate a different
997 if (vm_object_lock_try(object
))
999 vm_object_cache_unlock();
1002 mutex_pause(try_failed_count
); /* wait a bit */
1004 vm_object_cached_count
++;
1005 if (vm_object_cached_count
> vm_object_cached_high
)
1006 vm_object_cached_high
= vm_object_cached_count
;
1007 queue_enter(&vm_object_cached_list
, object
,
1008 vm_object_t
, cached_list
);
1009 vm_object_cache_unlock();
1011 vm_object_deactivate_all_pages(object
);
1012 vm_object_unlock(object
);
1014 #if MIGHT_NOT_CACHE_SHADOWS
1016 * If we have a shadow that we need
1017 * to deallocate, do so now, remembering
1018 * to trim the cache later.
1020 if (! cache_shadows
&& shadow
!= VM_OBJECT_NULL
) {
1022 retry_cache_trim
= TRUE
;
1025 #endif /* MIGHT_NOT_CACHE_SHADOWS */
1028 * Trim the cache. If the cache trim
1029 * returns with a shadow for us to deallocate,
1030 * then remember to retry the cache trim
1031 * when we are done deallocating the shadow.
1032 * Otherwise, we are done.
1035 object
= vm_object_cache_trim(TRUE
);
1036 if (object
== VM_OBJECT_NULL
) {
1039 retry_cache_trim
= TRUE
;
1041 #endif /* VM_OBJECT_CACHE */
1044 * This object is not cachable; terminate it.
1047 "vm_o_deallocate: !cacheable 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
1048 object
, object
->resident_page_count
,
1049 object
->paging_in_progress
,
1050 (void *)current_thread(),object
->ref_count
);
1052 VM_OBJ_RES_DECR(object
); /* XXX ? */
1054 * Terminate this object. If it had a shadow,
1055 * then deallocate it; otherwise, if we need
1056 * to retry a cache trim, do so now; otherwise,
1057 * we are done. "pageout" objects have a shadow,
1058 * but maintain a "paging reference" rather than
1059 * a normal reference.
1061 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
1063 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
1066 if (shadow
!= VM_OBJECT_NULL
) {
1071 if (retry_cache_trim
&&
1072 ((object
= vm_object_cache_trim(TRUE
)) !=
1081 assert(! retry_cache_trim
);
1088 vm_object_page_grab(
1091 vm_page_t p
, next_p
;
1095 vm_object_lock_assert_exclusive(object
);
1097 next_p
= (vm_page_t
)queue_first(&object
->memq
);
1098 p_limit
= MIN(50, object
->resident_page_count
);
1100 while (!queue_end(&object
->memq
, (queue_entry_t
)next_p
) && --p_limit
> 0) {
1103 next_p
= (vm_page_t
)queue_next(&next_p
->listq
);
1105 if (VM_PAGE_WIRED(p
) || p
->busy
|| p
->cleaning
|| p
->laundry
|| p
->fictitious
)
1106 goto move_page_in_obj
;
1108 if (p
->pmapped
|| p
->dirty
|| p
->precious
) {
1109 vm_page_lockspin_queues();
1114 vm_object_page_grab_pmapped
++;
1116 if (p
->reference
== FALSE
|| p
->dirty
== FALSE
) {
1118 refmod_state
= pmap_get_refmod(p
->phys_page
);
1120 if (refmod_state
& VM_MEM_REFERENCED
)
1121 p
->reference
= TRUE
;
1122 if (refmod_state
& VM_MEM_MODIFIED
) {
1123 SET_PAGE_DIRTY(p
, FALSE
);
1126 if (p
->dirty
== FALSE
&& p
->precious
== FALSE
) {
1128 refmod_state
= pmap_disconnect(p
->phys_page
);
1130 if (refmod_state
& VM_MEM_REFERENCED
)
1131 p
->reference
= TRUE
;
1132 if (refmod_state
& VM_MEM_MODIFIED
) {
1133 SET_PAGE_DIRTY(p
, FALSE
);
1136 if (p
->dirty
== FALSE
)
1140 if (p
->inactive
&& p
->reference
== TRUE
) {
1141 vm_page_activate(p
);
1143 VM_STAT_INCR(reactivations
);
1144 vm_object_page_grab_reactivations
++;
1146 vm_page_unlock_queues();
1148 queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
1149 queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
1154 vm_page_lockspin_queues();
1156 vm_page_free_prepare_queues(p
);
1157 vm_object_page_grab_returned
++;
1158 vm_object_page_grab_skipped
+= p_skipped
;
1160 vm_page_unlock_queues();
1162 vm_page_free_prepare_object(p
, TRUE
);
1166 vm_object_page_grab_skipped
+= p_skipped
;
1167 vm_object_page_grab_failed
++;
1174 #define EVICT_PREPARE_LIMIT 64
1175 #define EVICT_AGE 10
1177 static clock_sec_t vm_object_cache_aging_ts
= 0;
1180 vm_object_cache_remove_locked(
1183 queue_remove(&vm_object_cached_list
, object
, vm_object_t
, objq
);
1184 object
->objq
.next
= NULL
;
1185 object
->objq
.prev
= NULL
;
1187 vm_object_cached_count
--;
1191 vm_object_cache_remove(
1194 vm_object_cache_lock_spin();
1196 if (object
->objq
.next
|| object
->objq
.prev
)
1197 vm_object_cache_remove_locked(object
);
1199 vm_object_cache_unlock();
1203 vm_object_cache_add(
1209 if (object
->resident_page_count
== 0)
1211 clock_get_system_nanotime(&sec
, &nsec
);
1213 vm_object_cache_lock_spin();
1215 if (object
->objq
.next
== NULL
&& object
->objq
.prev
== NULL
) {
1216 queue_enter(&vm_object_cached_list
, object
, vm_object_t
, objq
);
1217 object
->vo_cache_ts
= sec
+ EVICT_AGE
;
1218 object
->vo_cache_pages_to_scan
= object
->resident_page_count
;
1220 vm_object_cached_count
++;
1221 vm_object_cache_adds
++;
1223 vm_object_cache_unlock();
1227 vm_object_cache_evict(
1229 int max_objects_to_examine
)
1231 vm_object_t object
= VM_OBJECT_NULL
;
1232 vm_object_t next_obj
= VM_OBJECT_NULL
;
1233 vm_page_t local_free_q
= VM_PAGE_NULL
;
1237 vm_page_t ep_array
[EVICT_PREPARE_LIMIT
];
1243 uint32_t ep_skipped
= 0;
1247 KERNEL_DEBUG(0x13001ec | DBG_FUNC_START
, 0, 0, 0, 0, 0);
1249 * do a couple of quick checks to see if it's
1250 * worthwhile grabbing the lock
1252 if (queue_empty(&vm_object_cached_list
)) {
1253 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, 0, 0, 0, 0, 0);
1256 clock_get_system_nanotime(&sec
, &nsec
);
1259 * the object on the head of the queue has not
1260 * yet sufficiently aged
1262 if (sec
< vm_object_cache_aging_ts
) {
1263 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, 0, 0, 0, 0, 0);
1267 * don't need the queue lock to find
1268 * and lock an object on the cached list
1270 vm_page_unlock_queues();
1272 vm_object_cache_lock_spin();
1275 next_obj
= (vm_object_t
)queue_first(&vm_object_cached_list
);
1277 while (!queue_end(&vm_object_cached_list
, (queue_entry_t
)next_obj
) && object_cnt
++ < max_objects_to_examine
) {
1280 next_obj
= (vm_object_t
)queue_next(&next_obj
->objq
);
1282 if (sec
< object
->vo_cache_ts
) {
1283 KERNEL_DEBUG(0x130020c, object
, object
->resident_page_count
, object
->vo_cache_ts
, sec
, 0);
1285 vm_object_cache_aging_ts
= object
->vo_cache_ts
;
1286 object
= VM_OBJECT_NULL
;
1289 if (!vm_object_lock_try_scan(object
)) {
1291 * just skip over this guy for now... if we find
1292 * an object to steal pages from, we'll revist in a bit...
1293 * hopefully, the lock will have cleared
1295 KERNEL_DEBUG(0x13001f8, object
, object
->resident_page_count
, 0, 0, 0);
1297 object
= VM_OBJECT_NULL
;
1300 if (queue_empty(&object
->memq
) || object
->vo_cache_pages_to_scan
== 0) {
1302 * this case really shouldn't happen, but it's not fatal
1303 * so deal with it... if we don't remove the object from
1304 * the list, we'll never move past it.
1306 KERNEL_DEBUG(0x13001fc, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1308 vm_object_cache_remove_locked(object
);
1309 vm_object_unlock(object
);
1310 object
= VM_OBJECT_NULL
;
1314 * we have a locked object with pages...
1315 * time to start harvesting
1319 vm_object_cache_unlock();
1321 if (object
== VM_OBJECT_NULL
)
1325 * object is locked at this point and
1326 * has resident pages
1328 next_p
= (vm_page_t
)queue_first(&object
->memq
);
1331 * break the page scan into 2 pieces to minimize the time spent
1332 * behind the page queue lock...
1333 * the list of pages on these unused objects is likely to be cold
1334 * w/r to the cpu cache which increases the time to scan the list
1335 * tenfold... and we may have a 'run' of pages we can't utilize that
1336 * needs to be skipped over...
1338 if ((ep_limit
= num_to_evict
- (ep_freed
+ ep_moved
)) > EVICT_PREPARE_LIMIT
)
1339 ep_limit
= EVICT_PREPARE_LIMIT
;
1342 while (!queue_end(&object
->memq
, (queue_entry_t
)next_p
) && object
->vo_cache_pages_to_scan
&& ep_count
< ep_limit
) {
1345 next_p
= (vm_page_t
)queue_next(&next_p
->listq
);
1347 object
->vo_cache_pages_to_scan
--;
1349 if (VM_PAGE_WIRED(p
) || p
->busy
|| p
->cleaning
|| p
->laundry
) {
1350 queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
1351 queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
1356 if (p
->wpmapped
|| p
->dirty
|| p
->precious
) {
1357 queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
1358 queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
1360 pmap_clear_reference(p
->phys_page
);
1362 ep_array
[ep_count
++] = p
;
1364 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_START
, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1366 vm_page_lockspin_queues();
1368 for (ep_index
= 0; ep_index
< ep_count
; ep_index
++) {
1370 p
= ep_array
[ep_index
];
1372 if (p
->wpmapped
|| p
->dirty
|| p
->precious
) {
1373 p
->reference
= FALSE
;
1374 p
->no_cache
= FALSE
;
1377 * we've already filtered out pages that are in the laundry
1378 * so if we get here, this page can't be on the pageout queue
1380 assert(!p
->pageout_queue
);
1382 VM_PAGE_QUEUES_REMOVE(p
);
1383 VM_PAGE_ENQUEUE_INACTIVE(p
, TRUE
);
1387 vm_page_free_prepare_queues(p
);
1389 assert(p
->pageq
.next
== NULL
&& p
->pageq
.prev
== NULL
);
1391 * Add this page to our list of reclaimed pages,
1392 * to be freed later.
1394 p
->pageq
.next
= (queue_entry_t
) local_free_q
;
1400 vm_page_unlock_queues();
1402 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_END
, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1405 vm_page_free_list(local_free_q
, TRUE
);
1406 local_free_q
= VM_PAGE_NULL
;
1408 if (object
->vo_cache_pages_to_scan
== 0) {
1409 KERNEL_DEBUG(0x1300208, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1411 vm_object_cache_remove(object
);
1413 KERNEL_DEBUG(0x13001fc, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1416 * done with this object
1418 vm_object_unlock(object
);
1419 object
= VM_OBJECT_NULL
;
1422 * at this point, we are not holding any locks
1424 if ((ep_freed
+ ep_moved
) >= num_to_evict
) {
1426 * we've reached our target for the
1427 * number of pages to evict
1431 vm_object_cache_lock_spin();
1434 * put the page queues lock back to the caller's
1437 vm_page_lock_queues();
1439 vm_object_cache_pages_freed
+= ep_freed
;
1440 vm_object_cache_pages_moved
+= ep_moved
;
1441 vm_object_cache_pages_skipped
+= ep_skipped
;
1443 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, ep_freed
, 0, 0, 0, 0);
1450 * Check to see whether we really need to trim
1451 * down the cache. If so, remove an object from
1452 * the cache, terminate it, and repeat.
1454 * Called with, and returns with, cache lock unlocked.
1457 vm_object_cache_trim(
1458 boolean_t called_from_vm_object_deallocate
)
1460 register vm_object_t object
= VM_OBJECT_NULL
;
1466 * If we no longer need to trim the cache,
1469 if (vm_object_cached_count
<= vm_object_cached_max
)
1470 return VM_OBJECT_NULL
;
1472 vm_object_cache_lock();
1473 if (vm_object_cached_count
<= vm_object_cached_max
) {
1474 vm_object_cache_unlock();
1475 return VM_OBJECT_NULL
;
1479 * We must trim down the cache, so remove
1480 * the first object in the cache.
1483 "vm_object_cache_trim: removing from front of cache (%x, %x)\n",
1484 vm_object_cached_list
.next
,
1485 vm_object_cached_list
.prev
, 0, 0, 0);
1487 object
= (vm_object_t
) queue_first(&vm_object_cached_list
);
1488 if(object
== (vm_object_t
) &vm_object_cached_list
) {
1489 /* something's wrong with the calling parameter or */
1490 /* the value of vm_object_cached_count, just fix */
1492 if(vm_object_cached_max
< 0)
1493 vm_object_cached_max
= 0;
1494 vm_object_cached_count
= 0;
1495 vm_object_cache_unlock();
1496 return VM_OBJECT_NULL
;
1498 vm_object_lock(object
);
1499 queue_remove(&vm_object_cached_list
, object
, vm_object_t
,
1501 vm_object_cached_count
--;
1503 vm_object_cache_unlock();
1505 * Since this object is in the cache, we know
1506 * that it is initialized and has no references.
1507 * Take a reference to avoid recursive deallocations.
1510 assert(object
->pager_initialized
);
1511 assert(object
->ref_count
== 0);
1512 vm_object_lock_assert_exclusive(object
);
1513 object
->ref_count
++;
1516 * Terminate the object.
1517 * If the object had a shadow, we let vm_object_deallocate
1518 * deallocate it. "pageout" objects have a shadow, but
1519 * maintain a "paging reference" rather than a normal
1521 * (We are careful here to limit recursion.)
1523 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
1525 if(vm_object_terminate(object
) != KERN_SUCCESS
)
1528 if (shadow
!= VM_OBJECT_NULL
) {
1529 if (called_from_vm_object_deallocate
) {
1532 vm_object_deallocate(shadow
);
1541 * Routine: vm_object_terminate
1543 * Free all resources associated with a vm_object.
1544 * In/out conditions:
1545 * Upon entry, the object must be locked,
1546 * and the object must have exactly one reference.
1548 * The shadow object reference is left alone.
1550 * The object must be unlocked if its found that pages
1551 * must be flushed to a backing object. If someone
1552 * manages to map the object while it is being flushed
1553 * the object is returned unlocked and unchanged. Otherwise,
1554 * upon exit, the cache will be unlocked, and the
1555 * object will cease to exist.
1557 static kern_return_t
1558 vm_object_terminate(
1561 vm_object_t shadow_object
;
1563 XPR(XPR_VM_OBJECT
, "vm_object_terminate, object 0x%X ref %d\n",
1564 object
, object
->ref_count
, 0, 0, 0);
1566 if (!object
->pageout
&& (!object
->temporary
|| object
->can_persist
) &&
1567 (object
->pager
!= NULL
|| object
->shadow_severed
)) {
1569 * Clear pager_trusted bit so that the pages get yanked
1570 * out of the object instead of cleaned in place. This
1571 * prevents a deadlock in XMM and makes more sense anyway.
1573 object
->pager_trusted
= FALSE
;
1575 vm_object_reap_pages(object
, REAP_TERMINATE
);
1578 * Make sure the object isn't already being terminated
1580 if (object
->terminating
) {
1581 vm_object_lock_assert_exclusive(object
);
1582 object
->ref_count
--;
1583 assert(object
->ref_count
> 0);
1584 vm_object_unlock(object
);
1585 return KERN_FAILURE
;
1589 * Did somebody get a reference to the object while we were
1592 if (object
->ref_count
!= 1) {
1593 vm_object_lock_assert_exclusive(object
);
1594 object
->ref_count
--;
1595 assert(object
->ref_count
> 0);
1596 vm_object_res_deallocate(object
);
1597 vm_object_unlock(object
);
1598 return KERN_FAILURE
;
1602 * Make sure no one can look us up now.
1605 object
->terminating
= TRUE
;
1606 object
->alive
= FALSE
;
1608 if ( !object
->internal
&& (object
->objq
.next
|| object
->objq
.prev
))
1609 vm_object_cache_remove(object
);
1611 if (object
->hashed
) {
1614 lck
= vm_object_hash_lock_spin(object
->pager
);
1615 vm_object_remove(object
);
1616 vm_object_hash_unlock(lck
);
1619 * Detach the object from its shadow if we are the shadow's
1620 * copy. The reference we hold on the shadow must be dropped
1623 if (((shadow_object
= object
->shadow
) != VM_OBJECT_NULL
) &&
1624 !(object
->pageout
)) {
1625 vm_object_lock(shadow_object
);
1626 if (shadow_object
->copy
== object
)
1627 shadow_object
->copy
= VM_OBJECT_NULL
;
1628 vm_object_unlock(shadow_object
);
1631 if (object
->paging_in_progress
!= 0 ||
1632 object
->activity_in_progress
!= 0) {
1634 * There are still some paging_in_progress references
1635 * on this object, meaning that there are some paging
1636 * or other I/O operations in progress for this VM object.
1637 * Such operations take some paging_in_progress references
1638 * up front to ensure that the object doesn't go away, but
1639 * they may also need to acquire a reference on the VM object,
1640 * to map it in kernel space, for example. That means that
1641 * they may end up releasing the last reference on the VM
1642 * object, triggering its termination, while still holding
1643 * paging_in_progress references. Waiting for these
1644 * pending paging_in_progress references to go away here would
1647 * To avoid deadlocking, we'll let the vm_object_reaper_thread
1648 * complete the VM object termination if it still holds
1649 * paging_in_progress references at this point.
1651 * No new paging_in_progress should appear now that the
1652 * VM object is "terminating" and not "alive".
1654 vm_object_reap_async(object
);
1655 vm_object_unlock(object
);
1657 * Return KERN_FAILURE to let the caller know that we
1658 * haven't completed the termination and it can't drop this
1659 * object's reference on its shadow object yet.
1660 * The reaper thread will take care of that once it has
1661 * completed this object's termination.
1663 return KERN_FAILURE
;
1666 * complete the VM object termination
1668 vm_object_reap(object
);
1669 object
= VM_OBJECT_NULL
;
1672 * the object lock was released by vm_object_reap()
1674 * KERN_SUCCESS means that this object has been terminated
1675 * and no longer needs its shadow object but still holds a
1677 * The caller is responsible for dropping that reference.
1678 * We can't call vm_object_deallocate() here because that
1679 * would create a recursion.
1681 return KERN_SUCCESS
;
1688 * Complete the termination of a VM object after it's been marked
1689 * as "terminating" and "!alive" by vm_object_terminate().
1691 * The VM object must be locked by caller.
1692 * The lock will be released on return and the VM object is no longer valid.
1698 memory_object_t pager
;
1700 vm_object_lock_assert_exclusive(object
);
1701 assert(object
->paging_in_progress
== 0);
1702 assert(object
->activity_in_progress
== 0);
1704 vm_object_reap_count
++;
1706 pager
= object
->pager
;
1707 object
->pager
= MEMORY_OBJECT_NULL
;
1709 if (pager
!= MEMORY_OBJECT_NULL
)
1710 memory_object_control_disable(object
->pager_control
);
1712 object
->ref_count
--;
1714 assert(object
->res_count
== 0);
1715 #endif /* TASK_SWAPPER */
1717 assert (object
->ref_count
== 0);
1720 * remove from purgeable queue if it's on
1722 if (object
->internal
&& (object
->objq
.next
|| object
->objq
.prev
)) {
1723 purgeable_q_t queue
= vm_purgeable_object_remove(object
);
1726 /* Must take page lock for this - using it to protect token queue */
1727 vm_page_lock_queues();
1728 vm_purgeable_token_delete_first(queue
);
1730 assert(queue
->debug_count_objects
>=0);
1731 vm_page_unlock_queues();
1735 * Clean or free the pages, as appropriate.
1736 * It is possible for us to find busy/absent pages,
1737 * if some faults on this object were aborted.
1739 if (object
->pageout
) {
1740 assert(object
->shadow
!= VM_OBJECT_NULL
);
1742 vm_pageout_object_terminate(object
);
1744 } else if (((object
->temporary
&& !object
->can_persist
) || (pager
== MEMORY_OBJECT_NULL
))) {
1746 vm_object_reap_pages(object
, REAP_REAP
);
1748 assert(queue_empty(&object
->memq
));
1749 assert(object
->paging_in_progress
== 0);
1750 assert(object
->activity_in_progress
== 0);
1751 assert(object
->ref_count
== 0);
1754 * If the pager has not already been released by
1755 * vm_object_destroy, we need to terminate it and
1756 * release our reference to it here.
1758 if (pager
!= MEMORY_OBJECT_NULL
) {
1759 vm_object_unlock(object
);
1760 vm_object_release_pager(pager
, object
->hashed
);
1761 vm_object_lock(object
);
1764 /* kick off anyone waiting on terminating */
1765 object
->terminating
= FALSE
;
1766 vm_object_paging_begin(object
);
1767 vm_object_paging_end(object
);
1768 vm_object_unlock(object
);
1771 vm_external_destroy(object
->existence_map
, object
->vo_size
);
1772 #endif /* MACH_PAGEMAP */
1774 object
->shadow
= VM_OBJECT_NULL
;
1776 vm_object_lock_destroy(object
);
1778 * Free the space for the object.
1780 zfree(vm_object_zone
, object
);
1781 object
= VM_OBJECT_NULL
;
1785 unsigned int vm_max_batch
= 256;
1787 #define V_O_R_MAX_BATCH 128
1789 #define BATCH_LIMIT(max) (vm_max_batch >= max ? max : vm_max_batch)
1792 #define VM_OBJ_REAP_FREELIST(_local_free_q, do_disconnect) \
1794 if (_local_free_q) { \
1795 if (do_disconnect) { \
1797 for (m = _local_free_q; \
1798 m != VM_PAGE_NULL; \
1799 m = (vm_page_t) m->pageq.next) { \
1801 pmap_disconnect(m->phys_page); \
1805 vm_page_free_list(_local_free_q, TRUE); \
1806 _local_free_q = VM_PAGE_NULL; \
1812 vm_object_reap_pages(
1818 vm_page_t local_free_q
= VM_PAGE_NULL
;
1820 boolean_t disconnect_on_release
;
1822 if (reap_type
== REAP_DATA_FLUSH
) {
1824 * We need to disconnect pages from all pmaps before
1825 * releasing them to the free list
1827 disconnect_on_release
= TRUE
;
1830 * Either the caller has already disconnected the pages
1831 * from all pmaps, or we disconnect them here as we add
1832 * them to out local list of pages to be released.
1833 * No need to re-disconnect them when we release the pages
1836 disconnect_on_release
= FALSE
;
1839 restart_after_sleep
:
1840 if (queue_empty(&object
->memq
))
1842 loop_count
= BATCH_LIMIT(V_O_R_MAX_BATCH
);
1844 vm_page_lockspin_queues();
1846 next
= (vm_page_t
)queue_first(&object
->memq
);
1848 while (!queue_end(&object
->memq
, (queue_entry_t
)next
)) {
1851 next
= (vm_page_t
)queue_next(&next
->listq
);
1853 if (--loop_count
== 0) {
1855 vm_page_unlock_queues();
1859 * Free the pages we reclaimed so far
1860 * and take a little break to avoid
1861 * hogging the page queue lock too long
1863 VM_OBJ_REAP_FREELIST(local_free_q
,
1864 disconnect_on_release
);
1868 loop_count
= BATCH_LIMIT(V_O_R_MAX_BATCH
);
1870 vm_page_lockspin_queues();
1872 if (reap_type
== REAP_DATA_FLUSH
|| reap_type
== REAP_TERMINATE
) {
1874 if (p
->busy
|| p
->cleaning
) {
1876 vm_page_unlock_queues();
1878 * free the pages reclaimed so far
1880 VM_OBJ_REAP_FREELIST(local_free_q
,
1881 disconnect_on_release
);
1883 PAGE_SLEEP(object
, p
, THREAD_UNINT
);
1885 goto restart_after_sleep
;
1890 vm_pageout_steal_laundry(p
, TRUE
);
1893 switch (reap_type
) {
1895 case REAP_DATA_FLUSH
:
1896 if (VM_PAGE_WIRED(p
)) {
1898 * this is an odd case... perhaps we should
1899 * zero-fill this page since we're conceptually
1900 * tossing its data at this point, but leaving
1901 * it on the object to honor the 'wire' contract
1907 case REAP_PURGEABLE
:
1908 if (VM_PAGE_WIRED(p
)) {
1910 * can't purge a wired page
1912 vm_page_purged_wired
++;
1915 if (p
->laundry
&& !p
->busy
&& !p
->cleaning
) {
1918 vm_pageout_steal_laundry(p
, TRUE
);
1920 if (p
->cleaning
|| p
->laundry
) {
1922 * page is being acted upon,
1923 * so don't mess with it
1925 vm_page_purged_others
++;
1930 * We can't reclaim a busy page but we can
1931 * make it more likely to be paged (it's not wired) to make
1932 * sure that it gets considered by
1933 * vm_pageout_scan() later.
1935 vm_page_deactivate(p
);
1936 vm_page_purged_busy
++;
1940 assert(p
->object
!= kernel_object
);
1943 * we can discard this page...
1945 if (p
->pmapped
== TRUE
) {
1950 refmod_state
= pmap_disconnect(p
->phys_page
);
1951 if (refmod_state
& VM_MEM_MODIFIED
) {
1952 SET_PAGE_DIRTY(p
, FALSE
);
1955 if (p
->dirty
|| p
->precious
) {
1957 * we saved the cost of cleaning this page !
1959 vm_page_purged_count
++;
1964 case REAP_TERMINATE
:
1965 if (p
->absent
|| p
->private) {
1967 * For private pages, VM_PAGE_FREE just
1968 * leaves the page structure around for
1969 * its owner to clean up. For absent
1970 * pages, the structure is returned to
1971 * the appropriate pool.
1975 if (p
->fictitious
) {
1976 assert (p
->phys_page
== vm_page_guard_addr
);
1979 if (!p
->dirty
&& p
->wpmapped
)
1980 p
->dirty
= pmap_is_modified(p
->phys_page
);
1982 if ((p
->dirty
|| p
->precious
) && !p
->error
&& object
->alive
) {
1985 VM_PAGE_QUEUES_REMOVE(p
);
1987 * flush page... page will be freed
1988 * upon completion of I/O
1990 vm_pageout_cluster(p
, TRUE
);
1992 vm_page_unlock_queues();
1994 * free the pages reclaimed so far
1996 VM_OBJ_REAP_FREELIST(local_free_q
,
1997 disconnect_on_release
);
1999 vm_object_paging_wait(object
, THREAD_UNINT
);
2001 goto restart_after_sleep
;
2008 vm_page_free_prepare_queues(p
);
2009 assert(p
->pageq
.next
== NULL
&& p
->pageq
.prev
== NULL
);
2011 * Add this page to our list of reclaimed pages,
2012 * to be freed later.
2014 p
->pageq
.next
= (queue_entry_t
) local_free_q
;
2017 vm_page_unlock_queues();
2020 * Free the remaining reclaimed pages
2022 VM_OBJ_REAP_FREELIST(local_free_q
,
2023 disconnect_on_release
);
2028 vm_object_reap_async(
2031 vm_object_lock_assert_exclusive(object
);
2033 vm_object_reaper_lock_spin();
2035 vm_object_reap_count_async
++;
2037 /* enqueue the VM object... */
2038 queue_enter(&vm_object_reaper_queue
, object
,
2039 vm_object_t
, cached_list
);
2041 vm_object_reaper_unlock();
2043 /* ... and wake up the reaper thread */
2044 thread_wakeup((event_t
) &vm_object_reaper_queue
);
2049 vm_object_reaper_thread(void)
2051 vm_object_t object
, shadow_object
;
2053 vm_object_reaper_lock_spin();
2055 while (!queue_empty(&vm_object_reaper_queue
)) {
2056 queue_remove_first(&vm_object_reaper_queue
,
2061 vm_object_reaper_unlock();
2062 vm_object_lock(object
);
2064 assert(object
->terminating
);
2065 assert(!object
->alive
);
2068 * The pageout daemon might be playing with our pages.
2069 * Now that the object is dead, it won't touch any more
2070 * pages, but some pages might already be on their way out.
2071 * Hence, we wait until the active paging activities have
2072 * ceased before we break the association with the pager
2075 while (object
->paging_in_progress
!= 0 ||
2076 object
->activity_in_progress
!= 0) {
2077 vm_object_wait(object
,
2078 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
2080 vm_object_lock(object
);
2084 object
->pageout
? VM_OBJECT_NULL
: object
->shadow
;
2086 vm_object_reap(object
);
2087 /* cache is unlocked and object is no longer valid */
2088 object
= VM_OBJECT_NULL
;
2090 if (shadow_object
!= VM_OBJECT_NULL
) {
2092 * Drop the reference "object" was holding on
2093 * its shadow object.
2095 vm_object_deallocate(shadow_object
);
2096 shadow_object
= VM_OBJECT_NULL
;
2098 vm_object_reaper_lock_spin();
2101 /* wait for more work... */
2102 assert_wait((event_t
) &vm_object_reaper_queue
, THREAD_UNINT
);
2104 vm_object_reaper_unlock();
2106 thread_block((thread_continue_t
) vm_object_reaper_thread
);
2111 * Routine: vm_object_pager_wakeup
2112 * Purpose: Wake up anyone waiting for termination of a pager.
2116 vm_object_pager_wakeup(
2117 memory_object_t pager
)
2119 vm_object_hash_entry_t entry
;
2120 boolean_t waiting
= FALSE
;
2124 * If anyone was waiting for the memory_object_terminate
2125 * to be queued, wake them up now.
2127 lck
= vm_object_hash_lock_spin(pager
);
2128 entry
= vm_object_hash_lookup(pager
, TRUE
);
2129 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
2130 waiting
= entry
->waiting
;
2131 vm_object_hash_unlock(lck
);
2133 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
) {
2135 thread_wakeup((event_t
) pager
);
2136 vm_object_hash_entry_free(entry
);
2141 * Routine: vm_object_release_pager
2142 * Purpose: Terminate the pager and, upon completion,
2143 * release our last reference to it.
2144 * just like memory_object_terminate, except
2145 * that we wake up anyone blocked in vm_object_enter
2146 * waiting for termination message to be queued
2147 * before calling memory_object_init.
2150 vm_object_release_pager(
2151 memory_object_t pager
,
2156 * Terminate the pager.
2159 (void) memory_object_terminate(pager
);
2161 if (hashed
== TRUE
) {
2163 * Wakeup anyone waiting for this terminate
2164 * and remove the entry from the hash
2166 vm_object_pager_wakeup(pager
);
2169 * Release reference to pager.
2171 memory_object_deallocate(pager
);
2175 * Routine: vm_object_destroy
2177 * Shut down a VM object, despite the
2178 * presence of address map (or other) references
2184 __unused kern_return_t reason
)
2186 memory_object_t old_pager
;
2188 if (object
== VM_OBJECT_NULL
)
2189 return(KERN_SUCCESS
);
2192 * Remove the pager association immediately.
2194 * This will prevent the memory manager from further
2195 * meddling. [If it wanted to flush data or make
2196 * other changes, it should have done so before performing
2197 * the destroy call.]
2200 vm_object_lock(object
);
2201 object
->can_persist
= FALSE
;
2202 object
->named
= FALSE
;
2203 object
->alive
= FALSE
;
2205 if (object
->hashed
) {
2208 * Rip out the pager from the vm_object now...
2210 lck
= vm_object_hash_lock_spin(object
->pager
);
2211 vm_object_remove(object
);
2212 vm_object_hash_unlock(lck
);
2214 old_pager
= object
->pager
;
2215 object
->pager
= MEMORY_OBJECT_NULL
;
2216 if (old_pager
!= MEMORY_OBJECT_NULL
)
2217 memory_object_control_disable(object
->pager_control
);
2220 * Wait for the existing paging activity (that got
2221 * through before we nulled out the pager) to subside.
2224 vm_object_paging_wait(object
, THREAD_UNINT
);
2225 vm_object_unlock(object
);
2228 * Terminate the object now.
2230 if (old_pager
!= MEMORY_OBJECT_NULL
) {
2231 vm_object_release_pager(old_pager
, object
->hashed
);
2234 * JMM - Release the caller's reference. This assumes the
2235 * caller had a reference to release, which is a big (but
2236 * currently valid) assumption if this is driven from the
2237 * vnode pager (it is holding a named reference when making
2240 vm_object_deallocate(object
);
2243 return(KERN_SUCCESS
);
2249 #define VM_OBJ_DEACT_ALL_STATS DEBUG
2250 #if VM_OBJ_DEACT_ALL_STATS
2251 uint32_t vm_object_deactivate_all_pages_batches
= 0;
2252 uint32_t vm_object_deactivate_all_pages_pages
= 0;
2253 #endif /* VM_OBJ_DEACT_ALL_STATS */
2255 * vm_object_deactivate_all_pages
2257 * Deactivate all pages in the specified object. (Keep its pages
2258 * in memory even though it is no longer referenced.)
2260 * The object must be locked.
2263 vm_object_deactivate_all_pages(
2264 register vm_object_t object
)
2266 register vm_page_t p
;
2268 #if VM_OBJ_DEACT_ALL_STATS
2270 #endif /* VM_OBJ_DEACT_ALL_STATS */
2271 #define V_O_D_A_P_MAX_BATCH 256
2273 loop_count
= BATCH_LIMIT(V_O_D_A_P_MAX_BATCH
);
2274 #if VM_OBJ_DEACT_ALL_STATS
2276 #endif /* VM_OBJ_DEACT_ALL_STATS */
2277 vm_page_lock_queues();
2278 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2279 if (--loop_count
== 0) {
2280 #if VM_OBJ_DEACT_ALL_STATS
2281 hw_atomic_add(&vm_object_deactivate_all_pages_batches
,
2283 hw_atomic_add(&vm_object_deactivate_all_pages_pages
,
2286 #endif /* VM_OBJ_DEACT_ALL_STATS */
2287 lck_mtx_yield(&vm_page_queue_lock
);
2288 loop_count
= BATCH_LIMIT(V_O_D_A_P_MAX_BATCH
);
2290 if (!p
->busy
&& !p
->throttled
) {
2291 #if VM_OBJ_DEACT_ALL_STATS
2293 #endif /* VM_OBJ_DEACT_ALL_STATS */
2294 vm_page_deactivate(p
);
2297 #if VM_OBJ_DEACT_ALL_STATS
2299 hw_atomic_add(&vm_object_deactivate_all_pages_batches
, 1);
2300 hw_atomic_add(&vm_object_deactivate_all_pages_pages
,
2304 #endif /* VM_OBJ_DEACT_ALL_STATS */
2305 vm_page_unlock_queues();
2307 #endif /* VM_OBJECT_CACHE */
2312 * The "chunk" macros are used by routines below when looking for pages to deactivate. These
2313 * exist because of the need to handle shadow chains. When deactivating pages, we only
2314 * want to deactive the ones at the top most level in the object chain. In order to do
2315 * this efficiently, the specified address range is divided up into "chunks" and we use
2316 * a bit map to keep track of which pages have already been processed as we descend down
2317 * the shadow chain. These chunk macros hide the details of the bit map implementation
2318 * as much as we can.
2320 * For convenience, we use a 64-bit data type as the bit map, and therefore a chunk is
2321 * set to 64 pages. The bit map is indexed from the low-order end, so that the lowest
2322 * order bit represents page 0 in the current range and highest order bit represents
2325 * For further convenience, we also use negative logic for the page state in the bit map.
2326 * The bit is set to 1 to indicate it has not yet been seen, and to 0 to indicate it has
2327 * been processed. This way we can simply test the 64-bit long word to see if it's zero
2328 * to easily tell if the whole range has been processed. Therefore, the bit map starts
2329 * out with all the bits set. The macros below hide all these details from the caller.
2332 #define PAGES_IN_A_CHUNK 64 /* The number of pages in the chunk must */
2333 /* be the same as the number of bits in */
2334 /* the chunk_state_t type. We use 64 */
2335 /* just for convenience. */
2337 #define CHUNK_SIZE (PAGES_IN_A_CHUNK * PAGE_SIZE_64) /* Size of a chunk in bytes */
2339 typedef uint64_t chunk_state_t
;
2342 * The bit map uses negative logic, so we start out with all 64 bits set to indicate
2343 * that no pages have been processed yet. Also, if len is less than the full CHUNK_SIZE,
2344 * then we mark pages beyond the len as having been "processed" so that we don't waste time
2345 * looking at pages in that range. This can save us from unnecessarily chasing down the
2349 #define CHUNK_INIT(c, len) \
2353 (c) = 0xffffffffffffffffLL; \
2355 for (p = (len) / PAGE_SIZE_64; p < PAGES_IN_A_CHUNK; p++) \
2356 MARK_PAGE_HANDLED(c, p); \
2361 * Return true if all pages in the chunk have not yet been processed.
2364 #define CHUNK_NOT_COMPLETE(c) ((c) != 0)
2367 * Return true if the page at offset 'p' in the bit map has already been handled
2368 * while processing a higher level object in the shadow chain.
2371 #define PAGE_ALREADY_HANDLED(c, p) (((c) & (1LL << (p))) == 0)
2374 * Mark the page at offset 'p' in the bit map as having been processed.
2377 #define MARK_PAGE_HANDLED(c, p) \
2379 (c) = (c) & ~(1LL << (p)); \
2384 * Return true if the page at the given offset has been paged out. Object is
2385 * locked upon entry and returned locked.
2391 vm_object_offset_t offset
)
2394 memory_object_t pager
;
2397 * Check the existence map for the page if we have one, otherwise
2398 * ask the pager about this page.
2402 if (object
->existence_map
) {
2403 if (vm_external_state_get(object
->existence_map
, offset
)
2404 == VM_EXTERNAL_STATE_EXISTS
) {
2413 if (object
->internal
&&
2415 !object
->terminating
&&
2416 object
->pager_ready
) {
2419 * We're already holding a "paging in progress" reference
2420 * so the object can't disappear when we release the lock.
2423 assert(object
->paging_in_progress
);
2424 pager
= object
->pager
;
2425 vm_object_unlock(object
);
2427 kr
= memory_object_data_request(
2429 offset
+ object
->paging_offset
,
2430 0, /* just poke the pager */
2434 vm_object_lock(object
);
2436 if (kr
== KERN_SUCCESS
) {
2452 * Deactivate the pages in the specified object and range. If kill_page is set, also discard any
2453 * page modified state from the pmap. Update the chunk_state as we go along. The caller must specify
2454 * a size that is less than or equal to the CHUNK_SIZE.
2458 deactivate_pages_in_object(
2460 vm_object_offset_t offset
,
2461 vm_object_size_t size
,
2462 boolean_t kill_page
,
2463 boolean_t reusable_page
,
2467 boolean_t all_reusable
,
2468 chunk_state_t
*chunk_state
)
2472 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
2473 struct vm_page_delayed_work
*dwp
;
2476 unsigned int reusable
= 0;
2480 * Examine each page in the chunk. The variable 'p' is the page number relative to the start of the
2481 * chunk. Since this routine is called once for each level in the shadow chain, the chunk_state may
2482 * have pages marked as having been processed already. We stop the loop early if we find we've handled
2483 * all the pages in the chunk.
2488 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
2490 for(p
= 0; size
&& CHUNK_NOT_COMPLETE(*chunk_state
); p
++, size
-= PAGE_SIZE_64
, offset
+= PAGE_SIZE_64
) {
2493 * If this offset has already been found and handled in a higher level object, then don't
2494 * do anything with it in the current shadow object.
2497 if (PAGE_ALREADY_HANDLED(*chunk_state
, p
))
2501 * See if the page at this offset is around. First check to see if the page is resident,
2502 * then if not, check the existence map or with the pager.
2505 if ((m
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
2508 * We found a page we were looking for. Mark it as "handled" now in the chunk_state
2509 * so that we won't bother looking for a page at this offset again if there are more
2510 * shadow objects. Then deactivate the page.
2513 MARK_PAGE_HANDLED(*chunk_state
, p
);
2515 if (( !VM_PAGE_WIRED(m
)) && (!m
->private) && (!m
->gobbled
) && (!m
->busy
) && (!m
->laundry
)) {
2518 clear_refmod
= VM_MEM_REFERENCED
;
2519 dwp
->dw_mask
= DW_clear_reference
;
2521 if ((kill_page
) && (object
->internal
)) {
2522 m
->precious
= FALSE
;
2525 clear_refmod
|= VM_MEM_MODIFIED
;
2528 * This page is now clean and
2529 * reclaimable. Move it out
2530 * of the throttled queue, so
2531 * that vm_pageout_scan() can
2534 dwp
->dw_mask
|= DW_move_page
;
2537 vm_external_state_clr(object
->existence_map
, offset
);
2538 #endif /* MACH_PAGEMAP */
2540 if (reusable_page
&& !m
->reusable
) {
2541 assert(!all_reusable
);
2542 assert(!object
->all_reusable
);
2544 object
->reusable_page_count
++;
2545 assert(object
->resident_page_count
>= object
->reusable_page_count
);
2549 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
2551 if (!m
->throttled
&& !(reusable_page
|| all_reusable
))
2552 dwp
->dw_mask
|= DW_move_page
;
2554 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
, dw_count
);
2556 if (dw_count
>= dw_limit
) {
2558 OSAddAtomic(reusable
,
2559 &vm_page_stats_reusable
.reusable_count
);
2560 vm_page_stats_reusable
.reusable
+= reusable
;
2563 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
2573 * The page at this offset isn't memory resident, check to see if it's
2574 * been paged out. If so, mark it as handled so we don't bother looking
2575 * for it in the shadow chain.
2578 if (page_is_paged_out(object
, offset
)) {
2579 MARK_PAGE_HANDLED(*chunk_state
, p
);
2582 * If we're killing a non-resident page, then clear the page in the existence
2583 * map so we don't bother paging it back in if it's touched again in the future.
2586 if ((kill_page
) && (object
->internal
)) {
2588 vm_external_state_clr(object
->existence_map
, offset
);
2589 #endif /* MACH_PAGEMAP */
2596 OSAddAtomic(reusable
, &vm_page_stats_reusable
.reusable_count
);
2597 vm_page_stats_reusable
.reusable
+= reusable
;
2602 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
2607 * Deactive a "chunk" of the given range of the object starting at offset. A "chunk"
2608 * will always be less than or equal to the given size. The total range is divided up
2609 * into chunks for efficiency and performance related to the locks and handling the shadow
2610 * chain. This routine returns how much of the given "size" it actually processed. It's
2611 * up to the caler to loop and keep calling this routine until the entire range they want
2612 * to process has been done.
2615 static vm_object_size_t
2617 vm_object_t orig_object
,
2618 vm_object_offset_t offset
,
2619 vm_object_size_t size
,
2620 boolean_t kill_page
,
2621 boolean_t reusable_page
,
2622 boolean_t all_reusable
)
2625 vm_object_t tmp_object
;
2626 vm_object_size_t length
;
2627 chunk_state_t chunk_state
;
2631 * Get set to do a chunk. We'll do up to CHUNK_SIZE, but no more than the
2632 * remaining size the caller asked for.
2635 length
= MIN(size
, CHUNK_SIZE
);
2638 * The chunk_state keeps track of which pages we've already processed if there's
2639 * a shadow chain on this object. At this point, we haven't done anything with this
2640 * range of pages yet, so initialize the state to indicate no pages processed yet.
2643 CHUNK_INIT(chunk_state
, length
);
2644 object
= orig_object
;
2647 * Start at the top level object and iterate around the loop once for each object
2648 * in the shadow chain. We stop processing early if we've already found all the pages
2649 * in the range. Otherwise we stop when we run out of shadow objects.
2652 while (object
&& CHUNK_NOT_COMPLETE(chunk_state
)) {
2653 vm_object_paging_begin(object
);
2655 deactivate_pages_in_object(object
, offset
, length
, kill_page
, reusable_page
, all_reusable
, &chunk_state
);
2657 vm_object_paging_end(object
);
2660 * We've finished with this object, see if there's a shadow object. If
2661 * there is, update the offset and lock the new object. We also turn off
2662 * kill_page at this point since we only kill pages in the top most object.
2665 tmp_object
= object
->shadow
;
2669 reusable_page
= FALSE
;
2670 all_reusable
= FALSE
;
2671 offset
+= object
->vo_shadow_offset
;
2672 vm_object_lock(tmp_object
);
2675 if (object
!= orig_object
)
2676 vm_object_unlock(object
);
2678 object
= tmp_object
;
2681 if (object
&& object
!= orig_object
)
2682 vm_object_unlock(object
);
2690 * Move any resident pages in the specified range to the inactive queue. If kill_page is set,
2691 * we also clear the modified status of the page and "forget" any changes that have been made
2695 __private_extern__
void
2696 vm_object_deactivate_pages(
2698 vm_object_offset_t offset
,
2699 vm_object_size_t size
,
2700 boolean_t kill_page
,
2701 boolean_t reusable_page
)
2703 vm_object_size_t length
;
2704 boolean_t all_reusable
;
2707 * We break the range up into chunks and do one chunk at a time. This is for
2708 * efficiency and performance while handling the shadow chains and the locks.
2709 * The deactivate_a_chunk() function returns how much of the range it processed.
2710 * We keep calling this routine until the given size is exhausted.
2714 all_reusable
= FALSE
;
2715 if (reusable_page
&&
2717 object
->vo_size
!= 0 &&
2718 object
->vo_size
== size
&&
2719 object
->reusable_page_count
== 0) {
2720 all_reusable
= TRUE
;
2721 reusable_page
= FALSE
;
2724 if ((reusable_page
|| all_reusable
) && object
->all_reusable
) {
2725 /* This means MADV_FREE_REUSABLE has been called twice, which
2726 * is probably illegal. */
2731 length
= deactivate_a_chunk(object
, offset
, size
, kill_page
, reusable_page
, all_reusable
);
2738 if (!object
->all_reusable
) {
2739 unsigned int reusable
;
2741 object
->all_reusable
= TRUE
;
2742 assert(object
->reusable_page_count
== 0);
2743 /* update global stats */
2744 reusable
= object
->resident_page_count
;
2745 OSAddAtomic(reusable
,
2746 &vm_page_stats_reusable
.reusable_count
);
2747 vm_page_stats_reusable
.reusable
+= reusable
;
2748 vm_page_stats_reusable
.all_reusable_calls
++;
2750 } else if (reusable_page
) {
2751 vm_page_stats_reusable
.partial_reusable_calls
++;
2756 vm_object_reuse_pages(
2758 vm_object_offset_t start_offset
,
2759 vm_object_offset_t end_offset
,
2760 boolean_t allow_partial_reuse
)
2762 vm_object_offset_t cur_offset
;
2764 unsigned int reused
, reusable
;
2766 #define VM_OBJECT_REUSE_PAGE(object, m, reused) \
2768 if ((m) != VM_PAGE_NULL && \
2770 assert((object)->reusable_page_count <= \
2771 (object)->resident_page_count); \
2772 assert((object)->reusable_page_count > 0); \
2773 (object)->reusable_page_count--; \
2774 (m)->reusable = FALSE; \
2782 vm_object_lock_assert_exclusive(object
);
2784 if (object
->all_reusable
) {
2785 assert(object
->reusable_page_count
== 0);
2786 object
->all_reusable
= FALSE
;
2787 if (end_offset
- start_offset
== object
->vo_size
||
2788 !allow_partial_reuse
) {
2789 vm_page_stats_reusable
.all_reuse_calls
++;
2790 reused
= object
->resident_page_count
;
2792 vm_page_stats_reusable
.partial_reuse_calls
++;
2793 queue_iterate(&object
->memq
, m
, vm_page_t
, listq
) {
2794 if (m
->offset
< start_offset
||
2795 m
->offset
>= end_offset
) {
2797 object
->reusable_page_count
++;
2798 assert(object
->resident_page_count
>= object
->reusable_page_count
);
2801 assert(!m
->reusable
);
2806 } else if (object
->resident_page_count
>
2807 ((end_offset
- start_offset
) >> PAGE_SHIFT
)) {
2808 vm_page_stats_reusable
.partial_reuse_calls
++;
2809 for (cur_offset
= start_offset
;
2810 cur_offset
< end_offset
;
2811 cur_offset
+= PAGE_SIZE_64
) {
2812 if (object
->reusable_page_count
== 0) {
2815 m
= vm_page_lookup(object
, cur_offset
);
2816 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
2819 vm_page_stats_reusable
.partial_reuse_calls
++;
2820 queue_iterate(&object
->memq
, m
, vm_page_t
, listq
) {
2821 if (object
->reusable_page_count
== 0) {
2824 if (m
->offset
< start_offset
||
2825 m
->offset
>= end_offset
) {
2828 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
2832 /* update global stats */
2833 OSAddAtomic(reusable
-reused
, &vm_page_stats_reusable
.reusable_count
);
2834 vm_page_stats_reusable
.reused
+= reused
;
2835 vm_page_stats_reusable
.reusable
+= reusable
;
2839 * Routine: vm_object_pmap_protect
2842 * Reduces the permission for all physical
2843 * pages in the specified object range.
2845 * If removing write permission only, it is
2846 * sufficient to protect only the pages in
2847 * the top-level object; only those pages may
2848 * have write permission.
2850 * If removing all access, we must follow the
2851 * shadow chain from the top-level object to
2852 * remove access to all pages in shadowed objects.
2854 * The object must *not* be locked. The object must
2855 * be temporary/internal.
2857 * If pmap is not NULL, this routine assumes that
2858 * the only mappings for the pages are in that
2862 __private_extern__
void
2863 vm_object_pmap_protect(
2864 register vm_object_t object
,
2865 register vm_object_offset_t offset
,
2866 vm_object_size_t size
,
2868 vm_map_offset_t pmap_start
,
2871 if (object
== VM_OBJECT_NULL
)
2873 size
= vm_object_round_page(size
);
2874 offset
= vm_object_trunc_page(offset
);
2876 vm_object_lock(object
);
2878 if (object
->phys_contiguous
) {
2880 vm_object_unlock(object
);
2881 pmap_protect(pmap
, pmap_start
, pmap_start
+ size
, prot
);
2883 vm_object_offset_t phys_start
, phys_end
, phys_addr
;
2885 phys_start
= object
->vo_shadow_offset
+ offset
;
2886 phys_end
= phys_start
+ size
;
2887 assert(phys_start
<= phys_end
);
2888 assert(phys_end
<= object
->vo_shadow_offset
+ object
->vo_size
);
2889 vm_object_unlock(object
);
2891 for (phys_addr
= phys_start
;
2892 phys_addr
< phys_end
;
2893 phys_addr
+= PAGE_SIZE_64
) {
2894 pmap_page_protect((ppnum_t
) (phys_addr
>> PAGE_SHIFT
), prot
);
2900 assert(object
->internal
);
2903 if (ptoa_64(object
->resident_page_count
) > size
/2 && pmap
!= PMAP_NULL
) {
2904 vm_object_unlock(object
);
2905 pmap_protect(pmap
, pmap_start
, pmap_start
+ size
, prot
);
2909 /* if we are doing large ranges with respect to resident */
2910 /* page count then we should interate over pages otherwise */
2911 /* inverse page look-up will be faster */
2912 if (ptoa_64(object
->resident_page_count
/ 4) < size
) {
2914 vm_object_offset_t end
;
2916 end
= offset
+ size
;
2918 if (pmap
!= PMAP_NULL
) {
2919 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2920 if (!p
->fictitious
&&
2921 (offset
<= p
->offset
) && (p
->offset
< end
)) {
2922 vm_map_offset_t start
;
2924 start
= pmap_start
+ p
->offset
- offset
;
2925 pmap_protect(pmap
, start
, start
+ PAGE_SIZE_64
, prot
);
2929 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2930 if (!p
->fictitious
&&
2931 (offset
<= p
->offset
) && (p
->offset
< end
)) {
2933 pmap_page_protect(p
->phys_page
, prot
);
2939 vm_object_offset_t end
;
2940 vm_object_offset_t target_off
;
2942 end
= offset
+ size
;
2944 if (pmap
!= PMAP_NULL
) {
2945 for(target_off
= offset
;
2947 target_off
+= PAGE_SIZE
) {
2948 p
= vm_page_lookup(object
, target_off
);
2949 if (p
!= VM_PAGE_NULL
) {
2950 vm_object_offset_t start
;
2951 start
= pmap_start
+
2952 (p
->offset
- offset
);
2953 pmap_protect(pmap
, start
,
2954 start
+ PAGE_SIZE
, prot
);
2958 for(target_off
= offset
;
2959 target_off
< end
; target_off
+= PAGE_SIZE
) {
2960 p
= vm_page_lookup(object
, target_off
);
2961 if (p
!= VM_PAGE_NULL
) {
2962 pmap_page_protect(p
->phys_page
, prot
);
2968 if (prot
== VM_PROT_NONE
) {
2970 * Must follow shadow chain to remove access
2971 * to pages in shadowed objects.
2973 register vm_object_t next_object
;
2975 next_object
= object
->shadow
;
2976 if (next_object
!= VM_OBJECT_NULL
) {
2977 offset
+= object
->vo_shadow_offset
;
2978 vm_object_lock(next_object
);
2979 vm_object_unlock(object
);
2980 object
= next_object
;
2984 * End of chain - we are done.
2991 * Pages in shadowed objects may never have
2992 * write permission - we may stop here.
2998 vm_object_unlock(object
);
3002 * Routine: vm_object_copy_slowly
3005 * Copy the specified range of the source
3006 * virtual memory object without using
3007 * protection-based optimizations (such
3008 * as copy-on-write). The pages in the
3009 * region are actually copied.
3011 * In/out conditions:
3012 * The caller must hold a reference and a lock
3013 * for the source virtual memory object. The source
3014 * object will be returned *unlocked*.
3017 * If the copy is completed successfully, KERN_SUCCESS is
3018 * returned. If the caller asserted the interruptible
3019 * argument, and an interruption occurred while waiting
3020 * for a user-generated event, MACH_SEND_INTERRUPTED is
3021 * returned. Other values may be returned to indicate
3022 * hard errors during the copy operation.
3024 * A new virtual memory object is returned in a
3025 * parameter (_result_object). The contents of this
3026 * new object, starting at a zero offset, are a copy
3027 * of the source memory region. In the event of
3028 * an error, this parameter will contain the value
3031 __private_extern__ kern_return_t
3032 vm_object_copy_slowly(
3033 register vm_object_t src_object
,
3034 vm_object_offset_t src_offset
,
3035 vm_object_size_t size
,
3036 boolean_t interruptible
,
3037 vm_object_t
*_result_object
) /* OUT */
3039 vm_object_t new_object
;
3040 vm_object_offset_t new_offset
;
3042 struct vm_object_fault_info fault_info
;
3044 XPR(XPR_VM_OBJECT
, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
3045 src_object
, src_offset
, size
, 0, 0);
3048 vm_object_unlock(src_object
);
3049 *_result_object
= VM_OBJECT_NULL
;
3050 return(KERN_INVALID_ARGUMENT
);
3054 * Prevent destruction of the source object while we copy.
3057 vm_object_reference_locked(src_object
);
3058 vm_object_unlock(src_object
);
3061 * Create a new object to hold the copied pages.
3063 * We fill the new object starting at offset 0,
3064 * regardless of the input offset.
3065 * We don't bother to lock the new object within
3066 * this routine, since we have the only reference.
3069 new_object
= vm_object_allocate(size
);
3072 assert(size
== trunc_page_64(size
)); /* Will the loop terminate? */
3074 fault_info
.interruptible
= interruptible
;
3075 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
3076 fault_info
.user_tag
= 0;
3077 fault_info
.lo_offset
= src_offset
;
3078 fault_info
.hi_offset
= src_offset
+ size
;
3079 fault_info
.no_cache
= FALSE
;
3080 fault_info
.stealth
= TRUE
;
3081 fault_info
.io_sync
= FALSE
;
3082 fault_info
.cs_bypass
= FALSE
;
3083 fault_info
.mark_zf_absent
= FALSE
;
3084 fault_info
.batch_pmap_op
= FALSE
;
3088 src_offset
+= PAGE_SIZE_64
,
3089 new_offset
+= PAGE_SIZE_64
, size
-= PAGE_SIZE_64
3092 vm_fault_return_t result
;
3094 vm_object_lock(new_object
);
3096 while ((new_page
= vm_page_alloc(new_object
, new_offset
))
3099 vm_object_unlock(new_object
);
3101 if (!vm_page_wait(interruptible
)) {
3102 vm_object_deallocate(new_object
);
3103 vm_object_deallocate(src_object
);
3104 *_result_object
= VM_OBJECT_NULL
;
3105 return(MACH_SEND_INTERRUPTED
);
3107 vm_object_lock(new_object
);
3109 vm_object_unlock(new_object
);
3112 vm_prot_t prot
= VM_PROT_READ
;
3113 vm_page_t _result_page
;
3116 vm_page_t result_page
;
3117 kern_return_t error_code
;
3119 vm_object_lock(src_object
);
3120 vm_object_paging_begin(src_object
);
3122 if (size
> (vm_size_t
) -1) {
3123 /* 32-bit overflow */
3124 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
3126 fault_info
.cluster_size
= (vm_size_t
) size
;
3127 assert(fault_info
.cluster_size
== size
);
3130 XPR(XPR_VM_FAULT
,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
3131 result
= vm_fault_page(src_object
, src_offset
,
3132 VM_PROT_READ
, FALSE
,
3133 &prot
, &_result_page
, &top_page
,
3135 &error_code
, FALSE
, FALSE
, &fault_info
);
3138 case VM_FAULT_SUCCESS
:
3139 result_page
= _result_page
;
3142 * Copy the page to the new object.
3145 * If result_page is clean,
3146 * we could steal it instead
3150 vm_page_copy(result_page
, new_page
);
3151 vm_object_unlock(result_page
->object
);
3154 * Let go of both pages (make them
3155 * not busy, perform wakeup, activate).
3157 vm_object_lock(new_object
);
3158 SET_PAGE_DIRTY(new_page
, FALSE
);
3159 PAGE_WAKEUP_DONE(new_page
);
3160 vm_object_unlock(new_object
);
3162 vm_object_lock(result_page
->object
);
3163 PAGE_WAKEUP_DONE(result_page
);
3165 vm_page_lockspin_queues();
3166 if (!result_page
->active
&&
3167 !result_page
->inactive
&&
3168 !result_page
->throttled
)
3169 vm_page_activate(result_page
);
3170 vm_page_activate(new_page
);
3171 vm_page_unlock_queues();
3174 * Release paging references and
3175 * top-level placeholder page, if any.
3178 vm_fault_cleanup(result_page
->object
,
3183 case VM_FAULT_RETRY
:
3186 case VM_FAULT_MEMORY_SHORTAGE
:
3187 if (vm_page_wait(interruptible
))
3191 case VM_FAULT_INTERRUPTED
:
3192 vm_object_lock(new_object
);
3193 VM_PAGE_FREE(new_page
);
3194 vm_object_unlock(new_object
);
3196 vm_object_deallocate(new_object
);
3197 vm_object_deallocate(src_object
);
3198 *_result_object
= VM_OBJECT_NULL
;
3199 return(MACH_SEND_INTERRUPTED
);
3201 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
3202 /* success but no VM page: fail */
3203 vm_object_paging_end(src_object
);
3204 vm_object_unlock(src_object
);
3206 case VM_FAULT_MEMORY_ERROR
:
3209 * (a) ignore pages that we can't
3211 * (b) return the null object if
3212 * any page fails [chosen]
3215 vm_object_lock(new_object
);
3216 VM_PAGE_FREE(new_page
);
3217 vm_object_unlock(new_object
);
3219 vm_object_deallocate(new_object
);
3220 vm_object_deallocate(src_object
);
3221 *_result_object
= VM_OBJECT_NULL
;
3222 return(error_code
? error_code
:
3226 panic("vm_object_copy_slowly: unexpected error"
3227 " 0x%x from vm_fault_page()\n", result
);
3229 } while (result
!= VM_FAULT_SUCCESS
);
3233 * Lose the extra reference, and return our object.
3235 vm_object_deallocate(src_object
);
3236 *_result_object
= new_object
;
3237 return(KERN_SUCCESS
);
3241 * Routine: vm_object_copy_quickly
3244 * Copy the specified range of the source virtual
3245 * memory object, if it can be done without waiting
3246 * for user-generated events.
3249 * If the copy is successful, the copy is returned in
3250 * the arguments; otherwise, the arguments are not
3253 * In/out conditions:
3254 * The object should be unlocked on entry and exit.
3258 __private_extern__ boolean_t
3259 vm_object_copy_quickly(
3260 vm_object_t
*_object
, /* INOUT */
3261 __unused vm_object_offset_t offset
, /* IN */
3262 __unused vm_object_size_t size
, /* IN */
3263 boolean_t
*_src_needs_copy
, /* OUT */
3264 boolean_t
*_dst_needs_copy
) /* OUT */
3266 vm_object_t object
= *_object
;
3267 memory_object_copy_strategy_t copy_strategy
;
3269 XPR(XPR_VM_OBJECT
, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
3270 *_object
, offset
, size
, 0, 0);
3271 if (object
== VM_OBJECT_NULL
) {
3272 *_src_needs_copy
= FALSE
;
3273 *_dst_needs_copy
= FALSE
;
3277 vm_object_lock(object
);
3279 copy_strategy
= object
->copy_strategy
;
3281 switch (copy_strategy
) {
3282 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3285 * Symmetric copy strategy.
3286 * Make another reference to the object.
3287 * Leave object/offset unchanged.
3290 vm_object_reference_locked(object
);
3291 object
->shadowed
= TRUE
;
3292 vm_object_unlock(object
);
3295 * Both source and destination must make
3296 * shadows, and the source must be made
3297 * read-only if not already.
3300 *_src_needs_copy
= TRUE
;
3301 *_dst_needs_copy
= TRUE
;
3305 case MEMORY_OBJECT_COPY_DELAY
:
3306 vm_object_unlock(object
);
3310 vm_object_unlock(object
);
3316 static int copy_call_count
= 0;
3317 static int copy_call_sleep_count
= 0;
3318 static int copy_call_restart_count
= 0;
3321 * Routine: vm_object_copy_call [internal]
3324 * Copy the source object (src_object), using the
3325 * user-managed copy algorithm.
3327 * In/out conditions:
3328 * The source object must be locked on entry. It
3329 * will be *unlocked* on exit.
3332 * If the copy is successful, KERN_SUCCESS is returned.
3333 * A new object that represents the copied virtual
3334 * memory is returned in a parameter (*_result_object).
3335 * If the return value indicates an error, this parameter
3338 static kern_return_t
3339 vm_object_copy_call(
3340 vm_object_t src_object
,
3341 vm_object_offset_t src_offset
,
3342 vm_object_size_t size
,
3343 vm_object_t
*_result_object
) /* OUT */
3347 boolean_t check_ready
= FALSE
;
3348 uint32_t try_failed_count
= 0;
3351 * If a copy is already in progress, wait and retry.
3354 * Consider making this call interruptable, as Mike
3355 * intended it to be.
3358 * Need a counter or version or something to allow
3359 * us to use the copy that the currently requesting
3360 * thread is obtaining -- is it worth adding to the
3361 * vm object structure? Depends how common this case it.
3364 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3365 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3367 copy_call_restart_count
++;
3371 * Indicate (for the benefit of memory_object_create_copy)
3372 * that we want a copy for src_object. (Note that we cannot
3373 * do a real assert_wait before calling memory_object_copy,
3374 * so we simply set the flag.)
3377 vm_object_set_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
);
3378 vm_object_unlock(src_object
);
3381 * Ask the memory manager to give us a memory object
3382 * which represents a copy of the src object.
3383 * The memory manager may give us a memory object
3384 * which we already have, or it may give us a
3385 * new memory object. This memory object will arrive
3386 * via memory_object_create_copy.
3389 kr
= KERN_FAILURE
; /* XXX need to change memory_object.defs */
3390 if (kr
!= KERN_SUCCESS
) {
3395 * Wait for the copy to arrive.
3397 vm_object_lock(src_object
);
3398 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3399 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3401 copy_call_sleep_count
++;
3404 assert(src_object
->copy
!= VM_OBJECT_NULL
);
3405 copy
= src_object
->copy
;
3406 if (!vm_object_lock_try(copy
)) {
3407 vm_object_unlock(src_object
);
3410 mutex_pause(try_failed_count
); /* wait a bit */
3412 vm_object_lock(src_object
);
3415 if (copy
->vo_size
< src_offset
+size
)
3416 copy
->vo_size
= src_offset
+size
;
3418 if (!copy
->pager_ready
)
3424 *_result_object
= copy
;
3425 vm_object_unlock(copy
);
3426 vm_object_unlock(src_object
);
3428 /* Wait for the copy to be ready. */
3429 if (check_ready
== TRUE
) {
3430 vm_object_lock(copy
);
3431 while (!copy
->pager_ready
) {
3432 vm_object_sleep(copy
, VM_OBJECT_EVENT_PAGER_READY
, THREAD_UNINT
);
3434 vm_object_unlock(copy
);
3437 return KERN_SUCCESS
;
3440 static int copy_delayed_lock_collisions
= 0;
3441 static int copy_delayed_max_collisions
= 0;
3442 static int copy_delayed_lock_contention
= 0;
3443 static int copy_delayed_protect_iterate
= 0;
3446 * Routine: vm_object_copy_delayed [internal]
3449 * Copy the specified virtual memory object, using
3450 * the asymmetric copy-on-write algorithm.
3452 * In/out conditions:
3453 * The src_object must be locked on entry. It will be unlocked
3454 * on exit - so the caller must also hold a reference to it.
3456 * This routine will not block waiting for user-generated
3457 * events. It is not interruptible.
3459 __private_extern__ vm_object_t
3460 vm_object_copy_delayed(
3461 vm_object_t src_object
,
3462 vm_object_offset_t src_offset
,
3463 vm_object_size_t size
,
3464 boolean_t src_object_shared
)
3466 vm_object_t new_copy
= VM_OBJECT_NULL
;
3467 vm_object_t old_copy
;
3469 vm_object_size_t copy_size
= src_offset
+ size
;
3474 * The user-level memory manager wants to see all of the changes
3475 * to this object, but it has promised not to make any changes on
3478 * Perform an asymmetric copy-on-write, as follows:
3479 * Create a new object, called a "copy object" to hold
3480 * pages modified by the new mapping (i.e., the copy,
3481 * not the original mapping).
3482 * Record the original object as the backing object for
3483 * the copy object. If the original mapping does not
3484 * change a page, it may be used read-only by the copy.
3485 * Record the copy object in the original object.
3486 * When the original mapping causes a page to be modified,
3487 * it must be copied to a new page that is "pushed" to
3489 * Mark the new mapping (the copy object) copy-on-write.
3490 * This makes the copy object itself read-only, allowing
3491 * it to be reused if the original mapping makes no
3492 * changes, and simplifying the synchronization required
3493 * in the "push" operation described above.
3495 * The copy-on-write is said to be assymetric because the original
3496 * object is *not* marked copy-on-write. A copied page is pushed
3497 * to the copy object, regardless which party attempted to modify
3500 * Repeated asymmetric copy operations may be done. If the
3501 * original object has not been changed since the last copy, its
3502 * copy object can be reused. Otherwise, a new copy object can be
3503 * inserted between the original object and its previous copy
3504 * object. Since any copy object is read-only, this cannot affect
3505 * affect the contents of the previous copy object.
3507 * Note that a copy object is higher in the object tree than the
3508 * original object; therefore, use of the copy object recorded in
3509 * the original object must be done carefully, to avoid deadlock.
3515 * Wait for paging in progress.
3517 if (!src_object
->true_share
&&
3518 (src_object
->paging_in_progress
!= 0 ||
3519 src_object
->activity_in_progress
!= 0)) {
3520 if (src_object_shared
== TRUE
) {
3521 vm_object_unlock(src_object
);
3522 vm_object_lock(src_object
);
3523 src_object_shared
= FALSE
;
3526 vm_object_paging_wait(src_object
, THREAD_UNINT
);
3529 * See whether we can reuse the result of a previous
3533 old_copy
= src_object
->copy
;
3534 if (old_copy
!= VM_OBJECT_NULL
) {
3538 * Try to get the locks (out of order)
3540 if (src_object_shared
== TRUE
)
3541 lock_granted
= vm_object_lock_try_shared(old_copy
);
3543 lock_granted
= vm_object_lock_try(old_copy
);
3545 if (!lock_granted
) {
3546 vm_object_unlock(src_object
);
3548 if (collisions
++ == 0)
3549 copy_delayed_lock_contention
++;
3550 mutex_pause(collisions
);
3552 /* Heisenberg Rules */
3553 copy_delayed_lock_collisions
++;
3555 if (collisions
> copy_delayed_max_collisions
)
3556 copy_delayed_max_collisions
= collisions
;
3558 if (src_object_shared
== TRUE
)
3559 vm_object_lock_shared(src_object
);
3561 vm_object_lock(src_object
);
3567 * Determine whether the old copy object has
3571 if (old_copy
->resident_page_count
== 0 &&
3572 !old_copy
->pager_created
) {
3574 * It has not been modified.
3576 * Return another reference to
3577 * the existing copy-object if
3578 * we can safely grow it (if
3582 if (old_copy
->vo_size
< copy_size
) {
3583 if (src_object_shared
== TRUE
) {
3584 vm_object_unlock(old_copy
);
3585 vm_object_unlock(src_object
);
3587 vm_object_lock(src_object
);
3588 src_object_shared
= FALSE
;
3592 * We can't perform a delayed copy if any of the
3593 * pages in the extended range are wired (because
3594 * we can't safely take write permission away from
3595 * wired pages). If the pages aren't wired, then
3596 * go ahead and protect them.
3598 copy_delayed_protect_iterate
++;
3600 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
3601 if (!p
->fictitious
&&
3602 p
->offset
>= old_copy
->vo_size
&&
3603 p
->offset
< copy_size
) {
3604 if (VM_PAGE_WIRED(p
)) {
3605 vm_object_unlock(old_copy
);
3606 vm_object_unlock(src_object
);
3608 if (new_copy
!= VM_OBJECT_NULL
) {
3609 vm_object_unlock(new_copy
);
3610 vm_object_deallocate(new_copy
);
3613 return VM_OBJECT_NULL
;
3615 pmap_page_protect(p
->phys_page
,
3616 (VM_PROT_ALL
& ~VM_PROT_WRITE
));
3620 old_copy
->vo_size
= copy_size
;
3622 if (src_object_shared
== TRUE
)
3623 vm_object_reference_shared(old_copy
);
3625 vm_object_reference_locked(old_copy
);
3626 vm_object_unlock(old_copy
);
3627 vm_object_unlock(src_object
);
3629 if (new_copy
!= VM_OBJECT_NULL
) {
3630 vm_object_unlock(new_copy
);
3631 vm_object_deallocate(new_copy
);
3639 * Adjust the size argument so that the newly-created
3640 * copy object will be large enough to back either the
3641 * old copy object or the new mapping.
3643 if (old_copy
->vo_size
> copy_size
)
3644 copy_size
= old_copy
->vo_size
;
3646 if (new_copy
== VM_OBJECT_NULL
) {
3647 vm_object_unlock(old_copy
);
3648 vm_object_unlock(src_object
);
3649 new_copy
= vm_object_allocate(copy_size
);
3650 vm_object_lock(src_object
);
3651 vm_object_lock(new_copy
);
3653 src_object_shared
= FALSE
;
3656 new_copy
->vo_size
= copy_size
;
3659 * The copy-object is always made large enough to
3660 * completely shadow the original object, since
3661 * it may have several users who want to shadow
3662 * the original object at different points.
3665 assert((old_copy
->shadow
== src_object
) &&
3666 (old_copy
->vo_shadow_offset
== (vm_object_offset_t
) 0));
3668 } else if (new_copy
== VM_OBJECT_NULL
) {
3669 vm_object_unlock(src_object
);
3670 new_copy
= vm_object_allocate(copy_size
);
3671 vm_object_lock(src_object
);
3672 vm_object_lock(new_copy
);
3674 src_object_shared
= FALSE
;
3679 * We now have the src object locked, and the new copy object
3680 * allocated and locked (and potentially the old copy locked).
3681 * Before we go any further, make sure we can still perform
3682 * a delayed copy, as the situation may have changed.
3684 * Specifically, we can't perform a delayed copy if any of the
3685 * pages in the range are wired (because we can't safely take
3686 * write permission away from wired pages). If the pages aren't
3687 * wired, then go ahead and protect them.
3689 copy_delayed_protect_iterate
++;
3691 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
3692 if (!p
->fictitious
&& p
->offset
< copy_size
) {
3693 if (VM_PAGE_WIRED(p
)) {
3695 vm_object_unlock(old_copy
);
3696 vm_object_unlock(src_object
);
3697 vm_object_unlock(new_copy
);
3698 vm_object_deallocate(new_copy
);
3699 return VM_OBJECT_NULL
;
3701 pmap_page_protect(p
->phys_page
,
3702 (VM_PROT_ALL
& ~VM_PROT_WRITE
));
3706 if (old_copy
!= VM_OBJECT_NULL
) {
3708 * Make the old copy-object shadow the new one.
3709 * It will receive no more pages from the original
3713 /* remove ref. from old_copy */
3714 vm_object_lock_assert_exclusive(src_object
);
3715 src_object
->ref_count
--;
3716 assert(src_object
->ref_count
> 0);
3717 vm_object_lock_assert_exclusive(old_copy
);
3718 old_copy
->shadow
= new_copy
;
3719 vm_object_lock_assert_exclusive(new_copy
);
3720 assert(new_copy
->ref_count
> 0);
3721 new_copy
->ref_count
++; /* for old_copy->shadow ref. */
3724 if (old_copy
->res_count
) {
3725 VM_OBJ_RES_INCR(new_copy
);
3726 VM_OBJ_RES_DECR(src_object
);
3730 vm_object_unlock(old_copy
); /* done with old_copy */
3734 * Point the new copy at the existing object.
3736 vm_object_lock_assert_exclusive(new_copy
);
3737 new_copy
->shadow
= src_object
;
3738 new_copy
->vo_shadow_offset
= 0;
3739 new_copy
->shadowed
= TRUE
; /* caller must set needs_copy */
3741 vm_object_lock_assert_exclusive(src_object
);
3742 vm_object_reference_locked(src_object
);
3743 src_object
->copy
= new_copy
;
3744 vm_object_unlock(src_object
);
3745 vm_object_unlock(new_copy
);
3748 "vm_object_copy_delayed: used copy object %X for source %X\n",
3749 new_copy
, src_object
, 0, 0, 0);
3755 * Routine: vm_object_copy_strategically
3758 * Perform a copy according to the source object's
3759 * declared strategy. This operation may block,
3760 * and may be interrupted.
3762 __private_extern__ kern_return_t
3763 vm_object_copy_strategically(
3764 register vm_object_t src_object
,
3765 vm_object_offset_t src_offset
,
3766 vm_object_size_t size
,
3767 vm_object_t
*dst_object
, /* OUT */
3768 vm_object_offset_t
*dst_offset
, /* OUT */
3769 boolean_t
*dst_needs_copy
) /* OUT */
3772 boolean_t interruptible
= THREAD_ABORTSAFE
; /* XXX */
3773 boolean_t object_lock_shared
= FALSE
;
3774 memory_object_copy_strategy_t copy_strategy
;
3776 assert(src_object
!= VM_OBJECT_NULL
);
3778 copy_strategy
= src_object
->copy_strategy
;
3780 if (copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
3781 vm_object_lock_shared(src_object
);
3782 object_lock_shared
= TRUE
;
3784 vm_object_lock(src_object
);
3787 * The copy strategy is only valid if the memory manager
3788 * is "ready". Internal objects are always ready.
3791 while (!src_object
->internal
&& !src_object
->pager_ready
) {
3792 wait_result_t wait_result
;
3794 if (object_lock_shared
== TRUE
) {
3795 vm_object_unlock(src_object
);
3796 vm_object_lock(src_object
);
3797 object_lock_shared
= FALSE
;
3800 wait_result
= vm_object_sleep( src_object
,
3801 VM_OBJECT_EVENT_PAGER_READY
,
3803 if (wait_result
!= THREAD_AWAKENED
) {
3804 vm_object_unlock(src_object
);
3805 *dst_object
= VM_OBJECT_NULL
;
3807 *dst_needs_copy
= FALSE
;
3808 return(MACH_SEND_INTERRUPTED
);
3813 * Use the appropriate copy strategy.
3816 switch (copy_strategy
) {
3817 case MEMORY_OBJECT_COPY_DELAY
:
3818 *dst_object
= vm_object_copy_delayed(src_object
,
3819 src_offset
, size
, object_lock_shared
);
3820 if (*dst_object
!= VM_OBJECT_NULL
) {
3821 *dst_offset
= src_offset
;
3822 *dst_needs_copy
= TRUE
;
3823 result
= KERN_SUCCESS
;
3826 vm_object_lock(src_object
);
3827 /* fall thru when delayed copy not allowed */
3829 case MEMORY_OBJECT_COPY_NONE
:
3830 result
= vm_object_copy_slowly(src_object
, src_offset
, size
,
3831 interruptible
, dst_object
);
3832 if (result
== KERN_SUCCESS
) {
3834 *dst_needs_copy
= FALSE
;
3838 case MEMORY_OBJECT_COPY_CALL
:
3839 result
= vm_object_copy_call(src_object
, src_offset
, size
,
3841 if (result
== KERN_SUCCESS
) {
3842 *dst_offset
= src_offset
;
3843 *dst_needs_copy
= TRUE
;
3847 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3848 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);
3849 vm_object_unlock(src_object
);
3850 result
= KERN_MEMORY_RESTART_COPY
;
3854 panic("copy_strategically: bad strategy");
3855 result
= KERN_INVALID_ARGUMENT
;
3863 * Create a new object which is backed by the
3864 * specified existing object range. The source
3865 * object reference is deallocated.
3867 * The new object and offset into that object
3868 * are returned in the source parameters.
3870 boolean_t vm_object_shadow_check
= TRUE
;
3872 __private_extern__ boolean_t
3874 vm_object_t
*object
, /* IN/OUT */
3875 vm_object_offset_t
*offset
, /* IN/OUT */
3876 vm_object_size_t length
)
3878 register vm_object_t source
;
3879 register vm_object_t result
;
3882 assert(source
!= VM_OBJECT_NULL
);
3883 if (source
== VM_OBJECT_NULL
)
3889 * This assertion is valid but it gets triggered by Rosetta for example
3890 * due to a combination of vm_remap() that changes a VM object's
3891 * copy_strategy from SYMMETRIC to DELAY and vm_protect(VM_PROT_COPY)
3892 * that then sets "needs_copy" on its map entry. This creates a
3893 * mapping situation that VM should never see and doesn't know how to
3895 * It's not clear if this can create any real problem but we should
3896 * look into fixing this, probably by having vm_protect(VM_PROT_COPY)
3897 * do more than just set "needs_copy" to handle the copy-on-write...
3898 * In the meantime, let's disable the assertion.
3900 assert(source
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
);
3904 * Determine if we really need a shadow.
3906 * If the source object is larger than what we are trying
3907 * to create, then force the shadow creation even if the
3908 * ref count is 1. This will allow us to [potentially]
3909 * collapse the underlying object away in the future
3910 * (freeing up the extra data it might contain and that
3913 if (vm_object_shadow_check
&&
3914 source
->vo_size
== length
&&
3915 source
->ref_count
== 1 &&
3916 (source
->shadow
== VM_OBJECT_NULL
||
3917 source
->shadow
->copy
== VM_OBJECT_NULL
) )
3919 source
->shadowed
= FALSE
;
3924 * Allocate a new object with the given length
3927 if ((result
= vm_object_allocate(length
)) == VM_OBJECT_NULL
)
3928 panic("vm_object_shadow: no object for shadowing");
3931 * The new object shadows the source object, adding
3932 * a reference to it. Our caller changes his reference
3933 * to point to the new object, removing a reference to
3934 * the source object. Net result: no change of reference
3937 result
->shadow
= source
;
3940 * Store the offset into the source object,
3941 * and fix up the offset into the new object.
3944 result
->vo_shadow_offset
= *offset
;
3947 * Return the new things
3956 * The relationship between vm_object structures and
3957 * the memory_object requires careful synchronization.
3959 * All associations are created by memory_object_create_named
3960 * for external pagers and vm_object_pager_create for internal
3961 * objects as follows:
3963 * pager: the memory_object itself, supplied by
3964 * the user requesting a mapping (or the kernel,
3965 * when initializing internal objects); the
3966 * kernel simulates holding send rights by keeping
3970 * the memory object control port,
3971 * created by the kernel; the kernel holds
3972 * receive (and ownership) rights to this
3973 * port, but no other references.
3975 * When initialization is complete, the "initialized" field
3976 * is asserted. Other mappings using a particular memory object,
3977 * and any references to the vm_object gained through the
3978 * port association must wait for this initialization to occur.
3980 * In order to allow the memory manager to set attributes before
3981 * requests (notably virtual copy operations, but also data or
3982 * unlock requests) are made, a "ready" attribute is made available.
3983 * Only the memory manager may affect the value of this attribute.
3984 * Its value does not affect critical kernel functions, such as
3985 * internal object initialization or destruction. [Furthermore,
3986 * memory objects created by the kernel are assumed to be ready
3987 * immediately; the default memory manager need not explicitly
3988 * set the "ready" attribute.]
3990 * [Both the "initialized" and "ready" attribute wait conditions
3991 * use the "pager" field as the wait event.]
3993 * The port associations can be broken down by any of the
3994 * following routines:
3995 * vm_object_terminate:
3996 * No references to the vm_object remain, and
3997 * the object cannot (or will not) be cached.
3998 * This is the normal case, and is done even
3999 * though one of the other cases has already been
4001 * memory_object_destroy:
4002 * The memory manager has requested that the
4003 * kernel relinquish references to the memory
4004 * object. [The memory manager may not want to
4005 * destroy the memory object, but may wish to
4006 * refuse or tear down existing memory mappings.]
4008 * Each routine that breaks an association must break all of
4009 * them at once. At some later time, that routine must clear
4010 * the pager field and release the memory object references.
4011 * [Furthermore, each routine must cope with the simultaneous
4012 * or previous operations of the others.]
4014 * In addition to the lock on the object, the vm_object_hash_lock
4015 * governs the associations. References gained through the
4016 * association require use of the hash lock.
4018 * Because the pager field may be cleared spontaneously, it
4019 * cannot be used to determine whether a memory object has
4020 * ever been associated with a particular vm_object. [This
4021 * knowledge is important to the shadow object mechanism.]
4022 * For this reason, an additional "created" attribute is
4025 * During various paging operations, the pager reference found in the
4026 * vm_object must be valid. To prevent this from being released,
4027 * (other than being removed, i.e., made null), routines may use
4028 * the vm_object_paging_begin/end routines [actually, macros].
4029 * The implementation uses the "paging_in_progress" and "wanted" fields.
4030 * [Operations that alter the validity of the pager values include the
4031 * termination routines and vm_object_collapse.]
4036 * Routine: vm_object_enter
4038 * Find a VM object corresponding to the given
4039 * pager; if no such object exists, create one,
4040 * and initialize the pager.
4044 memory_object_t pager
,
4045 vm_object_size_t size
,
4050 register vm_object_t object
;
4051 vm_object_t new_object
;
4052 boolean_t must_init
;
4053 vm_object_hash_entry_t entry
, new_entry
;
4054 uint32_t try_failed_count
= 0;
4057 if (pager
== MEMORY_OBJECT_NULL
)
4058 return(vm_object_allocate(size
));
4060 new_object
= VM_OBJECT_NULL
;
4061 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
4065 * Look for an object associated with this port.
4068 lck
= vm_object_hash_lock_spin(pager
);
4070 entry
= vm_object_hash_lookup(pager
, FALSE
);
4072 if (entry
== VM_OBJECT_HASH_ENTRY_NULL
) {
4073 if (new_object
== VM_OBJECT_NULL
) {
4075 * We must unlock to create a new object;
4076 * if we do so, we must try the lookup again.
4078 vm_object_hash_unlock(lck
);
4079 assert(new_entry
== VM_OBJECT_HASH_ENTRY_NULL
);
4080 new_entry
= vm_object_hash_entry_alloc(pager
);
4081 new_object
= vm_object_allocate(size
);
4082 lck
= vm_object_hash_lock_spin(pager
);
4085 * Lookup failed twice, and we have something
4086 * to insert; set the object.
4088 vm_object_hash_insert(new_entry
, new_object
);
4090 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
4091 new_object
= VM_OBJECT_NULL
;
4094 } else if (entry
->object
== VM_OBJECT_NULL
) {
4096 * If a previous object is being terminated,
4097 * we must wait for the termination message
4098 * to be queued (and lookup the entry again).
4100 entry
->waiting
= TRUE
;
4101 entry
= VM_OBJECT_HASH_ENTRY_NULL
;
4102 assert_wait((event_t
) pager
, THREAD_UNINT
);
4103 vm_object_hash_unlock(lck
);
4105 thread_block(THREAD_CONTINUE_NULL
);
4106 lck
= vm_object_hash_lock_spin(pager
);
4108 } while (entry
== VM_OBJECT_HASH_ENTRY_NULL
);
4110 object
= entry
->object
;
4111 assert(object
!= VM_OBJECT_NULL
);
4114 if ( !vm_object_lock_try(object
)) {
4116 vm_object_hash_unlock(lck
);
4119 mutex_pause(try_failed_count
); /* wait a bit */
4122 assert(!internal
|| object
->internal
);
4124 if (object
->ref_count
== 0) {
4125 if ( !vm_object_cache_lock_try()) {
4127 vm_object_hash_unlock(lck
);
4128 vm_object_unlock(object
);
4131 mutex_pause(try_failed_count
); /* wait a bit */
4134 XPR(XPR_VM_OBJECT_CACHE
,
4135 "vm_object_enter: removing %x from cache, head (%x, %x)\n",
4137 vm_object_cached_list
.next
,
4138 vm_object_cached_list
.prev
, 0,0);
4139 queue_remove(&vm_object_cached_list
, object
,
4140 vm_object_t
, cached_list
);
4141 vm_object_cached_count
--;
4143 vm_object_cache_unlock();
4147 assert(!object
->named
);
4148 object
->named
= TRUE
;
4150 vm_object_lock_assert_exclusive(object
);
4151 object
->ref_count
++;
4152 vm_object_res_reference(object
);
4154 vm_object_hash_unlock(lck
);
4155 vm_object_unlock(object
);
4159 vm_object_hash_unlock(lck
);
4161 assert(object
->ref_count
> 0);
4163 VM_STAT_INCR(lookups
);
4166 "vm_o_enter: pager 0x%x obj 0x%x must_init %d\n",
4167 pager
, object
, must_init
, 0, 0);
4170 * If we raced to create a vm_object but lost, let's
4174 if (new_object
!= VM_OBJECT_NULL
)
4175 vm_object_deallocate(new_object
);
4177 if (new_entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
4178 vm_object_hash_entry_free(new_entry
);
4181 memory_object_control_t control
;
4184 * Allocate request port.
4187 control
= memory_object_control_allocate(object
);
4188 assert (control
!= MEMORY_OBJECT_CONTROL_NULL
);
4190 vm_object_lock(object
);
4191 assert(object
!= kernel_object
);
4194 * Copy the reference we were given.
4197 memory_object_reference(pager
);
4198 object
->pager_created
= TRUE
;
4199 object
->pager
= pager
;
4200 object
->internal
= internal
;
4201 object
->pager_trusted
= internal
;
4203 /* copy strategy invalid until set by memory manager */
4204 object
->copy_strategy
= MEMORY_OBJECT_COPY_INVALID
;
4206 object
->pager_control
= control
;
4207 object
->pager_ready
= FALSE
;
4209 vm_object_unlock(object
);
4212 * Let the pager know we're using it.
4215 (void) memory_object_init(pager
,
4216 object
->pager_control
,
4219 vm_object_lock(object
);
4221 object
->named
= TRUE
;
4223 object
->pager_ready
= TRUE
;
4224 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
4227 object
->pager_initialized
= TRUE
;
4228 vm_object_wakeup(object
, VM_OBJECT_EVENT_INITIALIZED
);
4230 vm_object_lock(object
);
4234 * [At this point, the object must be locked]
4238 * Wait for the work above to be done by the first
4239 * thread to map this object.
4242 while (!object
->pager_initialized
) {
4243 vm_object_sleep(object
,
4244 VM_OBJECT_EVENT_INITIALIZED
,
4247 vm_object_unlock(object
);
4250 "vm_object_enter: vm_object %x, memory_object %x, internal %d\n",
4251 object
, object
->pager
, internal
, 0,0);
4256 * Routine: vm_object_pager_create
4258 * Create a memory object for an internal object.
4259 * In/out conditions:
4260 * The object is locked on entry and exit;
4261 * it may be unlocked within this call.
4263 * Only one thread may be performing a
4264 * vm_object_pager_create on an object at
4265 * a time. Presumably, only the pageout
4266 * daemon will be using this routine.
4270 vm_object_pager_create(
4271 register vm_object_t object
)
4273 memory_object_t pager
;
4274 vm_object_hash_entry_t entry
;
4277 vm_object_size_t size
;
4278 vm_external_map_t map
;
4279 #endif /* MACH_PAGEMAP */
4281 XPR(XPR_VM_OBJECT
, "vm_object_pager_create, object 0x%X\n",
4284 assert(object
!= kernel_object
);
4286 if (memory_manager_default_check() != KERN_SUCCESS
)
4290 * Prevent collapse or termination by holding a paging reference
4293 vm_object_paging_begin(object
);
4294 if (object
->pager_created
) {
4296 * Someone else got to it first...
4297 * wait for them to finish initializing the ports
4299 while (!object
->pager_initialized
) {
4300 vm_object_sleep(object
,
4301 VM_OBJECT_EVENT_INITIALIZED
,
4304 vm_object_paging_end(object
);
4309 * Indicate that a memory object has been assigned
4310 * before dropping the lock, to prevent a race.
4313 object
->pager_created
= TRUE
;
4314 object
->paging_offset
= 0;
4317 size
= object
->vo_size
;
4318 #endif /* MACH_PAGEMAP */
4319 vm_object_unlock(object
);
4322 map
= vm_external_create(size
);
4323 vm_object_lock(object
);
4324 assert(object
->vo_size
== size
);
4325 object
->existence_map
= map
;
4326 vm_object_unlock(object
);
4327 #endif /* MACH_PAGEMAP */
4329 if ((uint32_t) object
->vo_size
!= object
->vo_size
) {
4330 panic("vm_object_pager_create(): object size 0x%llx >= 4GB\n",
4331 (uint64_t) object
->vo_size
);
4335 * Create the [internal] pager, and associate it with this object.
4337 * We make the association here so that vm_object_enter()
4338 * can look up the object to complete initializing it. No
4339 * user will ever map this object.
4342 memory_object_default_t dmm
;
4344 /* acquire a reference for the default memory manager */
4345 dmm
= memory_manager_default_reference();
4347 assert(object
->temporary
);
4349 /* create our new memory object */
4350 assert((vm_size_t
) object
->vo_size
== object
->vo_size
);
4351 (void) memory_object_create(dmm
, (vm_size_t
) object
->vo_size
,
4354 memory_object_default_deallocate(dmm
);
4357 entry
= vm_object_hash_entry_alloc(pager
);
4359 lck
= vm_object_hash_lock_spin(pager
);
4360 vm_object_hash_insert(entry
, object
);
4361 vm_object_hash_unlock(lck
);
4364 * A reference was returned by
4365 * memory_object_create(), and it is
4366 * copied by vm_object_enter().
4369 if (vm_object_enter(pager
, object
->vo_size
, TRUE
, TRUE
, FALSE
) != object
)
4370 panic("vm_object_pager_create: mismatch");
4373 * Drop the reference we were passed.
4375 memory_object_deallocate(pager
);
4377 vm_object_lock(object
);
4380 * Release the paging reference
4382 vm_object_paging_end(object
);
4386 * Routine: vm_object_remove
4388 * Eliminate the pager/object association
4391 * The object cache must be locked.
4393 __private_extern__
void
4397 memory_object_t pager
;
4399 if ((pager
= object
->pager
) != MEMORY_OBJECT_NULL
) {
4400 vm_object_hash_entry_t entry
;
4402 entry
= vm_object_hash_lookup(pager
, FALSE
);
4403 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
4404 entry
->object
= VM_OBJECT_NULL
;
4410 * Global variables for vm_object_collapse():
4412 * Counts for normal collapses and bypasses.
4413 * Debugging variables, to watch or disable collapse.
4415 static long object_collapses
= 0;
4416 static long object_bypasses
= 0;
4418 static boolean_t vm_object_collapse_allowed
= TRUE
;
4419 static boolean_t vm_object_bypass_allowed
= TRUE
;
4422 static int vm_external_discarded
;
4423 static int vm_external_collapsed
;
4426 unsigned long vm_object_collapse_encrypted
= 0;
4429 * Routine: vm_object_do_collapse
4431 * Collapse an object with the object backing it.
4432 * Pages in the backing object are moved into the
4433 * parent, and the backing object is deallocated.
4435 * Both objects and the cache are locked; the page
4436 * queues are unlocked.
4440 vm_object_do_collapse(
4442 vm_object_t backing_object
)
4445 vm_object_offset_t new_offset
, backing_offset
;
4446 vm_object_size_t size
;
4448 vm_object_lock_assert_exclusive(object
);
4449 vm_object_lock_assert_exclusive(backing_object
);
4451 backing_offset
= object
->vo_shadow_offset
;
4452 size
= object
->vo_size
;
4455 * Move all in-memory pages from backing_object
4456 * to the parent. Pages that have been paged out
4457 * will be overwritten by any of the parent's
4458 * pages that shadow them.
4461 while (!queue_empty(&backing_object
->memq
)) {
4463 p
= (vm_page_t
) queue_first(&backing_object
->memq
);
4465 new_offset
= (p
->offset
- backing_offset
);
4467 assert(!p
->busy
|| p
->absent
);
4470 * If the parent has a page here, or if
4471 * this page falls outside the parent,
4474 * Otherwise, move it as planned.
4477 if (p
->offset
< backing_offset
|| new_offset
>= size
) {
4482 * The encryption key includes the "pager" and the
4483 * "paging_offset". These will not change during the
4484 * object collapse, so we can just move an encrypted
4485 * page from one object to the other in this case.
4486 * We can't decrypt the page here, since we can't drop
4490 vm_object_collapse_encrypted
++;
4492 pp
= vm_page_lookup(object
, new_offset
);
4493 if (pp
== VM_PAGE_NULL
) {
4496 * Parent now has no page.
4497 * Move the backing object's page up.
4500 vm_page_rename(p
, object
, new_offset
, TRUE
);
4502 } else if (pp
->absent
) {
4505 * Parent has an absent page...
4506 * it's not being paged in, so
4507 * it must really be missing from
4510 * Throw out the absent page...
4511 * any faults looking for that
4512 * page will restart with the new
4517 vm_page_rename(p
, object
, new_offset
, TRUE
);
4518 #endif /* MACH_PAGEMAP */
4520 assert(! pp
->absent
);
4523 * Parent object has a real page.
4524 * Throw away the backing object's
4533 assert((!object
->pager_created
&& (object
->pager
== MEMORY_OBJECT_NULL
))
4534 || (!backing_object
->pager_created
4535 && (backing_object
->pager
== MEMORY_OBJECT_NULL
)));
4537 assert(!object
->pager_created
&& object
->pager
== MEMORY_OBJECT_NULL
);
4538 #endif /* !MACH_PAGEMAP */
4540 if (backing_object
->pager
!= MEMORY_OBJECT_NULL
) {
4541 vm_object_hash_entry_t entry
;
4544 * Move the pager from backing_object to object.
4546 * XXX We're only using part of the paging space
4547 * for keeps now... we ought to discard the
4551 assert(!object
->paging_in_progress
);
4552 assert(!object
->activity_in_progress
);
4553 object
->pager
= backing_object
->pager
;
4555 if (backing_object
->hashed
) {
4558 lck
= vm_object_hash_lock_spin(backing_object
->pager
);
4559 entry
= vm_object_hash_lookup(object
->pager
, FALSE
);
4560 assert(entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
4561 entry
->object
= object
;
4562 vm_object_hash_unlock(lck
);
4564 object
->hashed
= TRUE
;
4566 object
->pager_created
= backing_object
->pager_created
;
4567 object
->pager_control
= backing_object
->pager_control
;
4568 object
->pager_ready
= backing_object
->pager_ready
;
4569 object
->pager_initialized
= backing_object
->pager_initialized
;
4570 object
->paging_offset
=
4571 backing_object
->paging_offset
+ backing_offset
;
4572 if (object
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
4573 memory_object_control_collapse(object
->pager_control
,
4580 * If the shadow offset is 0, the use the existence map from
4581 * the backing object if there is one. If the shadow offset is
4582 * not zero, toss it.
4584 * XXX - If the shadow offset is not 0 then a bit copy is needed
4585 * if the map is to be salvaged. For now, we just just toss the
4586 * old map, giving the collapsed object no map. This means that
4587 * the pager is invoked for zero fill pages. If analysis shows
4588 * that this happens frequently and is a performance hit, then
4589 * this code should be fixed to salvage the map.
4591 assert(object
->existence_map
== VM_EXTERNAL_NULL
);
4592 if (backing_offset
|| (size
!= backing_object
->vo_size
)) {
4593 vm_external_discarded
++;
4594 vm_external_destroy(backing_object
->existence_map
,
4595 backing_object
->vo_size
);
4598 vm_external_collapsed
++;
4599 object
->existence_map
= backing_object
->existence_map
;
4601 backing_object
->existence_map
= VM_EXTERNAL_NULL
;
4602 #endif /* MACH_PAGEMAP */
4605 * Object now shadows whatever backing_object did.
4606 * Note that the reference to backing_object->shadow
4607 * moves from within backing_object to within object.
4610 assert(!object
->phys_contiguous
);
4611 assert(!backing_object
->phys_contiguous
);
4612 object
->shadow
= backing_object
->shadow
;
4613 if (object
->shadow
) {
4614 object
->vo_shadow_offset
+= backing_object
->vo_shadow_offset
;
4616 /* no shadow, therefore no shadow offset... */
4617 object
->vo_shadow_offset
= 0;
4619 assert((object
->shadow
== VM_OBJECT_NULL
) ||
4620 (object
->shadow
->copy
!= backing_object
));
4623 * Discard backing_object.
4625 * Since the backing object has no pages, no
4626 * pager left, and no object references within it,
4627 * all that is necessary is to dispose of it.
4630 assert((backing_object
->ref_count
== 1) &&
4631 (backing_object
->resident_page_count
== 0) &&
4632 (backing_object
->paging_in_progress
== 0) &&
4633 (backing_object
->activity_in_progress
== 0));
4635 backing_object
->alive
= FALSE
;
4636 vm_object_unlock(backing_object
);
4638 XPR(XPR_VM_OBJECT
, "vm_object_collapse, collapsed 0x%X\n",
4639 backing_object
, 0,0,0,0);
4641 vm_object_lock_destroy(backing_object
);
4643 zfree(vm_object_zone
, backing_object
);
4649 vm_object_do_bypass(
4651 vm_object_t backing_object
)
4654 * Make the parent shadow the next object
4658 vm_object_lock_assert_exclusive(object
);
4659 vm_object_lock_assert_exclusive(backing_object
);
4663 * Do object reference in-line to
4664 * conditionally increment shadow's
4665 * residence count. If object is not
4666 * resident, leave residence count
4669 if (backing_object
->shadow
!= VM_OBJECT_NULL
) {
4670 vm_object_lock(backing_object
->shadow
);
4671 vm_object_lock_assert_exclusive(backing_object
->shadow
);
4672 backing_object
->shadow
->ref_count
++;
4673 if (object
->res_count
!= 0)
4674 vm_object_res_reference(backing_object
->shadow
);
4675 vm_object_unlock(backing_object
->shadow
);
4677 #else /* TASK_SWAPPER */
4678 vm_object_reference(backing_object
->shadow
);
4679 #endif /* TASK_SWAPPER */
4681 assert(!object
->phys_contiguous
);
4682 assert(!backing_object
->phys_contiguous
);
4683 object
->shadow
= backing_object
->shadow
;
4684 if (object
->shadow
) {
4685 object
->vo_shadow_offset
+= backing_object
->vo_shadow_offset
;
4687 /* no shadow, therefore no shadow offset... */
4688 object
->vo_shadow_offset
= 0;
4692 * Backing object might have had a copy pointer
4693 * to us. If it did, clear it.
4695 if (backing_object
->copy
== object
) {
4696 backing_object
->copy
= VM_OBJECT_NULL
;
4700 * Drop the reference count on backing_object.
4702 * Since its ref_count was at least 2, it
4703 * will not vanish; so we don't need to call
4704 * vm_object_deallocate.
4705 * [with a caveat for "named" objects]
4707 * The res_count on the backing object is
4708 * conditionally decremented. It's possible
4709 * (via vm_pageout_scan) to get here with
4710 * a "swapped" object, which has a 0 res_count,
4711 * in which case, the backing object res_count
4712 * is already down by one.
4714 * Don't call vm_object_deallocate unless
4715 * ref_count drops to zero.
4717 * The ref_count can drop to zero here if the
4718 * backing object could be bypassed but not
4719 * collapsed, such as when the backing object
4720 * is temporary and cachable.
4723 if (backing_object
->ref_count
> 2 ||
4724 (!backing_object
->named
&& backing_object
->ref_count
> 1)) {
4725 vm_object_lock_assert_exclusive(backing_object
);
4726 backing_object
->ref_count
--;
4728 if (object
->res_count
!= 0)
4729 vm_object_res_deallocate(backing_object
);
4730 assert(backing_object
->ref_count
> 0);
4731 #endif /* TASK_SWAPPER */
4732 vm_object_unlock(backing_object
);
4736 * Drop locks so that we can deallocate
4737 * the backing object.
4741 if (object
->res_count
== 0) {
4742 /* XXX get a reference for the deallocate below */
4743 vm_object_res_reference(backing_object
);
4745 #endif /* TASK_SWAPPER */
4747 * vm_object_collapse (the caller of this function) is
4748 * now called from contexts that may not guarantee that a
4749 * valid reference is held on the object... w/o a valid
4750 * reference, it is unsafe and unwise (you will definitely
4751 * regret it) to unlock the object and then retake the lock
4752 * since the object may be terminated and recycled in between.
4753 * The "activity_in_progress" reference will keep the object
4756 vm_object_activity_begin(object
);
4757 vm_object_unlock(object
);
4759 vm_object_unlock(backing_object
);
4760 vm_object_deallocate(backing_object
);
4763 * Relock object. We don't have to reverify
4764 * its state since vm_object_collapse will
4765 * do that for us as it starts at the
4769 vm_object_lock(object
);
4770 vm_object_activity_end(object
);
4778 * vm_object_collapse:
4780 * Perform an object collapse or an object bypass if appropriate.
4781 * The real work of collapsing and bypassing is performed in
4782 * the routines vm_object_do_collapse and vm_object_do_bypass.
4784 * Requires that the object be locked and the page queues be unlocked.
4787 static unsigned long vm_object_collapse_calls
= 0;
4788 static unsigned long vm_object_collapse_objects
= 0;
4789 static unsigned long vm_object_collapse_do_collapse
= 0;
4790 static unsigned long vm_object_collapse_do_bypass
= 0;
4792 __private_extern__
void
4794 register vm_object_t object
,
4795 register vm_object_offset_t hint_offset
,
4796 boolean_t can_bypass
)
4798 register vm_object_t backing_object
;
4799 register unsigned int rcount
;
4800 register unsigned int size
;
4801 vm_object_t original_object
;
4802 int object_lock_type
;
4803 int backing_object_lock_type
;
4805 vm_object_collapse_calls
++;
4807 if (! vm_object_collapse_allowed
&&
4808 ! (can_bypass
&& vm_object_bypass_allowed
)) {
4812 XPR(XPR_VM_OBJECT
, "vm_object_collapse, obj 0x%X\n",
4815 if (object
== VM_OBJECT_NULL
)
4818 original_object
= object
;
4821 * The top object was locked "exclusive" by the caller.
4822 * In the first pass, to determine if we can collapse the shadow chain,
4823 * take a "shared" lock on the shadow objects. If we can collapse,
4824 * we'll have to go down the chain again with exclusive locks.
4826 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4827 backing_object_lock_type
= OBJECT_LOCK_SHARED
;
4830 object
= original_object
;
4831 vm_object_lock_assert_exclusive(object
);
4834 vm_object_collapse_objects
++;
4836 * Verify that the conditions are right for either
4837 * collapse or bypass:
4841 * There is a backing object, and
4844 backing_object
= object
->shadow
;
4845 if (backing_object
== VM_OBJECT_NULL
) {
4846 if (object
!= original_object
) {
4847 vm_object_unlock(object
);
4851 if (backing_object_lock_type
== OBJECT_LOCK_SHARED
) {
4852 vm_object_lock_shared(backing_object
);
4854 vm_object_lock(backing_object
);
4858 * No pages in the object are currently
4859 * being paged out, and
4861 if (object
->paging_in_progress
!= 0 ||
4862 object
->activity_in_progress
!= 0) {
4863 /* try and collapse the rest of the shadow chain */
4864 if (object
!= original_object
) {
4865 vm_object_unlock(object
);
4867 object
= backing_object
;
4868 object_lock_type
= backing_object_lock_type
;
4874 * The backing object is not read_only,
4875 * and no pages in the backing object are
4876 * currently being paged out.
4877 * The backing object is internal.
4881 if (!backing_object
->internal
||
4882 backing_object
->paging_in_progress
!= 0 ||
4883 backing_object
->activity_in_progress
!= 0) {
4884 /* try and collapse the rest of the shadow chain */
4885 if (object
!= original_object
) {
4886 vm_object_unlock(object
);
4888 object
= backing_object
;
4889 object_lock_type
= backing_object_lock_type
;
4894 * The backing object can't be a copy-object:
4895 * the shadow_offset for the copy-object must stay
4896 * as 0. Furthermore (for the 'we have all the
4897 * pages' case), if we bypass backing_object and
4898 * just shadow the next object in the chain, old
4899 * pages from that object would then have to be copied
4900 * BOTH into the (former) backing_object and into the
4903 if (backing_object
->shadow
!= VM_OBJECT_NULL
&&
4904 backing_object
->shadow
->copy
== backing_object
) {
4905 /* try and collapse the rest of the shadow chain */
4906 if (object
!= original_object
) {
4907 vm_object_unlock(object
);
4909 object
= backing_object
;
4910 object_lock_type
= backing_object_lock_type
;
4915 * We can now try to either collapse the backing
4916 * object (if the parent is the only reference to
4917 * it) or (perhaps) remove the parent's reference
4920 * If there is exactly one reference to the backing
4921 * object, we may be able to collapse it into the
4924 * If MACH_PAGEMAP is defined:
4925 * The parent must not have a pager created for it,
4926 * since collapsing a backing_object dumps new pages
4927 * into the parent that its pager doesn't know about
4928 * (and the collapse code can't merge the existence
4931 * As long as one of the objects is still not known
4932 * to the pager, we can collapse them.
4934 if (backing_object
->ref_count
== 1 &&
4935 (!object
->pager_created
4937 || !backing_object
->pager_created
4938 #endif /*!MACH_PAGEMAP */
4939 ) && vm_object_collapse_allowed
) {
4942 * We need the exclusive lock on the VM objects.
4944 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
4946 * We have an object and its shadow locked
4947 * "shared". We can't just upgrade the locks
4948 * to "exclusive", as some other thread might
4949 * also have these objects locked "shared" and
4950 * attempt to upgrade one or the other to
4951 * "exclusive". The upgrades would block
4952 * forever waiting for the other "shared" locks
4954 * So we have to release the locks and go
4955 * down the shadow chain again (since it could
4956 * have changed) with "exclusive" locking.
4958 vm_object_unlock(backing_object
);
4959 if (object
!= original_object
)
4960 vm_object_unlock(object
);
4961 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4962 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4967 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
4968 backing_object
, object
,
4969 backing_object
->pager
,
4970 backing_object
->pager_control
, 0);
4973 * Collapse the object with its backing
4974 * object, and try again with the object's
4975 * new backing object.
4978 vm_object_do_collapse(object
, backing_object
);
4979 vm_object_collapse_do_collapse
++;
4984 * Collapsing the backing object was not possible
4985 * or permitted, so let's try bypassing it.
4988 if (! (can_bypass
&& vm_object_bypass_allowed
)) {
4989 /* try and collapse the rest of the shadow chain */
4990 if (object
!= original_object
) {
4991 vm_object_unlock(object
);
4993 object
= backing_object
;
4994 object_lock_type
= backing_object_lock_type
;
5000 * If the object doesn't have all its pages present,
5001 * we have to make sure no pages in the backing object
5002 * "show through" before bypassing it.
5004 size
= atop(object
->vo_size
);
5005 rcount
= object
->resident_page_count
;
5007 if (rcount
!= size
) {
5008 vm_object_offset_t offset
;
5009 vm_object_offset_t backing_offset
;
5010 unsigned int backing_rcount
;
5013 * If the backing object has a pager but no pagemap,
5014 * then we cannot bypass it, because we don't know
5015 * what pages it has.
5017 if (backing_object
->pager_created
5019 && (backing_object
->existence_map
== VM_EXTERNAL_NULL
)
5020 #endif /* MACH_PAGEMAP */
5022 /* try and collapse the rest of the shadow chain */
5023 if (object
!= original_object
) {
5024 vm_object_unlock(object
);
5026 object
= backing_object
;
5027 object_lock_type
= backing_object_lock_type
;
5032 * If the object has a pager but no pagemap,
5033 * then we cannot bypass it, because we don't know
5034 * what pages it has.
5036 if (object
->pager_created
5038 && (object
->existence_map
== VM_EXTERNAL_NULL
)
5039 #endif /* MACH_PAGEMAP */
5041 /* try and collapse the rest of the shadow chain */
5042 if (object
!= original_object
) {
5043 vm_object_unlock(object
);
5045 object
= backing_object
;
5046 object_lock_type
= backing_object_lock_type
;
5050 backing_offset
= object
->vo_shadow_offset
;
5051 backing_rcount
= backing_object
->resident_page_count
;
5053 if ( (int)backing_rcount
- (int)(atop(backing_object
->vo_size
) - size
) > (int)rcount
) {
5055 * we have enough pages in the backing object to guarantee that
5056 * at least 1 of them must be 'uncovered' by a resident page
5057 * in the object we're evaluating, so move on and
5058 * try to collapse the rest of the shadow chain
5060 if (object
!= original_object
) {
5061 vm_object_unlock(object
);
5063 object
= backing_object
;
5064 object_lock_type
= backing_object_lock_type
;
5069 * If all of the pages in the backing object are
5070 * shadowed by the parent object, the parent
5071 * object no longer has to shadow the backing
5072 * object; it can shadow the next one in the
5075 * If the backing object has existence info,
5076 * we must check examine its existence info
5082 #define EXISTS_IN_OBJECT(obj, off, rc) \
5083 (vm_external_state_get((obj)->existence_map, \
5084 (vm_offset_t)(off)) == VM_EXTERNAL_STATE_EXISTS || \
5085 ((rc) && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
5087 #define EXISTS_IN_OBJECT(obj, off, rc) \
5088 (((rc) && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
5089 #endif /* MACH_PAGEMAP */
5092 * Check the hint location first
5093 * (since it is often the quickest way out of here).
5095 if (object
->cow_hint
!= ~(vm_offset_t
)0)
5096 hint_offset
= (vm_object_offset_t
)object
->cow_hint
;
5098 hint_offset
= (hint_offset
> 8 * PAGE_SIZE_64
) ?
5099 (hint_offset
- 8 * PAGE_SIZE_64
) : 0;
5101 if (EXISTS_IN_OBJECT(backing_object
, hint_offset
+
5102 backing_offset
, backing_rcount
) &&
5103 !EXISTS_IN_OBJECT(object
, hint_offset
, rcount
)) {
5104 /* dependency right at the hint */
5105 object
->cow_hint
= (vm_offset_t
) hint_offset
; /* atomic */
5106 /* try and collapse the rest of the shadow chain */
5107 if (object
!= original_object
) {
5108 vm_object_unlock(object
);
5110 object
= backing_object
;
5111 object_lock_type
= backing_object_lock_type
;
5116 * If the object's window onto the backing_object
5117 * is large compared to the number of resident
5118 * pages in the backing object, it makes sense to
5119 * walk the backing_object's resident pages first.
5121 * NOTE: Pages may be in both the existence map and/or
5122 * resident, so if we don't find a dependency while
5123 * walking the backing object's resident page list
5124 * directly, and there is an existence map, we'll have
5125 * to run the offset based 2nd pass. Because we may
5126 * have to run both passes, we need to be careful
5127 * not to decrement 'rcount' in the 1st pass
5129 if (backing_rcount
&& backing_rcount
< (size
/ 8)) {
5130 unsigned int rc
= rcount
;
5133 backing_rcount
= backing_object
->resident_page_count
;
5134 p
= (vm_page_t
)queue_first(&backing_object
->memq
);
5136 offset
= (p
->offset
- backing_offset
);
5138 if (offset
< object
->vo_size
&&
5139 offset
!= hint_offset
&&
5140 !EXISTS_IN_OBJECT(object
, offset
, rc
)) {
5141 /* found a dependency */
5142 object
->cow_hint
= (vm_offset_t
) offset
; /* atomic */
5146 p
= (vm_page_t
) queue_next(&p
->listq
);
5148 } while (--backing_rcount
);
5150 if (backing_rcount
!= 0 ) {
5151 /* try and collapse the rest of the shadow chain */
5152 if (object
!= original_object
) {
5153 vm_object_unlock(object
);
5155 object
= backing_object
;
5156 object_lock_type
= backing_object_lock_type
;
5162 * Walk through the offsets looking for pages in the
5163 * backing object that show through to the object.
5167 || backing_object
->existence_map
5168 #endif /* MACH_PAGEMAP */
5170 offset
= hint_offset
;
5173 (offset
+ PAGE_SIZE_64
< object
->vo_size
) ?
5174 (offset
+ PAGE_SIZE_64
) : 0) != hint_offset
) {
5176 if (EXISTS_IN_OBJECT(backing_object
, offset
+
5177 backing_offset
, backing_rcount
) &&
5178 !EXISTS_IN_OBJECT(object
, offset
, rcount
)) {
5179 /* found a dependency */
5180 object
->cow_hint
= (vm_offset_t
) offset
; /* atomic */
5184 if (offset
!= hint_offset
) {
5185 /* try and collapse the rest of the shadow chain */
5186 if (object
!= original_object
) {
5187 vm_object_unlock(object
);
5189 object
= backing_object
;
5190 object_lock_type
= backing_object_lock_type
;
5197 * We need "exclusive" locks on the 2 VM objects.
5199 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
5200 vm_object_unlock(backing_object
);
5201 if (object
!= original_object
)
5202 vm_object_unlock(object
);
5203 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5204 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5208 /* reset the offset hint for any objects deeper in the chain */
5209 object
->cow_hint
= (vm_offset_t
)0;
5212 * All interesting pages in the backing object
5213 * already live in the parent or its pager.
5214 * Thus we can bypass the backing object.
5217 vm_object_do_bypass(object
, backing_object
);
5218 vm_object_collapse_do_bypass
++;
5221 * Try again with this object's new backing object.
5227 if (object
!= original_object
) {
5228 vm_object_unlock(object
);
5233 * Routine: vm_object_page_remove: [internal]
5235 * Removes all physical pages in the specified
5236 * object range from the object's list of pages.
5238 * In/out conditions:
5239 * The object must be locked.
5240 * The object must not have paging_in_progress, usually
5241 * guaranteed by not having a pager.
5243 unsigned int vm_object_page_remove_lookup
= 0;
5244 unsigned int vm_object_page_remove_iterate
= 0;
5246 __private_extern__
void
5247 vm_object_page_remove(
5248 register vm_object_t object
,
5249 register vm_object_offset_t start
,
5250 register vm_object_offset_t end
)
5252 register vm_page_t p
, next
;
5255 * One and two page removals are most popular.
5256 * The factor of 16 here is somewhat arbitrary.
5257 * It balances vm_object_lookup vs iteration.
5260 if (atop_64(end
- start
) < (unsigned)object
->resident_page_count
/16) {
5261 vm_object_page_remove_lookup
++;
5263 for (; start
< end
; start
+= PAGE_SIZE_64
) {
5264 p
= vm_page_lookup(object
, start
);
5265 if (p
!= VM_PAGE_NULL
) {
5266 assert(!p
->cleaning
&& !p
->pageout
&& !p
->laundry
);
5267 if (!p
->fictitious
&& p
->pmapped
)
5268 pmap_disconnect(p
->phys_page
);
5273 vm_object_page_remove_iterate
++;
5275 p
= (vm_page_t
) queue_first(&object
->memq
);
5276 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
5277 next
= (vm_page_t
) queue_next(&p
->listq
);
5278 if ((start
<= p
->offset
) && (p
->offset
< end
)) {
5279 assert(!p
->cleaning
&& !p
->pageout
&& !p
->laundry
);
5280 if (!p
->fictitious
&& p
->pmapped
)
5281 pmap_disconnect(p
->phys_page
);
5291 * Routine: vm_object_coalesce
5292 * Function: Coalesces two objects backing up adjoining
5293 * regions of memory into a single object.
5295 * returns TRUE if objects were combined.
5297 * NOTE: Only works at the moment if the second object is NULL -
5298 * if it's not, which object do we lock first?
5301 * prev_object First object to coalesce
5302 * prev_offset Offset into prev_object
5303 * next_object Second object into coalesce
5304 * next_offset Offset into next_object
5306 * prev_size Size of reference to prev_object
5307 * next_size Size of reference to next_object
5310 * The object(s) must *not* be locked. The map must be locked
5311 * to preserve the reference to the object(s).
5313 static int vm_object_coalesce_count
= 0;
5315 __private_extern__ boolean_t
5317 register vm_object_t prev_object
,
5318 vm_object_t next_object
,
5319 vm_object_offset_t prev_offset
,
5320 __unused vm_object_offset_t next_offset
,
5321 vm_object_size_t prev_size
,
5322 vm_object_size_t next_size
)
5324 vm_object_size_t newsize
;
5330 if (next_object
!= VM_OBJECT_NULL
) {
5334 if (prev_object
== VM_OBJECT_NULL
) {
5339 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
5340 prev_object
, prev_offset
, prev_size
, next_size
, 0);
5342 vm_object_lock(prev_object
);
5345 * Try to collapse the object first
5347 vm_object_collapse(prev_object
, prev_offset
, TRUE
);
5350 * Can't coalesce if pages not mapped to
5351 * prev_entry may be in use any way:
5352 * . more than one reference
5354 * . shadows another object
5355 * . has a copy elsewhere
5357 * . paging references (pages might be in page-list)
5360 if ((prev_object
->ref_count
> 1) ||
5361 prev_object
->pager_created
||
5362 (prev_object
->shadow
!= VM_OBJECT_NULL
) ||
5363 (prev_object
->copy
!= VM_OBJECT_NULL
) ||
5364 (prev_object
->true_share
!= FALSE
) ||
5365 (prev_object
->purgable
!= VM_PURGABLE_DENY
) ||
5366 (prev_object
->paging_in_progress
!= 0) ||
5367 (prev_object
->activity_in_progress
!= 0)) {
5368 vm_object_unlock(prev_object
);
5372 vm_object_coalesce_count
++;
5375 * Remove any pages that may still be in the object from
5376 * a previous deallocation.
5378 vm_object_page_remove(prev_object
,
5379 prev_offset
+ prev_size
,
5380 prev_offset
+ prev_size
+ next_size
);
5383 * Extend the object if necessary.
5385 newsize
= prev_offset
+ prev_size
+ next_size
;
5386 if (newsize
> prev_object
->vo_size
) {
5389 * We cannot extend an object that has existence info,
5390 * since the existence info might then fail to cover
5391 * the entire object.
5393 * This assertion must be true because the object
5394 * has no pager, and we only create existence info
5395 * for objects with pagers.
5397 assert(prev_object
->existence_map
== VM_EXTERNAL_NULL
);
5398 #endif /* MACH_PAGEMAP */
5399 prev_object
->vo_size
= newsize
;
5402 vm_object_unlock(prev_object
);
5407 * Attach a set of physical pages to an object, so that they can
5408 * be mapped by mapping the object. Typically used to map IO memory.
5410 * The mapping function and its private data are used to obtain the
5411 * physical addresses for each page to be mapped.
5416 vm_object_offset_t offset
,
5417 vm_object_size_t size
,
5418 vm_object_offset_t (*map_fn
)(void *map_fn_data
,
5419 vm_object_offset_t offset
),
5420 void *map_fn_data
) /* private to map_fn */
5426 vm_object_offset_t addr
;
5428 num_pages
= atop_64(size
);
5430 for (i
= 0; i
< num_pages
; i
++, offset
+= PAGE_SIZE_64
) {
5432 addr
= (*map_fn
)(map_fn_data
, offset
);
5434 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
5435 vm_page_more_fictitious();
5437 vm_object_lock(object
);
5438 if ((old_page
= vm_page_lookup(object
, offset
))
5441 VM_PAGE_FREE(old_page
);
5444 assert((ppnum_t
) addr
== addr
);
5445 vm_page_init(m
, (ppnum_t
) addr
, FALSE
);
5447 * private normally requires lock_queues but since we
5448 * are initializing the page, its not necessary here
5450 m
->private = TRUE
; /* don`t free page */
5452 vm_page_insert(m
, object
, offset
);
5454 PAGE_WAKEUP_DONE(m
);
5455 vm_object_unlock(object
);
5460 vm_object_populate_with_private(
5462 vm_object_offset_t offset
,
5467 vm_object_offset_t base_offset
;
5470 if (!object
->private)
5471 return KERN_FAILURE
;
5473 base_page
= phys_page
;
5475 vm_object_lock(object
);
5477 if (!object
->phys_contiguous
) {
5480 if ((base_offset
= trunc_page_64(offset
)) != offset
) {
5481 vm_object_unlock(object
);
5482 return KERN_FAILURE
;
5484 base_offset
+= object
->paging_offset
;
5487 m
= vm_page_lookup(object
, base_offset
);
5489 if (m
!= VM_PAGE_NULL
) {
5490 if (m
->fictitious
) {
5491 if (m
->phys_page
!= vm_page_guard_addr
) {
5493 vm_page_lockspin_queues();
5495 vm_page_unlock_queues();
5497 m
->fictitious
= FALSE
;
5498 m
->phys_page
= base_page
;
5500 } else if (m
->phys_page
!= base_page
) {
5504 * we'd leak a real page... that can't be right
5506 panic("vm_object_populate_with_private - %p not private", m
);
5510 * pmap call to clear old mapping
5512 pmap_disconnect(m
->phys_page
);
5514 m
->phys_page
= base_page
;
5518 * we should never see this on a ficticious or private page
5520 panic("vm_object_populate_with_private - %p encrypted", m
);
5524 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
5525 vm_page_more_fictitious();
5528 * private normally requires lock_queues but since we
5529 * are initializing the page, its not necessary here
5532 m
->fictitious
= FALSE
;
5533 m
->phys_page
= base_page
;
5537 vm_page_insert(m
, object
, base_offset
);
5539 base_page
++; /* Go to the next physical page */
5540 base_offset
+= PAGE_SIZE
;
5544 /* NOTE: we should check the original settings here */
5545 /* if we have a size > zero a pmap call should be made */
5546 /* to disable the range */
5550 /* shadows on contiguous memory are not allowed */
5551 /* we therefore can use the offset field */
5552 object
->vo_shadow_offset
= (vm_object_offset_t
)phys_page
<< PAGE_SHIFT
;
5553 object
->vo_size
= size
;
5555 vm_object_unlock(object
);
5557 return KERN_SUCCESS
;
5561 * memory_object_free_from_cache:
5563 * Walk the vm_object cache list, removing and freeing vm_objects
5564 * which are backed by the pager identified by the caller, (pager_ops).
5565 * Remove up to "count" objects, if there are that may available
5568 * Walk the list at most once, return the number of vm_objects
5572 __private_extern__ kern_return_t
5573 memory_object_free_from_cache(
5574 __unused host_t host
,
5575 __unused memory_object_pager_ops_t pager_ops
,
5579 int object_released
= 0;
5581 register vm_object_t object
= VM_OBJECT_NULL
;
5585 if(host == HOST_NULL)
5586 return(KERN_INVALID_ARGUMENT);
5590 vm_object_cache_lock();
5592 queue_iterate(&vm_object_cached_list
, object
,
5593 vm_object_t
, cached_list
) {
5594 if (object
->pager
&&
5595 (pager_ops
== object
->pager
->mo_pager_ops
)) {
5596 vm_object_lock(object
);
5597 queue_remove(&vm_object_cached_list
, object
,
5598 vm_object_t
, cached_list
);
5599 vm_object_cached_count
--;
5601 vm_object_cache_unlock();
5603 * Since this object is in the cache, we know
5604 * that it is initialized and has only a pager's
5605 * (implicit) reference. Take a reference to avoid
5606 * recursive deallocations.
5609 assert(object
->pager_initialized
);
5610 assert(object
->ref_count
== 0);
5611 vm_object_lock_assert_exclusive(object
);
5612 object
->ref_count
++;
5615 * Terminate the object.
5616 * If the object had a shadow, we let
5617 * vm_object_deallocate deallocate it.
5618 * "pageout" objects have a shadow, but
5619 * maintain a "paging reference" rather
5620 * than a normal reference.
5621 * (We are careful here to limit recursion.)
5623 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
5625 if ((vm_object_terminate(object
) == KERN_SUCCESS
)
5626 && (shadow
!= VM_OBJECT_NULL
)) {
5627 vm_object_deallocate(shadow
);
5630 if(object_released
++ == *count
)
5631 return KERN_SUCCESS
;
5635 vm_object_cache_unlock();
5636 *count
= object_released
;
5640 return KERN_SUCCESS
;
5646 memory_object_create_named(
5647 memory_object_t pager
,
5648 memory_object_offset_t size
,
5649 memory_object_control_t
*control
)
5652 vm_object_hash_entry_t entry
;
5655 *control
= MEMORY_OBJECT_CONTROL_NULL
;
5656 if (pager
== MEMORY_OBJECT_NULL
)
5657 return KERN_INVALID_ARGUMENT
;
5659 lck
= vm_object_hash_lock_spin(pager
);
5660 entry
= vm_object_hash_lookup(pager
, FALSE
);
5662 if ((entry
!= VM_OBJECT_HASH_ENTRY_NULL
) &&
5663 (entry
->object
!= VM_OBJECT_NULL
)) {
5664 if (entry
->object
->named
== TRUE
)
5665 panic("memory_object_create_named: caller already holds the right"); }
5666 vm_object_hash_unlock(lck
);
5668 if ((object
= vm_object_enter(pager
, size
, FALSE
, FALSE
, TRUE
)) == VM_OBJECT_NULL
) {
5669 return(KERN_INVALID_OBJECT
);
5672 /* wait for object (if any) to be ready */
5673 if (object
!= VM_OBJECT_NULL
) {
5674 vm_object_lock(object
);
5675 object
->named
= TRUE
;
5676 while (!object
->pager_ready
) {
5677 vm_object_sleep(object
,
5678 VM_OBJECT_EVENT_PAGER_READY
,
5681 *control
= object
->pager_control
;
5682 vm_object_unlock(object
);
5684 return (KERN_SUCCESS
);
5689 * Routine: memory_object_recover_named [user interface]
5691 * Attempt to recover a named reference for a VM object.
5692 * VM will verify that the object has not already started
5693 * down the termination path, and if it has, will optionally
5694 * wait for that to finish.
5696 * KERN_SUCCESS - we recovered a named reference on the object
5697 * KERN_FAILURE - we could not recover a reference (object dead)
5698 * KERN_INVALID_ARGUMENT - bad memory object control
5701 memory_object_recover_named(
5702 memory_object_control_t control
,
5703 boolean_t wait_on_terminating
)
5707 object
= memory_object_control_to_vm_object(control
);
5708 if (object
== VM_OBJECT_NULL
) {
5709 return (KERN_INVALID_ARGUMENT
);
5712 vm_object_lock(object
);
5714 if (object
->terminating
&& wait_on_terminating
) {
5715 vm_object_wait(object
,
5716 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
5721 if (!object
->alive
) {
5722 vm_object_unlock(object
);
5723 return KERN_FAILURE
;
5726 if (object
->named
== TRUE
) {
5727 vm_object_unlock(object
);
5728 return KERN_SUCCESS
;
5731 if ((object
->ref_count
== 0) && (!object
->terminating
)) {
5732 if (!vm_object_cache_lock_try()) {
5733 vm_object_unlock(object
);
5736 queue_remove(&vm_object_cached_list
, object
,
5737 vm_object_t
, cached_list
);
5738 vm_object_cached_count
--;
5739 XPR(XPR_VM_OBJECT_CACHE
,
5740 "memory_object_recover_named: removing %X, head (%X, %X)\n",
5742 vm_object_cached_list
.next
,
5743 vm_object_cached_list
.prev
, 0,0);
5745 vm_object_cache_unlock();
5748 object
->named
= TRUE
;
5749 vm_object_lock_assert_exclusive(object
);
5750 object
->ref_count
++;
5751 vm_object_res_reference(object
);
5752 while (!object
->pager_ready
) {
5753 vm_object_sleep(object
,
5754 VM_OBJECT_EVENT_PAGER_READY
,
5757 vm_object_unlock(object
);
5758 return (KERN_SUCCESS
);
5763 * vm_object_release_name:
5765 * Enforces name semantic on memory_object reference count decrement
5766 * This routine should not be called unless the caller holds a name
5767 * reference gained through the memory_object_create_named.
5769 * If the TERMINATE_IDLE flag is set, the call will return if the
5770 * reference count is not 1. i.e. idle with the only remaining reference
5772 * If the decision is made to proceed the name field flag is set to
5773 * false and the reference count is decremented. If the RESPECT_CACHE
5774 * flag is set and the reference count has gone to zero, the
5775 * memory_object is checked to see if it is cacheable otherwise when
5776 * the reference count is zero, it is simply terminated.
5779 __private_extern__ kern_return_t
5780 vm_object_release_name(
5785 boolean_t original_object
= TRUE
;
5787 while (object
!= VM_OBJECT_NULL
) {
5789 vm_object_lock(object
);
5791 assert(object
->alive
);
5792 if (original_object
)
5793 assert(object
->named
);
5794 assert(object
->ref_count
> 0);
5797 * We have to wait for initialization before
5798 * destroying or caching the object.
5801 if (object
->pager_created
&& !object
->pager_initialized
) {
5802 assert(!object
->can_persist
);
5803 vm_object_assert_wait(object
,
5804 VM_OBJECT_EVENT_INITIALIZED
,
5806 vm_object_unlock(object
);
5807 thread_block(THREAD_CONTINUE_NULL
);
5811 if (((object
->ref_count
> 1)
5812 && (flags
& MEMORY_OBJECT_TERMINATE_IDLE
))
5813 || (object
->terminating
)) {
5814 vm_object_unlock(object
);
5815 return KERN_FAILURE
;
5817 if (flags
& MEMORY_OBJECT_RELEASE_NO_OP
) {
5818 vm_object_unlock(object
);
5819 return KERN_SUCCESS
;
5823 if ((flags
& MEMORY_OBJECT_RESPECT_CACHE
) &&
5824 (object
->ref_count
== 1)) {
5825 if (original_object
)
5826 object
->named
= FALSE
;
5827 vm_object_unlock(object
);
5828 /* let vm_object_deallocate push this thing into */
5829 /* the cache, if that it is where it is bound */
5830 vm_object_deallocate(object
);
5831 return KERN_SUCCESS
;
5833 VM_OBJ_RES_DECR(object
);
5834 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
5836 if (object
->ref_count
== 1) {
5837 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
5838 if (original_object
) {
5839 return KERN_FAILURE
;
5841 return KERN_SUCCESS
;
5844 if (shadow
!= VM_OBJECT_NULL
) {
5845 original_object
= FALSE
;
5849 return KERN_SUCCESS
;
5851 vm_object_lock_assert_exclusive(object
);
5852 object
->ref_count
--;
5853 assert(object
->ref_count
> 0);
5855 object
->named
= FALSE
;
5856 vm_object_unlock(object
);
5857 return KERN_SUCCESS
;
5862 return KERN_FAILURE
;
5866 __private_extern__ kern_return_t
5867 vm_object_lock_request(
5869 vm_object_offset_t offset
,
5870 vm_object_size_t size
,
5871 memory_object_return_t should_return
,
5875 __unused boolean_t should_flush
;
5877 should_flush
= flags
& MEMORY_OBJECT_DATA_FLUSH
;
5879 XPR(XPR_MEMORY_OBJECT
,
5880 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
5881 object
, offset
, size
,
5882 (((should_return
&1)<<1)|should_flush
), prot
);
5885 * Check for bogus arguments.
5887 if (object
== VM_OBJECT_NULL
)
5888 return (KERN_INVALID_ARGUMENT
);
5890 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
)
5891 return (KERN_INVALID_ARGUMENT
);
5893 size
= round_page_64(size
);
5896 * Lock the object, and acquire a paging reference to
5897 * prevent the memory_object reference from being released.
5899 vm_object_lock(object
);
5900 vm_object_paging_begin(object
);
5902 (void)vm_object_update(object
,
5903 offset
, size
, NULL
, NULL
, should_return
, flags
, prot
);
5905 vm_object_paging_end(object
);
5906 vm_object_unlock(object
);
5908 return (KERN_SUCCESS
);
5912 * Empty a purgeable object by grabbing the physical pages assigned to it and
5913 * putting them on the free queue without writing them to backing store, etc.
5914 * When the pages are next touched they will be demand zero-fill pages. We
5915 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
5916 * skip referenced/dirty pages, pages on the active queue, etc. We're more
5917 * than happy to grab these since this is a purgeable object. We mark the
5918 * object as "empty" after reaping its pages.
5920 * On entry the object must be locked and it must be
5921 * purgeable with no delayed copies pending.
5924 vm_object_purge(vm_object_t object
)
5926 vm_object_lock_assert_exclusive(object
);
5928 if (object
->purgable
== VM_PURGABLE_DENY
)
5931 assert(object
->copy
== VM_OBJECT_NULL
);
5932 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
5934 if(object
->purgable
== VM_PURGABLE_VOLATILE
) {
5936 assert(object
->resident_page_count
>=
5937 object
->wired_page_count
);
5938 delta
= (object
->resident_page_count
-
5939 object
->wired_page_count
);
5941 assert(vm_page_purgeable_count
>=
5944 (SInt32
*)&vm_page_purgeable_count
);
5946 if (object
->wired_page_count
!= 0) {
5947 assert(vm_page_purgeable_wired_count
>=
5948 object
->wired_page_count
);
5949 OSAddAtomic(-object
->wired_page_count
,
5950 (SInt32
*)&vm_page_purgeable_wired_count
);
5953 object
->purgable
= VM_PURGABLE_EMPTY
;
5955 vm_object_reap_pages(object
, REAP_PURGEABLE
);
5960 * vm_object_purgeable_control() allows the caller to control and investigate the
5961 * state of a purgeable object. A purgeable object is created via a call to
5962 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgeable object will
5963 * never be coalesced with any other object -- even other purgeable objects --
5964 * and will thus always remain a distinct object. A purgeable object has
5965 * special semantics when its reference count is exactly 1. If its reference
5966 * count is greater than 1, then a purgeable object will behave like a normal
5967 * object and attempts to use this interface will result in an error return
5968 * of KERN_INVALID_ARGUMENT.
5970 * A purgeable object may be put into a "volatile" state which will make the
5971 * object's pages elligable for being reclaimed without paging to backing
5972 * store if the system runs low on memory. If the pages in a volatile
5973 * purgeable object are reclaimed, the purgeable object is said to have been
5974 * "emptied." When a purgeable object is emptied the system will reclaim as
5975 * many pages from the object as it can in a convenient manner (pages already
5976 * en route to backing store or busy for other reasons are left as is). When
5977 * a purgeable object is made volatile, its pages will generally be reclaimed
5978 * before other pages in the application's working set. This semantic is
5979 * generally used by applications which can recreate the data in the object
5980 * faster than it can be paged in. One such example might be media assets
5981 * which can be reread from a much faster RAID volume.
5983 * A purgeable object may be designated as "non-volatile" which means it will
5984 * behave like all other objects in the system with pages being written to and
5985 * read from backing store as needed to satisfy system memory needs. If the
5986 * object was emptied before the object was made non-volatile, that fact will
5987 * be returned as the old state of the purgeable object (see
5988 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
5989 * were reclaimed as part of emptying the object will be refaulted in as
5990 * zero-fill on demand. It is up to the application to note that an object
5991 * was emptied and recreate the objects contents if necessary. When a
5992 * purgeable object is made non-volatile, its pages will generally not be paged
5993 * out to backing store in the immediate future. A purgeable object may also
5994 * be manually emptied.
5996 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
5997 * volatile purgeable object may be queried at any time. This information may
5998 * be used as a control input to let the application know when the system is
5999 * experiencing memory pressure and is reclaiming memory.
6001 * The specified address may be any address within the purgeable object. If
6002 * the specified address does not represent any object in the target task's
6003 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
6004 * object containing the specified address is not a purgeable object, then
6005 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
6008 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
6009 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
6010 * state is used to set the new state of the purgeable object and return its
6011 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgeable
6012 * object is returned in the parameter state.
6014 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
6015 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
6016 * the non-volatile, volatile and volatile/empty states described above.
6017 * Setting the state of a purgeable object to VM_PURGABLE_EMPTY will
6018 * immediately reclaim as many pages in the object as can be conveniently
6019 * collected (some may have already been written to backing store or be
6022 * The process of making a purgeable object non-volatile and determining its
6023 * previous state is atomic. Thus, if a purgeable object is made
6024 * VM_PURGABLE_NONVOLATILE and the old state is returned as
6025 * VM_PURGABLE_VOLATILE, then the purgeable object's previous contents are
6026 * completely intact and will remain so until the object is made volatile
6027 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
6028 * was reclaimed while it was in a volatile state and its previous contents
6032 * The object must be locked.
6035 vm_object_purgable_control(
6037 vm_purgable_t control
,
6043 if (object
== VM_OBJECT_NULL
) {
6045 * Object must already be present or it can't be purgeable.
6047 return KERN_INVALID_ARGUMENT
;
6051 * Get current state of the purgeable object.
6053 old_state
= object
->purgable
;
6054 if (old_state
== VM_PURGABLE_DENY
)
6055 return KERN_INVALID_ARGUMENT
;
6057 /* purgeable cant have delayed copies - now or in the future */
6058 assert(object
->copy
== VM_OBJECT_NULL
);
6059 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
6062 * Execute the desired operation.
6064 if (control
== VM_PURGABLE_GET_STATE
) {
6066 return KERN_SUCCESS
;
6069 if ((*state
) & VM_PURGABLE_DEBUG_EMPTY
) {
6070 object
->volatile_empty
= TRUE
;
6072 if ((*state
) & VM_PURGABLE_DEBUG_FAULT
) {
6073 object
->volatile_fault
= TRUE
;
6076 new_state
= *state
& VM_PURGABLE_STATE_MASK
;
6077 if (new_state
== VM_PURGABLE_VOLATILE
&&
6078 object
->volatile_empty
) {
6079 new_state
= VM_PURGABLE_EMPTY
;
6082 switch (new_state
) {
6083 case VM_PURGABLE_DENY
:
6084 case VM_PURGABLE_NONVOLATILE
:
6085 object
->purgable
= new_state
;
6087 if (old_state
== VM_PURGABLE_VOLATILE
) {
6090 assert(object
->resident_page_count
>=
6091 object
->wired_page_count
);
6092 delta
= (object
->resident_page_count
-
6093 object
->wired_page_count
);
6095 assert(vm_page_purgeable_count
>= delta
);
6099 (SInt32
*)&vm_page_purgeable_count
);
6101 if (object
->wired_page_count
!= 0) {
6102 assert(vm_page_purgeable_wired_count
>=
6103 object
->wired_page_count
);
6104 OSAddAtomic(-object
->wired_page_count
,
6105 (SInt32
*)&vm_page_purgeable_wired_count
);
6108 vm_page_lock_queues();
6110 assert(object
->objq
.next
!= NULL
&& object
->objq
.prev
!= NULL
); /* object should be on a queue */
6111 purgeable_q_t queue
= vm_purgeable_object_remove(object
);
6114 vm_purgeable_token_delete_last(queue
);
6115 assert(queue
->debug_count_objects
>=0);
6117 vm_page_unlock_queues();
6121 case VM_PURGABLE_VOLATILE
:
6122 if (object
->volatile_fault
) {
6126 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
6132 refmod
= pmap_disconnect(p
->phys_page
);
6133 if ((refmod
& VM_MEM_MODIFIED
) &&
6135 SET_PAGE_DIRTY(p
, FALSE
);
6140 if (old_state
== VM_PURGABLE_EMPTY
&&
6141 object
->resident_page_count
== 0)
6144 purgeable_q_t queue
;
6146 /* find the correct queue */
6147 if ((*state
&VM_PURGABLE_ORDERING_MASK
) == VM_PURGABLE_ORDERING_OBSOLETE
)
6148 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_OBSOLETE
];
6150 if ((*state
&VM_PURGABLE_BEHAVIOR_MASK
) == VM_PURGABLE_BEHAVIOR_FIFO
)
6151 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_FIFO
];
6153 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_LIFO
];
6156 if (old_state
== VM_PURGABLE_NONVOLATILE
||
6157 old_state
== VM_PURGABLE_EMPTY
) {
6160 /* try to add token... this can fail */
6161 vm_page_lock_queues();
6163 kern_return_t result
= vm_purgeable_token_add(queue
);
6164 if (result
!= KERN_SUCCESS
) {
6165 vm_page_unlock_queues();
6168 vm_page_unlock_queues();
6170 assert(object
->resident_page_count
>=
6171 object
->wired_page_count
);
6172 delta
= (object
->resident_page_count
-
6173 object
->wired_page_count
);
6177 &vm_page_purgeable_count
);
6179 if (object
->wired_page_count
!= 0) {
6180 OSAddAtomic(object
->wired_page_count
,
6181 &vm_page_purgeable_wired_count
);
6184 object
->purgable
= new_state
;
6186 /* object should not be on a queue */
6187 assert(object
->objq
.next
== NULL
&& object
->objq
.prev
== NULL
);
6189 else if (old_state
== VM_PURGABLE_VOLATILE
) {
6191 * if reassigning priorities / purgeable groups, we don't change the
6192 * token queue. So moving priorities will not make pages stay around longer.
6193 * Reasoning is that the algorithm gives most priority to the most important
6194 * object. If a new token is added, the most important object' priority is boosted.
6195 * This biases the system already for purgeable queues that move a lot.
6196 * It doesn't seem more biasing is neccessary in this case, where no new object is added.
6198 assert(object
->objq
.next
!= NULL
&& object
->objq
.prev
!= NULL
); /* object should be on a queue */
6200 purgeable_q_t old_queue
=vm_purgeable_object_remove(object
);
6203 if (old_queue
!= queue
) {
6204 kern_return_t result
;
6206 /* Changing queue. Have to move token. */
6207 vm_page_lock_queues();
6208 vm_purgeable_token_delete_last(old_queue
);
6209 result
= vm_purgeable_token_add(queue
);
6210 vm_page_unlock_queues();
6212 assert(result
==KERN_SUCCESS
); /* this should never fail since we just freed a token */
6215 vm_purgeable_object_add(object
, queue
, (*state
&VM_VOLATILE_GROUP_MASK
)>>VM_VOLATILE_GROUP_SHIFT
);
6217 assert(queue
->debug_count_objects
>=0);
6222 case VM_PURGABLE_EMPTY
:
6223 if (object
->volatile_fault
) {
6227 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
6233 refmod
= pmap_disconnect(p
->phys_page
);
6234 if ((refmod
& VM_MEM_MODIFIED
) &&
6236 SET_PAGE_DIRTY(p
, FALSE
);
6241 if (old_state
!= new_state
) {
6242 assert(old_state
== VM_PURGABLE_NONVOLATILE
||
6243 old_state
== VM_PURGABLE_VOLATILE
);
6244 if (old_state
== VM_PURGABLE_VOLATILE
) {
6245 purgeable_q_t old_queue
;
6247 /* object should be on a queue */
6248 assert(object
->objq
.next
!= NULL
&&
6249 object
->objq
.prev
!= NULL
);
6250 old_queue
= vm_purgeable_object_remove(object
);
6252 vm_page_lock_queues();
6253 vm_purgeable_token_delete_last(old_queue
);
6254 vm_page_unlock_queues();
6256 (void) vm_object_purge(object
);
6263 return KERN_SUCCESS
;
6268 * vm_object_res_deallocate
6270 * (recursively) decrement residence counts on vm objects and their shadows.
6271 * Called from vm_object_deallocate and when swapping out an object.
6273 * The object is locked, and remains locked throughout the function,
6274 * even as we iterate down the shadow chain. Locks on intermediate objects
6275 * will be dropped, but not the original object.
6277 * NOTE: this function used to use recursion, rather than iteration.
6280 __private_extern__
void
6281 vm_object_res_deallocate(
6284 vm_object_t orig_object
= object
;
6286 * Object is locked so it can be called directly
6287 * from vm_object_deallocate. Original object is never
6290 assert(object
->res_count
> 0);
6291 while (--object
->res_count
== 0) {
6292 assert(object
->ref_count
>= object
->res_count
);
6293 vm_object_deactivate_all_pages(object
);
6294 /* iterate on shadow, if present */
6295 if (object
->shadow
!= VM_OBJECT_NULL
) {
6296 vm_object_t tmp_object
= object
->shadow
;
6297 vm_object_lock(tmp_object
);
6298 if (object
!= orig_object
)
6299 vm_object_unlock(object
);
6300 object
= tmp_object
;
6301 assert(object
->res_count
> 0);
6305 if (object
!= orig_object
)
6306 vm_object_unlock(object
);
6310 * vm_object_res_reference
6312 * Internal function to increment residence count on a vm object
6313 * and its shadows. It is called only from vm_object_reference, and
6314 * when swapping in a vm object, via vm_map_swap.
6316 * The object is locked, and remains locked throughout the function,
6317 * even as we iterate down the shadow chain. Locks on intermediate objects
6318 * will be dropped, but not the original object.
6320 * NOTE: this function used to use recursion, rather than iteration.
6323 __private_extern__
void
6324 vm_object_res_reference(
6327 vm_object_t orig_object
= object
;
6329 * Object is locked, so this can be called directly
6330 * from vm_object_reference. This lock is never released.
6332 while ((++object
->res_count
== 1) &&
6333 (object
->shadow
!= VM_OBJECT_NULL
)) {
6334 vm_object_t tmp_object
= object
->shadow
;
6336 assert(object
->ref_count
>= object
->res_count
);
6337 vm_object_lock(tmp_object
);
6338 if (object
!= orig_object
)
6339 vm_object_unlock(object
);
6340 object
= tmp_object
;
6342 if (object
!= orig_object
)
6343 vm_object_unlock(object
);
6344 assert(orig_object
->ref_count
>= orig_object
->res_count
);
6346 #endif /* TASK_SWAPPER */
6349 * vm_object_reference:
6351 * Gets another reference to the given object.
6353 #ifdef vm_object_reference
6354 #undef vm_object_reference
6356 __private_extern__
void
6357 vm_object_reference(
6358 register vm_object_t object
)
6360 if (object
== VM_OBJECT_NULL
)
6363 vm_object_lock(object
);
6364 assert(object
->ref_count
> 0);
6365 vm_object_reference_locked(object
);
6366 vm_object_unlock(object
);
6371 * Scale the vm_object_cache
6372 * This is required to make sure that the vm_object_cache is big
6373 * enough to effectively cache the mapped file.
6374 * This is really important with UBC as all the regular file vnodes
6375 * have memory object associated with them. Havving this cache too
6376 * small results in rapid reclaim of vnodes and hurts performance a LOT!
6378 * This is also needed as number of vnodes can be dynamically scaled.
6381 adjust_vm_object_cache(
6382 __unused vm_size_t oval
,
6383 __unused vm_size_t nval
)
6386 vm_object_cached_max
= nval
;
6387 vm_object_cache_trim(FALSE
);
6389 return (KERN_SUCCESS
);
6391 #endif /* MACH_BSD */
6395 * vm_object_transpose
6397 * This routine takes two VM objects of the same size and exchanges
6398 * their backing store.
6399 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
6400 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
6402 * The VM objects must not be locked by caller.
6404 unsigned int vm_object_transpose_count
= 0;
6406 vm_object_transpose(
6407 vm_object_t object1
,
6408 vm_object_t object2
,
6409 vm_object_size_t transpose_size
)
6411 vm_object_t tmp_object
;
6412 kern_return_t retval
;
6413 boolean_t object1_locked
, object2_locked
;
6415 vm_object_offset_t page_offset
;
6416 lck_mtx_t
*hash_lck
;
6417 vm_object_hash_entry_t hash_entry
;
6419 tmp_object
= VM_OBJECT_NULL
;
6420 object1_locked
= FALSE
; object2_locked
= FALSE
;
6422 if (object1
== object2
||
6423 object1
== VM_OBJECT_NULL
||
6424 object2
== VM_OBJECT_NULL
) {
6426 * If the 2 VM objects are the same, there's
6427 * no point in exchanging their backing store.
6429 retval
= KERN_INVALID_VALUE
;
6434 * Since we need to lock both objects at the same time,
6435 * make sure we always lock them in the same order to
6438 if (object1
> object2
) {
6439 tmp_object
= object1
;
6441 object2
= tmp_object
;
6445 * Allocate a temporary VM object to hold object1's contents
6446 * while we copy object2 to object1.
6448 tmp_object
= vm_object_allocate(transpose_size
);
6449 vm_object_lock(tmp_object
);
6450 tmp_object
->can_persist
= FALSE
;
6454 * Grab control of the 1st VM object.
6456 vm_object_lock(object1
);
6457 object1_locked
= TRUE
;
6458 if (!object1
->alive
|| object1
->terminating
||
6459 object1
->copy
|| object1
->shadow
|| object1
->shadowed
||
6460 object1
->purgable
!= VM_PURGABLE_DENY
) {
6462 * We don't deal with copy or shadow objects (yet).
6464 retval
= KERN_INVALID_VALUE
;
6468 * We're about to mess with the object's backing store and
6469 * taking a "paging_in_progress" reference wouldn't be enough
6470 * to prevent any paging activity on this object, so the caller should
6471 * have "quiesced" the objects beforehand, via a UPL operation with
6472 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
6473 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
6475 * Wait for any paging operation to complete (but only paging, not
6476 * other kind of activities not linked to the pager). After we're
6477 * statisfied that there's no more paging in progress, we keep the
6478 * object locked, to guarantee that no one tries to access its pager.
6480 vm_object_paging_only_wait(object1
, THREAD_UNINT
);
6483 * Same as above for the 2nd object...
6485 vm_object_lock(object2
);
6486 object2_locked
= TRUE
;
6487 if (! object2
->alive
|| object2
->terminating
||
6488 object2
->copy
|| object2
->shadow
|| object2
->shadowed
||
6489 object2
->purgable
!= VM_PURGABLE_DENY
) {
6490 retval
= KERN_INVALID_VALUE
;
6493 vm_object_paging_only_wait(object2
, THREAD_UNINT
);
6496 if (object1
->vo_size
!= object2
->vo_size
||
6497 object1
->vo_size
!= transpose_size
) {
6499 * If the 2 objects don't have the same size, we can't
6500 * exchange their backing stores or one would overflow.
6501 * If their size doesn't match the caller's
6502 * "transpose_size", we can't do it either because the
6503 * transpose operation will affect the entire span of
6506 retval
= KERN_INVALID_VALUE
;
6512 * Transpose the lists of resident pages.
6513 * This also updates the resident_page_count and the memq_hint.
6515 if (object1
->phys_contiguous
|| queue_empty(&object1
->memq
)) {
6517 * No pages in object1, just transfer pages
6518 * from object2 to object1. No need to go through
6519 * an intermediate object.
6521 while (!queue_empty(&object2
->memq
)) {
6522 page
= (vm_page_t
) queue_first(&object2
->memq
);
6523 vm_page_rename(page
, object1
, page
->offset
, FALSE
);
6525 assert(queue_empty(&object2
->memq
));
6526 } else if (object2
->phys_contiguous
|| queue_empty(&object2
->memq
)) {
6528 * No pages in object2, just transfer pages
6529 * from object1 to object2. No need to go through
6530 * an intermediate object.
6532 while (!queue_empty(&object1
->memq
)) {
6533 page
= (vm_page_t
) queue_first(&object1
->memq
);
6534 vm_page_rename(page
, object2
, page
->offset
, FALSE
);
6536 assert(queue_empty(&object1
->memq
));
6538 /* transfer object1's pages to tmp_object */
6539 while (!queue_empty(&object1
->memq
)) {
6540 page
= (vm_page_t
) queue_first(&object1
->memq
);
6541 page_offset
= page
->offset
;
6542 vm_page_remove(page
, TRUE
);
6543 page
->offset
= page_offset
;
6544 queue_enter(&tmp_object
->memq
, page
, vm_page_t
, listq
);
6546 assert(queue_empty(&object1
->memq
));
6547 /* transfer object2's pages to object1 */
6548 while (!queue_empty(&object2
->memq
)) {
6549 page
= (vm_page_t
) queue_first(&object2
->memq
);
6550 vm_page_rename(page
, object1
, page
->offset
, FALSE
);
6552 assert(queue_empty(&object2
->memq
));
6553 /* transfer tmp_object's pages to object1 */
6554 while (!queue_empty(&tmp_object
->memq
)) {
6555 page
= (vm_page_t
) queue_first(&tmp_object
->memq
);
6556 queue_remove(&tmp_object
->memq
, page
,
6558 vm_page_insert(page
, object2
, page
->offset
);
6560 assert(queue_empty(&tmp_object
->memq
));
6563 #define __TRANSPOSE_FIELD(field) \
6565 tmp_object->field = object1->field; \
6566 object1->field = object2->field; \
6567 object2->field = tmp_object->field; \
6570 /* "Lock" refers to the object not its contents */
6571 /* "size" should be identical */
6572 assert(object1
->vo_size
== object2
->vo_size
);
6573 /* "memq_hint" was updated above when transposing pages */
6574 /* "ref_count" refers to the object not its contents */
6576 /* "res_count" refers to the object not its contents */
6578 /* "resident_page_count" was updated above when transposing pages */
6579 /* "wired_page_count" was updated above when transposing pages */
6580 /* "reusable_page_count" was updated above when transposing pages */
6581 /* there should be no "copy" */
6582 assert(!object1
->copy
);
6583 assert(!object2
->copy
);
6584 /* there should be no "shadow" */
6585 assert(!object1
->shadow
);
6586 assert(!object2
->shadow
);
6587 __TRANSPOSE_FIELD(vo_shadow_offset
); /* used by phys_contiguous objects */
6588 __TRANSPOSE_FIELD(pager
);
6589 __TRANSPOSE_FIELD(paging_offset
);
6590 __TRANSPOSE_FIELD(pager_control
);
6591 /* update the memory_objects' pointers back to the VM objects */
6592 if (object1
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6593 memory_object_control_collapse(object1
->pager_control
,
6596 if (object2
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6597 memory_object_control_collapse(object2
->pager_control
,
6600 __TRANSPOSE_FIELD(copy_strategy
);
6601 /* "paging_in_progress" refers to the object not its contents */
6602 assert(!object1
->paging_in_progress
);
6603 assert(!object2
->paging_in_progress
);
6604 assert(object1
->activity_in_progress
);
6605 assert(object2
->activity_in_progress
);
6606 /* "all_wanted" refers to the object not its contents */
6607 __TRANSPOSE_FIELD(pager_created
);
6608 __TRANSPOSE_FIELD(pager_initialized
);
6609 __TRANSPOSE_FIELD(pager_ready
);
6610 __TRANSPOSE_FIELD(pager_trusted
);
6611 __TRANSPOSE_FIELD(can_persist
);
6612 __TRANSPOSE_FIELD(internal
);
6613 __TRANSPOSE_FIELD(temporary
);
6614 __TRANSPOSE_FIELD(private);
6615 __TRANSPOSE_FIELD(pageout
);
6616 /* "alive" should be set */
6617 assert(object1
->alive
);
6618 assert(object2
->alive
);
6619 /* "purgeable" should be non-purgeable */
6620 assert(object1
->purgable
== VM_PURGABLE_DENY
);
6621 assert(object2
->purgable
== VM_PURGABLE_DENY
);
6622 /* "shadowed" refers to the the object not its contents */
6623 __TRANSPOSE_FIELD(silent_overwrite
);
6624 __TRANSPOSE_FIELD(advisory_pageout
);
6625 __TRANSPOSE_FIELD(true_share
);
6626 /* "terminating" should not be set */
6627 assert(!object1
->terminating
);
6628 assert(!object2
->terminating
);
6629 __TRANSPOSE_FIELD(named
);
6630 /* "shadow_severed" refers to the object not its contents */
6631 __TRANSPOSE_FIELD(phys_contiguous
);
6632 __TRANSPOSE_FIELD(nophyscache
);
6633 /* "cached_list.next" points to transposed object */
6634 object1
->cached_list
.next
= (queue_entry_t
) object2
;
6635 object2
->cached_list
.next
= (queue_entry_t
) object1
;
6636 /* "cached_list.prev" should be NULL */
6637 assert(object1
->cached_list
.prev
== NULL
);
6638 assert(object2
->cached_list
.prev
== NULL
);
6639 /* "msr_q" is linked to the object not its contents */
6640 assert(queue_empty(&object1
->msr_q
));
6641 assert(queue_empty(&object2
->msr_q
));
6642 __TRANSPOSE_FIELD(last_alloc
);
6643 __TRANSPOSE_FIELD(sequential
);
6644 __TRANSPOSE_FIELD(pages_created
);
6645 __TRANSPOSE_FIELD(pages_used
);
6646 __TRANSPOSE_FIELD(scan_collisions
);
6648 __TRANSPOSE_FIELD(existence_map
);
6650 __TRANSPOSE_FIELD(cow_hint
);
6652 __TRANSPOSE_FIELD(paging_object
);
6654 __TRANSPOSE_FIELD(wimg_bits
);
6655 __TRANSPOSE_FIELD(set_cache_attr
);
6656 __TRANSPOSE_FIELD(code_signed
);
6657 if (object1
->hashed
) {
6658 hash_lck
= vm_object_hash_lock_spin(object2
->pager
);
6659 hash_entry
= vm_object_hash_lookup(object2
->pager
, FALSE
);
6660 assert(hash_entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
6661 hash_entry
->object
= object2
;
6662 vm_object_hash_unlock(hash_lck
);
6664 if (object2
->hashed
) {
6665 hash_lck
= vm_object_hash_lock_spin(object1
->pager
);
6666 hash_entry
= vm_object_hash_lookup(object1
->pager
, FALSE
);
6667 assert(hash_entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
6668 hash_entry
->object
= object1
;
6669 vm_object_hash_unlock(hash_lck
);
6671 __TRANSPOSE_FIELD(hashed
);
6672 object1
->transposed
= TRUE
;
6673 object2
->transposed
= TRUE
;
6674 __TRANSPOSE_FIELD(mapping_in_progress
);
6675 __TRANSPOSE_FIELD(volatile_empty
);
6676 __TRANSPOSE_FIELD(volatile_fault
);
6677 __TRANSPOSE_FIELD(all_reusable
);
6678 assert(object1
->blocked_access
);
6679 assert(object2
->blocked_access
);
6680 assert(object1
->__object2_unused_bits
== 0);
6681 assert(object2
->__object2_unused_bits
== 0);
6683 /* "uplq" refers to the object not its contents (see upl_transpose()) */
6685 assert(object1
->objq
.next
== NULL
);
6686 assert(object1
->objq
.prev
== NULL
);
6687 assert(object2
->objq
.next
== NULL
);
6688 assert(object2
->objq
.prev
== NULL
);
6690 #undef __TRANSPOSE_FIELD
6692 retval
= KERN_SUCCESS
;
6698 if (tmp_object
!= VM_OBJECT_NULL
) {
6699 vm_object_unlock(tmp_object
);
6701 * Re-initialize the temporary object to avoid
6702 * deallocating a real pager.
6704 _vm_object_allocate(transpose_size
, tmp_object
);
6705 vm_object_deallocate(tmp_object
);
6706 tmp_object
= VM_OBJECT_NULL
;
6709 if (object1_locked
) {
6710 vm_object_unlock(object1
);
6711 object1_locked
= FALSE
;
6713 if (object2_locked
) {
6714 vm_object_unlock(object2
);
6715 object2_locked
= FALSE
;
6718 vm_object_transpose_count
++;
6725 * vm_object_cluster_size
6727 * Determine how big a cluster we should issue an I/O for...
6729 * Inputs: *start == offset of page needed
6730 * *length == maximum cluster pager can handle
6731 * Outputs: *start == beginning offset of cluster
6732 * *length == length of cluster to try
6734 * The original *start will be encompassed by the cluster
6737 extern int speculative_reads_disabled
;
6738 extern int ignore_is_ssd
;
6741 unsigned int preheat_pages_max
= MAX_UPL_TRANSFER
;
6742 unsigned int preheat_pages_min
= 10;
6744 unsigned int preheat_pages_max
= MAX_UPL_TRANSFER
;
6745 unsigned int preheat_pages_min
= 8;
6748 uint32_t pre_heat_scaling
[MAX_UPL_TRANSFER
+ 1];
6749 uint32_t pre_heat_cluster
[MAX_UPL_TRANSFER
+ 1];
6752 __private_extern__
void
6753 vm_object_cluster_size(vm_object_t object
, vm_object_offset_t
*start
,
6754 vm_size_t
*length
, vm_object_fault_info_t fault_info
, uint32_t *io_streaming
)
6756 vm_size_t pre_heat_size
;
6757 vm_size_t tail_size
;
6758 vm_size_t head_size
;
6759 vm_size_t max_length
;
6760 vm_size_t cluster_size
;
6761 vm_object_offset_t object_size
;
6762 vm_object_offset_t orig_start
;
6763 vm_object_offset_t target_start
;
6764 vm_object_offset_t offset
;
6765 vm_behavior_t behavior
;
6766 boolean_t look_behind
= TRUE
;
6767 boolean_t look_ahead
= TRUE
;
6768 boolean_t isSSD
= FALSE
;
6769 uint32_t throttle_limit
;
6771 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6772 unsigned int max_ph_size
;
6773 unsigned int min_ph_size
;
6774 unsigned int min_ph_size_in_bytes
;
6776 assert( !(*length
& PAGE_MASK
));
6777 assert( !(*start
& PAGE_MASK_64
));
6780 * remember maxiumum length of run requested
6782 max_length
= *length
;
6784 * we'll always return a cluster size of at least
6785 * 1 page, since the original fault must always
6788 *length
= PAGE_SIZE
;
6791 if (speculative_reads_disabled
|| fault_info
== NULL
) {
6793 * no cluster... just fault the page in
6797 orig_start
= *start
;
6798 target_start
= orig_start
;
6799 cluster_size
= round_page(fault_info
->cluster_size
);
6800 behavior
= fault_info
->behavior
;
6802 vm_object_lock(object
);
6804 if (object
->pager
== MEMORY_OBJECT_NULL
)
6805 goto out
; /* pager is gone for this object, nothing more to do */
6808 vnode_pager_get_isSSD(object
->pager
, &isSSD
);
6810 min_ph_size
= preheat_pages_min
;
6811 max_ph_size
= preheat_pages_max
;
6817 if (min_ph_size
< 1)
6820 if (max_ph_size
< 1)
6822 else if (max_ph_size
> MAX_UPL_TRANSFER
)
6823 max_ph_size
= MAX_UPL_TRANSFER
;
6825 if (max_length
> (max_ph_size
* PAGE_SIZE
))
6826 max_length
= max_ph_size
* PAGE_SIZE
;
6828 if (max_length
<= PAGE_SIZE
)
6831 min_ph_size_in_bytes
= min_ph_size
* PAGE_SIZE
;
6833 if (object
->internal
)
6834 object_size
= object
->vo_size
;
6836 vnode_pager_get_object_size(object
->pager
, &object_size
);
6838 object_size
= round_page_64(object_size
);
6840 if (orig_start
>= object_size
) {
6842 * fault occurred beyond the EOF...
6843 * we need to punt w/o changing the
6848 if (object
->pages_used
> object
->pages_created
) {
6850 * must have wrapped our 32 bit counters
6853 object
->pages_used
= object
->pages_created
= 0;
6855 if ((sequential_run
= object
->sequential
)) {
6856 if (sequential_run
< 0) {
6857 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
6858 sequential_run
= 0 - sequential_run
;
6860 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6867 behavior
= VM_BEHAVIOR_DEFAULT
;
6869 case VM_BEHAVIOR_DEFAULT
:
6870 if (object
->internal
&& fault_info
->user_tag
== VM_MEMORY_STACK
)
6873 if (sequential_run
>= (3 * PAGE_SIZE
)) {
6874 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6876 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
)
6877 look_behind
= FALSE
;
6884 if (object
->pages_created
< (20 * min_ph_size
)) {
6888 pre_heat_size
= min_ph_size_in_bytes
;
6891 * Linear growth in PH size: The maximum size is max_length...
6892 * this cacluation will result in a size that is neither a
6893 * power of 2 nor a multiple of PAGE_SIZE... so round
6894 * it up to the nearest PAGE_SIZE boundary
6896 pre_heat_size
= (max_length
* object
->pages_used
) / object
->pages_created
;
6898 if (pre_heat_size
< min_ph_size_in_bytes
)
6899 pre_heat_size
= min_ph_size_in_bytes
;
6901 pre_heat_size
= round_page(pre_heat_size
);
6906 case VM_BEHAVIOR_RANDOM
:
6907 if ((pre_heat_size
= cluster_size
) <= PAGE_SIZE
)
6911 case VM_BEHAVIOR_SEQUENTIAL
:
6912 if ((pre_heat_size
= cluster_size
) == 0)
6913 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6914 look_behind
= FALSE
;
6919 case VM_BEHAVIOR_RSEQNTL
:
6920 if ((pre_heat_size
= cluster_size
) == 0)
6921 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6928 throttle_limit
= (uint32_t) max_length
;
6929 assert(throttle_limit
== max_length
);
6931 if (vnode_pager_check_hard_throttle(object
->pager
, &throttle_limit
, *io_streaming
) == KERN_SUCCESS
) {
6932 if (max_length
> throttle_limit
)
6933 max_length
= throttle_limit
;
6935 if (pre_heat_size
> max_length
)
6936 pre_heat_size
= max_length
;
6938 if (behavior
== VM_BEHAVIOR_DEFAULT
&& (pre_heat_size
> min_ph_size_in_bytes
)) {
6940 unsigned int consider_free
= vm_page_free_count
+ vm_page_cleaned_count
;
6942 if (consider_free
< vm_page_throttle_limit
) {
6943 pre_heat_size
= trunc_page(pre_heat_size
/ 16);
6944 } else if (consider_free
< vm_page_free_target
) {
6945 pre_heat_size
= trunc_page(pre_heat_size
/ 4);
6948 if (pre_heat_size
< min_ph_size_in_bytes
)
6949 pre_heat_size
= min_ph_size_in_bytes
;
6951 if (look_ahead
== TRUE
) {
6952 if (look_behind
== TRUE
) {
6954 * if we get here its due to a random access...
6955 * so we want to center the original fault address
6956 * within the cluster we will issue... make sure
6957 * to calculate 'head_size' as a multiple of PAGE_SIZE...
6958 * 'pre_heat_size' is a multiple of PAGE_SIZE but not
6959 * necessarily an even number of pages so we need to truncate
6960 * the result to a PAGE_SIZE boundary
6962 head_size
= trunc_page(pre_heat_size
/ 2);
6964 if (target_start
> head_size
)
6965 target_start
-= head_size
;
6970 * 'target_start' at this point represents the beginning offset
6971 * of the cluster we are considering... 'orig_start' will be in
6972 * the center of this cluster if we didn't have to clip the start
6973 * due to running into the start of the file
6976 if ((target_start
+ pre_heat_size
) > object_size
)
6977 pre_heat_size
= (vm_size_t
)(round_page_64(object_size
- target_start
));
6979 * at this point caclulate the number of pages beyond the original fault
6980 * address that we want to consider... this is guaranteed not to extend beyond
6981 * the current EOF...
6983 assert((vm_size_t
)(orig_start
- target_start
) == (orig_start
- target_start
));
6984 tail_size
= pre_heat_size
- (vm_size_t
)(orig_start
- target_start
) - PAGE_SIZE
;
6986 if (pre_heat_size
> target_start
) {
6988 * since pre_heat_size is always smaller then 2^32,
6989 * if it is larger then target_start (a 64 bit value)
6990 * it is safe to clip target_start to 32 bits
6992 pre_heat_size
= (vm_size_t
) target_start
;
6996 assert( !(target_start
& PAGE_MASK_64
));
6997 assert( !(pre_heat_size
& PAGE_MASK
));
6999 pre_heat_scaling
[pre_heat_size
/ PAGE_SIZE
]++;
7001 if (pre_heat_size
<= PAGE_SIZE
)
7004 if (look_behind
== TRUE
) {
7006 * take a look at the pages before the original
7007 * faulting offset... recalculate this in case
7008 * we had to clip 'pre_heat_size' above to keep
7009 * from running past the EOF.
7011 head_size
= pre_heat_size
- tail_size
- PAGE_SIZE
;
7013 for (offset
= orig_start
- PAGE_SIZE_64
; head_size
; offset
-= PAGE_SIZE_64
, head_size
-= PAGE_SIZE
) {
7015 * don't poke below the lowest offset
7017 if (offset
< fault_info
->lo_offset
)
7020 * for external objects and internal objects w/o an existence map
7021 * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN
7024 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
7026 * we know for a fact that the pager can't provide the page
7027 * so don't include it or any pages beyond it in this cluster
7032 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
7034 * don't bridge resident pages
7039 *length
+= PAGE_SIZE
;
7042 if (look_ahead
== TRUE
) {
7043 for (offset
= orig_start
+ PAGE_SIZE_64
; tail_size
; offset
+= PAGE_SIZE_64
, tail_size
-= PAGE_SIZE
) {
7045 * don't poke above the highest offset
7047 if (offset
>= fault_info
->hi_offset
)
7049 assert(offset
< object_size
);
7052 * for external objects and internal objects w/o an existence map
7053 * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN
7056 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
7058 * we know for a fact that the pager can't provide the page
7059 * so don't include it or any pages beyond it in this cluster
7064 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
7066 * don't bridge resident pages
7070 *length
+= PAGE_SIZE
;
7074 if (*length
> max_length
)
7075 *length
= max_length
;
7077 pre_heat_cluster
[*length
/ PAGE_SIZE
]++;
7079 vm_object_unlock(object
);
7081 DTRACE_VM1(clustersize
, vm_size_t
, *length
);
7086 * Allow manipulation of individual page state. This is actually part of
7087 * the UPL regimen but takes place on the VM object rather than on a UPL
7093 vm_object_offset_t offset
,
7095 ppnum_t
*phys_entry
,
7100 vm_object_lock(object
);
7102 if(ops
& UPL_POP_PHYSICAL
) {
7103 if(object
->phys_contiguous
) {
7105 *phys_entry
= (ppnum_t
)
7106 (object
->vo_shadow_offset
>> PAGE_SHIFT
);
7108 vm_object_unlock(object
);
7109 return KERN_SUCCESS
;
7111 vm_object_unlock(object
);
7112 return KERN_INVALID_OBJECT
;
7115 if(object
->phys_contiguous
) {
7116 vm_object_unlock(object
);
7117 return KERN_INVALID_OBJECT
;
7121 if((dst_page
= vm_page_lookup(object
,offset
)) == VM_PAGE_NULL
) {
7122 vm_object_unlock(object
);
7123 return KERN_FAILURE
;
7126 /* Sync up on getting the busy bit */
7127 if((dst_page
->busy
|| dst_page
->cleaning
) &&
7128 (((ops
& UPL_POP_SET
) &&
7129 (ops
& UPL_POP_BUSY
)) || (ops
& UPL_POP_DUMP
))) {
7130 /* someone else is playing with the page, we will */
7132 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7136 if (ops
& UPL_POP_DUMP
) {
7137 if (dst_page
->pmapped
== TRUE
)
7138 pmap_disconnect(dst_page
->phys_page
);
7140 VM_PAGE_FREE(dst_page
);
7147 /* Get the condition of flags before requested ops */
7148 /* are undertaken */
7150 if(dst_page
->dirty
) *flags
|= UPL_POP_DIRTY
;
7151 if(dst_page
->pageout
) *flags
|= UPL_POP_PAGEOUT
;
7152 if(dst_page
->precious
) *flags
|= UPL_POP_PRECIOUS
;
7153 if(dst_page
->absent
) *flags
|= UPL_POP_ABSENT
;
7154 if(dst_page
->busy
) *flags
|= UPL_POP_BUSY
;
7157 /* The caller should have made a call either contingent with */
7158 /* or prior to this call to set UPL_POP_BUSY */
7159 if(ops
& UPL_POP_SET
) {
7160 /* The protection granted with this assert will */
7161 /* not be complete. If the caller violates the */
7162 /* convention and attempts to change page state */
7163 /* without first setting busy we may not see it */
7164 /* because the page may already be busy. However */
7165 /* if such violations occur we will assert sooner */
7167 assert(dst_page
->busy
|| (ops
& UPL_POP_BUSY
));
7168 if (ops
& UPL_POP_DIRTY
) {
7169 SET_PAGE_DIRTY(dst_page
, FALSE
);
7171 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= TRUE
;
7172 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= TRUE
;
7173 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= TRUE
;
7174 if (ops
& UPL_POP_BUSY
) dst_page
->busy
= TRUE
;
7177 if(ops
& UPL_POP_CLR
) {
7178 assert(dst_page
->busy
);
7179 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= FALSE
;
7180 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= FALSE
;
7181 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= FALSE
;
7182 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= FALSE
;
7183 if (ops
& UPL_POP_BUSY
) {
7184 dst_page
->busy
= FALSE
;
7185 PAGE_WAKEUP(dst_page
);
7189 if (dst_page
->encrypted
) {
7192 * We need to decrypt this encrypted page before the
7193 * caller can access its contents.
7194 * But if the caller really wants to access the page's
7195 * contents, they have to keep the page "busy".
7196 * Otherwise, the page could get recycled or re-encrypted
7199 if ((ops
& UPL_POP_SET
) && (ops
& UPL_POP_BUSY
) &&
7202 * The page is stable enough to be accessed by
7203 * the caller, so make sure its contents are
7206 vm_page_decrypt(dst_page
, 0);
7209 * The page is not busy, so don't bother
7210 * decrypting it, since anything could
7211 * happen to it between now and when the
7212 * caller wants to access it.
7213 * We should not give the caller access
7216 assert(!phys_entry
);
7222 * The physical page number will remain valid
7223 * only if the page is kept busy.
7224 * ENCRYPTED SWAP: make sure we don't let the
7225 * caller access an encrypted page.
7227 assert(dst_page
->busy
);
7228 assert(!dst_page
->encrypted
);
7229 *phys_entry
= dst_page
->phys_page
;
7235 vm_object_unlock(object
);
7236 return KERN_SUCCESS
;
7241 * vm_object_range_op offers performance enhancement over
7242 * vm_object_page_op for page_op functions which do not require page
7243 * level state to be returned from the call. Page_op was created to provide
7244 * a low-cost alternative to page manipulation via UPLs when only a single
7245 * page was involved. The range_op call establishes the ability in the _op
7246 * family of functions to work on multiple pages where the lack of page level
7247 * state handling allows the caller to avoid the overhead of the upl structures.
7253 vm_object_offset_t offset_beg
,
7254 vm_object_offset_t offset_end
,
7258 vm_object_offset_t offset
;
7261 if (offset_end
- offset_beg
> (uint32_t) -1) {
7262 /* range is too big and would overflow "*range" */
7263 return KERN_INVALID_ARGUMENT
;
7265 if (object
->resident_page_count
== 0) {
7267 if (ops
& UPL_ROP_PRESENT
) {
7270 *range
= (uint32_t) (offset_end
- offset_beg
);
7271 assert(*range
== (offset_end
- offset_beg
));
7274 return KERN_SUCCESS
;
7276 vm_object_lock(object
);
7278 if (object
->phys_contiguous
) {
7279 vm_object_unlock(object
);
7280 return KERN_INVALID_OBJECT
;
7283 offset
= offset_beg
& ~PAGE_MASK_64
;
7285 while (offset
< offset_end
) {
7286 dst_page
= vm_page_lookup(object
, offset
);
7287 if (dst_page
!= VM_PAGE_NULL
) {
7288 if (ops
& UPL_ROP_DUMP
) {
7289 if (dst_page
->busy
|| dst_page
->cleaning
) {
7291 * someone else is playing with the
7292 * page, we will have to wait
7294 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7296 * need to relook the page up since it's
7297 * state may have changed while we slept
7298 * it might even belong to a different object
7303 if (dst_page
->laundry
) {
7304 dst_page
->pageout
= FALSE
;
7306 vm_pageout_steal_laundry(dst_page
, FALSE
);
7308 if (dst_page
->pmapped
== TRUE
)
7309 pmap_disconnect(dst_page
->phys_page
);
7311 VM_PAGE_FREE(dst_page
);
7313 } else if ((ops
& UPL_ROP_ABSENT
) && !dst_page
->absent
)
7315 } else if (ops
& UPL_ROP_PRESENT
)
7318 offset
+= PAGE_SIZE
;
7320 vm_object_unlock(object
);
7323 if (offset
> offset_end
)
7324 offset
= offset_end
;
7325 if(offset
> offset_beg
) {
7326 *range
= (uint32_t) (offset
- offset_beg
);
7327 assert(*range
== (offset
- offset_beg
));
7332 return KERN_SUCCESS
;
7336 uint32_t scan_object_collision
= 0;
7339 vm_object_lock(vm_object_t object
)
7341 if (object
== vm_pageout_scan_wants_object
) {
7342 scan_object_collision
++;
7345 lck_rw_lock_exclusive(&object
->Lock
);
7349 vm_object_lock_avoid(vm_object_t object
)
7351 if (object
== vm_pageout_scan_wants_object
) {
7352 scan_object_collision
++;
7359 _vm_object_lock_try(vm_object_t object
)
7361 return (lck_rw_try_lock_exclusive(&object
->Lock
));
7365 vm_object_lock_try(vm_object_t object
)
7368 * Called from hibernate path so check before blocking.
7370 if (vm_object_lock_avoid(object
) && ml_get_interrupts_enabled() && get_preemption_level()==0) {
7373 return _vm_object_lock_try(object
);
7377 vm_object_lock_shared(vm_object_t object
)
7379 if (vm_object_lock_avoid(object
)) {
7382 lck_rw_lock_shared(&object
->Lock
);
7386 vm_object_lock_try_shared(vm_object_t object
)
7388 if (vm_object_lock_avoid(object
)) {
7391 return (lck_rw_try_lock_shared(&object
->Lock
));
7395 unsigned int vm_object_change_wimg_mode_count
= 0;
7398 * The object must be locked
7401 vm_object_change_wimg_mode(vm_object_t object
, unsigned int wimg_mode
)
7405 vm_object_lock_assert_exclusive(object
);
7407 vm_object_paging_wait(object
, THREAD_UNINT
);
7409 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
7412 pmap_set_cache_attributes(p
->phys_page
, wimg_mode
);
7414 if (wimg_mode
== VM_WIMG_USE_DEFAULT
)
7415 object
->set_cache_attr
= FALSE
;
7417 object
->set_cache_attr
= TRUE
;
7419 object
->wimg_bits
= wimg_mode
;
7421 vm_object_change_wimg_mode_count
++;
7426 kern_return_t
vm_object_pack(
7427 unsigned int *purgeable_count
,
7428 unsigned int *wired_count
,
7429 unsigned int *clean_count
,
7430 unsigned int *dirty_count
,
7431 unsigned int dirty_budget
,
7433 vm_object_t src_object
,
7434 struct default_freezer_handle
*df_handle
)
7436 kern_return_t kr
= KERN_SUCCESS
;
7438 vm_object_lock(src_object
);
7440 *purgeable_count
= *wired_count
= *clean_count
= *dirty_count
= 0;
7443 if (!src_object
->alive
|| src_object
->terminating
){
7448 if (src_object
->purgable
== VM_PURGABLE_VOLATILE
) {
7449 *purgeable_count
= src_object
->resident_page_count
;
7451 /* If the default freezer handle is null, we're just walking the pages to discover how many can be hibernated */
7452 if (df_handle
!= NULL
) {
7453 purgeable_q_t queue
;
7454 /* object should be on a queue */
7455 assert(src_object
->objq
.next
!= NULL
&&
7456 src_object
->objq
.prev
!= NULL
);
7457 queue
= vm_purgeable_object_remove(src_object
);
7459 vm_page_lock_queues();
7460 vm_purgeable_token_delete_first(queue
);
7461 vm_page_unlock_queues();
7462 vm_object_purge(src_object
);
7467 if (src_object
->ref_count
== 1) {
7468 vm_object_pack_pages(wired_count
, clean_count
, dirty_count
, dirty_budget
, src_object
, df_handle
);
7470 if (src_object
->internal
) {
7475 vm_object_unlock(src_object
);
7482 vm_object_pack_pages(
7483 unsigned int *wired_count
,
7484 unsigned int *clean_count
,
7485 unsigned int *dirty_count
,
7486 unsigned int dirty_budget
,
7487 vm_object_t src_object
,
7488 struct default_freezer_handle
*df_handle
)
7492 next
= (vm_page_t
)queue_first(&src_object
->memq
);
7494 while (!queue_end(&src_object
->memq
, (queue_entry_t
)next
)) {
7496 next
= (vm_page_t
)queue_next(&next
->listq
);
7498 /* Finish up if we've hit our pageout limit */
7499 if (dirty_budget
&& (dirty_budget
== *dirty_count
)) {
7502 assert(!p
->laundry
);
7504 if (p
->fictitious
|| p
->busy
)
7507 if (p
->absent
|| p
->unusual
|| p
->error
)
7510 if (VM_PAGE_WIRED(p
)) {
7515 if (df_handle
== NULL
) {
7516 if (p
->dirty
|| pmap_is_modified(p
->phys_page
)) {
7529 if (p
->pmapped
== TRUE
) {
7531 refmod_state
= pmap_disconnect(p
->phys_page
);
7532 if (refmod_state
& VM_MEM_MODIFIED
) {
7533 SET_PAGE_DIRTY(p
, FALSE
);
7538 default_freezer_pack_page(p
, df_handle
);
7554 assert(object
!= VM_OBJECT_NULL
);
7556 vm_object_lock(object
);
7558 next
= (vm_page_t
)queue_first(&object
->memq
);
7560 while (!queue_end(&object
->memq
, (queue_entry_t
)next
)) {
7562 next
= (vm_page_t
)queue_next(&next
->listq
);
7564 /* Throw to the pageout queue */
7565 vm_page_lockspin_queues();
7568 * see if page is already in the process of
7569 * being cleaned... if so, leave it alone
7572 VM_PAGE_QUEUES_REMOVE(p
);
7573 vm_pageout_cluster(p
, TRUE
);
7575 vm_page_unlock_queues();
7578 vm_object_unlock(object
);
7585 memory_object_t pager
;
7588 vm_object_lock(object
);
7590 pager
= object
->pager
;
7592 if (!object
->pager_ready
|| pager
== MEMORY_OBJECT_NULL
) {
7593 vm_object_unlock(object
);
7594 return KERN_FAILURE
;
7597 vm_object_paging_wait(object
, THREAD_UNINT
);
7598 vm_object_paging_begin(object
);
7600 object
->blocked_access
= TRUE
;
7601 vm_object_unlock(object
);
7603 kr
= memory_object_data_reclaim(pager
, TRUE
);
7605 vm_object_lock(object
);
7607 object
->blocked_access
= FALSE
;
7608 vm_object_paging_end(object
);
7610 vm_object_unlock(object
);
7614 #endif /* CONFIG_FREEZE */