2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
61 * Author: Avadis Tevanian, Jr., Michael Wayne Young
64 * Virtual memory map module definitions.
73 #include <mach/mach_types.h>
74 #include <mach/kern_return.h>
75 #include <mach/boolean.h>
76 #include <mach/vm_types.h>
77 #include <mach/vm_prot.h>
78 #include <mach/vm_inherit.h>
79 #include <mach/vm_behavior.h>
80 #include <mach/vm_param.h>
85 #include <sys/cdefs.h>
89 extern void vm_map_reference(vm_map_t map
);
90 extern vm_map_t
current_map(void);
92 /* Setup reserved areas in a new VM map */
93 extern kern_return_t
vm_map_exec(
102 #ifdef MACH_KERNEL_PRIVATE
104 #include <task_swapper.h>
105 #include <mach_assert.h>
107 #include <vm/vm_object.h>
108 #include <vm/vm_page.h>
109 #include <kern/locks.h>
110 #include <kern/zalloc.h>
111 #include <kern/macro_help.h>
113 #include <kern/thread.h>
115 #define current_map_fast() (current_thread()->map)
116 #define current_map() (current_map_fast())
118 #include <vm/vm_map_store.h>
124 * vm_map_t the high-level address map data structure.
125 * vm_map_entry_t an entry in an address map.
126 * vm_map_version_t a timestamp of a map, for use with vm_map_lookup
127 * vm_map_copy_t represents memory copied from an address map,
128 * used for inter-map copy operations
130 typedef struct vm_map_entry
*vm_map_entry_t
;
131 #define VM_MAP_ENTRY_NULL ((vm_map_entry_t) 0)
135 * Type: vm_map_object_t [internal use only]
138 * The target of an address mapping, either a virtual
139 * memory object or a sub map (of the kernel map).
141 typedef union vm_map_object
{
142 vm_object_t vmo_object
; /* object object */
143 vm_map_t vmo_submap
; /* belongs to another map */
146 #define named_entry_lock_init(object) lck_mtx_init(&(object)->Lock, &vm_object_lck_grp, &vm_object_lck_attr)
147 #define named_entry_lock_destroy(object) lck_mtx_destroy(&(object)->Lock, &vm_object_lck_grp)
148 #define named_entry_lock(object) lck_mtx_lock(&(object)->Lock)
149 #define named_entry_unlock(object) lck_mtx_unlock(&(object)->Lock)
152 * Type: vm_named_entry_t [internal use only]
155 * Description of a mapping to a memory cache object.
158 * While the handle to this object is used as a means to map
159 * and pass around the right to map regions backed by pagers
160 * of all sorts, the named_entry itself is only manipulated
161 * by the kernel. Named entries hold information on the
162 * right to map a region of a cached object. Namely,
163 * the target cache object, the beginning and ending of the
164 * region to be mapped, and the permissions, (read, write)
165 * with which it can be mapped.
169 struct vm_named_entry
{
170 decl_lck_mtx_data(, Lock
) /* Synchronization */
172 vm_object_t object
; /* object I point to */
173 memory_object_t pager
; /* amo pager port */
174 vm_map_t map
; /* map backing submap */
175 vm_map_copy_t copy
; /* a VM map copy */
177 vm_object_offset_t offset
; /* offset into object */
178 vm_object_size_t size
; /* size of region */
179 vm_object_offset_t data_offset
; /* offset to first byte of data */
180 vm_prot_t protection
; /* access permissions */
181 int ref_count
; /* Number of references */
182 unsigned int /* Is backing.xxx : */
183 /* boolean_t */ internal
:1, /* ... an internal object */
184 /* boolean_t */ is_sub_map
:1, /* ... a submap? */
185 /* boolean_t */ is_pager
:1, /* ... a pager port */
186 /* boolean_t */ is_copy
:1; /* ... a VM map copy */
190 * Type: vm_map_entry_t [internal use only]
193 * A single mapping within an address map.
196 * Address map entries consist of start and end addresses,
197 * a VM object (or sub map) and offset into that object,
198 * and user-exported inheritance and protection information.
199 * Control information for virtual copy operations is also
200 * stored in the address map entry.
203 struct vm_map_links
{
204 struct vm_map_entry
*prev
; /* previous entry */
205 struct vm_map_entry
*next
; /* next entry */
206 vm_map_offset_t start
; /* start address */
207 vm_map_offset_t end
; /* end address */
212 * The "alias" field can be updated while holding the VM map lock
213 * "shared". It's OK as along as it's the only field that can be
214 * updated without the VM map "exclusive" lock.
216 #define VME_OBJECT(entry) ((entry)->vme_object.vmo_object)
217 #define VME_OBJECT_SET(entry, object) \
219 (entry)->vme_object.vmo_object = (object); \
221 #define VME_SUBMAP(entry) ((entry)->vme_object.vmo_submap)
222 #define VME_SUBMAP_SET(entry, submap) \
224 (entry)->vme_object.vmo_submap = (submap); \
226 #define VME_OFFSET(entry) ((entry)->vme_offset & ~PAGE_MASK)
227 #define VME_OFFSET_SET(entry, offset) \
230 __alias = VME_ALIAS((entry)); \
231 assert((offset & PAGE_MASK) == 0); \
232 (entry)->vme_offset = offset | __alias; \
234 #define VME_OBJECT_SHADOW(entry, length) \
236 vm_object_t __object; \
237 vm_object_offset_t __offset; \
238 __object = VME_OBJECT((entry)); \
239 __offset = VME_OFFSET((entry)); \
240 vm_object_shadow(&__object, &__offset, (length)); \
241 if (__object != VME_OBJECT((entry))) { \
242 VME_OBJECT_SET((entry), __object); \
244 if (__offset != VME_OFFSET((entry))) { \
245 VME_OFFSET_SET((entry), __offset); \
249 #define VME_ALIAS_MASK (PAGE_MASK)
250 #define VME_ALIAS(entry) ((unsigned int)((entry)->vme_offset & VME_ALIAS_MASK))
251 #define VME_ALIAS_SET(entry, alias) \
253 vm_map_offset_t __offset; \
254 __offset = VME_OFFSET((entry)); \
255 (entry)->vme_offset = __offset | ((alias) & VME_ALIAS_MASK); \
258 struct vm_map_entry
{
259 struct vm_map_links links
; /* links to other entries */
260 #define vme_prev links.prev
261 #define vme_next links.next
262 #define vme_start links.start
263 #define vme_end links.end
265 struct vm_map_store store
;
266 union vm_map_object vme_object
; /* object I point to */
267 vm_object_offset_t vme_offset
; /* offset into object */
270 /* boolean_t */ is_shared
:1, /* region is shared */
271 /* boolean_t */ is_sub_map
:1, /* Is "object" a submap? */
272 /* boolean_t */ in_transition
:1, /* Entry being changed */
273 /* boolean_t */ needs_wakeup
:1, /* Waiters on in_transition */
274 /* vm_behavior_t */ behavior
:2, /* user paging behavior hint */
275 /* behavior is not defined for submap type */
276 /* boolean_t */ needs_copy
:1, /* object need to be copied? */
278 /* Only in task maps: */
279 /* vm_prot_t */ protection
:3, /* protection code */
280 /* vm_prot_t */ max_protection
:3, /* maximum protection */
281 /* vm_inherit_t */ inheritance
:2, /* inheritance */
282 /* boolean_t */ use_pmap
:1, /*
283 * use_pmap is overloaded:
286 * else (i.e. if object):
287 * use pmap accounting
290 /* boolean_t */ no_cache
:1, /* should new pages be cached? */
291 /* boolean_t */ permanent
:1, /* mapping can not be removed */
292 /* boolean_t */ superpage_size
:1, /* use superpages of a certain size */
293 /* boolean_t */ map_aligned
:1, /* align to map's page size */
294 /* boolean_t */ zero_wired_pages
:1, /* zero out the wired pages of
295 * this entry it is being deleted
296 * without unwiring them */
297 /* boolean_t */ used_for_jit
:1,
298 /* boolean_t */ from_reserved_zone
:1, /* Allocated from
299 * kernel reserved zone */
301 /* iokit accounting: use the virtual size rather than resident size: */
302 /* boolean_t */ iokit_acct
:1,
303 /* boolean_t */ vme_resilient_codesign
:1,
304 /* boolean_t */ vme_resilient_media
:1,
305 /* boolean_t */ vme_atomic
:1, /* entry cannot be split/coalesced */
309 unsigned short wired_count
; /* can be paged if = 0 */
310 unsigned short user_wired_count
; /* for vm_wire */
312 #define MAP_ENTRY_CREATION_DEBUG (1)
313 #define MAP_ENTRY_INSERTION_DEBUG (1)
315 #if MAP_ENTRY_CREATION_DEBUG
316 struct vm_map_header
*vme_creation_maphdr
;
317 uintptr_t vme_creation_bt
[16];
319 #if MAP_ENTRY_INSERTION_DEBUG
320 uintptr_t vme_insertion_bt
[16];
325 * Convenience macros for dealing with superpages
326 * SUPERPAGE_NBASEPAGES is architecture dependent and defined in pmap.h
328 #define SUPERPAGE_SIZE (PAGE_SIZE*SUPERPAGE_NBASEPAGES)
329 #define SUPERPAGE_MASK (-SUPERPAGE_SIZE)
330 #define SUPERPAGE_ROUND_DOWN(a) (a & SUPERPAGE_MASK)
331 #define SUPERPAGE_ROUND_UP(a) ((a + SUPERPAGE_SIZE-1) & SUPERPAGE_MASK)
334 * wired_counts are unsigned short. This value is used to safeguard
335 * against any mishaps due to runaway user programs.
337 #define MAX_WIRE_COUNT 65535
342 * Type: struct vm_map_header
345 * Header for a vm_map and a vm_map_copy.
349 struct vm_map_header
{
350 struct vm_map_links links
; /* first, last, min, max */
351 int nentries
; /* Number of entries */
352 boolean_t entries_pageable
;
353 /* are map entries pageable? */
354 #ifdef VM_MAP_STORE_USE_RB
355 struct rb_head rb_head_store
;
357 int page_shift
; /* page shift */
360 #define VM_MAP_HDR_PAGE_SHIFT(hdr) ((hdr)->page_shift)
361 #define VM_MAP_HDR_PAGE_SIZE(hdr) (1 << VM_MAP_HDR_PAGE_SHIFT((hdr)))
362 #define VM_MAP_HDR_PAGE_MASK(hdr) (VM_MAP_HDR_PAGE_SIZE((hdr)) - 1)
365 * Type: vm_map_t [exported; contents invisible]
368 * An address map -- a directory relating valid
369 * regions of a task's address space to the corresponding
370 * virtual memory objects.
373 * Maps are doubly-linked lists of map entries, sorted
374 * by address. One hint is used to start
375 * searches again from the last successful search,
376 * insertion, or removal. Another hint is used to
377 * quickly find free space.
380 lck_rw_t lock
; /* map lock */
381 struct vm_map_header hdr
; /* Map entry header */
382 #define min_offset hdr.links.start /* start of range */
383 #define max_offset hdr.links.end /* end of range */
384 pmap_t pmap
; /* Physical map */
385 vm_map_size_t size
; /* virtual size */
386 vm_map_size_t user_wire_limit
;/* rlimit on user locked memory */
387 vm_map_size_t user_wire_size
; /* current size of user locked memory in this map */
391 * If map->disable_vmentry_reuse == TRUE:
392 * the end address of the highest allocated vm_map_entry_t.
394 vm_map_offset_t vmu1_highest_entry_end
;
396 * For a nested VM map:
397 * the lowest address in this nested VM map that we would
398 * expect to be unnested under normal operation (i.e. for
399 * regular copy-on-write on DATA section).
401 vm_map_offset_t vmu1_lowest_unnestable_start
;
403 #define highest_entry_end vmu1.vmu1_highest_entry_end
404 #define lowest_unnestable_start vmu1.vmu1_lowest_unnestable_start
406 int ref_count
; /* Reference count */
408 int res_count
; /* Residence count (swap) */
409 int sw_state
; /* Swap state */
410 #endif /* TASK_SWAPPER */
411 decl_lck_mtx_data(, s_lock
) /* Lock ref, res fields */
412 lck_mtx_ext_t s_lock_ext
;
413 vm_map_entry_t hint
; /* hint for quick lookups */
414 struct vm_map_links
* hole_hint
; /* hint for quick hole lookups */
416 vm_map_entry_t _first_free
; /* First free space hint */
417 struct vm_map_links
* _holes
; /* links all holes between entries */
418 }f_s
; /* Union for free space data structures being used */
420 #define first_free f_s._first_free
421 #define holes_list f_s._holes
424 /* boolean_t */ wait_for_space
:1, /* Should callers wait for space? */
425 /* boolean_t */ wiring_required
:1, /* All memory wired? */
426 /* boolean_t */ no_zero_fill
:1, /*No zero fill absent pages */
427 /* boolean_t */ mapped_in_other_pmaps
:1, /*has this submap been mapped in maps that use a different pmap */
428 /* boolean_t */ switch_protect
:1, /* Protect map from write faults while switched */
429 /* boolean_t */ disable_vmentry_reuse
:1, /* All vm entries should keep using newer and higher addresses in the map */
430 /* boolean_t */ map_disallow_data_exec
:1, /* Disallow execution from data pages on exec-permissive architectures */
431 /* boolean_t */ holelistenabled
:1,
432 /* boolean_t */ is_nested_map
:1,
433 /* reserved */ pad
:23;
434 unsigned int timestamp
; /* Version number */
435 unsigned int color_rr
; /* next color (not protected by a lock) */
437 boolean_t jit_entry_exists
;
440 #define vm_map_to_entry(map) ((struct vm_map_entry *) &(map)->hdr.links)
441 #define vm_map_first_entry(map) ((map)->hdr.links.next)
442 #define vm_map_last_entry(map) ((map)->hdr.links.prev)
446 * VM map swap states. There are no transition states.
448 #define MAP_SW_IN 1 /* map is swapped in; residence count > 0 */
449 #define MAP_SW_OUT 2 /* map is out (res_count == 0 */
450 #endif /* TASK_SWAPPER */
453 * Type: vm_map_version_t [exported; contents invisible]
456 * Map versions may be used to quickly validate a previous
460 * Because they are bulky objects, map versions are usually
461 * passed by reference.
464 * Just a timestamp for the main map.
466 typedef struct vm_map_version
{
467 unsigned int main_timestamp
;
471 * Type: vm_map_copy_t [exported; contents invisible]
474 * A map copy object represents a region of virtual memory
475 * that has been copied from an address map but is still
478 * A map copy object may only be used by a single thread
482 * There are three formats for map copy objects.
483 * The first is very similar to the main
484 * address map in structure, and as a result, some
485 * of the internal maintenance functions/macros can
486 * be used with either address maps or map copy objects.
488 * The map copy object contains a header links
489 * entry onto which the other entries that represent
490 * the region are chained.
492 * The second format is a single vm object. This was used
493 * primarily in the pageout path - but is not currently used
494 * except for placeholder copy objects (see vm_map_copy_copy()).
496 * The third format is a kernel buffer copy object - for data
497 * small enough that physical copies were the most efficient
498 * method. This method uses a zero-sized array unioned with
499 * other format-specific data in the 'c_u' member. This unsized
500 * array overlaps the other elements and allows us to use this
501 * extra structure space for physical memory copies. On 64-bit
502 * systems this saves ~64 bytes per vm_map_copy.
507 #define VM_MAP_COPY_ENTRY_LIST 1
508 #define VM_MAP_COPY_OBJECT 2
509 #define VM_MAP_COPY_KERNEL_BUFFER 3
510 vm_object_offset_t offset
;
513 struct vm_map_header hdr
; /* ENTRY_LIST */
514 vm_object_t object
; /* OBJECT */
515 uint8_t kdata
[0]; /* KERNEL_BUFFER */
520 #define cpy_hdr c_u.hdr
522 #define cpy_object c_u.object
523 #define cpy_kdata c_u.kdata
524 #define cpy_kdata_hdr_sz (offsetof(struct vm_map_copy, c_u.kdata))
526 #define VM_MAP_COPY_PAGE_SHIFT(copy) ((copy)->cpy_hdr.page_shift)
527 #define VM_MAP_COPY_PAGE_SIZE(copy) (1 << VM_MAP_COPY_PAGE_SHIFT((copy)))
528 #define VM_MAP_COPY_PAGE_MASK(copy) (VM_MAP_COPY_PAGE_SIZE((copy)) - 1)
531 * Useful macros for entry list copy objects
534 #define vm_map_copy_to_entry(copy) \
535 ((struct vm_map_entry *) &(copy)->cpy_hdr.links)
536 #define vm_map_copy_first_entry(copy) \
537 ((copy)->cpy_hdr.links.next)
538 #define vm_map_copy_last_entry(copy) \
539 ((copy)->cpy_hdr.links.prev)
542 * Macros: vm_map_lock, etc. [internal use only]
544 * Perform locking on the data portion of a map.
545 * When multiple maps are to be locked, order by map address.
546 * (See vm_map.c::vm_remap())
549 #define vm_map_lock_init(map) \
550 ((map)->timestamp = 0 , \
551 lck_rw_init(&(map)->lock, &vm_map_lck_grp, &vm_map_lck_rw_attr))
553 #define vm_map_lock(map) lck_rw_lock_exclusive(&(map)->lock)
554 #define vm_map_unlock(map) \
555 ((map)->timestamp++ , lck_rw_done(&(map)->lock))
556 #define vm_map_lock_read(map) lck_rw_lock_shared(&(map)->lock)
557 #define vm_map_unlock_read(map) lck_rw_done(&(map)->lock)
558 #define vm_map_lock_write_to_read(map) \
559 ((map)->timestamp++ , lck_rw_lock_exclusive_to_shared(&(map)->lock))
560 /* lock_read_to_write() returns FALSE on failure. Macro evaluates to
561 * zero on success and non-zero value on failure.
563 #define vm_map_lock_read_to_write(map) (lck_rw_lock_shared_to_exclusive(&(map)->lock) != TRUE)
565 #if MACH_ASSERT || DEBUG
566 #define vm_map_lock_assert_held(map) \
567 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_HELD)
568 #define vm_map_lock_assert_shared(map) \
569 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_SHARED)
570 #define vm_map_lock_assert_exclusive(map) \
571 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_EXCLUSIVE)
572 #define vm_map_lock_assert_notheld(map) \
573 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_NOTHELD)
574 #else /* MACH_ASSERT || DEBUG */
575 #define vm_map_lock_assert_held(map)
576 #define vm_map_lock_assert_shared(map)
577 #define vm_map_lock_assert_exclusive(map)
578 #define vm_map_lock_assert_notheld(map)
579 #endif /* MACH_ASSERT || DEBUG */
582 * Exported procedures that operate on vm_map_t.
585 /* Initialize the module */
586 extern void vm_map_init(void);
588 extern void vm_kernel_reserved_entry_init(void);
590 /* Allocate a range in the specified virtual address map and
591 * return the entry allocated for that range. */
592 extern kern_return_t
vm_map_find_space(
594 vm_map_address_t
*address
, /* OUT */
596 vm_map_offset_t mask
,
598 vm_map_entry_t
*o_entry
); /* OUT */
600 extern void vm_map_clip_start(
602 vm_map_entry_t entry
,
603 vm_map_offset_t endaddr
);
604 extern void vm_map_clip_end(
606 vm_map_entry_t entry
,
607 vm_map_offset_t endaddr
);
608 extern boolean_t
vm_map_entry_should_cow_for_true_share(
609 vm_map_entry_t entry
);
611 /* Lookup map entry containing or the specified address in the given map */
612 extern boolean_t
vm_map_lookup_entry(
614 vm_map_address_t address
,
615 vm_map_entry_t
*entry
); /* OUT */
617 extern void vm_map_copy_remap(
619 vm_map_entry_t where
,
621 vm_map_offset_t adjustment
,
624 vm_inherit_t inheritance
);
626 /* Find the VM object, offset, and protection for a given virtual address
627 * in the specified map, assuming a page fault of the type specified. */
628 extern kern_return_t
vm_map_lookup_locked(
629 vm_map_t
*var_map
, /* IN/OUT */
630 vm_map_address_t vaddr
,
631 vm_prot_t fault_type
,
632 int object_lock_type
,
633 vm_map_version_t
*out_version
, /* OUT */
634 vm_object_t
*object
, /* OUT */
635 vm_object_offset_t
*offset
, /* OUT */
636 vm_prot_t
*out_prot
, /* OUT */
637 boolean_t
*wired
, /* OUT */
638 vm_object_fault_info_t fault_info
, /* OUT */
639 vm_map_t
*real_map
); /* OUT */
641 /* Verifies that the map has not changed since the given version. */
642 extern boolean_t
vm_map_verify(
644 vm_map_version_t
*version
); /* REF */
646 extern vm_map_entry_t
vm_map_entry_insert(
648 vm_map_entry_t insp_entry
,
649 vm_map_offset_t start
,
652 vm_object_offset_t offset
,
653 boolean_t needs_copy
,
655 boolean_t in_transition
,
656 vm_prot_t cur_protection
,
657 vm_prot_t max_protection
,
658 vm_behavior_t behavior
,
659 vm_inherit_t inheritance
,
660 unsigned wired_count
,
663 unsigned int superpage_size
,
664 boolean_t clear_map_aligned
,
665 boolean_t is_submap
);
669 * Functions implemented as macros
671 #define vm_map_min(map) ((map)->min_offset)
672 /* Lowest valid address in
675 #define vm_map_max(map) ((map)->max_offset)
676 /* Highest valid address */
678 #define vm_map_pmap(map) ((map)->pmap)
679 /* Physical map associated
680 * with this address map */
682 #define vm_map_verify_done(map, version) vm_map_unlock_read(map)
683 /* Operation that required
684 * a verified lookup is
688 * Macros/functions for map residence counts and swapin/out of vm maps
693 /* Gain a reference to an existing map */
694 extern void vm_map_reference(
696 /* Lose a residence count */
697 extern void vm_map_res_deallocate(
699 /* Gain a residence count on a map */
700 extern void vm_map_res_reference(
702 /* Gain reference & residence counts to possibly swapped-out map */
703 extern void vm_map_reference_swap(
706 #else /* MACH_ASSERT */
708 #define vm_map_reference(map) \
710 vm_map_t Map = (map); \
712 lck_mtx_lock(&Map->s_lock); \
715 lck_mtx_unlock(&Map->s_lock); \
719 #define vm_map_res_reference(map) \
721 vm_map_t Lmap = (map); \
722 if (Lmap->res_count == 0) { \
723 lck_mtx_unlock(&Lmap->s_lock);\
725 vm_map_swapin(Lmap); \
726 lck_mtx_lock(&Lmap->s_lock); \
728 vm_map_unlock(Lmap); \
733 #define vm_map_res_deallocate(map) \
735 vm_map_t Map = (map); \
736 if (--Map->res_count == 0) { \
737 lck_mtx_unlock(&Map->s_lock); \
739 vm_map_swapout(Map); \
740 vm_map_unlock(Map); \
741 lck_mtx_lock(&Map->s_lock); \
745 #define vm_map_reference_swap(map) \
747 vm_map_t Map = (map); \
748 lck_mtx_lock(&Map->s_lock); \
750 vm_map_res_reference(Map); \
751 lck_mtx_unlock(&Map->s_lock); \
753 #endif /* MACH_ASSERT */
755 extern void vm_map_swapin(
758 extern void vm_map_swapout(
761 #else /* TASK_SWAPPER */
763 #define vm_map_reference(map) \
765 vm_map_t Map = (map); \
767 lck_mtx_lock(&Map->s_lock); \
769 lck_mtx_unlock(&Map->s_lock); \
773 #define vm_map_reference_swap(map) vm_map_reference(map)
774 #define vm_map_res_reference(map)
775 #define vm_map_res_deallocate(map)
777 #endif /* TASK_SWAPPER */
780 * Submap object. Must be used to create memory to be put
781 * in a submap by vm_map_submap.
783 extern vm_object_t vm_submap_object
;
786 * Wait and wakeup macros for in_transition map entries.
788 #define vm_map_entry_wait(map, interruptible) \
789 ((map)->timestamp++ , \
790 lck_rw_sleep(&(map)->lock, LCK_SLEEP_EXCLUSIVE|LCK_SLEEP_PROMOTED_PRI, \
791 (event_t)&(map)->hdr, interruptible))
794 #define vm_map_entry_wakeup(map) \
795 thread_wakeup((event_t)(&(map)->hdr))
798 #define vm_map_ref_fast(map) \
800 lck_mtx_lock(&map->s_lock); \
802 vm_map_res_reference(map); \
803 lck_mtx_unlock(&map->s_lock); \
806 #define vm_map_dealloc_fast(map) \
810 lck_mtx_lock(&map->s_lock); \
811 c = --map->ref_count; \
813 vm_map_res_deallocate(map); \
814 lck_mtx_unlock(&map->s_lock); \
816 vm_map_destroy(map); \
820 /* simplify map entries */
821 extern void vm_map_simplify_entry(
823 vm_map_entry_t this_entry
);
824 extern void vm_map_simplify(
826 vm_map_offset_t start
);
828 /* Move the information in a map copy object to a new map copy object */
829 extern vm_map_copy_t
vm_map_copy_copy(
832 /* Create a copy object from an object. */
833 extern kern_return_t
vm_map_copyin_object(
835 vm_object_offset_t offset
,
836 vm_object_size_t size
,
837 vm_map_copy_t
*copy_result
); /* OUT */
839 extern kern_return_t
vm_map_random_address_for_size(
841 vm_map_offset_t
*address
,
844 /* Enter a mapping */
845 extern kern_return_t
vm_map_enter(
847 vm_map_offset_t
*address
,
849 vm_map_offset_t mask
,
852 vm_object_offset_t offset
,
853 boolean_t needs_copy
,
854 vm_prot_t cur_protection
,
855 vm_prot_t max_protection
,
856 vm_inherit_t inheritance
);
859 /* XXX should go away - replaced with regular enter of contig object */
860 extern kern_return_t
vm_map_enter_cpm(
862 vm_map_address_t
*addr
,
866 extern kern_return_t
vm_map_remap(
868 vm_map_offset_t
*address
,
870 vm_map_offset_t mask
,
873 vm_map_offset_t memory_address
,
875 vm_prot_t
*cur_protection
,
876 vm_prot_t
*max_protection
,
877 vm_inherit_t inheritance
);
881 * Read and write from a kernel buffer to a specified map.
883 extern kern_return_t
vm_map_write_user(
886 vm_map_offset_t dst_addr
,
889 extern kern_return_t
vm_map_read_user(
891 vm_map_offset_t src_addr
,
895 /* Create a new task map using an existing task map as a template. */
896 extern vm_map_t
vm_map_fork(
900 #define VM_MAP_FORK_SHARE_IF_INHERIT_NONE 0x00000001
901 #define VM_MAP_FORK_PRESERVE_PURGEABLE 0x00000002
903 /* Change inheritance */
904 extern kern_return_t
vm_map_inherit(
906 vm_map_offset_t start
,
908 vm_inherit_t new_inheritance
);
910 /* Add or remove machine-dependent attributes from map regions */
911 extern kern_return_t
vm_map_machine_attribute(
913 vm_map_offset_t start
,
915 vm_machine_attribute_t attribute
,
916 vm_machine_attribute_val_t
* value
); /* IN/OUT */
918 extern kern_return_t
vm_map_msync(
920 vm_map_address_t address
,
922 vm_sync_t sync_flags
);
924 /* Set paging behavior */
925 extern kern_return_t
vm_map_behavior_set(
927 vm_map_offset_t start
,
929 vm_behavior_t new_behavior
);
931 extern kern_return_t
vm_map_purgable_control(
933 vm_map_offset_t address
,
934 vm_purgable_t control
,
937 extern kern_return_t
vm_map_region(
939 vm_map_offset_t
*address
,
941 vm_region_flavor_t flavor
,
942 vm_region_info_t info
,
943 mach_msg_type_number_t
*count
,
944 mach_port_t
*object_name
);
946 extern kern_return_t
vm_map_region_recurse_64(
948 vm_map_offset_t
*address
,
950 natural_t
*nesting_depth
,
951 vm_region_submap_info_64_t info
,
952 mach_msg_type_number_t
*count
);
954 extern kern_return_t
vm_map_page_query_internal(
956 vm_map_offset_t offset
,
960 extern kern_return_t
vm_map_query_volatile(
962 mach_vm_size_t
*volatile_virtual_size_p
,
963 mach_vm_size_t
*volatile_resident_size_p
,
964 mach_vm_size_t
*volatile_compressed_size_p
,
965 mach_vm_size_t
*volatile_pmap_size_p
,
966 mach_vm_size_t
*volatile_compressed_pmap_size_p
);
968 extern kern_return_t
vm_map_submap(
970 vm_map_offset_t start
,
973 vm_map_offset_t offset
,
976 extern void vm_map_submap_pmap_clean(
978 vm_map_offset_t start
,
981 vm_map_offset_t offset
);
983 /* Convert from a map entry port to a map */
984 extern vm_map_t
convert_port_entry_to_map(
987 /* Convert from a port to a vm_object */
988 extern vm_object_t
convert_port_entry_to_object(
992 extern kern_return_t
vm_map_set_cache_attr(
997 /* definitions related to overriding the NX behavior */
999 #define VM_ABI_32 0x1
1000 #define VM_ABI_64 0x2
1002 extern int override_nx(vm_map_t map
, uint32_t user_tag
);
1005 /* kext exported versions */
1007 extern kern_return_t
vm_map_wire_external(
1009 vm_map_offset_t start
,
1010 vm_map_offset_t end
,
1011 vm_prot_t caller_prot
,
1012 boolean_t user_wire
);
1014 extern kern_return_t
vm_map_wire_and_extract_external(
1016 vm_map_offset_t start
,
1017 vm_prot_t caller_prot
,
1018 boolean_t user_wire
,
1019 ppnum_t
*physpage_p
);
1021 #endif /* MACH_KERNEL_PRIVATE */
1025 /* Create an empty map */
1026 extern vm_map_t
vm_map_create(
1028 vm_map_offset_t min_off
,
1029 vm_map_offset_t max_off
,
1030 boolean_t pageable
);
1032 extern void vm_map_disable_hole_optimization(vm_map_t map
);
1034 /* Get rid of a map */
1035 extern void vm_map_destroy(
1039 /* Lose a reference */
1040 extern void vm_map_deallocate(
1043 extern vm_map_t
vm_map_switch(
1046 /* Change protection */
1047 extern kern_return_t
vm_map_protect(
1049 vm_map_offset_t start
,
1050 vm_map_offset_t end
,
1054 /* Check protection */
1055 extern boolean_t
vm_map_check_protection(
1057 vm_map_offset_t start
,
1058 vm_map_offset_t end
,
1059 vm_prot_t protection
);
1061 /* wire down a region */
1062 extern kern_return_t
vm_map_wire(
1064 vm_map_offset_t start
,
1065 vm_map_offset_t end
,
1066 vm_prot_t access_type
,
1067 boolean_t user_wire
);
1069 extern kern_return_t
vm_map_wire_and_extract(
1071 vm_map_offset_t start
,
1072 vm_prot_t access_type
,
1073 boolean_t user_wire
,
1074 ppnum_t
*physpage_p
);
1076 /* unwire a region */
1077 extern kern_return_t
vm_map_unwire(
1079 vm_map_offset_t start
,
1080 vm_map_offset_t end
,
1081 boolean_t user_wire
);
1083 /* Enter a mapping of a memory object */
1084 extern kern_return_t
vm_map_enter_mem_object(
1086 vm_map_offset_t
*address
,
1088 vm_map_offset_t mask
,
1091 vm_object_offset_t offset
,
1092 boolean_t needs_copy
,
1093 vm_prot_t cur_protection
,
1094 vm_prot_t max_protection
,
1095 vm_inherit_t inheritance
);
1097 /* Enter a mapping of a memory object */
1098 extern kern_return_t
vm_map_enter_mem_object_prefault(
1100 vm_map_offset_t
*address
,
1102 vm_map_offset_t mask
,
1105 vm_object_offset_t offset
,
1106 vm_prot_t cur_protection
,
1107 vm_prot_t max_protection
,
1108 upl_page_list_ptr_t page_list
,
1109 unsigned int page_list_count
);
1111 /* Enter a mapping of a memory object */
1112 extern kern_return_t
vm_map_enter_mem_object_control(
1114 vm_map_offset_t
*address
,
1116 vm_map_offset_t mask
,
1118 memory_object_control_t control
,
1119 vm_object_offset_t offset
,
1120 boolean_t needs_copy
,
1121 vm_prot_t cur_protection
,
1122 vm_prot_t max_protection
,
1123 vm_inherit_t inheritance
);
1125 /* Deallocate a region */
1126 extern kern_return_t
vm_map_remove(
1128 vm_map_offset_t start
,
1129 vm_map_offset_t end
,
1132 /* Deallocate a region when the map is already locked */
1133 extern kern_return_t
vm_map_remove_locked(
1135 vm_map_offset_t start
,
1136 vm_map_offset_t end
,
1139 /* Discard a copy without using it */
1140 extern void vm_map_copy_discard(
1141 vm_map_copy_t copy
);
1143 /* Overwrite existing memory with a copy */
1144 extern kern_return_t
vm_map_copy_overwrite(
1146 vm_map_address_t dst_addr
,
1148 boolean_t interruptible
);
1150 /* returns TRUE if size of vm_map_copy == size parameter FALSE otherwise */
1151 extern boolean_t
vm_map_copy_validate_size(
1154 vm_map_size_t
*size
);
1156 /* Place a copy into a map */
1157 extern kern_return_t
vm_map_copyout(
1159 vm_map_address_t
*dst_addr
, /* OUT */
1160 vm_map_copy_t copy
);
1162 extern kern_return_t
vm_map_copyout_size(
1164 vm_map_address_t
*dst_addr
, /* OUT */
1166 vm_map_size_t copy_size
);
1168 extern kern_return_t
vm_map_copyout_internal(
1170 vm_map_address_t
*dst_addr
, /* OUT */
1172 vm_map_size_t copy_size
,
1173 boolean_t consume_on_success
,
1174 vm_prot_t cur_protection
,
1175 vm_prot_t max_protection
,
1176 vm_inherit_t inheritance
);
1178 extern kern_return_t
vm_map_copyin(
1180 vm_map_address_t src_addr
,
1182 boolean_t src_destroy
,
1183 vm_map_copy_t
*copy_result
); /* OUT */
1185 extern kern_return_t
vm_map_copyin_common(
1187 vm_map_address_t src_addr
,
1189 boolean_t src_destroy
,
1190 boolean_t src_volatile
,
1191 vm_map_copy_t
*copy_result
, /* OUT */
1192 boolean_t use_maxprot
);
1194 #define VM_MAP_COPYIN_SRC_DESTROY 0x00000001
1195 #define VM_MAP_COPYIN_USE_MAXPROT 0x00000002
1196 #define VM_MAP_COPYIN_ENTRY_LIST 0x00000004
1197 #define VM_MAP_COPYIN_PRESERVE_PURGEABLE 0x00000008
1198 #define VM_MAP_COPYIN_ALL_FLAGS 0x0000000F
1199 extern kern_return_t
vm_map_copyin_internal(
1201 vm_map_address_t src_addr
,
1204 vm_map_copy_t
*copy_result
); /* OUT */
1206 extern kern_return_t
vm_map_copy_extract(
1208 vm_map_address_t src_addr
,
1210 vm_map_copy_t
*copy_result
, /* OUT */
1211 vm_prot_t
*cur_prot
, /* OUT */
1212 vm_prot_t
*max_prot
);
1215 extern void vm_map_disable_NX(
1218 extern void vm_map_disallow_data_exec(
1221 extern void vm_map_set_64bit(
1224 extern void vm_map_set_32bit(
1227 extern boolean_t
vm_map_has_hard_pagezero(
1229 vm_map_offset_t pagezero_size
);
1230 extern void vm_commit_pagezero_status(vm_map_t tmap
);
1232 extern boolean_t
vm_map_is_64bit(
1236 extern kern_return_t
vm_map_raise_max_offset(
1238 vm_map_offset_t new_max_offset
);
1240 extern kern_return_t
vm_map_raise_min_offset(
1242 vm_map_offset_t new_min_offset
);
1244 extern vm_map_offset_t
vm_compute_max_offset(
1247 extern uint64_t vm_map_get_max_aslr_slide_pages(
1250 extern void vm_map_set_user_wire_limit(
1254 extern void vm_map_switch_protect(
1258 extern void vm_map_iokit_mapped_region(
1262 extern void vm_map_iokit_unmapped_region(
1267 extern boolean_t
first_free_is_valid(vm_map_t
);
1269 extern int vm_map_page_shift(
1272 extern vm_map_offset_t
vm_map_page_mask(
1275 extern int vm_map_page_size(
1278 extern vm_map_offset_t
vm_map_round_page_mask(
1279 vm_map_offset_t offset
,
1280 vm_map_offset_t mask
);
1282 extern vm_map_offset_t
vm_map_trunc_page_mask(
1283 vm_map_offset_t offset
,
1284 vm_map_offset_t mask
);
1286 extern boolean_t
vm_map_page_aligned(
1287 vm_map_offset_t offset
,
1288 vm_map_offset_t mask
);
1290 #ifdef XNU_KERNEL_PRIVATE
1291 extern kern_return_t
vm_map_page_info(
1293 vm_map_offset_t offset
,
1294 vm_page_info_flavor_t flavor
,
1295 vm_page_info_t info
,
1296 mach_msg_type_number_t
*count
);
1297 #endif /* XNU_KERNEL_PRIVATE */
1300 #ifdef MACH_KERNEL_PRIVATE
1303 * Macros to invoke vm_map_copyin_common. vm_map_copyin is the
1304 * usual form; it handles a copyin based on the current protection
1305 * (current protection == VM_PROT_NONE) is a failure.
1306 * vm_map_copyin_maxprot handles a copyin based on maximum possible
1307 * access. The difference is that a region with no current access
1308 * BUT possible maximum access is rejected by vm_map_copyin(), but
1309 * returned by vm_map_copyin_maxprot.
1311 #define vm_map_copyin(src_map, src_addr, len, src_destroy, copy_result) \
1312 vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
1313 FALSE, copy_result, FALSE)
1315 #define vm_map_copyin_maxprot(src_map, \
1316 src_addr, len, src_destroy, copy_result) \
1317 vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
1318 FALSE, copy_result, TRUE)
1322 * Internal macros for rounding and truncation of vm_map offsets and sizes
1324 #define VM_MAP_ROUND_PAGE(x,pgmask) (((vm_map_offset_t)(x) + (pgmask)) & ~((signed)(pgmask)))
1325 #define VM_MAP_TRUNC_PAGE(x,pgmask) ((vm_map_offset_t)(x) & ~((signed)(pgmask)))
1328 * Macros for rounding and truncation of vm_map offsets and sizes
1330 #define VM_MAP_PAGE_SHIFT(map) ((map) ? (map)->hdr.page_shift : PAGE_SHIFT)
1331 #define VM_MAP_PAGE_SIZE(map) (1 << VM_MAP_PAGE_SHIFT((map)))
1332 #define VM_MAP_PAGE_MASK(map) (VM_MAP_PAGE_SIZE((map)) - 1)
1333 #define VM_MAP_PAGE_ALIGNED(x,pgmask) (((x) & (pgmask)) == 0)
1335 #endif /* MACH_KERNEL_PRIVATE */
1337 #ifdef XNU_KERNEL_PRIVATE
1338 extern kern_return_t
vm_map_set_page_shift(vm_map_t map
, int pageshift
);
1339 #endif /* XNU_KERNEL_PRIVATE */
1341 #define vm_map_round_page(x,pgmask) (((vm_map_offset_t)(x) + (pgmask)) & ~((signed)(pgmask)))
1342 #define vm_map_trunc_page(x,pgmask) ((vm_map_offset_t)(x) & ~((signed)(pgmask)))
1345 * Flags for vm_map_remove() and vm_map_delete()
1347 #define VM_MAP_NO_FLAGS 0x0
1348 #define VM_MAP_REMOVE_KUNWIRE 0x1
1349 #define VM_MAP_REMOVE_INTERRUPTIBLE 0x2
1350 #define VM_MAP_REMOVE_WAIT_FOR_KWIRE 0x4
1351 #define VM_MAP_REMOVE_SAVE_ENTRIES 0x8
1352 #define VM_MAP_REMOVE_NO_PMAP_CLEANUP 0x10
1353 #define VM_MAP_REMOVE_NO_MAP_ALIGN 0x20
1354 #define VM_MAP_REMOVE_NO_UNNESTING 0x40
1356 /* Support for UPLs from vm_maps */
1358 extern kern_return_t
vm_map_get_upl(
1359 vm_map_t target_map
,
1360 vm_map_offset_t map_offset
,
1363 upl_page_info_array_t page_info
,
1364 unsigned int *page_infoCnt
,
1365 upl_control_flags_t
*flags
,
1366 int force_data_sync
);
1369 vm_map_sizes(vm_map_t map
,
1370 vm_map_size_t
* psize
,
1371 vm_map_size_t
* pfree
,
1372 vm_map_size_t
* plargest_free
);
1374 #if CONFIG_DYNAMIC_CODE_SIGNING
1375 extern kern_return_t
vm_map_sign(vm_map_t map
,
1376 vm_map_offset_t start
,
1377 vm_map_offset_t end
);
1380 extern kern_return_t
vm_map_partial_reap(
1382 unsigned int *reclaimed_resident
,
1383 unsigned int *reclaimed_compressed
);
1386 #if DEVELOPMENT || DEBUG
1388 extern int vm_map_disconnect_page_mappings(
1396 extern kern_return_t
vm_map_freeze(
1398 unsigned int *purgeable_count
,
1399 unsigned int *wired_count
,
1400 unsigned int *clean_count
,
1401 unsigned int *dirty_count
,
1402 unsigned int dirty_budget
,
1403 boolean_t
*has_shared
);
1408 #endif /* KERNEL_PRIVATE */
1410 #endif /* _VM_VM_MAP_H_ */