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