]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_object.h
5c10d57c60bacb7df789165c6bad1914d7035452
[apple/xnu.git] / osfmk / vm / vm_object.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 */
58 /*
59 */
60 /*
61 * File: vm_object.h
62 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * Date: 1985
64 *
65 * Virtual memory object module definitions.
66 */
67
68 #ifndef _VM_VM_OBJECT_H_
69 #define _VM_VM_OBJECT_H_
70
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/lock.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 #if MACH_PAGEMAP
90 #include <vm/vm_external.h>
91 #endif /* MACH_PAGEMAP */
92
93 struct vm_page;
94
95 /*
96 * Types defined:
97 *
98 * vm_object_t Virtual memory object.
99 */
100
101 struct vm_object {
102 queue_head_t memq; /* Resident memory */
103 decl_mutex_data(, Lock) /* Synchronization */
104
105 vm_object_size_t size; /* Object size (only valid
106 * if internal)
107 */
108 struct vm_page *memq_hint;
109 int ref_count; /* Number of references */
110 #if TASK_SWAPPER
111 int res_count; /* Residency references (swap)*/
112 #endif /* TASK_SWAPPER */
113 unsigned int resident_page_count;
114 /* number of resident pages */
115
116 struct vm_object *copy; /* Object that should receive
117 * a copy of my changed pages,
118 * for copy_delay, or just the
119 * temporary object that
120 * shadows this object, for
121 * copy_call.
122 */
123 struct vm_object *shadow; /* My shadow */
124 vm_object_offset_t shadow_offset; /* Offset into shadow */
125
126 memory_object_t pager; /* Where to get data */
127 vm_object_offset_t paging_offset; /* Offset into memory object */
128 memory_object_control_t pager_control; /* Where data comes back */
129
130 memory_object_copy_strategy_t
131 copy_strategy; /* How to handle data copy */
132
133 unsigned int absent_count; /* The number of pages that
134 * have been requested but
135 * not filled. That is, the
136 * number of pages for which
137 * the "absent" attribute is
138 * asserted.
139 */
140
141 unsigned int paging_in_progress;
142 /* The memory object ports are
143 * being used (e.g., for pagein
144 * or pageout) -- don't change
145 * any of these fields (i.e.,
146 * don't collapse, destroy or
147 * terminate)
148 */
149 unsigned int
150 /* boolean_t array */ all_wanted:11, /* Bit array of "want to be
151 * awakened" notations. See
152 * VM_OBJECT_EVENT_* items
153 * below */
154 /* boolean_t */ pager_created:1, /* Has pager been created? */
155 /* boolean_t */ pager_initialized:1, /* Are fields ready to use? */
156 /* boolean_t */ pager_ready:1, /* Will pager take requests? */
157
158 /* boolean_t */ pager_trusted:1,/* The pager for this object
159 * is trusted. This is true for
160 * all internal objects (backed
161 * by the default pager)
162 */
163 /* boolean_t */ can_persist:1, /* The kernel may keep the data
164 * for this object (and rights
165 * to the memory object) after
166 * all address map references
167 * are deallocated?
168 */
169 /* boolean_t */ internal:1, /* Created by the kernel (and
170 * therefore, managed by the
171 * default memory manger)
172 */
173 /* boolean_t */ temporary:1, /* Permanent objects may be
174 * changed externally by the
175 * memory manager, and changes
176 * made in memory must be
177 * reflected back to the memory
178 * manager. Temporary objects
179 * lack both of these
180 * characteristics.
181 */
182 /* boolean_t */ private:1, /* magic device_pager object,
183 * holds private pages only */
184 /* boolean_t */ pageout:1, /* pageout object. contains
185 * private pages that refer to
186 * a real memory object. */
187 /* boolean_t */ alive:1, /* Not yet terminated */
188
189 /* boolean_t */ purgable:2, /* Purgable state. See
190 * VM_OBJECT_PURGABLE_*
191 * items below.
192 */
193 /* boolean_t */ shadowed:1, /* Shadow may exist */
194 /* boolean_t */ silent_overwrite:1,
195 /* Allow full page overwrite
196 * without data_request if
197 * page is absent */
198 /* boolean_t */ advisory_pageout:1,
199 /* Instead of sending page
200 * via OOL, just notify
201 * pager that the kernel
202 * wants to discard it, page
203 * remains in object */
204 /* boolean_t */ true_share:1,
205 /* This object is mapped
206 * in more than one place
207 * and hence cannot be
208 * coalesced */
209 /* boolean_t */ terminating:1,
210 /* Allows vm_object_lookup
211 * and vm_object_deallocate
212 * to special case their
213 * behavior when they are
214 * called as a result of
215 * page cleaning during
216 * object termination
217 */
218 /* boolean_t */ named:1, /* An enforces an internal
219 * naming convention, by
220 * calling the right routines
221 * for allocation and
222 * destruction, UBC references
223 * against the vm_object are
224 * checked.
225 */
226 /* boolean_t */ shadow_severed:1,
227 /* When a permanent object
228 * backing a COW goes away
229 * unexpectedly. This bit
230 * allows vm_fault to return
231 * an error rather than a
232 * zero filled page.
233 */
234 /* boolean_t */ phys_contiguous:1,
235 /* Memory is wired and
236 * guaranteed physically
237 * contiguous. However
238 * it is not device memory
239 * and obeys normal virtual
240 * memory rules w.r.t pmap
241 * access bits.
242 */
243 /* boolean_t */ nophyscache:1;
244 /* When mapped at the
245 * pmap level, don't allow
246 * primary caching. (for
247 * I/O)
248 */
249
250
251
252 queue_chain_t cached_list; /* Attachment point for the
253 * list of objects cached as a
254 * result of their can_persist
255 * value
256 */
257
258 queue_head_t msr_q; /* memory object synchronise
259 request queue */
260
261 vm_object_offset_t last_alloc; /* last allocation offset */
262 vm_object_offset_t sequential; /* sequential access size */
263 vm_size_t cluster_size; /* size of paging cluster */
264 #if MACH_PAGEMAP
265 vm_external_map_t existence_map; /* bitmap of pages written to
266 * backing storage */
267 #endif /* MACH_PAGEMAP */
268 vm_offset_t cow_hint; /* last page present in */
269 /* shadow but not in object */
270 #if MACH_ASSERT
271 struct vm_object *paging_object; /* object which pages to be
272 * swapped out are temporary
273 * put in current object
274 */
275 #endif
276 /* hold object lock when altering */
277 unsigned int /* cache WIMG bits */
278 wimg_bits:8, /* wimg plus some expansion*/
279 not_in_use:24;
280 #ifdef UPL_DEBUG
281 queue_head_t uplq; /* List of outstanding upls */
282 #endif /* UPL_DEBUG */
283 };
284
285 #define VM_PAGE_REMOVE(page) \
286 MACRO_BEGIN \
287 vm_page_t __page = (page); \
288 vm_object_t __object = __page->object; \
289 if (__page == __object->memq_hint) { \
290 vm_page_t __new_hint; \
291 queue_entry_t __qe; \
292 __qe = queue_next(&__page->listq); \
293 if (queue_end(&__object->memq, __qe)) { \
294 __qe = queue_prev(&__page->listq); \
295 if (queue_end(&__object->memq, __qe)) { \
296 __qe = NULL; \
297 } \
298 } \
299 __new_hint = (vm_page_t) __qe; \
300 __object->memq_hint = __new_hint; \
301 } \
302 queue_remove(&__object->memq, __page, vm_page_t, listq); \
303 MACRO_END
304
305 #define VM_PAGE_INSERT(page, object) \
306 MACRO_BEGIN \
307 vm_page_t __page = (page); \
308 vm_object_t __object = (object); \
309 queue_enter(&__object->memq, __page, vm_page_t, listq); \
310 __object->memq_hint = __page; \
311 MACRO_END
312
313 __private_extern__
314 vm_object_t kernel_object; /* the single kernel object */
315
316 __private_extern__
317 unsigned int vm_object_absent_max; /* maximum number of absent pages
318 at a time for each object */
319
320 # define VM_MSYNC_INITIALIZED 0
321 # define VM_MSYNC_SYNCHRONIZING 1
322 # define VM_MSYNC_DONE 2
323
324 struct msync_req {
325 queue_chain_t msr_q; /* object request queue */
326 queue_chain_t req_q; /* vm_msync request queue */
327 unsigned int flag;
328 vm_object_offset_t offset;
329 vm_object_size_t length;
330 vm_object_t object; /* back pointer */
331 decl_mutex_data(, msync_req_lock) /* Lock for this structure */
332 };
333
334 typedef struct msync_req *msync_req_t;
335 #define MSYNC_REQ_NULL ((msync_req_t) 0)
336
337 /*
338 * Macros to allocate and free msync_reqs
339 */
340 #define msync_req_alloc(msr) \
341 MACRO_BEGIN \
342 (msr) = (msync_req_t)kalloc(sizeof(struct msync_req)); \
343 mutex_init(&(msr)->msync_req_lock, 0); \
344 msr->flag = VM_MSYNC_INITIALIZED; \
345 MACRO_END
346
347 #define msync_req_free(msr) \
348 (kfree((msr), sizeof(struct msync_req)))
349
350 #define msr_lock(msr) mutex_lock(&(msr)->msync_req_lock)
351 #define msr_unlock(msr) mutex_unlock(&(msr)->msync_req_lock)
352
353 /*
354 * Declare procedures that operate on VM objects.
355 */
356
357 __private_extern__ void vm_object_bootstrap(void);
358
359 __private_extern__ void vm_object_init(void);
360
361 __private_extern__ vm_object_t vm_object_allocate(
362 vm_object_size_t size);
363
364 __private_extern__ void _vm_object_allocate(vm_object_size_t size,
365 vm_object_t object);
366
367 #if TASK_SWAPPER
368
369 __private_extern__ void vm_object_res_reference(
370 vm_object_t object);
371 __private_extern__ void vm_object_res_deallocate(
372 vm_object_t object);
373 #define VM_OBJ_RES_INCR(object) (object)->res_count++
374 #define VM_OBJ_RES_DECR(object) (object)->res_count--
375
376 #else /* TASK_SWAPPER */
377
378 #define VM_OBJ_RES_INCR(object)
379 #define VM_OBJ_RES_DECR(object)
380 #define vm_object_res_reference(object)
381 #define vm_object_res_deallocate(object)
382
383 #endif /* TASK_SWAPPER */
384
385 #define vm_object_reference_locked(object) \
386 MACRO_BEGIN \
387 vm_object_t RLObject = (object); \
388 assert((RLObject)->ref_count > 0); \
389 (RLObject)->ref_count++; \
390 vm_object_res_reference(RLObject); \
391 MACRO_END
392
393
394 __private_extern__ void vm_object_reference(
395 vm_object_t object);
396
397 #if !MACH_ASSERT
398
399 #define vm_object_reference(object) \
400 MACRO_BEGIN \
401 vm_object_t RObject = (object); \
402 if (RObject) { \
403 vm_object_lock(RObject); \
404 vm_object_reference_locked(RObject); \
405 vm_object_unlock(RObject); \
406 } \
407 MACRO_END
408
409 #endif /* MACH_ASSERT */
410
411 __private_extern__ void vm_object_deallocate(
412 vm_object_t object);
413
414 __private_extern__ kern_return_t vm_object_release_name(
415 vm_object_t object,
416 int flags);
417
418 __private_extern__ void vm_object_pmap_protect(
419 vm_object_t object,
420 vm_object_offset_t offset,
421 vm_object_size_t size,
422 pmap_t pmap,
423 vm_map_offset_t pmap_start,
424 vm_prot_t prot);
425
426 __private_extern__ void vm_object_page_remove(
427 vm_object_t object,
428 vm_object_offset_t start,
429 vm_object_offset_t end);
430
431 __private_extern__ void vm_object_deactivate_pages(
432 vm_object_t object,
433 vm_object_offset_t offset,
434 vm_object_size_t size,
435 boolean_t kill_page);
436
437 __private_extern__ unsigned int vm_object_purge(
438 vm_object_t object);
439
440 __private_extern__ kern_return_t vm_object_purgable_control(
441 vm_object_t object,
442 vm_purgable_t control,
443 int *state);
444
445 __private_extern__ boolean_t vm_object_coalesce(
446 vm_object_t prev_object,
447 vm_object_t next_object,
448 vm_object_offset_t prev_offset,
449 vm_object_offset_t next_offset,
450 vm_object_size_t prev_size,
451 vm_object_size_t next_size);
452
453 __private_extern__ boolean_t vm_object_shadow(
454 vm_object_t *object,
455 vm_object_offset_t *offset,
456 vm_object_size_t length);
457
458 __private_extern__ void vm_object_collapse(
459 vm_object_t object,
460 vm_object_offset_t offset);
461
462 __private_extern__ boolean_t vm_object_copy_quickly(
463 vm_object_t *_object,
464 vm_object_offset_t src_offset,
465 vm_object_size_t size,
466 boolean_t *_src_needs_copy,
467 boolean_t *_dst_needs_copy);
468
469 __private_extern__ kern_return_t vm_object_copy_strategically(
470 vm_object_t src_object,
471 vm_object_offset_t src_offset,
472 vm_object_size_t size,
473 vm_object_t *dst_object,
474 vm_object_offset_t *dst_offset,
475 boolean_t *dst_needs_copy);
476
477 __private_extern__ kern_return_t vm_object_copy_slowly(
478 vm_object_t src_object,
479 vm_object_offset_t src_offset,
480 vm_object_size_t size,
481 int interruptible,
482 vm_object_t *_result_object);
483
484 __private_extern__ vm_object_t vm_object_copy_delayed(
485 vm_object_t src_object,
486 vm_object_offset_t src_offset,
487 vm_object_size_t size);
488
489
490
491 __private_extern__ kern_return_t vm_object_destroy(
492 vm_object_t object,
493 kern_return_t reason);
494
495 __private_extern__ void vm_object_pager_create(
496 vm_object_t object);
497
498 __private_extern__ void vm_object_page_map(
499 vm_object_t object,
500 vm_object_offset_t offset,
501 vm_object_size_t size,
502 vm_object_offset_t (*map_fn)
503 (void *, vm_object_offset_t),
504 void *map_fn_data);
505
506 __private_extern__ kern_return_t vm_object_upl_request(
507 vm_object_t object,
508 vm_object_offset_t offset,
509 upl_size_t size,
510 upl_t *upl,
511 upl_page_info_t *page_info,
512 unsigned int *count,
513 int flags);
514
515 __private_extern__ kern_return_t vm_object_transpose(
516 vm_object_t object1,
517 vm_object_t object2,
518 vm_object_size_t transpose_size);
519
520 __private_extern__ boolean_t vm_object_sync(
521 vm_object_t object,
522 vm_object_offset_t offset,
523 vm_object_size_t size,
524 boolean_t should_flush,
525 boolean_t should_return,
526 boolean_t should_iosync);
527
528 __private_extern__ kern_return_t vm_object_update(
529 vm_object_t object,
530 vm_object_offset_t offset,
531 vm_object_size_t size,
532 vm_object_offset_t *error_offset,
533 int *io_errno,
534 memory_object_return_t should_return,
535 int flags,
536 vm_prot_t prot);
537
538 __private_extern__ kern_return_t vm_object_lock_request(
539 vm_object_t object,
540 vm_object_offset_t offset,
541 vm_object_size_t size,
542 memory_object_return_t should_return,
543 int flags,
544 vm_prot_t prot);
545
546
547
548 __private_extern__ vm_object_t vm_object_enter(
549 memory_object_t pager,
550 vm_object_size_t size,
551 boolean_t internal,
552 boolean_t init,
553 boolean_t check_named);
554
555
556 /*
557 * Purgable object state.
558 */
559
560 #define VM_OBJECT_NONPURGABLE 0 /* not a purgable object */
561 #define VM_OBJECT_PURGABLE_NONVOLATILE 1 /* non-volatile purgable object */
562 #define VM_OBJECT_PURGABLE_VOLATILE 2 /* volatile (but intact) purgable object */
563 #define VM_OBJECT_PURGABLE_EMPTY 3 /* volatile purgable object that has been emptied */
564
565 __private_extern__ kern_return_t vm_object_populate_with_private(
566 vm_object_t object,
567 vm_object_offset_t offset,
568 ppnum_t phys_page,
569 vm_size_t size);
570
571 __private_extern__ kern_return_t adjust_vm_object_cache(
572 vm_size_t oval,
573 vm_size_t nval);
574
575 /*
576 * Event waiting handling
577 */
578
579 #define VM_OBJECT_EVENT_INITIALIZED 0
580 #define VM_OBJECT_EVENT_PAGER_READY 1
581 #define VM_OBJECT_EVENT_PAGING_IN_PROGRESS 2
582 #define VM_OBJECT_EVENT_ABSENT_COUNT 3
583 #define VM_OBJECT_EVENT_LOCK_IN_PROGRESS 4
584 #define VM_OBJECT_EVENT_UNCACHING 5
585 #define VM_OBJECT_EVENT_COPY_CALL 6
586 #define VM_OBJECT_EVENT_CACHING 7
587
588 #define vm_object_assert_wait(object, event, interruptible) \
589 (((object)->all_wanted |= 1 << (event)), \
590 assert_wait((event_t)((vm_offset_t)(object)+(event)),(interruptible)))
591
592 #define vm_object_wait(object, event, interruptible) \
593 (vm_object_assert_wait((object),(event),(interruptible)), \
594 vm_object_unlock(object), \
595 thread_block(THREAD_CONTINUE_NULL)) \
596
597 #define thread_sleep_vm_object(object, event, interruptible) \
598 thread_sleep_mutex((event_t)(event), &(object)->Lock, (interruptible))
599
600 #define vm_object_sleep(object, event, interruptible) \
601 (((object)->all_wanted |= 1 << (event)), \
602 thread_sleep_vm_object((object), \
603 ((vm_offset_t)(object)+(event)), (interruptible)))
604
605 #define vm_object_wakeup(object, event) \
606 MACRO_BEGIN \
607 if ((object)->all_wanted & (1 << (event))) \
608 thread_wakeup((event_t)((vm_offset_t)(object) + (event))); \
609 (object)->all_wanted &= ~(1 << (event)); \
610 MACRO_END
611
612 #define vm_object_set_wanted(object, event) \
613 MACRO_BEGIN \
614 ((object)->all_wanted |= (1 << (event))); \
615 MACRO_END
616
617 #define vm_object_wanted(object, event) \
618 ((object)->all_wanted & (1 << (event)))
619
620 /*
621 * Routines implemented as macros
622 */
623
624 #define vm_object_paging_begin(object) \
625 MACRO_BEGIN \
626 (object)->paging_in_progress++; \
627 MACRO_END
628
629 #define vm_object_paging_end(object) \
630 MACRO_BEGIN \
631 assert((object)->paging_in_progress != 0); \
632 if (--(object)->paging_in_progress == 0) { \
633 vm_object_wakeup(object, \
634 VM_OBJECT_EVENT_PAGING_IN_PROGRESS); \
635 } \
636 MACRO_END
637
638 #define vm_object_paging_wait(object, interruptible) \
639 MACRO_BEGIN \
640 while ((object)->paging_in_progress != 0) { \
641 wait_result_t _wr; \
642 \
643 _wr = vm_object_sleep((object), \
644 VM_OBJECT_EVENT_PAGING_IN_PROGRESS, \
645 (interruptible)); \
646 \
647 /*XXX if ((interruptible) && (_wr != THREAD_AWAKENED))*/\
648 /*XXX break; */ \
649 } \
650 MACRO_END
651
652 #define vm_object_absent_assert_wait(object, interruptible) \
653 MACRO_BEGIN \
654 vm_object_assert_wait( (object), \
655 VM_OBJECT_EVENT_ABSENT_COUNT, \
656 (interruptible)); \
657 MACRO_END
658
659
660 #define vm_object_absent_release(object) \
661 MACRO_BEGIN \
662 (object)->absent_count--; \
663 vm_object_wakeup((object), \
664 VM_OBJECT_EVENT_ABSENT_COUNT); \
665 MACRO_END
666
667 /*
668 * Object locking macros
669 */
670
671 #define vm_object_lock_init(object) mutex_init(&(object)->Lock, 0)
672 #define vm_object_lock(object) mutex_lock(&(object)->Lock)
673 #define vm_object_unlock(object) mutex_unlock(&(object)->Lock)
674 #define vm_object_lock_try(object) mutex_try(&(object)->Lock)
675
676 #define vm_object_round_page(x) (((vm_object_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
677 #define vm_object_trunc_page(x) ((vm_object_offset_t)(x) & ~((signed)PAGE_MASK))
678
679 #endif /* _VM_VM_OBJECT_H_ */