]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_object.c
2f7d54e3c1d05b002742f6367b3b89505ad6adef
[apple/xnu.git] / osfmk / vm / vm_object.c
1 /*
2 * Copyright (c) 2000-2007 Apple 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 <debug.h>
66 #include <mach_pagemap.h>
67 #include <task_swapper.h>
68
69 #include <mach/mach_types.h>
70 #include <mach/memory_object.h>
71 #include <mach/memory_object_default.h>
72 #include <mach/memory_object_control_server.h>
73 #include <mach/vm_param.h>
74
75 #include <ipc/ipc_types.h>
76 #include <ipc/ipc_port.h>
77
78 #include <kern/kern_types.h>
79 #include <kern/assert.h>
80 #include <kern/lock.h>
81 #include <kern/queue.h>
82 #include <kern/xpr.h>
83 #include <kern/kalloc.h>
84 #include <kern/zalloc.h>
85 #include <kern/host.h>
86 #include <kern/host_statistics.h>
87 #include <kern/processor.h>
88 #include <kern/misc_protos.h>
89
90 #include <vm/memory_object.h>
91 #include <vm/vm_fault.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_pageout.h>
96 #include <vm/vm_protos.h>
97 #include <vm/vm_purgeable_internal.h>
98
99 #if CONFIG_EMBEDDED
100 #include <sys/kern_memorystatus.h>
101 #endif
102
103 /*
104 * Virtual memory objects maintain the actual data
105 * associated with allocated virtual memory. A given
106 * page of memory exists within exactly one object.
107 *
108 * An object is only deallocated when all "references"
109 * are given up.
110 *
111 * Associated with each object is a list of all resident
112 * memory pages belonging to that object; this list is
113 * maintained by the "vm_page" module, but locked by the object's
114 * lock.
115 *
116 * Each object also records the memory object reference
117 * that is used by the kernel to request and write
118 * back data (the memory object, field "pager"), etc...
119 *
120 * Virtual memory objects are allocated to provide
121 * zero-filled memory (vm_allocate) or map a user-defined
122 * memory object into a virtual address space (vm_map).
123 *
124 * Virtual memory objects that refer to a user-defined
125 * memory object are called "permanent", because all changes
126 * made in virtual memory are reflected back to the
127 * memory manager, which may then store it permanently.
128 * Other virtual memory objects are called "temporary",
129 * meaning that changes need be written back only when
130 * necessary to reclaim pages, and that storage associated
131 * with the object can be discarded once it is no longer
132 * mapped.
133 *
134 * A permanent memory object may be mapped into more
135 * than one virtual address space. Moreover, two threads
136 * may attempt to make the first mapping of a memory
137 * object concurrently. Only one thread is allowed to
138 * complete this mapping; all others wait for the
139 * "pager_initialized" field is asserted, indicating
140 * that the first thread has initialized all of the
141 * necessary fields in the virtual memory object structure.
142 *
143 * The kernel relies on a *default memory manager* to
144 * provide backing storage for the zero-filled virtual
145 * memory objects. The pager memory objects associated
146 * with these temporary virtual memory objects are only
147 * requested from the default memory manager when it
148 * becomes necessary. Virtual memory objects
149 * that depend on the default memory manager are called
150 * "internal". The "pager_created" field is provided to
151 * indicate whether these ports have ever been allocated.
152 *
153 * The kernel may also create virtual memory objects to
154 * hold changed pages after a copy-on-write operation.
155 * In this case, the virtual memory object (and its
156 * backing storage -- its memory object) only contain
157 * those pages that have been changed. The "shadow"
158 * field refers to the virtual memory object that contains
159 * the remainder of the contents. The "shadow_offset"
160 * field indicates where in the "shadow" these contents begin.
161 * The "copy" field refers to a virtual memory object
162 * to which changed pages must be copied before changing
163 * this object, in order to implement another form
164 * of copy-on-write optimization.
165 *
166 * The virtual memory object structure also records
167 * the attributes associated with its memory object.
168 * The "pager_ready", "can_persist" and "copy_strategy"
169 * fields represent those attributes. The "cached_list"
170 * field is used in the implementation of the persistence
171 * attribute.
172 *
173 * ZZZ Continue this comment.
174 */
175
176 /* Forward declarations for internal functions. */
177 static kern_return_t vm_object_terminate(
178 vm_object_t object);
179
180 extern void vm_object_remove(
181 vm_object_t object);
182
183 static kern_return_t vm_object_copy_call(
184 vm_object_t src_object,
185 vm_object_offset_t src_offset,
186 vm_object_size_t size,
187 vm_object_t *_result_object);
188
189 static void vm_object_do_collapse(
190 vm_object_t object,
191 vm_object_t backing_object);
192
193 static void vm_object_do_bypass(
194 vm_object_t object,
195 vm_object_t backing_object);
196
197 static void vm_object_release_pager(
198 memory_object_t pager,
199 boolean_t hashed);
200
201 static zone_t vm_object_zone; /* vm backing store zone */
202
203 /*
204 * All wired-down kernel memory belongs to a single virtual
205 * memory object (kernel_object) to avoid wasting data structures.
206 */
207 static struct vm_object kernel_object_store;
208 vm_object_t kernel_object;
209
210
211 /*
212 * The submap object is used as a placeholder for vm_map_submap
213 * operations. The object is declared in vm_map.c because it
214 * is exported by the vm_map module. The storage is declared
215 * here because it must be initialized here.
216 */
217 static struct vm_object vm_submap_object_store;
218
219 /*
220 * Virtual memory objects are initialized from
221 * a template (see vm_object_allocate).
222 *
223 * When adding a new field to the virtual memory
224 * object structure, be sure to add initialization
225 * (see _vm_object_allocate()).
226 */
227 static struct vm_object vm_object_template;
228
229 unsigned int vm_page_purged_wired = 0;
230 unsigned int vm_page_purged_busy = 0;
231 unsigned int vm_page_purged_others = 0;
232
233 #if VM_OBJECT_CACHE
234 /*
235 * Virtual memory objects that are not referenced by
236 * any address maps, but that are allowed to persist
237 * (an attribute specified by the associated memory manager),
238 * are kept in a queue (vm_object_cached_list).
239 *
240 * When an object from this queue is referenced again,
241 * for example to make another address space mapping,
242 * it must be removed from the queue. That is, the
243 * queue contains *only* objects with zero references.
244 *
245 * The kernel may choose to terminate objects from this
246 * queue in order to reclaim storage. The current policy
247 * is to permit a fixed maximum number of unreferenced
248 * objects (vm_object_cached_max).
249 *
250 * A spin lock (accessed by routines
251 * vm_object_cache_{lock,lock_try,unlock}) governs the
252 * object cache. It must be held when objects are
253 * added to or removed from the cache (in vm_object_terminate).
254 * The routines that acquire a reference to a virtual
255 * memory object based on one of the memory object ports
256 * must also lock the cache.
257 *
258 * Ideally, the object cache should be more isolated
259 * from the reference mechanism, so that the lock need
260 * not be held to make simple references.
261 */
262 static vm_object_t vm_object_cache_trim(
263 boolean_t called_from_vm_object_deallocate);
264
265 static void vm_object_deactivate_all_pages(
266 vm_object_t object);
267
268 static int vm_object_cached_high; /* highest # cached objects */
269 static int vm_object_cached_max = 512; /* may be patched*/
270
271 #define vm_object_cache_lock() \
272 lck_mtx_lock(&vm_object_cached_lock_data)
273 #define vm_object_cache_lock_try() \
274 lck_mtx_try_lock(&vm_object_cached_lock_data)
275
276 #endif /* VM_OBJECT_CACHE */
277
278 static queue_head_t vm_object_cached_list;
279 static uint32_t vm_object_cache_pages_freed = 0;
280 static uint32_t vm_object_cache_pages_moved = 0;
281 static uint32_t vm_object_cache_pages_skipped = 0;
282 static uint32_t vm_object_cache_adds = 0;
283 static uint32_t vm_object_cached_count = 0;
284 static lck_mtx_t vm_object_cached_lock_data;
285 static lck_mtx_ext_t vm_object_cached_lock_data_ext;
286
287 static uint32_t vm_object_page_grab_failed = 0;
288 static uint32_t vm_object_page_grab_skipped = 0;
289 static uint32_t vm_object_page_grab_returned = 0;
290 static uint32_t vm_object_page_grab_pmapped = 0;
291 static uint32_t vm_object_page_grab_reactivations = 0;
292
293 #define vm_object_cache_lock_spin() \
294 lck_mtx_lock_spin(&vm_object_cached_lock_data)
295 #define vm_object_cache_unlock() \
296 lck_mtx_unlock(&vm_object_cached_lock_data)
297
298 static void vm_object_cache_remove_locked(vm_object_t);
299
300
301 #define VM_OBJECT_HASH_COUNT 1024
302 #define VM_OBJECT_HASH_LOCK_COUNT 512
303
304 static lck_mtx_t vm_object_hashed_lock_data[VM_OBJECT_HASH_LOCK_COUNT];
305 static lck_mtx_ext_t vm_object_hashed_lock_data_ext[VM_OBJECT_HASH_LOCK_COUNT];
306
307 static queue_head_t vm_object_hashtable[VM_OBJECT_HASH_COUNT];
308 static struct zone *vm_object_hash_zone;
309
310 struct vm_object_hash_entry {
311 queue_chain_t hash_link; /* hash chain link */
312 memory_object_t pager; /* pager we represent */
313 vm_object_t object; /* corresponding object */
314 boolean_t waiting; /* someone waiting for
315 * termination */
316 };
317
318 typedef struct vm_object_hash_entry *vm_object_hash_entry_t;
319 #define VM_OBJECT_HASH_ENTRY_NULL ((vm_object_hash_entry_t) 0)
320
321 #define VM_OBJECT_HASH_SHIFT 5
322 #define vm_object_hash(pager) \
323 ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_COUNT))
324
325 #define vm_object_lock_hash(pager) \
326 ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_LOCK_COUNT))
327
328 void vm_object_hash_entry_free(
329 vm_object_hash_entry_t entry);
330
331 static void vm_object_reap(vm_object_t object);
332 static void vm_object_reap_async(vm_object_t object);
333 static void vm_object_reaper_thread(void);
334
335 static lck_mtx_t vm_object_reaper_lock_data;
336 static lck_mtx_ext_t vm_object_reaper_lock_data_ext;
337
338 static queue_head_t vm_object_reaper_queue; /* protected by vm_object_reaper_lock() */
339 unsigned int vm_object_reap_count = 0;
340 unsigned int vm_object_reap_count_async = 0;
341
342 #define vm_object_reaper_lock() \
343 lck_mtx_lock(&vm_object_reaper_lock_data)
344 #define vm_object_reaper_lock_spin() \
345 lck_mtx_lock_spin(&vm_object_reaper_lock_data)
346 #define vm_object_reaper_unlock() \
347 lck_mtx_unlock(&vm_object_reaper_lock_data)
348
349 #if 0
350 #undef KERNEL_DEBUG
351 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
352 #endif
353
354
355 static lck_mtx_t *
356 vm_object_hash_lock_spin(
357 memory_object_t pager)
358 {
359 int index;
360
361 index = vm_object_lock_hash(pager);
362
363 lck_mtx_lock_spin(&vm_object_hashed_lock_data[index]);
364
365 return (&vm_object_hashed_lock_data[index]);
366 }
367
368 static void
369 vm_object_hash_unlock(lck_mtx_t *lck)
370 {
371 lck_mtx_unlock(lck);
372 }
373
374
375 /*
376 * vm_object_hash_lookup looks up a pager in the hashtable
377 * and returns the corresponding entry, with optional removal.
378 */
379 static vm_object_hash_entry_t
380 vm_object_hash_lookup(
381 memory_object_t pager,
382 boolean_t remove_entry)
383 {
384 queue_t bucket;
385 vm_object_hash_entry_t entry;
386
387 bucket = &vm_object_hashtable[vm_object_hash(pager)];
388
389 entry = (vm_object_hash_entry_t)queue_first(bucket);
390 while (!queue_end(bucket, (queue_entry_t)entry)) {
391 if (entry->pager == pager) {
392 if (remove_entry) {
393 queue_remove(bucket, entry,
394 vm_object_hash_entry_t, hash_link);
395 }
396 return(entry);
397 }
398 entry = (vm_object_hash_entry_t)queue_next(&entry->hash_link);
399 }
400 return(VM_OBJECT_HASH_ENTRY_NULL);
401 }
402
403 /*
404 * vm_object_hash_enter enters the specified
405 * pager / cache object association in the hashtable.
406 */
407
408 static void
409 vm_object_hash_insert(
410 vm_object_hash_entry_t entry,
411 vm_object_t object)
412 {
413 queue_t bucket;
414
415 bucket = &vm_object_hashtable[vm_object_hash(entry->pager)];
416
417 queue_enter(bucket, entry, vm_object_hash_entry_t, hash_link);
418
419 entry->object = object;
420 object->hashed = TRUE;
421 }
422
423 static vm_object_hash_entry_t
424 vm_object_hash_entry_alloc(
425 memory_object_t pager)
426 {
427 vm_object_hash_entry_t entry;
428
429 entry = (vm_object_hash_entry_t)zalloc(vm_object_hash_zone);
430 entry->pager = pager;
431 entry->object = VM_OBJECT_NULL;
432 entry->waiting = FALSE;
433
434 return(entry);
435 }
436
437 void
438 vm_object_hash_entry_free(
439 vm_object_hash_entry_t entry)
440 {
441 zfree(vm_object_hash_zone, entry);
442 }
443
444 /*
445 * vm_object_allocate:
446 *
447 * Returns a new object with the given size.
448 */
449
450 __private_extern__ void
451 _vm_object_allocate(
452 vm_object_size_t size,
453 vm_object_t object)
454 {
455 XPR(XPR_VM_OBJECT,
456 "vm_object_allocate, object 0x%X size 0x%X\n",
457 object, size, 0,0,0);
458
459 *object = vm_object_template;
460 queue_init(&object->memq);
461 queue_init(&object->msr_q);
462 #if UPL_DEBUG
463 queue_init(&object->uplq);
464 #endif /* UPL_DEBUG */
465 vm_object_lock_init(object);
466 object->vo_size = size;
467 }
468
469 __private_extern__ vm_object_t
470 vm_object_allocate(
471 vm_object_size_t size)
472 {
473 register vm_object_t object;
474
475 object = (vm_object_t) zalloc(vm_object_zone);
476
477 // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
478
479 if (object != VM_OBJECT_NULL)
480 _vm_object_allocate(size, object);
481
482 return object;
483 }
484
485
486 lck_grp_t vm_object_lck_grp;
487 lck_grp_t vm_object_cache_lck_grp;
488 lck_grp_attr_t vm_object_lck_grp_attr;
489 lck_attr_t vm_object_lck_attr;
490 lck_attr_t kernel_object_lck_attr;
491
492 /*
493 * vm_object_bootstrap:
494 *
495 * Initialize the VM objects module.
496 */
497 __private_extern__ void
498 vm_object_bootstrap(void)
499 {
500 register int i;
501
502 vm_object_zone = zinit((vm_size_t) sizeof(struct vm_object),
503 round_page(512*1024),
504 round_page(12*1024),
505 "vm objects");
506 zone_change(vm_object_zone, Z_CALLERACCT, FALSE); /* don't charge caller */
507 zone_change(vm_object_zone, Z_NOENCRYPT, TRUE);
508
509 vm_object_init_lck_grp();
510
511 queue_init(&vm_object_cached_list);
512
513 lck_mtx_init_ext(&vm_object_cached_lock_data,
514 &vm_object_cached_lock_data_ext,
515 &vm_object_cache_lck_grp,
516 &vm_object_lck_attr);
517
518 queue_init(&vm_object_reaper_queue);
519
520 for (i = 0; i < VM_OBJECT_HASH_LOCK_COUNT; i++) {
521 lck_mtx_init_ext(&vm_object_hashed_lock_data[i],
522 &vm_object_hashed_lock_data_ext[i],
523 &vm_object_lck_grp,
524 &vm_object_lck_attr);
525 }
526 lck_mtx_init_ext(&vm_object_reaper_lock_data,
527 &vm_object_reaper_lock_data_ext,
528 &vm_object_lck_grp,
529 &vm_object_lck_attr);
530
531 vm_object_hash_zone =
532 zinit((vm_size_t) sizeof (struct vm_object_hash_entry),
533 round_page(512*1024),
534 round_page(12*1024),
535 "vm object hash entries");
536 zone_change(vm_object_hash_zone, Z_CALLERACCT, FALSE);
537 zone_change(vm_object_hash_zone, Z_NOENCRYPT, TRUE);
538
539 for (i = 0; i < VM_OBJECT_HASH_COUNT; i++)
540 queue_init(&vm_object_hashtable[i]);
541
542
543 /*
544 * Fill in a template object, for quick initialization
545 */
546
547 /* memq; Lock; init after allocation */
548 vm_object_template.memq.prev = NULL;
549 vm_object_template.memq.next = NULL;
550 #if 0
551 /*
552 * We can't call vm_object_lock_init() here because that will
553 * allocate some memory and VM is not fully initialized yet.
554 * The lock will be initialized for each allocated object in
555 * _vm_object_allocate(), so we don't need to initialize it in
556 * the vm_object_template.
557 */
558 vm_object_lock_init(&vm_object_template);
559 #endif
560 vm_object_template.vo_size = 0;
561 vm_object_template.memq_hint = VM_PAGE_NULL;
562 vm_object_template.ref_count = 1;
563 #if TASK_SWAPPER
564 vm_object_template.res_count = 1;
565 #endif /* TASK_SWAPPER */
566 vm_object_template.resident_page_count = 0;
567 vm_object_template.wired_page_count = 0;
568 vm_object_template.reusable_page_count = 0;
569 vm_object_template.copy = VM_OBJECT_NULL;
570 vm_object_template.shadow = VM_OBJECT_NULL;
571 vm_object_template.vo_shadow_offset = (vm_object_offset_t) 0;
572 vm_object_template.pager = MEMORY_OBJECT_NULL;
573 vm_object_template.paging_offset = 0;
574 vm_object_template.pager_control = MEMORY_OBJECT_CONTROL_NULL;
575 vm_object_template.copy_strategy = MEMORY_OBJECT_COPY_SYMMETRIC;
576 vm_object_template.paging_in_progress = 0;
577 vm_object_template.activity_in_progress = 0;
578
579 /* Begin bitfields */
580 vm_object_template.all_wanted = 0; /* all bits FALSE */
581 vm_object_template.pager_created = FALSE;
582 vm_object_template.pager_initialized = FALSE;
583 vm_object_template.pager_ready = FALSE;
584 vm_object_template.pager_trusted = FALSE;
585 vm_object_template.can_persist = FALSE;
586 vm_object_template.internal = TRUE;
587 vm_object_template.temporary = TRUE;
588 vm_object_template.private = FALSE;
589 vm_object_template.pageout = FALSE;
590 vm_object_template.alive = TRUE;
591 vm_object_template.purgable = VM_PURGABLE_DENY;
592 vm_object_template.shadowed = FALSE;
593 vm_object_template.silent_overwrite = FALSE;
594 vm_object_template.advisory_pageout = FALSE;
595 vm_object_template.true_share = FALSE;
596 vm_object_template.terminating = FALSE;
597 vm_object_template.named = FALSE;
598 vm_object_template.shadow_severed = FALSE;
599 vm_object_template.phys_contiguous = FALSE;
600 vm_object_template.nophyscache = FALSE;
601 /* End bitfields */
602
603 vm_object_template.cached_list.prev = NULL;
604 vm_object_template.cached_list.next = NULL;
605 vm_object_template.msr_q.prev = NULL;
606 vm_object_template.msr_q.next = NULL;
607
608 vm_object_template.last_alloc = (vm_object_offset_t) 0;
609 vm_object_template.sequential = (vm_object_offset_t) 0;
610 vm_object_template.pages_created = 0;
611 vm_object_template.pages_used = 0;
612 vm_object_template.scan_collisions = 0;
613
614 #if MACH_PAGEMAP
615 vm_object_template.existence_map = VM_EXTERNAL_NULL;
616 #endif /* MACH_PAGEMAP */
617 vm_object_template.cow_hint = ~(vm_offset_t)0;
618 #if MACH_ASSERT
619 vm_object_template.paging_object = VM_OBJECT_NULL;
620 #endif /* MACH_ASSERT */
621
622 /* cache bitfields */
623 vm_object_template.wimg_bits = VM_WIMG_USE_DEFAULT;
624 vm_object_template.set_cache_attr = FALSE;
625 vm_object_template.code_signed = FALSE;
626 vm_object_template.hashed = FALSE;
627 vm_object_template.transposed = FALSE;
628 vm_object_template.mapping_in_progress = FALSE;
629 vm_object_template.volatile_empty = FALSE;
630 vm_object_template.volatile_fault = FALSE;
631 vm_object_template.all_reusable = FALSE;
632 vm_object_template.blocked_access = FALSE;
633 vm_object_template.__object2_unused_bits = 0;
634 #if UPL_DEBUG
635 vm_object_template.uplq.prev = NULL;
636 vm_object_template.uplq.next = NULL;
637 #endif /* UPL_DEBUG */
638 #ifdef VM_PIP_DEBUG
639 bzero(&vm_object_template.pip_holders,
640 sizeof (vm_object_template.pip_holders));
641 #endif /* VM_PIP_DEBUG */
642
643 vm_object_template.objq.next=NULL;
644 vm_object_template.objq.prev=NULL;
645
646 vm_object_template.vo_cache_ts = 0;
647
648 /*
649 * Initialize the "kernel object"
650 */
651
652 kernel_object = &kernel_object_store;
653
654 /*
655 * Note that in the following size specifications, we need to add 1 because
656 * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size.
657 */
658
659 #ifdef ppc
660 _vm_object_allocate(vm_last_addr + 1,
661 kernel_object);
662 #else
663 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS + 1,
664 kernel_object);
665 #endif
666 kernel_object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
667
668 /*
669 * Initialize the "submap object". Make it as large as the
670 * kernel object so that no limit is imposed on submap sizes.
671 */
672
673 vm_submap_object = &vm_submap_object_store;
674 #ifdef ppc
675 _vm_object_allocate(vm_last_addr + 1,
676 vm_submap_object);
677 #else
678 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS + 1,
679 vm_submap_object);
680 #endif
681 vm_submap_object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
682
683 /*
684 * Create an "extra" reference to this object so that we never
685 * try to deallocate it; zfree doesn't like to be called with
686 * non-zone memory.
687 */
688 vm_object_reference(vm_submap_object);
689
690 #if MACH_PAGEMAP
691 vm_external_module_initialize();
692 #endif /* MACH_PAGEMAP */
693 }
694
695 void
696 vm_object_reaper_init(void)
697 {
698 kern_return_t kr;
699 thread_t thread;
700
701 kr = kernel_thread_start_priority(
702 (thread_continue_t) vm_object_reaper_thread,
703 NULL,
704 BASEPRI_PREEMPT - 1,
705 &thread);
706 if (kr != KERN_SUCCESS) {
707 panic("failed to launch vm_object_reaper_thread kr=0x%x", kr);
708 }
709 thread_deallocate(thread);
710 }
711
712 __private_extern__ void
713 vm_object_init(void)
714 {
715 /*
716 * Finish initializing the kernel object.
717 */
718 }
719
720
721 __private_extern__ void
722 vm_object_init_lck_grp(void)
723 {
724 /*
725 * initialze the vm_object lock world
726 */
727 lck_grp_attr_setdefault(&vm_object_lck_grp_attr);
728 lck_grp_init(&vm_object_lck_grp, "vm_object", &vm_object_lck_grp_attr);
729 lck_grp_init(&vm_object_cache_lck_grp, "vm_object_cache", &vm_object_lck_grp_attr);
730 lck_attr_setdefault(&vm_object_lck_attr);
731 lck_attr_setdefault(&kernel_object_lck_attr);
732 lck_attr_cleardebug(&kernel_object_lck_attr);
733 }
734
735 #if VM_OBJECT_CACHE
736 #define MIGHT_NOT_CACHE_SHADOWS 1
737 #if MIGHT_NOT_CACHE_SHADOWS
738 static int cache_shadows = TRUE;
739 #endif /* MIGHT_NOT_CACHE_SHADOWS */
740 #endif
741
742 /*
743 * vm_object_deallocate:
744 *
745 * Release a reference to the specified object,
746 * gained either through a vm_object_allocate
747 * or a vm_object_reference call. When all references
748 * are gone, storage associated with this object
749 * may be relinquished.
750 *
751 * No object may be locked.
752 */
753 unsigned long vm_object_deallocate_shared_successes = 0;
754 unsigned long vm_object_deallocate_shared_failures = 0;
755 unsigned long vm_object_deallocate_shared_swap_failures = 0;
756 __private_extern__ void
757 vm_object_deallocate(
758 register vm_object_t object)
759 {
760 #if VM_OBJECT_CACHE
761 boolean_t retry_cache_trim = FALSE;
762 uint32_t try_failed_count = 0;
763 #endif
764 vm_object_t shadow = VM_OBJECT_NULL;
765
766 // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
767 // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
768
769 if (object == VM_OBJECT_NULL)
770 return;
771
772 if (object == kernel_object) {
773 vm_object_lock_shared(object);
774
775 OSAddAtomic(-1, &object->ref_count);
776
777 if (object->ref_count == 0) {
778 panic("vm_object_deallocate: losing kernel_object\n");
779 }
780 vm_object_unlock(object);
781 return;
782 }
783
784 if (object->ref_count > 2 ||
785 (!object->named && object->ref_count > 1)) {
786 UInt32 original_ref_count;
787 volatile UInt32 *ref_count_p;
788 Boolean atomic_swap;
789
790 /*
791 * The object currently looks like it is not being
792 * kept alive solely by the reference we're about to release.
793 * Let's try and release our reference without taking
794 * all the locks we would need if we had to terminate the
795 * object (cache lock + exclusive object lock).
796 * Lock the object "shared" to make sure we don't race with
797 * anyone holding it "exclusive".
798 */
799 vm_object_lock_shared(object);
800 ref_count_p = (volatile UInt32 *) &object->ref_count;
801 original_ref_count = object->ref_count;
802 /*
803 * Test again as "ref_count" could have changed.
804 * "named" shouldn't change.
805 */
806 if (original_ref_count > 2 ||
807 (!object->named && original_ref_count > 1)) {
808 atomic_swap = OSCompareAndSwap(
809 original_ref_count,
810 original_ref_count - 1,
811 (UInt32 *) &object->ref_count);
812 if (atomic_swap == FALSE) {
813 vm_object_deallocate_shared_swap_failures++;
814 }
815
816 } else {
817 atomic_swap = FALSE;
818 }
819 vm_object_unlock(object);
820
821 if (atomic_swap) {
822 /*
823 * ref_count was updated atomically !
824 */
825 vm_object_deallocate_shared_successes++;
826 return;
827 }
828
829 /*
830 * Someone else updated the ref_count at the same
831 * time and we lost the race. Fall back to the usual
832 * slow but safe path...
833 */
834 vm_object_deallocate_shared_failures++;
835 }
836
837 while (object != VM_OBJECT_NULL) {
838
839 vm_object_lock(object);
840
841 assert(object->ref_count > 0);
842
843 /*
844 * If the object has a named reference, and only
845 * that reference would remain, inform the pager
846 * about the last "mapping" reference going away.
847 */
848 if ((object->ref_count == 2) && (object->named)) {
849 memory_object_t pager = object->pager;
850
851 /* Notify the Pager that there are no */
852 /* more mappers for this object */
853
854 if (pager != MEMORY_OBJECT_NULL) {
855 vm_object_mapping_wait(object, THREAD_UNINT);
856 vm_object_mapping_begin(object);
857 vm_object_unlock(object);
858
859 memory_object_last_unmap(pager);
860
861 vm_object_lock(object);
862 vm_object_mapping_end(object);
863 }
864 assert(object->ref_count > 0);
865 }
866
867 /*
868 * Lose the reference. If other references
869 * remain, then we are done, unless we need
870 * to retry a cache trim.
871 * If it is the last reference, then keep it
872 * until any pending initialization is completed.
873 */
874
875 /* if the object is terminating, it cannot go into */
876 /* the cache and we obviously should not call */
877 /* terminate again. */
878
879 if ((object->ref_count > 1) || object->terminating) {
880 vm_object_lock_assert_exclusive(object);
881 object->ref_count--;
882 vm_object_res_deallocate(object);
883
884 if (object->ref_count == 1 &&
885 object->shadow != VM_OBJECT_NULL) {
886 /*
887 * There's only one reference left on this
888 * VM object. We can't tell if it's a valid
889 * one (from a mapping for example) or if this
890 * object is just part of a possibly stale and
891 * useless shadow chain.
892 * We would like to try and collapse it into
893 * its parent, but we don't have any pointers
894 * back to this parent object.
895 * But we can try and collapse this object with
896 * its own shadows, in case these are useless
897 * too...
898 * We can't bypass this object though, since we
899 * don't know if this last reference on it is
900 * meaningful or not.
901 */
902 vm_object_collapse(object, 0, FALSE);
903 }
904 vm_object_unlock(object);
905 #if VM_OBJECT_CACHE
906 if (retry_cache_trim &&
907 ((object = vm_object_cache_trim(TRUE)) !=
908 VM_OBJECT_NULL)) {
909 continue;
910 }
911 #endif
912 return;
913 }
914
915 /*
916 * We have to wait for initialization
917 * before destroying or caching the object.
918 */
919
920 if (object->pager_created && ! object->pager_initialized) {
921 assert(! object->can_persist);
922 vm_object_assert_wait(object,
923 VM_OBJECT_EVENT_INITIALIZED,
924 THREAD_UNINT);
925 vm_object_unlock(object);
926
927 thread_block(THREAD_CONTINUE_NULL);
928 continue;
929 }
930
931 #if VM_OBJECT_CACHE
932 /*
933 * If this object can persist, then enter it in
934 * the cache. Otherwise, terminate it.
935 *
936 * NOTE: Only permanent objects are cached, and
937 * permanent objects cannot have shadows. This
938 * affects the residence counting logic in a minor
939 * way (can do it in-line, mostly).
940 */
941
942 if ((object->can_persist) && (object->alive)) {
943 /*
944 * Now it is safe to decrement reference count,
945 * and to return if reference count is > 0.
946 */
947
948 vm_object_lock_assert_exclusive(object);
949 if (--object->ref_count > 0) {
950 vm_object_res_deallocate(object);
951 vm_object_unlock(object);
952
953 if (retry_cache_trim &&
954 ((object = vm_object_cache_trim(TRUE)) !=
955 VM_OBJECT_NULL)) {
956 continue;
957 }
958 return;
959 }
960
961 #if MIGHT_NOT_CACHE_SHADOWS
962 /*
963 * Remove shadow now if we don't
964 * want to cache shadows.
965 */
966 if (! cache_shadows) {
967 shadow = object->shadow;
968 object->shadow = VM_OBJECT_NULL;
969 }
970 #endif /* MIGHT_NOT_CACHE_SHADOWS */
971
972 /*
973 * Enter the object onto the queue of
974 * cached objects, and deactivate
975 * all of its pages.
976 */
977 assert(object->shadow == VM_OBJECT_NULL);
978 VM_OBJ_RES_DECR(object);
979 XPR(XPR_VM_OBJECT,
980 "vm_o_deallocate: adding %x to cache, queue = (%x, %x)\n",
981 object,
982 vm_object_cached_list.next,
983 vm_object_cached_list.prev,0,0);
984
985
986 vm_object_unlock(object);
987
988 try_failed_count = 0;
989 for (;;) {
990 vm_object_cache_lock();
991
992 /*
993 * if we try to take a regular lock here
994 * we risk deadlocking against someone
995 * holding a lock on this object while
996 * trying to vm_object_deallocate a different
997 * object
998 */
999 if (vm_object_lock_try(object))
1000 break;
1001 vm_object_cache_unlock();
1002 try_failed_count++;
1003
1004 mutex_pause(try_failed_count); /* wait a bit */
1005 }
1006 vm_object_cached_count++;
1007 if (vm_object_cached_count > vm_object_cached_high)
1008 vm_object_cached_high = vm_object_cached_count;
1009 queue_enter(&vm_object_cached_list, object,
1010 vm_object_t, cached_list);
1011 vm_object_cache_unlock();
1012
1013 vm_object_deactivate_all_pages(object);
1014 vm_object_unlock(object);
1015
1016 #if MIGHT_NOT_CACHE_SHADOWS
1017 /*
1018 * If we have a shadow that we need
1019 * to deallocate, do so now, remembering
1020 * to trim the cache later.
1021 */
1022 if (! cache_shadows && shadow != VM_OBJECT_NULL) {
1023 object = shadow;
1024 retry_cache_trim = TRUE;
1025 continue;
1026 }
1027 #endif /* MIGHT_NOT_CACHE_SHADOWS */
1028
1029 /*
1030 * Trim the cache. If the cache trim
1031 * returns with a shadow for us to deallocate,
1032 * then remember to retry the cache trim
1033 * when we are done deallocating the shadow.
1034 * Otherwise, we are done.
1035 */
1036
1037 object = vm_object_cache_trim(TRUE);
1038 if (object == VM_OBJECT_NULL) {
1039 return;
1040 }
1041 retry_cache_trim = TRUE;
1042 } else
1043 #endif /* VM_OBJECT_CACHE */
1044 {
1045 /*
1046 * This object is not cachable; terminate it.
1047 */
1048 XPR(XPR_VM_OBJECT,
1049 "vm_o_deallocate: !cacheable 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
1050 object, object->resident_page_count,
1051 object->paging_in_progress,
1052 (void *)current_thread(),object->ref_count);
1053
1054 VM_OBJ_RES_DECR(object); /* XXX ? */
1055 /*
1056 * Terminate this object. If it had a shadow,
1057 * then deallocate it; otherwise, if we need
1058 * to retry a cache trim, do so now; otherwise,
1059 * we are done. "pageout" objects have a shadow,
1060 * but maintain a "paging reference" rather than
1061 * a normal reference.
1062 */
1063 shadow = object->pageout?VM_OBJECT_NULL:object->shadow;
1064
1065 if (vm_object_terminate(object) != KERN_SUCCESS) {
1066 return;
1067 }
1068 if (shadow != VM_OBJECT_NULL) {
1069 object = shadow;
1070 continue;
1071 }
1072 #if VM_OBJECT_CACHE
1073 if (retry_cache_trim &&
1074 ((object = vm_object_cache_trim(TRUE)) !=
1075 VM_OBJECT_NULL)) {
1076 continue;
1077 }
1078 #endif
1079 return;
1080 }
1081 }
1082 #if VM_OBJECT_CACHE
1083 assert(! retry_cache_trim);
1084 #endif
1085 }
1086
1087
1088
1089 vm_page_t
1090 vm_object_page_grab(
1091 vm_object_t object)
1092 {
1093 vm_page_t p, next_p;
1094 int p_limit = 0;
1095 int p_skipped = 0;
1096
1097 vm_object_lock_assert_exclusive(object);
1098
1099 next_p = (vm_page_t)queue_first(&object->memq);
1100 p_limit = MIN(50, object->resident_page_count);
1101
1102 while (!queue_end(&object->memq, (queue_entry_t)next_p) && --p_limit > 0) {
1103
1104 p = next_p;
1105 next_p = (vm_page_t)queue_next(&next_p->listq);
1106
1107 if (VM_PAGE_WIRED(p) || p->busy || p->cleaning || p->fictitious)
1108 goto move_page_in_obj;
1109
1110 if (p->pmapped || p->dirty || p->precious) {
1111 vm_page_lockspin_queues();
1112
1113 if (p->pmapped) {
1114 int refmod_state;
1115
1116 vm_object_page_grab_pmapped++;
1117
1118 if (p->reference == FALSE || p->dirty == FALSE) {
1119
1120 refmod_state = pmap_get_refmod(p->phys_page);
1121
1122 if (refmod_state & VM_MEM_REFERENCED)
1123 p->reference = TRUE;
1124 if (refmod_state & VM_MEM_MODIFIED)
1125 p->dirty = TRUE;
1126 }
1127 if (p->dirty == FALSE && p->precious == FALSE) {
1128
1129 refmod_state = pmap_disconnect(p->phys_page);
1130
1131 if (refmod_state & VM_MEM_REFERENCED)
1132 p->reference = TRUE;
1133 if (refmod_state & VM_MEM_MODIFIED)
1134 p->dirty = TRUE;
1135
1136 if (p->dirty == FALSE)
1137 goto take_page;
1138 }
1139 }
1140 if (p->inactive && p->reference == TRUE) {
1141 vm_page_activate(p);
1142
1143 VM_STAT_INCR(reactivations);
1144 vm_object_page_grab_reactivations++;
1145 }
1146 vm_page_unlock_queues();
1147 move_page_in_obj:
1148 queue_remove(&object->memq, p, vm_page_t, listq);
1149 queue_enter(&object->memq, p, vm_page_t, listq);
1150
1151 p_skipped++;
1152 continue;
1153 }
1154 vm_page_lockspin_queues();
1155 take_page:
1156 vm_page_free_prepare_queues(p);
1157 vm_object_page_grab_returned++;
1158 vm_object_page_grab_skipped += p_skipped;
1159
1160 vm_page_unlock_queues();
1161
1162 vm_page_free_prepare_object(p, TRUE);
1163
1164 return (p);
1165 }
1166 vm_object_page_grab_skipped += p_skipped;
1167 vm_object_page_grab_failed++;
1168
1169 return (NULL);
1170 }
1171
1172
1173
1174 #define EVICT_PREPARE_LIMIT 64
1175 #define EVICT_AGE 10
1176
1177 static clock_sec_t vm_object_cache_aging_ts = 0;
1178
1179 static void
1180 vm_object_cache_remove_locked(
1181 vm_object_t object)
1182 {
1183 queue_remove(&vm_object_cached_list, object, vm_object_t, objq);
1184 object->objq.next = NULL;
1185 object->objq.prev = NULL;
1186
1187 vm_object_cached_count--;
1188 }
1189
1190 void
1191 vm_object_cache_remove(
1192 vm_object_t object)
1193 {
1194 vm_object_cache_lock_spin();
1195
1196 if (object->objq.next || object->objq.prev)
1197 vm_object_cache_remove_locked(object);
1198
1199 vm_object_cache_unlock();
1200 }
1201
1202 void
1203 vm_object_cache_add(
1204 vm_object_t object)
1205 {
1206 clock_sec_t sec;
1207 clock_nsec_t nsec;
1208
1209 if (object->resident_page_count == 0)
1210 return;
1211 clock_get_system_nanotime(&sec, &nsec);
1212
1213 vm_object_cache_lock_spin();
1214
1215 if (object->objq.next == NULL && object->objq.prev == NULL) {
1216 queue_enter(&vm_object_cached_list, object, vm_object_t, objq);
1217 object->vo_cache_ts = sec + EVICT_AGE;
1218 object->vo_cache_pages_to_scan = object->resident_page_count;
1219
1220 vm_object_cached_count++;
1221 vm_object_cache_adds++;
1222 }
1223 vm_object_cache_unlock();
1224 }
1225
1226 int
1227 vm_object_cache_evict(
1228 int num_to_evict,
1229 int max_objects_to_examine)
1230 {
1231 vm_object_t object = VM_OBJECT_NULL;
1232 vm_object_t next_obj = VM_OBJECT_NULL;
1233 vm_page_t local_free_q = VM_PAGE_NULL;
1234 vm_page_t p;
1235 vm_page_t next_p;
1236 int object_cnt = 0;
1237 vm_page_t ep_array[EVICT_PREPARE_LIMIT];
1238 int ep_count;
1239 int ep_limit;
1240 int ep_index;
1241 int ep_freed = 0;
1242 int ep_moved = 0;
1243 uint32_t ep_skipped = 0;
1244 clock_sec_t sec;
1245 clock_nsec_t nsec;
1246
1247 KERNEL_DEBUG(0x13001ec | DBG_FUNC_START, 0, 0, 0, 0, 0);
1248 /*
1249 * do a couple of quick checks to see if it's
1250 * worthwhile grabbing the lock
1251 */
1252 if (queue_empty(&vm_object_cached_list)) {
1253 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END, 0, 0, 0, 0, 0);
1254 return (0);
1255 }
1256 clock_get_system_nanotime(&sec, &nsec);
1257
1258 /*
1259 * the object on the head of the queue has not
1260 * yet sufficiently aged
1261 */
1262 if (sec < vm_object_cache_aging_ts) {
1263 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END, 0, 0, 0, 0, 0);
1264 return (0);
1265 }
1266 /*
1267 * don't need the queue lock to find
1268 * and lock an object on the cached list
1269 */
1270 vm_page_unlock_queues();
1271
1272 vm_object_cache_lock_spin();
1273
1274 for (;;) {
1275 next_obj = (vm_object_t)queue_first(&vm_object_cached_list);
1276
1277 while (!queue_end(&vm_object_cached_list, (queue_entry_t)next_obj) && object_cnt++ < max_objects_to_examine) {
1278
1279 object = next_obj;
1280 next_obj = (vm_object_t)queue_next(&next_obj->objq);
1281
1282 if (sec < object->vo_cache_ts) {
1283 KERNEL_DEBUG(0x130020c, object, object->resident_page_count, object->vo_cache_ts, sec, 0);
1284
1285 vm_object_cache_aging_ts = object->vo_cache_ts;
1286 object = VM_OBJECT_NULL;
1287 break;
1288 }
1289 if (!vm_object_lock_try_scan(object)) {
1290 /*
1291 * just skip over this guy for now... if we find
1292 * an object to steal pages from, we'll revist in a bit...
1293 * hopefully, the lock will have cleared
1294 */
1295 KERNEL_DEBUG(0x13001f8, object, object->resident_page_count, 0, 0, 0);
1296
1297 object = VM_OBJECT_NULL;
1298 continue;
1299 }
1300 if (queue_empty(&object->memq) || object->vo_cache_pages_to_scan == 0) {
1301 /*
1302 * this case really shouldn't happen, but it's not fatal
1303 * so deal with it... if we don't remove the object from
1304 * the list, we'll never move past it.
1305 */
1306 KERNEL_DEBUG(0x13001fc, object, object->resident_page_count, ep_freed, ep_moved, 0);
1307
1308 vm_object_cache_remove_locked(object);
1309 vm_object_unlock(object);
1310 object = VM_OBJECT_NULL;
1311 continue;
1312 }
1313 /*
1314 * we have a locked object with pages...
1315 * time to start harvesting
1316 */
1317 break;
1318 }
1319 vm_object_cache_unlock();
1320
1321 if (object == VM_OBJECT_NULL)
1322 break;
1323
1324 /*
1325 * object is locked at this point and
1326 * has resident pages
1327 */
1328 next_p = (vm_page_t)queue_first(&object->memq);
1329
1330 /*
1331 * break the page scan into 2 pieces to minimize the time spent
1332 * behind the page queue lock...
1333 * the list of pages on these unused objects is likely to be cold
1334 * w/r to the cpu cache which increases the time to scan the list
1335 * tenfold... and we may have a 'run' of pages we can't utilize that
1336 * needs to be skipped over...
1337 */
1338 if ((ep_limit = num_to_evict - (ep_freed + ep_moved)) > EVICT_PREPARE_LIMIT)
1339 ep_limit = EVICT_PREPARE_LIMIT;
1340 ep_count = 0;
1341
1342 while (!queue_end(&object->memq, (queue_entry_t)next_p) && object->vo_cache_pages_to_scan && ep_count < ep_limit) {
1343
1344 p = next_p;
1345 next_p = (vm_page_t)queue_next(&next_p->listq);
1346
1347 object->vo_cache_pages_to_scan--;
1348
1349 if (VM_PAGE_WIRED(p) || p->busy || p->cleaning) {
1350 queue_remove(&object->memq, p, vm_page_t, listq);
1351 queue_enter(&object->memq, p, vm_page_t, listq);
1352
1353 ep_skipped++;
1354 continue;
1355 }
1356 if (p->wpmapped || p->dirty || p->precious) {
1357 queue_remove(&object->memq, p, vm_page_t, listq);
1358 queue_enter(&object->memq, p, vm_page_t, listq);
1359
1360 pmap_clear_reference(p->phys_page);
1361 }
1362 ep_array[ep_count++] = p;
1363 }
1364 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_START, object, object->resident_page_count, ep_freed, ep_moved, 0);
1365
1366 vm_page_lockspin_queues();
1367
1368 for (ep_index = 0; ep_index < ep_count; ep_index++) {
1369
1370 p = ep_array[ep_index];
1371
1372 if (p->wpmapped || p->dirty || p->precious) {
1373 p->reference = FALSE;
1374 p->no_cache = FALSE;
1375
1376 VM_PAGE_QUEUES_REMOVE(p);
1377 VM_PAGE_ENQUEUE_INACTIVE(p, TRUE);
1378
1379 ep_moved++;
1380 } else {
1381 vm_page_free_prepare_queues(p);
1382
1383 assert(p->pageq.next == NULL && p->pageq.prev == NULL);
1384 /*
1385 * Add this page to our list of reclaimed pages,
1386 * to be freed later.
1387 */
1388 p->pageq.next = (queue_entry_t) local_free_q;
1389 local_free_q = p;
1390
1391 ep_freed++;
1392 }
1393 }
1394 vm_page_unlock_queues();
1395
1396 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_END, object, object->resident_page_count, ep_freed, ep_moved, 0);
1397
1398 if (local_free_q) {
1399 vm_page_free_list(local_free_q, TRUE);
1400 local_free_q = VM_PAGE_NULL;
1401 }
1402 if (object->vo_cache_pages_to_scan == 0) {
1403 KERNEL_DEBUG(0x1300208, object, object->resident_page_count, ep_freed, ep_moved, 0);
1404
1405 vm_object_cache_remove(object);
1406
1407 KERNEL_DEBUG(0x13001fc, object, object->resident_page_count, ep_freed, ep_moved, 0);
1408 }
1409 /*
1410 * done with this object
1411 */
1412 vm_object_unlock(object);
1413 object = VM_OBJECT_NULL;
1414
1415 /*
1416 * at this point, we are not holding any locks
1417 */
1418 if ((ep_freed + ep_moved) >= num_to_evict) {
1419 /*
1420 * we've reached our target for the
1421 * number of pages to evict
1422 */
1423 break;
1424 }
1425 vm_object_cache_lock_spin();
1426 }
1427 /*
1428 * put the page queues lock back to the caller's
1429 * idea of it
1430 */
1431 vm_page_lock_queues();
1432
1433 vm_object_cache_pages_freed += ep_freed;
1434 vm_object_cache_pages_moved += ep_moved;
1435 vm_object_cache_pages_skipped += ep_skipped;
1436
1437 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END, ep_freed, 0, 0, 0, 0);
1438 return (ep_freed);
1439 }
1440
1441
1442 #if VM_OBJECT_CACHE
1443 /*
1444 * Check to see whether we really need to trim
1445 * down the cache. If so, remove an object from
1446 * the cache, terminate it, and repeat.
1447 *
1448 * Called with, and returns with, cache lock unlocked.
1449 */
1450 vm_object_t
1451 vm_object_cache_trim(
1452 boolean_t called_from_vm_object_deallocate)
1453 {
1454 register vm_object_t object = VM_OBJECT_NULL;
1455 vm_object_t shadow;
1456
1457 for (;;) {
1458
1459 /*
1460 * If we no longer need to trim the cache,
1461 * then we are done.
1462 */
1463 if (vm_object_cached_count <= vm_object_cached_max)
1464 return VM_OBJECT_NULL;
1465
1466 vm_object_cache_lock();
1467 if (vm_object_cached_count <= vm_object_cached_max) {
1468 vm_object_cache_unlock();
1469 return VM_OBJECT_NULL;
1470 }
1471
1472 /*
1473 * We must trim down the cache, so remove
1474 * the first object in the cache.
1475 */
1476 XPR(XPR_VM_OBJECT,
1477 "vm_object_cache_trim: removing from front of cache (%x, %x)\n",
1478 vm_object_cached_list.next,
1479 vm_object_cached_list.prev, 0, 0, 0);
1480
1481 object = (vm_object_t) queue_first(&vm_object_cached_list);
1482 if(object == (vm_object_t) &vm_object_cached_list) {
1483 /* something's wrong with the calling parameter or */
1484 /* the value of vm_object_cached_count, just fix */
1485 /* and return */
1486 if(vm_object_cached_max < 0)
1487 vm_object_cached_max = 0;
1488 vm_object_cached_count = 0;
1489 vm_object_cache_unlock();
1490 return VM_OBJECT_NULL;
1491 }
1492 vm_object_lock(object);
1493 queue_remove(&vm_object_cached_list, object, vm_object_t,
1494 cached_list);
1495 vm_object_cached_count--;
1496
1497 vm_object_cache_unlock();
1498 /*
1499 * Since this object is in the cache, we know
1500 * that it is initialized and has no references.
1501 * Take a reference to avoid recursive deallocations.
1502 */
1503
1504 assert(object->pager_initialized);
1505 assert(object->ref_count == 0);
1506 vm_object_lock_assert_exclusive(object);
1507 object->ref_count++;
1508
1509 /*
1510 * Terminate the object.
1511 * If the object had a shadow, we let vm_object_deallocate
1512 * deallocate it. "pageout" objects have a shadow, but
1513 * maintain a "paging reference" rather than a normal
1514 * reference.
1515 * (We are careful here to limit recursion.)
1516 */
1517 shadow = object->pageout?VM_OBJECT_NULL:object->shadow;
1518
1519 if(vm_object_terminate(object) != KERN_SUCCESS)
1520 continue;
1521
1522 if (shadow != VM_OBJECT_NULL) {
1523 if (called_from_vm_object_deallocate) {
1524 return shadow;
1525 } else {
1526 vm_object_deallocate(shadow);
1527 }
1528 }
1529 }
1530 }
1531 #endif
1532
1533
1534 /*
1535 * Routine: vm_object_terminate
1536 * Purpose:
1537 * Free all resources associated with a vm_object.
1538 * In/out conditions:
1539 * Upon entry, the object must be locked,
1540 * and the object must have exactly one reference.
1541 *
1542 * The shadow object reference is left alone.
1543 *
1544 * The object must be unlocked if its found that pages
1545 * must be flushed to a backing object. If someone
1546 * manages to map the object while it is being flushed
1547 * the object is returned unlocked and unchanged. Otherwise,
1548 * upon exit, the cache will be unlocked, and the
1549 * object will cease to exist.
1550 */
1551 static kern_return_t
1552 vm_object_terminate(
1553 vm_object_t object)
1554 {
1555 vm_object_t shadow_object;
1556
1557 XPR(XPR_VM_OBJECT, "vm_object_terminate, object 0x%X ref %d\n",
1558 object, object->ref_count, 0, 0, 0);
1559
1560 if (!object->pageout && (!object->temporary || object->can_persist) &&
1561 (object->pager != NULL || object->shadow_severed)) {
1562 /*
1563 * Clear pager_trusted bit so that the pages get yanked
1564 * out of the object instead of cleaned in place. This
1565 * prevents a deadlock in XMM and makes more sense anyway.
1566 */
1567 object->pager_trusted = FALSE;
1568
1569 vm_object_reap_pages(object, REAP_TERMINATE);
1570 }
1571 /*
1572 * Make sure the object isn't already being terminated
1573 */
1574 if (object->terminating) {
1575 vm_object_lock_assert_exclusive(object);
1576 object->ref_count--;
1577 assert(object->ref_count > 0);
1578 vm_object_unlock(object);
1579 return KERN_FAILURE;
1580 }
1581
1582 /*
1583 * Did somebody get a reference to the object while we were
1584 * cleaning it?
1585 */
1586 if (object->ref_count != 1) {
1587 vm_object_lock_assert_exclusive(object);
1588 object->ref_count--;
1589 assert(object->ref_count > 0);
1590 vm_object_res_deallocate(object);
1591 vm_object_unlock(object);
1592 return KERN_FAILURE;
1593 }
1594
1595 /*
1596 * Make sure no one can look us up now.
1597 */
1598
1599 object->terminating = TRUE;
1600 object->alive = FALSE;
1601
1602 if ( !object->internal && (object->objq.next || object->objq.prev))
1603 vm_object_cache_remove(object);
1604
1605 if (object->hashed) {
1606 lck_mtx_t *lck;
1607
1608 lck = vm_object_hash_lock_spin(object->pager);
1609 vm_object_remove(object);
1610 vm_object_hash_unlock(lck);
1611 }
1612 /*
1613 * Detach the object from its shadow if we are the shadow's
1614 * copy. The reference we hold on the shadow must be dropped
1615 * by our caller.
1616 */
1617 if (((shadow_object = object->shadow) != VM_OBJECT_NULL) &&
1618 !(object->pageout)) {
1619 vm_object_lock(shadow_object);
1620 if (shadow_object->copy == object)
1621 shadow_object->copy = VM_OBJECT_NULL;
1622 vm_object_unlock(shadow_object);
1623 }
1624
1625 if (object->paging_in_progress != 0 ||
1626 object->activity_in_progress != 0) {
1627 /*
1628 * There are still some paging_in_progress references
1629 * on this object, meaning that there are some paging
1630 * or other I/O operations in progress for this VM object.
1631 * Such operations take some paging_in_progress references
1632 * up front to ensure that the object doesn't go away, but
1633 * they may also need to acquire a reference on the VM object,
1634 * to map it in kernel space, for example. That means that
1635 * they may end up releasing the last reference on the VM
1636 * object, triggering its termination, while still holding
1637 * paging_in_progress references. Waiting for these
1638 * pending paging_in_progress references to go away here would
1639 * deadlock.
1640 *
1641 * To avoid deadlocking, we'll let the vm_object_reaper_thread
1642 * complete the VM object termination if it still holds
1643 * paging_in_progress references at this point.
1644 *
1645 * No new paging_in_progress should appear now that the
1646 * VM object is "terminating" and not "alive".
1647 */
1648 vm_object_reap_async(object);
1649 vm_object_unlock(object);
1650 /*
1651 * Return KERN_FAILURE to let the caller know that we
1652 * haven't completed the termination and it can't drop this
1653 * object's reference on its shadow object yet.
1654 * The reaper thread will take care of that once it has
1655 * completed this object's termination.
1656 */
1657 return KERN_FAILURE;
1658 }
1659 /*
1660 * complete the VM object termination
1661 */
1662 vm_object_reap(object);
1663 object = VM_OBJECT_NULL;
1664
1665 /*
1666 * the object lock was released by vm_object_reap()
1667 *
1668 * KERN_SUCCESS means that this object has been terminated
1669 * and no longer needs its shadow object but still holds a
1670 * reference on it.
1671 * The caller is responsible for dropping that reference.
1672 * We can't call vm_object_deallocate() here because that
1673 * would create a recursion.
1674 */
1675 return KERN_SUCCESS;
1676 }
1677
1678
1679 /*
1680 * vm_object_reap():
1681 *
1682 * Complete the termination of a VM object after it's been marked
1683 * as "terminating" and "!alive" by vm_object_terminate().
1684 *
1685 * The VM object must be locked by caller.
1686 * The lock will be released on return and the VM object is no longer valid.
1687 */
1688 void
1689 vm_object_reap(
1690 vm_object_t object)
1691 {
1692 memory_object_t pager;
1693
1694 vm_object_lock_assert_exclusive(object);
1695 assert(object->paging_in_progress == 0);
1696 assert(object->activity_in_progress == 0);
1697
1698 vm_object_reap_count++;
1699
1700 pager = object->pager;
1701 object->pager = MEMORY_OBJECT_NULL;
1702
1703 if (pager != MEMORY_OBJECT_NULL)
1704 memory_object_control_disable(object->pager_control);
1705
1706 object->ref_count--;
1707 #if TASK_SWAPPER
1708 assert(object->res_count == 0);
1709 #endif /* TASK_SWAPPER */
1710
1711 assert (object->ref_count == 0);
1712
1713 /*
1714 * remove from purgeable queue if it's on
1715 */
1716 if (object->internal && (object->objq.next || object->objq.prev)) {
1717 purgeable_q_t queue = vm_purgeable_object_remove(object);
1718 assert(queue);
1719
1720 /* Must take page lock for this - using it to protect token queue */
1721 vm_page_lock_queues();
1722 vm_purgeable_token_delete_first(queue);
1723
1724 assert(queue->debug_count_objects>=0);
1725 vm_page_unlock_queues();
1726 }
1727
1728 /*
1729 * Clean or free the pages, as appropriate.
1730 * It is possible for us to find busy/absent pages,
1731 * if some faults on this object were aborted.
1732 */
1733 if (object->pageout) {
1734 assert(object->shadow != VM_OBJECT_NULL);
1735
1736 vm_pageout_object_terminate(object);
1737
1738 } else if (((object->temporary && !object->can_persist) || (pager == MEMORY_OBJECT_NULL))) {
1739
1740 vm_object_reap_pages(object, REAP_REAP);
1741 }
1742 assert(queue_empty(&object->memq));
1743 assert(object->paging_in_progress == 0);
1744 assert(object->activity_in_progress == 0);
1745 assert(object->ref_count == 0);
1746
1747 /*
1748 * If the pager has not already been released by
1749 * vm_object_destroy, we need to terminate it and
1750 * release our reference to it here.
1751 */
1752 if (pager != MEMORY_OBJECT_NULL) {
1753 vm_object_unlock(object);
1754 vm_object_release_pager(pager, object->hashed);
1755 vm_object_lock(object);
1756 }
1757
1758 /* kick off anyone waiting on terminating */
1759 object->terminating = FALSE;
1760 vm_object_paging_begin(object);
1761 vm_object_paging_end(object);
1762 vm_object_unlock(object);
1763
1764 #if MACH_PAGEMAP
1765 vm_external_destroy(object->existence_map, object->vo_size);
1766 #endif /* MACH_PAGEMAP */
1767
1768 object->shadow = VM_OBJECT_NULL;
1769
1770 vm_object_lock_destroy(object);
1771 /*
1772 * Free the space for the object.
1773 */
1774 zfree(vm_object_zone, object);
1775 object = VM_OBJECT_NULL;
1776 }
1777
1778
1779 unsigned int vm_max_batch = 256;
1780
1781 #define V_O_R_MAX_BATCH 128
1782
1783 #define BATCH_LIMIT(max) (vm_max_batch >= max ? max : vm_max_batch)
1784
1785
1786 #define VM_OBJ_REAP_FREELIST(_local_free_q, do_disconnect) \
1787 MACRO_BEGIN \
1788 if (_local_free_q) { \
1789 if (do_disconnect) { \
1790 vm_page_t m; \
1791 for (m = _local_free_q; \
1792 m != VM_PAGE_NULL; \
1793 m = (vm_page_t) m->pageq.next) { \
1794 if (m->pmapped) { \
1795 pmap_disconnect(m->phys_page); \
1796 } \
1797 } \
1798 } \
1799 vm_page_free_list(_local_free_q, TRUE); \
1800 _local_free_q = VM_PAGE_NULL; \
1801 } \
1802 MACRO_END
1803
1804
1805 void
1806 vm_object_reap_pages(
1807 vm_object_t object,
1808 int reap_type)
1809 {
1810 vm_page_t p;
1811 vm_page_t next;
1812 vm_page_t local_free_q = VM_PAGE_NULL;
1813 int loop_count;
1814 boolean_t disconnect_on_release;
1815
1816 if (reap_type == REAP_DATA_FLUSH) {
1817 /*
1818 * We need to disconnect pages from all pmaps before
1819 * releasing them to the free list
1820 */
1821 disconnect_on_release = TRUE;
1822 } else {
1823 /*
1824 * Either the caller has already disconnected the pages
1825 * from all pmaps, or we disconnect them here as we add
1826 * them to out local list of pages to be released.
1827 * No need to re-disconnect them when we release the pages
1828 * to the free list.
1829 */
1830 disconnect_on_release = FALSE;
1831 }
1832
1833 restart_after_sleep:
1834 if (queue_empty(&object->memq))
1835 return;
1836 loop_count = BATCH_LIMIT(V_O_R_MAX_BATCH) + 1;
1837
1838 vm_page_lockspin_queues();
1839
1840 next = (vm_page_t)queue_first(&object->memq);
1841
1842 while (!queue_end(&object->memq, (queue_entry_t)next)) {
1843
1844 p = next;
1845 next = (vm_page_t)queue_next(&next->listq);
1846
1847 if (--loop_count == 0) {
1848
1849 vm_page_unlock_queues();
1850
1851 if (local_free_q) {
1852 /*
1853 * Free the pages we reclaimed so far
1854 * and take a little break to avoid
1855 * hogging the page queue lock too long
1856 */
1857 VM_OBJ_REAP_FREELIST(local_free_q,
1858 disconnect_on_release);
1859 } else
1860 mutex_pause(0);
1861
1862 loop_count = BATCH_LIMIT(V_O_R_MAX_BATCH) + 1;
1863
1864 vm_page_lockspin_queues();
1865 }
1866 if (reap_type == REAP_DATA_FLUSH || reap_type == REAP_TERMINATE) {
1867
1868 if (reap_type == REAP_DATA_FLUSH &&
1869 ((p->pageout == TRUE || p->cleaning == TRUE) && p->list_req_pending == TRUE)) {
1870 p->list_req_pending = FALSE;
1871 p->cleaning = FALSE;
1872 /*
1873 * need to drop the laundry count...
1874 * we may also need to remove it
1875 * from the I/O paging queue...
1876 * vm_pageout_throttle_up handles both cases
1877 *
1878 * the laundry and pageout_queue flags are cleared...
1879 */
1880 vm_pageout_throttle_up(p);
1881
1882 if (p->pageout == TRUE) {
1883 /*
1884 * toss the wire count we picked up
1885 * when we initially set this page up
1886 * to be cleaned and stolen...
1887 */
1888 vm_page_unwire(p, TRUE);
1889 p->pageout = FALSE;
1890 }
1891 PAGE_WAKEUP(p);
1892
1893 } else if (p->busy || p->cleaning) {
1894
1895 vm_page_unlock_queues();
1896 /*
1897 * free the pages reclaimed so far
1898 */
1899 VM_OBJ_REAP_FREELIST(local_free_q,
1900 disconnect_on_release);
1901
1902 PAGE_SLEEP(object, p, THREAD_UNINT);
1903
1904 goto restart_after_sleep;
1905 }
1906 }
1907 switch (reap_type) {
1908
1909 case REAP_DATA_FLUSH:
1910 if (VM_PAGE_WIRED(p)) {
1911 /*
1912 * this is an odd case... perhaps we should
1913 * zero-fill this page since we're conceptually
1914 * tossing its data at this point, but leaving
1915 * it on the object to honor the 'wire' contract
1916 */
1917 continue;
1918 }
1919 break;
1920
1921 case REAP_PURGEABLE:
1922 if (VM_PAGE_WIRED(p)) {
1923 /* can't purge a wired page */
1924 vm_page_purged_wired++;
1925 continue;
1926 }
1927
1928 if (p->busy) {
1929 /*
1930 * We can't reclaim a busy page but we can
1931 * make it pageable (it's not wired) to make
1932 * sure that it gets considered by
1933 * vm_pageout_scan() later.
1934 */
1935 vm_page_deactivate(p);
1936 vm_page_purged_busy++;
1937 continue;
1938 }
1939
1940 if (p->cleaning || p->laundry || p->list_req_pending) {
1941 /*
1942 * page is being acted upon,
1943 * so don't mess with it
1944 */
1945 vm_page_purged_others++;
1946 continue;
1947 }
1948 assert(p->object != kernel_object);
1949
1950 /*
1951 * we can discard this page...
1952 */
1953 if (p->pmapped == TRUE) {
1954 int refmod_state;
1955 /*
1956 * unmap the page
1957 */
1958 refmod_state = pmap_disconnect(p->phys_page);
1959 if (refmod_state & VM_MEM_MODIFIED) {
1960 p->dirty = TRUE;
1961 }
1962 }
1963 if (p->dirty || p->precious) {
1964 /*
1965 * we saved the cost of cleaning this page !
1966 */
1967 vm_page_purged_count++;
1968 }
1969
1970 break;
1971
1972 case REAP_TERMINATE:
1973 if (p->absent || p->private) {
1974 /*
1975 * For private pages, VM_PAGE_FREE just
1976 * leaves the page structure around for
1977 * its owner to clean up. For absent
1978 * pages, the structure is returned to
1979 * the appropriate pool.
1980 */
1981 break;
1982 }
1983 if (p->fictitious) {
1984 assert (p->phys_page == vm_page_guard_addr);
1985 break;
1986 }
1987 if (!p->dirty && p->wpmapped)
1988 p->dirty = pmap_is_modified(p->phys_page);
1989
1990 if ((p->dirty || p->precious) && !p->error && object->alive) {
1991
1992 p->busy = TRUE;
1993
1994 VM_PAGE_QUEUES_REMOVE(p);
1995 /*
1996 * flush page... page will be freed
1997 * upon completion of I/O
1998 */
1999 vm_pageout_cluster(p);
2000
2001 vm_page_unlock_queues();
2002 /*
2003 * free the pages reclaimed so far
2004 */
2005 VM_OBJ_REAP_FREELIST(local_free_q,
2006 disconnect_on_release);
2007
2008 vm_object_paging_wait(object, THREAD_UNINT);
2009
2010 goto restart_after_sleep;
2011 }
2012 break;
2013
2014 case REAP_REAP:
2015 break;
2016 }
2017 vm_page_free_prepare_queues(p);
2018 assert(p->pageq.next == NULL && p->pageq.prev == NULL);
2019 /*
2020 * Add this page to our list of reclaimed pages,
2021 * to be freed later.
2022 */
2023 p->pageq.next = (queue_entry_t) local_free_q;
2024 local_free_q = p;
2025 }
2026 vm_page_unlock_queues();
2027
2028 /*
2029 * Free the remaining reclaimed pages
2030 */
2031 VM_OBJ_REAP_FREELIST(local_free_q,
2032 disconnect_on_release);
2033 }
2034
2035
2036 void
2037 vm_object_reap_async(
2038 vm_object_t object)
2039 {
2040 vm_object_lock_assert_exclusive(object);
2041
2042 vm_object_reaper_lock_spin();
2043
2044 vm_object_reap_count_async++;
2045
2046 /* enqueue the VM object... */
2047 queue_enter(&vm_object_reaper_queue, object,
2048 vm_object_t, cached_list);
2049
2050 vm_object_reaper_unlock();
2051
2052 /* ... and wake up the reaper thread */
2053 thread_wakeup((event_t) &vm_object_reaper_queue);
2054 }
2055
2056
2057 void
2058 vm_object_reaper_thread(void)
2059 {
2060 vm_object_t object, shadow_object;
2061
2062 vm_object_reaper_lock_spin();
2063
2064 while (!queue_empty(&vm_object_reaper_queue)) {
2065 queue_remove_first(&vm_object_reaper_queue,
2066 object,
2067 vm_object_t,
2068 cached_list);
2069
2070 vm_object_reaper_unlock();
2071 vm_object_lock(object);
2072
2073 assert(object->terminating);
2074 assert(!object->alive);
2075
2076 /*
2077 * The pageout daemon might be playing with our pages.
2078 * Now that the object is dead, it won't touch any more
2079 * pages, but some pages might already be on their way out.
2080 * Hence, we wait until the active paging activities have
2081 * ceased before we break the association with the pager
2082 * itself.
2083 */
2084 while (object->paging_in_progress != 0 ||
2085 object->activity_in_progress != 0) {
2086 vm_object_wait(object,
2087 VM_OBJECT_EVENT_PAGING_IN_PROGRESS,
2088 THREAD_UNINT);
2089 vm_object_lock(object);
2090 }
2091
2092 shadow_object =
2093 object->pageout ? VM_OBJECT_NULL : object->shadow;
2094
2095 vm_object_reap(object);
2096 /* cache is unlocked and object is no longer valid */
2097 object = VM_OBJECT_NULL;
2098
2099 if (shadow_object != VM_OBJECT_NULL) {
2100 /*
2101 * Drop the reference "object" was holding on
2102 * its shadow object.
2103 */
2104 vm_object_deallocate(shadow_object);
2105 shadow_object = VM_OBJECT_NULL;
2106 }
2107 vm_object_reaper_lock_spin();
2108 }
2109
2110 /* wait for more work... */
2111 assert_wait((event_t) &vm_object_reaper_queue, THREAD_UNINT);
2112
2113 vm_object_reaper_unlock();
2114
2115 thread_block((thread_continue_t) vm_object_reaper_thread);
2116 /*NOTREACHED*/
2117 }
2118
2119 /*
2120 * Routine: vm_object_pager_wakeup
2121 * Purpose: Wake up anyone waiting for termination of a pager.
2122 */
2123
2124 static void
2125 vm_object_pager_wakeup(
2126 memory_object_t pager)
2127 {
2128 vm_object_hash_entry_t entry;
2129 boolean_t waiting = FALSE;
2130 lck_mtx_t *lck;
2131
2132 /*
2133 * If anyone was waiting for the memory_object_terminate
2134 * to be queued, wake them up now.
2135 */
2136 lck = vm_object_hash_lock_spin(pager);
2137 entry = vm_object_hash_lookup(pager, TRUE);
2138 if (entry != VM_OBJECT_HASH_ENTRY_NULL)
2139 waiting = entry->waiting;
2140 vm_object_hash_unlock(lck);
2141
2142 if (entry != VM_OBJECT_HASH_ENTRY_NULL) {
2143 if (waiting)
2144 thread_wakeup((event_t) pager);
2145 vm_object_hash_entry_free(entry);
2146 }
2147 }
2148
2149 /*
2150 * Routine: vm_object_release_pager
2151 * Purpose: Terminate the pager and, upon completion,
2152 * release our last reference to it.
2153 * just like memory_object_terminate, except
2154 * that we wake up anyone blocked in vm_object_enter
2155 * waiting for termination message to be queued
2156 * before calling memory_object_init.
2157 */
2158 static void
2159 vm_object_release_pager(
2160 memory_object_t pager,
2161 boolean_t hashed)
2162 {
2163
2164 /*
2165 * Terminate the pager.
2166 */
2167
2168 (void) memory_object_terminate(pager);
2169
2170 if (hashed == TRUE) {
2171 /*
2172 * Wakeup anyone waiting for this terminate
2173 * and remove the entry from the hash
2174 */
2175 vm_object_pager_wakeup(pager);
2176 }
2177 /*
2178 * Release reference to pager.
2179 */
2180 memory_object_deallocate(pager);
2181 }
2182
2183 /*
2184 * Routine: vm_object_destroy
2185 * Purpose:
2186 * Shut down a VM object, despite the
2187 * presence of address map (or other) references
2188 * to the vm_object.
2189 */
2190 kern_return_t
2191 vm_object_destroy(
2192 vm_object_t object,
2193 __unused kern_return_t reason)
2194 {
2195 memory_object_t old_pager;
2196
2197 if (object == VM_OBJECT_NULL)
2198 return(KERN_SUCCESS);
2199
2200 /*
2201 * Remove the pager association immediately.
2202 *
2203 * This will prevent the memory manager from further
2204 * meddling. [If it wanted to flush data or make
2205 * other changes, it should have done so before performing
2206 * the destroy call.]
2207 */
2208
2209 vm_object_lock(object);
2210 object->can_persist = FALSE;
2211 object->named = FALSE;
2212 object->alive = FALSE;
2213
2214 if (object->hashed) {
2215 lck_mtx_t *lck;
2216 /*
2217 * Rip out the pager from the vm_object now...
2218 */
2219 lck = vm_object_hash_lock_spin(object->pager);
2220 vm_object_remove(object);
2221 vm_object_hash_unlock(lck);
2222 }
2223 old_pager = object->pager;
2224 object->pager = MEMORY_OBJECT_NULL;
2225 if (old_pager != MEMORY_OBJECT_NULL)
2226 memory_object_control_disable(object->pager_control);
2227
2228 /*
2229 * Wait for the existing paging activity (that got
2230 * through before we nulled out the pager) to subside.
2231 */
2232
2233 vm_object_paging_wait(object, THREAD_UNINT);
2234 vm_object_unlock(object);
2235
2236 /*
2237 * Terminate the object now.
2238 */
2239 if (old_pager != MEMORY_OBJECT_NULL) {
2240 vm_object_release_pager(old_pager, object->hashed);
2241
2242 /*
2243 * JMM - Release the caller's reference. This assumes the
2244 * caller had a reference to release, which is a big (but
2245 * currently valid) assumption if this is driven from the
2246 * vnode pager (it is holding a named reference when making
2247 * this call)..
2248 */
2249 vm_object_deallocate(object);
2250
2251 }
2252 return(KERN_SUCCESS);
2253 }
2254
2255
2256 #if VM_OBJECT_CACHE
2257
2258 #define VM_OBJ_DEACT_ALL_STATS DEBUG
2259 #if VM_OBJ_DEACT_ALL_STATS
2260 uint32_t vm_object_deactivate_all_pages_batches = 0;
2261 uint32_t vm_object_deactivate_all_pages_pages = 0;
2262 #endif /* VM_OBJ_DEACT_ALL_STATS */
2263 /*
2264 * vm_object_deactivate_all_pages
2265 *
2266 * Deactivate all pages in the specified object. (Keep its pages
2267 * in memory even though it is no longer referenced.)
2268 *
2269 * The object must be locked.
2270 */
2271 static void
2272 vm_object_deactivate_all_pages(
2273 register vm_object_t object)
2274 {
2275 register vm_page_t p;
2276 int loop_count;
2277 #if VM_OBJ_DEACT_ALL_STATS
2278 int pages_count;
2279 #endif /* VM_OBJ_DEACT_ALL_STATS */
2280 #define V_O_D_A_P_MAX_BATCH 256
2281
2282 loop_count = BATCH_LIMIT(V_O_D_A_P_MAX_BATCH);
2283 #if VM_OBJ_DEACT_ALL_STATS
2284 pages_count = 0;
2285 #endif /* VM_OBJ_DEACT_ALL_STATS */
2286 vm_page_lock_queues();
2287 queue_iterate(&object->memq, p, vm_page_t, listq) {
2288 if (--loop_count == 0) {
2289 #if VM_OBJ_DEACT_ALL_STATS
2290 hw_atomic_add(&vm_object_deactivate_all_pages_batches,
2291 1);
2292 hw_atomic_add(&vm_object_deactivate_all_pages_pages,
2293 pages_count);
2294 pages_count = 0;
2295 #endif /* VM_OBJ_DEACT_ALL_STATS */
2296 lck_mtx_yield(&vm_page_queue_lock);
2297 loop_count = BATCH_LIMIT(V_O_D_A_P_MAX_BATCH);
2298 }
2299 if (!p->busy && !p->throttled) {
2300 #if VM_OBJ_DEACT_ALL_STATS
2301 pages_count++;
2302 #endif /* VM_OBJ_DEACT_ALL_STATS */
2303 vm_page_deactivate(p);
2304 }
2305 }
2306 #if VM_OBJ_DEACT_ALL_STATS
2307 if (pages_count) {
2308 hw_atomic_add(&vm_object_deactivate_all_pages_batches, 1);
2309 hw_atomic_add(&vm_object_deactivate_all_pages_pages,
2310 pages_count);
2311 pages_count = 0;
2312 }
2313 #endif /* VM_OBJ_DEACT_ALL_STATS */
2314 vm_page_unlock_queues();
2315 }
2316 #endif /* VM_OBJECT_CACHE */
2317
2318
2319
2320 /*
2321 * The "chunk" macros are used by routines below when looking for pages to deactivate. These
2322 * exist because of the need to handle shadow chains. When deactivating pages, we only
2323 * want to deactive the ones at the top most level in the object chain. In order to do
2324 * this efficiently, the specified address range is divided up into "chunks" and we use
2325 * a bit map to keep track of which pages have already been processed as we descend down
2326 * the shadow chain. These chunk macros hide the details of the bit map implementation
2327 * as much as we can.
2328 *
2329 * For convenience, we use a 64-bit data type as the bit map, and therefore a chunk is
2330 * set to 64 pages. The bit map is indexed from the low-order end, so that the lowest
2331 * order bit represents page 0 in the current range and highest order bit represents
2332 * page 63.
2333 *
2334 * For further convenience, we also use negative logic for the page state in the bit map.
2335 * The bit is set to 1 to indicate it has not yet been seen, and to 0 to indicate it has
2336 * been processed. This way we can simply test the 64-bit long word to see if it's zero
2337 * to easily tell if the whole range has been processed. Therefore, the bit map starts
2338 * out with all the bits set. The macros below hide all these details from the caller.
2339 */
2340
2341 #define PAGES_IN_A_CHUNK 64 /* The number of pages in the chunk must */
2342 /* be the same as the number of bits in */
2343 /* the chunk_state_t type. We use 64 */
2344 /* just for convenience. */
2345
2346 #define CHUNK_SIZE (PAGES_IN_A_CHUNK * PAGE_SIZE_64) /* Size of a chunk in bytes */
2347
2348 typedef uint64_t chunk_state_t;
2349
2350 /*
2351 * The bit map uses negative logic, so we start out with all 64 bits set to indicate
2352 * that no pages have been processed yet. Also, if len is less than the full CHUNK_SIZE,
2353 * then we mark pages beyond the len as having been "processed" so that we don't waste time
2354 * looking at pages in that range. This can save us from unnecessarily chasing down the
2355 * shadow chain.
2356 */
2357
2358 #define CHUNK_INIT(c, len) \
2359 MACRO_BEGIN \
2360 uint64_t p; \
2361 \
2362 (c) = 0xffffffffffffffffLL; \
2363 \
2364 for (p = (len) / PAGE_SIZE_64; p < PAGES_IN_A_CHUNK; p++) \
2365 MARK_PAGE_HANDLED(c, p); \
2366 MACRO_END
2367
2368
2369 /*
2370 * Return true if all pages in the chunk have not yet been processed.
2371 */
2372
2373 #define CHUNK_NOT_COMPLETE(c) ((c) != 0)
2374
2375 /*
2376 * Return true if the page at offset 'p' in the bit map has already been handled
2377 * while processing a higher level object in the shadow chain.
2378 */
2379
2380 #define PAGE_ALREADY_HANDLED(c, p) (((c) & (1LL << (p))) == 0)
2381
2382 /*
2383 * Mark the page at offset 'p' in the bit map as having been processed.
2384 */
2385
2386 #define MARK_PAGE_HANDLED(c, p) \
2387 MACRO_BEGIN \
2388 (c) = (c) & ~(1LL << (p)); \
2389 MACRO_END
2390
2391
2392 /*
2393 * Return true if the page at the given offset has been paged out. Object is
2394 * locked upon entry and returned locked.
2395 */
2396
2397 static boolean_t
2398 page_is_paged_out(
2399 vm_object_t object,
2400 vm_object_offset_t offset)
2401 {
2402 kern_return_t kr;
2403 memory_object_t pager;
2404
2405 /*
2406 * Check the existence map for the page if we have one, otherwise
2407 * ask the pager about this page.
2408 */
2409
2410 #if MACH_PAGEMAP
2411 if (object->existence_map) {
2412 if (vm_external_state_get(object->existence_map, offset)
2413 == VM_EXTERNAL_STATE_EXISTS) {
2414 /*
2415 * We found the page
2416 */
2417
2418 return TRUE;
2419 }
2420 } else
2421 #endif
2422 if (object->internal &&
2423 object->alive &&
2424 !object->terminating &&
2425 object->pager_ready) {
2426
2427 /*
2428 * We're already holding a "paging in progress" reference
2429 * so the object can't disappear when we release the lock.
2430 */
2431
2432 assert(object->paging_in_progress);
2433 pager = object->pager;
2434 vm_object_unlock(object);
2435
2436 kr = memory_object_data_request(
2437 pager,
2438 offset + object->paging_offset,
2439 0, /* just poke the pager */
2440 VM_PROT_READ,
2441 NULL);
2442
2443 vm_object_lock(object);
2444
2445 if (kr == KERN_SUCCESS) {
2446
2447 /*
2448 * We found the page
2449 */
2450
2451 return TRUE;
2452 }
2453 }
2454
2455 return FALSE;
2456 }
2457
2458
2459
2460 /*
2461 * Deactivate the pages in the specified object and range. If kill_page is set, also discard any
2462 * page modified state from the pmap. Update the chunk_state as we go along. The caller must specify
2463 * a size that is less than or equal to the CHUNK_SIZE.
2464 */
2465
2466 static void
2467 deactivate_pages_in_object(
2468 vm_object_t object,
2469 vm_object_offset_t offset,
2470 vm_object_size_t size,
2471 boolean_t kill_page,
2472 boolean_t reusable_page,
2473 #if !MACH_ASSERT
2474 __unused
2475 #endif
2476 boolean_t all_reusable,
2477 chunk_state_t *chunk_state)
2478 {
2479 vm_page_t m;
2480 int p;
2481 struct vm_page_delayed_work dw_array[DEFAULT_DELAYED_WORK_LIMIT];
2482 struct vm_page_delayed_work *dwp;
2483 int dw_count;
2484 int dw_limit;
2485 unsigned int reusable = 0;
2486
2487
2488 /*
2489 * Examine each page in the chunk. The variable 'p' is the page number relative to the start of the
2490 * chunk. Since this routine is called once for each level in the shadow chain, the chunk_state may
2491 * have pages marked as having been processed already. We stop the loop early if we find we've handled
2492 * all the pages in the chunk.
2493 */
2494
2495 dwp = &dw_array[0];
2496 dw_count = 0;
2497 dw_limit = DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT);
2498
2499 for(p = 0; size && CHUNK_NOT_COMPLETE(*chunk_state); p++, size -= PAGE_SIZE_64, offset += PAGE_SIZE_64) {
2500
2501 /*
2502 * If this offset has already been found and handled in a higher level object, then don't
2503 * do anything with it in the current shadow object.
2504 */
2505
2506 if (PAGE_ALREADY_HANDLED(*chunk_state, p))
2507 continue;
2508
2509 /*
2510 * See if the page at this offset is around. First check to see if the page is resident,
2511 * then if not, check the existence map or with the pager.
2512 */
2513
2514 if ((m = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
2515
2516 /*
2517 * We found a page we were looking for. Mark it as "handled" now in the chunk_state
2518 * so that we won't bother looking for a page at this offset again if there are more
2519 * shadow objects. Then deactivate the page.
2520 */
2521
2522 MARK_PAGE_HANDLED(*chunk_state, p);
2523
2524 if (( !VM_PAGE_WIRED(m)) && (!m->private) && (!m->gobbled) && (!m->busy)) {
2525 int clear_refmod;
2526
2527 assert(!m->laundry);
2528
2529 clear_refmod = VM_MEM_REFERENCED;
2530 dwp->dw_mask = DW_clear_reference;
2531
2532 if ((kill_page) && (object->internal)) {
2533 m->precious = FALSE;
2534 m->dirty = FALSE;
2535
2536 clear_refmod |= VM_MEM_MODIFIED;
2537 if (m->throttled) {
2538 /*
2539 * This page is now clean and
2540 * reclaimable. Move it out
2541 * of the throttled queue, so
2542 * that vm_pageout_scan() can
2543 * find it.
2544 */
2545 dwp->dw_mask |= DW_move_page;
2546 }
2547 #if MACH_PAGEMAP
2548 vm_external_state_clr(object->existence_map, offset);
2549 #endif /* MACH_PAGEMAP */
2550
2551 if (reusable_page && !m->reusable) {
2552 assert(!all_reusable);
2553 assert(!object->all_reusable);
2554 m->reusable = TRUE;
2555 object->reusable_page_count++;
2556 assert(object->resident_page_count >= object->reusable_page_count);
2557 reusable++;
2558 }
2559 }
2560 pmap_clear_refmod(m->phys_page, clear_refmod);
2561
2562 if (!m->throttled && !(reusable_page || all_reusable))
2563 dwp->dw_mask |= DW_move_page;
2564
2565 VM_PAGE_ADD_DELAYED_WORK(dwp, m, dw_count);
2566
2567 if (dw_count >= dw_limit) {
2568 if (reusable) {
2569 OSAddAtomic(reusable,
2570 &vm_page_stats_reusable.reusable_count);
2571 vm_page_stats_reusable.reusable += reusable;
2572 reusable = 0;
2573 }
2574 vm_page_do_delayed_work(object, &dw_array[0], dw_count);
2575
2576 dwp = &dw_array[0];
2577 dw_count = 0;
2578 }
2579 }
2580
2581 } else {
2582
2583 /*
2584 * The page at this offset isn't memory resident, check to see if it's
2585 * been paged out. If so, mark it as handled so we don't bother looking
2586 * for it in the shadow chain.
2587 */
2588
2589 if (page_is_paged_out(object, offset)) {
2590 MARK_PAGE_HANDLED(*chunk_state, p);
2591
2592 /*
2593 * If we're killing a non-resident page, then clear the page in the existence
2594 * map so we don't bother paging it back in if it's touched again in the future.
2595 */
2596
2597 if ((kill_page) && (object->internal)) {
2598 #if MACH_PAGEMAP
2599 vm_external_state_clr(object->existence_map, offset);
2600 #endif /* MACH_PAGEMAP */
2601 }
2602 }
2603 }
2604 }
2605
2606 if (reusable) {
2607 OSAddAtomic(reusable, &vm_page_stats_reusable.reusable_count);
2608 vm_page_stats_reusable.reusable += reusable;
2609 reusable = 0;
2610 }
2611
2612 if (dw_count)
2613 vm_page_do_delayed_work(object, &dw_array[0], dw_count);
2614 }
2615
2616
2617 /*
2618 * Deactive a "chunk" of the given range of the object starting at offset. A "chunk"
2619 * will always be less than or equal to the given size. The total range is divided up
2620 * into chunks for efficiency and performance related to the locks and handling the shadow
2621 * chain. This routine returns how much of the given "size" it actually processed. It's
2622 * up to the caler to loop and keep calling this routine until the entire range they want
2623 * to process has been done.
2624 */
2625
2626 static vm_object_size_t
2627 deactivate_a_chunk(
2628 vm_object_t orig_object,
2629 vm_object_offset_t offset,
2630 vm_object_size_t size,
2631 boolean_t kill_page,
2632 boolean_t reusable_page,
2633 boolean_t all_reusable)
2634 {
2635 vm_object_t object;
2636 vm_object_t tmp_object;
2637 vm_object_size_t length;
2638 chunk_state_t chunk_state;
2639
2640
2641 /*
2642 * Get set to do a chunk. We'll do up to CHUNK_SIZE, but no more than the
2643 * remaining size the caller asked for.
2644 */
2645
2646 length = MIN(size, CHUNK_SIZE);
2647
2648 /*
2649 * The chunk_state keeps track of which pages we've already processed if there's
2650 * a shadow chain on this object. At this point, we haven't done anything with this
2651 * range of pages yet, so initialize the state to indicate no pages processed yet.
2652 */
2653
2654 CHUNK_INIT(chunk_state, length);
2655 object = orig_object;
2656
2657 /*
2658 * Start at the top level object and iterate around the loop once for each object
2659 * in the shadow chain. We stop processing early if we've already found all the pages
2660 * in the range. Otherwise we stop when we run out of shadow objects.
2661 */
2662
2663 while (object && CHUNK_NOT_COMPLETE(chunk_state)) {
2664 vm_object_paging_begin(object);
2665
2666 deactivate_pages_in_object(object, offset, length, kill_page, reusable_page, all_reusable, &chunk_state);
2667
2668 vm_object_paging_end(object);
2669
2670 /*
2671 * We've finished with this object, see if there's a shadow object. If
2672 * there is, update the offset and lock the new object. We also turn off
2673 * kill_page at this point since we only kill pages in the top most object.
2674 */
2675
2676 tmp_object = object->shadow;
2677
2678 if (tmp_object) {
2679 kill_page = FALSE;
2680 reusable_page = FALSE;
2681 all_reusable = FALSE;
2682 offset += object->vo_shadow_offset;
2683 vm_object_lock(tmp_object);
2684 }
2685
2686 if (object != orig_object)
2687 vm_object_unlock(object);
2688
2689 object = tmp_object;
2690 }
2691
2692 if (object && object != orig_object)
2693 vm_object_unlock(object);
2694
2695 return length;
2696 }
2697
2698
2699
2700 /*
2701 * Move any resident pages in the specified range to the inactive queue. If kill_page is set,
2702 * we also clear the modified status of the page and "forget" any changes that have been made
2703 * to the page.
2704 */
2705
2706 __private_extern__ void
2707 vm_object_deactivate_pages(
2708 vm_object_t object,
2709 vm_object_offset_t offset,
2710 vm_object_size_t size,
2711 boolean_t kill_page,
2712 boolean_t reusable_page)
2713 {
2714 vm_object_size_t length;
2715 boolean_t all_reusable;
2716
2717 /*
2718 * We break the range up into chunks and do one chunk at a time. This is for
2719 * efficiency and performance while handling the shadow chains and the locks.
2720 * The deactivate_a_chunk() function returns how much of the range it processed.
2721 * We keep calling this routine until the given size is exhausted.
2722 */
2723
2724
2725 all_reusable = FALSE;
2726 if (reusable_page &&
2727 object->internal &&
2728 object->vo_size != 0 &&
2729 object->vo_size == size &&
2730 object->reusable_page_count == 0) {
2731 all_reusable = TRUE;
2732 reusable_page = FALSE;
2733 }
2734
2735 if ((reusable_page || all_reusable) && object->all_reusable) {
2736 /* This means MADV_FREE_REUSABLE has been called twice, which
2737 * is probably illegal. */
2738 return;
2739 }
2740
2741 while (size) {
2742 length = deactivate_a_chunk(object, offset, size, kill_page, reusable_page, all_reusable);
2743
2744 size -= length;
2745 offset += length;
2746 }
2747
2748 if (all_reusable) {
2749 if (!object->all_reusable) {
2750 unsigned int reusable;
2751
2752 object->all_reusable = TRUE;
2753 assert(object->reusable_page_count == 0);
2754 /* update global stats */
2755 reusable = object->resident_page_count;
2756 OSAddAtomic(reusable,
2757 &vm_page_stats_reusable.reusable_count);
2758 vm_page_stats_reusable.reusable += reusable;
2759 vm_page_stats_reusable.all_reusable_calls++;
2760 }
2761 } else if (reusable_page) {
2762 vm_page_stats_reusable.partial_reusable_calls++;
2763 }
2764 }
2765
2766 void
2767 vm_object_reuse_pages(
2768 vm_object_t object,
2769 vm_object_offset_t start_offset,
2770 vm_object_offset_t end_offset,
2771 boolean_t allow_partial_reuse)
2772 {
2773 vm_object_offset_t cur_offset;
2774 vm_page_t m;
2775 unsigned int reused, reusable;
2776
2777 #define VM_OBJECT_REUSE_PAGE(object, m, reused) \
2778 MACRO_BEGIN \
2779 if ((m) != VM_PAGE_NULL && \
2780 (m)->reusable) { \
2781 assert((object)->reusable_page_count <= \
2782 (object)->resident_page_count); \
2783 assert((object)->reusable_page_count > 0); \
2784 (object)->reusable_page_count--; \
2785 (m)->reusable = FALSE; \
2786 (reused)++; \
2787 } \
2788 MACRO_END
2789
2790 reused = 0;
2791 reusable = 0;
2792
2793 vm_object_lock_assert_exclusive(object);
2794
2795 if (object->all_reusable) {
2796 assert(object->reusable_page_count == 0);
2797 object->all_reusable = FALSE;
2798 if (end_offset - start_offset == object->vo_size ||
2799 !allow_partial_reuse) {
2800 vm_page_stats_reusable.all_reuse_calls++;
2801 reused = object->resident_page_count;
2802 } else {
2803 vm_page_stats_reusable.partial_reuse_calls++;
2804 queue_iterate(&object->memq, m, vm_page_t, listq) {
2805 if (m->offset < start_offset ||
2806 m->offset >= end_offset) {
2807 m->reusable = TRUE;
2808 object->reusable_page_count++;
2809 assert(object->resident_page_count >= object->reusable_page_count);
2810 continue;
2811 } else {
2812 assert(!m->reusable);
2813 reused++;
2814 }
2815 }
2816 }
2817 } else if (object->resident_page_count >
2818 ((end_offset - start_offset) >> PAGE_SHIFT)) {
2819 vm_page_stats_reusable.partial_reuse_calls++;
2820 for (cur_offset = start_offset;
2821 cur_offset < end_offset;
2822 cur_offset += PAGE_SIZE_64) {
2823 if (object->reusable_page_count == 0) {
2824 break;
2825 }
2826 m = vm_page_lookup(object, cur_offset);
2827 VM_OBJECT_REUSE_PAGE(object, m, reused);
2828 }
2829 } else {
2830 vm_page_stats_reusable.partial_reuse_calls++;
2831 queue_iterate(&object->memq, m, vm_page_t, listq) {
2832 if (object->reusable_page_count == 0) {
2833 break;
2834 }
2835 if (m->offset < start_offset ||
2836 m->offset >= end_offset) {
2837 continue;
2838 }
2839 VM_OBJECT_REUSE_PAGE(object, m, reused);
2840 }
2841 }
2842
2843 /* update global stats */
2844 OSAddAtomic(reusable-reused, &vm_page_stats_reusable.reusable_count);
2845 vm_page_stats_reusable.reused += reused;
2846 vm_page_stats_reusable.reusable += reusable;
2847 }
2848
2849 /*
2850 * Routine: vm_object_pmap_protect
2851 *
2852 * Purpose:
2853 * Reduces the permission for all physical
2854 * pages in the specified object range.
2855 *
2856 * If removing write permission only, it is
2857 * sufficient to protect only the pages in
2858 * the top-level object; only those pages may
2859 * have write permission.
2860 *
2861 * If removing all access, we must follow the
2862 * shadow chain from the top-level object to
2863 * remove access to all pages in shadowed objects.
2864 *
2865 * The object must *not* be locked. The object must
2866 * be temporary/internal.
2867 *
2868 * If pmap is not NULL, this routine assumes that
2869 * the only mappings for the pages are in that
2870 * pmap.
2871 */
2872
2873 __private_extern__ void
2874 vm_object_pmap_protect(
2875 register vm_object_t object,
2876 register vm_object_offset_t offset,
2877 vm_object_size_t size,
2878 pmap_t pmap,
2879 vm_map_offset_t pmap_start,
2880 vm_prot_t prot)
2881 {
2882 if (object == VM_OBJECT_NULL)
2883 return;
2884 size = vm_object_round_page(size);
2885 offset = vm_object_trunc_page(offset);
2886
2887 vm_object_lock(object);
2888
2889 if (object->phys_contiguous) {
2890 if (pmap != NULL) {
2891 vm_object_unlock(object);
2892 pmap_protect(pmap, pmap_start, pmap_start + size, prot);
2893 } else {
2894 vm_object_offset_t phys_start, phys_end, phys_addr;
2895
2896 phys_start = object->vo_shadow_offset + offset;
2897 phys_end = phys_start + size;
2898 assert(phys_start <= phys_end);
2899 assert(phys_end <= object->vo_shadow_offset + object->vo_size);
2900 vm_object_unlock(object);
2901
2902 for (phys_addr = phys_start;
2903 phys_addr < phys_end;
2904 phys_addr += PAGE_SIZE_64) {
2905 pmap_page_protect((ppnum_t) (phys_addr >> PAGE_SHIFT), prot);
2906 }
2907 }
2908 return;
2909 }
2910
2911 assert(object->internal);
2912
2913 while (TRUE) {
2914 if (ptoa_64(object->resident_page_count) > size/2 && pmap != PMAP_NULL) {
2915 vm_object_unlock(object);
2916 pmap_protect(pmap, pmap_start, pmap_start + size, prot);
2917 return;
2918 }
2919
2920 /* if we are doing large ranges with respect to resident */
2921 /* page count then we should interate over pages otherwise */
2922 /* inverse page look-up will be faster */
2923 if (ptoa_64(object->resident_page_count / 4) < size) {
2924 vm_page_t p;
2925 vm_object_offset_t end;
2926
2927 end = offset + size;
2928
2929 if (pmap != PMAP_NULL) {
2930 queue_iterate(&object->memq, p, vm_page_t, listq) {
2931 if (!p->fictitious &&
2932 (offset <= p->offset) && (p->offset < end)) {
2933 vm_map_offset_t start;
2934
2935 start = pmap_start + p->offset - offset;
2936 pmap_protect(pmap, start, start + PAGE_SIZE_64, prot);
2937 }
2938 }
2939 } else {
2940 queue_iterate(&object->memq, p, vm_page_t, listq) {
2941 if (!p->fictitious &&
2942 (offset <= p->offset) && (p->offset < end)) {
2943
2944 pmap_page_protect(p->phys_page, prot);
2945 }
2946 }
2947 }
2948 } else {
2949 vm_page_t p;
2950 vm_object_offset_t end;
2951 vm_object_offset_t target_off;
2952
2953 end = offset + size;
2954
2955 if (pmap != PMAP_NULL) {
2956 for(target_off = offset;
2957 target_off < end;
2958 target_off += PAGE_SIZE) {
2959 p = vm_page_lookup(object, target_off);
2960 if (p != VM_PAGE_NULL) {
2961 vm_object_offset_t start;
2962 start = pmap_start +
2963 (p->offset - offset);
2964 pmap_protect(pmap, start,
2965 start + PAGE_SIZE, prot);
2966 }
2967 }
2968 } else {
2969 for(target_off = offset;
2970 target_off < end; target_off += PAGE_SIZE) {
2971 p = vm_page_lookup(object, target_off);
2972 if (p != VM_PAGE_NULL) {
2973 pmap_page_protect(p->phys_page, prot);
2974 }
2975 }
2976 }
2977 }
2978
2979 if (prot == VM_PROT_NONE) {
2980 /*
2981 * Must follow shadow chain to remove access
2982 * to pages in shadowed objects.
2983 */
2984 register vm_object_t next_object;
2985
2986 next_object = object->shadow;
2987 if (next_object != VM_OBJECT_NULL) {
2988 offset += object->vo_shadow_offset;
2989 vm_object_lock(next_object);
2990 vm_object_unlock(object);
2991 object = next_object;
2992 }
2993 else {
2994 /*
2995 * End of chain - we are done.
2996 */
2997 break;
2998 }
2999 }
3000 else {
3001 /*
3002 * Pages in shadowed objects may never have
3003 * write permission - we may stop here.
3004 */
3005 break;
3006 }
3007 }
3008
3009 vm_object_unlock(object);
3010 }
3011
3012 /*
3013 * Routine: vm_object_copy_slowly
3014 *
3015 * Description:
3016 * Copy the specified range of the source
3017 * virtual memory object without using
3018 * protection-based optimizations (such
3019 * as copy-on-write). The pages in the
3020 * region are actually copied.
3021 *
3022 * In/out conditions:
3023 * The caller must hold a reference and a lock
3024 * for the source virtual memory object. The source
3025 * object will be returned *unlocked*.
3026 *
3027 * Results:
3028 * If the copy is completed successfully, KERN_SUCCESS is
3029 * returned. If the caller asserted the interruptible
3030 * argument, and an interruption occurred while waiting
3031 * for a user-generated event, MACH_SEND_INTERRUPTED is
3032 * returned. Other values may be returned to indicate
3033 * hard errors during the copy operation.
3034 *
3035 * A new virtual memory object is returned in a
3036 * parameter (_result_object). The contents of this
3037 * new object, starting at a zero offset, are a copy
3038 * of the source memory region. In the event of
3039 * an error, this parameter will contain the value
3040 * VM_OBJECT_NULL.
3041 */
3042 __private_extern__ kern_return_t
3043 vm_object_copy_slowly(
3044 register vm_object_t src_object,
3045 vm_object_offset_t src_offset,
3046 vm_object_size_t size,
3047 boolean_t interruptible,
3048 vm_object_t *_result_object) /* OUT */
3049 {
3050 vm_object_t new_object;
3051 vm_object_offset_t new_offset;
3052
3053 struct vm_object_fault_info fault_info;
3054
3055 XPR(XPR_VM_OBJECT, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
3056 src_object, src_offset, size, 0, 0);
3057
3058 if (size == 0) {
3059 vm_object_unlock(src_object);
3060 *_result_object = VM_OBJECT_NULL;
3061 return(KERN_INVALID_ARGUMENT);
3062 }
3063
3064 /*
3065 * Prevent destruction of the source object while we copy.
3066 */
3067
3068 vm_object_reference_locked(src_object);
3069 vm_object_unlock(src_object);
3070
3071 /*
3072 * Create a new object to hold the copied pages.
3073 * A few notes:
3074 * We fill the new object starting at offset 0,
3075 * regardless of the input offset.
3076 * We don't bother to lock the new object within
3077 * this routine, since we have the only reference.
3078 */
3079
3080 new_object = vm_object_allocate(size);
3081 new_offset = 0;
3082
3083 assert(size == trunc_page_64(size)); /* Will the loop terminate? */
3084
3085 fault_info.interruptible = interruptible;
3086 fault_info.behavior = VM_BEHAVIOR_SEQUENTIAL;
3087 fault_info.user_tag = 0;
3088 fault_info.lo_offset = src_offset;
3089 fault_info.hi_offset = src_offset + size;
3090 fault_info.no_cache = FALSE;
3091 fault_info.stealth = TRUE;
3092 fault_info.io_sync = FALSE;
3093 fault_info.cs_bypass = FALSE;
3094 fault_info.mark_zf_absent = FALSE;
3095
3096 for ( ;
3097 size != 0 ;
3098 src_offset += PAGE_SIZE_64,
3099 new_offset += PAGE_SIZE_64, size -= PAGE_SIZE_64
3100 ) {
3101 vm_page_t new_page;
3102 vm_fault_return_t result;
3103
3104 vm_object_lock(new_object);
3105
3106 while ((new_page = vm_page_alloc(new_object, new_offset))
3107 == VM_PAGE_NULL) {
3108
3109 vm_object_unlock(new_object);
3110
3111 if (!vm_page_wait(interruptible)) {
3112 vm_object_deallocate(new_object);
3113 vm_object_deallocate(src_object);
3114 *_result_object = VM_OBJECT_NULL;
3115 return(MACH_SEND_INTERRUPTED);
3116 }
3117 vm_object_lock(new_object);
3118 }
3119 vm_object_unlock(new_object);
3120
3121 do {
3122 vm_prot_t prot = VM_PROT_READ;
3123 vm_page_t _result_page;
3124 vm_page_t top_page;
3125 register
3126 vm_page_t result_page;
3127 kern_return_t error_code;
3128
3129 vm_object_lock(src_object);
3130 vm_object_paging_begin(src_object);
3131
3132 if (size > (vm_size_t) -1) {
3133 /* 32-bit overflow */
3134 fault_info.cluster_size = (vm_size_t) (0 - PAGE_SIZE);
3135 } else {
3136 fault_info.cluster_size = (vm_size_t) size;
3137 assert(fault_info.cluster_size == size);
3138 }
3139
3140 XPR(XPR_VM_FAULT,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
3141 result = vm_fault_page(src_object, src_offset,
3142 VM_PROT_READ, FALSE,
3143 &prot, &_result_page, &top_page,
3144 (int *)0,
3145 &error_code, FALSE, FALSE, &fault_info);
3146
3147 switch(result) {
3148 case VM_FAULT_SUCCESS:
3149 result_page = _result_page;
3150
3151 /*
3152 * We don't need to hold the object
3153 * lock -- the busy page will be enough.
3154 * [We don't care about picking up any
3155 * new modifications.]
3156 *
3157 * Copy the page to the new object.
3158 *
3159 * POLICY DECISION:
3160 * If result_page is clean,
3161 * we could steal it instead
3162 * of copying.
3163 */
3164
3165 vm_object_unlock(result_page->object);
3166 vm_page_copy(result_page, new_page);
3167
3168 /*
3169 * Let go of both pages (make them
3170 * not busy, perform wakeup, activate).
3171 */
3172 vm_object_lock(new_object);
3173 new_page->dirty = TRUE;
3174 PAGE_WAKEUP_DONE(new_page);
3175 vm_object_unlock(new_object);
3176
3177 vm_object_lock(result_page->object);
3178 PAGE_WAKEUP_DONE(result_page);
3179
3180 vm_page_lockspin_queues();
3181 if (!result_page->active &&
3182 !result_page->inactive &&
3183 !result_page->throttled)
3184 vm_page_activate(result_page);
3185 vm_page_activate(new_page);
3186 vm_page_unlock_queues();
3187
3188 /*
3189 * Release paging references and
3190 * top-level placeholder page, if any.
3191 */
3192
3193 vm_fault_cleanup(result_page->object,
3194 top_page);
3195
3196 break;
3197
3198 case VM_FAULT_RETRY:
3199 break;
3200
3201 case VM_FAULT_MEMORY_SHORTAGE:
3202 if (vm_page_wait(interruptible))
3203 break;
3204 /* fall thru */
3205
3206 case VM_FAULT_INTERRUPTED:
3207 vm_object_lock(new_object);
3208 VM_PAGE_FREE(new_page);
3209 vm_object_unlock(new_object);
3210
3211 vm_object_deallocate(new_object);
3212 vm_object_deallocate(src_object);
3213 *_result_object = VM_OBJECT_NULL;
3214 return(MACH_SEND_INTERRUPTED);
3215
3216 case VM_FAULT_SUCCESS_NO_VM_PAGE:
3217 /* success but no VM page: fail */
3218 vm_object_paging_end(src_object);
3219 vm_object_unlock(src_object);
3220 /*FALLTHROUGH*/
3221 case VM_FAULT_MEMORY_ERROR:
3222 /*
3223 * A policy choice:
3224 * (a) ignore pages that we can't
3225 * copy
3226 * (b) return the null object if
3227 * any page fails [chosen]
3228 */
3229
3230 vm_object_lock(new_object);
3231 VM_PAGE_FREE(new_page);
3232 vm_object_unlock(new_object);
3233
3234 vm_object_deallocate(new_object);
3235 vm_object_deallocate(src_object);
3236 *_result_object = VM_OBJECT_NULL;
3237 return(error_code ? error_code:
3238 KERN_MEMORY_ERROR);
3239
3240 default:
3241 panic("vm_object_copy_slowly: unexpected error"
3242 " 0x%x from vm_fault_page()\n", result);
3243 }
3244 } while (result != VM_FAULT_SUCCESS);
3245 }
3246
3247 /*
3248 * Lose the extra reference, and return our object.
3249 */
3250 vm_object_deallocate(src_object);
3251 *_result_object = new_object;
3252 return(KERN_SUCCESS);
3253 }
3254
3255 /*
3256 * Routine: vm_object_copy_quickly
3257 *
3258 * Purpose:
3259 * Copy the specified range of the source virtual
3260 * memory object, if it can be done without waiting
3261 * for user-generated events.
3262 *
3263 * Results:
3264 * If the copy is successful, the copy is returned in
3265 * the arguments; otherwise, the arguments are not
3266 * affected.
3267 *
3268 * In/out conditions:
3269 * The object should be unlocked on entry and exit.
3270 */
3271
3272 /*ARGSUSED*/
3273 __private_extern__ boolean_t
3274 vm_object_copy_quickly(
3275 vm_object_t *_object, /* INOUT */
3276 __unused vm_object_offset_t offset, /* IN */
3277 __unused vm_object_size_t size, /* IN */
3278 boolean_t *_src_needs_copy, /* OUT */
3279 boolean_t *_dst_needs_copy) /* OUT */
3280 {
3281 vm_object_t object = *_object;
3282 memory_object_copy_strategy_t copy_strategy;
3283
3284 XPR(XPR_VM_OBJECT, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
3285 *_object, offset, size, 0, 0);
3286 if (object == VM_OBJECT_NULL) {
3287 *_src_needs_copy = FALSE;
3288 *_dst_needs_copy = FALSE;
3289 return(TRUE);
3290 }
3291
3292 vm_object_lock(object);
3293
3294 copy_strategy = object->copy_strategy;
3295
3296 switch (copy_strategy) {
3297 case MEMORY_OBJECT_COPY_SYMMETRIC:
3298
3299 /*
3300 * Symmetric copy strategy.
3301 * Make another reference to the object.
3302 * Leave object/offset unchanged.
3303 */
3304
3305 vm_object_reference_locked(object);
3306 object->shadowed = TRUE;
3307 vm_object_unlock(object);
3308
3309 /*
3310 * Both source and destination must make
3311 * shadows, and the source must be made
3312 * read-only if not already.
3313 */
3314
3315 *_src_needs_copy = TRUE;
3316 *_dst_needs_copy = TRUE;
3317
3318 break;
3319
3320 case MEMORY_OBJECT_COPY_DELAY:
3321 vm_object_unlock(object);
3322 return(FALSE);
3323
3324 default:
3325 vm_object_unlock(object);
3326 return(FALSE);
3327 }
3328 return(TRUE);
3329 }
3330
3331 static int copy_call_count = 0;
3332 static int copy_call_sleep_count = 0;
3333 static int copy_call_restart_count = 0;
3334
3335 /*
3336 * Routine: vm_object_copy_call [internal]
3337 *
3338 * Description:
3339 * Copy the source object (src_object), using the
3340 * user-managed copy algorithm.
3341 *
3342 * In/out conditions:
3343 * The source object must be locked on entry. It
3344 * will be *unlocked* on exit.
3345 *
3346 * Results:
3347 * If the copy is successful, KERN_SUCCESS is returned.
3348 * A new object that represents the copied virtual
3349 * memory is returned in a parameter (*_result_object).
3350 * If the return value indicates an error, this parameter
3351 * is not valid.
3352 */
3353 static kern_return_t
3354 vm_object_copy_call(
3355 vm_object_t src_object,
3356 vm_object_offset_t src_offset,
3357 vm_object_size_t size,
3358 vm_object_t *_result_object) /* OUT */
3359 {
3360 kern_return_t kr;
3361 vm_object_t copy;
3362 boolean_t check_ready = FALSE;
3363 uint32_t try_failed_count = 0;
3364
3365 /*
3366 * If a copy is already in progress, wait and retry.
3367 *
3368 * XXX
3369 * Consider making this call interruptable, as Mike
3370 * intended it to be.
3371 *
3372 * XXXO
3373 * Need a counter or version or something to allow
3374 * us to use the copy that the currently requesting
3375 * thread is obtaining -- is it worth adding to the
3376 * vm object structure? Depends how common this case it.
3377 */
3378 copy_call_count++;
3379 while (vm_object_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL)) {
3380 vm_object_sleep(src_object, VM_OBJECT_EVENT_COPY_CALL,
3381 THREAD_UNINT);
3382 copy_call_restart_count++;
3383 }
3384
3385 /*
3386 * Indicate (for the benefit of memory_object_create_copy)
3387 * that we want a copy for src_object. (Note that we cannot
3388 * do a real assert_wait before calling memory_object_copy,
3389 * so we simply set the flag.)
3390 */
3391
3392 vm_object_set_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL);
3393 vm_object_unlock(src_object);
3394
3395 /*
3396 * Ask the memory manager to give us a memory object
3397 * which represents a copy of the src object.
3398 * The memory manager may give us a memory object
3399 * which we already have, or it may give us a
3400 * new memory object. This memory object will arrive
3401 * via memory_object_create_copy.
3402 */
3403
3404 kr = KERN_FAILURE; /* XXX need to change memory_object.defs */
3405 if (kr != KERN_SUCCESS) {
3406 return kr;
3407 }
3408
3409 /*
3410 * Wait for the copy to arrive.
3411 */
3412 vm_object_lock(src_object);
3413 while (vm_object_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL)) {
3414 vm_object_sleep(src_object, VM_OBJECT_EVENT_COPY_CALL,
3415 THREAD_UNINT);
3416 copy_call_sleep_count++;
3417 }
3418 Retry:
3419 assert(src_object->copy != VM_OBJECT_NULL);
3420 copy = src_object->copy;
3421 if (!vm_object_lock_try(copy)) {
3422 vm_object_unlock(src_object);
3423
3424 try_failed_count++;
3425 mutex_pause(try_failed_count); /* wait a bit */
3426
3427 vm_object_lock(src_object);
3428 goto Retry;
3429 }
3430 if (copy->vo_size < src_offset+size)
3431 copy->vo_size = src_offset+size;
3432
3433 if (!copy->pager_ready)
3434 check_ready = TRUE;
3435
3436 /*
3437 * Return the copy.
3438 */
3439 *_result_object = copy;
3440 vm_object_unlock(copy);
3441 vm_object_unlock(src_object);
3442
3443 /* Wait for the copy to be ready. */
3444 if (check_ready == TRUE) {
3445 vm_object_lock(copy);
3446 while (!copy->pager_ready) {
3447 vm_object_sleep(copy, VM_OBJECT_EVENT_PAGER_READY, THREAD_UNINT);
3448 }
3449 vm_object_unlock(copy);
3450 }
3451
3452 return KERN_SUCCESS;
3453 }
3454
3455 static int copy_delayed_lock_collisions = 0;
3456 static int copy_delayed_max_collisions = 0;
3457 static int copy_delayed_lock_contention = 0;
3458 static int copy_delayed_protect_iterate = 0;
3459
3460 /*
3461 * Routine: vm_object_copy_delayed [internal]
3462 *
3463 * Description:
3464 * Copy the specified virtual memory object, using
3465 * the asymmetric copy-on-write algorithm.
3466 *
3467 * In/out conditions:
3468 * The src_object must be locked on entry. It will be unlocked
3469 * on exit - so the caller must also hold a reference to it.
3470 *
3471 * This routine will not block waiting for user-generated
3472 * events. It is not interruptible.
3473 */
3474 __private_extern__ vm_object_t
3475 vm_object_copy_delayed(
3476 vm_object_t src_object,
3477 vm_object_offset_t src_offset,
3478 vm_object_size_t size,
3479 boolean_t src_object_shared)
3480 {
3481 vm_object_t new_copy = VM_OBJECT_NULL;
3482 vm_object_t old_copy;
3483 vm_page_t p;
3484 vm_object_size_t copy_size = src_offset + size;
3485
3486
3487 int collisions = 0;
3488 /*
3489 * The user-level memory manager wants to see all of the changes
3490 * to this object, but it has promised not to make any changes on
3491 * its own.
3492 *
3493 * Perform an asymmetric copy-on-write, as follows:
3494 * Create a new object, called a "copy object" to hold
3495 * pages modified by the new mapping (i.e., the copy,
3496 * not the original mapping).
3497 * Record the original object as the backing object for
3498 * the copy object. If the original mapping does not
3499 * change a page, it may be used read-only by the copy.
3500 * Record the copy object in the original object.
3501 * When the original mapping causes a page to be modified,
3502 * it must be copied to a new page that is "pushed" to
3503 * the copy object.
3504 * Mark the new mapping (the copy object) copy-on-write.
3505 * This makes the copy object itself read-only, allowing
3506 * it to be reused if the original mapping makes no
3507 * changes, and simplifying the synchronization required
3508 * in the "push" operation described above.
3509 *
3510 * The copy-on-write is said to be assymetric because the original
3511 * object is *not* marked copy-on-write. A copied page is pushed
3512 * to the copy object, regardless which party attempted to modify
3513 * the page.
3514 *
3515 * Repeated asymmetric copy operations may be done. If the
3516 * original object has not been changed since the last copy, its
3517 * copy object can be reused. Otherwise, a new copy object can be
3518 * inserted between the original object and its previous copy
3519 * object. Since any copy object is read-only, this cannot affect
3520 * affect the contents of the previous copy object.
3521 *
3522 * Note that a copy object is higher in the object tree than the
3523 * original object; therefore, use of the copy object recorded in
3524 * the original object must be done carefully, to avoid deadlock.
3525 */
3526
3527 Retry:
3528
3529 /*
3530 * Wait for paging in progress.
3531 */
3532 if (!src_object->true_share &&
3533 (src_object->paging_in_progress != 0 ||
3534 src_object->activity_in_progress != 0)) {
3535 if (src_object_shared == TRUE) {
3536 vm_object_unlock(src_object);
3537 vm_object_lock(src_object);
3538 src_object_shared = FALSE;
3539 goto Retry;
3540 }
3541 vm_object_paging_wait(src_object, THREAD_UNINT);
3542 }
3543 /*
3544 * See whether we can reuse the result of a previous
3545 * copy operation.
3546 */
3547
3548 old_copy = src_object->copy;
3549 if (old_copy != VM_OBJECT_NULL) {
3550 int lock_granted;
3551
3552 /*
3553 * Try to get the locks (out of order)
3554 */
3555 if (src_object_shared == TRUE)
3556 lock_granted = vm_object_lock_try_shared(old_copy);
3557 else
3558 lock_granted = vm_object_lock_try(old_copy);
3559
3560 if (!lock_granted) {
3561 vm_object_unlock(src_object);
3562
3563 if (collisions++ == 0)
3564 copy_delayed_lock_contention++;
3565 mutex_pause(collisions);
3566
3567 /* Heisenberg Rules */
3568 copy_delayed_lock_collisions++;
3569
3570 if (collisions > copy_delayed_max_collisions)
3571 copy_delayed_max_collisions = collisions;
3572
3573 if (src_object_shared == TRUE)
3574 vm_object_lock_shared(src_object);
3575 else
3576 vm_object_lock(src_object);
3577
3578 goto Retry;
3579 }
3580
3581 /*
3582 * Determine whether the old copy object has
3583 * been modified.
3584 */
3585
3586 if (old_copy->resident_page_count == 0 &&
3587 !old_copy->pager_created) {
3588 /*
3589 * It has not been modified.
3590 *
3591 * Return another reference to
3592 * the existing copy-object if
3593 * we can safely grow it (if
3594 * needed).
3595 */
3596
3597 if (old_copy->vo_size < copy_size) {
3598 if (src_object_shared == TRUE) {
3599 vm_object_unlock(old_copy);
3600 vm_object_unlock(src_object);
3601
3602 vm_object_lock(src_object);
3603 src_object_shared = FALSE;
3604 goto Retry;
3605 }
3606 /*
3607 * We can't perform a delayed copy if any of the
3608 * pages in the extended range are wired (because
3609 * we can't safely take write permission away from
3610 * wired pages). If the pages aren't wired, then
3611 * go ahead and protect them.
3612 */
3613 copy_delayed_protect_iterate++;
3614
3615 queue_iterate(&src_object->memq, p, vm_page_t, listq) {
3616 if (!p->fictitious &&
3617 p->offset >= old_copy->vo_size &&
3618 p->offset < copy_size) {
3619 if (VM_PAGE_WIRED(p)) {
3620 vm_object_unlock(old_copy);
3621 vm_object_unlock(src_object);
3622
3623 if (new_copy != VM_OBJECT_NULL) {
3624 vm_object_unlock(new_copy);
3625 vm_object_deallocate(new_copy);
3626 }
3627
3628 return VM_OBJECT_NULL;
3629 } else {
3630 pmap_page_protect(p->phys_page,
3631 (VM_PROT_ALL & ~VM_PROT_WRITE));
3632 }
3633 }
3634 }
3635 old_copy->vo_size = copy_size;
3636 }
3637 if (src_object_shared == TRUE)
3638 vm_object_reference_shared(old_copy);
3639 else
3640 vm_object_reference_locked(old_copy);
3641 vm_object_unlock(old_copy);
3642 vm_object_unlock(src_object);
3643
3644 if (new_copy != VM_OBJECT_NULL) {
3645 vm_object_unlock(new_copy);
3646 vm_object_deallocate(new_copy);
3647 }
3648 return(old_copy);
3649 }
3650
3651
3652
3653 /*
3654 * Adjust the size argument so that the newly-created
3655 * copy object will be large enough to back either the
3656 * old copy object or the new mapping.
3657 */
3658 if (old_copy->vo_size > copy_size)
3659 copy_size = old_copy->vo_size;
3660
3661 if (new_copy == VM_OBJECT_NULL) {
3662 vm_object_unlock(old_copy);
3663 vm_object_unlock(src_object);
3664 new_copy = vm_object_allocate(copy_size);
3665 vm_object_lock(src_object);
3666 vm_object_lock(new_copy);
3667
3668 src_object_shared = FALSE;
3669 goto Retry;
3670 }
3671 new_copy->vo_size = copy_size;
3672
3673 /*
3674 * The copy-object is always made large enough to
3675 * completely shadow the original object, since
3676 * it may have several users who want to shadow
3677 * the original object at different points.
3678 */
3679
3680 assert((old_copy->shadow == src_object) &&
3681 (old_copy->vo_shadow_offset == (vm_object_offset_t) 0));
3682
3683 } else if (new_copy == VM_OBJECT_NULL) {
3684 vm_object_unlock(src_object);
3685 new_copy = vm_object_allocate(copy_size);
3686 vm_object_lock(src_object);
3687 vm_object_lock(new_copy);
3688
3689 src_object_shared = FALSE;
3690 goto Retry;
3691 }
3692
3693 /*
3694 * We now have the src object locked, and the new copy object
3695 * allocated and locked (and potentially the old copy locked).
3696 * Before we go any further, make sure we can still perform
3697 * a delayed copy, as the situation may have changed.
3698 *
3699 * Specifically, we can't perform a delayed copy if any of the
3700 * pages in the range are wired (because we can't safely take
3701 * write permission away from wired pages). If the pages aren't
3702 * wired, then go ahead and protect them.
3703 */
3704 copy_delayed_protect_iterate++;
3705
3706 queue_iterate(&src_object->memq, p, vm_page_t, listq) {
3707 if (!p->fictitious && p->offset < copy_size) {
3708 if (VM_PAGE_WIRED(p)) {
3709 if (old_copy)
3710 vm_object_unlock(old_copy);
3711 vm_object_unlock(src_object);
3712 vm_object_unlock(new_copy);
3713 vm_object_deallocate(new_copy);
3714 return VM_OBJECT_NULL;
3715 } else {
3716 pmap_page_protect(p->phys_page,
3717 (VM_PROT_ALL & ~VM_PROT_WRITE));
3718 }
3719 }
3720 }
3721 if (old_copy != VM_OBJECT_NULL) {
3722 /*
3723 * Make the old copy-object shadow the new one.
3724 * It will receive no more pages from the original
3725 * object.
3726 */
3727
3728 /* remove ref. from old_copy */
3729 vm_object_lock_assert_exclusive(src_object);
3730 src_object->ref_count--;
3731 assert(src_object->ref_count > 0);
3732 vm_object_lock_assert_exclusive(old_copy);
3733 old_copy->shadow = new_copy;
3734 vm_object_lock_assert_exclusive(new_copy);
3735 assert(new_copy->ref_count > 0);
3736 new_copy->ref_count++; /* for old_copy->shadow ref. */
3737
3738 #if TASK_SWAPPER
3739 if (old_copy->res_count) {
3740 VM_OBJ_RES_INCR(new_copy);
3741 VM_OBJ_RES_DECR(src_object);
3742 }
3743 #endif
3744
3745 vm_object_unlock(old_copy); /* done with old_copy */
3746 }
3747
3748 /*
3749 * Point the new copy at the existing object.
3750 */
3751 vm_object_lock_assert_exclusive(new_copy);
3752 new_copy->shadow = src_object;
3753 new_copy->vo_shadow_offset = 0;
3754 new_copy->shadowed = TRUE; /* caller must set needs_copy */
3755
3756 vm_object_lock_assert_exclusive(src_object);
3757 vm_object_reference_locked(src_object);
3758 src_object->copy = new_copy;
3759 vm_object_unlock(src_object);
3760 vm_object_unlock(new_copy);
3761
3762 XPR(XPR_VM_OBJECT,
3763 "vm_object_copy_delayed: used copy object %X for source %X\n",
3764 new_copy, src_object, 0, 0, 0);
3765
3766 return new_copy;
3767 }
3768
3769 /*
3770 * Routine: vm_object_copy_strategically
3771 *
3772 * Purpose:
3773 * Perform a copy according to the source object's
3774 * declared strategy. This operation may block,
3775 * and may be interrupted.
3776 */
3777 __private_extern__ kern_return_t
3778 vm_object_copy_strategically(
3779 register vm_object_t src_object,
3780 vm_object_offset_t src_offset,
3781 vm_object_size_t size,
3782 vm_object_t *dst_object, /* OUT */
3783 vm_object_offset_t *dst_offset, /* OUT */
3784 boolean_t *dst_needs_copy) /* OUT */
3785 {
3786 boolean_t result;
3787 boolean_t interruptible = THREAD_ABORTSAFE; /* XXX */
3788 boolean_t object_lock_shared = FALSE;
3789 memory_object_copy_strategy_t copy_strategy;
3790
3791 assert(src_object != VM_OBJECT_NULL);
3792
3793 copy_strategy = src_object->copy_strategy;
3794
3795 if (copy_strategy == MEMORY_OBJECT_COPY_DELAY) {
3796 vm_object_lock_shared(src_object);
3797 object_lock_shared = TRUE;
3798 } else
3799 vm_object_lock(src_object);
3800
3801 /*
3802 * The copy strategy is only valid if the memory manager
3803 * is "ready". Internal objects are always ready.
3804 */
3805
3806 while (!src_object->internal && !src_object->pager_ready) {
3807 wait_result_t wait_result;
3808
3809 if (object_lock_shared == TRUE) {
3810 vm_object_unlock(src_object);
3811 vm_object_lock(src_object);
3812 object_lock_shared = FALSE;
3813 continue;
3814 }
3815 wait_result = vm_object_sleep( src_object,
3816 VM_OBJECT_EVENT_PAGER_READY,
3817 interruptible);
3818 if (wait_result != THREAD_AWAKENED) {
3819 vm_object_unlock(src_object);
3820 *dst_object = VM_OBJECT_NULL;
3821 *dst_offset = 0;
3822 *dst_needs_copy = FALSE;
3823 return(MACH_SEND_INTERRUPTED);
3824 }
3825 }
3826
3827 /*
3828 * Use the appropriate copy strategy.
3829 */
3830
3831 switch (copy_strategy) {
3832 case MEMORY_OBJECT_COPY_DELAY:
3833 *dst_object = vm_object_copy_delayed(src_object,
3834 src_offset, size, object_lock_shared);
3835 if (*dst_object != VM_OBJECT_NULL) {
3836 *dst_offset = src_offset;
3837 *dst_needs_copy = TRUE;
3838 result = KERN_SUCCESS;
3839 break;
3840 }
3841 vm_object_lock(src_object);
3842 /* fall thru when delayed copy not allowed */
3843
3844 case MEMORY_OBJECT_COPY_NONE:
3845 result = vm_object_copy_slowly(src_object, src_offset, size,
3846 interruptible, dst_object);
3847 if (result == KERN_SUCCESS) {
3848 *dst_offset = 0;
3849 *dst_needs_copy = FALSE;
3850 }
3851 break;
3852
3853 case MEMORY_OBJECT_COPY_CALL:
3854 result = vm_object_copy_call(src_object, src_offset, size,
3855 dst_object);
3856 if (result == KERN_SUCCESS) {
3857 *dst_offset = src_offset;
3858 *dst_needs_copy = TRUE;
3859 }
3860 break;
3861
3862 case MEMORY_OBJECT_COPY_SYMMETRIC:
3863 XPR(XPR_VM_OBJECT, "v_o_c_strategically obj 0x%x off 0x%x size 0x%x\n", src_object, src_offset, size, 0, 0);
3864 vm_object_unlock(src_object);
3865 result = KERN_MEMORY_RESTART_COPY;
3866 break;
3867
3868 default:
3869 panic("copy_strategically: bad strategy");
3870 result = KERN_INVALID_ARGUMENT;
3871 }
3872 return(result);
3873 }
3874
3875 /*
3876 * vm_object_shadow:
3877 *
3878 * Create a new object which is backed by the
3879 * specified existing object range. The source
3880 * object reference is deallocated.
3881 *
3882 * The new object and offset into that object
3883 * are returned in the source parameters.
3884 */
3885 boolean_t vm_object_shadow_check = TRUE;
3886
3887 __private_extern__ boolean_t
3888 vm_object_shadow(
3889 vm_object_t *object, /* IN/OUT */
3890 vm_object_offset_t *offset, /* IN/OUT */
3891 vm_object_size_t length)
3892 {
3893 register vm_object_t source;
3894 register vm_object_t result;
3895
3896 source = *object;
3897 assert(source != VM_OBJECT_NULL);
3898 if (source == VM_OBJECT_NULL)
3899 return FALSE;
3900
3901 #if 0
3902 /*
3903 * XXX FBDP
3904 * This assertion is valid but it gets triggered by Rosetta for example
3905 * due to a combination of vm_remap() that changes a VM object's
3906 * copy_strategy from SYMMETRIC to DELAY and vm_protect(VM_PROT_COPY)
3907 * that then sets "needs_copy" on its map entry. This creates a
3908 * mapping situation that VM should never see and doesn't know how to
3909 * handle.
3910 * It's not clear if this can create any real problem but we should
3911 * look into fixing this, probably by having vm_protect(VM_PROT_COPY)
3912 * do more than just set "needs_copy" to handle the copy-on-write...
3913 * In the meantime, let's disable the assertion.
3914 */
3915 assert(source->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC);
3916 #endif
3917
3918 /*
3919 * Determine if we really need a shadow.
3920 *
3921 * If the source object is larger than what we are trying
3922 * to create, then force the shadow creation even if the
3923 * ref count is 1. This will allow us to [potentially]
3924 * collapse the underlying object away in the future
3925 * (freeing up the extra data it might contain and that
3926 * we don't need).
3927 */
3928 if (vm_object_shadow_check &&
3929 source->vo_size == length &&
3930 source->ref_count == 1 &&
3931 (source->shadow == VM_OBJECT_NULL ||
3932 source->shadow->copy == VM_OBJECT_NULL) )
3933 {
3934 source->shadowed = FALSE;
3935 return FALSE;
3936 }
3937
3938 /*
3939 * Allocate a new object with the given length
3940 */
3941
3942 if ((result = vm_object_allocate(length)) == VM_OBJECT_NULL)
3943 panic("vm_object_shadow: no object for shadowing");
3944
3945 /*
3946 * The new object shadows the source object, adding
3947 * a reference to it. Our caller changes his reference
3948 * to point to the new object, removing a reference to
3949 * the source object. Net result: no change of reference
3950 * count.
3951 */
3952 result->shadow = source;
3953
3954 /*
3955 * Store the offset into the source object,
3956 * and fix up the offset into the new object.
3957 */
3958
3959 result->vo_shadow_offset = *offset;
3960
3961 /*
3962 * Return the new things
3963 */
3964
3965 *offset = 0;
3966 *object = result;
3967 return TRUE;
3968 }
3969
3970 /*
3971 * The relationship between vm_object structures and
3972 * the memory_object requires careful synchronization.
3973 *
3974 * All associations are created by memory_object_create_named
3975 * for external pagers and vm_object_pager_create for internal
3976 * objects as follows:
3977 *
3978 * pager: the memory_object itself, supplied by
3979 * the user requesting a mapping (or the kernel,
3980 * when initializing internal objects); the
3981 * kernel simulates holding send rights by keeping
3982 * a port reference;
3983 *
3984 * pager_request:
3985 * the memory object control port,
3986 * created by the kernel; the kernel holds
3987 * receive (and ownership) rights to this
3988 * port, but no other references.
3989 *
3990 * When initialization is complete, the "initialized" field
3991 * is asserted. Other mappings using a particular memory object,
3992 * and any references to the vm_object gained through the
3993 * port association must wait for this initialization to occur.
3994 *
3995 * In order to allow the memory manager to set attributes before
3996 * requests (notably virtual copy operations, but also data or
3997 * unlock requests) are made, a "ready" attribute is made available.
3998 * Only the memory manager may affect the value of this attribute.
3999 * Its value does not affect critical kernel functions, such as
4000 * internal object initialization or destruction. [Furthermore,
4001 * memory objects created by the kernel are assumed to be ready
4002 * immediately; the default memory manager need not explicitly
4003 * set the "ready" attribute.]
4004 *
4005 * [Both the "initialized" and "ready" attribute wait conditions
4006 * use the "pager" field as the wait event.]
4007 *
4008 * The port associations can be broken down by any of the
4009 * following routines:
4010 * vm_object_terminate:
4011 * No references to the vm_object remain, and
4012 * the object cannot (or will not) be cached.
4013 * This is the normal case, and is done even
4014 * though one of the other cases has already been
4015 * done.
4016 * memory_object_destroy:
4017 * The memory manager has requested that the
4018 * kernel relinquish references to the memory
4019 * object. [The memory manager may not want to
4020 * destroy the memory object, but may wish to
4021 * refuse or tear down existing memory mappings.]
4022 *
4023 * Each routine that breaks an association must break all of
4024 * them at once. At some later time, that routine must clear
4025 * the pager field and release the memory object references.
4026 * [Furthermore, each routine must cope with the simultaneous
4027 * or previous operations of the others.]
4028 *
4029 * In addition to the lock on the object, the vm_object_hash_lock
4030 * governs the associations. References gained through the
4031 * association require use of the hash lock.
4032 *
4033 * Because the pager field may be cleared spontaneously, it
4034 * cannot be used to determine whether a memory object has
4035 * ever been associated with a particular vm_object. [This
4036 * knowledge is important to the shadow object mechanism.]
4037 * For this reason, an additional "created" attribute is
4038 * provided.
4039 *
4040 * During various paging operations, the pager reference found in the
4041 * vm_object must be valid. To prevent this from being released,
4042 * (other than being removed, i.e., made null), routines may use
4043 * the vm_object_paging_begin/end routines [actually, macros].
4044 * The implementation uses the "paging_in_progress" and "wanted" fields.
4045 * [Operations that alter the validity of the pager values include the
4046 * termination routines and vm_object_collapse.]
4047 */
4048
4049
4050 /*
4051 * Routine: vm_object_enter
4052 * Purpose:
4053 * Find a VM object corresponding to the given
4054 * pager; if no such object exists, create one,
4055 * and initialize the pager.
4056 */
4057 vm_object_t
4058 vm_object_enter(
4059 memory_object_t pager,
4060 vm_object_size_t size,
4061 boolean_t internal,
4062 boolean_t init,
4063 boolean_t named)
4064 {
4065 register vm_object_t object;
4066 vm_object_t new_object;
4067 boolean_t must_init;
4068 vm_object_hash_entry_t entry, new_entry;
4069 uint32_t try_failed_count = 0;
4070 lck_mtx_t *lck;
4071
4072 if (pager == MEMORY_OBJECT_NULL)
4073 return(vm_object_allocate(size));
4074
4075 new_object = VM_OBJECT_NULL;
4076 new_entry = VM_OBJECT_HASH_ENTRY_NULL;
4077 must_init = init;
4078
4079 /*
4080 * Look for an object associated with this port.
4081 */
4082 Retry:
4083 lck = vm_object_hash_lock_spin(pager);
4084 do {
4085 entry = vm_object_hash_lookup(pager, FALSE);
4086
4087 if (entry == VM_OBJECT_HASH_ENTRY_NULL) {
4088 if (new_object == VM_OBJECT_NULL) {
4089 /*
4090 * We must unlock to create a new object;
4091 * if we do so, we must try the lookup again.
4092 */
4093 vm_object_hash_unlock(lck);
4094 assert(new_entry == VM_OBJECT_HASH_ENTRY_NULL);
4095 new_entry = vm_object_hash_entry_alloc(pager);
4096 new_object = vm_object_allocate(size);
4097 lck = vm_object_hash_lock_spin(pager);
4098 } else {
4099 /*
4100 * Lookup failed twice, and we have something
4101 * to insert; set the object.
4102 */
4103 vm_object_hash_insert(new_entry, new_object);
4104 entry = new_entry;
4105 new_entry = VM_OBJECT_HASH_ENTRY_NULL;
4106 new_object = VM_OBJECT_NULL;
4107 must_init = TRUE;
4108 }
4109 } else if (entry->object == VM_OBJECT_NULL) {
4110 /*
4111 * If a previous object is being terminated,
4112 * we must wait for the termination message
4113 * to be queued (and lookup the entry again).
4114 */
4115 entry->waiting = TRUE;
4116 entry = VM_OBJECT_HASH_ENTRY_NULL;
4117 assert_wait((event_t) pager, THREAD_UNINT);
4118 vm_object_hash_unlock(lck);
4119
4120 thread_block(THREAD_CONTINUE_NULL);
4121 lck = vm_object_hash_lock_spin(pager);
4122 }
4123 } while (entry == VM_OBJECT_HASH_ENTRY_NULL);
4124
4125 object = entry->object;
4126 assert(object != VM_OBJECT_NULL);
4127
4128 if (!must_init) {
4129 if ( !vm_object_lock_try(object)) {
4130
4131 vm_object_hash_unlock(lck);
4132
4133 try_failed_count++;
4134 mutex_pause(try_failed_count); /* wait a bit */
4135 goto Retry;
4136 }
4137 assert(!internal || object->internal);
4138 #if VM_OBJECT_CACHE
4139 if (object->ref_count == 0) {
4140 if ( !vm_object_cache_lock_try()) {
4141
4142 vm_object_hash_unlock(lck);
4143 vm_object_unlock(object);
4144
4145 try_failed_count++;
4146 mutex_pause(try_failed_count); /* wait a bit */
4147 goto Retry;
4148 }
4149 XPR(XPR_VM_OBJECT_CACHE,
4150 "vm_object_enter: removing %x from cache, head (%x, %x)\n",
4151 object,
4152 vm_object_cached_list.next,
4153 vm_object_cached_list.prev, 0,0);
4154 queue_remove(&vm_object_cached_list, object,
4155 vm_object_t, cached_list);
4156 vm_object_cached_count--;
4157
4158 vm_object_cache_unlock();
4159 }
4160 #endif
4161 if (named) {
4162 assert(!object->named);
4163 object->named = TRUE;
4164 }
4165 vm_object_lock_assert_exclusive(object);
4166 object->ref_count++;
4167 vm_object_res_reference(object);
4168
4169 vm_object_hash_unlock(lck);
4170 vm_object_unlock(object);
4171
4172 VM_STAT_INCR(hits);
4173 } else
4174 vm_object_hash_unlock(lck);
4175
4176 assert(object->ref_count > 0);
4177
4178 VM_STAT_INCR(lookups);
4179
4180 XPR(XPR_VM_OBJECT,
4181 "vm_o_enter: pager 0x%x obj 0x%x must_init %d\n",
4182 pager, object, must_init, 0, 0);
4183
4184 /*
4185 * If we raced to create a vm_object but lost, let's
4186 * throw away ours.
4187 */
4188
4189 if (new_object != VM_OBJECT_NULL)
4190 vm_object_deallocate(new_object);
4191
4192 if (new_entry != VM_OBJECT_HASH_ENTRY_NULL)
4193 vm_object_hash_entry_free(new_entry);
4194
4195 if (must_init) {
4196 memory_object_control_t control;
4197
4198 /*
4199 * Allocate request port.
4200 */
4201
4202 control = memory_object_control_allocate(object);
4203 assert (control != MEMORY_OBJECT_CONTROL_NULL);
4204
4205 vm_object_lock(object);
4206 assert(object != kernel_object);
4207
4208 /*
4209 * Copy the reference we were given.
4210 */
4211
4212 memory_object_reference(pager);
4213 object->pager_created = TRUE;
4214 object->pager = pager;
4215 object->internal = internal;
4216 object->pager_trusted = internal;
4217 if (!internal) {
4218 /* copy strategy invalid until set by memory manager */
4219 object->copy_strategy = MEMORY_OBJECT_COPY_INVALID;
4220 }
4221 object->pager_control = control;
4222 object->pager_ready = FALSE;
4223
4224 vm_object_unlock(object);
4225
4226 /*
4227 * Let the pager know we're using it.
4228 */
4229
4230 (void) memory_object_init(pager,
4231 object->pager_control,
4232 PAGE_SIZE);
4233
4234 vm_object_lock(object);
4235 if (named)
4236 object->named = TRUE;
4237 if (internal) {
4238 object->pager_ready = TRUE;
4239 vm_object_wakeup(object, VM_OBJECT_EVENT_PAGER_READY);
4240 }
4241
4242 object->pager_initialized = TRUE;
4243 vm_object_wakeup(object, VM_OBJECT_EVENT_INITIALIZED);
4244 } else {
4245 vm_object_lock(object);
4246 }
4247
4248 /*
4249 * [At this point, the object must be locked]
4250 */
4251
4252 /*
4253 * Wait for the work above to be done by the first
4254 * thread to map this object.
4255 */
4256
4257 while (!object->pager_initialized) {
4258 vm_object_sleep(object,
4259 VM_OBJECT_EVENT_INITIALIZED,
4260 THREAD_UNINT);
4261 }
4262 vm_object_unlock(object);
4263
4264 XPR(XPR_VM_OBJECT,
4265 "vm_object_enter: vm_object %x, memory_object %x, internal %d\n",
4266 object, object->pager, internal, 0,0);
4267 return(object);
4268 }
4269
4270 /*
4271 * Routine: vm_object_pager_create
4272 * Purpose:
4273 * Create a memory object for an internal object.
4274 * In/out conditions:
4275 * The object is locked on entry and exit;
4276 * it may be unlocked within this call.
4277 * Limitations:
4278 * Only one thread may be performing a
4279 * vm_object_pager_create on an object at
4280 * a time. Presumably, only the pageout
4281 * daemon will be using this routine.
4282 */
4283
4284 void
4285 vm_object_pager_create(
4286 register vm_object_t object)
4287 {
4288 memory_object_t pager;
4289 vm_object_hash_entry_t entry;
4290 lck_mtx_t *lck;
4291 #if MACH_PAGEMAP
4292 vm_object_size_t size;
4293 vm_external_map_t map;
4294 #endif /* MACH_PAGEMAP */
4295
4296 XPR(XPR_VM_OBJECT, "vm_object_pager_create, object 0x%X\n",
4297 object, 0,0,0,0);
4298
4299 assert(object != kernel_object);
4300
4301 if (memory_manager_default_check() != KERN_SUCCESS)
4302 return;
4303
4304 /*
4305 * Prevent collapse or termination by holding a paging reference
4306 */
4307
4308 vm_object_paging_begin(object);
4309 if (object->pager_created) {
4310 /*
4311 * Someone else got to it first...
4312 * wait for them to finish initializing the ports
4313 */
4314 while (!object->pager_initialized) {
4315 vm_object_sleep(object,
4316 VM_OBJECT_EVENT_INITIALIZED,
4317 THREAD_UNINT);
4318 }
4319 vm_object_paging_end(object);
4320 return;
4321 }
4322
4323 /*
4324 * Indicate that a memory object has been assigned
4325 * before dropping the lock, to prevent a race.
4326 */
4327
4328 object->pager_created = TRUE;
4329 object->paging_offset = 0;
4330
4331 #if MACH_PAGEMAP
4332 size = object->vo_size;
4333 #endif /* MACH_PAGEMAP */
4334 vm_object_unlock(object);
4335
4336 #if MACH_PAGEMAP
4337 map = vm_external_create(size);
4338 vm_object_lock(object);
4339 assert(object->vo_size == size);
4340 object->existence_map = map;
4341 vm_object_unlock(object);
4342 #endif /* MACH_PAGEMAP */
4343
4344 if ((uint32_t) object->vo_size != object->vo_size) {
4345 panic("vm_object_pager_create(): object size 0x%llx >= 4GB\n",
4346 (uint64_t) object->vo_size);
4347 }
4348
4349 /*
4350 * Create the [internal] pager, and associate it with this object.
4351 *
4352 * We make the association here so that vm_object_enter()
4353 * can look up the object to complete initializing it. No
4354 * user will ever map this object.
4355 */
4356 {
4357 memory_object_default_t dmm;
4358
4359 /* acquire a reference for the default memory manager */
4360 dmm = memory_manager_default_reference();
4361
4362 assert(object->temporary);
4363
4364 /* create our new memory object */
4365 assert((vm_size_t) object->vo_size == object->vo_size);
4366 (void) memory_object_create(dmm, (vm_size_t) object->vo_size,
4367 &pager);
4368
4369 memory_object_default_deallocate(dmm);
4370 }
4371
4372 entry = vm_object_hash_entry_alloc(pager);
4373
4374 lck = vm_object_hash_lock_spin(pager);
4375 vm_object_hash_insert(entry, object);
4376 vm_object_hash_unlock(lck);
4377
4378 /*
4379 * A reference was returned by
4380 * memory_object_create(), and it is
4381 * copied by vm_object_enter().
4382 */
4383
4384 if (vm_object_enter(pager, object->vo_size, TRUE, TRUE, FALSE) != object)
4385 panic("vm_object_pager_create: mismatch");
4386
4387 /*
4388 * Drop the reference we were passed.
4389 */
4390 memory_object_deallocate(pager);
4391
4392 vm_object_lock(object);
4393
4394 /*
4395 * Release the paging reference
4396 */
4397 vm_object_paging_end(object);
4398 }
4399
4400 /*
4401 * Routine: vm_object_remove
4402 * Purpose:
4403 * Eliminate the pager/object association
4404 * for this pager.
4405 * Conditions:
4406 * The object cache must be locked.
4407 */
4408 __private_extern__ void
4409 vm_object_remove(
4410 vm_object_t object)
4411 {
4412 memory_object_t pager;
4413
4414 if ((pager = object->pager) != MEMORY_OBJECT_NULL) {
4415 vm_object_hash_entry_t entry;
4416
4417 entry = vm_object_hash_lookup(pager, FALSE);
4418 if (entry != VM_OBJECT_HASH_ENTRY_NULL)
4419 entry->object = VM_OBJECT_NULL;
4420 }
4421
4422 }
4423
4424 /*
4425 * Global variables for vm_object_collapse():
4426 *
4427 * Counts for normal collapses and bypasses.
4428 * Debugging variables, to watch or disable collapse.
4429 */
4430 static long object_collapses = 0;
4431 static long object_bypasses = 0;
4432
4433 static boolean_t vm_object_collapse_allowed = TRUE;
4434 static boolean_t vm_object_bypass_allowed = TRUE;
4435
4436 #if MACH_PAGEMAP
4437 static int vm_external_discarded;
4438 static int vm_external_collapsed;
4439 #endif
4440
4441 unsigned long vm_object_collapse_encrypted = 0;
4442
4443 /*
4444 * Routine: vm_object_do_collapse
4445 * Purpose:
4446 * Collapse an object with the object backing it.
4447 * Pages in the backing object are moved into the
4448 * parent, and the backing object is deallocated.
4449 * Conditions:
4450 * Both objects and the cache are locked; the page
4451 * queues are unlocked.
4452 *
4453 */
4454 static void
4455 vm_object_do_collapse(
4456 vm_object_t object,
4457 vm_object_t backing_object)
4458 {
4459 vm_page_t p, pp;
4460 vm_object_offset_t new_offset, backing_offset;
4461 vm_object_size_t size;
4462
4463 vm_object_lock_assert_exclusive(object);
4464 vm_object_lock_assert_exclusive(backing_object);
4465
4466 backing_offset = object->vo_shadow_offset;
4467 size = object->vo_size;
4468
4469 /*
4470 * Move all in-memory pages from backing_object
4471 * to the parent. Pages that have been paged out
4472 * will be overwritten by any of the parent's
4473 * pages that shadow them.
4474 */
4475
4476 while (!queue_empty(&backing_object->memq)) {
4477
4478 p = (vm_page_t) queue_first(&backing_object->memq);
4479
4480 new_offset = (p->offset - backing_offset);
4481
4482 assert(!p->busy || p->absent);
4483
4484 /*
4485 * If the parent has a page here, or if
4486 * this page falls outside the parent,
4487 * dispose of it.
4488 *
4489 * Otherwise, move it as planned.
4490 */
4491
4492 if (p->offset < backing_offset || new_offset >= size) {
4493 VM_PAGE_FREE(p);
4494 } else {
4495 /*
4496 * ENCRYPTED SWAP:
4497 * The encryption key includes the "pager" and the
4498 * "paging_offset". These will not change during the
4499 * object collapse, so we can just move an encrypted
4500 * page from one object to the other in this case.
4501 * We can't decrypt the page here, since we can't drop
4502 * the object lock.
4503 */
4504 if (p->encrypted) {
4505 vm_object_collapse_encrypted++;
4506 }
4507 pp = vm_page_lookup(object, new_offset);
4508 if (pp == VM_PAGE_NULL) {
4509
4510 /*
4511 * Parent now has no page.
4512 * Move the backing object's page up.
4513 */
4514
4515 vm_page_rename(p, object, new_offset, TRUE);
4516 #if MACH_PAGEMAP
4517 } else if (pp->absent) {
4518
4519 /*
4520 * Parent has an absent page...
4521 * it's not being paged in, so
4522 * it must really be missing from
4523 * the parent.
4524 *
4525 * Throw out the absent page...
4526 * any faults looking for that
4527 * page will restart with the new
4528 * one.
4529 */
4530
4531 VM_PAGE_FREE(pp);
4532 vm_page_rename(p, object, new_offset, TRUE);
4533 #endif /* MACH_PAGEMAP */
4534 } else {
4535 assert(! pp->absent);
4536
4537 /*
4538 * Parent object has a real page.
4539 * Throw away the backing object's
4540 * page.
4541 */
4542 VM_PAGE_FREE(p);
4543 }
4544 }
4545 }
4546
4547 #if !MACH_PAGEMAP
4548 assert((!object->pager_created && (object->pager == MEMORY_OBJECT_NULL))
4549 || (!backing_object->pager_created
4550 && (backing_object->pager == MEMORY_OBJECT_NULL)));
4551 #else
4552 assert(!object->pager_created && object->pager == MEMORY_OBJECT_NULL);
4553 #endif /* !MACH_PAGEMAP */
4554
4555 if (backing_object->pager != MEMORY_OBJECT_NULL) {
4556 vm_object_hash_entry_t entry;
4557
4558 /*
4559 * Move the pager from backing_object to object.
4560 *
4561 * XXX We're only using part of the paging space
4562 * for keeps now... we ought to discard the
4563 * unused portion.
4564 */
4565
4566 assert(!object->paging_in_progress);
4567 assert(!object->activity_in_progress);
4568 object->pager = backing_object->pager;
4569
4570 if (backing_object->hashed) {
4571 lck_mtx_t *lck;
4572
4573 lck = vm_object_hash_lock_spin(backing_object->pager);
4574 entry = vm_object_hash_lookup(object->pager, FALSE);
4575 assert(entry != VM_OBJECT_HASH_ENTRY_NULL);
4576 entry->object = object;
4577 vm_object_hash_unlock(lck);
4578
4579 object->hashed = TRUE;
4580 }
4581 object->pager_created = backing_object->pager_created;
4582 object->pager_control = backing_object->pager_control;
4583 object->pager_ready = backing_object->pager_ready;
4584 object->pager_initialized = backing_object->pager_initialized;
4585 object->paging_offset =
4586 backing_object->paging_offset + backing_offset;
4587 if (object->pager_control != MEMORY_OBJECT_CONTROL_NULL) {
4588 memory_object_control_collapse(object->pager_control,
4589 object);
4590 }
4591 }
4592
4593 #if MACH_PAGEMAP
4594 /*
4595 * If the shadow offset is 0, the use the existence map from
4596 * the backing object if there is one. If the shadow offset is
4597 * not zero, toss it.
4598 *
4599 * XXX - If the shadow offset is not 0 then a bit copy is needed
4600 * if the map is to be salvaged. For now, we just just toss the
4601 * old map, giving the collapsed object no map. This means that
4602 * the pager is invoked for zero fill pages. If analysis shows
4603 * that this happens frequently and is a performance hit, then
4604 * this code should be fixed to salvage the map.
4605 */
4606 assert(object->existence_map == VM_EXTERNAL_NULL);
4607 if (backing_offset || (size != backing_object->vo_size)) {
4608 vm_external_discarded++;
4609 vm_external_destroy(backing_object->existence_map,
4610 backing_object->vo_size);
4611 }
4612 else {
4613 vm_external_collapsed++;
4614 object->existence_map = backing_object->existence_map;
4615 }
4616 backing_object->existence_map = VM_EXTERNAL_NULL;
4617 #endif /* MACH_PAGEMAP */
4618
4619 /*
4620 * Object now shadows whatever backing_object did.
4621 * Note that the reference to backing_object->shadow
4622 * moves from within backing_object to within object.
4623 */
4624
4625 assert(!object->phys_contiguous);
4626 assert(!backing_object->phys_contiguous);
4627 object->shadow = backing_object->shadow;
4628 if (object->shadow) {
4629 object->vo_shadow_offset += backing_object->vo_shadow_offset;
4630 } else {
4631 /* no shadow, therefore no shadow offset... */
4632 object->vo_shadow_offset = 0;
4633 }
4634 assert((object->shadow == VM_OBJECT_NULL) ||
4635 (object->shadow->copy != backing_object));
4636
4637 /*
4638 * Discard backing_object.
4639 *
4640 * Since the backing object has no pages, no
4641 * pager left, and no object references within it,
4642 * all that is necessary is to dispose of it.
4643 */
4644
4645 assert((backing_object->ref_count == 1) &&
4646 (backing_object->resident_page_count == 0) &&
4647 (backing_object->paging_in_progress == 0) &&
4648 (backing_object->activity_in_progress == 0));
4649
4650 backing_object->alive = FALSE;
4651 vm_object_unlock(backing_object);
4652
4653 XPR(XPR_VM_OBJECT, "vm_object_collapse, collapsed 0x%X\n",
4654 backing_object, 0,0,0,0);
4655
4656 vm_object_lock_destroy(backing_object);
4657
4658 zfree(vm_object_zone, backing_object);
4659
4660 object_collapses++;
4661 }
4662
4663 static void
4664 vm_object_do_bypass(
4665 vm_object_t object,
4666 vm_object_t backing_object)
4667 {
4668 /*
4669 * Make the parent shadow the next object
4670 * in the chain.
4671 */
4672
4673 vm_object_lock_assert_exclusive(object);
4674 vm_object_lock_assert_exclusive(backing_object);
4675
4676 #if TASK_SWAPPER
4677 /*
4678 * Do object reference in-line to
4679 * conditionally increment shadow's
4680 * residence count. If object is not
4681 * resident, leave residence count
4682 * on shadow alone.
4683 */
4684 if (backing_object->shadow != VM_OBJECT_NULL) {
4685 vm_object_lock(backing_object->shadow);
4686 vm_object_lock_assert_exclusive(backing_object->shadow);
4687 backing_object->shadow->ref_count++;
4688 if (object->res_count != 0)
4689 vm_object_res_reference(backing_object->shadow);
4690 vm_object_unlock(backing_object->shadow);
4691 }
4692 #else /* TASK_SWAPPER */
4693 vm_object_reference(backing_object->shadow);
4694 #endif /* TASK_SWAPPER */
4695
4696 assert(!object->phys_contiguous);
4697 assert(!backing_object->phys_contiguous);
4698 object->shadow = backing_object->shadow;
4699 if (object->shadow) {
4700 object->vo_shadow_offset += backing_object->vo_shadow_offset;
4701 } else {
4702 /* no shadow, therefore no shadow offset... */
4703 object->vo_shadow_offset = 0;
4704 }
4705
4706 /*
4707 * Backing object might have had a copy pointer
4708 * to us. If it did, clear it.
4709 */
4710 if (backing_object->copy == object) {
4711 backing_object->copy = VM_OBJECT_NULL;
4712 }
4713
4714 /*
4715 * Drop the reference count on backing_object.
4716 #if TASK_SWAPPER
4717 * Since its ref_count was at least 2, it
4718 * will not vanish; so we don't need to call
4719 * vm_object_deallocate.
4720 * [with a caveat for "named" objects]
4721 *
4722 * The res_count on the backing object is
4723 * conditionally decremented. It's possible
4724 * (via vm_pageout_scan) to get here with
4725 * a "swapped" object, which has a 0 res_count,
4726 * in which case, the backing object res_count
4727 * is already down by one.
4728 #else
4729 * Don't call vm_object_deallocate unless
4730 * ref_count drops to zero.
4731 *
4732 * The ref_count can drop to zero here if the
4733 * backing object could be bypassed but not
4734 * collapsed, such as when the backing object
4735 * is temporary and cachable.
4736 #endif
4737 */
4738 if (backing_object->ref_count > 2 ||
4739 (!backing_object->named && backing_object->ref_count > 1)) {
4740 vm_object_lock_assert_exclusive(backing_object);
4741 backing_object->ref_count--;
4742 #if TASK_SWAPPER
4743 if (object->res_count != 0)
4744 vm_object_res_deallocate(backing_object);
4745 assert(backing_object->ref_count > 0);
4746 #endif /* TASK_SWAPPER */
4747 vm_object_unlock(backing_object);
4748 } else {
4749
4750 /*
4751 * Drop locks so that we can deallocate
4752 * the backing object.
4753 */
4754
4755 #if TASK_SWAPPER
4756 if (object->res_count == 0) {
4757 /* XXX get a reference for the deallocate below */
4758 vm_object_res_reference(backing_object);
4759 }
4760 #endif /* TASK_SWAPPER */
4761 vm_object_unlock(object);
4762 vm_object_unlock(backing_object);
4763 vm_object_deallocate(backing_object);
4764
4765 /*
4766 * Relock object. We don't have to reverify
4767 * its state since vm_object_collapse will
4768 * do that for us as it starts at the
4769 * top of its loop.
4770 */
4771
4772 vm_object_lock(object);
4773 }
4774
4775 object_bypasses++;
4776 }
4777
4778
4779 /*
4780 * vm_object_collapse:
4781 *
4782 * Perform an object collapse or an object bypass if appropriate.
4783 * The real work of collapsing and bypassing is performed in
4784 * the routines vm_object_do_collapse and vm_object_do_bypass.
4785 *
4786 * Requires that the object be locked and the page queues be unlocked.
4787 *
4788 */
4789 static unsigned long vm_object_collapse_calls = 0;
4790 static unsigned long vm_object_collapse_objects = 0;
4791 static unsigned long vm_object_collapse_do_collapse = 0;
4792 static unsigned long vm_object_collapse_do_bypass = 0;
4793 static unsigned long vm_object_collapse_delays = 0;
4794 __private_extern__ void
4795 vm_object_collapse(
4796 register vm_object_t object,
4797 register vm_object_offset_t hint_offset,
4798 boolean_t can_bypass)
4799 {
4800 register vm_object_t backing_object;
4801 register unsigned int rcount;
4802 register unsigned int size;
4803 vm_object_t original_object;
4804 int object_lock_type;
4805 int backing_object_lock_type;
4806
4807 vm_object_collapse_calls++;
4808
4809 if (! vm_object_collapse_allowed &&
4810 ! (can_bypass && vm_object_bypass_allowed)) {
4811 return;
4812 }
4813
4814 XPR(XPR_VM_OBJECT, "vm_object_collapse, obj 0x%X\n",
4815 object, 0,0,0,0);
4816
4817 if (object == VM_OBJECT_NULL)
4818 return;
4819
4820 original_object = object;
4821
4822 /*
4823 * The top object was locked "exclusive" by the caller.
4824 * In the first pass, to determine if we can collapse the shadow chain,
4825 * take a "shared" lock on the shadow objects. If we can collapse,
4826 * we'll have to go down the chain again with exclusive locks.
4827 */
4828 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4829 backing_object_lock_type = OBJECT_LOCK_SHARED;
4830
4831 retry:
4832 object = original_object;
4833 vm_object_lock_assert_exclusive(object);
4834
4835 while (TRUE) {
4836 vm_object_collapse_objects++;
4837 /*
4838 * Verify that the conditions are right for either
4839 * collapse or bypass:
4840 */
4841
4842 /*
4843 * There is a backing object, and
4844 */
4845
4846 backing_object = object->shadow;
4847 if (backing_object == VM_OBJECT_NULL) {
4848 if (object != original_object) {
4849 vm_object_unlock(object);
4850 }
4851 return;
4852 }
4853 if (backing_object_lock_type == OBJECT_LOCK_SHARED) {
4854 vm_object_lock_shared(backing_object);
4855 } else {
4856 vm_object_lock(backing_object);
4857 }
4858
4859 /*
4860 * No pages in the object are currently
4861 * being paged out, and
4862 */
4863 if (object->paging_in_progress != 0 ||
4864 object->activity_in_progress != 0) {
4865 /* try and collapse the rest of the shadow chain */
4866 if (object != original_object) {
4867 vm_object_unlock(object);
4868 }
4869 object = backing_object;
4870 object_lock_type = backing_object_lock_type;
4871 continue;
4872 }
4873
4874 /*
4875 * ...
4876 * The backing object is not read_only,
4877 * and no pages in the backing object are
4878 * currently being paged out.
4879 * The backing object is internal.
4880 *
4881 */
4882
4883 if (!backing_object->internal ||
4884 backing_object->paging_in_progress != 0 ||
4885 backing_object->activity_in_progress != 0) {
4886 /* try and collapse the rest of the shadow chain */
4887 if (object != original_object) {
4888 vm_object_unlock(object);
4889 }
4890 object = backing_object;
4891 object_lock_type = backing_object_lock_type;
4892 continue;
4893 }
4894
4895 /*
4896 * The backing object can't be a copy-object:
4897 * the shadow_offset for the copy-object must stay
4898 * as 0. Furthermore (for the 'we have all the
4899 * pages' case), if we bypass backing_object and
4900 * just shadow the next object in the chain, old
4901 * pages from that object would then have to be copied
4902 * BOTH into the (former) backing_object and into the
4903 * parent object.
4904 */
4905 if (backing_object->shadow != VM_OBJECT_NULL &&
4906 backing_object->shadow->copy == backing_object) {
4907 /* try and collapse the rest of the shadow chain */
4908 if (object != original_object) {
4909 vm_object_unlock(object);
4910 }
4911 object = backing_object;
4912 object_lock_type = backing_object_lock_type;
4913 continue;
4914 }
4915
4916 /*
4917 * We can now try to either collapse the backing
4918 * object (if the parent is the only reference to
4919 * it) or (perhaps) remove the parent's reference
4920 * to it.
4921 *
4922 * If there is exactly one reference to the backing
4923 * object, we may be able to collapse it into the
4924 * parent.
4925 *
4926 * If MACH_PAGEMAP is defined:
4927 * The parent must not have a pager created for it,
4928 * since collapsing a backing_object dumps new pages
4929 * into the parent that its pager doesn't know about
4930 * (and the collapse code can't merge the existence
4931 * maps).
4932 * Otherwise:
4933 * As long as one of the objects is still not known
4934 * to the pager, we can collapse them.
4935 */
4936 if (backing_object->ref_count == 1 &&
4937 (!object->pager_created
4938 #if !MACH_PAGEMAP
4939 || !backing_object->pager_created
4940 #endif /*!MACH_PAGEMAP */
4941 ) && vm_object_collapse_allowed) {
4942
4943 /*
4944 * We need the exclusive lock on the VM objects.
4945 */
4946 if (backing_object_lock_type != OBJECT_LOCK_EXCLUSIVE) {
4947 /*
4948 * We have an object and its shadow locked
4949 * "shared". We can't just upgrade the locks
4950 * to "exclusive", as some other thread might
4951 * also have these objects locked "shared" and
4952 * attempt to upgrade one or the other to
4953 * "exclusive". The upgrades would block
4954 * forever waiting for the other "shared" locks
4955 * to get released.
4956 * So we have to release the locks and go
4957 * down the shadow chain again (since it could
4958 * have changed) with "exclusive" locking.
4959 */
4960 vm_object_unlock(backing_object);
4961 if (object != original_object)
4962 vm_object_unlock(object);
4963 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4964 backing_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4965 goto retry;
4966 }
4967
4968 XPR(XPR_VM_OBJECT,
4969 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
4970 backing_object, object,
4971 backing_object->pager,
4972 backing_object->pager_control, 0);
4973
4974 /*
4975 * Collapse the object with its backing
4976 * object, and try again with the object's
4977 * new backing object.
4978 */
4979
4980 vm_object_do_collapse(object, backing_object);
4981 vm_object_collapse_do_collapse++;
4982 continue;
4983 }
4984
4985 /*
4986 * Collapsing the backing object was not possible
4987 * or permitted, so let's try bypassing it.
4988 */
4989
4990 if (! (can_bypass && vm_object_bypass_allowed)) {
4991 /* try and collapse the rest of the shadow chain */
4992 if (object != original_object) {
4993 vm_object_unlock(object);
4994 }
4995 object = backing_object;
4996 object_lock_type = backing_object_lock_type;
4997 continue;
4998 }
4999
5000
5001 /*
5002 * If the object doesn't have all its pages present,
5003 * we have to make sure no pages in the backing object
5004 * "show through" before bypassing it.
5005 */
5006 size = atop(object->vo_size);
5007 rcount = object->resident_page_count;
5008 if (rcount != size) {
5009 vm_object_offset_t offset;
5010 vm_object_offset_t backing_offset;
5011 unsigned int backing_rcount;
5012 unsigned int lookups = 0;
5013
5014 /*
5015 * If the backing object has a pager but no pagemap,
5016 * then we cannot bypass it, because we don't know
5017 * what pages it has.
5018 */
5019 if (backing_object->pager_created
5020 #if MACH_PAGEMAP
5021 && (backing_object->existence_map == VM_EXTERNAL_NULL)
5022 #endif /* MACH_PAGEMAP */
5023 ) {
5024 /* try and collapse the rest of the shadow chain */
5025 if (object != original_object) {
5026 vm_object_unlock(object);
5027 }
5028 object = backing_object;
5029 object_lock_type = backing_object_lock_type;
5030 continue;
5031 }
5032
5033 /*
5034 * If the object has a pager but no pagemap,
5035 * then we cannot bypass it, because we don't know
5036 * what pages it has.
5037 */
5038 if (object->pager_created
5039 #if MACH_PAGEMAP
5040 && (object->existence_map == VM_EXTERNAL_NULL)
5041 #endif /* MACH_PAGEMAP */
5042 ) {
5043 /* try and collapse the rest of the shadow chain */
5044 if (object != original_object) {
5045 vm_object_unlock(object);
5046 }
5047 object = backing_object;
5048 object_lock_type = backing_object_lock_type;
5049 continue;
5050 }
5051
5052 /*
5053 * If all of the pages in the backing object are
5054 * shadowed by the parent object, the parent
5055 * object no longer has to shadow the backing
5056 * object; it can shadow the next one in the
5057 * chain.
5058 *
5059 * If the backing object has existence info,
5060 * we must check examine its existence info
5061 * as well.
5062 *
5063 */
5064
5065 backing_offset = object->vo_shadow_offset;
5066 backing_rcount = backing_object->resident_page_count;
5067
5068 #if MACH_PAGEMAP
5069 #define EXISTS_IN_OBJECT(obj, off, rc) \
5070 (vm_external_state_get((obj)->existence_map, \
5071 (vm_offset_t)(off)) == VM_EXTERNAL_STATE_EXISTS || \
5072 ((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
5073 #else
5074 #define EXISTS_IN_OBJECT(obj, off, rc) \
5075 (((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
5076 #endif /* MACH_PAGEMAP */
5077
5078 /*
5079 * Check the hint location first
5080 * (since it is often the quickest way out of here).
5081 */
5082 if (object->cow_hint != ~(vm_offset_t)0)
5083 hint_offset = (vm_object_offset_t)object->cow_hint;
5084 else
5085 hint_offset = (hint_offset > 8 * PAGE_SIZE_64) ?
5086 (hint_offset - 8 * PAGE_SIZE_64) : 0;
5087
5088 if (EXISTS_IN_OBJECT(backing_object, hint_offset +
5089 backing_offset, backing_rcount) &&
5090 !EXISTS_IN_OBJECT(object, hint_offset, rcount)) {
5091 /* dependency right at the hint */
5092 object->cow_hint = (vm_offset_t) hint_offset; /* atomic */
5093 /* try and collapse the rest of the shadow chain */
5094 if (object != original_object) {
5095 vm_object_unlock(object);
5096 }
5097 object = backing_object;
5098 object_lock_type = backing_object_lock_type;
5099 continue;
5100 }
5101
5102 /*
5103 * If the object's window onto the backing_object
5104 * is large compared to the number of resident
5105 * pages in the backing object, it makes sense to
5106 * walk the backing_object's resident pages first.
5107 *
5108 * NOTE: Pages may be in both the existence map and
5109 * resident. So, we can't permanently decrement
5110 * the rcount here because the second loop may
5111 * find the same pages in the backing object'
5112 * existence map that we found here and we would
5113 * double-decrement the rcount. We also may or
5114 * may not have found the
5115 */
5116 if (backing_rcount &&
5117 #if MACH_PAGEMAP
5118 size > ((backing_object->existence_map) ?
5119 backing_rcount : (backing_rcount >> 1))
5120 #else
5121 size > (backing_rcount >> 1)
5122 #endif /* MACH_PAGEMAP */
5123 ) {
5124 unsigned int rc = rcount;
5125 vm_page_t p;
5126
5127 backing_rcount = backing_object->resident_page_count;
5128 p = (vm_page_t)queue_first(&backing_object->memq);
5129 do {
5130 /* Until we get more than one lookup lock */
5131 if (lookups > 256) {
5132 vm_object_collapse_delays++;
5133 lookups = 0;
5134 mutex_pause(0);
5135 }
5136
5137 offset = (p->offset - backing_offset);
5138 if (offset < object->vo_size &&
5139 offset != hint_offset &&
5140 !EXISTS_IN_OBJECT(object, offset, rc)) {
5141 /* found a dependency */
5142 object->cow_hint = (vm_offset_t) offset; /* atomic */
5143
5144 break;
5145 }
5146 p = (vm_page_t) queue_next(&p->listq);
5147
5148 } while (--backing_rcount);
5149 if (backing_rcount != 0 ) {
5150 /* try and collapse the rest of the shadow chain */
5151 if (object != original_object) {
5152 vm_object_unlock(object);
5153 }
5154 object = backing_object;
5155 object_lock_type = backing_object_lock_type;
5156 continue;
5157 }
5158 }
5159
5160 /*
5161 * Walk through the offsets looking for pages in the
5162 * backing object that show through to the object.
5163 */
5164 if (backing_rcount
5165 #if MACH_PAGEMAP
5166 || backing_object->existence_map
5167 #endif /* MACH_PAGEMAP */
5168 ) {
5169 offset = hint_offset;
5170
5171 while((offset =
5172 (offset + PAGE_SIZE_64 < object->vo_size) ?
5173 (offset + PAGE_SIZE_64) : 0) != hint_offset) {
5174
5175 /* Until we get more than one lookup lock */
5176 if (lookups > 256) {
5177 vm_object_collapse_delays++;
5178 lookups = 0;
5179 mutex_pause(0);
5180 }
5181
5182 if (EXISTS_IN_OBJECT(backing_object, offset +
5183 backing_offset, backing_rcount) &&
5184 !EXISTS_IN_OBJECT(object, offset, rcount)) {
5185 /* found a dependency */
5186 object->cow_hint = (vm_offset_t) offset; /* atomic */
5187 break;
5188 }
5189 }
5190 if (offset != hint_offset) {
5191 /* try and collapse the rest of the shadow chain */
5192 if (object != original_object) {
5193 vm_object_unlock(object);
5194 }
5195 object = backing_object;
5196 object_lock_type = backing_object_lock_type;
5197 continue;
5198 }
5199 }
5200 }
5201
5202 /*
5203 * We need "exclusive" locks on the 2 VM objects.
5204 */
5205 if (backing_object_lock_type != OBJECT_LOCK_EXCLUSIVE) {
5206 vm_object_unlock(backing_object);
5207 if (object != original_object)
5208 vm_object_unlock(object);
5209 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
5210 backing_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
5211 goto retry;
5212 }
5213
5214 /* reset the offset hint for any objects deeper in the chain */
5215 object->cow_hint = (vm_offset_t)0;
5216
5217 /*
5218 * All interesting pages in the backing object
5219 * already live in the parent or its pager.
5220 * Thus we can bypass the backing object.
5221 */
5222
5223 vm_object_do_bypass(object, backing_object);
5224 vm_object_collapse_do_bypass++;
5225
5226 /*
5227 * Try again with this object's new backing object.
5228 */
5229
5230 continue;
5231 }
5232
5233 if (object != original_object) {
5234 vm_object_unlock(object);
5235 }
5236 }
5237
5238 /*
5239 * Routine: vm_object_page_remove: [internal]
5240 * Purpose:
5241 * Removes all physical pages in the specified
5242 * object range from the object's list of pages.
5243 *
5244 * In/out conditions:
5245 * The object must be locked.
5246 * The object must not have paging_in_progress, usually
5247 * guaranteed by not having a pager.
5248 */
5249 unsigned int vm_object_page_remove_lookup = 0;
5250 unsigned int vm_object_page_remove_iterate = 0;
5251
5252 __private_extern__ void
5253 vm_object_page_remove(
5254 register vm_object_t object,
5255 register vm_object_offset_t start,
5256 register vm_object_offset_t end)
5257 {
5258 register vm_page_t p, next;
5259
5260 /*
5261 * One and two page removals are most popular.
5262 * The factor of 16 here is somewhat arbitrary.
5263 * It balances vm_object_lookup vs iteration.
5264 */
5265
5266 if (atop_64(end - start) < (unsigned)object->resident_page_count/16) {
5267 vm_object_page_remove_lookup++;
5268
5269 for (; start < end; start += PAGE_SIZE_64) {
5270 p = vm_page_lookup(object, start);
5271 if (p != VM_PAGE_NULL) {
5272 assert(!p->cleaning && !p->pageout);
5273 if (!p->fictitious && p->pmapped)
5274 pmap_disconnect(p->phys_page);
5275 VM_PAGE_FREE(p);
5276 }
5277 }
5278 } else {
5279 vm_object_page_remove_iterate++;
5280
5281 p = (vm_page_t) queue_first(&object->memq);
5282 while (!queue_end(&object->memq, (queue_entry_t) p)) {
5283 next = (vm_page_t) queue_next(&p->listq);
5284 if ((start <= p->offset) && (p->offset < end)) {
5285 assert(!p->cleaning && !p->pageout);
5286 if (!p->fictitious && p->pmapped)
5287 pmap_disconnect(p->phys_page);
5288 VM_PAGE_FREE(p);
5289 }
5290 p = next;
5291 }
5292 }
5293 }
5294
5295
5296 /*
5297 * Routine: vm_object_coalesce
5298 * Function: Coalesces two objects backing up adjoining
5299 * regions of memory into a single object.
5300 *
5301 * returns TRUE if objects were combined.
5302 *
5303 * NOTE: Only works at the moment if the second object is NULL -
5304 * if it's not, which object do we lock first?
5305 *
5306 * Parameters:
5307 * prev_object First object to coalesce
5308 * prev_offset Offset into prev_object
5309 * next_object Second object into coalesce
5310 * next_offset Offset into next_object
5311 *
5312 * prev_size Size of reference to prev_object
5313 * next_size Size of reference to next_object
5314 *
5315 * Conditions:
5316 * The object(s) must *not* be locked. The map must be locked
5317 * to preserve the reference to the object(s).
5318 */
5319 static int vm_object_coalesce_count = 0;
5320
5321 __private_extern__ boolean_t
5322 vm_object_coalesce(
5323 register vm_object_t prev_object,
5324 vm_object_t next_object,
5325 vm_object_offset_t prev_offset,
5326 __unused vm_object_offset_t next_offset,
5327 vm_object_size_t prev_size,
5328 vm_object_size_t next_size)
5329 {
5330 vm_object_size_t newsize;
5331
5332 #ifdef lint
5333 next_offset++;
5334 #endif /* lint */
5335
5336 if (next_object != VM_OBJECT_NULL) {
5337 return(FALSE);
5338 }
5339
5340 if (prev_object == VM_OBJECT_NULL) {
5341 return(TRUE);
5342 }
5343
5344 XPR(XPR_VM_OBJECT,
5345 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
5346 prev_object, prev_offset, prev_size, next_size, 0);
5347
5348 vm_object_lock(prev_object);
5349
5350 /*
5351 * Try to collapse the object first
5352 */
5353 vm_object_collapse(prev_object, prev_offset, TRUE);
5354
5355 /*
5356 * Can't coalesce if pages not mapped to
5357 * prev_entry may be in use any way:
5358 * . more than one reference
5359 * . paged out
5360 * . shadows another object
5361 * . has a copy elsewhere
5362 * . is purgeable
5363 * . paging references (pages might be in page-list)
5364 */
5365
5366 if ((prev_object->ref_count > 1) ||
5367 prev_object->pager_created ||
5368 (prev_object->shadow != VM_OBJECT_NULL) ||
5369 (prev_object->copy != VM_OBJECT_NULL) ||
5370 (prev_object->true_share != FALSE) ||
5371 (prev_object->purgable != VM_PURGABLE_DENY) ||
5372 (prev_object->paging_in_progress != 0) ||
5373 (prev_object->activity_in_progress != 0)) {
5374 vm_object_unlock(prev_object);
5375 return(FALSE);
5376 }
5377
5378 vm_object_coalesce_count++;
5379
5380 /*
5381 * Remove any pages that may still be in the object from
5382 * a previous deallocation.
5383 */
5384 vm_object_page_remove(prev_object,
5385 prev_offset + prev_size,
5386 prev_offset + prev_size + next_size);
5387
5388 /*
5389 * Extend the object if necessary.
5390 */
5391 newsize = prev_offset + prev_size + next_size;
5392 if (newsize > prev_object->vo_size) {
5393 #if MACH_PAGEMAP
5394 /*
5395 * We cannot extend an object that has existence info,
5396 * since the existence info might then fail to cover
5397 * the entire object.
5398 *
5399 * This assertion must be true because the object
5400 * has no pager, and we only create existence info
5401 * for objects with pagers.
5402 */
5403 assert(prev_object->existence_map == VM_EXTERNAL_NULL);
5404 #endif /* MACH_PAGEMAP */
5405 prev_object->vo_size = newsize;
5406 }
5407
5408 vm_object_unlock(prev_object);
5409 return(TRUE);
5410 }
5411
5412 /*
5413 * Attach a set of physical pages to an object, so that they can
5414 * be mapped by mapping the object. Typically used to map IO memory.
5415 *
5416 * The mapping function and its private data are used to obtain the
5417 * physical addresses for each page to be mapped.
5418 */
5419 void
5420 vm_object_page_map(
5421 vm_object_t object,
5422 vm_object_offset_t offset,
5423 vm_object_size_t size,
5424 vm_object_offset_t (*map_fn)(void *map_fn_data,
5425 vm_object_offset_t offset),
5426 void *map_fn_data) /* private to map_fn */
5427 {
5428 int64_t num_pages;
5429 int i;
5430 vm_page_t m;
5431 vm_page_t old_page;
5432 vm_object_offset_t addr;
5433
5434 num_pages = atop_64(size);
5435
5436 for (i = 0; i < num_pages; i++, offset += PAGE_SIZE_64) {
5437
5438 addr = (*map_fn)(map_fn_data, offset);
5439
5440 while ((m = vm_page_grab_fictitious()) == VM_PAGE_NULL)
5441 vm_page_more_fictitious();
5442
5443 vm_object_lock(object);
5444 if ((old_page = vm_page_lookup(object, offset))
5445 != VM_PAGE_NULL)
5446 {
5447 VM_PAGE_FREE(old_page);
5448 }
5449
5450 assert((ppnum_t) addr == addr);
5451 vm_page_init(m, (ppnum_t) addr, FALSE);
5452 /*
5453 * private normally requires lock_queues but since we
5454 * are initializing the page, its not necessary here
5455 */
5456 m->private = TRUE; /* don`t free page */
5457 m->wire_count = 1;
5458 vm_page_insert(m, object, offset);
5459
5460 PAGE_WAKEUP_DONE(m);
5461 vm_object_unlock(object);
5462 }
5463 }
5464
5465 #include <mach_kdb.h>
5466
5467 #if MACH_KDB
5468 #include <ddb/db_output.h>
5469 #include <vm/vm_print.h>
5470
5471 #define printf kdbprintf
5472
5473 extern boolean_t vm_object_cached(
5474 vm_object_t object);
5475
5476 extern void print_bitstring(
5477 char byte);
5478
5479 boolean_t vm_object_print_pages = FALSE;
5480
5481 void
5482 print_bitstring(
5483 char byte)
5484 {
5485 printf("%c%c%c%c%c%c%c%c",
5486 ((byte & (1 << 0)) ? '1' : '0'),
5487 ((byte & (1 << 1)) ? '1' : '0'),
5488 ((byte & (1 << 2)) ? '1' : '0'),
5489 ((byte & (1 << 3)) ? '1' : '0'),
5490 ((byte & (1 << 4)) ? '1' : '0'),
5491 ((byte & (1 << 5)) ? '1' : '0'),
5492 ((byte & (1 << 6)) ? '1' : '0'),
5493 ((byte & (1 << 7)) ? '1' : '0'));
5494 }
5495
5496 boolean_t
5497 vm_object_cached(
5498 __unused register vm_object_t object)
5499 {
5500 #if VM_OBJECT_CACHE
5501 register vm_object_t o;
5502
5503 queue_iterate(&vm_object_cached_list, o, vm_object_t, cached_list) {
5504 if (object == o) {
5505 return TRUE;
5506 }
5507 }
5508 #endif
5509 return FALSE;
5510 }
5511
5512 #if MACH_PAGEMAP
5513 /*
5514 * vm_external_print: [ debug ]
5515 */
5516 void
5517 vm_external_print(
5518 vm_external_map_t emap,
5519 vm_object_size_t size)
5520 {
5521 if (emap == VM_EXTERNAL_NULL) {
5522 printf("0 ");
5523 } else {
5524 vm_object_size_t existence_size = stob(size);
5525 printf("{ size=%lld, map=[", (uint64_t) existence_size);
5526 if (existence_size > 0) {
5527 print_bitstring(emap[0]);
5528 }
5529 if (existence_size > 1) {
5530 print_bitstring(emap[1]);
5531 }
5532 if (existence_size > 2) {
5533 printf("...");
5534 print_bitstring(emap[existence_size-1]);
5535 }
5536 printf("] }\n");
5537 }
5538 return;
5539 }
5540 #endif /* MACH_PAGEMAP */
5541
5542 int
5543 vm_follow_object(
5544 vm_object_t object)
5545 {
5546 int count = 0;
5547 int orig_db_indent = db_indent;
5548
5549 while (TRUE) {
5550 if (object == VM_OBJECT_NULL) {
5551 db_indent = orig_db_indent;
5552 return count;
5553 }
5554
5555 count += 1;
5556
5557 iprintf("object 0x%x", object);
5558 printf(", shadow=0x%x", object->shadow);
5559 printf(", copy=0x%x", object->copy);
5560 printf(", pager=0x%x", object->pager);
5561 printf(", ref=%d\n", object->ref_count);
5562
5563 db_indent += 2;
5564 object = object->shadow;
5565 }
5566
5567 }
5568
5569 /*
5570 * vm_object_print: [ debug ]
5571 */
5572 void
5573 vm_object_print(db_expr_t db_addr, __unused boolean_t have_addr,
5574 __unused db_expr_t arg_count, __unused char *modif)
5575 {
5576 vm_object_t object;
5577 register vm_page_t p;
5578 const char *s;
5579
5580 register int count;
5581
5582 object = (vm_object_t) (long) db_addr;
5583 if (object == VM_OBJECT_NULL)
5584 return;
5585
5586 iprintf("object 0x%x\n", object);
5587
5588 db_indent += 2;
5589
5590 iprintf("size=0x%x", object->vo_size);
5591 printf(", memq_hint=%p", object->memq_hint);
5592 printf(", ref_count=%d\n", object->ref_count);
5593 iprintf("");
5594 #if TASK_SWAPPER
5595 printf("res_count=%d, ", object->res_count);
5596 #endif /* TASK_SWAPPER */
5597 printf("resident_page_count=%d\n", object->resident_page_count);
5598
5599 iprintf("shadow=0x%x", object->shadow);
5600 if (object->shadow) {
5601 register int i = 0;
5602 vm_object_t shadow = object;
5603 while((shadow = shadow->shadow))
5604 i++;
5605 printf(" (depth %d)", i);
5606 }
5607 printf(", copy=0x%x", object->copy);
5608 printf(", shadow_offset=0x%x", object->vo_shadow_offset);
5609 printf(", last_alloc=0x%x\n", object->last_alloc);
5610
5611 iprintf("pager=0x%x", object->pager);
5612 printf(", paging_offset=0x%x", object->paging_offset);
5613 printf(", pager_control=0x%x\n", object->pager_control);
5614
5615 iprintf("copy_strategy=%d[", object->copy_strategy);
5616 switch (object->copy_strategy) {
5617 case MEMORY_OBJECT_COPY_NONE:
5618 printf("copy_none");
5619 break;
5620
5621 case MEMORY_OBJECT_COPY_CALL:
5622 printf("copy_call");
5623 break;
5624
5625 case MEMORY_OBJECT_COPY_DELAY:
5626 printf("copy_delay");
5627 break;
5628
5629 case MEMORY_OBJECT_COPY_SYMMETRIC:
5630 printf("copy_symmetric");
5631 break;
5632
5633 case MEMORY_OBJECT_COPY_INVALID:
5634 printf("copy_invalid");
5635 break;
5636
5637 default:
5638 printf("?");
5639 }
5640 printf("]");
5641
5642 iprintf("all_wanted=0x%x<", object->all_wanted);
5643 s = "";
5644 if (vm_object_wanted(object, VM_OBJECT_EVENT_INITIALIZED)) {
5645 printf("%sinit", s);
5646 s = ",";
5647 }
5648 if (vm_object_wanted(object, VM_OBJECT_EVENT_PAGER_READY)) {
5649 printf("%sready", s);
5650 s = ",";
5651 }
5652 if (vm_object_wanted(object, VM_OBJECT_EVENT_PAGING_IN_PROGRESS)) {
5653 printf("%spaging", s);
5654 s = ",";
5655 }
5656 if (vm_object_wanted(object, VM_OBJECT_EVENT_LOCK_IN_PROGRESS)) {
5657 printf("%slock", s);
5658 s = ",";
5659 }
5660 if (vm_object_wanted(object, VM_OBJECT_EVENT_UNCACHING)) {
5661 printf("%suncaching", s);
5662 s = ",";
5663 }
5664 if (vm_object_wanted(object, VM_OBJECT_EVENT_COPY_CALL)) {
5665 printf("%scopy_call", s);
5666 s = ",";
5667 }
5668 if (vm_object_wanted(object, VM_OBJECT_EVENT_CACHING)) {
5669 printf("%scaching", s);
5670 s = ",";
5671 }
5672 printf(">");
5673 printf(", paging_in_progress=%d\n", object->paging_in_progress);
5674 printf(", activity_in_progress=%d\n", object->activity_in_progress);
5675
5676 iprintf("%screated, %sinit, %sready, %spersist, %strusted, %spageout, %s, %s\n",
5677 (object->pager_created ? "" : "!"),
5678 (object->pager_initialized ? "" : "!"),
5679 (object->pager_ready ? "" : "!"),
5680 (object->can_persist ? "" : "!"),
5681 (object->pager_trusted ? "" : "!"),
5682 (object->pageout ? "" : "!"),
5683 (object->internal ? "internal" : "external"),
5684 (object->temporary ? "temporary" : "permanent"));
5685 iprintf("%salive, %spurgeable, %spurgeable_volatile, %spurgeable_empty, %sshadowed, %scached, %sprivate\n",
5686 (object->alive ? "" : "!"),
5687 ((object->purgable != VM_PURGABLE_DENY) ? "" : "!"),
5688 ((object->purgable == VM_PURGABLE_VOLATILE) ? "" : "!"),
5689 ((object->purgable == VM_PURGABLE_EMPTY) ? "" : "!"),
5690 (object->shadowed ? "" : "!"),
5691 (vm_object_cached(object) ? "" : "!"),
5692 (object->private ? "" : "!"));
5693 iprintf("%sadvisory_pageout, %ssilent_overwrite\n",
5694 (object->advisory_pageout ? "" : "!"),
5695 (object->silent_overwrite ? "" : "!"));
5696
5697 #if MACH_PAGEMAP
5698 iprintf("existence_map=");
5699 vm_external_print(object->existence_map, object->vo_size);
5700 #endif /* MACH_PAGEMAP */
5701 #if MACH_ASSERT
5702 iprintf("paging_object=0x%x\n", object->paging_object);
5703 #endif /* MACH_ASSERT */
5704
5705 if (vm_object_print_pages) {
5706 count = 0;
5707 p = (vm_page_t) queue_first(&object->memq);
5708 while (!queue_end(&object->memq, (queue_entry_t) p)) {
5709 if (count == 0) {
5710 iprintf("memory:=");
5711 } else if (count == 2) {
5712 printf("\n");
5713 iprintf(" ...");
5714 count = 0;
5715 } else {
5716 printf(",");
5717 }
5718 count++;
5719
5720 printf("(off=0x%llX,page=%p)", p->offset, p);
5721 p = (vm_page_t) queue_next(&p->listq);
5722 }
5723 if (count != 0) {
5724 printf("\n");
5725 }
5726 }
5727 db_indent -= 2;
5728 }
5729
5730
5731 /*
5732 * vm_object_find [ debug ]
5733 *
5734 * Find all tasks which reference the given vm_object.
5735 */
5736
5737 boolean_t vm_object_find(vm_object_t object);
5738 boolean_t vm_object_print_verbose = FALSE;
5739
5740 boolean_t
5741 vm_object_find(
5742 vm_object_t object)
5743 {
5744 task_t task;
5745 vm_map_t map;
5746 vm_map_entry_t entry;
5747 boolean_t found = FALSE;
5748
5749 queue_iterate(&tasks, task, task_t, tasks) {
5750 map = task->map;
5751 for (entry = vm_map_first_entry(map);
5752 entry && entry != vm_map_to_entry(map);
5753 entry = entry->vme_next) {
5754
5755 vm_object_t obj;
5756
5757 /*
5758 * For the time being skip submaps,
5759 * only the kernel can have submaps,
5760 * and unless we are interested in
5761 * kernel objects, we can simply skip
5762 * submaps. See sb/dejan/nmk18b7/src/mach_kernel/vm
5763 * for a full solution.
5764 */
5765 if (entry->is_sub_map)
5766 continue;
5767 if (entry)
5768 obj = entry->object.vm_object;
5769 else
5770 continue;
5771
5772 while (obj != VM_OBJECT_NULL) {
5773 if (obj == object) {
5774 if (!found) {
5775 printf("TASK\t\tMAP\t\tENTRY\n");
5776 found = TRUE;
5777 }
5778 printf("0x%x\t0x%x\t0x%x\n",
5779 task, map, entry);
5780 }
5781 obj = obj->shadow;
5782 }
5783 }
5784 }
5785
5786 return(found);
5787 }
5788
5789 #endif /* MACH_KDB */
5790
5791 kern_return_t
5792 vm_object_populate_with_private(
5793 vm_object_t object,
5794 vm_object_offset_t offset,
5795 ppnum_t phys_page,
5796 vm_size_t size)
5797 {
5798 ppnum_t base_page;
5799 vm_object_offset_t base_offset;
5800
5801
5802 if(!object->private)
5803 return KERN_FAILURE;
5804
5805 base_page = phys_page;
5806
5807 vm_object_lock(object);
5808 if(!object->phys_contiguous) {
5809 vm_page_t m;
5810 if((base_offset = trunc_page_64(offset)) != offset) {
5811 vm_object_unlock(object);
5812 return KERN_FAILURE;
5813 }
5814 base_offset += object->paging_offset;
5815 while(size) {
5816 m = vm_page_lookup(object, base_offset);
5817 if(m != VM_PAGE_NULL) {
5818 if(m->fictitious) {
5819 if (m->phys_page != vm_page_guard_addr) {
5820
5821 vm_page_lockspin_queues();
5822 m->private = TRUE;
5823 vm_page_unlock_queues();
5824
5825 m->fictitious = FALSE;
5826 m->phys_page = base_page;
5827 if(!m->busy) {
5828 m->busy = TRUE;
5829 }
5830 if(!m->absent) {
5831 m->absent = TRUE;
5832 }
5833 m->list_req_pending = TRUE;
5834 }
5835 } else if (m->phys_page != base_page) {
5836 if (m->pmapped) {
5837 /*
5838 * pmap call to clear old mapping
5839 */
5840 pmap_disconnect(m->phys_page);
5841 }
5842 m->phys_page = base_page;
5843 }
5844
5845 /*
5846 * ENCRYPTED SWAP:
5847 * We're not pointing to the same
5848 * physical page any longer and the
5849 * contents of the new one are not
5850 * supposed to be encrypted.
5851 * XXX What happens to the original
5852 * physical page. Is it lost ?
5853 */
5854 m->encrypted = FALSE;
5855
5856 } else {
5857 while ((m = vm_page_grab_fictitious()) == VM_PAGE_NULL)
5858 vm_page_more_fictitious();
5859
5860 /*
5861 * private normally requires lock_queues but since we
5862 * are initializing the page, its not necessary here
5863 */
5864 m->private = TRUE;
5865 m->fictitious = FALSE;
5866 m->phys_page = base_page;
5867 m->list_req_pending = TRUE;
5868 m->absent = TRUE;
5869 m->unusual = TRUE;
5870
5871 vm_page_insert(m, object, base_offset);
5872 }
5873 base_page++; /* Go to the next physical page */
5874 base_offset += PAGE_SIZE;
5875 size -= PAGE_SIZE;
5876 }
5877 } else {
5878 /* NOTE: we should check the original settings here */
5879 /* if we have a size > zero a pmap call should be made */
5880 /* to disable the range */
5881
5882 /* pmap_? */
5883
5884 /* shadows on contiguous memory are not allowed */
5885 /* we therefore can use the offset field */
5886 object->vo_shadow_offset = (vm_object_offset_t)phys_page << PAGE_SHIFT;
5887 object->vo_size = size;
5888 }
5889 vm_object_unlock(object);
5890 return KERN_SUCCESS;
5891 }
5892
5893 /*
5894 * memory_object_free_from_cache:
5895 *
5896 * Walk the vm_object cache list, removing and freeing vm_objects
5897 * which are backed by the pager identified by the caller, (pager_ops).
5898 * Remove up to "count" objects, if there are that may available
5899 * in the cache.
5900 *
5901 * Walk the list at most once, return the number of vm_objects
5902 * actually freed.
5903 */
5904
5905 __private_extern__ kern_return_t
5906 memory_object_free_from_cache(
5907 __unused host_t host,
5908 __unused memory_object_pager_ops_t pager_ops,
5909 int *count)
5910 {
5911 #if VM_OBJECT_CACHE
5912 int object_released = 0;
5913
5914 register vm_object_t object = VM_OBJECT_NULL;
5915 vm_object_t shadow;
5916
5917 /*
5918 if(host == HOST_NULL)
5919 return(KERN_INVALID_ARGUMENT);
5920 */
5921
5922 try_again:
5923 vm_object_cache_lock();
5924
5925 queue_iterate(&vm_object_cached_list, object,
5926 vm_object_t, cached_list) {
5927 if (object->pager &&
5928 (pager_ops == object->pager->mo_pager_ops)) {
5929 vm_object_lock(object);
5930 queue_remove(&vm_object_cached_list, object,
5931 vm_object_t, cached_list);
5932 vm_object_cached_count--;
5933
5934 vm_object_cache_unlock();
5935 /*
5936 * Since this object is in the cache, we know
5937 * that it is initialized and has only a pager's
5938 * (implicit) reference. Take a reference to avoid
5939 * recursive deallocations.
5940 */
5941
5942 assert(object->pager_initialized);
5943 assert(object->ref_count == 0);
5944 vm_object_lock_assert_exclusive(object);
5945 object->ref_count++;
5946
5947 /*
5948 * Terminate the object.
5949 * If the object had a shadow, we let
5950 * vm_object_deallocate deallocate it.
5951 * "pageout" objects have a shadow, but
5952 * maintain a "paging reference" rather
5953 * than a normal reference.
5954 * (We are careful here to limit recursion.)
5955 */
5956 shadow = object->pageout?VM_OBJECT_NULL:object->shadow;
5957
5958 if ((vm_object_terminate(object) == KERN_SUCCESS)
5959 && (shadow != VM_OBJECT_NULL)) {
5960 vm_object_deallocate(shadow);
5961 }
5962
5963 if(object_released++ == *count)
5964 return KERN_SUCCESS;
5965 goto try_again;
5966 }
5967 }
5968 vm_object_cache_unlock();
5969 *count = object_released;
5970 #else
5971 *count = 0;
5972 #endif
5973 return KERN_SUCCESS;
5974 }
5975
5976
5977
5978 kern_return_t
5979 memory_object_create_named(
5980 memory_object_t pager,
5981 memory_object_offset_t size,
5982 memory_object_control_t *control)
5983 {
5984 vm_object_t object;
5985 vm_object_hash_entry_t entry;
5986 lck_mtx_t *lck;
5987
5988 *control = MEMORY_OBJECT_CONTROL_NULL;
5989 if (pager == MEMORY_OBJECT_NULL)
5990 return KERN_INVALID_ARGUMENT;
5991
5992 lck = vm_object_hash_lock_spin(pager);
5993 entry = vm_object_hash_lookup(pager, FALSE);
5994
5995 if ((entry != VM_OBJECT_HASH_ENTRY_NULL) &&
5996 (entry->object != VM_OBJECT_NULL)) {
5997 if (entry->object->named == TRUE)
5998 panic("memory_object_create_named: caller already holds the right"); }
5999 vm_object_hash_unlock(lck);
6000
6001 if ((object = vm_object_enter(pager, size, FALSE, FALSE, TRUE)) == VM_OBJECT_NULL) {
6002 return(KERN_INVALID_OBJECT);
6003 }
6004
6005 /* wait for object (if any) to be ready */
6006 if (object != VM_OBJECT_NULL) {
6007 vm_object_lock(object);
6008 object->named = TRUE;
6009 while (!object->pager_ready) {
6010 vm_object_sleep(object,
6011 VM_OBJECT_EVENT_PAGER_READY,
6012 THREAD_UNINT);
6013 }
6014 *control = object->pager_control;
6015 vm_object_unlock(object);
6016 }
6017 return (KERN_SUCCESS);
6018 }
6019
6020
6021 /*
6022 * Routine: memory_object_recover_named [user interface]
6023 * Purpose:
6024 * Attempt to recover a named reference for a VM object.
6025 * VM will verify that the object has not already started
6026 * down the termination path, and if it has, will optionally
6027 * wait for that to finish.
6028 * Returns:
6029 * KERN_SUCCESS - we recovered a named reference on the object
6030 * KERN_FAILURE - we could not recover a reference (object dead)
6031 * KERN_INVALID_ARGUMENT - bad memory object control
6032 */
6033 kern_return_t
6034 memory_object_recover_named(
6035 memory_object_control_t control,
6036 boolean_t wait_on_terminating)
6037 {
6038 vm_object_t object;
6039
6040 object = memory_object_control_to_vm_object(control);
6041 if (object == VM_OBJECT_NULL) {
6042 return (KERN_INVALID_ARGUMENT);
6043 }
6044 restart:
6045 vm_object_lock(object);
6046
6047 if (object->terminating && wait_on_terminating) {
6048 vm_object_wait(object,
6049 VM_OBJECT_EVENT_PAGING_IN_PROGRESS,
6050 THREAD_UNINT);
6051 goto restart;
6052 }
6053
6054 if (!object->alive) {
6055 vm_object_unlock(object);
6056 return KERN_FAILURE;
6057 }
6058
6059 if (object->named == TRUE) {
6060 vm_object_unlock(object);
6061 return KERN_SUCCESS;
6062 }
6063 #if VM_OBJECT_CACHE
6064 if ((object->ref_count == 0) && (!object->terminating)) {
6065 if (!vm_object_cache_lock_try()) {
6066 vm_object_unlock(object);
6067 goto restart;
6068 }
6069 queue_remove(&vm_object_cached_list, object,
6070 vm_object_t, cached_list);
6071 vm_object_cached_count--;
6072 XPR(XPR_VM_OBJECT_CACHE,
6073 "memory_object_recover_named: removing %X, head (%X, %X)\n",
6074 object,
6075 vm_object_cached_list.next,
6076 vm_object_cached_list.prev, 0,0);
6077
6078 vm_object_cache_unlock();
6079 }
6080 #endif
6081 object->named = TRUE;
6082 vm_object_lock_assert_exclusive(object);
6083 object->ref_count++;
6084 vm_object_res_reference(object);
6085 while (!object->pager_ready) {
6086 vm_object_sleep(object,
6087 VM_OBJECT_EVENT_PAGER_READY,
6088 THREAD_UNINT);
6089 }
6090 vm_object_unlock(object);
6091 return (KERN_SUCCESS);
6092 }
6093
6094
6095 /*
6096 * vm_object_release_name:
6097 *
6098 * Enforces name semantic on memory_object reference count decrement
6099 * This routine should not be called unless the caller holds a name
6100 * reference gained through the memory_object_create_named.
6101 *
6102 * If the TERMINATE_IDLE flag is set, the call will return if the
6103 * reference count is not 1. i.e. idle with the only remaining reference
6104 * being the name.
6105 * If the decision is made to proceed the name field flag is set to
6106 * false and the reference count is decremented. If the RESPECT_CACHE
6107 * flag is set and the reference count has gone to zero, the
6108 * memory_object is checked to see if it is cacheable otherwise when
6109 * the reference count is zero, it is simply terminated.
6110 */
6111
6112 __private_extern__ kern_return_t
6113 vm_object_release_name(
6114 vm_object_t object,
6115 int flags)
6116 {
6117 vm_object_t shadow;
6118 boolean_t original_object = TRUE;
6119
6120 while (object != VM_OBJECT_NULL) {
6121
6122 vm_object_lock(object);
6123
6124 assert(object->alive);
6125 if (original_object)
6126 assert(object->named);
6127 assert(object->ref_count > 0);
6128
6129 /*
6130 * We have to wait for initialization before
6131 * destroying or caching the object.
6132 */
6133
6134 if (object->pager_created && !object->pager_initialized) {
6135 assert(!object->can_persist);
6136 vm_object_assert_wait(object,
6137 VM_OBJECT_EVENT_INITIALIZED,
6138 THREAD_UNINT);
6139 vm_object_unlock(object);
6140 thread_block(THREAD_CONTINUE_NULL);
6141 continue;
6142 }
6143
6144 if (((object->ref_count > 1)
6145 && (flags & MEMORY_OBJECT_TERMINATE_IDLE))
6146 || (object->terminating)) {
6147 vm_object_unlock(object);
6148 return KERN_FAILURE;
6149 } else {
6150 if (flags & MEMORY_OBJECT_RELEASE_NO_OP) {
6151 vm_object_unlock(object);
6152 return KERN_SUCCESS;
6153 }
6154 }
6155
6156 if ((flags & MEMORY_OBJECT_RESPECT_CACHE) &&
6157 (object->ref_count == 1)) {
6158 if (original_object)
6159 object->named = FALSE;
6160 vm_object_unlock(object);
6161 /* let vm_object_deallocate push this thing into */
6162 /* the cache, if that it is where it is bound */
6163 vm_object_deallocate(object);
6164 return KERN_SUCCESS;
6165 }
6166 VM_OBJ_RES_DECR(object);
6167 shadow = object->pageout?VM_OBJECT_NULL:object->shadow;
6168
6169 if (object->ref_count == 1) {
6170 if (vm_object_terminate(object) != KERN_SUCCESS) {
6171 if (original_object) {
6172 return KERN_FAILURE;
6173 } else {
6174 return KERN_SUCCESS;
6175 }
6176 }
6177 if (shadow != VM_OBJECT_NULL) {
6178 original_object = FALSE;
6179 object = shadow;
6180 continue;
6181 }
6182 return KERN_SUCCESS;
6183 } else {
6184 vm_object_lock_assert_exclusive(object);
6185 object->ref_count--;
6186 assert(object->ref_count > 0);
6187 if(original_object)
6188 object->named = FALSE;
6189 vm_object_unlock(object);
6190 return KERN_SUCCESS;
6191 }
6192 }
6193 /*NOTREACHED*/
6194 assert(0);
6195 return KERN_FAILURE;
6196 }
6197
6198
6199 __private_extern__ kern_return_t
6200 vm_object_lock_request(
6201 vm_object_t object,
6202 vm_object_offset_t offset,
6203 vm_object_size_t size,
6204 memory_object_return_t should_return,
6205 int flags,
6206 vm_prot_t prot)
6207 {
6208 __unused boolean_t should_flush;
6209
6210 should_flush = flags & MEMORY_OBJECT_DATA_FLUSH;
6211
6212 XPR(XPR_MEMORY_OBJECT,
6213 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
6214 object, offset, size,
6215 (((should_return&1)<<1)|should_flush), prot);
6216
6217 /*
6218 * Check for bogus arguments.
6219 */
6220 if (object == VM_OBJECT_NULL)
6221 return (KERN_INVALID_ARGUMENT);
6222
6223 if ((prot & ~VM_PROT_ALL) != 0 && prot != VM_PROT_NO_CHANGE)
6224 return (KERN_INVALID_ARGUMENT);
6225
6226 size = round_page_64(size);
6227
6228 /*
6229 * Lock the object, and acquire a paging reference to
6230 * prevent the memory_object reference from being released.
6231 */
6232 vm_object_lock(object);
6233 vm_object_paging_begin(object);
6234
6235 (void)vm_object_update(object,
6236 offset, size, NULL, NULL, should_return, flags, prot);
6237
6238 vm_object_paging_end(object);
6239 vm_object_unlock(object);
6240
6241 return (KERN_SUCCESS);
6242 }
6243
6244 /*
6245 * Empty a purgeable object by grabbing the physical pages assigned to it and
6246 * putting them on the free queue without writing them to backing store, etc.
6247 * When the pages are next touched they will be demand zero-fill pages. We
6248 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
6249 * skip referenced/dirty pages, pages on the active queue, etc. We're more
6250 * than happy to grab these since this is a purgeable object. We mark the
6251 * object as "empty" after reaping its pages.
6252 *
6253 * On entry the object must be locked and it must be
6254 * purgeable with no delayed copies pending.
6255 */
6256 void
6257 vm_object_purge(vm_object_t object)
6258 {
6259 vm_object_lock_assert_exclusive(object);
6260
6261 if (object->purgable == VM_PURGABLE_DENY)
6262 return;
6263
6264 assert(object->copy == VM_OBJECT_NULL);
6265 assert(object->copy_strategy == MEMORY_OBJECT_COPY_NONE);
6266
6267 if(object->purgable == VM_PURGABLE_VOLATILE) {
6268 unsigned int delta;
6269 assert(object->resident_page_count >=
6270 object->wired_page_count);
6271 delta = (object->resident_page_count -
6272 object->wired_page_count);
6273 if (delta != 0) {
6274 assert(vm_page_purgeable_count >=
6275 delta);
6276 OSAddAtomic(-delta,
6277 (SInt32 *)&vm_page_purgeable_count);
6278 }
6279 if (object->wired_page_count != 0) {
6280 assert(vm_page_purgeable_wired_count >=
6281 object->wired_page_count);
6282 OSAddAtomic(-object->wired_page_count,
6283 (SInt32 *)&vm_page_purgeable_wired_count);
6284 }
6285 }
6286 object->purgable = VM_PURGABLE_EMPTY;
6287
6288 vm_object_reap_pages(object, REAP_PURGEABLE);
6289 }
6290
6291
6292 /*
6293 * vm_object_purgeable_control() allows the caller to control and investigate the
6294 * state of a purgeable object. A purgeable object is created via a call to
6295 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgeable object will
6296 * never be coalesced with any other object -- even other purgeable objects --
6297 * and will thus always remain a distinct object. A purgeable object has
6298 * special semantics when its reference count is exactly 1. If its reference
6299 * count is greater than 1, then a purgeable object will behave like a normal
6300 * object and attempts to use this interface will result in an error return
6301 * of KERN_INVALID_ARGUMENT.
6302 *
6303 * A purgeable object may be put into a "volatile" state which will make the
6304 * object's pages elligable for being reclaimed without paging to backing
6305 * store if the system runs low on memory. If the pages in a volatile
6306 * purgeable object are reclaimed, the purgeable object is said to have been
6307 * "emptied." When a purgeable object is emptied the system will reclaim as
6308 * many pages from the object as it can in a convenient manner (pages already
6309 * en route to backing store or busy for other reasons are left as is). When
6310 * a purgeable object is made volatile, its pages will generally be reclaimed
6311 * before other pages in the application's working set. This semantic is
6312 * generally used by applications which can recreate the data in the object
6313 * faster than it can be paged in. One such example might be media assets
6314 * which can be reread from a much faster RAID volume.
6315 *
6316 * A purgeable object may be designated as "non-volatile" which means it will
6317 * behave like all other objects in the system with pages being written to and
6318 * read from backing store as needed to satisfy system memory needs. If the
6319 * object was emptied before the object was made non-volatile, that fact will
6320 * be returned as the old state of the purgeable object (see
6321 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
6322 * were reclaimed as part of emptying the object will be refaulted in as
6323 * zero-fill on demand. It is up to the application to note that an object
6324 * was emptied and recreate the objects contents if necessary. When a
6325 * purgeable object is made non-volatile, its pages will generally not be paged
6326 * out to backing store in the immediate future. A purgeable object may also
6327 * be manually emptied.
6328 *
6329 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
6330 * volatile purgeable object may be queried at any time. This information may
6331 * be used as a control input to let the application know when the system is
6332 * experiencing memory pressure and is reclaiming memory.
6333 *
6334 * The specified address may be any address within the purgeable object. If
6335 * the specified address does not represent any object in the target task's
6336 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
6337 * object containing the specified address is not a purgeable object, then
6338 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
6339 * returned.
6340 *
6341 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
6342 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
6343 * state is used to set the new state of the purgeable object and return its
6344 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgeable
6345 * object is returned in the parameter state.
6346 *
6347 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
6348 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
6349 * the non-volatile, volatile and volatile/empty states described above.
6350 * Setting the state of a purgeable object to VM_PURGABLE_EMPTY will
6351 * immediately reclaim as many pages in the object as can be conveniently
6352 * collected (some may have already been written to backing store or be
6353 * otherwise busy).
6354 *
6355 * The process of making a purgeable object non-volatile and determining its
6356 * previous state is atomic. Thus, if a purgeable object is made
6357 * VM_PURGABLE_NONVOLATILE and the old state is returned as
6358 * VM_PURGABLE_VOLATILE, then the purgeable object's previous contents are
6359 * completely intact and will remain so until the object is made volatile
6360 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
6361 * was reclaimed while it was in a volatile state and its previous contents
6362 * have been lost.
6363 */
6364 /*
6365 * The object must be locked.
6366 */
6367 kern_return_t
6368 vm_object_purgable_control(
6369 vm_object_t object,
6370 vm_purgable_t control,
6371 int *state)
6372 {
6373 int old_state;
6374 int new_state;
6375
6376 if (object == VM_OBJECT_NULL) {
6377 /*
6378 * Object must already be present or it can't be purgeable.
6379 */
6380 return KERN_INVALID_ARGUMENT;
6381 }
6382
6383 /*
6384 * Get current state of the purgeable object.
6385 */
6386 old_state = object->purgable;
6387 if (old_state == VM_PURGABLE_DENY)
6388 return KERN_INVALID_ARGUMENT;
6389
6390 /* purgeable cant have delayed copies - now or in the future */
6391 assert(object->copy == VM_OBJECT_NULL);
6392 assert(object->copy_strategy == MEMORY_OBJECT_COPY_NONE);
6393
6394 /*
6395 * Execute the desired operation.
6396 */
6397 if (control == VM_PURGABLE_GET_STATE) {
6398 *state = old_state;
6399 return KERN_SUCCESS;
6400 }
6401
6402 if ((*state) & VM_PURGABLE_DEBUG_EMPTY) {
6403 object->volatile_empty = TRUE;
6404 }
6405 if ((*state) & VM_PURGABLE_DEBUG_FAULT) {
6406 object->volatile_fault = TRUE;
6407 }
6408
6409 new_state = *state & VM_PURGABLE_STATE_MASK;
6410 if (new_state == VM_PURGABLE_VOLATILE &&
6411 object->volatile_empty) {
6412 new_state = VM_PURGABLE_EMPTY;
6413 }
6414
6415 switch (new_state) {
6416 case VM_PURGABLE_DENY:
6417 case VM_PURGABLE_NONVOLATILE:
6418 object->purgable = new_state;
6419
6420 if (old_state == VM_PURGABLE_VOLATILE) {
6421 unsigned int delta;
6422
6423 assert(object->resident_page_count >=
6424 object->wired_page_count);
6425 delta = (object->resident_page_count -
6426 object->wired_page_count);
6427
6428 assert(vm_page_purgeable_count >= delta);
6429
6430 if (delta != 0) {
6431 OSAddAtomic(-delta,
6432 (SInt32 *)&vm_page_purgeable_count);
6433 }
6434 if (object->wired_page_count != 0) {
6435 assert(vm_page_purgeable_wired_count >=
6436 object->wired_page_count);
6437 OSAddAtomic(-object->wired_page_count,
6438 (SInt32 *)&vm_page_purgeable_wired_count);
6439 }
6440
6441 vm_page_lock_queues();
6442
6443 assert(object->objq.next != NULL && object->objq.prev != NULL); /* object should be on a queue */
6444 purgeable_q_t queue = vm_purgeable_object_remove(object);
6445 assert(queue);
6446
6447 vm_purgeable_token_delete_first(queue);
6448 assert(queue->debug_count_objects>=0);
6449
6450 vm_page_unlock_queues();
6451 }
6452 break;
6453
6454 case VM_PURGABLE_VOLATILE:
6455 if (object->volatile_fault) {
6456 vm_page_t p;
6457 int refmod;
6458
6459 queue_iterate(&object->memq, p, vm_page_t, listq) {
6460 if (p->busy ||
6461 VM_PAGE_WIRED(p) ||
6462 p->fictitious) {
6463 continue;
6464 }
6465 refmod = pmap_disconnect(p->phys_page);
6466 if ((refmod & VM_MEM_MODIFIED) &&
6467 !p->dirty) {
6468 p->dirty = TRUE;
6469 }
6470 }
6471 }
6472
6473 if (old_state == VM_PURGABLE_EMPTY &&
6474 object->resident_page_count == 0)
6475 break;
6476
6477 purgeable_q_t queue;
6478
6479 /* find the correct queue */
6480 if ((*state&VM_PURGABLE_ORDERING_MASK) == VM_PURGABLE_ORDERING_OBSOLETE)
6481 queue = &purgeable_queues[PURGEABLE_Q_TYPE_OBSOLETE];
6482 else {
6483 if ((*state&VM_PURGABLE_BEHAVIOR_MASK) == VM_PURGABLE_BEHAVIOR_FIFO)
6484 queue = &purgeable_queues[PURGEABLE_Q_TYPE_FIFO];
6485 else
6486 queue = &purgeable_queues[PURGEABLE_Q_TYPE_LIFO];
6487 }
6488
6489 if (old_state == VM_PURGABLE_NONVOLATILE ||
6490 old_state == VM_PURGABLE_EMPTY) {
6491 unsigned int delta;
6492
6493 /* try to add token... this can fail */
6494 vm_page_lock_queues();
6495
6496 kern_return_t result = vm_purgeable_token_add(queue);
6497 if (result != KERN_SUCCESS) {
6498 vm_page_unlock_queues();
6499 return result;
6500 }
6501 vm_page_unlock_queues();
6502
6503 assert(object->resident_page_count >=
6504 object->wired_page_count);
6505 delta = (object->resident_page_count -
6506 object->wired_page_count);
6507
6508 if (delta != 0) {
6509 OSAddAtomic(delta,
6510 &vm_page_purgeable_count);
6511 }
6512 if (object->wired_page_count != 0) {
6513 OSAddAtomic(object->wired_page_count,
6514 &vm_page_purgeable_wired_count);
6515 }
6516
6517 object->purgable = new_state;
6518
6519 /* object should not be on a queue */
6520 assert(object->objq.next == NULL && object->objq.prev == NULL);
6521 }
6522 else if (old_state == VM_PURGABLE_VOLATILE) {
6523 /*
6524 * if reassigning priorities / purgeable groups, we don't change the
6525 * token queue. So moving priorities will not make pages stay around longer.
6526 * Reasoning is that the algorithm gives most priority to the most important
6527 * object. If a new token is added, the most important object' priority is boosted.
6528 * This biases the system already for purgeable queues that move a lot.
6529 * It doesn't seem more biasing is neccessary in this case, where no new object is added.
6530 */
6531 assert(object->objq.next != NULL && object->objq.prev != NULL); /* object should be on a queue */
6532
6533 purgeable_q_t old_queue=vm_purgeable_object_remove(object);
6534 assert(old_queue);
6535
6536 if (old_queue != queue) {
6537 kern_return_t result;
6538
6539 /* Changing queue. Have to move token. */
6540 vm_page_lock_queues();
6541 vm_purgeable_token_delete_first(old_queue);
6542 result = vm_purgeable_token_add(queue);
6543 vm_page_unlock_queues();
6544
6545 assert(result==KERN_SUCCESS); /* this should never fail since we just freed a token */
6546 }
6547 };
6548 vm_purgeable_object_add(object, queue, (*state&VM_VOLATILE_GROUP_MASK)>>VM_VOLATILE_GROUP_SHIFT );
6549
6550 assert(queue->debug_count_objects>=0);
6551
6552 break;
6553
6554
6555 case VM_PURGABLE_EMPTY:
6556 if (object->volatile_fault) {
6557 vm_page_t p;
6558 int refmod;
6559
6560 queue_iterate(&object->memq, p, vm_page_t, listq) {
6561 if (p->busy ||
6562 VM_PAGE_WIRED(p) ||
6563 p->fictitious) {
6564 continue;
6565 }
6566 refmod = pmap_disconnect(p->phys_page);
6567 if ((refmod & VM_MEM_MODIFIED) &&
6568 !p->dirty) {
6569 p->dirty = TRUE;
6570 }
6571 }
6572 }
6573
6574 if (old_state != new_state) {
6575 assert(old_state == VM_PURGABLE_NONVOLATILE ||
6576 old_state == VM_PURGABLE_VOLATILE);
6577 if (old_state == VM_PURGABLE_VOLATILE) {
6578 purgeable_q_t old_queue;
6579
6580 /* object should be on a queue */
6581 assert(object->objq.next != NULL &&
6582 object->objq.prev != NULL);
6583 old_queue = vm_purgeable_object_remove(object);
6584 assert(old_queue);
6585 vm_page_lock_queues();
6586 vm_purgeable_token_delete_first(old_queue);
6587 vm_page_unlock_queues();
6588 }
6589 (void) vm_object_purge(object);
6590 }
6591 break;
6592
6593 }
6594 *state = old_state;
6595
6596 return KERN_SUCCESS;
6597 }
6598
6599 #if TASK_SWAPPER
6600 /*
6601 * vm_object_res_deallocate
6602 *
6603 * (recursively) decrement residence counts on vm objects and their shadows.
6604 * Called from vm_object_deallocate and when swapping out an object.
6605 *
6606 * The object is locked, and remains locked throughout the function,
6607 * even as we iterate down the shadow chain. Locks on intermediate objects
6608 * will be dropped, but not the original object.
6609 *
6610 * NOTE: this function used to use recursion, rather than iteration.
6611 */
6612
6613 __private_extern__ void
6614 vm_object_res_deallocate(
6615 vm_object_t object)
6616 {
6617 vm_object_t orig_object = object;
6618 /*
6619 * Object is locked so it can be called directly
6620 * from vm_object_deallocate. Original object is never
6621 * unlocked.
6622 */
6623 assert(object->res_count > 0);
6624 while (--object->res_count == 0) {
6625 assert(object->ref_count >= object->res_count);
6626 vm_object_deactivate_all_pages(object);
6627 /* iterate on shadow, if present */
6628 if (object->shadow != VM_OBJECT_NULL) {
6629 vm_object_t tmp_object = object->shadow;
6630 vm_object_lock(tmp_object);
6631 if (object != orig_object)
6632 vm_object_unlock(object);
6633 object = tmp_object;
6634 assert(object->res_count > 0);
6635 } else
6636 break;
6637 }
6638 if (object != orig_object)
6639 vm_object_unlock(object);
6640 }
6641
6642 /*
6643 * vm_object_res_reference
6644 *
6645 * Internal function to increment residence count on a vm object
6646 * and its shadows. It is called only from vm_object_reference, and
6647 * when swapping in a vm object, via vm_map_swap.
6648 *
6649 * The object is locked, and remains locked throughout the function,
6650 * even as we iterate down the shadow chain. Locks on intermediate objects
6651 * will be dropped, but not the original object.
6652 *
6653 * NOTE: this function used to use recursion, rather than iteration.
6654 */
6655
6656 __private_extern__ void
6657 vm_object_res_reference(
6658 vm_object_t object)
6659 {
6660 vm_object_t orig_object = object;
6661 /*
6662 * Object is locked, so this can be called directly
6663 * from vm_object_reference. This lock is never released.
6664 */
6665 while ((++object->res_count == 1) &&
6666 (object->shadow != VM_OBJECT_NULL)) {
6667 vm_object_t tmp_object = object->shadow;
6668
6669 assert(object->ref_count >= object->res_count);
6670 vm_object_lock(tmp_object);
6671 if (object != orig_object)
6672 vm_object_unlock(object);
6673 object = tmp_object;
6674 }
6675 if (object != orig_object)
6676 vm_object_unlock(object);
6677 assert(orig_object->ref_count >= orig_object->res_count);
6678 }
6679 #endif /* TASK_SWAPPER */
6680
6681 /*
6682 * vm_object_reference:
6683 *
6684 * Gets another reference to the given object.
6685 */
6686 #ifdef vm_object_reference
6687 #undef vm_object_reference
6688 #endif
6689 __private_extern__ void
6690 vm_object_reference(
6691 register vm_object_t object)
6692 {
6693 if (object == VM_OBJECT_NULL)
6694 return;
6695
6696 vm_object_lock(object);
6697 assert(object->ref_count > 0);
6698 vm_object_reference_locked(object);
6699 vm_object_unlock(object);
6700 }
6701
6702 #ifdef MACH_BSD
6703 /*
6704 * Scale the vm_object_cache
6705 * This is required to make sure that the vm_object_cache is big
6706 * enough to effectively cache the mapped file.
6707 * This is really important with UBC as all the regular file vnodes
6708 * have memory object associated with them. Havving this cache too
6709 * small results in rapid reclaim of vnodes and hurts performance a LOT!
6710 *
6711 * This is also needed as number of vnodes can be dynamically scaled.
6712 */
6713 kern_return_t
6714 adjust_vm_object_cache(
6715 __unused vm_size_t oval,
6716 __unused vm_size_t nval)
6717 {
6718 #if VM_OBJECT_CACHE
6719 vm_object_cached_max = nval;
6720 vm_object_cache_trim(FALSE);
6721 #endif
6722 return (KERN_SUCCESS);
6723 }
6724 #endif /* MACH_BSD */
6725
6726
6727 /*
6728 * vm_object_transpose
6729 *
6730 * This routine takes two VM objects of the same size and exchanges
6731 * their backing store.
6732 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
6733 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
6734 *
6735 * The VM objects must not be locked by caller.
6736 */
6737 unsigned int vm_object_transpose_count = 0;
6738 kern_return_t
6739 vm_object_transpose(
6740 vm_object_t object1,
6741 vm_object_t object2,
6742 vm_object_size_t transpose_size)
6743 {
6744 vm_object_t tmp_object;
6745 kern_return_t retval;
6746 boolean_t object1_locked, object2_locked;
6747 vm_page_t page;
6748 vm_object_offset_t page_offset;
6749 lck_mtx_t *hash_lck;
6750 vm_object_hash_entry_t hash_entry;
6751
6752 tmp_object = VM_OBJECT_NULL;
6753 object1_locked = FALSE; object2_locked = FALSE;
6754
6755 if (object1 == object2 ||
6756 object1 == VM_OBJECT_NULL ||
6757 object2 == VM_OBJECT_NULL) {
6758 /*
6759 * If the 2 VM objects are the same, there's
6760 * no point in exchanging their backing store.
6761 */
6762 retval = KERN_INVALID_VALUE;
6763 goto done;
6764 }
6765
6766 /*
6767 * Since we need to lock both objects at the same time,
6768 * make sure we always lock them in the same order to
6769 * avoid deadlocks.
6770 */
6771 if (object1 > object2) {
6772 tmp_object = object1;
6773 object1 = object2;
6774 object2 = tmp_object;
6775 }
6776
6777 /*
6778 * Allocate a temporary VM object to hold object1's contents
6779 * while we copy object2 to object1.
6780 */
6781 tmp_object = vm_object_allocate(transpose_size);
6782 vm_object_lock(tmp_object);
6783 tmp_object->can_persist = FALSE;
6784
6785
6786 /*
6787 * Grab control of the 1st VM object.
6788 */
6789 vm_object_lock(object1);
6790 object1_locked = TRUE;
6791 if (!object1->alive || object1->terminating ||
6792 object1->copy || object1->shadow || object1->shadowed ||
6793 object1->purgable != VM_PURGABLE_DENY) {
6794 /*
6795 * We don't deal with copy or shadow objects (yet).
6796 */
6797 retval = KERN_INVALID_VALUE;
6798 goto done;
6799 }
6800 /*
6801 * We're about to mess with the object's backing store and
6802 * taking a "paging_in_progress" reference wouldn't be enough
6803 * to prevent any paging activity on this object, so the caller should
6804 * have "quiesced" the objects beforehand, via a UPL operation with
6805 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
6806 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
6807 *
6808 * Wait for any paging operation to complete (but only paging, not
6809 * other kind of activities not linked to the pager). After we're
6810 * statisfied that there's no more paging in progress, we keep the
6811 * object locked, to guarantee that no one tries to access its pager.
6812 */
6813 vm_object_paging_only_wait(object1, THREAD_UNINT);
6814
6815 /*
6816 * Same as above for the 2nd object...
6817 */
6818 vm_object_lock(object2);
6819 object2_locked = TRUE;
6820 if (! object2->alive || object2->terminating ||
6821 object2->copy || object2->shadow || object2->shadowed ||
6822 object2->purgable != VM_PURGABLE_DENY) {
6823 retval = KERN_INVALID_VALUE;
6824 goto done;
6825 }
6826 vm_object_paging_only_wait(object2, THREAD_UNINT);
6827
6828
6829 if (object1->vo_size != object2->vo_size ||
6830 object1->vo_size != transpose_size) {
6831 /*
6832 * If the 2 objects don't have the same size, we can't
6833 * exchange their backing stores or one would overflow.
6834 * If their size doesn't match the caller's
6835 * "transpose_size", we can't do it either because the
6836 * transpose operation will affect the entire span of
6837 * the objects.
6838 */
6839 retval = KERN_INVALID_VALUE;
6840 goto done;
6841 }
6842
6843
6844 /*
6845 * Transpose the lists of resident pages.
6846 * This also updates the resident_page_count and the memq_hint.
6847 */
6848 if (object1->phys_contiguous || queue_empty(&object1->memq)) {
6849 /*
6850 * No pages in object1, just transfer pages
6851 * from object2 to object1. No need to go through
6852 * an intermediate object.
6853 */
6854 while (!queue_empty(&object2->memq)) {
6855 page = (vm_page_t) queue_first(&object2->memq);
6856 vm_page_rename(page, object1, page->offset, FALSE);
6857 }
6858 assert(queue_empty(&object2->memq));
6859 } else if (object2->phys_contiguous || queue_empty(&object2->memq)) {
6860 /*
6861 * No pages in object2, just transfer pages
6862 * from object1 to object2. No need to go through
6863 * an intermediate object.
6864 */
6865 while (!queue_empty(&object1->memq)) {
6866 page = (vm_page_t) queue_first(&object1->memq);
6867 vm_page_rename(page, object2, page->offset, FALSE);
6868 }
6869 assert(queue_empty(&object1->memq));
6870 } else {
6871 /* transfer object1's pages to tmp_object */
6872 while (!queue_empty(&object1->memq)) {
6873 page = (vm_page_t) queue_first(&object1->memq);
6874 page_offset = page->offset;
6875 vm_page_remove(page, TRUE);
6876 page->offset = page_offset;
6877 queue_enter(&tmp_object->memq, page, vm_page_t, listq);
6878 }
6879 assert(queue_empty(&object1->memq));
6880 /* transfer object2's pages to object1 */
6881 while (!queue_empty(&object2->memq)) {
6882 page = (vm_page_t) queue_first(&object2->memq);
6883 vm_page_rename(page, object1, page->offset, FALSE);
6884 }
6885 assert(queue_empty(&object2->memq));
6886 /* transfer tmp_object's pages to object1 */
6887 while (!queue_empty(&tmp_object->memq)) {
6888 page = (vm_page_t) queue_first(&tmp_object->memq);
6889 queue_remove(&tmp_object->memq, page,
6890 vm_page_t, listq);
6891 vm_page_insert(page, object2, page->offset);
6892 }
6893 assert(queue_empty(&tmp_object->memq));
6894 }
6895
6896 #define __TRANSPOSE_FIELD(field) \
6897 MACRO_BEGIN \
6898 tmp_object->field = object1->field; \
6899 object1->field = object2->field; \
6900 object2->field = tmp_object->field; \
6901 MACRO_END
6902
6903 /* "Lock" refers to the object not its contents */
6904 /* "size" should be identical */
6905 assert(object1->vo_size == object2->vo_size);
6906 /* "memq_hint" was updated above when transposing pages */
6907 /* "ref_count" refers to the object not its contents */
6908 #if TASK_SWAPPER
6909 /* "res_count" refers to the object not its contents */
6910 #endif
6911 /* "resident_page_count" was updated above when transposing pages */
6912 /* "wired_page_count" was updated above when transposing pages */
6913 /* "reusable_page_count" was updated above when transposing pages */
6914 /* there should be no "copy" */
6915 assert(!object1->copy);
6916 assert(!object2->copy);
6917 /* there should be no "shadow" */
6918 assert(!object1->shadow);
6919 assert(!object2->shadow);
6920 __TRANSPOSE_FIELD(vo_shadow_offset); /* used by phys_contiguous objects */
6921 __TRANSPOSE_FIELD(pager);
6922 __TRANSPOSE_FIELD(paging_offset);
6923 __TRANSPOSE_FIELD(pager_control);
6924 /* update the memory_objects' pointers back to the VM objects */
6925 if (object1->pager_control != MEMORY_OBJECT_CONTROL_NULL) {
6926 memory_object_control_collapse(object1->pager_control,
6927 object1);
6928 }
6929 if (object2->pager_control != MEMORY_OBJECT_CONTROL_NULL) {
6930 memory_object_control_collapse(object2->pager_control,
6931 object2);
6932 }
6933 __TRANSPOSE_FIELD(copy_strategy);
6934 /* "paging_in_progress" refers to the object not its contents */
6935 assert(!object1->paging_in_progress);
6936 assert(!object2->paging_in_progress);
6937 assert(object1->activity_in_progress);
6938 assert(object2->activity_in_progress);
6939 /* "all_wanted" refers to the object not its contents */
6940 __TRANSPOSE_FIELD(pager_created);
6941 __TRANSPOSE_FIELD(pager_initialized);
6942 __TRANSPOSE_FIELD(pager_ready);
6943 __TRANSPOSE_FIELD(pager_trusted);
6944 __TRANSPOSE_FIELD(can_persist);
6945 __TRANSPOSE_FIELD(internal);
6946 __TRANSPOSE_FIELD(temporary);
6947 __TRANSPOSE_FIELD(private);
6948 __TRANSPOSE_FIELD(pageout);
6949 /* "alive" should be set */
6950 assert(object1->alive);
6951 assert(object2->alive);
6952 /* "purgeable" should be non-purgeable */
6953 assert(object1->purgable == VM_PURGABLE_DENY);
6954 assert(object2->purgable == VM_PURGABLE_DENY);
6955 /* "shadowed" refers to the the object not its contents */
6956 __TRANSPOSE_FIELD(silent_overwrite);
6957 __TRANSPOSE_FIELD(advisory_pageout);
6958 __TRANSPOSE_FIELD(true_share);
6959 /* "terminating" should not be set */
6960 assert(!object1->terminating);
6961 assert(!object2->terminating);
6962 __TRANSPOSE_FIELD(named);
6963 /* "shadow_severed" refers to the object not its contents */
6964 __TRANSPOSE_FIELD(phys_contiguous);
6965 __TRANSPOSE_FIELD(nophyscache);
6966 /* "cached_list.next" points to transposed object */
6967 object1->cached_list.next = (queue_entry_t) object2;
6968 object2->cached_list.next = (queue_entry_t) object1;
6969 /* "cached_list.prev" should be NULL */
6970 assert(object1->cached_list.prev == NULL);
6971 assert(object2->cached_list.prev == NULL);
6972 /* "msr_q" is linked to the object not its contents */
6973 assert(queue_empty(&object1->msr_q));
6974 assert(queue_empty(&object2->msr_q));
6975 __TRANSPOSE_FIELD(last_alloc);
6976 __TRANSPOSE_FIELD(sequential);
6977 __TRANSPOSE_FIELD(pages_created);
6978 __TRANSPOSE_FIELD(pages_used);
6979 __TRANSPOSE_FIELD(scan_collisions);
6980 #if MACH_PAGEMAP
6981 __TRANSPOSE_FIELD(existence_map);
6982 #endif
6983 __TRANSPOSE_FIELD(cow_hint);
6984 #if MACH_ASSERT
6985 __TRANSPOSE_FIELD(paging_object);
6986 #endif
6987 __TRANSPOSE_FIELD(wimg_bits);
6988 __TRANSPOSE_FIELD(set_cache_attr);
6989 __TRANSPOSE_FIELD(code_signed);
6990 if (object1->hashed) {
6991 hash_lck = vm_object_hash_lock_spin(object2->pager);
6992 hash_entry = vm_object_hash_lookup(object2->pager, FALSE);
6993 assert(hash_entry != VM_OBJECT_HASH_ENTRY_NULL);
6994 hash_entry->object = object2;
6995 vm_object_hash_unlock(hash_lck);
6996 }
6997 if (object2->hashed) {
6998 hash_lck = vm_object_hash_lock_spin(object1->pager);
6999 hash_entry = vm_object_hash_lookup(object1->pager, FALSE);
7000 assert(hash_entry != VM_OBJECT_HASH_ENTRY_NULL);
7001 hash_entry->object = object1;
7002 vm_object_hash_unlock(hash_lck);
7003 }
7004 __TRANSPOSE_FIELD(hashed);
7005 object1->transposed = TRUE;
7006 object2->transposed = TRUE;
7007 __TRANSPOSE_FIELD(mapping_in_progress);
7008 __TRANSPOSE_FIELD(volatile_empty);
7009 __TRANSPOSE_FIELD(volatile_fault);
7010 __TRANSPOSE_FIELD(all_reusable);
7011 assert(object1->blocked_access);
7012 assert(object2->blocked_access);
7013 assert(object1->__object2_unused_bits == 0);
7014 assert(object2->__object2_unused_bits == 0);
7015 #if UPL_DEBUG
7016 /* "uplq" refers to the object not its contents (see upl_transpose()) */
7017 #endif
7018 assert(object1->objq.next == NULL);
7019 assert(object1->objq.prev == NULL);
7020 assert(object2->objq.next == NULL);
7021 assert(object2->objq.prev == NULL);
7022
7023 #undef __TRANSPOSE_FIELD
7024
7025 retval = KERN_SUCCESS;
7026
7027 done:
7028 /*
7029 * Cleanup.
7030 */
7031 if (tmp_object != VM_OBJECT_NULL) {
7032 vm_object_unlock(tmp_object);
7033 /*
7034 * Re-initialize the temporary object to avoid
7035 * deallocating a real pager.
7036 */
7037 _vm_object_allocate(transpose_size, tmp_object);
7038 vm_object_deallocate(tmp_object);
7039 tmp_object = VM_OBJECT_NULL;
7040 }
7041
7042 if (object1_locked) {
7043 vm_object_unlock(object1);
7044 object1_locked = FALSE;
7045 }
7046 if (object2_locked) {
7047 vm_object_unlock(object2);
7048 object2_locked = FALSE;
7049 }
7050
7051 vm_object_transpose_count++;
7052
7053 return retval;
7054 }
7055
7056
7057 /*
7058 * vm_object_cluster_size
7059 *
7060 * Determine how big a cluster we should issue an I/O for...
7061 *
7062 * Inputs: *start == offset of page needed
7063 * *length == maximum cluster pager can handle
7064 * Outputs: *start == beginning offset of cluster
7065 * *length == length of cluster to try
7066 *
7067 * The original *start will be encompassed by the cluster
7068 *
7069 */
7070 extern int speculative_reads_disabled;
7071 extern int ignore_is_ssd;
7072
7073 #if CONFIG_EMBEDDED
7074 unsigned int preheat_pages_max = MAX_UPL_TRANSFER;
7075 unsigned int preheat_pages_min = 8;
7076 #else
7077 unsigned int preheat_pages_max = MAX_UPL_TRANSFER;
7078 unsigned int preheat_pages_min = 8;
7079 #endif
7080
7081 uint32_t pre_heat_scaling[MAX_UPL_TRANSFER + 1];
7082 uint32_t pre_heat_cluster[MAX_UPL_TRANSFER + 1];
7083
7084
7085 __private_extern__ void
7086 vm_object_cluster_size(vm_object_t object, vm_object_offset_t *start,
7087 vm_size_t *length, vm_object_fault_info_t fault_info, uint32_t *io_streaming)
7088 {
7089 vm_size_t pre_heat_size;
7090 vm_size_t tail_size;
7091 vm_size_t head_size;
7092 vm_size_t max_length;
7093 vm_size_t cluster_size;
7094 vm_object_offset_t object_size;
7095 vm_object_offset_t orig_start;
7096 vm_object_offset_t target_start;
7097 vm_object_offset_t offset;
7098 vm_behavior_t behavior;
7099 boolean_t look_behind = TRUE;
7100 boolean_t look_ahead = TRUE;
7101 boolean_t isSSD = FALSE;
7102 uint32_t throttle_limit;
7103 int sequential_run;
7104 int sequential_behavior = VM_BEHAVIOR_SEQUENTIAL;
7105 unsigned int max_ph_size;
7106 unsigned int min_ph_size;
7107 unsigned int min_ph_size_in_bytes;
7108
7109 assert( !(*length & PAGE_MASK));
7110 assert( !(*start & PAGE_MASK_64));
7111
7112 /*
7113 * remember maxiumum length of run requested
7114 */
7115 max_length = *length;
7116 /*
7117 * we'll always return a cluster size of at least
7118 * 1 page, since the original fault must always
7119 * be processed
7120 */
7121 *length = PAGE_SIZE;
7122 *io_streaming = 0;
7123
7124 if (speculative_reads_disabled || fault_info == NULL) {
7125 /*
7126 * no cluster... just fault the page in
7127 */
7128 return;
7129 }
7130 orig_start = *start;
7131 target_start = orig_start;
7132 cluster_size = round_page(fault_info->cluster_size);
7133 behavior = fault_info->behavior;
7134
7135 vm_object_lock(object);
7136
7137 if (object->pager == MEMORY_OBJECT_NULL)
7138 goto out; /* pager is gone for this object, nothing more to do */
7139
7140 if (!ignore_is_ssd)
7141 vnode_pager_get_isSSD(object->pager, &isSSD);
7142
7143 min_ph_size = preheat_pages_min;
7144 max_ph_size = preheat_pages_max;
7145
7146 if (isSSD) {
7147 min_ph_size /= 2;
7148 max_ph_size /= 8;
7149 }
7150 if (min_ph_size < 1)
7151 min_ph_size = 1;
7152
7153 if (max_ph_size < 1)
7154 max_ph_size = 1;
7155 else if (max_ph_size > MAX_UPL_TRANSFER)
7156 max_ph_size = MAX_UPL_TRANSFER;
7157
7158 if (max_length > (max_ph_size * PAGE_SIZE))
7159 max_length = max_ph_size * PAGE_SIZE;
7160
7161 if (max_length <= PAGE_SIZE)
7162 goto out;
7163
7164 min_ph_size_in_bytes = min_ph_size * PAGE_SIZE;
7165
7166 if (object->internal)
7167 object_size = object->vo_size;
7168 else
7169 vnode_pager_get_object_size(object->pager, &object_size);
7170
7171 object_size = round_page_64(object_size);
7172
7173 if (orig_start >= object_size) {
7174 /*
7175 * fault occurred beyond the EOF...
7176 * we need to punt w/o changing the
7177 * starting offset
7178 */
7179 goto out;
7180 }
7181 if (object->pages_used > object->pages_created) {
7182 /*
7183 * must have wrapped our 32 bit counters
7184 * so reset
7185 */
7186 object->pages_used = object->pages_created = 0;
7187 }
7188 if ((sequential_run = object->sequential)) {
7189 if (sequential_run < 0) {
7190 sequential_behavior = VM_BEHAVIOR_RSEQNTL;
7191 sequential_run = 0 - sequential_run;
7192 } else {
7193 sequential_behavior = VM_BEHAVIOR_SEQUENTIAL;
7194 }
7195
7196 }
7197 switch (behavior) {
7198
7199 default:
7200 behavior = VM_BEHAVIOR_DEFAULT;
7201
7202 case VM_BEHAVIOR_DEFAULT:
7203 if (object->internal && fault_info->user_tag == VM_MEMORY_STACK)
7204 goto out;
7205
7206 if (sequential_run >= (3 * PAGE_SIZE)) {
7207 pre_heat_size = sequential_run + PAGE_SIZE;
7208
7209 if (sequential_behavior == VM_BEHAVIOR_SEQUENTIAL)
7210 look_behind = FALSE;
7211 else
7212 look_ahead = FALSE;
7213
7214 *io_streaming = 1;
7215 } else {
7216
7217 if (object->pages_created < (20 * min_ph_size)) {
7218 /*
7219 * prime the pump
7220 */
7221 pre_heat_size = min_ph_size_in_bytes;
7222 } else {
7223 /*
7224 * Linear growth in PH size: The maximum size is max_length...
7225 * this cacluation will result in a size that is neither a
7226 * power of 2 nor a multiple of PAGE_SIZE... so round
7227 * it up to the nearest PAGE_SIZE boundary
7228 */
7229 pre_heat_size = (max_length * object->pages_used) / object->pages_created;
7230
7231 if (pre_heat_size < min_ph_size_in_bytes)
7232 pre_heat_size = min_ph_size_in_bytes;
7233 else
7234 pre_heat_size = round_page(pre_heat_size);
7235 }
7236 }
7237 break;
7238
7239 case VM_BEHAVIOR_RANDOM:
7240 if ((pre_heat_size = cluster_size) <= PAGE_SIZE)
7241 goto out;
7242 break;
7243
7244 case VM_BEHAVIOR_SEQUENTIAL:
7245 if ((pre_heat_size = cluster_size) == 0)
7246 pre_heat_size = sequential_run + PAGE_SIZE;
7247 look_behind = FALSE;
7248 *io_streaming = 1;
7249
7250 break;
7251
7252 case VM_BEHAVIOR_RSEQNTL:
7253 if ((pre_heat_size = cluster_size) == 0)
7254 pre_heat_size = sequential_run + PAGE_SIZE;
7255 look_ahead = FALSE;
7256 *io_streaming = 1;
7257
7258 break;
7259
7260 }
7261 throttle_limit = (uint32_t) max_length;
7262 assert(throttle_limit == max_length);
7263
7264 if (vnode_pager_check_hard_throttle(object->pager, &throttle_limit, *io_streaming) == KERN_SUCCESS) {
7265 if (max_length > throttle_limit)
7266 max_length = throttle_limit;
7267 }
7268 if (pre_heat_size > max_length)
7269 pre_heat_size = max_length;
7270
7271 if (behavior == VM_BEHAVIOR_DEFAULT && (pre_heat_size > min_ph_size_in_bytes)) {
7272 if (vm_page_free_count < vm_page_throttle_limit)
7273 pre_heat_size = trunc_page(pre_heat_size / 16);
7274 else if (vm_page_free_count < vm_page_free_target)
7275 pre_heat_size = trunc_page(pre_heat_size / 4);
7276
7277 if (pre_heat_size < min_ph_size_in_bytes)
7278 pre_heat_size = min_ph_size_in_bytes;
7279 }
7280 if (look_ahead == TRUE) {
7281 if (look_behind == TRUE) {
7282 /*
7283 * if we get here its due to a random access...
7284 * so we want to center the original fault address
7285 * within the cluster we will issue... make sure
7286 * to calculate 'head_size' as a multiple of PAGE_SIZE...
7287 * 'pre_heat_size' is a multiple of PAGE_SIZE but not
7288 * necessarily an even number of pages so we need to truncate
7289 * the result to a PAGE_SIZE boundary
7290 */
7291 head_size = trunc_page(pre_heat_size / 2);
7292
7293 if (target_start > head_size)
7294 target_start -= head_size;
7295 else
7296 target_start = 0;
7297
7298 /*
7299 * 'target_start' at this point represents the beginning offset
7300 * of the cluster we are considering... 'orig_start' will be in
7301 * the center of this cluster if we didn't have to clip the start
7302 * due to running into the start of the file
7303 */
7304 }
7305 if ((target_start + pre_heat_size) > object_size)
7306 pre_heat_size = (vm_size_t)(round_page_64(object_size - target_start));
7307 /*
7308 * at this point caclulate the number of pages beyond the original fault
7309 * address that we want to consider... this is guaranteed not to extend beyond
7310 * the current EOF...
7311 */
7312 assert((vm_size_t)(orig_start - target_start) == (orig_start - target_start));
7313 tail_size = pre_heat_size - (vm_size_t)(orig_start - target_start) - PAGE_SIZE;
7314 } else {
7315 if (pre_heat_size > target_start) {
7316 /*
7317 * since pre_heat_size is always smaller then 2^32,
7318 * if it is larger then target_start (a 64 bit value)
7319 * it is safe to clip target_start to 32 bits
7320 */
7321 pre_heat_size = (vm_size_t) target_start;
7322 }
7323 tail_size = 0;
7324 }
7325 assert( !(target_start & PAGE_MASK_64));
7326 assert( !(pre_heat_size & PAGE_MASK));
7327
7328 pre_heat_scaling[pre_heat_size / PAGE_SIZE]++;
7329
7330 if (pre_heat_size <= PAGE_SIZE)
7331 goto out;
7332
7333 if (look_behind == TRUE) {
7334 /*
7335 * take a look at the pages before the original
7336 * faulting offset... recalculate this in case
7337 * we had to clip 'pre_heat_size' above to keep
7338 * from running past the EOF.
7339 */
7340 head_size = pre_heat_size - tail_size - PAGE_SIZE;
7341
7342 for (offset = orig_start - PAGE_SIZE_64; head_size; offset -= PAGE_SIZE_64, head_size -= PAGE_SIZE) {
7343 /*
7344 * don't poke below the lowest offset
7345 */
7346 if (offset < fault_info->lo_offset)
7347 break;
7348 /*
7349 * for external objects and internal objects w/o an existence map
7350 * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN
7351 */
7352 #if MACH_PAGEMAP
7353 if (vm_external_state_get(object->existence_map, offset) == VM_EXTERNAL_STATE_ABSENT) {
7354 /*
7355 * we know for a fact that the pager can't provide the page
7356 * so don't include it or any pages beyond it in this cluster
7357 */
7358 break;
7359 }
7360 #endif
7361 if (vm_page_lookup(object, offset) != VM_PAGE_NULL) {
7362 /*
7363 * don't bridge resident pages
7364 */
7365 break;
7366 }
7367 *start = offset;
7368 *length += PAGE_SIZE;
7369 }
7370 }
7371 if (look_ahead == TRUE) {
7372 for (offset = orig_start + PAGE_SIZE_64; tail_size; offset += PAGE_SIZE_64, tail_size -= PAGE_SIZE) {
7373 /*
7374 * don't poke above the highest offset
7375 */
7376 if (offset >= fault_info->hi_offset)
7377 break;
7378 assert(offset < object_size);
7379
7380 /*
7381 * for external objects and internal objects w/o an existence map
7382 * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN
7383 */
7384 #if MACH_PAGEMAP
7385 if (vm_external_state_get(object->existence_map, offset) == VM_EXTERNAL_STATE_ABSENT) {
7386 /*
7387 * we know for a fact that the pager can't provide the page
7388 * so don't include it or any pages beyond it in this cluster
7389 */
7390 break;
7391 }
7392 #endif
7393 if (vm_page_lookup(object, offset) != VM_PAGE_NULL) {
7394 /*
7395 * don't bridge resident pages
7396 */
7397 break;
7398 }
7399 *length += PAGE_SIZE;
7400 }
7401 }
7402 out:
7403 if (*length > max_length)
7404 *length = max_length;
7405
7406 pre_heat_cluster[*length / PAGE_SIZE]++;
7407
7408 vm_object_unlock(object);
7409 }
7410
7411
7412 /*
7413 * Allow manipulation of individual page state. This is actually part of
7414 * the UPL regimen but takes place on the VM object rather than on a UPL
7415 */
7416
7417 kern_return_t
7418 vm_object_page_op(
7419 vm_object_t object,
7420 vm_object_offset_t offset,
7421 int ops,
7422 ppnum_t *phys_entry,
7423 int *flags)
7424 {
7425 vm_page_t dst_page;
7426
7427 vm_object_lock(object);
7428
7429 if(ops & UPL_POP_PHYSICAL) {
7430 if(object->phys_contiguous) {
7431 if (phys_entry) {
7432 *phys_entry = (ppnum_t)
7433 (object->vo_shadow_offset >> PAGE_SHIFT);
7434 }
7435 vm_object_unlock(object);
7436 return KERN_SUCCESS;
7437 } else {
7438 vm_object_unlock(object);
7439 return KERN_INVALID_OBJECT;
7440 }
7441 }
7442 if(object->phys_contiguous) {
7443 vm_object_unlock(object);
7444 return KERN_INVALID_OBJECT;
7445 }
7446
7447 while(TRUE) {
7448 if((dst_page = vm_page_lookup(object,offset)) == VM_PAGE_NULL) {
7449 vm_object_unlock(object);
7450 return KERN_FAILURE;
7451 }
7452
7453 /* Sync up on getting the busy bit */
7454 if((dst_page->busy || dst_page->cleaning) &&
7455 (((ops & UPL_POP_SET) &&
7456 (ops & UPL_POP_BUSY)) || (ops & UPL_POP_DUMP))) {
7457 /* someone else is playing with the page, we will */
7458 /* have to wait */
7459 PAGE_SLEEP(object, dst_page, THREAD_UNINT);
7460 continue;
7461 }
7462
7463 if (ops & UPL_POP_DUMP) {
7464 if (dst_page->pmapped == TRUE)
7465 pmap_disconnect(dst_page->phys_page);
7466
7467 VM_PAGE_FREE(dst_page);
7468 break;
7469 }
7470
7471 if (flags) {
7472 *flags = 0;
7473
7474 /* Get the condition of flags before requested ops */
7475 /* are undertaken */
7476
7477 if(dst_page->dirty) *flags |= UPL_POP_DIRTY;
7478 if(dst_page->pageout) *flags |= UPL_POP_PAGEOUT;
7479 if(dst_page->precious) *flags |= UPL_POP_PRECIOUS;
7480 if(dst_page->absent) *flags |= UPL_POP_ABSENT;
7481 if(dst_page->busy) *flags |= UPL_POP_BUSY;
7482 }
7483
7484 /* The caller should have made a call either contingent with */
7485 /* or prior to this call to set UPL_POP_BUSY */
7486 if(ops & UPL_POP_SET) {
7487 /* The protection granted with this assert will */
7488 /* not be complete. If the caller violates the */
7489 /* convention and attempts to change page state */
7490 /* without first setting busy we may not see it */
7491 /* because the page may already be busy. However */
7492 /* if such violations occur we will assert sooner */
7493 /* or later. */
7494 assert(dst_page->busy || (ops & UPL_POP_BUSY));
7495 if (ops & UPL_POP_DIRTY) dst_page->dirty = TRUE;
7496 if (ops & UPL_POP_PAGEOUT) dst_page->pageout = TRUE;
7497 if (ops & UPL_POP_PRECIOUS) dst_page->precious = TRUE;
7498 if (ops & UPL_POP_ABSENT) dst_page->absent = TRUE;
7499 if (ops & UPL_POP_BUSY) dst_page->busy = TRUE;
7500 }
7501
7502 if(ops & UPL_POP_CLR) {
7503 assert(dst_page->busy);
7504 if (ops & UPL_POP_DIRTY) dst_page->dirty = FALSE;
7505 if (ops & UPL_POP_PAGEOUT) dst_page->pageout = FALSE;
7506 if (ops & UPL_POP_PRECIOUS) dst_page->precious = FALSE;
7507 if (ops & UPL_POP_ABSENT) dst_page->absent = FALSE;
7508 if (ops & UPL_POP_BUSY) {
7509 dst_page->busy = FALSE;
7510 PAGE_WAKEUP(dst_page);
7511 }
7512 }
7513
7514 if (dst_page->encrypted) {
7515 /*
7516 * ENCRYPTED SWAP:
7517 * We need to decrypt this encrypted page before the
7518 * caller can access its contents.
7519 * But if the caller really wants to access the page's
7520 * contents, they have to keep the page "busy".
7521 * Otherwise, the page could get recycled or re-encrypted
7522 * at any time.
7523 */
7524 if ((ops & UPL_POP_SET) && (ops & UPL_POP_BUSY) &&
7525 dst_page->busy) {
7526 /*
7527 * The page is stable enough to be accessed by
7528 * the caller, so make sure its contents are
7529 * not encrypted.
7530 */
7531 vm_page_decrypt(dst_page, 0);
7532 } else {
7533 /*
7534 * The page is not busy, so don't bother
7535 * decrypting it, since anything could
7536 * happen to it between now and when the
7537 * caller wants to access it.
7538 * We should not give the caller access
7539 * to this page.
7540 */
7541 assert(!phys_entry);
7542 }
7543 }
7544
7545 if (phys_entry) {
7546 /*
7547 * The physical page number will remain valid
7548 * only if the page is kept busy.
7549 * ENCRYPTED SWAP: make sure we don't let the
7550 * caller access an encrypted page.
7551 */
7552 assert(dst_page->busy);
7553 assert(!dst_page->encrypted);
7554 *phys_entry = dst_page->phys_page;
7555 }
7556
7557 break;
7558 }
7559
7560 vm_object_unlock(object);
7561 return KERN_SUCCESS;
7562
7563 }
7564
7565 /*
7566 * vm_object_range_op offers performance enhancement over
7567 * vm_object_page_op for page_op functions which do not require page
7568 * level state to be returned from the call. Page_op was created to provide
7569 * a low-cost alternative to page manipulation via UPLs when only a single
7570 * page was involved. The range_op call establishes the ability in the _op
7571 * family of functions to work on multiple pages where the lack of page level
7572 * state handling allows the caller to avoid the overhead of the upl structures.
7573 */
7574
7575 kern_return_t
7576 vm_object_range_op(
7577 vm_object_t object,
7578 vm_object_offset_t offset_beg,
7579 vm_object_offset_t offset_end,
7580 int ops,
7581 uint32_t *range)
7582 {
7583 vm_object_offset_t offset;
7584 vm_page_t dst_page;
7585
7586 if (offset_end - offset_beg > (uint32_t) -1) {
7587 /* range is too big and would overflow "*range" */
7588 return KERN_INVALID_ARGUMENT;
7589 }
7590 if (object->resident_page_count == 0) {
7591 if (range) {
7592 if (ops & UPL_ROP_PRESENT) {
7593 *range = 0;
7594 } else {
7595 *range = (uint32_t) (offset_end - offset_beg);
7596 assert(*range == (offset_end - offset_beg));
7597 }
7598 }
7599 return KERN_SUCCESS;
7600 }
7601 vm_object_lock(object);
7602
7603 if (object->phys_contiguous) {
7604 vm_object_unlock(object);
7605 return KERN_INVALID_OBJECT;
7606 }
7607
7608 offset = offset_beg & ~PAGE_MASK_64;
7609
7610 while (offset < offset_end) {
7611 dst_page = vm_page_lookup(object, offset);
7612 if (dst_page != VM_PAGE_NULL) {
7613 if (ops & UPL_ROP_DUMP) {
7614 if (dst_page->list_req_pending) {
7615 /*
7616 * This page isn't on a UPL yet.
7617 * So it's safe to steal it here and dump it.
7618 */
7619 } else if (dst_page->busy || dst_page->cleaning) {
7620 /*
7621 * someone else is playing with the
7622 * page, we will have to wait
7623 */
7624 PAGE_SLEEP(object, dst_page, THREAD_UNINT);
7625 /*
7626 * need to relook the page up since it's
7627 * state may have changed while we slept
7628 * it might even belong to a different object
7629 * at this point
7630 */
7631 continue;
7632 }
7633 if (dst_page->pmapped == TRUE)
7634 pmap_disconnect(dst_page->phys_page);
7635
7636 VM_PAGE_FREE(dst_page);
7637
7638 } else if ((ops & UPL_ROP_ABSENT) && !dst_page->absent)
7639 break;
7640 } else if (ops & UPL_ROP_PRESENT)
7641 break;
7642
7643 offset += PAGE_SIZE;
7644 }
7645 vm_object_unlock(object);
7646
7647 if (range) {
7648 if (offset > offset_end)
7649 offset = offset_end;
7650 if(offset > offset_beg) {
7651 *range = (uint32_t) (offset - offset_beg);
7652 assert(*range == (offset - offset_beg));
7653 } else {
7654 *range = 0;
7655 }
7656 }
7657 return KERN_SUCCESS;
7658 }
7659
7660
7661 uint32_t scan_object_collision = 0;
7662
7663 void
7664 vm_object_lock(vm_object_t object)
7665 {
7666 if (object == vm_pageout_scan_wants_object) {
7667 scan_object_collision++;
7668 mutex_pause(2);
7669 }
7670 lck_rw_lock_exclusive(&object->Lock);
7671 }
7672
7673 boolean_t
7674 vm_object_lock_avoid(vm_object_t object)
7675 {
7676 if (object == vm_pageout_scan_wants_object) {
7677 scan_object_collision++;
7678 return TRUE;
7679 }
7680 return FALSE;
7681 }
7682
7683 boolean_t
7684 _vm_object_lock_try(vm_object_t object)
7685 {
7686 return (lck_rw_try_lock_exclusive(&object->Lock));
7687 }
7688
7689 boolean_t
7690 vm_object_lock_try(vm_object_t object)
7691 {
7692 /*
7693 * Called from hibernate path so check before blocking.
7694 */
7695 if (vm_object_lock_avoid(object) && ml_get_interrupts_enabled() && get_preemption_level()==0) {
7696 mutex_pause(2);
7697 }
7698 return _vm_object_lock_try(object);
7699 }
7700
7701 void
7702 vm_object_lock_shared(vm_object_t object)
7703 {
7704 if (vm_object_lock_avoid(object)) {
7705 mutex_pause(2);
7706 }
7707 lck_rw_lock_shared(&object->Lock);
7708 }
7709
7710 boolean_t
7711 vm_object_lock_try_shared(vm_object_t object)
7712 {
7713 if (vm_object_lock_avoid(object)) {
7714 mutex_pause(2);
7715 }
7716 return (lck_rw_try_lock_shared(&object->Lock));
7717 }
7718
7719
7720 unsigned int vm_object_change_wimg_mode_count = 0;
7721
7722 /*
7723 * The object must be locked
7724 */
7725 void
7726 vm_object_change_wimg_mode(vm_object_t object, unsigned int wimg_mode)
7727 {
7728 vm_page_t p;
7729
7730 vm_object_lock_assert_exclusive(object);
7731
7732 vm_object_paging_wait(object, THREAD_UNINT);
7733
7734 queue_iterate(&object->memq, p, vm_page_t, listq) {
7735
7736 if (!p->fictitious)
7737 pmap_set_cache_attributes(p->phys_page, wimg_mode);
7738 }
7739 if (wimg_mode == VM_WIMG_USE_DEFAULT)
7740 object->set_cache_attr = FALSE;
7741 else
7742 object->set_cache_attr = TRUE;
7743
7744 object->wimg_bits = wimg_mode;
7745
7746 vm_object_change_wimg_mode_count++;
7747 }
7748
7749 #if CONFIG_FREEZE
7750
7751 __private_extern__ void default_freezer_pack_page(vm_page_t , vm_object_t , vm_object_offset_t, void**);
7752 __private_extern__ void default_freezer_unpack(vm_object_t , void**);
7753
7754 kern_return_t vm_object_pack(
7755 unsigned int *purgeable_count,
7756 unsigned int *wired_count,
7757 unsigned int *clean_count,
7758 unsigned int *dirty_count,
7759 boolean_t *shared,
7760 vm_object_t src_object,
7761 vm_object_t compact_object,
7762 void **table,
7763 vm_object_offset_t *offset)
7764 {
7765 kern_return_t kr = KERN_SUCCESS;
7766
7767 vm_object_lock(src_object);
7768
7769 *purgeable_count = *wired_count = *clean_count = *dirty_count = 0;
7770 *shared = FALSE;
7771
7772 if (!src_object->alive || src_object->terminating){
7773 kr = KERN_FAILURE;
7774 goto done;
7775 }
7776
7777 if (src_object->purgable == VM_PURGABLE_VOLATILE) {
7778 *purgeable_count = src_object->resident_page_count;
7779
7780 /* If the destination object is null, we're just walking the pages to discover how many can be hibernated */
7781 if (VM_OBJECT_NULL != compact_object) {
7782 purgeable_q_t queue;
7783 /* object should be on a queue */
7784 assert(src_object->objq.next != NULL &&
7785 src_object->objq.prev != NULL);
7786 queue = vm_purgeable_object_remove(src_object);
7787 assert(queue);
7788 vm_page_lock_queues();
7789 vm_purgeable_token_delete_first(queue);
7790 vm_page_unlock_queues();
7791 vm_object_purge(src_object);
7792 }
7793 goto done;
7794 }
7795
7796 if (src_object->ref_count == 1) {
7797 vm_object_pack_pages(wired_count, clean_count, dirty_count, src_object, compact_object, table, offset);
7798 } else {
7799 if (src_object->internal) {
7800 *shared = TRUE;
7801 }
7802 }
7803 done:
7804 vm_object_unlock(src_object);
7805
7806 return kr;
7807 }
7808
7809
7810 void
7811 vm_object_pack_pages(
7812 unsigned int *wired_count,
7813 unsigned int *clean_count,
7814 unsigned int *dirty_count,
7815 vm_object_t src_object,
7816 vm_object_t compact_object,
7817 void **table,
7818 vm_object_offset_t *offset)
7819 {
7820 vm_page_t p, next;
7821
7822 next = (vm_page_t)queue_first(&src_object->memq);
7823
7824 /* Since this function is dual purpose in order that we can count
7825 * the freezable pages as well as prepare them, assert that our
7826 * arguments are sane. Gnarly, but avoids code duplication.
7827 */
7828 if (VM_OBJECT_NULL == compact_object){
7829 assert(!table);
7830 assert(!offset);
7831 } else {
7832 assert(table);
7833 assert(offset);
7834 }
7835
7836 while (!queue_end(&src_object->memq, (queue_entry_t)next)) {
7837 p = next;
7838 next = (vm_page_t)queue_next(&next->listq);
7839
7840 if (p->fictitious || p->busy )
7841 continue;
7842
7843 if (p->absent || p->unusual || p->error)
7844 continue;
7845
7846 if (VM_PAGE_WIRED(p)) {
7847 (*wired_count)++;
7848 continue;
7849 }
7850
7851 if (VM_OBJECT_NULL == compact_object) {
7852 if (p->dirty || pmap_is_modified(p->phys_page)) {
7853 (*dirty_count)++;
7854 } else {
7855 (*clean_count)++;
7856 }
7857 continue;
7858 }
7859
7860 if (p->cleaning) {
7861 p->busy = TRUE;
7862 p->pageout = TRUE;
7863 p->dump_cleaning = TRUE;
7864
7865 vm_page_lockspin_queues();
7866 vm_page_wire(p);
7867 vm_page_unlock_queues();
7868
7869 continue;
7870 }
7871
7872 if (p->pmapped == TRUE) {
7873 int refmod_state;
7874 refmod_state = pmap_disconnect(p->phys_page);
7875 if (refmod_state & VM_MEM_MODIFIED) {
7876 p->dirty = TRUE;
7877 }
7878 }
7879
7880 if (p->dirty) {
7881 p->busy = TRUE;
7882
7883 default_freezer_pack_page(p, compact_object, *offset, table);
7884 *offset += PAGE_SIZE;
7885
7886 (*dirty_count)++;
7887 }
7888 else {
7889 VM_PAGE_FREE(p);
7890 (*clean_count)++;
7891 }
7892 }
7893 }
7894
7895 void
7896 vm_object_pageout(
7897 vm_object_t object)
7898 {
7899 vm_page_t p, next;
7900
7901 assert(object != VM_OBJECT_NULL );
7902
7903 vm_object_lock(object);
7904
7905 next = (vm_page_t)queue_first(&object->memq);
7906
7907 while (!queue_end(&object->memq, (queue_entry_t)next)) {
7908 p = next;
7909 next = (vm_page_t)queue_next(&next->listq);
7910
7911 /* Throw to the pageout queue */
7912 vm_page_lockspin_queues();
7913
7914 VM_PAGE_QUEUES_REMOVE(p);
7915 vm_pageout_cluster(p);
7916
7917 vm_page_unlock_queues();
7918 }
7919
7920 vm_object_unlock(object);
7921 }
7922
7923 kern_return_t
7924 vm_object_pagein(
7925 vm_object_t object)
7926 {
7927 memory_object_t pager;
7928 kern_return_t kr;
7929
7930 vm_object_lock(object);
7931
7932 pager = object->pager;
7933
7934 if (!object->pager_ready || pager == MEMORY_OBJECT_NULL) {
7935 vm_object_unlock(object);
7936 return KERN_FAILURE;
7937 }
7938
7939 vm_object_paging_wait(object, THREAD_UNINT);
7940 vm_object_paging_begin(object);
7941
7942 object->blocked_access = TRUE;
7943 vm_object_unlock(object);
7944
7945 kr = memory_object_data_reclaim(pager, TRUE);
7946
7947 vm_object_lock(object);
7948
7949 object->blocked_access = FALSE;
7950 vm_object_paging_end(object);
7951
7952 vm_object_unlock(object);
7953
7954 return kr;
7955 }
7956
7957 void
7958 vm_object_unpack(
7959 vm_object_t compact_object,
7960 void **table)
7961 {
7962 /*
7963 * Future Work:
7964 * Right now we treat the default freezer much like
7965 * the default pager with respect to when it is
7966 * created and terminated.
7967 * But, in the future, we may want to terminate the
7968 * default freezer at the very instant that an object
7969 * has been completely re-filled with all it's previously
7970 * paged-out pages.
7971 * At that time we'll need to reset the object fields like
7972 * "pager" and the associated "pager_{created,initialized,trusted}"
7973 * fields right here.
7974 */
7975 default_freezer_unpack(compact_object, table);
7976 }
7977
7978 #endif /* CONFIG_FREEZE */