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