]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_object.c
83b3a0eebcd9d07c072cbb4937ebf6cd36ad5c11
[apple/xnu.git] / osfmk / vm / vm_object.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: vm/vm_object.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
61 *
62 * Virtual memory object module.
63 */
64
65 #include <mach_pagemap.h>
66 #include <task_swapper.h>
67
68 #include <mach/mach_types.h>
69 #include <mach/memory_object.h>
70 #include <mach/memory_object_default.h>
71 #include <mach/memory_object_control_server.h>
72 #include <mach/vm_param.h>
73
74 #include <ipc/ipc_types.h>
75 #include <ipc/ipc_port.h>
76
77 #include <kern/kern_types.h>
78 #include <kern/assert.h>
79 #include <kern/lock.h>
80 #include <kern/queue.h>
81 #include <kern/xpr.h>
82 #include <kern/zalloc.h>
83 #include <kern/host.h>
84 #include <kern/host_statistics.h>
85 #include <kern/processor.h>
86 #include <kern/misc_protos.h>
87
88 #include <vm/memory_object.h>
89 #include <vm/vm_fault.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_pageout.h>
94 #include <vm/vm_protos.h>
95
96 /*
97 * Virtual memory objects maintain the actual data
98 * associated with allocated virtual memory. A given
99 * page of memory exists within exactly one object.
100 *
101 * An object is only deallocated when all "references"
102 * are given up.
103 *
104 * Associated with each object is a list of all resident
105 * memory pages belonging to that object; this list is
106 * maintained by the "vm_page" module, but locked by the object's
107 * lock.
108 *
109 * Each object also records the memory object reference
110 * that is used by the kernel to request and write
111 * back data (the memory object, field "pager"), etc...
112 *
113 * Virtual memory objects are allocated to provide
114 * zero-filled memory (vm_allocate) or map a user-defined
115 * memory object into a virtual address space (vm_map).
116 *
117 * Virtual memory objects that refer to a user-defined
118 * memory object are called "permanent", because all changes
119 * made in virtual memory are reflected back to the
120 * memory manager, which may then store it permanently.
121 * Other virtual memory objects are called "temporary",
122 * meaning that changes need be written back only when
123 * necessary to reclaim pages, and that storage associated
124 * with the object can be discarded once it is no longer
125 * mapped.
126 *
127 * A permanent memory object may be mapped into more
128 * than one virtual address space. Moreover, two threads
129 * may attempt to make the first mapping of a memory
130 * object concurrently. Only one thread is allowed to
131 * complete this mapping; all others wait for the
132 * "pager_initialized" field is asserted, indicating
133 * that the first thread has initialized all of the
134 * necessary fields in the virtual memory object structure.
135 *
136 * The kernel relies on a *default memory manager* to
137 * provide backing storage for the zero-filled virtual
138 * memory objects. The pager memory objects associated
139 * with these temporary virtual memory objects are only
140 * requested from the default memory manager when it
141 * becomes necessary. Virtual memory objects
142 * that depend on the default memory manager are called
143 * "internal". The "pager_created" field is provided to
144 * indicate whether these ports have ever been allocated.
145 *
146 * The kernel may also create virtual memory objects to
147 * hold changed pages after a copy-on-write operation.
148 * In this case, the virtual memory object (and its
149 * backing storage -- its memory object) only contain
150 * those pages that have been changed. The "shadow"
151 * field refers to the virtual memory object that contains
152 * the remainder of the contents. The "shadow_offset"
153 * field indicates where in the "shadow" these contents begin.
154 * The "copy" field refers to a virtual memory object
155 * to which changed pages must be copied before changing
156 * this object, in order to implement another form
157 * of copy-on-write optimization.
158 *
159 * The virtual memory object structure also records
160 * the attributes associated with its memory object.
161 * The "pager_ready", "can_persist" and "copy_strategy"
162 * fields represent those attributes. The "cached_list"
163 * field is used in the implementation of the persistence
164 * attribute.
165 *
166 * ZZZ Continue this comment.
167 */
168
169 /* Forward declarations for internal functions. */
170 static kern_return_t vm_object_terminate(
171 vm_object_t object);
172
173 extern void vm_object_remove(
174 vm_object_t object);
175
176 static vm_object_t vm_object_cache_trim(
177 boolean_t called_from_vm_object_deallocate);
178
179 static void vm_object_deactivate_all_pages(
180 vm_object_t object);
181
182 static kern_return_t vm_object_copy_call(
183 vm_object_t src_object,
184 vm_object_offset_t src_offset,
185 vm_object_size_t size,
186 vm_object_t *_result_object);
187
188 static void vm_object_do_collapse(
189 vm_object_t object,
190 vm_object_t backing_object);
191
192 static void vm_object_do_bypass(
193 vm_object_t object,
194 vm_object_t backing_object);
195
196 static void vm_object_release_pager(
197 memory_object_t pager);
198
199 static zone_t vm_object_zone; /* vm backing store zone */
200
201 /*
202 * All wired-down kernel memory belongs to a single virtual
203 * memory object (kernel_object) to avoid wasting data structures.
204 */
205 static struct vm_object kernel_object_store;
206 vm_object_t kernel_object;
207
208 /*
209 * The submap object is used as a placeholder for vm_map_submap
210 * operations. The object is declared in vm_map.c because it
211 * is exported by the vm_map module. The storage is declared
212 * here because it must be initialized here.
213 */
214 static struct vm_object vm_submap_object_store;
215
216 /*
217 * Virtual memory objects are initialized from
218 * a template (see vm_object_allocate).
219 *
220 * When adding a new field to the virtual memory
221 * object structure, be sure to add initialization
222 * (see _vm_object_allocate()).
223 */
224 static struct vm_object vm_object_template;
225
226 /*
227 * Virtual memory objects that are not referenced by
228 * any address maps, but that are allowed to persist
229 * (an attribute specified by the associated memory manager),
230 * are kept in a queue (vm_object_cached_list).
231 *
232 * When an object from this queue is referenced again,
233 * for example to make another address space mapping,
234 * it must be removed from the queue. That is, the
235 * queue contains *only* objects with zero references.
236 *
237 * The kernel may choose to terminate objects from this
238 * queue in order to reclaim storage. The current policy
239 * is to permit a fixed maximum number of unreferenced
240 * objects (vm_object_cached_max).
241 *
242 * A spin lock (accessed by routines
243 * vm_object_cache_{lock,lock_try,unlock}) governs the
244 * object cache. It must be held when objects are
245 * added to or removed from the cache (in vm_object_terminate).
246 * The routines that acquire a reference to a virtual
247 * memory object based on one of the memory object ports
248 * must also lock the cache.
249 *
250 * Ideally, the object cache should be more isolated
251 * from the reference mechanism, so that the lock need
252 * not be held to make simple references.
253 */
254 static queue_head_t vm_object_cached_list;
255 static int vm_object_cached_count=0;
256 static int vm_object_cached_high; /* highest # cached objects */
257 static int vm_object_cached_max = 512; /* may be patched*/
258
259 static decl_mutex_data(,vm_object_cached_lock_data)
260
261 #define vm_object_cache_lock() \
262 mutex_lock(&vm_object_cached_lock_data)
263 #define vm_object_cache_lock_try() \
264 mutex_try(&vm_object_cached_lock_data)
265 #define vm_object_cache_unlock() \
266 mutex_unlock(&vm_object_cached_lock_data)
267
268 #define VM_OBJECT_HASH_COUNT 1024
269 static queue_head_t vm_object_hashtable[VM_OBJECT_HASH_COUNT];
270 static struct zone *vm_object_hash_zone;
271
272 struct vm_object_hash_entry {
273 queue_chain_t hash_link; /* hash chain link */
274 memory_object_t pager; /* pager we represent */
275 vm_object_t object; /* corresponding object */
276 boolean_t waiting; /* someone waiting for
277 * termination */
278 };
279
280 typedef struct vm_object_hash_entry *vm_object_hash_entry_t;
281 #define VM_OBJECT_HASH_ENTRY_NULL ((vm_object_hash_entry_t) 0)
282
283 #define VM_OBJECT_HASH_SHIFT 8
284 #define vm_object_hash(pager) \
285 ((((unsigned)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_COUNT)
286
287 void vm_object_hash_entry_free(
288 vm_object_hash_entry_t entry);
289
290 static void vm_object_reap(vm_object_t object);
291 static void vm_object_reap_async(vm_object_t object);
292 static void vm_object_reaper_thread(void);
293 static queue_head_t vm_object_reaper_queue; /* protected by vm_object_cache_lock() */
294 unsigned int vm_object_reap_count = 0;
295 unsigned int vm_object_reap_count_async = 0;
296
297 /*
298 * vm_object_hash_lookup looks up a pager in the hashtable
299 * and returns the corresponding entry, with optional removal.
300 */
301
302 static vm_object_hash_entry_t
303 vm_object_hash_lookup(
304 memory_object_t pager,
305 boolean_t remove_entry)
306 {
307 register queue_t bucket;
308 register vm_object_hash_entry_t entry;
309
310 bucket = &vm_object_hashtable[vm_object_hash(pager)];
311
312 entry = (vm_object_hash_entry_t)queue_first(bucket);
313 while (!queue_end(bucket, (queue_entry_t)entry)) {
314 if (entry->pager == pager && !remove_entry)
315 return(entry);
316 else if (entry->pager == pager) {
317 queue_remove(bucket, entry,
318 vm_object_hash_entry_t, hash_link);
319 return(entry);
320 }
321
322 entry = (vm_object_hash_entry_t)queue_next(&entry->hash_link);
323 }
324
325 return(VM_OBJECT_HASH_ENTRY_NULL);
326 }
327
328 /*
329 * vm_object_hash_enter enters the specified
330 * pager / cache object association in the hashtable.
331 */
332
333 static void
334 vm_object_hash_insert(
335 vm_object_hash_entry_t entry)
336 {
337 register queue_t bucket;
338
339 bucket = &vm_object_hashtable[vm_object_hash(entry->pager)];
340
341 queue_enter(bucket, entry, vm_object_hash_entry_t, hash_link);
342 }
343
344 static vm_object_hash_entry_t
345 vm_object_hash_entry_alloc(
346 memory_object_t pager)
347 {
348 vm_object_hash_entry_t entry;
349
350 entry = (vm_object_hash_entry_t)zalloc(vm_object_hash_zone);
351 entry->pager = pager;
352 entry->object = VM_OBJECT_NULL;
353 entry->waiting = FALSE;
354
355 return(entry);
356 }
357
358 void
359 vm_object_hash_entry_free(
360 vm_object_hash_entry_t entry)
361 {
362 zfree(vm_object_hash_zone, entry);
363 }
364
365 /*
366 * vm_object_allocate:
367 *
368 * Returns a new object with the given size.
369 */
370
371 __private_extern__ void
372 _vm_object_allocate(
373 vm_object_size_t size,
374 vm_object_t object)
375 {
376 XPR(XPR_VM_OBJECT,
377 "vm_object_allocate, object 0x%X size 0x%X\n",
378 (integer_t)object, size, 0,0,0);
379
380 *object = vm_object_template;
381 queue_init(&object->memq);
382 queue_init(&object->msr_q);
383 #ifdef UPL_DEBUG
384 queue_init(&object->uplq);
385 #endif /* UPL_DEBUG */
386 vm_object_lock_init(object);
387 object->size = size;
388 }
389
390 __private_extern__ vm_object_t
391 vm_object_allocate(
392 vm_object_size_t size)
393 {
394 register vm_object_t object;
395
396 object = (vm_object_t) zalloc(vm_object_zone);
397
398 // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
399
400 if (object != VM_OBJECT_NULL)
401 _vm_object_allocate(size, object);
402
403 return object;
404 }
405
406 /*
407 * vm_object_bootstrap:
408 *
409 * Initialize the VM objects module.
410 */
411 __private_extern__ void
412 vm_object_bootstrap(void)
413 {
414 register int i;
415
416 vm_object_zone = zinit((vm_size_t) sizeof(struct vm_object),
417 round_page_32(512*1024),
418 round_page_32(12*1024),
419 "vm objects");
420
421 queue_init(&vm_object_cached_list);
422 mutex_init(&vm_object_cached_lock_data, 0);
423
424 vm_object_hash_zone =
425 zinit((vm_size_t) sizeof (struct vm_object_hash_entry),
426 round_page_32(512*1024),
427 round_page_32(12*1024),
428 "vm object hash entries");
429
430 for (i = 0; i < VM_OBJECT_HASH_COUNT; i++)
431 queue_init(&vm_object_hashtable[i]);
432
433 /*
434 * Fill in a template object, for quick initialization
435 */
436
437 /* memq; Lock; init after allocation */
438 vm_object_template.size = 0;
439 vm_object_template.memq_hint = VM_PAGE_NULL;
440 vm_object_template.ref_count = 1;
441 #if TASK_SWAPPER
442 vm_object_template.res_count = 1;
443 #endif /* TASK_SWAPPER */
444 vm_object_template.resident_page_count = 0;
445 vm_object_template.copy = VM_OBJECT_NULL;
446 vm_object_template.shadow = VM_OBJECT_NULL;
447 vm_object_template.shadow_offset = (vm_object_offset_t) 0;
448 vm_object_template.cow_hint = ~(vm_offset_t)0;
449 vm_object_template.true_share = FALSE;
450
451 vm_object_template.pager = MEMORY_OBJECT_NULL;
452 vm_object_template.paging_offset = 0;
453 vm_object_template.pager_control = MEMORY_OBJECT_CONTROL_NULL;
454 /* msr_q; init after allocation */
455
456 vm_object_template.copy_strategy = MEMORY_OBJECT_COPY_SYMMETRIC;
457 vm_object_template.absent_count = 0;
458 vm_object_template.paging_in_progress = 0;
459
460 /* Begin bitfields */
461 vm_object_template.all_wanted = 0; /* all bits FALSE */
462 vm_object_template.pager_created = FALSE;
463 vm_object_template.pager_initialized = FALSE;
464 vm_object_template.pager_ready = FALSE;
465 vm_object_template.pager_trusted = FALSE;
466 vm_object_template.can_persist = FALSE;
467 vm_object_template.internal = TRUE;
468 vm_object_template.temporary = TRUE;
469 vm_object_template.private = FALSE;
470 vm_object_template.pageout = FALSE;
471 vm_object_template.alive = TRUE;
472 vm_object_template.purgable = VM_OBJECT_NONPURGABLE;
473 vm_object_template.silent_overwrite = FALSE;
474 vm_object_template.advisory_pageout = FALSE;
475 vm_object_template.shadowed = FALSE;
476 vm_object_template.terminating = FALSE;
477 vm_object_template.shadow_severed = FALSE;
478 vm_object_template.phys_contiguous = FALSE;
479 vm_object_template.nophyscache = FALSE;
480 /* End bitfields */
481
482 /* cache bitfields */
483 vm_object_template.wimg_bits = VM_WIMG_DEFAULT;
484
485 /* cached_list; init after allocation */
486 vm_object_template.last_alloc = (vm_object_offset_t) 0;
487 vm_object_template.cluster_size = 0;
488 #if MACH_PAGEMAP
489 vm_object_template.existence_map = VM_EXTERNAL_NULL;
490 #endif /* MACH_PAGEMAP */
491 #if MACH_ASSERT
492 vm_object_template.paging_object = VM_OBJECT_NULL;
493 #endif /* MACH_ASSERT */
494
495 /*
496 * Initialize the "kernel object"
497 */
498
499 kernel_object = &kernel_object_store;
500
501 /*
502 * Note that in the following size specifications, we need to add 1 because
503 * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size.
504 */
505
506 #ifdef ppc
507 _vm_object_allocate((vm_last_addr - VM_MIN_KERNEL_ADDRESS) + 1,
508 kernel_object);
509 #else
510 _vm_object_allocate((VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) + 1,
511 kernel_object);
512 #endif
513 kernel_object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
514
515 /*
516 * Initialize the "submap object". Make it as large as the
517 * kernel object so that no limit is imposed on submap sizes.
518 */
519
520 vm_submap_object = &vm_submap_object_store;
521 #ifdef ppc
522 _vm_object_allocate((vm_last_addr - VM_MIN_KERNEL_ADDRESS) + 1,
523 vm_submap_object);
524 #else
525 _vm_object_allocate((VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) + 1,
526 vm_submap_object);
527 #endif
528 vm_submap_object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
529
530 /*
531 * Create an "extra" reference to this object so that we never
532 * try to deallocate it; zfree doesn't like to be called with
533 * non-zone memory.
534 */
535 vm_object_reference(vm_submap_object);
536
537 #if MACH_PAGEMAP
538 vm_external_module_initialize();
539 #endif /* MACH_PAGEMAP */
540 }
541
542 void
543 vm_object_reaper_init(void)
544 {
545 kern_return_t kr;
546 thread_t thread;
547
548 queue_init(&vm_object_reaper_queue);
549 kr = kernel_thread_start_priority(
550 (thread_continue_t) vm_object_reaper_thread,
551 NULL,
552 BASEPRI_PREEMPT - 1,
553 &thread);
554 if (kr != KERN_SUCCESS) {
555 panic("failed to launch vm_object_reaper_thread kr=0x%x\n", kr);
556 }
557 thread_deallocate(thread);
558 }
559
560 __private_extern__ void
561 vm_object_init(void)
562 {
563 /*
564 * Finish initializing the kernel object.
565 */
566 }
567
568 /* remove the typedef below when emergency work-around is taken out */
569 typedef struct vnode_pager {
570 memory_object_t pager;
571 memory_object_t pager_handle; /* pager */
572 memory_object_control_t control_handle; /* memory object's control handle */
573 void *vnode_handle; /* vnode handle */
574 } *vnode_pager_t;
575
576 #define MIGHT_NOT_CACHE_SHADOWS 1
577 #if MIGHT_NOT_CACHE_SHADOWS
578 static int cache_shadows = TRUE;
579 #endif /* MIGHT_NOT_CACHE_SHADOWS */
580
581 /*
582 * vm_object_deallocate:
583 *
584 * Release a reference to the specified object,
585 * gained either through a vm_object_allocate
586 * or a vm_object_reference call. When all references
587 * are gone, storage associated with this object
588 * may be relinquished.
589 *
590 * No object may be locked.
591 */
592 __private_extern__ void
593 vm_object_deallocate(
594 register vm_object_t object)
595 {
596 boolean_t retry_cache_trim = FALSE;
597 vm_object_t shadow = VM_OBJECT_NULL;
598
599 // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
600 // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
601
602
603 while (object != VM_OBJECT_NULL) {
604
605 /*
606 * The cache holds a reference (uncounted) to
607 * the object; we must lock it before removing
608 * the object.
609 */
610 for (;;) {
611 vm_object_cache_lock();
612
613 /*
614 * if we try to take a regular lock here
615 * we risk deadlocking against someone
616 * holding a lock on this object while
617 * trying to vm_object_deallocate a different
618 * object
619 */
620 if (vm_object_lock_try(object))
621 break;
622 vm_object_cache_unlock();
623 mutex_pause(); /* wait a bit */
624 }
625 assert(object->ref_count > 0);
626
627 /*
628 * If the object has a named reference, and only
629 * that reference would remain, inform the pager
630 * about the last "mapping" reference going away.
631 */
632 if ((object->ref_count == 2) && (object->named)) {
633 memory_object_t pager = object->pager;
634
635 /* Notify the Pager that there are no */
636 /* more mappers for this object */
637
638 if (pager != MEMORY_OBJECT_NULL) {
639 vm_object_unlock(object);
640 vm_object_cache_unlock();
641
642 memory_object_unmap(pager);
643
644 for (;;) {
645 vm_object_cache_lock();
646
647 /*
648 * if we try to take a regular lock here
649 * we risk deadlocking against someone
650 * holding a lock on this object while
651 * trying to vm_object_deallocate a different
652 * object
653 */
654 if (vm_object_lock_try(object))
655 break;
656 vm_object_cache_unlock();
657 mutex_pause(); /* wait a bit */
658 }
659 assert(object->ref_count > 0);
660 }
661 }
662
663 /*
664 * Lose the reference. If other references
665 * remain, then we are done, unless we need
666 * to retry a cache trim.
667 * If it is the last reference, then keep it
668 * until any pending initialization is completed.
669 */
670
671 /* if the object is terminating, it cannot go into */
672 /* the cache and we obviously should not call */
673 /* terminate again. */
674
675 if ((object->ref_count > 1) || object->terminating) {
676 object->ref_count--;
677 vm_object_res_deallocate(object);
678 vm_object_cache_unlock();
679
680 if (object->ref_count == 1 &&
681 object->shadow != VM_OBJECT_NULL) {
682 /*
683 * There's only one reference left on this
684 * VM object. We can't tell if it's a valid
685 * one (from a mapping for example) or if this
686 * object is just part of a possibly stale and
687 * useless shadow chain.
688 * We would like to try and collapse it into
689 * its parent, but we don't have any pointers
690 * back to this parent object.
691 * But we can try and collapse this object with
692 * its own shadows, in case these are useless
693 * too...
694 * We can't bypass this object though, since we
695 * don't know if this last reference on it is
696 * meaningful or not.
697 */
698 vm_object_collapse(object, 0, FALSE);
699 }
700
701 vm_object_unlock(object);
702 if (retry_cache_trim &&
703 ((object = vm_object_cache_trim(TRUE)) !=
704 VM_OBJECT_NULL)) {
705 continue;
706 }
707 return;
708 }
709
710 /*
711 * We have to wait for initialization
712 * before destroying or caching the object.
713 */
714
715 if (object->pager_created && ! object->pager_initialized) {
716 assert(! object->can_persist);
717 vm_object_assert_wait(object,
718 VM_OBJECT_EVENT_INITIALIZED,
719 THREAD_UNINT);
720 vm_object_unlock(object);
721 vm_object_cache_unlock();
722 thread_block(THREAD_CONTINUE_NULL);
723 continue;
724 }
725
726 /*
727 * If this object can persist, then enter it in
728 * the cache. Otherwise, terminate it.
729 *
730 * NOTE: Only permanent objects are cached, and
731 * permanent objects cannot have shadows. This
732 * affects the residence counting logic in a minor
733 * way (can do it in-line, mostly).
734 */
735
736 if ((object->can_persist) && (object->alive)) {
737 /*
738 * Now it is safe to decrement reference count,
739 * and to return if reference count is > 0.
740 */
741 if (--object->ref_count > 0) {
742 vm_object_res_deallocate(object);
743 vm_object_unlock(object);
744 vm_object_cache_unlock();
745 if (retry_cache_trim &&
746 ((object = vm_object_cache_trim(TRUE)) !=
747 VM_OBJECT_NULL)) {
748 continue;
749 }
750 return;
751 }
752
753 #if MIGHT_NOT_CACHE_SHADOWS
754 /*
755 * Remove shadow now if we don't
756 * want to cache shadows.
757 */
758 if (! cache_shadows) {
759 shadow = object->shadow;
760 object->shadow = VM_OBJECT_NULL;
761 }
762 #endif /* MIGHT_NOT_CACHE_SHADOWS */
763
764 /*
765 * Enter the object onto the queue of
766 * cached objects, and deactivate
767 * all of its pages.
768 */
769 assert(object->shadow == VM_OBJECT_NULL);
770 VM_OBJ_RES_DECR(object);
771 XPR(XPR_VM_OBJECT,
772 "vm_o_deallocate: adding %x to cache, queue = (%x, %x)\n",
773 (integer_t)object,
774 (integer_t)vm_object_cached_list.next,
775 (integer_t)vm_object_cached_list.prev,0,0);
776
777 vm_object_cached_count++;
778 if (vm_object_cached_count > vm_object_cached_high)
779 vm_object_cached_high = vm_object_cached_count;
780 queue_enter(&vm_object_cached_list, object,
781 vm_object_t, cached_list);
782 vm_object_cache_unlock();
783 vm_object_deactivate_all_pages(object);
784 vm_object_unlock(object);
785
786 #if MIGHT_NOT_CACHE_SHADOWS
787 /*
788 * If we have a shadow that we need
789 * to deallocate, do so now, remembering
790 * to trim the cache later.
791 */
792 if (! cache_shadows && shadow != VM_OBJECT_NULL) {
793 object = shadow;
794 retry_cache_trim = TRUE;
795 continue;
796 }
797 #endif /* MIGHT_NOT_CACHE_SHADOWS */
798
799 /*
800 * Trim the cache. If the cache trim
801 * returns with a shadow for us to deallocate,
802 * then remember to retry the cache trim
803 * when we are done deallocating the shadow.
804 * Otherwise, we are done.
805 */
806
807 object = vm_object_cache_trim(TRUE);
808 if (object == VM_OBJECT_NULL) {
809 return;
810 }
811 retry_cache_trim = TRUE;
812
813 } else {
814 /*
815 * This object is not cachable; terminate it.
816 */
817 XPR(XPR_VM_OBJECT,
818 "vm_o_deallocate: !cacheable 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
819 (integer_t)object, object->resident_page_count,
820 object->paging_in_progress,
821 (void *)current_thread(),object->ref_count);
822
823 VM_OBJ_RES_DECR(object); /* XXX ? */
824 /*
825 * Terminate this object. If it had a shadow,
826 * then deallocate it; otherwise, if we need
827 * to retry a cache trim, do so now; otherwise,
828 * we are done. "pageout" objects have a shadow,
829 * but maintain a "paging reference" rather than
830 * a normal reference.
831 */
832 shadow = object->pageout?VM_OBJECT_NULL:object->shadow;
833 if(vm_object_terminate(object) != KERN_SUCCESS) {
834 return;
835 }
836 if (shadow != VM_OBJECT_NULL) {
837 object = shadow;
838 continue;
839 }
840 if (retry_cache_trim &&
841 ((object = vm_object_cache_trim(TRUE)) !=
842 VM_OBJECT_NULL)) {
843 continue;
844 }
845 return;
846 }
847 }
848 assert(! retry_cache_trim);
849 }
850
851 /*
852 * Check to see whether we really need to trim
853 * down the cache. If so, remove an object from
854 * the cache, terminate it, and repeat.
855 *
856 * Called with, and returns with, cache lock unlocked.
857 */
858 vm_object_t
859 vm_object_cache_trim(
860 boolean_t called_from_vm_object_deallocate)
861 {
862 register vm_object_t object = VM_OBJECT_NULL;
863 vm_object_t shadow;
864
865 for (;;) {
866
867 /*
868 * If we no longer need to trim the cache,
869 * then we are done.
870 */
871
872 vm_object_cache_lock();
873 if (vm_object_cached_count <= vm_object_cached_max) {
874 vm_object_cache_unlock();
875 return VM_OBJECT_NULL;
876 }
877
878 /*
879 * We must trim down the cache, so remove
880 * the first object in the cache.
881 */
882 XPR(XPR_VM_OBJECT,
883 "vm_object_cache_trim: removing from front of cache (%x, %x)\n",
884 (integer_t)vm_object_cached_list.next,
885 (integer_t)vm_object_cached_list.prev, 0, 0, 0);
886
887 object = (vm_object_t) queue_first(&vm_object_cached_list);
888 if(object == (vm_object_t) &vm_object_cached_list) {
889 /* something's wrong with the calling parameter or */
890 /* the value of vm_object_cached_count, just fix */
891 /* and return */
892 if(vm_object_cached_max < 0)
893 vm_object_cached_max = 0;
894 vm_object_cached_count = 0;
895 vm_object_cache_unlock();
896 return VM_OBJECT_NULL;
897 }
898 vm_object_lock(object);
899 queue_remove(&vm_object_cached_list, object, vm_object_t,
900 cached_list);
901 vm_object_cached_count--;
902
903 /*
904 * Since this object is in the cache, we know
905 * that it is initialized and has no references.
906 * Take a reference to avoid recursive deallocations.
907 */
908
909 assert(object->pager_initialized);
910 assert(object->ref_count == 0);
911 object->ref_count++;
912
913 /*
914 * Terminate the object.
915 * If the object had a shadow, we let vm_object_deallocate
916 * deallocate it. "pageout" objects have a shadow, but
917 * maintain a "paging reference" rather than a normal
918 * reference.
919 * (We are careful here to limit recursion.)
920 */
921 shadow = object->pageout?VM_OBJECT_NULL:object->shadow;
922 if(vm_object_terminate(object) != KERN_SUCCESS)
923 continue;
924 if (shadow != VM_OBJECT_NULL) {
925 if (called_from_vm_object_deallocate) {
926 return shadow;
927 } else {
928 vm_object_deallocate(shadow);
929 }
930 }
931 }
932 }
933
934 boolean_t vm_object_terminate_remove_all = FALSE;
935
936 /*
937 * Routine: vm_object_terminate
938 * Purpose:
939 * Free all resources associated with a vm_object.
940 * In/out conditions:
941 * Upon entry, the object must be locked,
942 * and the object must have exactly one reference.
943 *
944 * The shadow object reference is left alone.
945 *
946 * The object must be unlocked if its found that pages
947 * must be flushed to a backing object. If someone
948 * manages to map the object while it is being flushed
949 * the object is returned unlocked and unchanged. Otherwise,
950 * upon exit, the cache will be unlocked, and the
951 * object will cease to exist.
952 */
953 static kern_return_t
954 vm_object_terminate(
955 register vm_object_t object)
956 {
957 register vm_page_t p;
958 vm_object_t shadow_object;
959
960 XPR(XPR_VM_OBJECT, "vm_object_terminate, object 0x%X ref %d\n",
961 (integer_t)object, object->ref_count, 0, 0, 0);
962
963 if (!object->pageout && (!object->temporary || object->can_persist)
964 && (object->pager != NULL || object->shadow_severed)) {
965 vm_object_cache_unlock();
966 while (!queue_empty(&object->memq)) {
967 /*
968 * Clear pager_trusted bit so that the pages get yanked
969 * out of the object instead of cleaned in place. This
970 * prevents a deadlock in XMM and makes more sense anyway.
971 */
972 object->pager_trusted = FALSE;
973
974 p = (vm_page_t) queue_first(&object->memq);
975
976 VM_PAGE_CHECK(p);
977
978 if (p->busy || p->cleaning) {
979 if(p->cleaning || p->absent) {
980 vm_object_paging_wait(object, THREAD_UNINT);
981 continue;
982 } else {
983 panic("vm_object_terminate.3 0x%x 0x%x", object, p);
984 }
985 }
986
987 vm_page_lock_queues();
988 p->busy = TRUE;
989 VM_PAGE_QUEUES_REMOVE(p);
990 vm_page_unlock_queues();
991
992 if (p->absent || p->private) {
993
994 /*
995 * For private pages, VM_PAGE_FREE just
996 * leaves the page structure around for
997 * its owner to clean up. For absent
998 * pages, the structure is returned to
999 * the appropriate pool.
1000 */
1001
1002 goto free_page;
1003 }
1004
1005 if (p->fictitious)
1006 panic("vm_object_terminate.4 0x%x 0x%x", object, p);
1007
1008 if (!p->dirty)
1009 p->dirty = pmap_is_modified(p->phys_page);
1010
1011 if ((p->dirty || p->precious) && !p->error && object->alive) {
1012 vm_pageout_cluster(p); /* flush page */
1013 vm_object_paging_wait(object, THREAD_UNINT);
1014 XPR(XPR_VM_OBJECT,
1015 "vm_object_terminate restart, object 0x%X ref %d\n",
1016 (integer_t)object, object->ref_count, 0, 0, 0);
1017 } else {
1018 free_page:
1019 VM_PAGE_FREE(p);
1020 }
1021 }
1022 vm_object_unlock(object);
1023 vm_object_cache_lock();
1024 vm_object_lock(object);
1025 }
1026
1027 /*
1028 * Make sure the object isn't already being terminated
1029 */
1030 if(object->terminating) {
1031 object->ref_count -= 1;
1032 assert(object->ref_count > 0);
1033 vm_object_cache_unlock();
1034 vm_object_unlock(object);
1035 return KERN_FAILURE;
1036 }
1037
1038 /*
1039 * Did somebody get a reference to the object while we were
1040 * cleaning it?
1041 */
1042 if(object->ref_count != 1) {
1043 object->ref_count -= 1;
1044 assert(object->ref_count > 0);
1045 vm_object_res_deallocate(object);
1046 vm_object_cache_unlock();
1047 vm_object_unlock(object);
1048 return KERN_FAILURE;
1049 }
1050
1051 /*
1052 * Make sure no one can look us up now.
1053 */
1054
1055 object->terminating = TRUE;
1056 object->alive = FALSE;
1057 vm_object_remove(object);
1058
1059 /*
1060 * Detach the object from its shadow if we are the shadow's
1061 * copy. The reference we hold on the shadow must be dropped
1062 * by our caller.
1063 */
1064 if (((shadow_object = object->shadow) != VM_OBJECT_NULL) &&
1065 !(object->pageout)) {
1066 vm_object_lock(shadow_object);
1067 if (shadow_object->copy == object)
1068 shadow_object->copy = VM_OBJECT_NULL;
1069 vm_object_unlock(shadow_object);
1070 }
1071
1072 if (FALSE && object->paging_in_progress != 0) {
1073 /*
1074 * There are still some paging_in_progress references
1075 * on this object, meaning that there are some paging
1076 * or other I/O operations in progress for this VM object.
1077 * Such operations take some paging_in_progress references
1078 * up front to ensure that the object doesn't go away, but
1079 * they may also need to acquire a reference on the VM object,
1080 * to map it in kernel space, for example. That means that
1081 * they may end up releasing the last reference on the VM
1082 * object, triggering its termination, while still holding
1083 * paging_in_progress references. Waiting for these
1084 * pending paging_in_progress references to go away here would
1085 * deadlock.
1086 *
1087 * To avoid deadlocking, we'll let the vm_object_reaper_thread
1088 * complete the VM object termination if it still holds
1089 * paging_in_progress references at this point.
1090 *
1091 * No new paging_in_progress should appear now that the
1092 * VM object is "terminating" and not "alive".
1093 */
1094 vm_object_reap_async(object);
1095 vm_object_cache_unlock();
1096 vm_object_unlock(object);
1097 return KERN_SUCCESS;
1098 }
1099
1100 /* complete the VM object termination */
1101 vm_object_reap(object);
1102 object = VM_OBJECT_NULL;
1103 /* cache lock and object lock were released by vm_object_reap() */
1104
1105 return KERN_SUCCESS;
1106 }
1107
1108 /*
1109 * vm_object_reap():
1110 *
1111 * Complete the termination of a VM object after it's been marked
1112 * as "terminating" and "!alive" by vm_object_terminate().
1113 *
1114 * The VM object cache and the VM object must be locked by caller.
1115 * The locks will be released on return and the VM object is no longer valid.
1116 */
1117 void
1118 vm_object_reap(
1119 vm_object_t object)
1120 {
1121 memory_object_t pager;
1122 vm_page_t p;
1123
1124 #if DEBUG
1125 mutex_assert(&vm_object_cached_lock_data, MA_OWNED);
1126 mutex_assert(&object->Lock, MA_OWNED);
1127 #endif /* DEBUG */
1128
1129 vm_object_reap_count++;
1130
1131 /*
1132 * The pageout daemon might be playing with our pages.
1133 * Now that the object is dead, it won't touch any more
1134 * pages, but some pages might already be on their way out.
1135 * Hence, we wait until the active paging activities have
1136 * ceased before we break the association with the pager
1137 * itself.
1138 */
1139 while (object->paging_in_progress != 0) {
1140 vm_object_cache_unlock();
1141 vm_object_wait(object,
1142 VM_OBJECT_EVENT_PAGING_IN_PROGRESS,
1143 THREAD_UNINT);
1144 vm_object_cache_lock();
1145 vm_object_lock(object);
1146 }
1147
1148 assert(object->paging_in_progress == 0);
1149 pager = object->pager;
1150 object->pager = MEMORY_OBJECT_NULL;
1151
1152 if (pager != MEMORY_OBJECT_NULL)
1153 memory_object_control_disable(object->pager_control);
1154 vm_object_cache_unlock();
1155
1156 object->ref_count--;
1157 #if TASK_SWAPPER
1158 assert(object->res_count == 0);
1159 #endif /* TASK_SWAPPER */
1160
1161 assert (object->ref_count == 0);
1162
1163 /*
1164 * Clean or free the pages, as appropriate.
1165 * It is possible for us to find busy/absent pages,
1166 * if some faults on this object were aborted.
1167 */
1168 if (object->pageout) {
1169 assert(object->shadow != VM_OBJECT_NULL);
1170
1171 vm_pageout_object_terminate(object);
1172
1173 } else if ((object->temporary && !object->can_persist) ||
1174 (pager == MEMORY_OBJECT_NULL)) {
1175 while (!queue_empty(&object->memq)) {
1176 p = (vm_page_t) queue_first(&object->memq);
1177
1178 VM_PAGE_CHECK(p);
1179 VM_PAGE_FREE(p);
1180 }
1181 } else if (!queue_empty(&object->memq)) {
1182 panic("vm_object_reap: queue just emptied isn't");
1183 }
1184
1185 assert(object->paging_in_progress == 0);
1186 assert(object->ref_count == 0);
1187
1188 /*
1189 * If the pager has not already been released by
1190 * vm_object_destroy, we need to terminate it and
1191 * release our reference to it here.
1192 */
1193 if (pager != MEMORY_OBJECT_NULL) {
1194 vm_object_unlock(object);
1195 vm_object_release_pager(pager);
1196 vm_object_lock(object);
1197 }
1198
1199 /* kick off anyone waiting on terminating */
1200 object->terminating = FALSE;
1201 vm_object_paging_begin(object);
1202 vm_object_paging_end(object);
1203 vm_object_unlock(object);
1204
1205 #if MACH_PAGEMAP
1206 vm_external_destroy(object->existence_map, object->size);
1207 #endif /* MACH_PAGEMAP */
1208
1209 /*
1210 * Free the space for the object.
1211 */
1212 zfree(vm_object_zone, object);
1213 object = VM_OBJECT_NULL;
1214 }
1215
1216 void
1217 vm_object_reap_async(
1218 vm_object_t object)
1219 {
1220 #if DEBUG
1221 mutex_assert(&vm_object_cached_lock_data, MA_OWNED);
1222 mutex_assert(&object->Lock, MA_OWNED);
1223 #endif /* DEBUG */
1224
1225 vm_object_reap_count_async++;
1226
1227 /* enqueue the VM object... */
1228 queue_enter(&vm_object_reaper_queue, object,
1229 vm_object_t, cached_list);
1230 /* ... and wake up the reaper thread */
1231 thread_wakeup((event_t) &vm_object_reaper_queue);
1232 }
1233
1234 void
1235 vm_object_reaper_thread(void)
1236 {
1237 vm_object_t object;
1238
1239 vm_object_cache_lock();
1240
1241 while (!queue_empty(&vm_object_reaper_queue)) {
1242 queue_remove_first(&vm_object_reaper_queue,
1243 object,
1244 vm_object_t,
1245 cached_list);
1246 vm_object_lock(object);
1247 assert(object->terminating);
1248 assert(!object->alive);
1249
1250 vm_object_reap(object);
1251 /* cache is unlocked and object is no longer valid */
1252 object = VM_OBJECT_NULL;
1253
1254 vm_object_cache_lock();
1255 }
1256
1257 /* wait for more work... */
1258 assert_wait((event_t) &vm_object_reaper_queue, THREAD_UNINT);
1259 vm_object_cache_unlock();
1260 thread_block((thread_continue_t) vm_object_reaper_thread);
1261 /*NOTREACHED*/
1262 }
1263
1264 /*
1265 * Routine: vm_object_pager_wakeup
1266 * Purpose: Wake up anyone waiting for termination of a pager.
1267 */
1268
1269 static void
1270 vm_object_pager_wakeup(
1271 memory_object_t pager)
1272 {
1273 vm_object_hash_entry_t entry;
1274 boolean_t waiting = FALSE;
1275
1276 /*
1277 * If anyone was waiting for the memory_object_terminate
1278 * to be queued, wake them up now.
1279 */
1280 vm_object_cache_lock();
1281 entry = vm_object_hash_lookup(pager, TRUE);
1282 if (entry != VM_OBJECT_HASH_ENTRY_NULL)
1283 waiting = entry->waiting;
1284 vm_object_cache_unlock();
1285 if (entry != VM_OBJECT_HASH_ENTRY_NULL) {
1286 if (waiting)
1287 thread_wakeup((event_t) pager);
1288 vm_object_hash_entry_free(entry);
1289 }
1290 }
1291
1292 /*
1293 * Routine: vm_object_release_pager
1294 * Purpose: Terminate the pager and, upon completion,
1295 * release our last reference to it.
1296 * just like memory_object_terminate, except
1297 * that we wake up anyone blocked in vm_object_enter
1298 * waiting for termination message to be queued
1299 * before calling memory_object_init.
1300 */
1301 static void
1302 vm_object_release_pager(
1303 memory_object_t pager)
1304 {
1305
1306 /*
1307 * Terminate the pager.
1308 */
1309
1310 (void) memory_object_terminate(pager);
1311
1312 /*
1313 * Wakeup anyone waiting for this terminate
1314 */
1315 vm_object_pager_wakeup(pager);
1316
1317 /*
1318 * Release reference to pager.
1319 */
1320 memory_object_deallocate(pager);
1321 }
1322
1323 /*
1324 * Routine: vm_object_destroy
1325 * Purpose:
1326 * Shut down a VM object, despite the
1327 * presence of address map (or other) references
1328 * to the vm_object.
1329 */
1330 kern_return_t
1331 vm_object_destroy(
1332 vm_object_t object,
1333 __unused kern_return_t reason)
1334 {
1335 memory_object_t old_pager;
1336
1337 if (object == VM_OBJECT_NULL)
1338 return(KERN_SUCCESS);
1339
1340 /*
1341 * Remove the pager association immediately.
1342 *
1343 * This will prevent the memory manager from further
1344 * meddling. [If it wanted to flush data or make
1345 * other changes, it should have done so before performing
1346 * the destroy call.]
1347 */
1348
1349 vm_object_cache_lock();
1350 vm_object_lock(object);
1351 object->can_persist = FALSE;
1352 object->named = FALSE;
1353 object->alive = FALSE;
1354
1355 /*
1356 * Rip out the pager from the vm_object now...
1357 */
1358
1359 vm_object_remove(object);
1360 old_pager = object->pager;
1361 object->pager = MEMORY_OBJECT_NULL;
1362 if (old_pager != MEMORY_OBJECT_NULL)
1363 memory_object_control_disable(object->pager_control);
1364 vm_object_cache_unlock();
1365
1366 /*
1367 * Wait for the existing paging activity (that got
1368 * through before we nulled out the pager) to subside.
1369 */
1370
1371 vm_object_paging_wait(object, THREAD_UNINT);
1372 vm_object_unlock(object);
1373
1374 /*
1375 * Terminate the object now.
1376 */
1377 if (old_pager != MEMORY_OBJECT_NULL) {
1378 vm_object_release_pager(old_pager);
1379
1380 /*
1381 * JMM - Release the caller's reference. This assumes the
1382 * caller had a reference to release, which is a big (but
1383 * currently valid) assumption if this is driven from the
1384 * vnode pager (it is holding a named reference when making
1385 * this call)..
1386 */
1387 vm_object_deallocate(object);
1388
1389 }
1390 return(KERN_SUCCESS);
1391 }
1392
1393 /*
1394 * vm_object_deactivate_pages
1395 *
1396 * Deactivate all pages in the specified object. (Keep its pages
1397 * in memory even though it is no longer referenced.)
1398 *
1399 * The object must be locked.
1400 */
1401 static void
1402 vm_object_deactivate_all_pages(
1403 register vm_object_t object)
1404 {
1405 register vm_page_t p;
1406
1407 queue_iterate(&object->memq, p, vm_page_t, listq) {
1408 vm_page_lock_queues();
1409 if (!p->busy)
1410 vm_page_deactivate(p);
1411 vm_page_unlock_queues();
1412 }
1413 }
1414
1415 __private_extern__ void
1416 vm_object_deactivate_pages(
1417 vm_object_t object,
1418 vm_object_offset_t offset,
1419 vm_object_size_t size,
1420 boolean_t kill_page)
1421 {
1422 vm_object_t orig_object;
1423 int pages_moved = 0;
1424 int pages_found = 0;
1425
1426 /*
1427 * entered with object lock held, acquire a paging reference to
1428 * prevent the memory_object and control ports from
1429 * being destroyed.
1430 */
1431 orig_object = object;
1432
1433 for (;;) {
1434 register vm_page_t m;
1435 vm_object_offset_t toffset;
1436 vm_object_size_t tsize;
1437
1438 vm_object_paging_begin(object);
1439 vm_page_lock_queues();
1440
1441 for (tsize = size, toffset = offset; tsize; tsize -= PAGE_SIZE, toffset += PAGE_SIZE) {
1442
1443 if ((m = vm_page_lookup(object, toffset)) != VM_PAGE_NULL) {
1444
1445 pages_found++;
1446
1447 if ((m->wire_count == 0) && (!m->private) && (!m->gobbled) && (!m->busy)) {
1448
1449 assert(!m->laundry);
1450
1451 m->reference = FALSE;
1452 pmap_clear_reference(m->phys_page);
1453
1454 if ((kill_page) && (object->internal)) {
1455 m->precious = FALSE;
1456 m->dirty = FALSE;
1457 pmap_clear_modify(m->phys_page);
1458 vm_external_state_clr(object->existence_map, offset);
1459 }
1460 VM_PAGE_QUEUES_REMOVE(m);
1461
1462 assert(!m->laundry);
1463 assert(m->object != kernel_object);
1464 assert(m->pageq.next == NULL &&
1465 m->pageq.prev == NULL);
1466 if(m->zero_fill) {
1467 queue_enter_first(
1468 &vm_page_queue_zf,
1469 m, vm_page_t, pageq);
1470 } else {
1471 queue_enter_first(
1472 &vm_page_queue_inactive,
1473 m, vm_page_t, pageq);
1474 }
1475
1476 m->inactive = TRUE;
1477 if (!m->fictitious)
1478 vm_page_inactive_count++;
1479
1480 pages_moved++;
1481 }
1482 }
1483 }
1484 vm_page_unlock_queues();
1485 vm_object_paging_end(object);
1486
1487 if (object->shadow) {
1488 vm_object_t tmp_object;
1489
1490 kill_page = 0;
1491
1492 offset += object->shadow_offset;
1493
1494 tmp_object = object->shadow;
1495 vm_object_lock(tmp_object);
1496
1497 if (object != orig_object)
1498 vm_object_unlock(object);
1499 object = tmp_object;
1500 } else
1501 break;
1502 }
1503 if (object != orig_object)
1504 vm_object_unlock(object);
1505 }
1506
1507 /*
1508 * Routine: vm_object_pmap_protect
1509 *
1510 * Purpose:
1511 * Reduces the permission for all physical
1512 * pages in the specified object range.
1513 *
1514 * If removing write permission only, it is
1515 * sufficient to protect only the pages in
1516 * the top-level object; only those pages may
1517 * have write permission.
1518 *
1519 * If removing all access, we must follow the
1520 * shadow chain from the top-level object to
1521 * remove access to all pages in shadowed objects.
1522 *
1523 * The object must *not* be locked. The object must
1524 * be temporary/internal.
1525 *
1526 * If pmap is not NULL, this routine assumes that
1527 * the only mappings for the pages are in that
1528 * pmap.
1529 */
1530
1531 __private_extern__ void
1532 vm_object_pmap_protect(
1533 register vm_object_t object,
1534 register vm_object_offset_t offset,
1535 vm_object_size_t size,
1536 pmap_t pmap,
1537 vm_map_offset_t pmap_start,
1538 vm_prot_t prot)
1539 {
1540 if (object == VM_OBJECT_NULL)
1541 return;
1542 size = vm_object_round_page(size);
1543 offset = vm_object_trunc_page(offset);
1544
1545 vm_object_lock(object);
1546
1547 assert(object->internal);
1548
1549 while (TRUE) {
1550 if (ptoa_64(object->resident_page_count) > size/2 && pmap != PMAP_NULL) {
1551 vm_object_unlock(object);
1552 pmap_protect(pmap, pmap_start, pmap_start + size, prot);
1553 return;
1554 }
1555
1556 /* if we are doing large ranges with respect to resident */
1557 /* page count then we should interate over pages otherwise */
1558 /* inverse page look-up will be faster */
1559 if (ptoa_64(object->resident_page_count / 4) < size) {
1560 vm_page_t p;
1561 vm_object_offset_t end;
1562
1563 end = offset + size;
1564
1565 if (pmap != PMAP_NULL) {
1566 queue_iterate(&object->memq, p, vm_page_t, listq) {
1567 if (!p->fictitious &&
1568 (offset <= p->offset) && (p->offset < end)) {
1569 vm_map_offset_t start;
1570
1571 start = pmap_start + p->offset - offset;
1572 pmap_protect(pmap, start, start + PAGE_SIZE_64, prot);
1573 }
1574 }
1575 } else {
1576 queue_iterate(&object->memq, p, vm_page_t, listq) {
1577 if (!p->fictitious &&
1578 (offset <= p->offset) && (p->offset < end)) {
1579
1580 pmap_page_protect(p->phys_page,
1581 prot & ~p->page_lock);
1582 }
1583 }
1584 }
1585 } else {
1586 vm_page_t p;
1587 vm_object_offset_t end;
1588 vm_object_offset_t target_off;
1589
1590 end = offset + size;
1591
1592 if (pmap != PMAP_NULL) {
1593 for(target_off = offset;
1594 target_off < end;
1595 target_off += PAGE_SIZE) {
1596 p = vm_page_lookup(object, target_off);
1597 if (p != VM_PAGE_NULL) {
1598 vm_offset_t start;
1599 start = pmap_start +
1600 (vm_offset_t)(p->offset - offset);
1601 pmap_protect(pmap, start,
1602 start + PAGE_SIZE, prot);
1603 }
1604 }
1605 } else {
1606 for(target_off = offset;
1607 target_off < end; target_off += PAGE_SIZE) {
1608 p = vm_page_lookup(object, target_off);
1609 if (p != VM_PAGE_NULL) {
1610 pmap_page_protect(p->phys_page,
1611 prot & ~p->page_lock);
1612 }
1613 }
1614 }
1615 }
1616
1617 if (prot == VM_PROT_NONE) {
1618 /*
1619 * Must follow shadow chain to remove access
1620 * to pages in shadowed objects.
1621 */
1622 register vm_object_t next_object;
1623
1624 next_object = object->shadow;
1625 if (next_object != VM_OBJECT_NULL) {
1626 offset += object->shadow_offset;
1627 vm_object_lock(next_object);
1628 vm_object_unlock(object);
1629 object = next_object;
1630 }
1631 else {
1632 /*
1633 * End of chain - we are done.
1634 */
1635 break;
1636 }
1637 }
1638 else {
1639 /*
1640 * Pages in shadowed objects may never have
1641 * write permission - we may stop here.
1642 */
1643 break;
1644 }
1645 }
1646
1647 vm_object_unlock(object);
1648 }
1649
1650 /*
1651 * Routine: vm_object_copy_slowly
1652 *
1653 * Description:
1654 * Copy the specified range of the source
1655 * virtual memory object without using
1656 * protection-based optimizations (such
1657 * as copy-on-write). The pages in the
1658 * region are actually copied.
1659 *
1660 * In/out conditions:
1661 * The caller must hold a reference and a lock
1662 * for the source virtual memory object. The source
1663 * object will be returned *unlocked*.
1664 *
1665 * Results:
1666 * If the copy is completed successfully, KERN_SUCCESS is
1667 * returned. If the caller asserted the interruptible
1668 * argument, and an interruption occurred while waiting
1669 * for a user-generated event, MACH_SEND_INTERRUPTED is
1670 * returned. Other values may be returned to indicate
1671 * hard errors during the copy operation.
1672 *
1673 * A new virtual memory object is returned in a
1674 * parameter (_result_object). The contents of this
1675 * new object, starting at a zero offset, are a copy
1676 * of the source memory region. In the event of
1677 * an error, this parameter will contain the value
1678 * VM_OBJECT_NULL.
1679 */
1680 __private_extern__ kern_return_t
1681 vm_object_copy_slowly(
1682 register vm_object_t src_object,
1683 vm_object_offset_t src_offset,
1684 vm_object_size_t size,
1685 boolean_t interruptible,
1686 vm_object_t *_result_object) /* OUT */
1687 {
1688 vm_object_t new_object;
1689 vm_object_offset_t new_offset;
1690
1691 vm_object_offset_t src_lo_offset = src_offset;
1692 vm_object_offset_t src_hi_offset = src_offset + size;
1693
1694 XPR(XPR_VM_OBJECT, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
1695 src_object, src_offset, size, 0, 0);
1696
1697 if (size == 0) {
1698 vm_object_unlock(src_object);
1699 *_result_object = VM_OBJECT_NULL;
1700 return(KERN_INVALID_ARGUMENT);
1701 }
1702
1703 /*
1704 * Prevent destruction of the source object while we copy.
1705 */
1706
1707 assert(src_object->ref_count > 0);
1708 src_object->ref_count++;
1709 VM_OBJ_RES_INCR(src_object);
1710 vm_object_unlock(src_object);
1711
1712 /*
1713 * Create a new object to hold the copied pages.
1714 * A few notes:
1715 * We fill the new object starting at offset 0,
1716 * regardless of the input offset.
1717 * We don't bother to lock the new object within
1718 * this routine, since we have the only reference.
1719 */
1720
1721 new_object = vm_object_allocate(size);
1722 new_offset = 0;
1723 vm_object_lock(new_object);
1724
1725 assert(size == trunc_page_64(size)); /* Will the loop terminate? */
1726
1727 for ( ;
1728 size != 0 ;
1729 src_offset += PAGE_SIZE_64,
1730 new_offset += PAGE_SIZE_64, size -= PAGE_SIZE_64
1731 ) {
1732 vm_page_t new_page;
1733 vm_fault_return_t result;
1734
1735 while ((new_page = vm_page_alloc(new_object, new_offset))
1736 == VM_PAGE_NULL) {
1737 if (!vm_page_wait(interruptible)) {
1738 vm_object_unlock(new_object);
1739 vm_object_deallocate(new_object);
1740 vm_object_deallocate(src_object);
1741 *_result_object = VM_OBJECT_NULL;
1742 return(MACH_SEND_INTERRUPTED);
1743 }
1744 }
1745
1746 do {
1747 vm_prot_t prot = VM_PROT_READ;
1748 vm_page_t _result_page;
1749 vm_page_t top_page;
1750 register
1751 vm_page_t result_page;
1752 kern_return_t error_code;
1753
1754 vm_object_lock(src_object);
1755 vm_object_paging_begin(src_object);
1756
1757 XPR(XPR_VM_FAULT,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
1758 result = vm_fault_page(src_object, src_offset,
1759 VM_PROT_READ, FALSE, interruptible,
1760 src_lo_offset, src_hi_offset,
1761 VM_BEHAVIOR_SEQUENTIAL,
1762 &prot, &_result_page, &top_page,
1763 (int *)0,
1764 &error_code, FALSE, FALSE, NULL, 0);
1765
1766 switch(result) {
1767 case VM_FAULT_SUCCESS:
1768 result_page = _result_page;
1769
1770 /*
1771 * We don't need to hold the object
1772 * lock -- the busy page will be enough.
1773 * [We don't care about picking up any
1774 * new modifications.]
1775 *
1776 * Copy the page to the new object.
1777 *
1778 * POLICY DECISION:
1779 * If result_page is clean,
1780 * we could steal it instead
1781 * of copying.
1782 */
1783
1784 vm_object_unlock(result_page->object);
1785 vm_page_copy(result_page, new_page);
1786
1787 /*
1788 * Let go of both pages (make them
1789 * not busy, perform wakeup, activate).
1790 */
1791
1792 new_page->busy = FALSE;
1793 new_page->dirty = TRUE;
1794 vm_object_lock(result_page->object);
1795 PAGE_WAKEUP_DONE(result_page);
1796
1797 vm_page_lock_queues();
1798 if (!result_page->active &&
1799 !result_page->inactive)
1800 vm_page_activate(result_page);
1801 vm_page_activate(new_page);
1802 vm_page_unlock_queues();
1803
1804 /*
1805 * Release paging references and
1806 * top-level placeholder page, if any.
1807 */
1808
1809 vm_fault_cleanup(result_page->object,
1810 top_page);
1811
1812 break;
1813
1814 case VM_FAULT_RETRY:
1815 break;
1816
1817 case VM_FAULT_FICTITIOUS_SHORTAGE:
1818 vm_page_more_fictitious();
1819 break;
1820
1821 case VM_FAULT_MEMORY_SHORTAGE:
1822 if (vm_page_wait(interruptible))
1823 break;
1824 /* fall thru */
1825
1826 case VM_FAULT_INTERRUPTED:
1827 vm_page_free(new_page);
1828 vm_object_unlock(new_object);
1829 vm_object_deallocate(new_object);
1830 vm_object_deallocate(src_object);
1831 *_result_object = VM_OBJECT_NULL;
1832 return(MACH_SEND_INTERRUPTED);
1833
1834 case VM_FAULT_MEMORY_ERROR:
1835 /*
1836 * A policy choice:
1837 * (a) ignore pages that we can't
1838 * copy
1839 * (b) return the null object if
1840 * any page fails [chosen]
1841 */
1842
1843 vm_page_lock_queues();
1844 vm_page_free(new_page);
1845 vm_page_unlock_queues();
1846 vm_object_unlock(new_object);
1847 vm_object_deallocate(new_object);
1848 vm_object_deallocate(src_object);
1849 *_result_object = VM_OBJECT_NULL;
1850 return(error_code ? error_code:
1851 KERN_MEMORY_ERROR);
1852 }
1853 } while (result != VM_FAULT_SUCCESS);
1854 }
1855
1856 /*
1857 * Lose the extra reference, and return our object.
1858 */
1859
1860 vm_object_unlock(new_object);
1861 vm_object_deallocate(src_object);
1862 *_result_object = new_object;
1863 return(KERN_SUCCESS);
1864 }
1865
1866 /*
1867 * Routine: vm_object_copy_quickly
1868 *
1869 * Purpose:
1870 * Copy the specified range of the source virtual
1871 * memory object, if it can be done without waiting
1872 * for user-generated events.
1873 *
1874 * Results:
1875 * If the copy is successful, the copy is returned in
1876 * the arguments; otherwise, the arguments are not
1877 * affected.
1878 *
1879 * In/out conditions:
1880 * The object should be unlocked on entry and exit.
1881 */
1882
1883 /*ARGSUSED*/
1884 __private_extern__ boolean_t
1885 vm_object_copy_quickly(
1886 vm_object_t *_object, /* INOUT */
1887 __unused vm_object_offset_t offset, /* IN */
1888 __unused vm_object_size_t size, /* IN */
1889 boolean_t *_src_needs_copy, /* OUT */
1890 boolean_t *_dst_needs_copy) /* OUT */
1891 {
1892 vm_object_t object = *_object;
1893 memory_object_copy_strategy_t copy_strategy;
1894
1895 XPR(XPR_VM_OBJECT, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
1896 *_object, offset, size, 0, 0);
1897 if (object == VM_OBJECT_NULL) {
1898 *_src_needs_copy = FALSE;
1899 *_dst_needs_copy = FALSE;
1900 return(TRUE);
1901 }
1902
1903 vm_object_lock(object);
1904
1905 copy_strategy = object->copy_strategy;
1906
1907 switch (copy_strategy) {
1908 case MEMORY_OBJECT_COPY_SYMMETRIC:
1909
1910 /*
1911 * Symmetric copy strategy.
1912 * Make another reference to the object.
1913 * Leave object/offset unchanged.
1914 */
1915
1916 assert(object->ref_count > 0);
1917 object->ref_count++;
1918 vm_object_res_reference(object);
1919 object->shadowed = TRUE;
1920 vm_object_unlock(object);
1921
1922 /*
1923 * Both source and destination must make
1924 * shadows, and the source must be made
1925 * read-only if not already.
1926 */
1927
1928 *_src_needs_copy = TRUE;
1929 *_dst_needs_copy = TRUE;
1930
1931 break;
1932
1933 case MEMORY_OBJECT_COPY_DELAY:
1934 vm_object_unlock(object);
1935 return(FALSE);
1936
1937 default:
1938 vm_object_unlock(object);
1939 return(FALSE);
1940 }
1941 return(TRUE);
1942 }
1943
1944 static int copy_call_count = 0;
1945 static int copy_call_sleep_count = 0;
1946 static int copy_call_restart_count = 0;
1947
1948 /*
1949 * Routine: vm_object_copy_call [internal]
1950 *
1951 * Description:
1952 * Copy the source object (src_object), using the
1953 * user-managed copy algorithm.
1954 *
1955 * In/out conditions:
1956 * The source object must be locked on entry. It
1957 * will be *unlocked* on exit.
1958 *
1959 * Results:
1960 * If the copy is successful, KERN_SUCCESS is returned.
1961 * A new object that represents the copied virtual
1962 * memory is returned in a parameter (*_result_object).
1963 * If the return value indicates an error, this parameter
1964 * is not valid.
1965 */
1966 static kern_return_t
1967 vm_object_copy_call(
1968 vm_object_t src_object,
1969 vm_object_offset_t src_offset,
1970 vm_object_size_t size,
1971 vm_object_t *_result_object) /* OUT */
1972 {
1973 kern_return_t kr;
1974 vm_object_t copy;
1975 boolean_t check_ready = FALSE;
1976
1977 /*
1978 * If a copy is already in progress, wait and retry.
1979 *
1980 * XXX
1981 * Consider making this call interruptable, as Mike
1982 * intended it to be.
1983 *
1984 * XXXO
1985 * Need a counter or version or something to allow
1986 * us to use the copy that the currently requesting
1987 * thread is obtaining -- is it worth adding to the
1988 * vm object structure? Depends how common this case it.
1989 */
1990 copy_call_count++;
1991 while (vm_object_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL)) {
1992 vm_object_sleep(src_object, VM_OBJECT_EVENT_COPY_CALL,
1993 THREAD_UNINT);
1994 copy_call_restart_count++;
1995 }
1996
1997 /*
1998 * Indicate (for the benefit of memory_object_create_copy)
1999 * that we want a copy for src_object. (Note that we cannot
2000 * do a real assert_wait before calling memory_object_copy,
2001 * so we simply set the flag.)
2002 */
2003
2004 vm_object_set_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL);
2005 vm_object_unlock(src_object);
2006
2007 /*
2008 * Ask the memory manager to give us a memory object
2009 * which represents a copy of the src object.
2010 * The memory manager may give us a memory object
2011 * which we already have, or it may give us a
2012 * new memory object. This memory object will arrive
2013 * via memory_object_create_copy.
2014 */
2015
2016 kr = KERN_FAILURE; /* XXX need to change memory_object.defs */
2017 if (kr != KERN_SUCCESS) {
2018 return kr;
2019 }
2020
2021 /*
2022 * Wait for the copy to arrive.
2023 */
2024 vm_object_lock(src_object);
2025 while (vm_object_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL)) {
2026 vm_object_sleep(src_object, VM_OBJECT_EVENT_COPY_CALL,
2027 THREAD_UNINT);
2028 copy_call_sleep_count++;
2029 }
2030 Retry:
2031 assert(src_object->copy != VM_OBJECT_NULL);
2032 copy = src_object->copy;
2033 if (!vm_object_lock_try(copy)) {
2034 vm_object_unlock(src_object);
2035 mutex_pause(); /* wait a bit */
2036 vm_object_lock(src_object);
2037 goto Retry;
2038 }
2039 if (copy->size < src_offset+size)
2040 copy->size = src_offset+size;
2041
2042 if (!copy->pager_ready)
2043 check_ready = TRUE;
2044
2045 /*
2046 * Return the copy.
2047 */
2048 *_result_object = copy;
2049 vm_object_unlock(copy);
2050 vm_object_unlock(src_object);
2051
2052 /* Wait for the copy to be ready. */
2053 if (check_ready == TRUE) {
2054 vm_object_lock(copy);
2055 while (!copy->pager_ready) {
2056 vm_object_sleep(copy, VM_OBJECT_EVENT_PAGER_READY, THREAD_UNINT);
2057 }
2058 vm_object_unlock(copy);
2059 }
2060
2061 return KERN_SUCCESS;
2062 }
2063
2064 static int copy_delayed_lock_collisions = 0;
2065 static int copy_delayed_max_collisions = 0;
2066 static int copy_delayed_lock_contention = 0;
2067 static int copy_delayed_protect_iterate = 0;
2068
2069 /*
2070 * Routine: vm_object_copy_delayed [internal]
2071 *
2072 * Description:
2073 * Copy the specified virtual memory object, using
2074 * the asymmetric copy-on-write algorithm.
2075 *
2076 * In/out conditions:
2077 * The src_object must be locked on entry. It will be unlocked
2078 * on exit - so the caller must also hold a reference to it.
2079 *
2080 * This routine will not block waiting for user-generated
2081 * events. It is not interruptible.
2082 */
2083 __private_extern__ vm_object_t
2084 vm_object_copy_delayed(
2085 vm_object_t src_object,
2086 vm_object_offset_t src_offset,
2087 vm_object_size_t size)
2088 {
2089 vm_object_t new_copy = VM_OBJECT_NULL;
2090 vm_object_t old_copy;
2091 vm_page_t p;
2092 vm_object_size_t copy_size = src_offset + size;
2093
2094 int collisions = 0;
2095 /*
2096 * The user-level memory manager wants to see all of the changes
2097 * to this object, but it has promised not to make any changes on
2098 * its own.
2099 *
2100 * Perform an asymmetric copy-on-write, as follows:
2101 * Create a new object, called a "copy object" to hold
2102 * pages modified by the new mapping (i.e., the copy,
2103 * not the original mapping).
2104 * Record the original object as the backing object for
2105 * the copy object. If the original mapping does not
2106 * change a page, it may be used read-only by the copy.
2107 * Record the copy object in the original object.
2108 * When the original mapping causes a page to be modified,
2109 * it must be copied to a new page that is "pushed" to
2110 * the copy object.
2111 * Mark the new mapping (the copy object) copy-on-write.
2112 * This makes the copy object itself read-only, allowing
2113 * it to be reused if the original mapping makes no
2114 * changes, and simplifying the synchronization required
2115 * in the "push" operation described above.
2116 *
2117 * The copy-on-write is said to be assymetric because the original
2118 * object is *not* marked copy-on-write. A copied page is pushed
2119 * to the copy object, regardless which party attempted to modify
2120 * the page.
2121 *
2122 * Repeated asymmetric copy operations may be done. If the
2123 * original object has not been changed since the last copy, its
2124 * copy object can be reused. Otherwise, a new copy object can be
2125 * inserted between the original object and its previous copy
2126 * object. Since any copy object is read-only, this cannot affect
2127 * affect the contents of the previous copy object.
2128 *
2129 * Note that a copy object is higher in the object tree than the
2130 * original object; therefore, use of the copy object recorded in
2131 * the original object must be done carefully, to avoid deadlock.
2132 */
2133
2134 Retry:
2135
2136 /*
2137 * Wait for paging in progress.
2138 */
2139 if (!src_object->true_share)
2140 vm_object_paging_wait(src_object, THREAD_UNINT);
2141
2142 /*
2143 * See whether we can reuse the result of a previous
2144 * copy operation.
2145 */
2146
2147 old_copy = src_object->copy;
2148 if (old_copy != VM_OBJECT_NULL) {
2149 /*
2150 * Try to get the locks (out of order)
2151 */
2152 if (!vm_object_lock_try(old_copy)) {
2153 vm_object_unlock(src_object);
2154 mutex_pause();
2155
2156 /* Heisenberg Rules */
2157 copy_delayed_lock_collisions++;
2158 if (collisions++ == 0)
2159 copy_delayed_lock_contention++;
2160
2161 if (collisions > copy_delayed_max_collisions)
2162 copy_delayed_max_collisions = collisions;
2163
2164 vm_object_lock(src_object);
2165 goto Retry;
2166 }
2167
2168 /*
2169 * Determine whether the old copy object has
2170 * been modified.
2171 */
2172
2173 if (old_copy->resident_page_count == 0 &&
2174 !old_copy->pager_created) {
2175 /*
2176 * It has not been modified.
2177 *
2178 * Return another reference to
2179 * the existing copy-object if
2180 * we can safely grow it (if
2181 * needed).
2182 */
2183
2184 if (old_copy->size < copy_size) {
2185 /*
2186 * We can't perform a delayed copy if any of the
2187 * pages in the extended range are wired (because
2188 * we can't safely take write permission away from
2189 * wired pages). If the pages aren't wired, then
2190 * go ahead and protect them.
2191 */
2192 copy_delayed_protect_iterate++;
2193 queue_iterate(&src_object->memq, p, vm_page_t, listq) {
2194 if (!p->fictitious &&
2195 p->offset >= old_copy->size &&
2196 p->offset < copy_size) {
2197 if (p->wire_count > 0) {
2198 vm_object_unlock(old_copy);
2199 vm_object_unlock(src_object);
2200
2201 if (new_copy != VM_OBJECT_NULL) {
2202 vm_object_unlock(new_copy);
2203 vm_object_deallocate(new_copy);
2204 }
2205
2206 return VM_OBJECT_NULL;
2207 } else {
2208 pmap_page_protect(p->phys_page,
2209 (VM_PROT_ALL & ~VM_PROT_WRITE &
2210 ~p->page_lock));
2211 }
2212 }
2213 }
2214 old_copy->size = copy_size;
2215 }
2216
2217 vm_object_reference_locked(old_copy);
2218 vm_object_unlock(old_copy);
2219 vm_object_unlock(src_object);
2220
2221 if (new_copy != VM_OBJECT_NULL) {
2222 vm_object_unlock(new_copy);
2223 vm_object_deallocate(new_copy);
2224 }
2225
2226 return(old_copy);
2227 }
2228
2229 /*
2230 * Adjust the size argument so that the newly-created
2231 * copy object will be large enough to back either the
2232 * old copy object or the new mapping.
2233 */
2234 if (old_copy->size > copy_size)
2235 copy_size = old_copy->size;
2236
2237 if (new_copy == VM_OBJECT_NULL) {
2238 vm_object_unlock(old_copy);
2239 vm_object_unlock(src_object);
2240 new_copy = vm_object_allocate(copy_size);
2241 vm_object_lock(src_object);
2242 vm_object_lock(new_copy);
2243 goto Retry;
2244 }
2245 new_copy->size = copy_size;
2246
2247 /*
2248 * The copy-object is always made large enough to
2249 * completely shadow the original object, since
2250 * it may have several users who want to shadow
2251 * the original object at different points.
2252 */
2253
2254 assert((old_copy->shadow == src_object) &&
2255 (old_copy->shadow_offset == (vm_object_offset_t) 0));
2256
2257 } else if (new_copy == VM_OBJECT_NULL) {
2258 vm_object_unlock(src_object);
2259 new_copy = vm_object_allocate(copy_size);
2260 vm_object_lock(src_object);
2261 vm_object_lock(new_copy);
2262 goto Retry;
2263 }
2264
2265 /*
2266 * We now have the src object locked, and the new copy object
2267 * allocated and locked (and potentially the old copy locked).
2268 * Before we go any further, make sure we can still perform
2269 * a delayed copy, as the situation may have changed.
2270 *
2271 * Specifically, we can't perform a delayed copy if any of the
2272 * pages in the range are wired (because we can't safely take
2273 * write permission away from wired pages). If the pages aren't
2274 * wired, then go ahead and protect them.
2275 */
2276 copy_delayed_protect_iterate++;
2277 queue_iterate(&src_object->memq, p, vm_page_t, listq) {
2278 if (!p->fictitious && p->offset < copy_size) {
2279 if (p->wire_count > 0) {
2280 if (old_copy)
2281 vm_object_unlock(old_copy);
2282 vm_object_unlock(src_object);
2283 vm_object_unlock(new_copy);
2284 vm_object_deallocate(new_copy);
2285 return VM_OBJECT_NULL;
2286 } else {
2287 pmap_page_protect(p->phys_page,
2288 (VM_PROT_ALL & ~VM_PROT_WRITE &
2289 ~p->page_lock));
2290 }
2291 }
2292 }
2293
2294 if (old_copy != VM_OBJECT_NULL) {
2295 /*
2296 * Make the old copy-object shadow the new one.
2297 * It will receive no more pages from the original
2298 * object.
2299 */
2300
2301 src_object->ref_count--; /* remove ref. from old_copy */
2302 assert(src_object->ref_count > 0);
2303 old_copy->shadow = new_copy;
2304 assert(new_copy->ref_count > 0);
2305 new_copy->ref_count++; /* for old_copy->shadow ref. */
2306
2307 #if TASK_SWAPPER
2308 if (old_copy->res_count) {
2309 VM_OBJ_RES_INCR(new_copy);
2310 VM_OBJ_RES_DECR(src_object);
2311 }
2312 #endif
2313
2314 vm_object_unlock(old_copy); /* done with old_copy */
2315 }
2316
2317 /*
2318 * Point the new copy at the existing object.
2319 */
2320 new_copy->shadow = src_object;
2321 new_copy->shadow_offset = 0;
2322 new_copy->shadowed = TRUE; /* caller must set needs_copy */
2323 assert(src_object->ref_count > 0);
2324 src_object->ref_count++;
2325 VM_OBJ_RES_INCR(src_object);
2326 src_object->copy = new_copy;
2327 vm_object_unlock(src_object);
2328 vm_object_unlock(new_copy);
2329
2330 XPR(XPR_VM_OBJECT,
2331 "vm_object_copy_delayed: used copy object %X for source %X\n",
2332 (integer_t)new_copy, (integer_t)src_object, 0, 0, 0);
2333
2334 return(new_copy);
2335 }
2336
2337 /*
2338 * Routine: vm_object_copy_strategically
2339 *
2340 * Purpose:
2341 * Perform a copy according to the source object's
2342 * declared strategy. This operation may block,
2343 * and may be interrupted.
2344 */
2345 __private_extern__ kern_return_t
2346 vm_object_copy_strategically(
2347 register vm_object_t src_object,
2348 vm_object_offset_t src_offset,
2349 vm_object_size_t size,
2350 vm_object_t *dst_object, /* OUT */
2351 vm_object_offset_t *dst_offset, /* OUT */
2352 boolean_t *dst_needs_copy) /* OUT */
2353 {
2354 boolean_t result;
2355 boolean_t interruptible = THREAD_ABORTSAFE; /* XXX */
2356 memory_object_copy_strategy_t copy_strategy;
2357
2358 assert(src_object != VM_OBJECT_NULL);
2359
2360 vm_object_lock(src_object);
2361
2362 /*
2363 * The copy strategy is only valid if the memory manager
2364 * is "ready". Internal objects are always ready.
2365 */
2366
2367 while (!src_object->internal && !src_object->pager_ready) {
2368 wait_result_t wait_result;
2369
2370 wait_result = vm_object_sleep( src_object,
2371 VM_OBJECT_EVENT_PAGER_READY,
2372 interruptible);
2373 if (wait_result != THREAD_AWAKENED) {
2374 vm_object_unlock(src_object);
2375 *dst_object = VM_OBJECT_NULL;
2376 *dst_offset = 0;
2377 *dst_needs_copy = FALSE;
2378 return(MACH_SEND_INTERRUPTED);
2379 }
2380 }
2381
2382 copy_strategy = src_object->copy_strategy;
2383
2384 /*
2385 * Use the appropriate copy strategy.
2386 */
2387
2388 switch (copy_strategy) {
2389 case MEMORY_OBJECT_COPY_DELAY:
2390 *dst_object = vm_object_copy_delayed(src_object,
2391 src_offset, size);
2392 if (*dst_object != VM_OBJECT_NULL) {
2393 *dst_offset = src_offset;
2394 *dst_needs_copy = TRUE;
2395 result = KERN_SUCCESS;
2396 break;
2397 }
2398 vm_object_lock(src_object);
2399 /* fall thru when delayed copy not allowed */
2400
2401 case MEMORY_OBJECT_COPY_NONE:
2402 result = vm_object_copy_slowly(src_object, src_offset, size,
2403 interruptible, dst_object);
2404 if (result == KERN_SUCCESS) {
2405 *dst_offset = 0;
2406 *dst_needs_copy = FALSE;
2407 }
2408 break;
2409
2410 case MEMORY_OBJECT_COPY_CALL:
2411 result = vm_object_copy_call(src_object, src_offset, size,
2412 dst_object);
2413 if (result == KERN_SUCCESS) {
2414 *dst_offset = src_offset;
2415 *dst_needs_copy = TRUE;
2416 }
2417 break;
2418
2419 case MEMORY_OBJECT_COPY_SYMMETRIC:
2420 XPR(XPR_VM_OBJECT, "v_o_c_strategically obj 0x%x off 0x%x size 0x%x\n",(natural_t)src_object, src_offset, size, 0, 0);
2421 vm_object_unlock(src_object);
2422 result = KERN_MEMORY_RESTART_COPY;
2423 break;
2424
2425 default:
2426 panic("copy_strategically: bad strategy");
2427 result = KERN_INVALID_ARGUMENT;
2428 }
2429 return(result);
2430 }
2431
2432 /*
2433 * vm_object_shadow:
2434 *
2435 * Create a new object which is backed by the
2436 * specified existing object range. The source
2437 * object reference is deallocated.
2438 *
2439 * The new object and offset into that object
2440 * are returned in the source parameters.
2441 */
2442 boolean_t vm_object_shadow_check = FALSE;
2443
2444 __private_extern__ boolean_t
2445 vm_object_shadow(
2446 vm_object_t *object, /* IN/OUT */
2447 vm_object_offset_t *offset, /* IN/OUT */
2448 vm_object_size_t length)
2449 {
2450 register vm_object_t source;
2451 register vm_object_t result;
2452
2453 source = *object;
2454 assert(source->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC);
2455
2456 /*
2457 * Determine if we really need a shadow.
2458 */
2459
2460 if (vm_object_shadow_check && source->ref_count == 1 &&
2461 (source->shadow == VM_OBJECT_NULL ||
2462 source->shadow->copy == VM_OBJECT_NULL))
2463 {
2464 source->shadowed = FALSE;
2465 return FALSE;
2466 }
2467
2468 /*
2469 * Allocate a new object with the given length
2470 */
2471
2472 if ((result = vm_object_allocate(length)) == VM_OBJECT_NULL)
2473 panic("vm_object_shadow: no object for shadowing");
2474
2475 /*
2476 * The new object shadows the source object, adding
2477 * a reference to it. Our caller changes his reference
2478 * to point to the new object, removing a reference to
2479 * the source object. Net result: no change of reference
2480 * count.
2481 */
2482 result->shadow = source;
2483
2484 /*
2485 * Store the offset into the source object,
2486 * and fix up the offset into the new object.
2487 */
2488
2489 result->shadow_offset = *offset;
2490
2491 /*
2492 * Return the new things
2493 */
2494
2495 *offset = 0;
2496 *object = result;
2497 return TRUE;
2498 }
2499
2500 /*
2501 * The relationship between vm_object structures and
2502 * the memory_object requires careful synchronization.
2503 *
2504 * All associations are created by memory_object_create_named
2505 * for external pagers and vm_object_pager_create for internal
2506 * objects as follows:
2507 *
2508 * pager: the memory_object itself, supplied by
2509 * the user requesting a mapping (or the kernel,
2510 * when initializing internal objects); the
2511 * kernel simulates holding send rights by keeping
2512 * a port reference;
2513 *
2514 * pager_request:
2515 * the memory object control port,
2516 * created by the kernel; the kernel holds
2517 * receive (and ownership) rights to this
2518 * port, but no other references.
2519 *
2520 * When initialization is complete, the "initialized" field
2521 * is asserted. Other mappings using a particular memory object,
2522 * and any references to the vm_object gained through the
2523 * port association must wait for this initialization to occur.
2524 *
2525 * In order to allow the memory manager to set attributes before
2526 * requests (notably virtual copy operations, but also data or
2527 * unlock requests) are made, a "ready" attribute is made available.
2528 * Only the memory manager may affect the value of this attribute.
2529 * Its value does not affect critical kernel functions, such as
2530 * internal object initialization or destruction. [Furthermore,
2531 * memory objects created by the kernel are assumed to be ready
2532 * immediately; the default memory manager need not explicitly
2533 * set the "ready" attribute.]
2534 *
2535 * [Both the "initialized" and "ready" attribute wait conditions
2536 * use the "pager" field as the wait event.]
2537 *
2538 * The port associations can be broken down by any of the
2539 * following routines:
2540 * vm_object_terminate:
2541 * No references to the vm_object remain, and
2542 * the object cannot (or will not) be cached.
2543 * This is the normal case, and is done even
2544 * though one of the other cases has already been
2545 * done.
2546 * memory_object_destroy:
2547 * The memory manager has requested that the
2548 * kernel relinquish references to the memory
2549 * object. [The memory manager may not want to
2550 * destroy the memory object, but may wish to
2551 * refuse or tear down existing memory mappings.]
2552 *
2553 * Each routine that breaks an association must break all of
2554 * them at once. At some later time, that routine must clear
2555 * the pager field and release the memory object references.
2556 * [Furthermore, each routine must cope with the simultaneous
2557 * or previous operations of the others.]
2558 *
2559 * In addition to the lock on the object, the vm_object_cache_lock
2560 * governs the associations. References gained through the
2561 * association require use of the cache lock.
2562 *
2563 * Because the pager field may be cleared spontaneously, it
2564 * cannot be used to determine whether a memory object has
2565 * ever been associated with a particular vm_object. [This
2566 * knowledge is important to the shadow object mechanism.]
2567 * For this reason, an additional "created" attribute is
2568 * provided.
2569 *
2570 * During various paging operations, the pager reference found in the
2571 * vm_object must be valid. To prevent this from being released,
2572 * (other than being removed, i.e., made null), routines may use
2573 * the vm_object_paging_begin/end routines [actually, macros].
2574 * The implementation uses the "paging_in_progress" and "wanted" fields.
2575 * [Operations that alter the validity of the pager values include the
2576 * termination routines and vm_object_collapse.]
2577 */
2578
2579 #if 0
2580 static void vm_object_abort_activity(
2581 vm_object_t object);
2582
2583 /*
2584 * Routine: vm_object_abort_activity [internal use only]
2585 * Purpose:
2586 * Abort paging requests pending on this object.
2587 * In/out conditions:
2588 * The object is locked on entry and exit.
2589 */
2590 static void
2591 vm_object_abort_activity(
2592 vm_object_t object)
2593 {
2594 register
2595 vm_page_t p;
2596 vm_page_t next;
2597
2598 XPR(XPR_VM_OBJECT, "vm_object_abort_activity, object 0x%X\n",
2599 (integer_t)object, 0, 0, 0, 0);
2600
2601 /*
2602 * Abort all activity that would be waiting
2603 * for a result on this memory object.
2604 *
2605 * We could also choose to destroy all pages
2606 * that we have in memory for this object, but
2607 * we don't.
2608 */
2609
2610 p = (vm_page_t) queue_first(&object->memq);
2611 while (!queue_end(&object->memq, (queue_entry_t) p)) {
2612 next = (vm_page_t) queue_next(&p->listq);
2613
2614 /*
2615 * If it's being paged in, destroy it.
2616 * If an unlock has been requested, start it again.
2617 */
2618
2619 if (p->busy && p->absent) {
2620 VM_PAGE_FREE(p);
2621 }
2622 else {
2623 if (p->unlock_request != VM_PROT_NONE)
2624 p->unlock_request = VM_PROT_NONE;
2625 PAGE_WAKEUP(p);
2626 }
2627
2628 p = next;
2629 }
2630
2631 /*
2632 * Wake up threads waiting for the memory object to
2633 * become ready.
2634 */
2635
2636 object->pager_ready = TRUE;
2637 vm_object_wakeup(object, VM_OBJECT_EVENT_PAGER_READY);
2638 }
2639
2640 /*
2641 * Routine: vm_object_pager_dead
2642 *
2643 * Purpose:
2644 * A port is being destroy, and the IPC kobject code
2645 * can't tell if it represents a pager port or not.
2646 * So this function is called each time it sees a port
2647 * die.
2648 * THIS IS HORRIBLY INEFFICIENT. We should only call
2649 * this routine if we had requested a notification on
2650 * the port.
2651 */
2652
2653 __private_extern__ void
2654 vm_object_pager_dead(
2655 ipc_port_t pager)
2656 {
2657 vm_object_t object;
2658 vm_object_hash_entry_t entry;
2659
2660 /*
2661 * Perform essentially the same operations as in vm_object_lookup,
2662 * except that this time we look up based on the memory_object
2663 * port, not the control port.
2664 */
2665 vm_object_cache_lock();
2666 entry = vm_object_hash_lookup(pager, FALSE);
2667 if (entry == VM_OBJECT_HASH_ENTRY_NULL ||
2668 entry->object == VM_OBJECT_NULL) {
2669 vm_object_cache_unlock();
2670 return;
2671 }
2672
2673 object = entry->object;
2674 entry->object = VM_OBJECT_NULL;
2675
2676 vm_object_lock(object);
2677 if (object->ref_count == 0) {
2678 XPR(XPR_VM_OBJECT_CACHE,
2679 "vm_object_destroy: removing %x from cache, head (%x, %x)\n",
2680 (integer_t)object,
2681 (integer_t)vm_object_cached_list.next,
2682 (integer_t)vm_object_cached_list.prev, 0,0);
2683
2684 queue_remove(&vm_object_cached_list, object,
2685 vm_object_t, cached_list);
2686 vm_object_cached_count--;
2687 }
2688 object->ref_count++;
2689 vm_object_res_reference(object);
2690
2691 object->can_persist = FALSE;
2692
2693 assert(object->pager == pager);
2694
2695 /*
2696 * Remove the pager association.
2697 *
2698 * Note that the memory_object itself is dead, so
2699 * we don't bother with it.
2700 */
2701
2702 object->pager = MEMORY_OBJECT_NULL;
2703
2704 vm_object_unlock(object);
2705 vm_object_cache_unlock();
2706
2707 vm_object_pager_wakeup(pager);
2708
2709 /*
2710 * Release the pager reference. Note that there's no
2711 * point in trying the memory_object_terminate call
2712 * because the memory_object itself is dead. Also
2713 * release the memory_object_control reference, since
2714 * the pager didn't do that either.
2715 */
2716
2717 memory_object_deallocate(pager);
2718 memory_object_control_deallocate(object->pager_request);
2719
2720
2721 /*
2722 * Restart pending page requests
2723 */
2724 vm_object_lock(object);
2725 vm_object_abort_activity(object);
2726 vm_object_unlock(object);
2727
2728 /*
2729 * Lose the object reference.
2730 */
2731
2732 vm_object_deallocate(object);
2733 }
2734 #endif
2735
2736 /*
2737 * Routine: vm_object_enter
2738 * Purpose:
2739 * Find a VM object corresponding to the given
2740 * pager; if no such object exists, create one,
2741 * and initialize the pager.
2742 */
2743 vm_object_t
2744 vm_object_enter(
2745 memory_object_t pager,
2746 vm_object_size_t size,
2747 boolean_t internal,
2748 boolean_t init,
2749 boolean_t named)
2750 {
2751 register vm_object_t object;
2752 vm_object_t new_object;
2753 boolean_t must_init;
2754 vm_object_hash_entry_t entry, new_entry;
2755
2756 if (pager == MEMORY_OBJECT_NULL)
2757 return(vm_object_allocate(size));
2758
2759 new_object = VM_OBJECT_NULL;
2760 new_entry = VM_OBJECT_HASH_ENTRY_NULL;
2761 must_init = init;
2762
2763 /*
2764 * Look for an object associated with this port.
2765 */
2766
2767 vm_object_cache_lock();
2768 do {
2769 entry = vm_object_hash_lookup(pager, FALSE);
2770
2771 if (entry == VM_OBJECT_HASH_ENTRY_NULL) {
2772 if (new_object == VM_OBJECT_NULL) {
2773 /*
2774 * We must unlock to create a new object;
2775 * if we do so, we must try the lookup again.
2776 */
2777 vm_object_cache_unlock();
2778 assert(new_entry == VM_OBJECT_HASH_ENTRY_NULL);
2779 new_entry = vm_object_hash_entry_alloc(pager);
2780 new_object = vm_object_allocate(size);
2781 vm_object_cache_lock();
2782 } else {
2783 /*
2784 * Lookup failed twice, and we have something
2785 * to insert; set the object.
2786 */
2787 vm_object_hash_insert(new_entry);
2788 entry = new_entry;
2789 entry->object = new_object;
2790 new_entry = VM_OBJECT_HASH_ENTRY_NULL;
2791 new_object = VM_OBJECT_NULL;
2792 must_init = TRUE;
2793 }
2794 } else if (entry->object == VM_OBJECT_NULL) {
2795 /*
2796 * If a previous object is being terminated,
2797 * we must wait for the termination message
2798 * to be queued (and lookup the entry again).
2799 */
2800 entry->waiting = TRUE;
2801 entry = VM_OBJECT_HASH_ENTRY_NULL;
2802 assert_wait((event_t) pager, THREAD_UNINT);
2803 vm_object_cache_unlock();
2804 thread_block(THREAD_CONTINUE_NULL);
2805 vm_object_cache_lock();
2806 }
2807 } while (entry == VM_OBJECT_HASH_ENTRY_NULL);
2808
2809 object = entry->object;
2810 assert(object != VM_OBJECT_NULL);
2811
2812 if (!must_init) {
2813 vm_object_lock(object);
2814 assert(!internal || object->internal);
2815 if (named) {
2816 assert(!object->named);
2817 object->named = TRUE;
2818 }
2819 if (object->ref_count == 0) {
2820 XPR(XPR_VM_OBJECT_CACHE,
2821 "vm_object_enter: removing %x from cache, head (%x, %x)\n",
2822 (integer_t)object,
2823 (integer_t)vm_object_cached_list.next,
2824 (integer_t)vm_object_cached_list.prev, 0,0);
2825 queue_remove(&vm_object_cached_list, object,
2826 vm_object_t, cached_list);
2827 vm_object_cached_count--;
2828 }
2829 object->ref_count++;
2830 vm_object_res_reference(object);
2831 vm_object_unlock(object);
2832
2833 VM_STAT(hits++);
2834 }
2835 assert(object->ref_count > 0);
2836
2837 VM_STAT(lookups++);
2838
2839 vm_object_cache_unlock();
2840
2841 XPR(XPR_VM_OBJECT,
2842 "vm_o_enter: pager 0x%x obj 0x%x must_init %d\n",
2843 (integer_t)pager, (integer_t)object, must_init, 0, 0);
2844
2845 /*
2846 * If we raced to create a vm_object but lost, let's
2847 * throw away ours.
2848 */
2849
2850 if (new_object != VM_OBJECT_NULL)
2851 vm_object_deallocate(new_object);
2852
2853 if (new_entry != VM_OBJECT_HASH_ENTRY_NULL)
2854 vm_object_hash_entry_free(new_entry);
2855
2856 if (must_init) {
2857 memory_object_control_t control;
2858
2859 /*
2860 * Allocate request port.
2861 */
2862
2863 control = memory_object_control_allocate(object);
2864 assert (control != MEMORY_OBJECT_CONTROL_NULL);
2865
2866 vm_object_lock(object);
2867 assert(object != kernel_object);
2868
2869 /*
2870 * Copy the reference we were given.
2871 */
2872
2873 memory_object_reference(pager);
2874 object->pager_created = TRUE;
2875 object->pager = pager;
2876 object->internal = internal;
2877 object->pager_trusted = internal;
2878 if (!internal) {
2879 /* copy strategy invalid until set by memory manager */
2880 object->copy_strategy = MEMORY_OBJECT_COPY_INVALID;
2881 }
2882 object->pager_control = control;
2883 object->pager_ready = FALSE;
2884
2885 vm_object_unlock(object);
2886
2887 /*
2888 * Let the pager know we're using it.
2889 */
2890
2891 (void) memory_object_init(pager,
2892 object->pager_control,
2893 PAGE_SIZE);
2894
2895 vm_object_lock(object);
2896 if (named)
2897 object->named = TRUE;
2898 if (internal) {
2899 object->pager_ready = TRUE;
2900 vm_object_wakeup(object, VM_OBJECT_EVENT_PAGER_READY);
2901 }
2902
2903 object->pager_initialized = TRUE;
2904 vm_object_wakeup(object, VM_OBJECT_EVENT_INITIALIZED);
2905 } else {
2906 vm_object_lock(object);
2907 }
2908
2909 /*
2910 * [At this point, the object must be locked]
2911 */
2912
2913 /*
2914 * Wait for the work above to be done by the first
2915 * thread to map this object.
2916 */
2917
2918 while (!object->pager_initialized) {
2919 vm_object_sleep(object,
2920 VM_OBJECT_EVENT_INITIALIZED,
2921 THREAD_UNINT);
2922 }
2923 vm_object_unlock(object);
2924
2925 XPR(XPR_VM_OBJECT,
2926 "vm_object_enter: vm_object %x, memory_object %x, internal %d\n",
2927 (integer_t)object, (integer_t)object->pager, internal, 0,0);
2928 return(object);
2929 }
2930
2931 /*
2932 * Routine: vm_object_pager_create
2933 * Purpose:
2934 * Create a memory object for an internal object.
2935 * In/out conditions:
2936 * The object is locked on entry and exit;
2937 * it may be unlocked within this call.
2938 * Limitations:
2939 * Only one thread may be performing a
2940 * vm_object_pager_create on an object at
2941 * a time. Presumably, only the pageout
2942 * daemon will be using this routine.
2943 */
2944
2945 void
2946 vm_object_pager_create(
2947 register vm_object_t object)
2948 {
2949 memory_object_t pager;
2950 vm_object_hash_entry_t entry;
2951 #if MACH_PAGEMAP
2952 vm_object_size_t size;
2953 vm_external_map_t map;
2954 #endif /* MACH_PAGEMAP */
2955
2956 XPR(XPR_VM_OBJECT, "vm_object_pager_create, object 0x%X\n",
2957 (integer_t)object, 0,0,0,0);
2958
2959 assert(object != kernel_object);
2960
2961 if (memory_manager_default_check() != KERN_SUCCESS)
2962 return;
2963
2964 /*
2965 * Prevent collapse or termination by holding a paging reference
2966 */
2967
2968 vm_object_paging_begin(object);
2969 if (object->pager_created) {
2970 /*
2971 * Someone else got to it first...
2972 * wait for them to finish initializing the ports
2973 */
2974 while (!object->pager_initialized) {
2975 vm_object_sleep(object,
2976 VM_OBJECT_EVENT_INITIALIZED,
2977 THREAD_UNINT);
2978 }
2979 vm_object_paging_end(object);
2980 return;
2981 }
2982
2983 /*
2984 * Indicate that a memory object has been assigned
2985 * before dropping the lock, to prevent a race.
2986 */
2987
2988 object->pager_created = TRUE;
2989 object->paging_offset = 0;
2990
2991 #if MACH_PAGEMAP
2992 size = object->size;
2993 #endif /* MACH_PAGEMAP */
2994 vm_object_unlock(object);
2995
2996 #if MACH_PAGEMAP
2997 map = vm_external_create(size);
2998 vm_object_lock(object);
2999 assert(object->size == size);
3000 object->existence_map = map;
3001 vm_object_unlock(object);
3002 #endif /* MACH_PAGEMAP */
3003
3004 /*
3005 * Create the [internal] pager, and associate it with this object.
3006 *
3007 * We make the association here so that vm_object_enter()
3008 * can look up the object to complete initializing it. No
3009 * user will ever map this object.
3010 */
3011 {
3012 memory_object_default_t dmm;
3013 vm_size_t cluster_size;
3014
3015 /* acquire a reference for the default memory manager */
3016 dmm = memory_manager_default_reference(&cluster_size);
3017 assert(cluster_size >= PAGE_SIZE);
3018
3019 object->cluster_size = cluster_size; /* XXX ??? */
3020 assert(object->temporary);
3021
3022 /* create our new memory object */
3023 (void) memory_object_create(dmm, object->size, &pager);
3024
3025 memory_object_default_deallocate(dmm);
3026 }
3027
3028 entry = vm_object_hash_entry_alloc(pager);
3029
3030 vm_object_cache_lock();
3031 vm_object_hash_insert(entry);
3032
3033 entry->object = object;
3034 vm_object_cache_unlock();
3035
3036 /*
3037 * A reference was returned by
3038 * memory_object_create(), and it is
3039 * copied by vm_object_enter().
3040 */
3041
3042 if (vm_object_enter(pager, object->size, TRUE, TRUE, FALSE) != object)
3043 panic("vm_object_pager_create: mismatch");
3044
3045 /*
3046 * Drop the reference we were passed.
3047 */
3048 memory_object_deallocate(pager);
3049
3050 vm_object_lock(object);
3051
3052 /*
3053 * Release the paging reference
3054 */
3055 vm_object_paging_end(object);
3056 }
3057
3058 /*
3059 * Routine: vm_object_remove
3060 * Purpose:
3061 * Eliminate the pager/object association
3062 * for this pager.
3063 * Conditions:
3064 * The object cache must be locked.
3065 */
3066 __private_extern__ void
3067 vm_object_remove(
3068 vm_object_t object)
3069 {
3070 memory_object_t pager;
3071
3072 if ((pager = object->pager) != MEMORY_OBJECT_NULL) {
3073 vm_object_hash_entry_t entry;
3074
3075 entry = vm_object_hash_lookup(pager, FALSE);
3076 if (entry != VM_OBJECT_HASH_ENTRY_NULL)
3077 entry->object = VM_OBJECT_NULL;
3078 }
3079
3080 }
3081
3082 /*
3083 * Global variables for vm_object_collapse():
3084 *
3085 * Counts for normal collapses and bypasses.
3086 * Debugging variables, to watch or disable collapse.
3087 */
3088 static long object_collapses = 0;
3089 static long object_bypasses = 0;
3090
3091 static boolean_t vm_object_collapse_allowed = TRUE;
3092 static boolean_t vm_object_bypass_allowed = TRUE;
3093
3094 static int vm_external_discarded;
3095 static int vm_external_collapsed;
3096
3097 unsigned long vm_object_collapse_encrypted = 0;
3098
3099 /*
3100 * Routine: vm_object_do_collapse
3101 * Purpose:
3102 * Collapse an object with the object backing it.
3103 * Pages in the backing object are moved into the
3104 * parent, and the backing object is deallocated.
3105 * Conditions:
3106 * Both objects and the cache are locked; the page
3107 * queues are unlocked.
3108 *
3109 */
3110 static void
3111 vm_object_do_collapse(
3112 vm_object_t object,
3113 vm_object_t backing_object)
3114 {
3115 vm_page_t p, pp;
3116 vm_object_offset_t new_offset, backing_offset;
3117 vm_object_size_t size;
3118
3119 backing_offset = object->shadow_offset;
3120 size = object->size;
3121
3122 /*
3123 * Move all in-memory pages from backing_object
3124 * to the parent. Pages that have been paged out
3125 * will be overwritten by any of the parent's
3126 * pages that shadow them.
3127 */
3128
3129 while (!queue_empty(&backing_object->memq)) {
3130
3131 p = (vm_page_t) queue_first(&backing_object->memq);
3132
3133 new_offset = (p->offset - backing_offset);
3134
3135 assert(!p->busy || p->absent);
3136
3137 /*
3138 * If the parent has a page here, or if
3139 * this page falls outside the parent,
3140 * dispose of it.
3141 *
3142 * Otherwise, move it as planned.
3143 */
3144
3145 if (p->offset < backing_offset || new_offset >= size) {
3146 VM_PAGE_FREE(p);
3147 } else {
3148 /*
3149 * ENCRYPTED SWAP:
3150 * The encryption key includes the "pager" and the
3151 * "paging_offset". These might not be the same in
3152 * the new object, so we can't just move an encrypted
3153 * page from one object to the other. We can't just
3154 * decrypt the page here either, because that would drop
3155 * the object lock.
3156 * The caller should check for encrypted pages before
3157 * attempting to collapse.
3158 */
3159 ASSERT_PAGE_DECRYPTED(p);
3160
3161 pp = vm_page_lookup(object, new_offset);
3162 if (pp == VM_PAGE_NULL) {
3163
3164 /*
3165 * Parent now has no page.
3166 * Move the backing object's page up.
3167 */
3168
3169 vm_page_rename(p, object, new_offset);
3170 #if MACH_PAGEMAP
3171 } else if (pp->absent) {
3172
3173 /*
3174 * Parent has an absent page...
3175 * it's not being paged in, so
3176 * it must really be missing from
3177 * the parent.
3178 *
3179 * Throw out the absent page...
3180 * any faults looking for that
3181 * page will restart with the new
3182 * one.
3183 */
3184
3185 VM_PAGE_FREE(pp);
3186 vm_page_rename(p, object, new_offset);
3187 #endif /* MACH_PAGEMAP */
3188 } else {
3189 assert(! pp->absent);
3190
3191 /*
3192 * Parent object has a real page.
3193 * Throw away the backing object's
3194 * page.
3195 */
3196 VM_PAGE_FREE(p);
3197 }
3198 }
3199 }
3200
3201 #if !MACH_PAGEMAP
3202 assert(!object->pager_created && object->pager == MEMORY_OBJECT_NULL
3203 || (!backing_object->pager_created
3204 && backing_object->pager == MEMORY_OBJECT_NULL));
3205 #else
3206 assert(!object->pager_created && object->pager == MEMORY_OBJECT_NULL);
3207 #endif /* !MACH_PAGEMAP */
3208
3209 if (backing_object->pager != MEMORY_OBJECT_NULL) {
3210 vm_object_hash_entry_t entry;
3211
3212 /*
3213 * Move the pager from backing_object to object.
3214 *
3215 * XXX We're only using part of the paging space
3216 * for keeps now... we ought to discard the
3217 * unused portion.
3218 */
3219
3220 assert(!object->paging_in_progress);
3221 object->pager = backing_object->pager;
3222 entry = vm_object_hash_lookup(object->pager, FALSE);
3223 assert(entry != VM_OBJECT_HASH_ENTRY_NULL);
3224 entry->object = object;
3225 object->pager_created = backing_object->pager_created;
3226 object->pager_control = backing_object->pager_control;
3227 object->pager_ready = backing_object->pager_ready;
3228 object->pager_initialized = backing_object->pager_initialized;
3229 object->cluster_size = backing_object->cluster_size;
3230 object->paging_offset =
3231 backing_object->paging_offset + backing_offset;
3232 if (object->pager_control != MEMORY_OBJECT_CONTROL_NULL) {
3233 memory_object_control_collapse(object->pager_control,
3234 object);
3235 }
3236 }
3237
3238 vm_object_cache_unlock();
3239
3240 #if MACH_PAGEMAP
3241 /*
3242 * If the shadow offset is 0, the use the existence map from
3243 * the backing object if there is one. If the shadow offset is
3244 * not zero, toss it.
3245 *
3246 * XXX - If the shadow offset is not 0 then a bit copy is needed
3247 * if the map is to be salvaged. For now, we just just toss the
3248 * old map, giving the collapsed object no map. This means that
3249 * the pager is invoked for zero fill pages. If analysis shows
3250 * that this happens frequently and is a performance hit, then
3251 * this code should be fixed to salvage the map.
3252 */
3253 assert(object->existence_map == VM_EXTERNAL_NULL);
3254 if (backing_offset || (size != backing_object->size)) {
3255 vm_external_discarded++;
3256 vm_external_destroy(backing_object->existence_map,
3257 backing_object->size);
3258 }
3259 else {
3260 vm_external_collapsed++;
3261 object->existence_map = backing_object->existence_map;
3262 }
3263 backing_object->existence_map = VM_EXTERNAL_NULL;
3264 #endif /* MACH_PAGEMAP */
3265
3266 /*
3267 * Object now shadows whatever backing_object did.
3268 * Note that the reference to backing_object->shadow
3269 * moves from within backing_object to within object.
3270 */
3271
3272 assert(!object->phys_contiguous);
3273 assert(!backing_object->phys_contiguous);
3274 object->shadow = backing_object->shadow;
3275 if (object->shadow) {
3276 object->shadow_offset += backing_object->shadow_offset;
3277 } else {
3278 /* no shadow, therefore no shadow offset... */
3279 object->shadow_offset = 0;
3280 }
3281 assert((object->shadow == VM_OBJECT_NULL) ||
3282 (object->shadow->copy != backing_object));
3283
3284 /*
3285 * Discard backing_object.
3286 *
3287 * Since the backing object has no pages, no
3288 * pager left, and no object references within it,
3289 * all that is necessary is to dispose of it.
3290 */
3291
3292 assert((backing_object->ref_count == 1) &&
3293 (backing_object->resident_page_count == 0) &&
3294 (backing_object->paging_in_progress == 0));
3295
3296 backing_object->alive = FALSE;
3297 vm_object_unlock(backing_object);
3298
3299 XPR(XPR_VM_OBJECT, "vm_object_collapse, collapsed 0x%X\n",
3300 (integer_t)backing_object, 0,0,0,0);
3301
3302 zfree(vm_object_zone, backing_object);
3303
3304 object_collapses++;
3305 }
3306
3307 static void
3308 vm_object_do_bypass(
3309 vm_object_t object,
3310 vm_object_t backing_object)
3311 {
3312 /*
3313 * Make the parent shadow the next object
3314 * in the chain.
3315 */
3316
3317 #if TASK_SWAPPER
3318 /*
3319 * Do object reference in-line to
3320 * conditionally increment shadow's
3321 * residence count. If object is not
3322 * resident, leave residence count
3323 * on shadow alone.
3324 */
3325 if (backing_object->shadow != VM_OBJECT_NULL) {
3326 vm_object_lock(backing_object->shadow);
3327 backing_object->shadow->ref_count++;
3328 if (object->res_count != 0)
3329 vm_object_res_reference(backing_object->shadow);
3330 vm_object_unlock(backing_object->shadow);
3331 }
3332 #else /* TASK_SWAPPER */
3333 vm_object_reference(backing_object->shadow);
3334 #endif /* TASK_SWAPPER */
3335
3336 assert(!object->phys_contiguous);
3337 assert(!backing_object->phys_contiguous);
3338 object->shadow = backing_object->shadow;
3339 if (object->shadow) {
3340 object->shadow_offset += backing_object->shadow_offset;
3341 } else {
3342 /* no shadow, therefore no shadow offset... */
3343 object->shadow_offset = 0;
3344 }
3345
3346 /*
3347 * Backing object might have had a copy pointer
3348 * to us. If it did, clear it.
3349 */
3350 if (backing_object->copy == object) {
3351 backing_object->copy = VM_OBJECT_NULL;
3352 }
3353
3354 /*
3355 * Drop the reference count on backing_object.
3356 #if TASK_SWAPPER
3357 * Since its ref_count was at least 2, it
3358 * will not vanish; so we don't need to call
3359 * vm_object_deallocate.
3360 * [FBDP: that doesn't seem to be true any more]
3361 *
3362 * The res_count on the backing object is
3363 * conditionally decremented. It's possible
3364 * (via vm_pageout_scan) to get here with
3365 * a "swapped" object, which has a 0 res_count,
3366 * in which case, the backing object res_count
3367 * is already down by one.
3368 #else
3369 * Don't call vm_object_deallocate unless
3370 * ref_count drops to zero.
3371 *
3372 * The ref_count can drop to zero here if the
3373 * backing object could be bypassed but not
3374 * collapsed, such as when the backing object
3375 * is temporary and cachable.
3376 #endif
3377 */
3378 if (backing_object->ref_count > 1) {
3379 backing_object->ref_count--;
3380 #if TASK_SWAPPER
3381 if (object->res_count != 0)
3382 vm_object_res_deallocate(backing_object);
3383 assert(backing_object->ref_count > 0);
3384 #endif /* TASK_SWAPPER */
3385 vm_object_unlock(backing_object);
3386 } else {
3387
3388 /*
3389 * Drop locks so that we can deallocate
3390 * the backing object.
3391 */
3392
3393 #if TASK_SWAPPER
3394 if (object->res_count == 0) {
3395 /* XXX get a reference for the deallocate below */
3396 vm_object_res_reference(backing_object);
3397 }
3398 #endif /* TASK_SWAPPER */
3399 vm_object_unlock(object);
3400 vm_object_unlock(backing_object);
3401 vm_object_deallocate(backing_object);
3402
3403 /*
3404 * Relock object. We don't have to reverify
3405 * its state since vm_object_collapse will
3406 * do that for us as it starts at the
3407 * top of its loop.
3408 */
3409
3410 vm_object_lock(object);
3411 }
3412
3413 object_bypasses++;
3414 }
3415
3416
3417 /*
3418 * vm_object_collapse:
3419 *
3420 * Perform an object collapse or an object bypass if appropriate.
3421 * The real work of collapsing and bypassing is performed in
3422 * the routines vm_object_do_collapse and vm_object_do_bypass.
3423 *
3424 * Requires that the object be locked and the page queues be unlocked.
3425 *
3426 */
3427 static unsigned long vm_object_collapse_calls = 0;
3428 static unsigned long vm_object_collapse_objects = 0;
3429 static unsigned long vm_object_collapse_do_collapse = 0;
3430 static unsigned long vm_object_collapse_do_bypass = 0;
3431 __private_extern__ void
3432 vm_object_collapse(
3433 register vm_object_t object,
3434 register vm_object_offset_t hint_offset,
3435 boolean_t can_bypass)
3436 {
3437 register vm_object_t backing_object;
3438 register unsigned int rcount;
3439 register unsigned int size;
3440 vm_object_offset_t collapse_min_offset;
3441 vm_object_offset_t collapse_max_offset;
3442 vm_page_t page;
3443 vm_object_t original_object;
3444
3445 vm_object_collapse_calls++;
3446
3447 if (! vm_object_collapse_allowed &&
3448 ! (can_bypass && vm_object_bypass_allowed)) {
3449 return;
3450 }
3451
3452 XPR(XPR_VM_OBJECT, "vm_object_collapse, obj 0x%X\n",
3453 (integer_t)object, 0,0,0,0);
3454
3455 if (object == VM_OBJECT_NULL)
3456 return;
3457
3458 original_object = object;
3459
3460 while (TRUE) {
3461 vm_object_collapse_objects++;
3462 /*
3463 * Verify that the conditions are right for either
3464 * collapse or bypass:
3465 */
3466
3467 /*
3468 * There is a backing object, and
3469 */
3470
3471 backing_object = object->shadow;
3472 if (backing_object == VM_OBJECT_NULL) {
3473 if (object != original_object) {
3474 vm_object_unlock(object);
3475 }
3476 return;
3477 }
3478
3479 /*
3480 * No pages in the object are currently
3481 * being paged out, and
3482 */
3483 if (object->paging_in_progress != 0 ||
3484 object->absent_count != 0) {
3485 /* try and collapse the rest of the shadow chain */
3486 vm_object_lock(backing_object);
3487 if (object != original_object) {
3488 vm_object_unlock(object);
3489 }
3490 object = backing_object;
3491 continue;
3492 }
3493
3494 vm_object_lock(backing_object);
3495
3496 /*
3497 * ...
3498 * The backing object is not read_only,
3499 * and no pages in the backing object are
3500 * currently being paged out.
3501 * The backing object is internal.
3502 *
3503 */
3504
3505 if (!backing_object->internal ||
3506 backing_object->paging_in_progress != 0) {
3507 /* try and collapse the rest of the shadow chain */
3508 if (object != original_object) {
3509 vm_object_unlock(object);
3510 }
3511 object = backing_object;
3512 continue;
3513 }
3514
3515 /*
3516 * The backing object can't be a copy-object:
3517 * the shadow_offset for the copy-object must stay
3518 * as 0. Furthermore (for the 'we have all the
3519 * pages' case), if we bypass backing_object and
3520 * just shadow the next object in the chain, old
3521 * pages from that object would then have to be copied
3522 * BOTH into the (former) backing_object and into the
3523 * parent object.
3524 */
3525 if (backing_object->shadow != VM_OBJECT_NULL &&
3526 backing_object->shadow->copy == backing_object) {
3527 /* try and collapse the rest of the shadow chain */
3528 if (object != original_object) {
3529 vm_object_unlock(object);
3530 }
3531 object = backing_object;
3532 continue;
3533 }
3534
3535 /*
3536 * We can now try to either collapse the backing
3537 * object (if the parent is the only reference to
3538 * it) or (perhaps) remove the parent's reference
3539 * to it.
3540 *
3541 * If there is exactly one reference to the backing
3542 * object, we may be able to collapse it into the
3543 * parent.
3544 *
3545 * If MACH_PAGEMAP is defined:
3546 * The parent must not have a pager created for it,
3547 * since collapsing a backing_object dumps new pages
3548 * into the parent that its pager doesn't know about
3549 * (and the collapse code can't merge the existence
3550 * maps).
3551 * Otherwise:
3552 * As long as one of the objects is still not known
3553 * to the pager, we can collapse them.
3554 */
3555 if (backing_object->ref_count == 1 &&
3556 (!object->pager_created
3557 #if !MACH_PAGEMAP
3558 || !backing_object->pager_created
3559 #endif /*!MACH_PAGEMAP */
3560 ) && vm_object_collapse_allowed) {
3561
3562 XPR(XPR_VM_OBJECT,
3563 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
3564 (integer_t)backing_object, (integer_t)object,
3565 (integer_t)backing_object->pager,
3566 (integer_t)backing_object->pager_control, 0);
3567
3568 /*
3569 * We need the cache lock for collapsing,
3570 * but we must not deadlock.
3571 */
3572
3573 if (! vm_object_cache_lock_try()) {
3574 if (object != original_object) {
3575 vm_object_unlock(object);
3576 }
3577 vm_object_unlock(backing_object);
3578 return;
3579 }
3580
3581 /*
3582 * ENCRYPTED SWAP
3583 * We can't collapse the object if it contains
3584 * any encypted page, because the encryption key
3585 * includes the <object,offset> info. We can't
3586 * drop the object lock in vm_object_do_collapse()
3587 * so we can't decrypt the page there either.
3588 */
3589 if (vm_pages_encrypted) {
3590 collapse_min_offset = object->shadow_offset;
3591 collapse_max_offset =
3592 object->shadow_offset + object->size;
3593 queue_iterate(&backing_object->memq,
3594 page, vm_page_t, listq) {
3595 if (page->encrypted &&
3596 (page->offset >=
3597 collapse_min_offset) &&
3598 (page->offset <
3599 collapse_max_offset)) {
3600 /*
3601 * We found an encrypted page
3602 * in the backing object,
3603 * within the range covered
3604 * by the parent object: we can
3605 * not collapse them.
3606 */
3607 vm_object_collapse_encrypted++;
3608 vm_object_cache_unlock();
3609 goto try_bypass;
3610 }
3611 }
3612 }
3613
3614 /*
3615 * Collapse the object with its backing
3616 * object, and try again with the object's
3617 * new backing object.
3618 */
3619
3620 vm_object_do_collapse(object, backing_object);
3621 vm_object_collapse_do_collapse++;
3622 continue;
3623 }
3624
3625 try_bypass:
3626 /*
3627 * Collapsing the backing object was not possible
3628 * or permitted, so let's try bypassing it.
3629 */
3630
3631 if (! (can_bypass && vm_object_bypass_allowed)) {
3632 /* try and collapse the rest of the shadow chain */
3633 if (object != original_object) {
3634 vm_object_unlock(object);
3635 }
3636 object = backing_object;
3637 continue;
3638 }
3639
3640
3641 /*
3642 * If the object doesn't have all its pages present,
3643 * we have to make sure no pages in the backing object
3644 * "show through" before bypassing it.
3645 */
3646 size = atop(object->size);
3647 rcount = object->resident_page_count;
3648 if (rcount != size) {
3649 vm_object_offset_t offset;
3650 vm_object_offset_t backing_offset;
3651 unsigned int backing_rcount;
3652 unsigned int lookups = 0;
3653
3654 /*
3655 * If the backing object has a pager but no pagemap,
3656 * then we cannot bypass it, because we don't know
3657 * what pages it has.
3658 */
3659 if (backing_object->pager_created
3660 #if MACH_PAGEMAP
3661 && (backing_object->existence_map == VM_EXTERNAL_NULL)
3662 #endif /* MACH_PAGEMAP */
3663 ) {
3664 /* try and collapse the rest of the shadow chain */
3665 if (object != original_object) {
3666 vm_object_unlock(object);
3667 }
3668 object = backing_object;
3669 continue;
3670 }
3671
3672 /*
3673 * If the object has a pager but no pagemap,
3674 * then we cannot bypass it, because we don't know
3675 * what pages it has.
3676 */
3677 if (object->pager_created
3678 #if MACH_PAGEMAP
3679 && (object->existence_map == VM_EXTERNAL_NULL)
3680 #endif /* MACH_PAGEMAP */
3681 ) {
3682 /* try and collapse the rest of the shadow chain */
3683 if (object != original_object) {
3684 vm_object_unlock(object);
3685 }
3686 object = backing_object;
3687 continue;
3688 }
3689
3690 /*
3691 * If all of the pages in the backing object are
3692 * shadowed by the parent object, the parent
3693 * object no longer has to shadow the backing
3694 * object; it can shadow the next one in the
3695 * chain.
3696 *
3697 * If the backing object has existence info,
3698 * we must check examine its existence info
3699 * as well.
3700 *
3701 */
3702
3703 backing_offset = object->shadow_offset;
3704 backing_rcount = backing_object->resident_page_count;
3705
3706 #define EXISTS_IN_OBJECT(obj, off, rc) \
3707 (vm_external_state_get((obj)->existence_map, \
3708 (vm_offset_t)(off)) == VM_EXTERNAL_STATE_EXISTS || \
3709 ((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
3710
3711 /*
3712 * Check the hint location first
3713 * (since it is often the quickest way out of here).
3714 */
3715 if (object->cow_hint != ~(vm_offset_t)0)
3716 hint_offset = (vm_object_offset_t)object->cow_hint;
3717 else
3718 hint_offset = (hint_offset > 8 * PAGE_SIZE_64) ?
3719 (hint_offset - 8 * PAGE_SIZE_64) : 0;
3720
3721 if (EXISTS_IN_OBJECT(backing_object, hint_offset +
3722 backing_offset, backing_rcount) &&
3723 !EXISTS_IN_OBJECT(object, hint_offset, rcount)) {
3724 /* dependency right at the hint */
3725 object->cow_hint = (vm_offset_t)hint_offset;
3726 /* try and collapse the rest of the shadow chain */
3727 if (object != original_object) {
3728 vm_object_unlock(object);
3729 }
3730 object = backing_object;
3731 continue;
3732 }
3733
3734 /*
3735 * If the object's window onto the backing_object
3736 * is large compared to the number of resident
3737 * pages in the backing object, it makes sense to
3738 * walk the backing_object's resident pages first.
3739 *
3740 * NOTE: Pages may be in both the existence map and
3741 * resident. So, we can't permanently decrement
3742 * the rcount here because the second loop may
3743 * find the same pages in the backing object'
3744 * existence map that we found here and we would
3745 * double-decrement the rcount. We also may or
3746 * may not have found the
3747 */
3748 if (backing_rcount && size >
3749 ((backing_object->existence_map) ?
3750 backing_rcount : (backing_rcount >> 1))) {
3751 unsigned int rc = rcount;
3752 vm_page_t p;
3753
3754 backing_rcount = backing_object->resident_page_count;
3755 p = (vm_page_t)queue_first(&backing_object->memq);
3756 do {
3757 /* Until we get more than one lookup lock */
3758 if (lookups > 256) {
3759 lookups = 0;
3760 delay(1);
3761 }
3762
3763 offset = (p->offset - backing_offset);
3764 if (offset < object->size &&
3765 offset != hint_offset &&
3766 !EXISTS_IN_OBJECT(object, offset, rc)) {
3767 /* found a dependency */
3768 object->cow_hint = (vm_offset_t)offset;
3769 break;
3770 }
3771 p = (vm_page_t) queue_next(&p->listq);
3772
3773 } while (--backing_rcount);
3774 if (backing_rcount != 0 ) {
3775 /* try and collapse the rest of the shadow chain */
3776 if (object != original_object) {
3777 vm_object_unlock(object);
3778 }
3779 object = backing_object;
3780 continue;
3781 }
3782 }
3783
3784 /*
3785 * Walk through the offsets looking for pages in the
3786 * backing object that show through to the object.
3787 */
3788 if (backing_rcount || backing_object->existence_map) {
3789 offset = hint_offset;
3790
3791 while((offset =
3792 (offset + PAGE_SIZE_64 < object->size) ?
3793 (offset + PAGE_SIZE_64) : 0) != hint_offset) {
3794
3795 /* Until we get more than one lookup lock */
3796 if (lookups > 256) {
3797 lookups = 0;
3798 delay(1);
3799 }
3800
3801 if (EXISTS_IN_OBJECT(backing_object, offset +
3802 backing_offset, backing_rcount) &&
3803 !EXISTS_IN_OBJECT(object, offset, rcount)) {
3804 /* found a dependency */
3805 object->cow_hint = (vm_offset_t)offset;
3806 break;
3807 }
3808 }
3809 if (offset != hint_offset) {
3810 /* try and collapse the rest of the shadow chain */
3811 if (object != original_object) {
3812 vm_object_unlock(object);
3813 }
3814 object = backing_object;
3815 continue;
3816 }
3817 }
3818 }
3819
3820 /* reset the offset hint for any objects deeper in the chain */
3821 object->cow_hint = (vm_offset_t)0;
3822
3823 /*
3824 * All interesting pages in the backing object
3825 * already live in the parent or its pager.
3826 * Thus we can bypass the backing object.
3827 */
3828
3829 vm_object_do_bypass(object, backing_object);
3830 vm_object_collapse_do_bypass++;
3831
3832 /*
3833 * Try again with this object's new backing object.
3834 */
3835
3836 continue;
3837 }
3838
3839 if (object != original_object) {
3840 vm_object_unlock(object);
3841 }
3842 }
3843
3844 /*
3845 * Routine: vm_object_page_remove: [internal]
3846 * Purpose:
3847 * Removes all physical pages in the specified
3848 * object range from the object's list of pages.
3849 *
3850 * In/out conditions:
3851 * The object must be locked.
3852 * The object must not have paging_in_progress, usually
3853 * guaranteed by not having a pager.
3854 */
3855 unsigned int vm_object_page_remove_lookup = 0;
3856 unsigned int vm_object_page_remove_iterate = 0;
3857
3858 __private_extern__ void
3859 vm_object_page_remove(
3860 register vm_object_t object,
3861 register vm_object_offset_t start,
3862 register vm_object_offset_t end)
3863 {
3864 register vm_page_t p, next;
3865
3866 /*
3867 * One and two page removals are most popular.
3868 * The factor of 16 here is somewhat arbitrary.
3869 * It balances vm_object_lookup vs iteration.
3870 */
3871
3872 if (atop_64(end - start) < (unsigned)object->resident_page_count/16) {
3873 vm_object_page_remove_lookup++;
3874
3875 for (; start < end; start += PAGE_SIZE_64) {
3876 p = vm_page_lookup(object, start);
3877 if (p != VM_PAGE_NULL) {
3878 assert(!p->cleaning && !p->pageout);
3879 if (!p->fictitious)
3880 pmap_disconnect(p->phys_page);
3881 VM_PAGE_FREE(p);
3882 }
3883 }
3884 } else {
3885 vm_object_page_remove_iterate++;
3886
3887 p = (vm_page_t) queue_first(&object->memq);
3888 while (!queue_end(&object->memq, (queue_entry_t) p)) {
3889 next = (vm_page_t) queue_next(&p->listq);
3890 if ((start <= p->offset) && (p->offset < end)) {
3891 assert(!p->cleaning && !p->pageout);
3892 if (!p->fictitious)
3893 pmap_disconnect(p->phys_page);
3894 VM_PAGE_FREE(p);
3895 }
3896 p = next;
3897 }
3898 }
3899 }
3900
3901
3902 /*
3903 * Routine: vm_object_coalesce
3904 * Function: Coalesces two objects backing up adjoining
3905 * regions of memory into a single object.
3906 *
3907 * returns TRUE if objects were combined.
3908 *
3909 * NOTE: Only works at the moment if the second object is NULL -
3910 * if it's not, which object do we lock first?
3911 *
3912 * Parameters:
3913 * prev_object First object to coalesce
3914 * prev_offset Offset into prev_object
3915 * next_object Second object into coalesce
3916 * next_offset Offset into next_object
3917 *
3918 * prev_size Size of reference to prev_object
3919 * next_size Size of reference to next_object
3920 *
3921 * Conditions:
3922 * The object(s) must *not* be locked. The map must be locked
3923 * to preserve the reference to the object(s).
3924 */
3925 static int vm_object_coalesce_count = 0;
3926
3927 __private_extern__ boolean_t
3928 vm_object_coalesce(
3929 register vm_object_t prev_object,
3930 vm_object_t next_object,
3931 vm_object_offset_t prev_offset,
3932 __unused vm_object_offset_t next_offset,
3933 vm_object_size_t prev_size,
3934 vm_object_size_t next_size)
3935 {
3936 vm_object_size_t newsize;
3937
3938 #ifdef lint
3939 next_offset++;
3940 #endif /* lint */
3941
3942 if (next_object != VM_OBJECT_NULL) {
3943 return(FALSE);
3944 }
3945
3946 if (prev_object == VM_OBJECT_NULL) {
3947 return(TRUE);
3948 }
3949
3950 XPR(XPR_VM_OBJECT,
3951 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
3952 (integer_t)prev_object, prev_offset, prev_size, next_size, 0);
3953
3954 vm_object_lock(prev_object);
3955
3956 /*
3957 * Try to collapse the object first
3958 */
3959 vm_object_collapse(prev_object, prev_offset, TRUE);
3960
3961 /*
3962 * Can't coalesce if pages not mapped to
3963 * prev_entry may be in use any way:
3964 * . more than one reference
3965 * . paged out
3966 * . shadows another object
3967 * . has a copy elsewhere
3968 * . is purgable
3969 * . paging references (pages might be in page-list)
3970 */
3971
3972 if ((prev_object->ref_count > 1) ||
3973 prev_object->pager_created ||
3974 (prev_object->shadow != VM_OBJECT_NULL) ||
3975 (prev_object->copy != VM_OBJECT_NULL) ||
3976 (prev_object->true_share != FALSE) ||
3977 (prev_object->purgable != VM_OBJECT_NONPURGABLE) ||
3978 (prev_object->paging_in_progress != 0)) {
3979 vm_object_unlock(prev_object);
3980 return(FALSE);
3981 }
3982
3983 vm_object_coalesce_count++;
3984
3985 /*
3986 * Remove any pages that may still be in the object from
3987 * a previous deallocation.
3988 */
3989 vm_object_page_remove(prev_object,
3990 prev_offset + prev_size,
3991 prev_offset + prev_size + next_size);
3992
3993 /*
3994 * Extend the object if necessary.
3995 */
3996 newsize = prev_offset + prev_size + next_size;
3997 if (newsize > prev_object->size) {
3998 #if MACH_PAGEMAP
3999 /*
4000 * We cannot extend an object that has existence info,
4001 * since the existence info might then fail to cover
4002 * the entire object.
4003 *
4004 * This assertion must be true because the object
4005 * has no pager, and we only create existence info
4006 * for objects with pagers.
4007 */
4008 assert(prev_object->existence_map == VM_EXTERNAL_NULL);
4009 #endif /* MACH_PAGEMAP */
4010 prev_object->size = newsize;
4011 }
4012
4013 vm_object_unlock(prev_object);
4014 return(TRUE);
4015 }
4016
4017 /*
4018 * Attach a set of physical pages to an object, so that they can
4019 * be mapped by mapping the object. Typically used to map IO memory.
4020 *
4021 * The mapping function and its private data are used to obtain the
4022 * physical addresses for each page to be mapped.
4023 */
4024 void
4025 vm_object_page_map(
4026 vm_object_t object,
4027 vm_object_offset_t offset,
4028 vm_object_size_t size,
4029 vm_object_offset_t (*map_fn)(void *map_fn_data,
4030 vm_object_offset_t offset),
4031 void *map_fn_data) /* private to map_fn */
4032 {
4033 int num_pages;
4034 int i;
4035 vm_page_t m;
4036 vm_page_t old_page;
4037 vm_object_offset_t addr;
4038
4039 num_pages = atop_64(size);
4040
4041 for (i = 0; i < num_pages; i++, offset += PAGE_SIZE_64) {
4042
4043 addr = (*map_fn)(map_fn_data, offset);
4044
4045 while ((m = vm_page_grab_fictitious()) == VM_PAGE_NULL)
4046 vm_page_more_fictitious();
4047
4048 vm_object_lock(object);
4049 if ((old_page = vm_page_lookup(object, offset))
4050 != VM_PAGE_NULL)
4051 {
4052 vm_page_lock_queues();
4053 vm_page_free(old_page);
4054 vm_page_unlock_queues();
4055 }
4056
4057 vm_page_init(m, addr);
4058 /* private normally requires lock_queues but since we */
4059 /* are initializing the page, its not necessary here */
4060 m->private = TRUE; /* don`t free page */
4061 m->wire_count = 1;
4062 vm_page_insert(m, object, offset);
4063
4064 PAGE_WAKEUP_DONE(m);
4065 vm_object_unlock(object);
4066 }
4067 }
4068
4069 #include <mach_kdb.h>
4070
4071 #if MACH_KDB
4072 #include <ddb/db_output.h>
4073 #include <vm/vm_print.h>
4074
4075 #define printf kdbprintf
4076
4077 extern boolean_t vm_object_cached(
4078 vm_object_t object);
4079
4080 extern void print_bitstring(
4081 char byte);
4082
4083 boolean_t vm_object_print_pages = FALSE;
4084
4085 void
4086 print_bitstring(
4087 char byte)
4088 {
4089 printf("%c%c%c%c%c%c%c%c",
4090 ((byte & (1 << 0)) ? '1' : '0'),
4091 ((byte & (1 << 1)) ? '1' : '0'),
4092 ((byte & (1 << 2)) ? '1' : '0'),
4093 ((byte & (1 << 3)) ? '1' : '0'),
4094 ((byte & (1 << 4)) ? '1' : '0'),
4095 ((byte & (1 << 5)) ? '1' : '0'),
4096 ((byte & (1 << 6)) ? '1' : '0'),
4097 ((byte & (1 << 7)) ? '1' : '0'));
4098 }
4099
4100 boolean_t
4101 vm_object_cached(
4102 register vm_object_t object)
4103 {
4104 register vm_object_t o;
4105
4106 queue_iterate(&vm_object_cached_list, o, vm_object_t, cached_list) {
4107 if (object == o) {
4108 return TRUE;
4109 }
4110 }
4111 return FALSE;
4112 }
4113
4114 #if MACH_PAGEMAP
4115 /*
4116 * vm_external_print: [ debug ]
4117 */
4118 void
4119 vm_external_print(
4120 vm_external_map_t emap,
4121 vm_size_t size)
4122 {
4123 if (emap == VM_EXTERNAL_NULL) {
4124 printf("0 ");
4125 } else {
4126 vm_size_t existence_size = stob(size);
4127 printf("{ size=%d, map=[", existence_size);
4128 if (existence_size > 0) {
4129 print_bitstring(emap[0]);
4130 }
4131 if (existence_size > 1) {
4132 print_bitstring(emap[1]);
4133 }
4134 if (existence_size > 2) {
4135 printf("...");
4136 print_bitstring(emap[existence_size-1]);
4137 }
4138 printf("] }\n");
4139 }
4140 return;
4141 }
4142 #endif /* MACH_PAGEMAP */
4143
4144 int
4145 vm_follow_object(
4146 vm_object_t object)
4147 {
4148 int count = 0;
4149 int orig_db_indent = db_indent;
4150
4151 while (TRUE) {
4152 if (object == VM_OBJECT_NULL) {
4153 db_indent = orig_db_indent;
4154 return count;
4155 }
4156
4157 count += 1;
4158
4159 iprintf("object 0x%x", object);
4160 printf(", shadow=0x%x", object->shadow);
4161 printf(", copy=0x%x", object->copy);
4162 printf(", pager=0x%x", object->pager);
4163 printf(", ref=%d\n", object->ref_count);
4164
4165 db_indent += 2;
4166 object = object->shadow;
4167 }
4168
4169 }
4170
4171 /*
4172 * vm_object_print: [ debug ]
4173 */
4174 void
4175 vm_object_print(
4176 db_addr_t db_addr,
4177 __unused boolean_t have_addr,
4178 __unused int arg_count,
4179 __unused char *modif)
4180 {
4181 vm_object_t object;
4182 register vm_page_t p;
4183 const char *s;
4184
4185 register int count;
4186
4187 object = (vm_object_t) (long) db_addr;
4188 if (object == VM_OBJECT_NULL)
4189 return;
4190
4191 iprintf("object 0x%x\n", object);
4192
4193 db_indent += 2;
4194
4195 iprintf("size=0x%x", object->size);
4196 printf(", cluster=0x%x", object->cluster_size);
4197 printf(", memq_hint=%p", object->memq_hint);
4198 printf(", ref_count=%d\n", object->ref_count);
4199 iprintf("");
4200 #if TASK_SWAPPER
4201 printf("res_count=%d, ", object->res_count);
4202 #endif /* TASK_SWAPPER */
4203 printf("resident_page_count=%d\n", object->resident_page_count);
4204
4205 iprintf("shadow=0x%x", object->shadow);
4206 if (object->shadow) {
4207 register int i = 0;
4208 vm_object_t shadow = object;
4209 while((shadow = shadow->shadow))
4210 i++;
4211 printf(" (depth %d)", i);
4212 }
4213 printf(", copy=0x%x", object->copy);
4214 printf(", shadow_offset=0x%x", object->shadow_offset);
4215 printf(", last_alloc=0x%x\n", object->last_alloc);
4216
4217 iprintf("pager=0x%x", object->pager);
4218 printf(", paging_offset=0x%x", object->paging_offset);
4219 printf(", pager_control=0x%x\n", object->pager_control);
4220
4221 iprintf("copy_strategy=%d[", object->copy_strategy);
4222 switch (object->copy_strategy) {
4223 case MEMORY_OBJECT_COPY_NONE:
4224 printf("copy_none");
4225 break;
4226
4227 case MEMORY_OBJECT_COPY_CALL:
4228 printf("copy_call");
4229 break;
4230
4231 case MEMORY_OBJECT_COPY_DELAY:
4232 printf("copy_delay");
4233 break;
4234
4235 case MEMORY_OBJECT_COPY_SYMMETRIC:
4236 printf("copy_symmetric");
4237 break;
4238
4239 case MEMORY_OBJECT_COPY_INVALID:
4240 printf("copy_invalid");
4241 break;
4242
4243 default:
4244 printf("?");
4245 }
4246 printf("]");
4247 printf(", absent_count=%d\n", object->absent_count);
4248
4249 iprintf("all_wanted=0x%x<", object->all_wanted);
4250 s = "";
4251 if (vm_object_wanted(object, VM_OBJECT_EVENT_INITIALIZED)) {
4252 printf("%sinit", s);
4253 s = ",";
4254 }
4255 if (vm_object_wanted(object, VM_OBJECT_EVENT_PAGER_READY)) {
4256 printf("%sready", s);
4257 s = ",";
4258 }
4259 if (vm_object_wanted(object, VM_OBJECT_EVENT_PAGING_IN_PROGRESS)) {
4260 printf("%spaging", s);
4261 s = ",";
4262 }
4263 if (vm_object_wanted(object, VM_OBJECT_EVENT_ABSENT_COUNT)) {
4264 printf("%sabsent", s);
4265 s = ",";
4266 }
4267 if (vm_object_wanted(object, VM_OBJECT_EVENT_LOCK_IN_PROGRESS)) {
4268 printf("%slock", s);
4269 s = ",";
4270 }
4271 if (vm_object_wanted(object, VM_OBJECT_EVENT_UNCACHING)) {
4272 printf("%suncaching", s);
4273 s = ",";
4274 }
4275 if (vm_object_wanted(object, VM_OBJECT_EVENT_COPY_CALL)) {
4276 printf("%scopy_call", s);
4277 s = ",";
4278 }
4279 if (vm_object_wanted(object, VM_OBJECT_EVENT_CACHING)) {
4280 printf("%scaching", s);
4281 s = ",";
4282 }
4283 printf(">");
4284 printf(", paging_in_progress=%d\n", object->paging_in_progress);
4285
4286 iprintf("%screated, %sinit, %sready, %spersist, %strusted, %spageout, %s, %s\n",
4287 (object->pager_created ? "" : "!"),
4288 (object->pager_initialized ? "" : "!"),
4289 (object->pager_ready ? "" : "!"),
4290 (object->can_persist ? "" : "!"),
4291 (object->pager_trusted ? "" : "!"),
4292 (object->pageout ? "" : "!"),
4293 (object->internal ? "internal" : "external"),
4294 (object->temporary ? "temporary" : "permanent"));
4295 iprintf("%salive, %spurgable, %spurgable_volatile, %spurgable_empty, %sshadowed, %scached, %sprivate\n",
4296 (object->alive ? "" : "!"),
4297 ((object->purgable != VM_OBJECT_NONPURGABLE) ? "" : "!"),
4298 ((object->purgable == VM_OBJECT_PURGABLE_VOLATILE) ? "" : "!"),
4299 ((object->purgable == VM_OBJECT_PURGABLE_EMPTY) ? "" : "!"),
4300 (object->shadowed ? "" : "!"),
4301 (vm_object_cached(object) ? "" : "!"),
4302 (object->private ? "" : "!"));
4303 iprintf("%sadvisory_pageout, %ssilent_overwrite\n",
4304 (object->advisory_pageout ? "" : "!"),
4305 (object->silent_overwrite ? "" : "!"));
4306
4307 #if MACH_PAGEMAP
4308 iprintf("existence_map=");
4309 vm_external_print(object->existence_map, object->size);
4310 #endif /* MACH_PAGEMAP */
4311 #if MACH_ASSERT
4312 iprintf("paging_object=0x%x\n", object->paging_object);
4313 #endif /* MACH_ASSERT */
4314
4315 if (vm_object_print_pages) {
4316 count = 0;
4317 p = (vm_page_t) queue_first(&object->memq);
4318 while (!queue_end(&object->memq, (queue_entry_t) p)) {
4319 if (count == 0) {
4320 iprintf("memory:=");
4321 } else if (count == 2) {
4322 printf("\n");
4323 iprintf(" ...");
4324 count = 0;
4325 } else {
4326 printf(",");
4327 }
4328 count++;
4329
4330 printf("(off=0x%llX,page=%p)", p->offset, p);
4331 p = (vm_page_t) queue_next(&p->listq);
4332 }
4333 if (count != 0) {
4334 printf("\n");
4335 }
4336 }
4337 db_indent -= 2;
4338 }
4339
4340
4341 /*
4342 * vm_object_find [ debug ]
4343 *
4344 * Find all tasks which reference the given vm_object.
4345 */
4346
4347 boolean_t vm_object_find(vm_object_t object);
4348 boolean_t vm_object_print_verbose = FALSE;
4349
4350 boolean_t
4351 vm_object_find(
4352 vm_object_t object)
4353 {
4354 task_t task;
4355 vm_map_t map;
4356 vm_map_entry_t entry;
4357 processor_set_t pset = &default_pset;
4358 boolean_t found = FALSE;
4359
4360 queue_iterate(&pset->tasks, task, task_t, pset_tasks) {
4361 map = task->map;
4362 for (entry = vm_map_first_entry(map);
4363 entry && entry != vm_map_to_entry(map);
4364 entry = entry->vme_next) {
4365
4366 vm_object_t obj;
4367
4368 /*
4369 * For the time being skip submaps,
4370 * only the kernel can have submaps,
4371 * and unless we are interested in
4372 * kernel objects, we can simply skip
4373 * submaps. See sb/dejan/nmk18b7/src/mach_kernel/vm
4374 * for a full solution.
4375 */
4376 if (entry->is_sub_map)
4377 continue;
4378 if (entry)
4379 obj = entry->object.vm_object;
4380 else
4381 continue;
4382
4383 while (obj != VM_OBJECT_NULL) {
4384 if (obj == object) {
4385 if (!found) {
4386 printf("TASK\t\tMAP\t\tENTRY\n");
4387 found = TRUE;
4388 }
4389 printf("0x%x\t0x%x\t0x%x\n",
4390 task, map, entry);
4391 }
4392 obj = obj->shadow;
4393 }
4394 }
4395 }
4396
4397 return(found);
4398 }
4399
4400 #endif /* MACH_KDB */
4401
4402 kern_return_t
4403 vm_object_populate_with_private(
4404 vm_object_t object,
4405 vm_object_offset_t offset,
4406 ppnum_t phys_page,
4407 vm_size_t size)
4408 {
4409 ppnum_t base_page;
4410 vm_object_offset_t base_offset;
4411
4412
4413 if(!object->private)
4414 return KERN_FAILURE;
4415
4416 base_page = phys_page;
4417
4418 vm_object_lock(object);
4419 if(!object->phys_contiguous) {
4420 vm_page_t m;
4421 if((base_offset = trunc_page_64(offset)) != offset) {
4422 vm_object_unlock(object);
4423 return KERN_FAILURE;
4424 }
4425 base_offset += object->paging_offset;
4426 while(size) {
4427 m = vm_page_lookup(object, base_offset);
4428 if(m != VM_PAGE_NULL) {
4429 if(m->fictitious) {
4430 vm_page_lock_queues();
4431 m->fictitious = FALSE;
4432 m->private = TRUE;
4433 m->phys_page = base_page;
4434 if(!m->busy) {
4435 m->busy = TRUE;
4436 }
4437 if(!m->absent) {
4438 m->absent = TRUE;
4439 object->absent_count++;
4440 }
4441 m->list_req_pending = TRUE;
4442 vm_page_unlock_queues();
4443 } else if (m->phys_page != base_page) {
4444 /* pmap call to clear old mapping */
4445 pmap_disconnect(m->phys_page);
4446 m->phys_page = base_page;
4447 }
4448
4449 /*
4450 * ENCRYPTED SWAP:
4451 * We're not pointing to the same
4452 * physical page any longer and the
4453 * contents of the new one are not
4454 * supposed to be encrypted.
4455 * XXX What happens to the original
4456 * physical page. Is it lost ?
4457 */
4458 m->encrypted = FALSE;
4459
4460 } else {
4461 while ((m = vm_page_grab_fictitious())
4462 == VM_PAGE_NULL)
4463 vm_page_more_fictitious();
4464 vm_page_lock_queues();
4465 m->fictitious = FALSE;
4466 m->private = TRUE;
4467 m->phys_page = base_page;
4468 m->list_req_pending = TRUE;
4469 m->absent = TRUE;
4470 m->unusual = TRUE;
4471 object->absent_count++;
4472 vm_page_unlock_queues();
4473 vm_page_insert(m, object, base_offset);
4474 }
4475 base_page++; /* Go to the next physical page */
4476 base_offset += PAGE_SIZE;
4477 size -= PAGE_SIZE;
4478 }
4479 } else {
4480 /* NOTE: we should check the original settings here */
4481 /* if we have a size > zero a pmap call should be made */
4482 /* to disable the range */
4483
4484 /* pmap_? */
4485
4486 /* shadows on contiguous memory are not allowed */
4487 /* we therefore can use the offset field */
4488 object->shadow_offset = (vm_object_offset_t)(phys_page << 12);
4489 object->size = size;
4490 }
4491 vm_object_unlock(object);
4492 return KERN_SUCCESS;
4493 }
4494
4495 /*
4496 * memory_object_free_from_cache:
4497 *
4498 * Walk the vm_object cache list, removing and freeing vm_objects
4499 * which are backed by the pager identified by the caller, (pager_ops).
4500 * Remove up to "count" objects, if there are that may available
4501 * in the cache.
4502 *
4503 * Walk the list at most once, return the number of vm_objects
4504 * actually freed.
4505 */
4506
4507 __private_extern__ kern_return_t
4508 memory_object_free_from_cache(
4509 __unused host_t host,
4510 memory_object_pager_ops_t pager_ops,
4511 int *count)
4512 {
4513
4514 int object_released = 0;
4515
4516 register vm_object_t object = VM_OBJECT_NULL;
4517 vm_object_t shadow;
4518
4519 /*
4520 if(host == HOST_NULL)
4521 return(KERN_INVALID_ARGUMENT);
4522 */
4523
4524 try_again:
4525 vm_object_cache_lock();
4526
4527 queue_iterate(&vm_object_cached_list, object,
4528 vm_object_t, cached_list) {
4529 if (object->pager &&
4530 (pager_ops == object->pager->mo_pager_ops)) {
4531 vm_object_lock(object);
4532 queue_remove(&vm_object_cached_list, object,
4533 vm_object_t, cached_list);
4534 vm_object_cached_count--;
4535
4536 /*
4537 * Since this object is in the cache, we know
4538 * that it is initialized and has only a pager's
4539 * (implicit) reference. Take a reference to avoid
4540 * recursive deallocations.
4541 */
4542
4543 assert(object->pager_initialized);
4544 assert(object->ref_count == 0);
4545 object->ref_count++;
4546
4547 /*
4548 * Terminate the object.
4549 * If the object had a shadow, we let
4550 * vm_object_deallocate deallocate it.
4551 * "pageout" objects have a shadow, but
4552 * maintain a "paging reference" rather
4553 * than a normal reference.
4554 * (We are careful here to limit recursion.)
4555 */
4556 shadow = object->pageout?VM_OBJECT_NULL:object->shadow;
4557 if ((vm_object_terminate(object) == KERN_SUCCESS)
4558 && (shadow != VM_OBJECT_NULL)) {
4559 vm_object_deallocate(shadow);
4560 }
4561
4562 if(object_released++ == *count)
4563 return KERN_SUCCESS;
4564 goto try_again;
4565 }
4566 }
4567 vm_object_cache_unlock();
4568 *count = object_released;
4569 return KERN_SUCCESS;
4570 }
4571
4572
4573
4574 kern_return_t
4575 memory_object_create_named(
4576 memory_object_t pager,
4577 memory_object_offset_t size,
4578 memory_object_control_t *control)
4579 {
4580 vm_object_t object;
4581 vm_object_hash_entry_t entry;
4582
4583 *control = MEMORY_OBJECT_CONTROL_NULL;
4584 if (pager == MEMORY_OBJECT_NULL)
4585 return KERN_INVALID_ARGUMENT;
4586
4587 vm_object_cache_lock();
4588 entry = vm_object_hash_lookup(pager, FALSE);
4589 if ((entry != VM_OBJECT_HASH_ENTRY_NULL) &&
4590 (entry->object != VM_OBJECT_NULL)) {
4591 if (entry->object->named == TRUE)
4592 panic("memory_object_create_named: caller already holds the right"); }
4593
4594 vm_object_cache_unlock();
4595 if ((object = vm_object_enter(pager, size, FALSE, FALSE, TRUE))
4596 == VM_OBJECT_NULL) {
4597 return(KERN_INVALID_OBJECT);
4598 }
4599
4600 /* wait for object (if any) to be ready */
4601 if (object != VM_OBJECT_NULL) {
4602 vm_object_lock(object);
4603 object->named = TRUE;
4604 while (!object->pager_ready) {
4605 vm_object_sleep(object,
4606 VM_OBJECT_EVENT_PAGER_READY,
4607 THREAD_UNINT);
4608 }
4609 *control = object->pager_control;
4610 vm_object_unlock(object);
4611 }
4612 return (KERN_SUCCESS);
4613 }
4614
4615
4616 /*
4617 * Routine: memory_object_recover_named [user interface]
4618 * Purpose:
4619 * Attempt to recover a named reference for a VM object.
4620 * VM will verify that the object has not already started
4621 * down the termination path, and if it has, will optionally
4622 * wait for that to finish.
4623 * Returns:
4624 * KERN_SUCCESS - we recovered a named reference on the object
4625 * KERN_FAILURE - we could not recover a reference (object dead)
4626 * KERN_INVALID_ARGUMENT - bad memory object control
4627 */
4628 kern_return_t
4629 memory_object_recover_named(
4630 memory_object_control_t control,
4631 boolean_t wait_on_terminating)
4632 {
4633 vm_object_t object;
4634
4635 vm_object_cache_lock();
4636 object = memory_object_control_to_vm_object(control);
4637 if (object == VM_OBJECT_NULL) {
4638 vm_object_cache_unlock();
4639 return (KERN_INVALID_ARGUMENT);
4640 }
4641
4642 restart:
4643 vm_object_lock(object);
4644
4645 if (object->terminating && wait_on_terminating) {
4646 vm_object_cache_unlock();
4647 vm_object_wait(object,
4648 VM_OBJECT_EVENT_PAGING_IN_PROGRESS,
4649 THREAD_UNINT);
4650 vm_object_cache_lock();
4651 goto restart;
4652 }
4653
4654 if (!object->alive) {
4655 vm_object_cache_unlock();
4656 vm_object_unlock(object);
4657 return KERN_FAILURE;
4658 }
4659
4660 if (object->named == TRUE) {
4661 vm_object_cache_unlock();
4662 vm_object_unlock(object);
4663 return KERN_SUCCESS;
4664 }
4665
4666 if((object->ref_count == 0) && (!object->terminating)){
4667 queue_remove(&vm_object_cached_list, object,
4668 vm_object_t, cached_list);
4669 vm_object_cached_count--;
4670 XPR(XPR_VM_OBJECT_CACHE,
4671 "memory_object_recover_named: removing %X, head (%X, %X)\n",
4672 (integer_t)object,
4673 (integer_t)vm_object_cached_list.next,
4674 (integer_t)vm_object_cached_list.prev, 0,0);
4675 }
4676
4677 vm_object_cache_unlock();
4678
4679 object->named = TRUE;
4680 object->ref_count++;
4681 vm_object_res_reference(object);
4682 while (!object->pager_ready) {
4683 vm_object_sleep(object,
4684 VM_OBJECT_EVENT_PAGER_READY,
4685 THREAD_UNINT);
4686 }
4687 vm_object_unlock(object);
4688 return (KERN_SUCCESS);
4689 }
4690
4691
4692 /*
4693 * vm_object_release_name:
4694 *
4695 * Enforces name semantic on memory_object reference count decrement
4696 * This routine should not be called unless the caller holds a name
4697 * reference gained through the memory_object_create_named.
4698 *
4699 * If the TERMINATE_IDLE flag is set, the call will return if the
4700 * reference count is not 1. i.e. idle with the only remaining reference
4701 * being the name.
4702 * If the decision is made to proceed the name field flag is set to
4703 * false and the reference count is decremented. If the RESPECT_CACHE
4704 * flag is set and the reference count has gone to zero, the
4705 * memory_object is checked to see if it is cacheable otherwise when
4706 * the reference count is zero, it is simply terminated.
4707 */
4708
4709 __private_extern__ kern_return_t
4710 vm_object_release_name(
4711 vm_object_t object,
4712 int flags)
4713 {
4714 vm_object_t shadow;
4715 boolean_t original_object = TRUE;
4716
4717 while (object != VM_OBJECT_NULL) {
4718
4719 /*
4720 * The cache holds a reference (uncounted) to
4721 * the object. We must locke it before removing
4722 * the object.
4723 *
4724 */
4725
4726 vm_object_cache_lock();
4727 vm_object_lock(object);
4728 assert(object->alive);
4729 if(original_object)
4730 assert(object->named);
4731 assert(object->ref_count > 0);
4732
4733 /*
4734 * We have to wait for initialization before
4735 * destroying or caching the object.
4736 */
4737
4738 if (object->pager_created && !object->pager_initialized) {
4739 assert(!object->can_persist);
4740 vm_object_assert_wait(object,
4741 VM_OBJECT_EVENT_INITIALIZED,
4742 THREAD_UNINT);
4743 vm_object_unlock(object);
4744 vm_object_cache_unlock();
4745 thread_block(THREAD_CONTINUE_NULL);
4746 continue;
4747 }
4748
4749 if (((object->ref_count > 1)
4750 && (flags & MEMORY_OBJECT_TERMINATE_IDLE))
4751 || (object->terminating)) {
4752 vm_object_unlock(object);
4753 vm_object_cache_unlock();
4754 return KERN_FAILURE;
4755 } else {
4756 if (flags & MEMORY_OBJECT_RELEASE_NO_OP) {
4757 vm_object_unlock(object);
4758 vm_object_cache_unlock();
4759 return KERN_SUCCESS;
4760 }
4761 }
4762
4763 if ((flags & MEMORY_OBJECT_RESPECT_CACHE) &&
4764 (object->ref_count == 1)) {
4765 if(original_object)
4766 object->named = FALSE;
4767 vm_object_unlock(object);
4768 vm_object_cache_unlock();
4769 /* let vm_object_deallocate push this thing into */
4770 /* the cache, if that it is where it is bound */
4771 vm_object_deallocate(object);
4772 return KERN_SUCCESS;
4773 }
4774 VM_OBJ_RES_DECR(object);
4775 shadow = object->pageout?VM_OBJECT_NULL:object->shadow;
4776 if(object->ref_count == 1) {
4777 if(vm_object_terminate(object) != KERN_SUCCESS) {
4778 if(original_object) {
4779 return KERN_FAILURE;
4780 } else {
4781 return KERN_SUCCESS;
4782 }
4783 }
4784 if (shadow != VM_OBJECT_NULL) {
4785 original_object = FALSE;
4786 object = shadow;
4787 continue;
4788 }
4789 return KERN_SUCCESS;
4790 } else {
4791 object->ref_count--;
4792 assert(object->ref_count > 0);
4793 if(original_object)
4794 object->named = FALSE;
4795 vm_object_unlock(object);
4796 vm_object_cache_unlock();
4797 return KERN_SUCCESS;
4798 }
4799 }
4800 /*NOTREACHED*/
4801 assert(0);
4802 return KERN_FAILURE;
4803 }
4804
4805
4806 __private_extern__ kern_return_t
4807 vm_object_lock_request(
4808 vm_object_t object,
4809 vm_object_offset_t offset,
4810 vm_object_size_t size,
4811 memory_object_return_t should_return,
4812 int flags,
4813 vm_prot_t prot)
4814 {
4815 __unused boolean_t should_flush;
4816
4817 should_flush = flags & MEMORY_OBJECT_DATA_FLUSH;
4818
4819 XPR(XPR_MEMORY_OBJECT,
4820 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
4821 (integer_t)object, offset, size,
4822 (((should_return&1)<<1)|should_flush), prot);
4823
4824 /*
4825 * Check for bogus arguments.
4826 */
4827 if (object == VM_OBJECT_NULL)
4828 return (KERN_INVALID_ARGUMENT);
4829
4830 if ((prot & ~VM_PROT_ALL) != 0 && prot != VM_PROT_NO_CHANGE)
4831 return (KERN_INVALID_ARGUMENT);
4832
4833 size = round_page_64(size);
4834
4835 /*
4836 * Lock the object, and acquire a paging reference to
4837 * prevent the memory_object reference from being released.
4838 */
4839 vm_object_lock(object);
4840 vm_object_paging_begin(object);
4841
4842 (void)vm_object_update(object,
4843 offset, size, NULL, NULL, should_return, flags, prot);
4844
4845 vm_object_paging_end(object);
4846 vm_object_unlock(object);
4847
4848 return (KERN_SUCCESS);
4849 }
4850
4851 /*
4852 * Empty a purgable object by grabbing the physical pages assigned to it and
4853 * putting them on the free queue without writing them to backing store, etc.
4854 * When the pages are next touched they will be demand zero-fill pages. We
4855 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
4856 * skip referenced/dirty pages, pages on the active queue, etc. We're more
4857 * than happy to grab these since this is a purgable object. We mark the
4858 * object as "empty" after reaping its pages.
4859 *
4860 * On entry the object and page queues are locked, the object must be a
4861 * purgable object with no delayed copies pending.
4862 */
4863 unsigned int
4864 vm_object_purge(vm_object_t object)
4865 {
4866 vm_page_t p, next;
4867 unsigned int num_purged_pages;
4868 vm_page_t local_freeq;
4869 unsigned long local_freed;
4870 int purge_loop_quota;
4871 /* free pages as soon as we gather PURGE_BATCH_FREE_LIMIT pages to free */
4872 #define PURGE_BATCH_FREE_LIMIT 50
4873 /* release page queues lock every PURGE_LOOP_QUOTA iterations */
4874 #define PURGE_LOOP_QUOTA 100
4875
4876 num_purged_pages = 0;
4877 if (object->purgable == VM_OBJECT_NONPURGABLE)
4878 return num_purged_pages;
4879
4880 object->purgable = VM_OBJECT_PURGABLE_EMPTY;
4881
4882 assert(object->copy == VM_OBJECT_NULL);
4883 assert(object->copy_strategy == MEMORY_OBJECT_COPY_NONE);
4884 purge_loop_quota = PURGE_LOOP_QUOTA;
4885
4886 local_freeq = VM_PAGE_NULL;
4887 local_freed = 0;
4888
4889 /*
4890 * Go through the object's resident pages and try and discard them.
4891 */
4892 next = (vm_page_t)queue_first(&object->memq);
4893 while (!queue_end(&object->memq, (queue_entry_t)next)) {
4894 p = next;
4895 next = (vm_page_t)queue_next(&next->listq);
4896
4897 if (purge_loop_quota-- == 0) {
4898 /*
4899 * Avoid holding the page queues lock for too long.
4900 * Let someone else take it for a while if needed.
4901 * Keep holding the object's lock to guarantee that
4902 * the object's page list doesn't change under us
4903 * while we yield.
4904 */
4905 if (local_freeq != VM_PAGE_NULL) {
4906 /*
4907 * Flush our queue of pages to free.
4908 */
4909 vm_page_free_list(local_freeq);
4910 local_freeq = VM_PAGE_NULL;
4911 local_freed = 0;
4912 }
4913 vm_page_unlock_queues();
4914 mutex_pause();
4915 vm_page_lock_queues();
4916
4917 /* resume with the current page and a new quota */
4918 purge_loop_quota = PURGE_LOOP_QUOTA;
4919 }
4920
4921
4922 if (p->busy || p->cleaning || p->laundry ||
4923 p->list_req_pending) {
4924 /* page is being acted upon, so don't mess with it */
4925 continue;
4926 }
4927 if (p->wire_count) {
4928 /* don't discard a wired page */
4929 continue;
4930 }
4931
4932 if (p->tabled) {
4933 /* clean up the object/offset table */
4934 vm_page_remove(p);
4935 }
4936 if (p->absent) {
4937 /* update the object's count of absent pages */
4938 vm_object_absent_release(object);
4939 }
4940
4941 /* we can discard this page */
4942
4943 /* advertize that this page is in a transition state */
4944 p->busy = TRUE;
4945
4946 if (p->no_isync == TRUE) {
4947 /* the page hasn't been mapped yet */
4948 /* (optimization to delay the i-cache sync) */
4949 } else {
4950 /* unmap the page */
4951 int refmod_state;
4952
4953 refmod_state = pmap_disconnect(p->phys_page);
4954 if (refmod_state & VM_MEM_MODIFIED) {
4955 p->dirty = TRUE;
4956 }
4957 }
4958
4959 if (p->dirty || p->precious) {
4960 /* we saved the cost of cleaning this page ! */
4961 num_purged_pages++;
4962 vm_page_purged_count++;
4963 }
4964
4965 /* remove page from active or inactive queue... */
4966 VM_PAGE_QUEUES_REMOVE(p);
4967
4968 /* ... and put it on our queue of pages to free */
4969 assert(!p->laundry);
4970 assert(p->object != kernel_object);
4971 assert(p->pageq.next == NULL &&
4972 p->pageq.prev == NULL);
4973 p->pageq.next = (queue_entry_t) local_freeq;
4974 local_freeq = p;
4975 if (++local_freed >= PURGE_BATCH_FREE_LIMIT) {
4976 /* flush our queue of pages to free */
4977 vm_page_free_list(local_freeq);
4978 local_freeq = VM_PAGE_NULL;
4979 local_freed = 0;
4980 }
4981 }
4982
4983 /* flush our local queue of pages to free one last time */
4984 if (local_freeq != VM_PAGE_NULL) {
4985 vm_page_free_list(local_freeq);
4986 local_freeq = VM_PAGE_NULL;
4987 local_freed = 0;
4988 }
4989
4990 return num_purged_pages;
4991 }
4992
4993 /*
4994 * vm_object_purgable_control() allows the caller to control and investigate the
4995 * state of a purgable object. A purgable object is created via a call to
4996 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgable object will
4997 * never be coalesced with any other object -- even other purgable objects --
4998 * and will thus always remain a distinct object. A purgable object has
4999 * special semantics when its reference count is exactly 1. If its reference
5000 * count is greater than 1, then a purgable object will behave like a normal
5001 * object and attempts to use this interface will result in an error return
5002 * of KERN_INVALID_ARGUMENT.
5003 *
5004 * A purgable object may be put into a "volatile" state which will make the
5005 * object's pages elligable for being reclaimed without paging to backing
5006 * store if the system runs low on memory. If the pages in a volatile
5007 * purgable object are reclaimed, the purgable object is said to have been
5008 * "emptied." When a purgable object is emptied the system will reclaim as
5009 * many pages from the object as it can in a convenient manner (pages already
5010 * en route to backing store or busy for other reasons are left as is). When
5011 * a purgable object is made volatile, its pages will generally be reclaimed
5012 * before other pages in the application's working set. This semantic is
5013 * generally used by applications which can recreate the data in the object
5014 * faster than it can be paged in. One such example might be media assets
5015 * which can be reread from a much faster RAID volume.
5016 *
5017 * A purgable object may be designated as "non-volatile" which means it will
5018 * behave like all other objects in the system with pages being written to and
5019 * read from backing store as needed to satisfy system memory needs. If the
5020 * object was emptied before the object was made non-volatile, that fact will
5021 * be returned as the old state of the purgable object (see
5022 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
5023 * were reclaimed as part of emptying the object will be refaulted in as
5024 * zero-fill on demand. It is up to the application to note that an object
5025 * was emptied and recreate the objects contents if necessary. When a
5026 * purgable object is made non-volatile, its pages will generally not be paged
5027 * out to backing store in the immediate future. A purgable object may also
5028 * be manually emptied.
5029 *
5030 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
5031 * volatile purgable object may be queried at any time. This information may
5032 * be used as a control input to let the application know when the system is
5033 * experiencing memory pressure and is reclaiming memory.
5034 *
5035 * The specified address may be any address within the purgable object. If
5036 * the specified address does not represent any object in the target task's
5037 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
5038 * object containing the specified address is not a purgable object, then
5039 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
5040 * returned.
5041 *
5042 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
5043 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
5044 * state is used to set the new state of the purgable object and return its
5045 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgable
5046 * object is returned in the parameter state.
5047 *
5048 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
5049 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
5050 * the non-volatile, volatile and volatile/empty states described above.
5051 * Setting the state of a purgable object to VM_PURGABLE_EMPTY will
5052 * immediately reclaim as many pages in the object as can be conveniently
5053 * collected (some may have already been written to backing store or be
5054 * otherwise busy).
5055 *
5056 * The process of making a purgable object non-volatile and determining its
5057 * previous state is atomic. Thus, if a purgable object is made
5058 * VM_PURGABLE_NONVOLATILE and the old state is returned as
5059 * VM_PURGABLE_VOLATILE, then the purgable object's previous contents are
5060 * completely intact and will remain so until the object is made volatile
5061 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
5062 * was reclaimed while it was in a volatile state and its previous contents
5063 * have been lost.
5064 */
5065 /*
5066 * The object must be locked.
5067 */
5068 kern_return_t
5069 vm_object_purgable_control(
5070 vm_object_t object,
5071 vm_purgable_t control,
5072 int *state)
5073 {
5074 int old_state;
5075 vm_page_t p;
5076
5077 if (object == VM_OBJECT_NULL) {
5078 /*
5079 * Object must already be present or it can't be purgable.
5080 */
5081 return KERN_INVALID_ARGUMENT;
5082 }
5083
5084 /*
5085 * Get current state of the purgable object.
5086 */
5087 switch (object->purgable) {
5088 case VM_OBJECT_NONPURGABLE:
5089 return KERN_INVALID_ARGUMENT;
5090
5091 case VM_OBJECT_PURGABLE_NONVOLATILE:
5092 old_state = VM_PURGABLE_NONVOLATILE;
5093 break;
5094
5095 case VM_OBJECT_PURGABLE_VOLATILE:
5096 old_state = VM_PURGABLE_VOLATILE;
5097 break;
5098
5099 case VM_OBJECT_PURGABLE_EMPTY:
5100 old_state = VM_PURGABLE_EMPTY;
5101 break;
5102
5103 default:
5104 old_state = VM_PURGABLE_NONVOLATILE;
5105 panic("Bad state (%d) for purgable object!\n",
5106 object->purgable);
5107 /*NOTREACHED*/
5108 }
5109
5110 /* purgable cant have delayed copies - now or in the future */
5111 assert(object->copy == VM_OBJECT_NULL);
5112 assert(object->copy_strategy == MEMORY_OBJECT_COPY_NONE);
5113
5114 /*
5115 * Execute the desired operation.
5116 */
5117 if (control == VM_PURGABLE_GET_STATE) {
5118 *state = old_state;
5119 return KERN_SUCCESS;
5120 }
5121
5122 switch (*state) {
5123 case VM_PURGABLE_NONVOLATILE:
5124 vm_page_lock_queues();
5125 if (object->purgable != VM_OBJECT_PURGABLE_NONVOLATILE) {
5126 assert(vm_page_purgeable_count >=
5127 object->resident_page_count);
5128 vm_page_purgeable_count -= object->resident_page_count;
5129 }
5130
5131 object->purgable = VM_OBJECT_PURGABLE_NONVOLATILE;
5132
5133 /*
5134 * If the object wasn't emptied, then mark all pages of the
5135 * object as referenced in order to give them a complete turn
5136 * of the virtual memory "clock" before becoming candidates
5137 * for paging out (if the system is suffering from memory
5138 * pressure). We don't really need to set the pmap reference
5139 * bits (which would be expensive) since the software copies
5140 * are believed if they're set to true ...
5141 */
5142 if (old_state != VM_PURGABLE_EMPTY) {
5143 for (p = (vm_page_t)queue_first(&object->memq);
5144 !queue_end(&object->memq, (queue_entry_t)p);
5145 p = (vm_page_t)queue_next(&p->listq))
5146 p->reference = TRUE;
5147 }
5148
5149 vm_page_unlock_queues();
5150
5151 break;
5152
5153 case VM_PURGABLE_VOLATILE:
5154 vm_page_lock_queues();
5155
5156 if (object->purgable != VM_OBJECT_PURGABLE_VOLATILE &&
5157 object->purgable != VM_OBJECT_PURGABLE_EMPTY) {
5158 vm_page_purgeable_count += object->resident_page_count;
5159 }
5160
5161 object->purgable = VM_OBJECT_PURGABLE_VOLATILE;
5162
5163 /*
5164 * We want the newly volatile purgable object to be a
5165 * candidate for the pageout scan before other pages in the
5166 * application if the system is suffering from memory
5167 * pressure. To do this, we move a page of the object from
5168 * the active queue onto the inactive queue in order to
5169 * promote the object for early reclaim. We only need to move
5170 * a single page since the pageout scan will reap the entire
5171 * purgable object if it finds a single page in a volatile
5172 * state. Obviously we don't do this if there are no pages
5173 * associated with the object or we find a page of the object
5174 * already on the inactive queue.
5175 */
5176 for (p = (vm_page_t)queue_first(&object->memq);
5177 !queue_end(&object->memq, (queue_entry_t)p);
5178 p = (vm_page_t)queue_next(&p->listq)) {
5179 if (p->inactive) {
5180 /* already a page on the inactive queue */
5181 break;
5182 }
5183 if (p->active && !p->busy) {
5184 /* found one we can move */
5185 vm_page_deactivate(p);
5186 break;
5187 }
5188 }
5189 vm_page_unlock_queues();
5190
5191 break;
5192
5193
5194 case VM_PURGABLE_EMPTY:
5195 vm_page_lock_queues();
5196 if (object->purgable != VM_OBJECT_PURGABLE_VOLATILE &&
5197 object->purgable != VM_OBJECT_PURGABLE_EMPTY) {
5198 vm_page_purgeable_count += object->resident_page_count;
5199 }
5200 (void) vm_object_purge(object);
5201 vm_page_unlock_queues();
5202 break;
5203
5204 }
5205 *state = old_state;
5206
5207 return KERN_SUCCESS;
5208 }
5209
5210 #if TASK_SWAPPER
5211 /*
5212 * vm_object_res_deallocate
5213 *
5214 * (recursively) decrement residence counts on vm objects and their shadows.
5215 * Called from vm_object_deallocate and when swapping out an object.
5216 *
5217 * The object is locked, and remains locked throughout the function,
5218 * even as we iterate down the shadow chain. Locks on intermediate objects
5219 * will be dropped, but not the original object.
5220 *
5221 * NOTE: this function used to use recursion, rather than iteration.
5222 */
5223
5224 __private_extern__ void
5225 vm_object_res_deallocate(
5226 vm_object_t object)
5227 {
5228 vm_object_t orig_object = object;
5229 /*
5230 * Object is locked so it can be called directly
5231 * from vm_object_deallocate. Original object is never
5232 * unlocked.
5233 */
5234 assert(object->res_count > 0);
5235 while (--object->res_count == 0) {
5236 assert(object->ref_count >= object->res_count);
5237 vm_object_deactivate_all_pages(object);
5238 /* iterate on shadow, if present */
5239 if (object->shadow != VM_OBJECT_NULL) {
5240 vm_object_t tmp_object = object->shadow;
5241 vm_object_lock(tmp_object);
5242 if (object != orig_object)
5243 vm_object_unlock(object);
5244 object = tmp_object;
5245 assert(object->res_count > 0);
5246 } else
5247 break;
5248 }
5249 if (object != orig_object)
5250 vm_object_unlock(object);
5251 }
5252
5253 /*
5254 * vm_object_res_reference
5255 *
5256 * Internal function to increment residence count on a vm object
5257 * and its shadows. It is called only from vm_object_reference, and
5258 * when swapping in a vm object, via vm_map_swap.
5259 *
5260 * The object is locked, and remains locked throughout the function,
5261 * even as we iterate down the shadow chain. Locks on intermediate objects
5262 * will be dropped, but not the original object.
5263 *
5264 * NOTE: this function used to use recursion, rather than iteration.
5265 */
5266
5267 __private_extern__ void
5268 vm_object_res_reference(
5269 vm_object_t object)
5270 {
5271 vm_object_t orig_object = object;
5272 /*
5273 * Object is locked, so this can be called directly
5274 * from vm_object_reference. This lock is never released.
5275 */
5276 while ((++object->res_count == 1) &&
5277 (object->shadow != VM_OBJECT_NULL)) {
5278 vm_object_t tmp_object = object->shadow;
5279
5280 assert(object->ref_count >= object->res_count);
5281 vm_object_lock(tmp_object);
5282 if (object != orig_object)
5283 vm_object_unlock(object);
5284 object = tmp_object;
5285 }
5286 if (object != orig_object)
5287 vm_object_unlock(object);
5288 assert(orig_object->ref_count >= orig_object->res_count);
5289 }
5290 #endif /* TASK_SWAPPER */
5291
5292 /*
5293 * vm_object_reference:
5294 *
5295 * Gets another reference to the given object.
5296 */
5297 #ifdef vm_object_reference
5298 #undef vm_object_reference
5299 #endif
5300 __private_extern__ void
5301 vm_object_reference(
5302 register vm_object_t object)
5303 {
5304 if (object == VM_OBJECT_NULL)
5305 return;
5306
5307 vm_object_lock(object);
5308 assert(object->ref_count > 0);
5309 vm_object_reference_locked(object);
5310 vm_object_unlock(object);
5311 }
5312
5313 #ifdef MACH_BSD
5314 /*
5315 * Scale the vm_object_cache
5316 * This is required to make sure that the vm_object_cache is big
5317 * enough to effectively cache the mapped file.
5318 * This is really important with UBC as all the regular file vnodes
5319 * have memory object associated with them. Havving this cache too
5320 * small results in rapid reclaim of vnodes and hurts performance a LOT!
5321 *
5322 * This is also needed as number of vnodes can be dynamically scaled.
5323 */
5324 kern_return_t
5325 adjust_vm_object_cache(
5326 __unused vm_size_t oval,
5327 vm_size_t nval)
5328 {
5329 vm_object_cached_max = nval;
5330 vm_object_cache_trim(FALSE);
5331 return (KERN_SUCCESS);
5332 }
5333 #endif /* MACH_BSD */
5334
5335
5336 /*
5337 * vm_object_transpose
5338 *
5339 * This routine takes two VM objects of the same size and exchanges
5340 * their backing store.
5341 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
5342 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
5343 *
5344 * The VM objects must not be locked by caller.
5345 */
5346 kern_return_t
5347 vm_object_transpose(
5348 vm_object_t object1,
5349 vm_object_t object2,
5350 vm_object_size_t transpose_size)
5351 {
5352 vm_object_t tmp_object;
5353 kern_return_t retval;
5354 boolean_t object1_locked, object2_locked;
5355 boolean_t object1_paging, object2_paging;
5356 vm_page_t page;
5357 vm_object_offset_t page_offset;
5358
5359 tmp_object = VM_OBJECT_NULL;
5360 object1_locked = FALSE; object2_locked = FALSE;
5361 object1_paging = FALSE; object2_paging = FALSE;
5362
5363 if (object1 == object2 ||
5364 object1 == VM_OBJECT_NULL ||
5365 object2 == VM_OBJECT_NULL) {
5366 /*
5367 * If the 2 VM objects are the same, there's
5368 * no point in exchanging their backing store.
5369 */
5370 retval = KERN_INVALID_VALUE;
5371 goto done;
5372 }
5373
5374 vm_object_lock(object1);
5375 object1_locked = TRUE;
5376 if (object1->copy || object1->shadow || object1->shadowed ||
5377 object1->purgable != VM_OBJECT_NONPURGABLE) {
5378 /*
5379 * We don't deal with copy or shadow objects (yet).
5380 */
5381 retval = KERN_INVALID_VALUE;
5382 goto done;
5383 }
5384 /*
5385 * Since we're about to mess with the object's backing store,
5386 * mark it as "paging_in_progress". Note that this is not enough
5387 * to prevent any paging activity on this object, so the caller should
5388 * have "quiesced" the objects beforehand, via a UPL operation with
5389 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
5390 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
5391 */
5392 vm_object_paging_begin(object1);
5393 object1_paging = TRUE;
5394 vm_object_unlock(object1);
5395 object1_locked = FALSE;
5396
5397 /*
5398 * Same as above for the 2nd object...
5399 */
5400 vm_object_lock(object2);
5401 object2_locked = TRUE;
5402 if (object2->copy || object2->shadow || object2->shadowed ||
5403 object2->purgable != VM_OBJECT_NONPURGABLE) {
5404 retval = KERN_INVALID_VALUE;
5405 goto done;
5406 }
5407 vm_object_paging_begin(object2);
5408 object2_paging = TRUE;
5409 vm_object_unlock(object2);
5410 object2_locked = FALSE;
5411
5412 /*
5413 * Allocate a temporary VM object to hold object1's contents
5414 * while we copy object2 to object1.
5415 */
5416 tmp_object = vm_object_allocate(transpose_size);
5417 vm_object_lock(tmp_object);
5418 vm_object_paging_begin(tmp_object);
5419 tmp_object->can_persist = FALSE;
5420
5421 /*
5422 * Since we need to lock both objects at the same time,
5423 * make sure we always lock them in the same order to
5424 * avoid deadlocks.
5425 */
5426 if (object1 < object2) {
5427 vm_object_lock(object1);
5428 vm_object_lock(object2);
5429 } else {
5430 vm_object_lock(object2);
5431 vm_object_lock(object1);
5432 }
5433 object1_locked = TRUE;
5434 object2_locked = TRUE;
5435
5436 if (object1->size != object2->size ||
5437 object1->size != transpose_size) {
5438 /*
5439 * If the 2 objects don't have the same size, we can't
5440 * exchange their backing stores or one would overflow.
5441 * If their size doesn't match the caller's
5442 * "transpose_size", we can't do it either because the
5443 * transpose operation will affect the entire span of
5444 * the objects.
5445 */
5446 retval = KERN_INVALID_VALUE;
5447 goto done;
5448 }
5449
5450
5451 /*
5452 * Transpose the lists of resident pages.
5453 */
5454 if (object1->phys_contiguous || queue_empty(&object1->memq)) {
5455 /*
5456 * No pages in object1, just transfer pages
5457 * from object2 to object1. No need to go through
5458 * an intermediate object.
5459 */
5460 while (!queue_empty(&object2->memq)) {
5461 page = (vm_page_t) queue_first(&object2->memq);
5462 vm_page_rename(page, object1, page->offset);
5463 }
5464 assert(queue_empty(&object2->memq));
5465 } else if (object2->phys_contiguous || queue_empty(&object2->memq)) {
5466 /*
5467 * No pages in object2, just transfer pages
5468 * from object1 to object2. No need to go through
5469 * an intermediate object.
5470 */
5471 while (!queue_empty(&object1->memq)) {
5472 page = (vm_page_t) queue_first(&object1->memq);
5473 vm_page_rename(page, object2, page->offset);
5474 }
5475 assert(queue_empty(&object1->memq));
5476 } else {
5477 /* transfer object1's pages to tmp_object */
5478 vm_page_lock_queues();
5479 while (!queue_empty(&object1->memq)) {
5480 page = (vm_page_t) queue_first(&object1->memq);
5481 page_offset = page->offset;
5482 vm_page_remove(page);
5483 page->offset = page_offset;
5484 queue_enter(&tmp_object->memq, page, vm_page_t, listq);
5485 }
5486 vm_page_unlock_queues();
5487 assert(queue_empty(&object1->memq));
5488 /* transfer object2's pages to object1 */
5489 while (!queue_empty(&object2->memq)) {
5490 page = (vm_page_t) queue_first(&object2->memq);
5491 vm_page_rename(page, object1, page->offset);
5492 }
5493 assert(queue_empty(&object2->memq));
5494 /* transfer tmp_object's pages to object1 */
5495 while (!queue_empty(&tmp_object->memq)) {
5496 page = (vm_page_t) queue_first(&tmp_object->memq);
5497 queue_remove(&tmp_object->memq, page,
5498 vm_page_t, listq);
5499 vm_page_insert(page, object2, page->offset);
5500 }
5501 assert(queue_empty(&tmp_object->memq));
5502 }
5503
5504 /* no need to transpose the size: they should be identical */
5505 assert(object1->size == object2->size);
5506
5507 #define __TRANSPOSE_FIELD(field) \
5508 MACRO_BEGIN \
5509 tmp_object->field = object1->field; \
5510 object1->field = object2->field; \
5511 object2->field = tmp_object->field; \
5512 MACRO_END
5513
5514 assert(!object1->copy);
5515 assert(!object2->copy);
5516
5517 assert(!object1->shadow);
5518 assert(!object2->shadow);
5519
5520 __TRANSPOSE_FIELD(shadow_offset); /* used by phys_contiguous objects */
5521 __TRANSPOSE_FIELD(pager);
5522 __TRANSPOSE_FIELD(paging_offset);
5523
5524 __TRANSPOSE_FIELD(pager_control);
5525 /* update the memory_objects' pointers back to the VM objects */
5526 if (object1->pager_control != MEMORY_OBJECT_CONTROL_NULL) {
5527 memory_object_control_collapse(object1->pager_control,
5528 object1);
5529 }
5530 if (object2->pager_control != MEMORY_OBJECT_CONTROL_NULL) {
5531 memory_object_control_collapse(object2->pager_control,
5532 object2);
5533 }
5534
5535 __TRANSPOSE_FIELD(absent_count);
5536
5537 assert(object1->paging_in_progress);
5538 assert(object2->paging_in_progress);
5539
5540 __TRANSPOSE_FIELD(pager_created);
5541 __TRANSPOSE_FIELD(pager_initialized);
5542 __TRANSPOSE_FIELD(pager_ready);
5543 __TRANSPOSE_FIELD(pager_trusted);
5544 __TRANSPOSE_FIELD(internal);
5545 __TRANSPOSE_FIELD(temporary);
5546 __TRANSPOSE_FIELD(private);
5547 __TRANSPOSE_FIELD(pageout);
5548 __TRANSPOSE_FIELD(true_share);
5549 __TRANSPOSE_FIELD(phys_contiguous);
5550 __TRANSPOSE_FIELD(nophyscache);
5551 __TRANSPOSE_FIELD(last_alloc);
5552 __TRANSPOSE_FIELD(sequential);
5553 __TRANSPOSE_FIELD(cluster_size);
5554 __TRANSPOSE_FIELD(existence_map);
5555 __TRANSPOSE_FIELD(cow_hint);
5556 __TRANSPOSE_FIELD(wimg_bits);
5557
5558 #undef __TRANSPOSE_FIELD
5559
5560 retval = KERN_SUCCESS;
5561
5562 done:
5563 /*
5564 * Cleanup.
5565 */
5566 if (tmp_object != VM_OBJECT_NULL) {
5567 vm_object_paging_end(tmp_object);
5568 vm_object_unlock(tmp_object);
5569 /*
5570 * Re-initialize the temporary object to avoid
5571 * deallocating a real pager.
5572 */
5573 _vm_object_allocate(transpose_size, tmp_object);
5574 vm_object_deallocate(tmp_object);
5575 tmp_object = VM_OBJECT_NULL;
5576 }
5577
5578 if (object1_locked) {
5579 vm_object_unlock(object1);
5580 object1_locked = FALSE;
5581 }
5582 if (object2_locked) {
5583 vm_object_unlock(object2);
5584 object2_locked = FALSE;
5585 }
5586 if (object1_paging) {
5587 vm_object_lock(object1);
5588 vm_object_paging_end(object1);
5589 vm_object_unlock(object1);
5590 object1_paging = FALSE;
5591 }
5592 if (object2_paging) {
5593 vm_object_lock(object2);
5594 vm_object_paging_end(object2);
5595 vm_object_unlock(object2);
5596 object2_paging = FALSE;
5597 }
5598
5599 return retval;
5600 }
5601
5602
5603 /* Allow manipulation of individual page state. This is actually part of */
5604 /* the UPL regimen but takes place on the VM object rather than on a UPL */
5605
5606 kern_return_t
5607 vm_object_page_op(
5608 vm_object_t object,
5609 vm_object_offset_t offset,
5610 int ops,
5611 ppnum_t *phys_entry,
5612 int *flags)
5613 {
5614 vm_page_t dst_page;
5615
5616 vm_object_lock(object);
5617
5618 if(ops & UPL_POP_PHYSICAL) {
5619 if(object->phys_contiguous) {
5620 if (phys_entry) {
5621 *phys_entry = (ppnum_t)
5622 (object->shadow_offset >> 12);
5623 }
5624 vm_object_unlock(object);
5625 return KERN_SUCCESS;
5626 } else {
5627 vm_object_unlock(object);
5628 return KERN_INVALID_OBJECT;
5629 }
5630 }
5631 if(object->phys_contiguous) {
5632 vm_object_unlock(object);
5633 return KERN_INVALID_OBJECT;
5634 }
5635
5636 while(TRUE) {
5637 if((dst_page = vm_page_lookup(object,offset)) == VM_PAGE_NULL) {
5638 vm_object_unlock(object);
5639 return KERN_FAILURE;
5640 }
5641
5642 /* Sync up on getting the busy bit */
5643 if((dst_page->busy || dst_page->cleaning) &&
5644 (((ops & UPL_POP_SET) &&
5645 (ops & UPL_POP_BUSY)) || (ops & UPL_POP_DUMP))) {
5646 /* someone else is playing with the page, we will */
5647 /* have to wait */
5648 PAGE_SLEEP(object, dst_page, THREAD_UNINT);
5649 continue;
5650 }
5651
5652 if (ops & UPL_POP_DUMP) {
5653 vm_page_lock_queues();
5654
5655 if (dst_page->no_isync == FALSE)
5656 pmap_disconnect(dst_page->phys_page);
5657 vm_page_free(dst_page);
5658
5659 vm_page_unlock_queues();
5660 break;
5661 }
5662
5663 if (flags) {
5664 *flags = 0;
5665
5666 /* Get the condition of flags before requested ops */
5667 /* are undertaken */
5668
5669 if(dst_page->dirty) *flags |= UPL_POP_DIRTY;
5670 if(dst_page->pageout) *flags |= UPL_POP_PAGEOUT;
5671 if(dst_page->precious) *flags |= UPL_POP_PRECIOUS;
5672 if(dst_page->absent) *flags |= UPL_POP_ABSENT;
5673 if(dst_page->busy) *flags |= UPL_POP_BUSY;
5674 }
5675
5676 /* The caller should have made a call either contingent with */
5677 /* or prior to this call to set UPL_POP_BUSY */
5678 if(ops & UPL_POP_SET) {
5679 /* The protection granted with this assert will */
5680 /* not be complete. If the caller violates the */
5681 /* convention and attempts to change page state */
5682 /* without first setting busy we may not see it */
5683 /* because the page may already be busy. However */
5684 /* if such violations occur we will assert sooner */
5685 /* or later. */
5686 assert(dst_page->busy || (ops & UPL_POP_BUSY));
5687 if (ops & UPL_POP_DIRTY) dst_page->dirty = TRUE;
5688 if (ops & UPL_POP_PAGEOUT) dst_page->pageout = TRUE;
5689 if (ops & UPL_POP_PRECIOUS) dst_page->precious = TRUE;
5690 if (ops & UPL_POP_ABSENT) dst_page->absent = TRUE;
5691 if (ops & UPL_POP_BUSY) dst_page->busy = TRUE;
5692 }
5693
5694 if(ops & UPL_POP_CLR) {
5695 assert(dst_page->busy);
5696 if (ops & UPL_POP_DIRTY) dst_page->dirty = FALSE;
5697 if (ops & UPL_POP_PAGEOUT) dst_page->pageout = FALSE;
5698 if (ops & UPL_POP_PRECIOUS) dst_page->precious = FALSE;
5699 if (ops & UPL_POP_ABSENT) dst_page->absent = FALSE;
5700 if (ops & UPL_POP_BUSY) {
5701 dst_page->busy = FALSE;
5702 PAGE_WAKEUP(dst_page);
5703 }
5704 }
5705
5706 if (dst_page->encrypted) {
5707 /*
5708 * ENCRYPTED SWAP:
5709 * We need to decrypt this encrypted page before the
5710 * caller can access its contents.
5711 * But if the caller really wants to access the page's
5712 * contents, they have to keep the page "busy".
5713 * Otherwise, the page could get recycled or re-encrypted
5714 * at any time.
5715 */
5716 if ((ops & UPL_POP_SET) && (ops & UPL_POP_BUSY) &&
5717 dst_page->busy) {
5718 /*
5719 * The page is stable enough to be accessed by
5720 * the caller, so make sure its contents are
5721 * not encrypted.
5722 */
5723 vm_page_decrypt(dst_page, 0);
5724 } else {
5725 /*
5726 * The page is not busy, so don't bother
5727 * decrypting it, since anything could
5728 * happen to it between now and when the
5729 * caller wants to access it.
5730 * We should not give the caller access
5731 * to this page.
5732 */
5733 assert(!phys_entry);
5734 }
5735 }
5736
5737 if (phys_entry) {
5738 /*
5739 * The physical page number will remain valid
5740 * only if the page is kept busy.
5741 * ENCRYPTED SWAP: make sure we don't let the
5742 * caller access an encrypted page.
5743 */
5744 assert(dst_page->busy);
5745 assert(!dst_page->encrypted);
5746 *phys_entry = dst_page->phys_page;
5747 }
5748
5749 break;
5750 }
5751
5752 vm_object_unlock(object);
5753 return KERN_SUCCESS;
5754
5755 }
5756
5757 /*
5758 * vm_object_range_op offers performance enhancement over
5759 * vm_object_page_op for page_op functions which do not require page
5760 * level state to be returned from the call. Page_op was created to provide
5761 * a low-cost alternative to page manipulation via UPLs when only a single
5762 * page was involved. The range_op call establishes the ability in the _op
5763 * family of functions to work on multiple pages where the lack of page level
5764 * state handling allows the caller to avoid the overhead of the upl structures.
5765 */
5766
5767 kern_return_t
5768 vm_object_range_op(
5769 vm_object_t object,
5770 vm_object_offset_t offset_beg,
5771 vm_object_offset_t offset_end,
5772 int ops,
5773 int *range)
5774 {
5775 vm_object_offset_t offset;
5776 vm_page_t dst_page;
5777
5778 if (object->resident_page_count == 0) {
5779 if (range) {
5780 if (ops & UPL_ROP_PRESENT)
5781 *range = 0;
5782 else
5783 *range = offset_end - offset_beg;
5784 }
5785 return KERN_SUCCESS;
5786 }
5787 vm_object_lock(object);
5788
5789 if (object->phys_contiguous) {
5790 vm_object_unlock(object);
5791 return KERN_INVALID_OBJECT;
5792 }
5793
5794 offset = offset_beg;
5795
5796 while (offset < offset_end) {
5797 dst_page = vm_page_lookup(object, offset);
5798 if (dst_page != VM_PAGE_NULL) {
5799 if (ops & UPL_ROP_DUMP) {
5800 if (dst_page->busy || dst_page->cleaning) {
5801 /*
5802 * someone else is playing with the
5803 * page, we will have to wait
5804 */
5805 PAGE_SLEEP(object,
5806 dst_page, THREAD_UNINT);
5807 /*
5808 * need to relook the page up since it's
5809 * state may have changed while we slept
5810 * it might even belong to a different object
5811 * at this point
5812 */
5813 continue;
5814 }
5815 vm_page_lock_queues();
5816
5817 if (dst_page->no_isync == FALSE)
5818 pmap_disconnect(dst_page->phys_page);
5819 vm_page_free(dst_page);
5820
5821 vm_page_unlock_queues();
5822 } else if (ops & UPL_ROP_ABSENT)
5823 break;
5824 } else if (ops & UPL_ROP_PRESENT)
5825 break;
5826
5827 offset += PAGE_SIZE;
5828 }
5829 vm_object_unlock(object);
5830
5831 if (range)
5832 *range = offset - offset_beg;
5833
5834 return KERN_SUCCESS;
5835 }