]> git.saurik.com Git - apple/xnu.git/blame - osfmk/vm/vm_object.h
xnu-1504.15.3.tar.gz
[apple/xnu.git] / osfmk / vm / vm_object.h
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_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>
91447636 77#include <mach/vm_param.h>
1c79356b
A
78#include <mach/machine/vm_types.h>
79#include <kern/queue.h>
80#include <kern/lock.h>
2d21ac55 81#include <kern/locks.h>
1c79356b 82#include <kern/assert.h>
0b4e3aa0 83#include <kern/misc_protos.h>
1c79356b
A
84#include <kern/macro_help.h>
85#include <ipc/ipc_types.h>
86#include <vm/pmap.h>
1c79356b
A
87
88#if MACH_PAGEMAP
89#include <vm/vm_external.h>
90#endif /* MACH_PAGEMAP */
91
b0d623f7
A
92#include <vm/vm_options.h>
93
91447636 94struct vm_page;
1c79356b
A
95
96/*
97 * Types defined:
98 *
99 * vm_object_t Virtual memory object.
2d21ac55 100 * vm_object_fault_info_t Used to determine cluster size.
1c79356b
A
101 */
102
2d21ac55
A
103struct 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;
b0d623f7 111 boolean_t stealth;
0b4c1975 112 boolean_t mark_zf_absent;
2d21ac55
A
113};
114
115
116
1c79356b
A
117struct vm_object {
118 queue_head_t memq; /* Resident memory */
2d21ac55 119 lck_rw_t Lock; /* Synchronization */
1c79356b
A
120
121 vm_object_size_t size; /* Object size (only valid
122 * if internal)
123 */
91447636 124 struct vm_page *memq_hint;
1c79356b
A
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 */
b0d623f7
A
131 unsigned int wired_page_count; /* number of wired pages */
132 unsigned int reusable_page_count;
1c79356b
A
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
0b4e3aa0 144 memory_object_t pager; /* Where to get data */
1c79356b 145 vm_object_offset_t paging_offset; /* Offset into memory object */
91447636 146 memory_object_control_t pager_control; /* Where data comes back */
1c79356b
A
147
148 memory_object_copy_strategy_t
149 copy_strategy; /* How to handle data copy */
150
b0d623f7 151 short paging_in_progress;
1c79356b
A
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 */
b0d623f7
A
159 short activity_in_progress;
160
1c79356b
A
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
91447636 201 /* boolean_t */ purgable:2, /* Purgable state. See
2d21ac55 202 * VM_PURGABLE_*
1c79356b
A
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 */
0b4e3aa0 245 /* boolean_t */ phys_contiguous:1,
1c79356b
A
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 */
0b4e3aa0
A
254 /* boolean_t */ nophyscache:1;
255 /* When mapped at the
256 * pmap level, don't allow
257 * primary caching. (for
258 * I/O)
259 */
1c79356b
A
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
2d21ac55
A
272 /*
273 * the following fields are not protected by any locks
274 * they are updated via atomic compare and swap
275 */
1c79356b 276 vm_object_offset_t last_alloc; /* last allocation offset */
2d21ac55
A
277 int sequential; /* sequential access size */
278
279 uint32_t pages_created;
280 uint32_t pages_used;
1c79356b
A
281#if MACH_PAGEMAP
282 vm_external_map_t existence_map; /* bitmap of pages written to
283 * backing storage */
284#endif /* MACH_PAGEMAP */
55e303ae 285 vm_offset_t cow_hint; /* last page present in */
0b4e3aa0 286 /* shadow but not in object */
1c79356b
A
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
2d21ac55
A
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 */
b0d623f7
A
299 hashed:1, /* object/pager entered in hash */
300 transposed:1, /* object was transposed with another */
593a1d5f 301 mapping_in_progress:1, /* pager being mapped/unmapped */
b0d623f7
A
302 volatile_empty:1,
303 volatile_fault:1,
304 all_reusable:1,
305 blocked_access:1,
306 __object2_unused_bits:16; /* for expansion */
2d21ac55 307
b0d623f7 308#if UPL_DEBUG
1c79356b 309 queue_head_t uplq; /* List of outstanding upls */
2d21ac55
A
310#endif /* UPL_DEBUG */
311
0c530ab8
A
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 */
2d21ac55
A
323
324 queue_chain_t objq; /* object queue - currently used for purgable queues */
1c79356b
A
325};
326
b0d623f7
A
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
91447636
A
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
0b4e3aa0 360__private_extern__
1c79356b
A
361vm_object_t kernel_object; /* the single kernel object */
362
0b4e3aa0 363__private_extern__
91447636 364unsigned int vm_object_absent_max; /* maximum number of absent pages
1c79356b
A
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
371struct 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 */
b0d623f7 378 decl_lck_mtx_data(, msync_req_lock) /* Lock for this structure */
1c79356b
A
379};
380
381typedef struct msync_req *msync_req_t;
382#define MSYNC_REQ_NULL ((msync_req_t) 0)
383
b0d623f7
A
384
385extern lck_grp_t vm_map_lck_grp;
386extern lck_attr_t vm_map_lck_attr;
387
1c79356b
A
388/*
389 * Macros to allocate and free msync_reqs
390 */
391#define msync_req_alloc(msr) \
b0d623f7 392 MACRO_BEGIN \
1c79356b 393 (msr) = (msync_req_t)kalloc(sizeof(struct msync_req)); \
b0d623f7
A
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
1c79356b
A
397
398#define msync_req_free(msr) \
91447636 399 (kfree((msr), sizeof(struct msync_req)))
1c79356b 400
b0d623f7
A
401#define msr_lock(msr) lck_mtx_lock(&(msr)->msync_req_lock)
402#define msr_unlock(msr) lck_mtx_unlock(&(msr)->msync_req_lock)
1c79356b
A
403
404/*
405 * Declare procedures that operate on VM objects.
406 */
407
2d21ac55 408__private_extern__ void vm_object_bootstrap(void) __attribute__((section("__TEXT, initcode")));
1c79356b 409
0b4e3aa0 410__private_extern__ void vm_object_init(void);
1c79356b 411
2d21ac55
A
412__private_extern__ void vm_object_init_lck_grp(void);
413
8f6c56a5
A
414__private_extern__ void vm_object_reaper_init(void);
415
0b4e3aa0 416__private_extern__ vm_object_t vm_object_allocate(
1c79356b
A
417 vm_object_size_t size);
418
91447636
A
419__private_extern__ void _vm_object_allocate(vm_object_size_t size,
420 vm_object_t object);
421
0b4e3aa0
A
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) \
2d21ac55
A
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); \
b0d623f7 456 OSAddAtomic(1, &(RLObject)->ref_count); \
2d21ac55
A
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
0b4e3aa0
A
461
462
0b4e3aa0 463__private_extern__ void vm_object_reference(
1c79356b 464 vm_object_t object);
0b4e3aa0 465
91447636 466#if !MACH_ASSERT
0b4e3aa0 467
1c79356b
A
468#define vm_object_reference(object) \
469MACRO_BEGIN \
0b4e3aa0
A
470 vm_object_t RObject = (object); \
471 if (RObject) { \
b0d623f7
A
472 vm_object_lock_shared(RObject); \
473 vm_object_reference_shared(RObject); \
0b4e3aa0 474 vm_object_unlock(RObject); \
1c79356b
A
475 } \
476MACRO_END
0b4e3aa0 477
1c79356b
A
478#endif /* MACH_ASSERT */
479
0b4e3aa0 480__private_extern__ void vm_object_deallocate(
1c79356b
A
481 vm_object_t object);
482
0b4e3aa0
A
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(
1c79356b
A
488 vm_object_t object,
489 vm_object_offset_t offset,
91447636 490 vm_object_size_t size,
1c79356b 491 pmap_t pmap,
91447636 492 vm_map_offset_t pmap_start,
1c79356b
A
493 vm_prot_t prot);
494
0b4e3aa0 495__private_extern__ void vm_object_page_remove(
1c79356b
A
496 vm_object_t object,
497 vm_object_offset_t start,
498 vm_object_offset_t end);
499
0b4e3aa0
A
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,
b0d623f7
A
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);
0b4e3aa0 512
b0d623f7 513__private_extern__ void vm_object_purge(
91447636
A
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
0b4e3aa0 521__private_extern__ boolean_t vm_object_coalesce(
1c79356b
A
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
0b4e3aa0 529__private_extern__ boolean_t vm_object_shadow(
1c79356b
A
530 vm_object_t *object,
531 vm_object_offset_t *offset,
532 vm_object_size_t length);
533
0b4e3aa0 534__private_extern__ void vm_object_collapse(
55e303ae 535 vm_object_t object,
0c530ab8
A
536 vm_object_offset_t offset,
537 boolean_t can_bypass);
1c79356b 538
0b4e3aa0 539__private_extern__ boolean_t vm_object_copy_quickly(
1c79356b
A
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
0b4e3aa0 546__private_extern__ kern_return_t vm_object_copy_strategically(
1c79356b
A
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
0b4e3aa0 554__private_extern__ kern_return_t vm_object_copy_slowly(
1c79356b
A
555 vm_object_t src_object,
556 vm_object_offset_t src_offset,
557 vm_object_size_t size,
b0d623f7 558 boolean_t interruptible,
1c79356b
A
559 vm_object_t *_result_object);
560
0b4e3aa0
A
561__private_extern__ vm_object_t vm_object_copy_delayed(
562 vm_object_t src_object,
563 vm_object_offset_t src_offset,
2d21ac55
A
564 vm_object_size_t size,
565 boolean_t src_object_shared);
0b4e3aa0 566
1c79356b 567
1c79356b 568
0b4e3aa0
A
569__private_extern__ kern_return_t vm_object_destroy(
570 vm_object_t object,
571 kern_return_t reason);
1c79356b 572
0b4e3aa0
A
573__private_extern__ void vm_object_pager_create(
574 vm_object_t object);
575
576__private_extern__ void vm_object_page_map(
1c79356b
A
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
0b4e3aa0
A
584__private_extern__ kern_return_t vm_object_upl_request(
585 vm_object_t object,
586 vm_object_offset_t offset,
91447636 587 upl_size_t size,
0b4e3aa0
A
588 upl_t *upl,
589 upl_page_info_t *page_info,
590 unsigned int *count,
591 int flags);
592
91447636
A
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
0b4e3aa0
A
598__private_extern__ boolean_t vm_object_sync(
599 vm_object_t object,
600 vm_object_offset_t offset,
91447636 601 vm_object_size_t size,
0b4e3aa0 602 boolean_t should_flush,
91447636
A
603 boolean_t should_return,
604 boolean_t should_iosync);
1c79356b 605
0b4e3aa0
A
606__private_extern__ kern_return_t vm_object_update(
607 vm_object_t object,
608 vm_object_offset_t offset,
91447636
A
609 vm_object_size_t size,
610 vm_object_offset_t *error_offset,
611 int *io_errno,
0b4e3aa0
A
612 memory_object_return_t should_return,
613 int flags,
614 vm_prot_t prot);
1c79356b 615
0b4e3aa0
A
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);
1c79356b 623
1c79356b 624
1c79356b 625
0b4e3aa0
A
626__private_extern__ vm_object_t vm_object_enter(
627 memory_object_t pager,
1c79356b
A
628 vm_object_size_t size,
629 boolean_t internal,
630 boolean_t init,
631 boolean_t check_named);
632
633
2d21ac55
A
634__private_extern__ void vm_object_cluster_size(
635 vm_object_t object,
636 vm_object_offset_t *start,
637 vm_size_t *length,
b0d623f7
A
638 vm_object_fault_info_t fault_info,
639 uint32_t *io_streaming);
91447636
A
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
0c530ab8 647extern kern_return_t adjust_vm_object_cache(
91447636
A
648 vm_size_t oval,
649 vm_size_t nval);
650
0c530ab8
A
651extern 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
658extern 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,
b0d623f7
A
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
0c530ab8 674
1c79356b
A
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
593a1d5f 682#define VM_OBJECT_EVENT_MAPPING_IN_PROGRESS 3
1c79356b
A
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
b0d623f7
A
687#define VM_OBJECT_EVENT_UNBLOCKED 8
688#define VM_OBJECT_EVENT_PAGING_ONLY_IN_PROGRESS 9
1c79356b
A
689
690#define vm_object_assert_wait(object, event, interruptible) \
9bccf70c
A
691 (((object)->all_wanted |= 1 << (event)), \
692 assert_wait((event_t)((vm_offset_t)(object)+(event)),(interruptible)))
1c79356b
A
693
694#define vm_object_wait(object, event, interruptible) \
9bccf70c
A
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) \
2d21ac55 700 lck_rw_sleep(&(object)->Lock, LCK_SLEEP_DEFAULT, (event_t)(event), (interruptible))
9bccf70c
A
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)))
1c79356b
A
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 */
0c530ab8 725#ifdef VM_PIP_DEBUG
2d21ac55 726#include <libkern/OSDebug.h>
0c530ab8
A
727#define VM_PIP_DEBUG_BEGIN(object) \
728 MACRO_BEGIN \
b0d623f7
A
729 int pip = ((object)->paging_in_progress + \
730 (object)->activity_in_progress); \
731 if (pip < VM_PIP_DEBUG_MAX_REFS) { \
2d21ac55 732 (void) OSBacktrace(&(object)->pip_holders[pip].pip_retaddr[0], \
0c530ab8
A
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 */
1c79356b 739
b0d623f7
A
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) \
1c79356b 760 MACRO_BEGIN \
2d21ac55 761 vm_object_lock_assert_exclusive((object)); \
0c530ab8
A
762 assert((object)->paging_in_progress >= 0); \
763 VM_PIP_DEBUG_BEGIN((object)); \
1c79356b
A
764 (object)->paging_in_progress++; \
765 MACRO_END
766
b0d623f7 767#define vm_object_paging_end(object) \
1c79356b 768 MACRO_BEGIN \
2d21ac55 769 vm_object_lock_assert_exclusive((object)); \
0c530ab8 770 assert((object)->paging_in_progress > 0); \
b0d623f7
A
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); \
1c79356b
A
778 } \
779 MACRO_END
780
781#define vm_object_paging_wait(object, interruptible) \
782 MACRO_BEGIN \
2d21ac55 783 vm_object_lock_assert_exclusive((object)); \
b0d623f7
A
784 while ((object)->paging_in_progress != 0 || \
785 (object)->activity_in_progress != 0) { \
9bccf70c
A
786 wait_result_t _wr; \
787 \
788 _wr = vm_object_sleep((object), \
1c79356b
A
789 VM_OBJECT_EVENT_PAGING_IN_PROGRESS, \
790 (interruptible)); \
1c79356b 791 \
9bccf70c 792 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/\
1c79356b
A
793 /*XXX break; */ \
794 } \
795 MACRO_END
796
b0d623f7
A
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
1c79356b 812
593a1d5f
A
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
1c79356b 845
2d21ac55
A
846#define OBJECT_LOCK_SHARED 0
847#define OBJECT_LOCK_EXCLUSIVE 1
848
849extern lck_grp_t vm_object_lck_grp;
850extern lck_grp_attr_t vm_object_lck_grp_attr;
851extern lck_attr_t vm_object_lck_attr;
852extern lck_attr_t kernel_object_lck_attr;
853
854extern vm_object_t vm_pageout_scan_wants_object;
855
856extern void vm_object_lock(vm_object_t);
857extern boolean_t vm_object_lock_try(vm_object_t);
b0d623f7
A
858extern boolean_t _vm_object_lock_try(vm_object_t);
859extern boolean_t vm_object_lock_avoid(vm_object_t);
2d21ac55
A
860extern void vm_object_lock_shared(vm_object_t);
861extern boolean_t vm_object_lock_try_shared(vm_object_t);
1c79356b
A
862
863/*
864 * Object locking macros
865 */
866
2d21ac55
A
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)
b0d623f7 877#define vm_object_lock_try_scan(object) _vm_object_lock_try(object)
2d21ac55
A
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 */
1c79356b 896
91447636
A
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
1c79356b 900#endif /* _VM_VM_OBJECT_H_ */