]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_object.h
xnu-3248.50.21.tar.gz
[apple/xnu.git] / osfmk / vm / vm_object.h
1 /*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: vm_object.h
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
61 * Date: 1985
62 *
63 * Virtual memory object module definitions.
64 */
65
66 #ifndef _VM_VM_OBJECT_H_
67 #define _VM_VM_OBJECT_H_
68
69 #include <debug.h>
70 #include <mach_assert.h>
71 #include <mach_pagemap.h>
72 #include <task_swapper.h>
73
74 #include <mach/kern_return.h>
75 #include <mach/boolean.h>
76 #include <mach/memory_object_types.h>
77 #include <mach/port.h>
78 #include <mach/vm_prot.h>
79 #include <mach/vm_param.h>
80 #include <mach/machine/vm_types.h>
81 #include <kern/queue.h>
82 #include <kern/locks.h>
83 #include <kern/assert.h>
84 #include <kern/misc_protos.h>
85 #include <kern/macro_help.h>
86 #include <ipc/ipc_types.h>
87 #include <vm/pmap.h>
88
89 #include <vm/vm_external.h>
90
91 #include <vm/vm_options.h>
92
93 #if VM_OBJECT_TRACKING
94 #include <libkern/OSDebug.h>
95 #include <kern/btlog.h>
96 extern void vm_object_tracking_init(void);
97 extern boolean_t vm_object_tracking_inited;
98 extern btlog_t *vm_object_tracking_btlog;
99 #define VM_OBJECT_TRACKING_BTDEPTH 7
100 #define VM_OBJECT_TRACKING_OP_CREATED 1
101 #define VM_OBJECT_TRACKING_OP_MODIFIED 2
102 #define VM_OBJECT_TRACKING_OP_TRUESHARE 3
103 #endif /* VM_OBJECT_TRACKING */
104
105 struct vm_page;
106 struct vm_shared_region_slide_info;
107
108 /*
109 * Types defined:
110 *
111 * vm_object_t Virtual memory object.
112 * vm_object_fault_info_t Used to determine cluster size.
113 */
114
115 struct vm_object_fault_info {
116 int interruptible;
117 uint32_t user_tag;
118 vm_size_t cluster_size;
119 vm_behavior_t behavior;
120 vm_map_offset_t lo_offset;
121 vm_map_offset_t hi_offset;
122 unsigned int
123 /* boolean_t */ no_cache:1,
124 /* boolean_t */ stealth:1,
125 /* boolean_t */ io_sync:1,
126 /* boolean_t */ cs_bypass:1,
127 /* boolean_t */ mark_zf_absent:1,
128 /* boolean_t */ batch_pmap_op:1,
129 __vm_object_fault_info_unused_bits:26;
130 int pmap_options;
131 };
132
133
134 #define vo_size vo_un1.vou_size
135 #define vo_cache_pages_to_scan vo_un1.vou_cache_pages_to_scan
136 #define vo_shadow_offset vo_un2.vou_shadow_offset
137 #define vo_cache_ts vo_un2.vou_cache_ts
138 #define vo_purgeable_owner vo_un2.vou_purgeable_owner
139 #define vo_slide_info vo_un2.vou_slide_info
140
141 struct vm_object {
142 queue_head_t memq; /* Resident memory */
143 lck_rw_t Lock; /* Synchronization */
144
145 union {
146 vm_object_size_t vou_size; /* Object size (only valid if internal) */
147 int vou_cache_pages_to_scan; /* pages yet to be visited in an
148 * external object in cache
149 */
150 } vo_un1;
151
152 struct vm_page *memq_hint;
153 int ref_count; /* Number of references */
154 #if TASK_SWAPPER
155 int res_count; /* Residency references (swap)*/
156 #endif /* TASK_SWAPPER */
157 unsigned int resident_page_count;
158 /* number of resident pages */
159 unsigned int wired_page_count; /* number of wired pages */
160 unsigned int reusable_page_count;
161
162 struct vm_object *copy; /* Object that should receive
163 * a copy of my changed pages,
164 * for copy_delay, or just the
165 * temporary object that
166 * shadows this object, for
167 * copy_call.
168 */
169 struct vm_object *shadow; /* My shadow */
170
171 union {
172 vm_object_offset_t vou_shadow_offset; /* Offset into shadow */
173 clock_sec_t vou_cache_ts; /* age of an external object
174 * present in cache
175 */
176 task_t vou_purgeable_owner; /* If the purg'a'ble bits below are set
177 * to volatile/emtpy, this is the task
178 * that owns this purgeable object.
179 */
180 struct vm_shared_region_slide_info *vou_slide_info;
181 } vo_un2;
182
183 memory_object_t pager; /* Where to get data */
184 vm_object_offset_t paging_offset; /* Offset into memory object */
185 memory_object_control_t pager_control; /* Where data comes back */
186
187 memory_object_copy_strategy_t
188 copy_strategy; /* How to handle data copy */
189
190 #if __LP64__
191 /*
192 * Some user processes (mostly VirtualMachine software) take a large
193 * number of UPLs (via IOMemoryDescriptors) to wire pages in large
194 * VM objects and overflow the 16-bit "activity_in_progress" counter.
195 * Since we never enforced any limit there, let's give them 32 bits
196 * for backwards compatibility's sake.
197 */
198 unsigned int paging_in_progress:16,
199 __object1_unused_bits:16;
200 unsigned int activity_in_progress;
201 #else /* __LP64__ */
202 /*
203 * On 32-bit platforms, enlarging "activity_in_progress" would increase
204 * the size of "struct vm_object". Since we don't know of any actual
205 * overflow of these counters on these platforms, let's keep the
206 * counters as 16-bit integers.
207 */
208 unsigned short paging_in_progress;
209 unsigned short activity_in_progress;
210 #endif /* __LP64__ */
211 /* The memory object ports are
212 * being used (e.g., for pagein
213 * or pageout) -- don't change
214 * any of these fields (i.e.,
215 * don't collapse, destroy or
216 * terminate)
217 */
218
219 unsigned int
220 /* boolean_t array */ all_wanted:11, /* Bit array of "want to be
221 * awakened" notations. See
222 * VM_OBJECT_EVENT_* items
223 * below */
224 /* boolean_t */ pager_created:1, /* Has pager been created? */
225 /* boolean_t */ pager_initialized:1, /* Are fields ready to use? */
226 /* boolean_t */ pager_ready:1, /* Will pager take requests? */
227
228 /* boolean_t */ pager_trusted:1,/* The pager for this object
229 * is trusted. This is true for
230 * all internal objects (backed
231 * by the default pager)
232 */
233 /* boolean_t */ can_persist:1, /* The kernel may keep the data
234 * for this object (and rights
235 * to the memory object) after
236 * all address map references
237 * are deallocated?
238 */
239 /* boolean_t */ internal:1, /* Created by the kernel (and
240 * therefore, managed by the
241 * default memory manger)
242 */
243 /* boolean_t */ temporary:1, /* Permanent objects may be
244 * changed externally by the
245 * memory manager, and changes
246 * made in memory must be
247 * reflected back to the memory
248 * manager. Temporary objects
249 * lack both of these
250 * characteristics.
251 */
252 /* boolean_t */ private:1, /* magic device_pager object,
253 * holds private pages only */
254 /* boolean_t */ pageout:1, /* pageout object. contains
255 * private pages that refer to
256 * a real memory object. */
257 /* boolean_t */ alive:1, /* Not yet terminated */
258
259 /* boolean_t */ purgable:2, /* Purgable state. See
260 * VM_PURGABLE_*
261 */
262 /* boolean_t */ purgeable_when_ripe:1, /* Purgeable when a token
263 * becomes ripe.
264 */
265 /* boolean_t */ shadowed:1, /* Shadow may exist */
266 /* boolean_t */ advisory_pageout:1,
267 /* Instead of sending page
268 * via OOL, just notify
269 * pager that the kernel
270 * wants to discard it, page
271 * remains in object */
272 /* boolean_t */ true_share:1,
273 /* This object is mapped
274 * in more than one place
275 * and hence cannot be
276 * coalesced */
277 /* boolean_t */ terminating:1,
278 /* Allows vm_object_lookup
279 * and vm_object_deallocate
280 * to special case their
281 * behavior when they are
282 * called as a result of
283 * page cleaning during
284 * object termination
285 */
286 /* boolean_t */ named:1, /* An enforces an internal
287 * naming convention, by
288 * calling the right routines
289 * for allocation and
290 * destruction, UBC references
291 * against the vm_object are
292 * checked.
293 */
294 /* boolean_t */ shadow_severed:1,
295 /* When a permanent object
296 * backing a COW goes away
297 * unexpectedly. This bit
298 * allows vm_fault to return
299 * an error rather than a
300 * zero filled page.
301 */
302 /* boolean_t */ phys_contiguous:1,
303 /* Memory is wired and
304 * guaranteed physically
305 * contiguous. However
306 * it is not device memory
307 * and obeys normal virtual
308 * memory rules w.r.t pmap
309 * access bits.
310 */
311 /* boolean_t */ nophyscache:1;
312 /* When mapped at the
313 * pmap level, don't allow
314 * primary caching. (for
315 * I/O)
316 */
317
318
319
320 queue_chain_t cached_list; /* Attachment point for the
321 * list of objects cached as a
322 * result of their can_persist
323 * value
324 */
325
326 queue_head_t msr_q; /* memory object synchronise
327 request queue */
328
329 /*
330 * the following fields are not protected by any locks
331 * they are updated via atomic compare and swap
332 */
333 vm_object_offset_t last_alloc; /* last allocation offset */
334 int sequential; /* sequential access size */
335
336 uint32_t pages_created;
337 uint32_t pages_used;
338 #if MACH_PAGEMAP
339 vm_external_map_t existence_map; /* bitmap of pages written to
340 * backing storage */
341 #endif /* MACH_PAGEMAP */
342 vm_offset_t cow_hint; /* last page present in */
343 /* shadow but not in object */
344 #if MACH_ASSERT
345 struct vm_object *paging_object; /* object which pages to be
346 * swapped out are temporary
347 * put in current object
348 */
349 #endif
350 /* hold object lock when altering */
351 unsigned int
352 wimg_bits:8, /* cache WIMG bits */
353 code_signed:1, /* pages are signed and should be
354 validated; the signatures are stored
355 with the pager */
356 hashed:1, /* object/pager entered in hash */
357 transposed:1, /* object was transposed with another */
358 mapping_in_progress:1, /* pager being mapped/unmapped */
359 phantom_isssd:1,
360 volatile_empty:1,
361 volatile_fault:1,
362 all_reusable:1,
363 blocked_access:1,
364 set_cache_attr:1,
365 object_slid:1,
366 purgeable_queue_type:2,
367 purgeable_queue_group:3,
368 io_tracking:1,
369 __object2_unused_bits:7; /* for expansion */
370
371 uint8_t scan_collisions;
372 vm_tag_t wire_tag;
373 uint8_t __object4_unused_bits[2];
374
375 #if CONFIG_PHANTOM_CACHE
376 uint32_t phantom_object_id;
377 #endif
378 #if CONFIG_IOSCHED || UPL_DEBUG
379 queue_head_t uplq; /* List of outstanding upls */
380 #endif
381
382 #ifdef VM_PIP_DEBUG
383 /*
384 * Keep track of the stack traces for the first holders
385 * of a "paging_in_progress" reference for this VM object.
386 */
387 #define VM_PIP_DEBUG_STACK_FRAMES 25 /* depth of each stack trace */
388 #define VM_PIP_DEBUG_MAX_REFS 10 /* track that many references */
389 struct __pip_backtrace {
390 void *pip_retaddr[VM_PIP_DEBUG_STACK_FRAMES];
391 } pip_holders[VM_PIP_DEBUG_MAX_REFS];
392 #endif /* VM_PIP_DEBUG */
393
394 queue_chain_t objq; /* object queue - currently used for purgable queues */
395
396 #if DEBUG
397 void *purgeable_owner_bt[16];
398 task_t vo_purgeable_volatilizer; /* who made it volatile? */
399 void *purgeable_volatilizer_bt[16];
400 #endif /* DEBUG */
401 };
402
403 #define VM_OBJECT_PURGEABLE_FAULT_ERROR(object) \
404 ((object)->volatile_fault && \
405 ((object)->purgable == VM_PURGABLE_VOLATILE || \
406 (object)->purgable == VM_PURGABLE_EMPTY))
407
408 extern
409 vm_object_t kernel_object; /* the single kernel object */
410
411 extern
412 vm_object_t compressor_object; /* the single compressor object */
413
414 extern
415 unsigned int vm_object_absent_max; /* maximum number of absent pages
416 at a time for each object */
417
418 # define VM_MSYNC_INITIALIZED 0
419 # define VM_MSYNC_SYNCHRONIZING 1
420 # define VM_MSYNC_DONE 2
421
422 struct msync_req {
423 queue_chain_t msr_q; /* object request queue */
424 queue_chain_t req_q; /* vm_msync request queue */
425 unsigned int flag;
426 vm_object_offset_t offset;
427 vm_object_size_t length;
428 vm_object_t object; /* back pointer */
429 decl_lck_mtx_data(, msync_req_lock) /* Lock for this structure */
430 };
431
432 typedef struct msync_req *msync_req_t;
433 #define MSYNC_REQ_NULL ((msync_req_t) 0)
434
435
436 extern lck_grp_t vm_map_lck_grp;
437 extern lck_attr_t vm_map_lck_attr;
438
439 /*
440 * Macros to allocate and free msync_reqs
441 */
442 #define msync_req_alloc(msr) \
443 MACRO_BEGIN \
444 (msr) = (msync_req_t)kalloc(sizeof(struct msync_req)); \
445 lck_mtx_init(&(msr)->msync_req_lock, &vm_map_lck_grp, &vm_map_lck_attr); \
446 msr->flag = VM_MSYNC_INITIALIZED; \
447 MACRO_END
448
449 #define msync_req_free(msr) \
450 MACRO_BEGIN \
451 lck_mtx_destroy(&(msr)->msync_req_lock, &vm_map_lck_grp); \
452 kfree((msr), sizeof(struct msync_req)); \
453 MACRO_END
454
455 #define msr_lock(msr) lck_mtx_lock(&(msr)->msync_req_lock)
456 #define msr_unlock(msr) lck_mtx_unlock(&(msr)->msync_req_lock)
457
458 #define VM_OBJECT_WIRED(object) \
459 MACRO_BEGIN \
460 if ((object)->purgable == VM_PURGABLE_DENY) \
461 { \
462 lck_spin_lock(&vm_objects_wired_lock); \
463 assert(!(object)->objq.next); \
464 queue_enter(&vm_objects_wired, (object), vm_object_t, objq); \
465 lck_spin_unlock(&vm_objects_wired_lock); \
466 } \
467 MACRO_END
468
469 #define VM_OBJECT_UNWIRED(object) \
470 MACRO_BEGIN \
471 (object)->wire_tag = VM_KERN_MEMORY_NONE; \
472 if (((object)->purgable == VM_PURGABLE_DENY) && (object)->objq.next) \
473 { \
474 lck_spin_lock(&vm_objects_wired_lock); \
475 queue_remove(&vm_objects_wired, (object), vm_object_t, objq); \
476 lck_spin_unlock(&vm_objects_wired_lock); \
477 } \
478 MACRO_END
479
480
481 /*
482 * Declare procedures that operate on VM objects.
483 */
484
485 __private_extern__ void vm_object_bootstrap(void);
486
487 __private_extern__ void vm_object_init(void);
488
489 __private_extern__ void vm_object_init_lck_grp(void);
490
491 __private_extern__ void vm_object_reaper_init(void);
492
493 __private_extern__ vm_object_t vm_object_allocate(vm_object_size_t size);
494
495 __private_extern__ void _vm_object_allocate(vm_object_size_t size,
496 vm_object_t object);
497
498 #if TASK_SWAPPER
499
500 __private_extern__ void vm_object_res_reference(
501 vm_object_t object);
502 __private_extern__ void vm_object_res_deallocate(
503 vm_object_t object);
504 #define VM_OBJ_RES_INCR(object) (object)->res_count++
505 #define VM_OBJ_RES_DECR(object) (object)->res_count--
506
507 #else /* TASK_SWAPPER */
508
509 #define VM_OBJ_RES_INCR(object)
510 #define VM_OBJ_RES_DECR(object)
511 #define vm_object_res_reference(object)
512 #define vm_object_res_deallocate(object)
513
514 #endif /* TASK_SWAPPER */
515
516 #define vm_object_reference_locked(object) \
517 MACRO_BEGIN \
518 vm_object_t RLObject = (object); \
519 vm_object_lock_assert_exclusive(object); \
520 assert((RLObject)->ref_count > 0); \
521 (RLObject)->ref_count++; \
522 assert((RLObject)->ref_count > 1); \
523 vm_object_res_reference(RLObject); \
524 MACRO_END
525
526
527 #define vm_object_reference_shared(object) \
528 MACRO_BEGIN \
529 vm_object_t RLObject = (object); \
530 vm_object_lock_assert_shared(object); \
531 assert((RLObject)->ref_count > 0); \
532 OSAddAtomic(1, &(RLObject)->ref_count); \
533 assert((RLObject)->ref_count > 0); \
534 /* XXX we would need an atomic version of the following ... */ \
535 vm_object_res_reference(RLObject); \
536 MACRO_END
537
538
539 __private_extern__ void vm_object_reference(
540 vm_object_t object);
541
542 #if !MACH_ASSERT
543
544 #define vm_object_reference(object) \
545 MACRO_BEGIN \
546 vm_object_t RObject = (object); \
547 if (RObject) { \
548 vm_object_lock_shared(RObject); \
549 vm_object_reference_shared(RObject); \
550 vm_object_unlock(RObject); \
551 } \
552 MACRO_END
553
554 #endif /* MACH_ASSERT */
555
556 __private_extern__ void vm_object_deallocate(
557 vm_object_t object);
558
559 __private_extern__ kern_return_t vm_object_release_name(
560 vm_object_t object,
561 int flags);
562
563 __private_extern__ void vm_object_pmap_protect(
564 vm_object_t object,
565 vm_object_offset_t offset,
566 vm_object_size_t size,
567 pmap_t pmap,
568 vm_map_offset_t pmap_start,
569 vm_prot_t prot);
570
571 __private_extern__ void vm_object_pmap_protect_options(
572 vm_object_t object,
573 vm_object_offset_t offset,
574 vm_object_size_t size,
575 pmap_t pmap,
576 vm_map_offset_t pmap_start,
577 vm_prot_t prot,
578 int options);
579
580 __private_extern__ void vm_object_page_remove(
581 vm_object_t object,
582 vm_object_offset_t start,
583 vm_object_offset_t end);
584
585 __private_extern__ void vm_object_deactivate_pages(
586 vm_object_t object,
587 vm_object_offset_t offset,
588 vm_object_size_t size,
589 boolean_t kill_page,
590 boolean_t reusable_page,
591 struct pmap *pmap,
592 vm_map_offset_t pmap_offset);
593
594 __private_extern__ void vm_object_reuse_pages(
595 vm_object_t object,
596 vm_object_offset_t start_offset,
597 vm_object_offset_t end_offset,
598 boolean_t allow_partial_reuse);
599
600 __private_extern__ void vm_object_purge(
601 vm_object_t object,
602 int flags);
603
604 __private_extern__ kern_return_t vm_object_purgable_control(
605 vm_object_t object,
606 vm_purgable_t control,
607 int *state);
608
609 __private_extern__ kern_return_t vm_object_get_page_counts(
610 vm_object_t object,
611 vm_object_offset_t offset,
612 vm_object_size_t size,
613 unsigned int *resident_page_count,
614 unsigned int *dirty_page_count);
615
616 __private_extern__ boolean_t vm_object_coalesce(
617 vm_object_t prev_object,
618 vm_object_t next_object,
619 vm_object_offset_t prev_offset,
620 vm_object_offset_t next_offset,
621 vm_object_size_t prev_size,
622 vm_object_size_t next_size);
623
624 __private_extern__ boolean_t vm_object_shadow(
625 vm_object_t *object,
626 vm_object_offset_t *offset,
627 vm_object_size_t length);
628
629 __private_extern__ void vm_object_collapse(
630 vm_object_t object,
631 vm_object_offset_t offset,
632 boolean_t can_bypass);
633
634 __private_extern__ boolean_t vm_object_copy_quickly(
635 vm_object_t *_object,
636 vm_object_offset_t src_offset,
637 vm_object_size_t size,
638 boolean_t *_src_needs_copy,
639 boolean_t *_dst_needs_copy);
640
641 __private_extern__ kern_return_t vm_object_copy_strategically(
642 vm_object_t src_object,
643 vm_object_offset_t src_offset,
644 vm_object_size_t size,
645 vm_object_t *dst_object,
646 vm_object_offset_t *dst_offset,
647 boolean_t *dst_needs_copy);
648
649 __private_extern__ kern_return_t vm_object_copy_slowly(
650 vm_object_t src_object,
651 vm_object_offset_t src_offset,
652 vm_object_size_t size,
653 boolean_t interruptible,
654 vm_object_t *_result_object);
655
656 __private_extern__ vm_object_t vm_object_copy_delayed(
657 vm_object_t src_object,
658 vm_object_offset_t src_offset,
659 vm_object_size_t size,
660 boolean_t src_object_shared);
661
662
663
664 __private_extern__ kern_return_t vm_object_destroy(
665 vm_object_t object,
666 kern_return_t reason);
667
668 __private_extern__ void vm_object_pager_create(
669 vm_object_t object);
670
671 __private_extern__ void vm_object_compressor_pager_create(
672 vm_object_t object);
673
674 __private_extern__ void vm_object_page_map(
675 vm_object_t object,
676 vm_object_offset_t offset,
677 vm_object_size_t size,
678 vm_object_offset_t (*map_fn)
679 (void *, vm_object_offset_t),
680 void *map_fn_data);
681
682 __private_extern__ kern_return_t vm_object_upl_request(
683 vm_object_t object,
684 vm_object_offset_t offset,
685 upl_size_t size,
686 upl_t *upl,
687 upl_page_info_t *page_info,
688 unsigned int *count,
689 upl_control_flags_t flags);
690
691 __private_extern__ kern_return_t vm_object_transpose(
692 vm_object_t object1,
693 vm_object_t object2,
694 vm_object_size_t transpose_size);
695
696 __private_extern__ boolean_t vm_object_sync(
697 vm_object_t object,
698 vm_object_offset_t offset,
699 vm_object_size_t size,
700 boolean_t should_flush,
701 boolean_t should_return,
702 boolean_t should_iosync);
703
704 __private_extern__ kern_return_t vm_object_update(
705 vm_object_t object,
706 vm_object_offset_t offset,
707 vm_object_size_t size,
708 vm_object_offset_t *error_offset,
709 int *io_errno,
710 memory_object_return_t should_return,
711 int flags,
712 vm_prot_t prot);
713
714 __private_extern__ kern_return_t vm_object_lock_request(
715 vm_object_t object,
716 vm_object_offset_t offset,
717 vm_object_size_t size,
718 memory_object_return_t should_return,
719 int flags,
720 vm_prot_t prot);
721
722
723
724 __private_extern__ vm_object_t vm_object_enter(
725 memory_object_t pager,
726 vm_object_size_t size,
727 boolean_t internal,
728 boolean_t init,
729 boolean_t check_named);
730
731
732 __private_extern__ void vm_object_cluster_size(
733 vm_object_t object,
734 vm_object_offset_t *start,
735 vm_size_t *length,
736 vm_object_fault_info_t fault_info,
737 uint32_t *io_streaming);
738
739 __private_extern__ kern_return_t vm_object_populate_with_private(
740 vm_object_t object,
741 vm_object_offset_t offset,
742 ppnum_t phys_page,
743 vm_size_t size);
744
745 __private_extern__ void vm_object_change_wimg_mode(
746 vm_object_t object,
747 unsigned int wimg_mode);
748
749 extern kern_return_t adjust_vm_object_cache(
750 vm_size_t oval,
751 vm_size_t nval);
752
753 extern kern_return_t vm_object_page_op(
754 vm_object_t object,
755 vm_object_offset_t offset,
756 int ops,
757 ppnum_t *phys_entry,
758 int *flags);
759
760 extern kern_return_t vm_object_range_op(
761 vm_object_t object,
762 vm_object_offset_t offset_beg,
763 vm_object_offset_t offset_end,
764 int ops,
765 uint32_t *range);
766
767
768 __private_extern__ void vm_object_reap_pages(
769 vm_object_t object,
770 int reap_type);
771 #define REAP_REAP 0
772 #define REAP_TERMINATE 1
773 #define REAP_PURGEABLE 2
774 #define REAP_DATA_FLUSH 3
775
776 #if CONFIG_FREEZE
777 struct default_freezer_handle;
778
779 __private_extern__ kern_return_t
780 vm_object_pack(
781 unsigned int *purgeable_count,
782 unsigned int *wired_count,
783 unsigned int *clean_count,
784 unsigned int *dirty_count,
785 unsigned int dirty_budget,
786 boolean_t *shared,
787 vm_object_t src_object,
788 struct default_freezer_handle *df_handle);
789
790 __private_extern__ void
791 vm_object_pack_pages(
792 unsigned int *wired_count,
793 unsigned int *clean_count,
794 unsigned int *dirty_count,
795 unsigned int dirty_budget,
796 vm_object_t src_object,
797 struct default_freezer_handle *df_handle);
798
799 __private_extern__ void
800 vm_object_compressed_freezer_pageout(
801 vm_object_t object);
802
803 __private_extern__ void
804 vm_object_compressed_freezer_done(
805 void);
806
807 __private_extern__ kern_return_t
808 vm_object_pagein(
809 vm_object_t object);
810 #endif /* CONFIG_FREEZE */
811
812 __private_extern__ void
813 vm_object_pageout(
814 vm_object_t object);
815
816 #if CONFIG_IOSCHED
817 struct io_reprioritize_req {
818 uint64_t blkno;
819 uint32_t len;
820 int priority;
821 struct vnode *devvp;
822 queue_chain_t io_reprioritize_list;
823 };
824 typedef struct io_reprioritize_req *io_reprioritize_req_t;
825
826 extern void vm_io_reprioritize_init(void);
827 #endif
828
829 /*
830 * Event waiting handling
831 */
832
833 #define VM_OBJECT_EVENT_INITIALIZED 0
834 #define VM_OBJECT_EVENT_PAGER_READY 1
835 #define VM_OBJECT_EVENT_PAGING_IN_PROGRESS 2
836 #define VM_OBJECT_EVENT_MAPPING_IN_PROGRESS 3
837 #define VM_OBJECT_EVENT_LOCK_IN_PROGRESS 4
838 #define VM_OBJECT_EVENT_UNCACHING 5
839 #define VM_OBJECT_EVENT_COPY_CALL 6
840 #define VM_OBJECT_EVENT_CACHING 7
841 #define VM_OBJECT_EVENT_UNBLOCKED 8
842 #define VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS 9
843
844 #define vm_object_assert_wait(object, event, interruptible) \
845 (((object)->all_wanted |= 1 << (event)), \
846 assert_wait((event_t)((vm_offset_t)(object)+(event)),(interruptible)))
847
848 #define vm_object_wait(object, event, interruptible) \
849 (vm_object_assert_wait((object),(event),(interruptible)), \
850 vm_object_unlock(object), \
851 thread_block(THREAD_CONTINUE_NULL)) \
852
853 #define thread_sleep_vm_object(object, event, interruptible) \
854 lck_rw_sleep(&(object)->Lock, LCK_SLEEP_PROMOTED_PRI, (event_t)(event), (interruptible))
855
856 #define vm_object_sleep(object, event, interruptible) \
857 (((object)->all_wanted |= 1 << (event)), \
858 thread_sleep_vm_object((object), \
859 ((vm_offset_t)(object)+(event)), (interruptible)))
860
861 #define vm_object_wakeup(object, event) \
862 MACRO_BEGIN \
863 if ((object)->all_wanted & (1 << (event))) \
864 thread_wakeup((event_t)((vm_offset_t)(object) + (event))); \
865 (object)->all_wanted &= ~(1 << (event)); \
866 MACRO_END
867
868 #define vm_object_set_wanted(object, event) \
869 MACRO_BEGIN \
870 ((object)->all_wanted |= (1 << (event))); \
871 MACRO_END
872
873 #define vm_object_wanted(object, event) \
874 ((object)->all_wanted & (1 << (event)))
875
876 /*
877 * Routines implemented as macros
878 */
879 #ifdef VM_PIP_DEBUG
880 #include <libkern/OSDebug.h>
881 #define VM_PIP_DEBUG_BEGIN(object) \
882 MACRO_BEGIN \
883 int pip = ((object)->paging_in_progress + \
884 (object)->activity_in_progress); \
885 if (pip < VM_PIP_DEBUG_MAX_REFS) { \
886 (void) OSBacktrace(&(object)->pip_holders[pip].pip_retaddr[0], \
887 VM_PIP_DEBUG_STACK_FRAMES); \
888 } \
889 MACRO_END
890 #else /* VM_PIP_DEBUG */
891 #define VM_PIP_DEBUG_BEGIN(object)
892 #endif /* VM_PIP_DEBUG */
893
894 #define vm_object_activity_begin(object) \
895 MACRO_BEGIN \
896 vm_object_lock_assert_exclusive((object)); \
897 VM_PIP_DEBUG_BEGIN((object)); \
898 (object)->activity_in_progress++; \
899 if ((object)->activity_in_progress == 0) { \
900 panic("vm_object_activity_begin(%p): overflow\n", (object));\
901 } \
902 MACRO_END
903
904 #define vm_object_activity_end(object) \
905 MACRO_BEGIN \
906 vm_object_lock_assert_exclusive((object)); \
907 if ((object)->activity_in_progress == 0) { \
908 panic("vm_object_activity_end(%p): underflow\n", (object));\
909 } \
910 (object)->activity_in_progress--; \
911 if ((object)->paging_in_progress == 0 && \
912 (object)->activity_in_progress == 0) \
913 vm_object_wakeup((object), \
914 VM_OBJECT_EVENT_PAGING_IN_PROGRESS); \
915 MACRO_END
916
917 #define vm_object_paging_begin(object) \
918 MACRO_BEGIN \
919 vm_object_lock_assert_exclusive((object)); \
920 VM_PIP_DEBUG_BEGIN((object)); \
921 (object)->paging_in_progress++; \
922 if ((object)->paging_in_progress == 0) { \
923 panic("vm_object_paging_begin(%p): overflow\n", (object));\
924 } \
925 MACRO_END
926
927 #define vm_object_paging_end(object) \
928 MACRO_BEGIN \
929 vm_object_lock_assert_exclusive((object)); \
930 if ((object)->paging_in_progress == 0) { \
931 panic("vm_object_paging_end(%p): underflow\n", (object));\
932 } \
933 (object)->paging_in_progress--; \
934 if ((object)->paging_in_progress == 0) { \
935 vm_object_wakeup((object), \
936 VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS); \
937 if ((object)->activity_in_progress == 0) \
938 vm_object_wakeup((object), \
939 VM_OBJECT_EVENT_PAGING_IN_PROGRESS); \
940 } \
941 MACRO_END
942
943 #define vm_object_paging_wait(object, interruptible) \
944 MACRO_BEGIN \
945 vm_object_lock_assert_exclusive((object)); \
946 while ((object)->paging_in_progress != 0 || \
947 (object)->activity_in_progress != 0) { \
948 wait_result_t _wr; \
949 \
950 _wr = vm_object_sleep((object), \
951 VM_OBJECT_EVENT_PAGING_IN_PROGRESS, \
952 (interruptible)); \
953 \
954 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/\
955 /*XXX break; */ \
956 } \
957 MACRO_END
958
959 #define vm_object_paging_only_wait(object, interruptible) \
960 MACRO_BEGIN \
961 vm_object_lock_assert_exclusive((object)); \
962 while ((object)->paging_in_progress != 0) { \
963 wait_result_t _wr; \
964 \
965 _wr = vm_object_sleep((object), \
966 VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS,\
967 (interruptible)); \
968 \
969 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/\
970 /*XXX break; */ \
971 } \
972 MACRO_END
973
974
975 #define vm_object_mapping_begin(object) \
976 MACRO_BEGIN \
977 vm_object_lock_assert_exclusive((object)); \
978 assert(! (object)->mapping_in_progress); \
979 (object)->mapping_in_progress = TRUE; \
980 MACRO_END
981
982 #define vm_object_mapping_end(object) \
983 MACRO_BEGIN \
984 vm_object_lock_assert_exclusive((object)); \
985 assert((object)->mapping_in_progress); \
986 (object)->mapping_in_progress = FALSE; \
987 vm_object_wakeup((object), \
988 VM_OBJECT_EVENT_MAPPING_IN_PROGRESS); \
989 MACRO_END
990
991 #define vm_object_mapping_wait(object, interruptible) \
992 MACRO_BEGIN \
993 vm_object_lock_assert_exclusive((object)); \
994 while ((object)->mapping_in_progress) { \
995 wait_result_t _wr; \
996 \
997 _wr = vm_object_sleep((object), \
998 VM_OBJECT_EVENT_MAPPING_IN_PROGRESS, \
999 (interruptible)); \
1000 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/\
1001 /*XXX break; */ \
1002 } \
1003 assert(!(object)->mapping_in_progress); \
1004 MACRO_END
1005
1006
1007
1008 #define OBJECT_LOCK_SHARED 0
1009 #define OBJECT_LOCK_EXCLUSIVE 1
1010
1011 extern lck_grp_t vm_object_lck_grp;
1012 extern lck_grp_attr_t vm_object_lck_grp_attr;
1013 extern lck_attr_t vm_object_lck_attr;
1014 extern lck_attr_t kernel_object_lck_attr;
1015 extern lck_attr_t compressor_object_lck_attr;
1016
1017 extern vm_object_t vm_pageout_scan_wants_object;
1018
1019 extern void vm_object_lock(vm_object_t);
1020 extern boolean_t vm_object_lock_try(vm_object_t);
1021 extern boolean_t _vm_object_lock_try(vm_object_t);
1022 extern boolean_t vm_object_lock_avoid(vm_object_t);
1023 extern void vm_object_lock_shared(vm_object_t);
1024 extern boolean_t vm_object_lock_try_shared(vm_object_t);
1025
1026 /*
1027 * Object locking macros
1028 */
1029
1030 #define vm_object_lock_init(object) \
1031 lck_rw_init(&(object)->Lock, &vm_object_lck_grp, \
1032 (((object) == kernel_object || \
1033 (object) == vm_submap_object) ? \
1034 &kernel_object_lck_attr : \
1035 (((object) == compressor_object) ? \
1036 &compressor_object_lck_attr : \
1037 &vm_object_lck_attr)))
1038 #define vm_object_lock_destroy(object) lck_rw_destroy(&(object)->Lock, &vm_object_lck_grp)
1039
1040 #define vm_object_unlock(object) lck_rw_done(&(object)->Lock)
1041 #define vm_object_lock_upgrade(object) lck_rw_lock_shared_to_exclusive(&(object)->Lock)
1042 #define vm_object_lock_try_scan(object) _vm_object_lock_try(object)
1043
1044 /*
1045 * CAUTION: the following vm_object_lock_assert_held*() macros merely
1046 * check if anyone is holding the lock, but the holder may not necessarily
1047 * be the caller...
1048 */
1049 #if MACH_ASSERT || DEBUG
1050 #define vm_object_lock_assert_held(object) \
1051 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_HELD)
1052 #define vm_object_lock_assert_shared(object) \
1053 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_SHARED)
1054 #define vm_object_lock_assert_exclusive(object) \
1055 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_EXCLUSIVE)
1056 #define vm_object_lock_assert_notheld(object) \
1057 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_NOTHELD)
1058 #else /* MACH_ASSERT || DEBUG */
1059 #define vm_object_lock_assert_held(object)
1060 #define vm_object_lock_assert_shared(object)
1061 #define vm_object_lock_assert_exclusive(object)
1062 #define vm_object_lock_assert_notheld(object)
1063 #endif /* MACH_ASSERT || DEBUG */
1064
1065 #define vm_object_round_page(x) (((vm_object_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
1066 #define vm_object_trunc_page(x) ((vm_object_offset_t)(x) & ~((signed)PAGE_MASK))
1067
1068 extern void vm_object_cache_add(vm_object_t);
1069 extern void vm_object_cache_remove(vm_object_t);
1070 extern int vm_object_cache_evict(int, int);
1071
1072 #endif /* _VM_VM_OBJECT_H_ */