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