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