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