]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_object.h
xnu-1504.9.26.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 <mach_pagemap.h>
70 #include <task_swapper.h>
71
72 #include <mach/kern_return.h>
73 #include <mach/boolean.h>
74 #include <mach/memory_object_types.h>
75 #include <mach/port.h>
76 #include <mach/vm_prot.h>
77 #include <mach/vm_param.h>
78 #include <mach/machine/vm_types.h>
79 #include <kern/queue.h>
80 #include <kern/lock.h>
81 #include <kern/locks.h>
82 #include <kern/assert.h>
83 #include <kern/misc_protos.h>
84 #include <kern/macro_help.h>
85 #include <ipc/ipc_types.h>
86 #include <vm/pmap.h>
87
88 #if MACH_PAGEMAP
89 #include <vm/vm_external.h>
90 #endif /* MACH_PAGEMAP */
91
92 #include <vm/vm_options.h>
93
94 struct vm_page;
95
96 /*
97 * Types defined:
98 *
99 * vm_object_t Virtual memory object.
100 * vm_object_fault_info_t Used to determine cluster size.
101 */
102
103 struct vm_object_fault_info {
104 int interruptible;
105 uint32_t user_tag;
106 vm_size_t cluster_size;
107 vm_behavior_t behavior;
108 vm_map_offset_t lo_offset;
109 vm_map_offset_t hi_offset;
110 boolean_t no_cache;
111 boolean_t stealth;
112 boolean_t mark_zf_absent;
113 };
114
115
116
117 struct vm_object {
118 queue_head_t memq; /* Resident memory */
119 lck_rw_t Lock; /* Synchronization */
120
121 vm_object_size_t size; /* Object size (only valid
122 * if internal)
123 */
124 struct vm_page *memq_hint;
125 int ref_count; /* Number of references */
126 #if TASK_SWAPPER
127 int res_count; /* Residency references (swap)*/
128 #endif /* TASK_SWAPPER */
129 unsigned int resident_page_count;
130 /* number of resident pages */
131 unsigned int wired_page_count; /* number of wired pages */
132 unsigned int reusable_page_count;
133
134 struct vm_object *copy; /* Object that should receive
135 * a copy of my changed pages,
136 * for copy_delay, or just the
137 * temporary object that
138 * shadows this object, for
139 * copy_call.
140 */
141 struct vm_object *shadow; /* My shadow */
142 vm_object_offset_t shadow_offset; /* Offset into shadow */
143
144 memory_object_t pager; /* Where to get data */
145 vm_object_offset_t paging_offset; /* Offset into memory object */
146 memory_object_control_t pager_control; /* Where data comes back */
147
148 memory_object_copy_strategy_t
149 copy_strategy; /* How to handle data copy */
150
151 short paging_in_progress;
152 /* The memory object ports are
153 * being used (e.g., for pagein
154 * or pageout) -- don't change
155 * any of these fields (i.e.,
156 * don't collapse, destroy or
157 * terminate)
158 */
159 short activity_in_progress;
160
161 unsigned int
162 /* boolean_t array */ all_wanted:11, /* Bit array of "want to be
163 * awakened" notations. See
164 * VM_OBJECT_EVENT_* items
165 * below */
166 /* boolean_t */ pager_created:1, /* Has pager been created? */
167 /* boolean_t */ pager_initialized:1, /* Are fields ready to use? */
168 /* boolean_t */ pager_ready:1, /* Will pager take requests? */
169
170 /* boolean_t */ pager_trusted:1,/* The pager for this object
171 * is trusted. This is true for
172 * all internal objects (backed
173 * by the default pager)
174 */
175 /* boolean_t */ can_persist:1, /* The kernel may keep the data
176 * for this object (and rights
177 * to the memory object) after
178 * all address map references
179 * are deallocated?
180 */
181 /* boolean_t */ internal:1, /* Created by the kernel (and
182 * therefore, managed by the
183 * default memory manger)
184 */
185 /* boolean_t */ temporary:1, /* Permanent objects may be
186 * changed externally by the
187 * memory manager, and changes
188 * made in memory must be
189 * reflected back to the memory
190 * manager. Temporary objects
191 * lack both of these
192 * characteristics.
193 */
194 /* boolean_t */ private:1, /* magic device_pager object,
195 * holds private pages only */
196 /* boolean_t */ pageout:1, /* pageout object. contains
197 * private pages that refer to
198 * a real memory object. */
199 /* boolean_t */ alive:1, /* Not yet terminated */
200
201 /* boolean_t */ purgable:2, /* Purgable state. See
202 * VM_PURGABLE_*
203 */
204 /* boolean_t */ shadowed:1, /* Shadow may exist */
205 /* boolean_t */ silent_overwrite:1,
206 /* Allow full page overwrite
207 * without data_request if
208 * page is absent */
209 /* boolean_t */ advisory_pageout:1,
210 /* Instead of sending page
211 * via OOL, just notify
212 * pager that the kernel
213 * wants to discard it, page
214 * remains in object */
215 /* boolean_t */ true_share:1,
216 /* This object is mapped
217 * in more than one place
218 * and hence cannot be
219 * coalesced */
220 /* boolean_t */ terminating:1,
221 /* Allows vm_object_lookup
222 * and vm_object_deallocate
223 * to special case their
224 * behavior when they are
225 * called as a result of
226 * page cleaning during
227 * object termination
228 */
229 /* boolean_t */ named:1, /* An enforces an internal
230 * naming convention, by
231 * calling the right routines
232 * for allocation and
233 * destruction, UBC references
234 * against the vm_object are
235 * checked.
236 */
237 /* boolean_t */ shadow_severed:1,
238 /* When a permanent object
239 * backing a COW goes away
240 * unexpectedly. This bit
241 * allows vm_fault to return
242 * an error rather than a
243 * zero filled page.
244 */
245 /* boolean_t */ phys_contiguous:1,
246 /* Memory is wired and
247 * guaranteed physically
248 * contiguous. However
249 * it is not device memory
250 * and obeys normal virtual
251 * memory rules w.r.t pmap
252 * access bits.
253 */
254 /* boolean_t */ nophyscache:1;
255 /* When mapped at the
256 * pmap level, don't allow
257 * primary caching. (for
258 * I/O)
259 */
260
261
262
263 queue_chain_t cached_list; /* Attachment point for the
264 * list of objects cached as a
265 * result of their can_persist
266 * value
267 */
268
269 queue_head_t msr_q; /* memory object synchronise
270 request queue */
271
272 /*
273 * the following fields are not protected by any locks
274 * they are updated via atomic compare and swap
275 */
276 vm_object_offset_t last_alloc; /* last allocation offset */
277 int sequential; /* sequential access size */
278
279 uint32_t pages_created;
280 uint32_t pages_used;
281 #if MACH_PAGEMAP
282 vm_external_map_t existence_map; /* bitmap of pages written to
283 * backing storage */
284 #endif /* MACH_PAGEMAP */
285 vm_offset_t cow_hint; /* last page present in */
286 /* shadow but not in object */
287 #if MACH_ASSERT
288 struct vm_object *paging_object; /* object which pages to be
289 * swapped out are temporary
290 * put in current object
291 */
292 #endif
293 /* hold object lock when altering */
294 unsigned int
295 wimg_bits:8, /* cache WIMG bits */
296 code_signed:1, /* pages are signed and should be
297 validated; the signatures are stored
298 with the pager */
299 hashed:1, /* object/pager entered in hash */
300 transposed:1, /* object was transposed with another */
301 mapping_in_progress:1, /* pager being mapped/unmapped */
302 volatile_empty:1,
303 volatile_fault:1,
304 all_reusable:1,
305 blocked_access:1,
306 __object2_unused_bits:16; /* for expansion */
307
308 #if UPL_DEBUG
309 queue_head_t uplq; /* List of outstanding upls */
310 #endif /* UPL_DEBUG */
311
312 #ifdef VM_PIP_DEBUG
313 /*
314 * Keep track of the stack traces for the first holders
315 * of a "paging_in_progress" reference for this VM object.
316 */
317 #define VM_PIP_DEBUG_STACK_FRAMES 25 /* depth of each stack trace */
318 #define VM_PIP_DEBUG_MAX_REFS 10 /* track that many references */
319 struct __pip_backtrace {
320 void *pip_retaddr[VM_PIP_DEBUG_STACK_FRAMES];
321 } pip_holders[VM_PIP_DEBUG_MAX_REFS];
322 #endif /* VM_PIP_DEBUG */
323
324 queue_chain_t objq; /* object queue - currently used for purgable queues */
325 };
326
327 #define VM_OBJECT_PURGEABLE_FAULT_ERROR(object) \
328 ((object)->volatile_fault && \
329 ((object)->purgable == VM_PURGABLE_VOLATILE || \
330 (object)->purgable == VM_PURGABLE_EMPTY))
331
332 #define VM_PAGE_REMOVE(page) \
333 MACRO_BEGIN \
334 vm_page_t __page = (page); \
335 vm_object_t __object = __page->object; \
336 if (__page == __object->memq_hint) { \
337 vm_page_t __new_hint; \
338 queue_entry_t __qe; \
339 __qe = queue_next(&__page->listq); \
340 if (queue_end(&__object->memq, __qe)) { \
341 __qe = queue_prev(&__page->listq); \
342 if (queue_end(&__object->memq, __qe)) { \
343 __qe = NULL; \
344 } \
345 } \
346 __new_hint = (vm_page_t) __qe; \
347 __object->memq_hint = __new_hint; \
348 } \
349 queue_remove(&__object->memq, __page, vm_page_t, listq); \
350 MACRO_END
351
352 #define VM_PAGE_INSERT(page, object) \
353 MACRO_BEGIN \
354 vm_page_t __page = (page); \
355 vm_object_t __object = (object); \
356 queue_enter(&__object->memq, __page, vm_page_t, listq); \
357 __object->memq_hint = __page; \
358 MACRO_END
359
360 __private_extern__
361 vm_object_t kernel_object; /* the single kernel object */
362
363 __private_extern__
364 unsigned int vm_object_absent_max; /* maximum number of absent pages
365 at a time for each object */
366
367 # define VM_MSYNC_INITIALIZED 0
368 # define VM_MSYNC_SYNCHRONIZING 1
369 # define VM_MSYNC_DONE 2
370
371 struct msync_req {
372 queue_chain_t msr_q; /* object request queue */
373 queue_chain_t req_q; /* vm_msync request queue */
374 unsigned int flag;
375 vm_object_offset_t offset;
376 vm_object_size_t length;
377 vm_object_t object; /* back pointer */
378 decl_lck_mtx_data(, msync_req_lock) /* Lock for this structure */
379 };
380
381 typedef struct msync_req *msync_req_t;
382 #define MSYNC_REQ_NULL ((msync_req_t) 0)
383
384
385 extern lck_grp_t vm_map_lck_grp;
386 extern lck_attr_t vm_map_lck_attr;
387
388 /*
389 * Macros to allocate and free msync_reqs
390 */
391 #define msync_req_alloc(msr) \
392 MACRO_BEGIN \
393 (msr) = (msync_req_t)kalloc(sizeof(struct msync_req)); \
394 lck_mtx_init(&(msr)->msync_req_lock, &vm_map_lck_grp, &vm_map_lck_attr); \
395 msr->flag = VM_MSYNC_INITIALIZED; \
396 MACRO_END
397
398 #define msync_req_free(msr) \
399 (kfree((msr), sizeof(struct msync_req)))
400
401 #define msr_lock(msr) lck_mtx_lock(&(msr)->msync_req_lock)
402 #define msr_unlock(msr) lck_mtx_unlock(&(msr)->msync_req_lock)
403
404 /*
405 * Declare procedures that operate on VM objects.
406 */
407
408 __private_extern__ void vm_object_bootstrap(void) __attribute__((section("__TEXT, initcode")));
409
410 __private_extern__ void vm_object_init(void);
411
412 __private_extern__ void vm_object_init_lck_grp(void);
413
414 __private_extern__ void vm_object_reaper_init(void);
415
416 __private_extern__ vm_object_t vm_object_allocate(
417 vm_object_size_t size);
418
419 __private_extern__ void _vm_object_allocate(vm_object_size_t size,
420 vm_object_t object);
421
422 #if TASK_SWAPPER
423
424 __private_extern__ void vm_object_res_reference(
425 vm_object_t object);
426 __private_extern__ void vm_object_res_deallocate(
427 vm_object_t object);
428 #define VM_OBJ_RES_INCR(object) (object)->res_count++
429 #define VM_OBJ_RES_DECR(object) (object)->res_count--
430
431 #else /* TASK_SWAPPER */
432
433 #define VM_OBJ_RES_INCR(object)
434 #define VM_OBJ_RES_DECR(object)
435 #define vm_object_res_reference(object)
436 #define vm_object_res_deallocate(object)
437
438 #endif /* TASK_SWAPPER */
439
440 #define vm_object_reference_locked(object) \
441 MACRO_BEGIN \
442 vm_object_t RLObject = (object); \
443 vm_object_lock_assert_exclusive(object); \
444 assert((RLObject)->ref_count > 0); \
445 (RLObject)->ref_count++; \
446 assert((RLObject)->ref_count > 1); \
447 vm_object_res_reference(RLObject); \
448 MACRO_END
449
450
451 #define vm_object_reference_shared(object) \
452 MACRO_BEGIN \
453 vm_object_t RLObject = (object); \
454 vm_object_lock_assert_shared(object); \
455 assert((RLObject)->ref_count > 0); \
456 OSAddAtomic(1, &(RLObject)->ref_count); \
457 assert((RLObject)->ref_count > 1); \
458 /* XXX we would need an atomic version of the following ... */ \
459 vm_object_res_reference(RLObject); \
460 MACRO_END
461
462
463 __private_extern__ void vm_object_reference(
464 vm_object_t object);
465
466 #if !MACH_ASSERT
467
468 #define vm_object_reference(object) \
469 MACRO_BEGIN \
470 vm_object_t RObject = (object); \
471 if (RObject) { \
472 vm_object_lock_shared(RObject); \
473 vm_object_reference_shared(RObject); \
474 vm_object_unlock(RObject); \
475 } \
476 MACRO_END
477
478 #endif /* MACH_ASSERT */
479
480 __private_extern__ void vm_object_deallocate(
481 vm_object_t object);
482
483 __private_extern__ kern_return_t vm_object_release_name(
484 vm_object_t object,
485 int flags);
486
487 __private_extern__ void vm_object_pmap_protect(
488 vm_object_t object,
489 vm_object_offset_t offset,
490 vm_object_size_t size,
491 pmap_t pmap,
492 vm_map_offset_t pmap_start,
493 vm_prot_t prot);
494
495 __private_extern__ void vm_object_page_remove(
496 vm_object_t object,
497 vm_object_offset_t start,
498 vm_object_offset_t end);
499
500 __private_extern__ void vm_object_deactivate_pages(
501 vm_object_t object,
502 vm_object_offset_t offset,
503 vm_object_size_t size,
504 boolean_t kill_page,
505 boolean_t reusable_page);
506
507 __private_extern__ void vm_object_reuse_pages(
508 vm_object_t object,
509 vm_object_offset_t start_offset,
510 vm_object_offset_t end_offset,
511 boolean_t allow_partial_reuse);
512
513 __private_extern__ void vm_object_purge(
514 vm_object_t object);
515
516 __private_extern__ kern_return_t vm_object_purgable_control(
517 vm_object_t object,
518 vm_purgable_t control,
519 int *state);
520
521 __private_extern__ boolean_t vm_object_coalesce(
522 vm_object_t prev_object,
523 vm_object_t next_object,
524 vm_object_offset_t prev_offset,
525 vm_object_offset_t next_offset,
526 vm_object_size_t prev_size,
527 vm_object_size_t next_size);
528
529 __private_extern__ boolean_t vm_object_shadow(
530 vm_object_t *object,
531 vm_object_offset_t *offset,
532 vm_object_size_t length);
533
534 __private_extern__ void vm_object_collapse(
535 vm_object_t object,
536 vm_object_offset_t offset,
537 boolean_t can_bypass);
538
539 __private_extern__ boolean_t vm_object_copy_quickly(
540 vm_object_t *_object,
541 vm_object_offset_t src_offset,
542 vm_object_size_t size,
543 boolean_t *_src_needs_copy,
544 boolean_t *_dst_needs_copy);
545
546 __private_extern__ kern_return_t vm_object_copy_strategically(
547 vm_object_t src_object,
548 vm_object_offset_t src_offset,
549 vm_object_size_t size,
550 vm_object_t *dst_object,
551 vm_object_offset_t *dst_offset,
552 boolean_t *dst_needs_copy);
553
554 __private_extern__ kern_return_t vm_object_copy_slowly(
555 vm_object_t src_object,
556 vm_object_offset_t src_offset,
557 vm_object_size_t size,
558 boolean_t interruptible,
559 vm_object_t *_result_object);
560
561 __private_extern__ vm_object_t vm_object_copy_delayed(
562 vm_object_t src_object,
563 vm_object_offset_t src_offset,
564 vm_object_size_t size,
565 boolean_t src_object_shared);
566
567
568
569 __private_extern__ kern_return_t vm_object_destroy(
570 vm_object_t object,
571 kern_return_t reason);
572
573 __private_extern__ void vm_object_pager_create(
574 vm_object_t object);
575
576 __private_extern__ void vm_object_page_map(
577 vm_object_t object,
578 vm_object_offset_t offset,
579 vm_object_size_t size,
580 vm_object_offset_t (*map_fn)
581 (void *, vm_object_offset_t),
582 void *map_fn_data);
583
584 __private_extern__ kern_return_t vm_object_upl_request(
585 vm_object_t object,
586 vm_object_offset_t offset,
587 upl_size_t size,
588 upl_t *upl,
589 upl_page_info_t *page_info,
590 unsigned int *count,
591 int flags);
592
593 __private_extern__ kern_return_t vm_object_transpose(
594 vm_object_t object1,
595 vm_object_t object2,
596 vm_object_size_t transpose_size);
597
598 __private_extern__ boolean_t vm_object_sync(
599 vm_object_t object,
600 vm_object_offset_t offset,
601 vm_object_size_t size,
602 boolean_t should_flush,
603 boolean_t should_return,
604 boolean_t should_iosync);
605
606 __private_extern__ kern_return_t vm_object_update(
607 vm_object_t object,
608 vm_object_offset_t offset,
609 vm_object_size_t size,
610 vm_object_offset_t *error_offset,
611 int *io_errno,
612 memory_object_return_t should_return,
613 int flags,
614 vm_prot_t prot);
615
616 __private_extern__ kern_return_t vm_object_lock_request(
617 vm_object_t object,
618 vm_object_offset_t offset,
619 vm_object_size_t size,
620 memory_object_return_t should_return,
621 int flags,
622 vm_prot_t prot);
623
624
625
626 __private_extern__ vm_object_t vm_object_enter(
627 memory_object_t pager,
628 vm_object_size_t size,
629 boolean_t internal,
630 boolean_t init,
631 boolean_t check_named);
632
633
634 __private_extern__ void vm_object_cluster_size(
635 vm_object_t object,
636 vm_object_offset_t *start,
637 vm_size_t *length,
638 vm_object_fault_info_t fault_info,
639 uint32_t *io_streaming);
640
641 __private_extern__ kern_return_t vm_object_populate_with_private(
642 vm_object_t object,
643 vm_object_offset_t offset,
644 ppnum_t phys_page,
645 vm_size_t size);
646
647 extern kern_return_t adjust_vm_object_cache(
648 vm_size_t oval,
649 vm_size_t nval);
650
651 extern kern_return_t vm_object_page_op(
652 vm_object_t object,
653 vm_object_offset_t offset,
654 int ops,
655 ppnum_t *phys_entry,
656 int *flags);
657
658 extern kern_return_t vm_object_range_op(
659 vm_object_t object,
660 vm_object_offset_t offset_beg,
661 vm_object_offset_t offset_end,
662 int ops,
663 uint32_t *range);
664
665
666 __private_extern__ void vm_object_reap_pages(
667 vm_object_t object,
668 int reap_type);
669 #define REAP_REAP 0
670 #define REAP_TERMINATE 1
671 #define REAP_PURGEABLE 2
672 #define REAP_DATA_FLUSH 3
673
674
675 /*
676 * Event waiting handling
677 */
678
679 #define VM_OBJECT_EVENT_INITIALIZED 0
680 #define VM_OBJECT_EVENT_PAGER_READY 1
681 #define VM_OBJECT_EVENT_PAGING_IN_PROGRESS 2
682 #define VM_OBJECT_EVENT_MAPPING_IN_PROGRESS 3
683 #define VM_OBJECT_EVENT_LOCK_IN_PROGRESS 4
684 #define VM_OBJECT_EVENT_UNCACHING 5
685 #define VM_OBJECT_EVENT_COPY_CALL 6
686 #define VM_OBJECT_EVENT_CACHING 7
687 #define VM_OBJECT_EVENT_UNBLOCKED 8
688 #define VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS 9
689
690 #define vm_object_assert_wait(object, event, interruptible) \
691 (((object)->all_wanted |= 1 << (event)), \
692 assert_wait((event_t)((vm_offset_t)(object)+(event)),(interruptible)))
693
694 #define vm_object_wait(object, event, interruptible) \
695 (vm_object_assert_wait((object),(event),(interruptible)), \
696 vm_object_unlock(object), \
697 thread_block(THREAD_CONTINUE_NULL)) \
698
699 #define thread_sleep_vm_object(object, event, interruptible) \
700 lck_rw_sleep(&(object)->Lock, LCK_SLEEP_DEFAULT, (event_t)(event), (interruptible))
701
702 #define vm_object_sleep(object, event, interruptible) \
703 (((object)->all_wanted |= 1 << (event)), \
704 thread_sleep_vm_object((object), \
705 ((vm_offset_t)(object)+(event)), (interruptible)))
706
707 #define vm_object_wakeup(object, event) \
708 MACRO_BEGIN \
709 if ((object)->all_wanted & (1 << (event))) \
710 thread_wakeup((event_t)((vm_offset_t)(object) + (event))); \
711 (object)->all_wanted &= ~(1 << (event)); \
712 MACRO_END
713
714 #define vm_object_set_wanted(object, event) \
715 MACRO_BEGIN \
716 ((object)->all_wanted |= (1 << (event))); \
717 MACRO_END
718
719 #define vm_object_wanted(object, event) \
720 ((object)->all_wanted & (1 << (event)))
721
722 /*
723 * Routines implemented as macros
724 */
725 #ifdef VM_PIP_DEBUG
726 #include <libkern/OSDebug.h>
727 #define VM_PIP_DEBUG_BEGIN(object) \
728 MACRO_BEGIN \
729 int pip = ((object)->paging_in_progress + \
730 (object)->activity_in_progress); \
731 if (pip < VM_PIP_DEBUG_MAX_REFS) { \
732 (void) OSBacktrace(&(object)->pip_holders[pip].pip_retaddr[0], \
733 VM_PIP_DEBUG_STACK_FRAMES); \
734 } \
735 MACRO_END
736 #else /* VM_PIP_DEBUG */
737 #define VM_PIP_DEBUG_BEGIN(object)
738 #endif /* VM_PIP_DEBUG */
739
740 #define vm_object_activity_begin(object) \
741 MACRO_BEGIN \
742 vm_object_lock_assert_exclusive((object)); \
743 assert((object)->paging_in_progress >= 0); \
744 VM_PIP_DEBUG_BEGIN((object)); \
745 (object)->activity_in_progress++; \
746 MACRO_END
747
748 #define vm_object_activity_end(object) \
749 MACRO_BEGIN \
750 vm_object_lock_assert_exclusive((object)); \
751 assert((object)->activity_in_progress > 0); \
752 (object)->activity_in_progress--; \
753 if ((object)->paging_in_progress == 0 && \
754 (object)->activity_in_progress == 0) \
755 vm_object_wakeup((object), \
756 VM_OBJECT_EVENT_PAGING_IN_PROGRESS); \
757 MACRO_END
758
759 #define vm_object_paging_begin(object) \
760 MACRO_BEGIN \
761 vm_object_lock_assert_exclusive((object)); \
762 assert((object)->paging_in_progress >= 0); \
763 VM_PIP_DEBUG_BEGIN((object)); \
764 (object)->paging_in_progress++; \
765 MACRO_END
766
767 #define vm_object_paging_end(object) \
768 MACRO_BEGIN \
769 vm_object_lock_assert_exclusive((object)); \
770 assert((object)->paging_in_progress > 0); \
771 (object)->paging_in_progress--; \
772 if ((object)->paging_in_progress == 0) { \
773 vm_object_wakeup((object), \
774 VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS); \
775 if ((object)->activity_in_progress == 0) \
776 vm_object_wakeup((object), \
777 VM_OBJECT_EVENT_PAGING_IN_PROGRESS); \
778 } \
779 MACRO_END
780
781 #define vm_object_paging_wait(object, interruptible) \
782 MACRO_BEGIN \
783 vm_object_lock_assert_exclusive((object)); \
784 while ((object)->paging_in_progress != 0 || \
785 (object)->activity_in_progress != 0) { \
786 wait_result_t _wr; \
787 \
788 _wr = vm_object_sleep((object), \
789 VM_OBJECT_EVENT_PAGING_IN_PROGRESS, \
790 (interruptible)); \
791 \
792 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/\
793 /*XXX break; */ \
794 } \
795 MACRO_END
796
797 #define vm_object_paging_only_wait(object, interruptible) \
798 MACRO_BEGIN \
799 vm_object_lock_assert_exclusive((object)); \
800 while ((object)->paging_in_progress != 0) { \
801 wait_result_t _wr; \
802 \
803 _wr = vm_object_sleep((object), \
804 VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS,\
805 (interruptible)); \
806 \
807 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/\
808 /*XXX break; */ \
809 } \
810 MACRO_END
811
812
813 #define vm_object_mapping_begin(object) \
814 MACRO_BEGIN \
815 vm_object_lock_assert_exclusive((object)); \
816 assert(! (object)->mapping_in_progress); \
817 (object)->mapping_in_progress = TRUE; \
818 MACRO_END
819
820 #define vm_object_mapping_end(object) \
821 MACRO_BEGIN \
822 vm_object_lock_assert_exclusive((object)); \
823 assert((object)->mapping_in_progress); \
824 (object)->mapping_in_progress = FALSE; \
825 vm_object_wakeup((object), \
826 VM_OBJECT_EVENT_MAPPING_IN_PROGRESS); \
827 MACRO_END
828
829 #define vm_object_mapping_wait(object, interruptible) \
830 MACRO_BEGIN \
831 vm_object_lock_assert_exclusive((object)); \
832 while ((object)->mapping_in_progress) { \
833 wait_result_t _wr; \
834 \
835 _wr = vm_object_sleep((object), \
836 VM_OBJECT_EVENT_MAPPING_IN_PROGRESS, \
837 (interruptible)); \
838 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/\
839 /*XXX break; */ \
840 } \
841 assert(!(object)->mapping_in_progress); \
842 MACRO_END
843
844
845
846 #define OBJECT_LOCK_SHARED 0
847 #define OBJECT_LOCK_EXCLUSIVE 1
848
849 extern lck_grp_t vm_object_lck_grp;
850 extern lck_grp_attr_t vm_object_lck_grp_attr;
851 extern lck_attr_t vm_object_lck_attr;
852 extern lck_attr_t kernel_object_lck_attr;
853
854 extern vm_object_t vm_pageout_scan_wants_object;
855
856 extern void vm_object_lock(vm_object_t);
857 extern boolean_t vm_object_lock_try(vm_object_t);
858 extern boolean_t _vm_object_lock_try(vm_object_t);
859 extern boolean_t vm_object_lock_avoid(vm_object_t);
860 extern void vm_object_lock_shared(vm_object_t);
861 extern boolean_t vm_object_lock_try_shared(vm_object_t);
862
863 /*
864 * Object locking macros
865 */
866
867 #define vm_object_lock_init(object) \
868 lck_rw_init(&(object)->Lock, &vm_object_lck_grp, \
869 (((object) == kernel_object || \
870 (object) == vm_submap_object) ? \
871 &kernel_object_lck_attr : \
872 &vm_object_lck_attr))
873 #define vm_object_lock_destroy(object) lck_rw_destroy(&(object)->Lock, &vm_object_lck_grp)
874
875 #define vm_object_unlock(object) lck_rw_done(&(object)->Lock)
876 #define vm_object_lock_upgrade(object) lck_rw_lock_shared_to_exclusive(&(object)->Lock)
877 #define vm_object_lock_try_scan(object) _vm_object_lock_try(object)
878
879 /*
880 * CAUTION: the following vm_object_lock_assert_held*() macros merely
881 * check if anyone is holding the lock, but the holder may not necessarily
882 * be the caller...
883 */
884 #if DEBUG
885 #define vm_object_lock_assert_held(object) \
886 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_HELD)
887 #define vm_object_lock_assert_shared(object) \
888 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_SHARED)
889 #define vm_object_lock_assert_exclusive(object) \
890 lck_rw_assert(&(object)->Lock, LCK_RW_ASSERT_EXCLUSIVE)
891 #else /* DEBUG */
892 #define vm_object_lock_assert_held(object)
893 #define vm_object_lock_assert_shared(object)
894 #define vm_object_lock_assert_exclusive(object)
895 #endif /* DEBUG */
896
897 #define vm_object_round_page(x) (((vm_object_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
898 #define vm_object_trunc_page(x) ((vm_object_offset_t)(x) & ~((signed)PAGE_MASK))
899
900 #endif /* _VM_VM_OBJECT_H_ */