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(
101 #ifdef MACH_KERNEL_PRIVATE
103 #include <task_swapper.h>
104 #include <mach_assert.h>
106 #include <vm/vm_object.h>
107 #include <vm/vm_page.h>
108 #include <kern/locks.h>
109 #include <kern/zalloc.h>
110 #include <kern/macro_help.h>
112 #include <kern/thread.h>
114 #define current_map_fast() (current_thread()->map)
115 #define current_map() (current_map_fast())
117 #include <vm/vm_map_store.h>
123 * vm_map_t the high-level address map data structure.
124 * vm_map_entry_t an entry in an address map.
125 * vm_map_version_t a timestamp of a map, for use with vm_map_lookup
126 * vm_map_copy_t represents memory copied from an address map,
127 * used for inter-map copy operations
129 typedef struct vm_map_entry
*vm_map_entry_t
;
130 #define VM_MAP_ENTRY_NULL ((vm_map_entry_t) 0)
134 * Type: vm_map_object_t [internal use only]
137 * The target of an address mapping, either a virtual
138 * memory object or a sub map (of the kernel map).
140 typedef union vm_map_object
{
141 vm_object_t vmo_object
; /* object object */
142 vm_map_t vmo_submap
; /* belongs to another map */
145 #define named_entry_lock_init(object) lck_mtx_init(&(object)->Lock, &vm_object_lck_grp, &vm_object_lck_attr)
146 #define named_entry_lock_destroy(object) lck_mtx_destroy(&(object)->Lock, &vm_object_lck_grp)
147 #define named_entry_lock(object) lck_mtx_lock(&(object)->Lock)
148 #define named_entry_unlock(object) lck_mtx_unlock(&(object)->Lock)
151 * Type: vm_named_entry_t [internal use only]
154 * Description of a mapping to a memory cache object.
157 * While the handle to this object is used as a means to map
158 * and pass around the right to map regions backed by pagers
159 * of all sorts, the named_entry itself is only manipulated
160 * by the kernel. Named entries hold information on the
161 * right to map a region of a cached object. Namely,
162 * the target cache object, the beginning and ending of the
163 * region to be mapped, and the permissions, (read, write)
164 * with which it can be mapped.
168 struct vm_named_entry
{
169 decl_lck_mtx_data(, Lock
) /* Synchronization */
171 vm_object_t object
; /* object I point to */
172 memory_object_t pager
; /* amo pager port */
173 vm_map_t map
; /* map backing submap */
174 vm_map_copy_t copy
; /* a VM map copy */
176 vm_object_offset_t offset
; /* offset into object */
177 vm_object_size_t size
; /* size of region */
178 vm_object_offset_t data_offset
; /* offset to first byte of data */
179 vm_prot_t protection
; /* access permissions */
180 int ref_count
; /* Number of references */
181 unsigned int /* Is backing.xxx : */
182 /* boolean_t */ internal
:1, /* ... an internal object */
183 /* boolean_t */ is_sub_map
:1, /* ... a submap? */
184 /* boolean_t */ is_pager
:1, /* ... a pager port */
185 /* boolean_t */ is_copy
:1; /* ... a VM map copy */
189 * Type: vm_map_entry_t [internal use only]
192 * A single mapping within an address map.
195 * Address map entries consist of start and end addresses,
196 * a VM object (or sub map) and offset into that object,
197 * and user-exported inheritance and protection information.
198 * Control information for virtual copy operations is also
199 * stored in the address map entry.
202 struct vm_map_links
{
203 struct vm_map_entry
*prev
; /* previous entry */
204 struct vm_map_entry
*next
; /* next entry */
205 vm_map_offset_t start
; /* start address */
206 vm_map_offset_t end
; /* end address */
211 * The "alias" field can be updated while holding the VM map lock
212 * "shared". It's OK as along as it's the only field that can be
213 * updated without the VM map "exclusive" lock.
215 #define VME_OBJECT(entry) ((entry)->vme_object.vmo_object)
216 #define VME_OBJECT_SET(entry, object) \
218 (entry)->vme_object.vmo_object = (object); \
220 #define VME_SUBMAP(entry) ((entry)->vme_object.vmo_submap)
221 #define VME_SUBMAP_SET(entry, submap) \
223 (entry)->vme_object.vmo_submap = (submap); \
225 #define VME_OFFSET(entry) ((entry)->vme_offset & ~PAGE_MASK)
226 #define VME_OFFSET_SET(entry, offset) \
229 __alias = VME_ALIAS((entry)); \
230 assert((offset & PAGE_MASK) == 0); \
231 (entry)->vme_offset = offset | __alias; \
233 #define VME_OBJECT_SHADOW(entry, length) \
235 vm_object_t __object; \
236 vm_object_offset_t __offset; \
237 __object = VME_OBJECT((entry)); \
238 __offset = VME_OFFSET((entry)); \
239 vm_object_shadow(&__object, &__offset, (length)); \
240 if (__object != VME_OBJECT((entry))) { \
241 VME_OBJECT_SET((entry), __object); \
243 if (__offset != VME_OFFSET((entry))) { \
244 VME_OFFSET_SET((entry), __offset); \
248 #define VME_ALIAS_MASK (PAGE_MASK)
249 #define VME_ALIAS(entry) ((unsigned int)((entry)->vme_offset & VME_ALIAS_MASK))
250 #define VME_ALIAS_SET(entry, alias) \
252 vm_map_offset_t __offset; \
253 __offset = VME_OFFSET((entry)); \
254 (entry)->vme_offset = __offset | ((alias) & VME_ALIAS_MASK); \
257 struct vm_map_entry
{
258 struct vm_map_links links
; /* links to other entries */
259 #define vme_prev links.prev
260 #define vme_next links.next
261 #define vme_start links.start
262 #define vme_end links.end
264 struct vm_map_store store
;
265 union vm_map_object vme_object
; /* object I point to */
266 vm_object_offset_t vme_offset
; /* offset into object */
269 /* boolean_t */ is_shared
:1, /* region is shared */
270 /* boolean_t */ is_sub_map
:1, /* Is "object" a submap? */
271 /* boolean_t */ in_transition
:1, /* Entry being changed */
272 /* boolean_t */ needs_wakeup
:1, /* Waiters on in_transition */
273 /* vm_behavior_t */ behavior
:2, /* user paging behavior hint */
274 /* behavior is not defined for submap type */
275 /* boolean_t */ needs_copy
:1, /* object need to be copied? */
277 /* Only in task maps: */
278 /* vm_prot_t */ protection
:3, /* protection code */
279 /* vm_prot_t */ max_protection
:3, /* maximum protection */
280 /* vm_inherit_t */ inheritance
:2, /* inheritance */
281 /* boolean_t */ use_pmap
:1, /*
282 * use_pmap is overloaded:
285 * else (i.e. if object):
286 * use pmap accounting
289 /* boolean_t */ no_cache
:1, /* should new pages be cached? */
290 /* boolean_t */ permanent
:1, /* mapping can not be removed */
291 /* boolean_t */ superpage_size
:1, /* use superpages of a certain size */
292 /* boolean_t */ map_aligned
:1, /* align to map's page size */
293 /* boolean_t */ zero_wired_pages
:1, /* zero out the wired pages of
294 * this entry it is being deleted
295 * without unwiring them */
296 /* boolean_t */ used_for_jit
:1,
297 /* boolean_t */ from_reserved_zone
:1, /* Allocated from
298 * kernel reserved zone */
300 /* iokit accounting: use the virtual size rather than resident size: */
301 /* boolean_t */ iokit_acct
:1,
302 /* boolean_t */ vme_resilient_codesign
:1,
303 /* boolean_t */ vme_resilient_media
:1,
307 unsigned short wired_count
; /* can be paged if = 0 */
308 unsigned short user_wired_count
; /* for vm_wire */
310 #define MAP_ENTRY_CREATION_DEBUG (1)
311 #define MAP_ENTRY_INSERTION_DEBUG (1)
313 #if MAP_ENTRY_CREATION_DEBUG
314 struct vm_map_header
*vme_creation_maphdr
;
315 uintptr_t vme_creation_bt
[16];
317 #if MAP_ENTRY_INSERTION_DEBUG
318 uintptr_t vme_insertion_bt
[16];
323 * Convenience macros for dealing with superpages
324 * SUPERPAGE_NBASEPAGES is architecture dependent and defined in pmap.h
326 #define SUPERPAGE_SIZE (PAGE_SIZE*SUPERPAGE_NBASEPAGES)
327 #define SUPERPAGE_MASK (-SUPERPAGE_SIZE)
328 #define SUPERPAGE_ROUND_DOWN(a) (a & SUPERPAGE_MASK)
329 #define SUPERPAGE_ROUND_UP(a) ((a + SUPERPAGE_SIZE-1) & SUPERPAGE_MASK)
332 * wired_counts are unsigned short. This value is used to safeguard
333 * against any mishaps due to runaway user programs.
335 #define MAX_WIRE_COUNT 65535
340 * Type: struct vm_map_header
343 * Header for a vm_map and a vm_map_copy.
347 struct vm_map_header
{
348 struct vm_map_links links
; /* first, last, min, max */
349 int nentries
; /* Number of entries */
350 boolean_t entries_pageable
;
351 /* are map entries pageable? */
352 vm_map_offset_t highest_entry_end_addr
; /* The ending address of the highest allocated vm_entry_t */
353 #ifdef VM_MAP_STORE_USE_RB
354 struct rb_head rb_head_store
;
356 int page_shift
; /* page shift */
359 #define VM_MAP_HDR_PAGE_SHIFT(hdr) ((hdr)->page_shift)
360 #define VM_MAP_HDR_PAGE_SIZE(hdr) (1 << VM_MAP_HDR_PAGE_SHIFT((hdr)))
361 #define VM_MAP_HDR_PAGE_MASK(hdr) (VM_MAP_HDR_PAGE_SIZE((hdr)) - 1)
364 * Type: vm_map_t [exported; contents invisible]
367 * An address map -- a directory relating valid
368 * regions of a task's address space to the corresponding
369 * virtual memory objects.
372 * Maps are doubly-linked lists of map entries, sorted
373 * by address. One hint is used to start
374 * searches again from the last successful search,
375 * insertion, or removal. Another hint is used to
376 * quickly find free space.
379 lck_rw_t lock
; /* map lock */
380 struct vm_map_header hdr
; /* Map entry header */
381 #define min_offset hdr.links.start /* start of range */
382 #define max_offset hdr.links.end /* end of range */
383 #define highest_entry_end hdr.highest_entry_end_addr
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 */
388 int ref_count
; /* Reference count */
390 int res_count
; /* Residence count (swap) */
391 int sw_state
; /* Swap state */
392 #endif /* TASK_SWAPPER */
393 decl_lck_mtx_data(, s_lock
) /* Lock ref, res fields */
394 lck_mtx_ext_t s_lock_ext
;
395 vm_map_entry_t hint
; /* hint for quick lookups */
396 struct vm_map_links
* hole_hint
; /* hint for quick hole lookups */
398 vm_map_entry_t _first_free
; /* First free space hint */
399 struct vm_map_links
* _holes
; /* links all holes between entries */
400 }f_s
; /* Union for free space data structures being used */
402 #define first_free f_s._first_free
403 #define holes_list f_s._holes
406 /* boolean_t */ wait_for_space
:1, /* Should callers wait for space? */
407 /* boolean_t */ wiring_required
:1, /* All memory wired? */
408 /* boolean_t */ no_zero_fill
:1, /*No zero fill absent pages */
409 /* boolean_t */ mapped_in_other_pmaps
:1, /*has this submap been mapped in maps that use a different pmap */
410 /* boolean_t */ switch_protect
:1, /* Protect map from write faults while switched */
411 /* boolean_t */ disable_vmentry_reuse
:1, /* All vm entries should keep using newer and higher addresses in the map */
412 /* boolean_t */ map_disallow_data_exec
:1, /* Disallow execution from data pages on exec-permissive architectures */
413 /* boolean_t */ holelistenabled
:1,
414 /* reserved */ pad
:24;
415 unsigned int timestamp
; /* Version number */
416 unsigned int color_rr
; /* next color (not protected by a lock) */
418 void *default_freezer_handle
;
420 boolean_t jit_entry_exists
;
423 #define vm_map_to_entry(map) ((struct vm_map_entry *) &(map)->hdr.links)
424 #define vm_map_first_entry(map) ((map)->hdr.links.next)
425 #define vm_map_last_entry(map) ((map)->hdr.links.prev)
429 * VM map swap states. There are no transition states.
431 #define MAP_SW_IN 1 /* map is swapped in; residence count > 0 */
432 #define MAP_SW_OUT 2 /* map is out (res_count == 0 */
433 #endif /* TASK_SWAPPER */
436 * Type: vm_map_version_t [exported; contents invisible]
439 * Map versions may be used to quickly validate a previous
443 * Because they are bulky objects, map versions are usually
444 * passed by reference.
447 * Just a timestamp for the main map.
449 typedef struct vm_map_version
{
450 unsigned int main_timestamp
;
454 * Type: vm_map_copy_t [exported; contents invisible]
457 * A map copy object represents a region of virtual memory
458 * that has been copied from an address map but is still
461 * A map copy object may only be used by a single thread
465 * There are three formats for map copy objects.
466 * The first is very similar to the main
467 * address map in structure, and as a result, some
468 * of the internal maintenance functions/macros can
469 * be used with either address maps or map copy objects.
471 * The map copy object contains a header links
472 * entry onto which the other entries that represent
473 * the region are chained.
475 * The second format is a single vm object. This was used
476 * primarily in the pageout path - but is not currently used
477 * except for placeholder copy objects (see vm_map_copy_copy()).
479 * The third format is a kernel buffer copy object - for data
480 * small enough that physical copies were the most efficient
481 * method. This method uses a zero-sized array unioned with
482 * other format-specific data in the 'c_u' member. This unsized
483 * array overlaps the other elements and allows us to use this
484 * extra structure space for physical memory copies. On 64-bit
485 * systems this saves ~64 bytes per vm_map_copy.
490 #define VM_MAP_COPY_ENTRY_LIST 1
491 #define VM_MAP_COPY_OBJECT 2
492 #define VM_MAP_COPY_KERNEL_BUFFER 3
493 vm_object_offset_t offset
;
496 struct vm_map_header hdr
; /* ENTRY_LIST */
497 vm_object_t object
; /* OBJECT */
498 uint8_t kdata
[0]; /* KERNEL_BUFFER */
503 #define cpy_hdr c_u.hdr
505 #define cpy_object c_u.object
506 #define cpy_kdata c_u.kdata
507 #define cpy_kdata_hdr_sz (offsetof(struct vm_map_copy, c_u.kdata))
509 #define VM_MAP_COPY_PAGE_SHIFT(copy) ((copy)->cpy_hdr.page_shift)
510 #define VM_MAP_COPY_PAGE_SIZE(copy) (1 << VM_MAP_COPY_PAGE_SHIFT((copy)))
511 #define VM_MAP_COPY_PAGE_MASK(copy) (VM_MAP_COPY_PAGE_SIZE((copy)) - 1)
514 * Useful macros for entry list copy objects
517 #define vm_map_copy_to_entry(copy) \
518 ((struct vm_map_entry *) &(copy)->cpy_hdr.links)
519 #define vm_map_copy_first_entry(copy) \
520 ((copy)->cpy_hdr.links.next)
521 #define vm_map_copy_last_entry(copy) \
522 ((copy)->cpy_hdr.links.prev)
525 * Macros: vm_map_lock, etc. [internal use only]
527 * Perform locking on the data portion of a map.
528 * When multiple maps are to be locked, order by map address.
529 * (See vm_map.c::vm_remap())
532 #define vm_map_lock_init(map) \
533 ((map)->timestamp = 0 , \
534 lck_rw_init(&(map)->lock, &vm_map_lck_grp, &vm_map_lck_rw_attr))
536 #define vm_map_lock(map) lck_rw_lock_exclusive(&(map)->lock)
537 #define vm_map_unlock(map) \
538 ((map)->timestamp++ , lck_rw_done(&(map)->lock))
539 #define vm_map_lock_read(map) lck_rw_lock_shared(&(map)->lock)
540 #define vm_map_unlock_read(map) lck_rw_done(&(map)->lock)
541 #define vm_map_lock_write_to_read(map) \
542 ((map)->timestamp++ , lck_rw_lock_exclusive_to_shared(&(map)->lock))
543 /* lock_read_to_write() returns FALSE on failure. Macro evaluates to
544 * zero on success and non-zero value on failure.
546 #define vm_map_lock_read_to_write(map) (lck_rw_lock_shared_to_exclusive(&(map)->lock) != TRUE)
548 #if MACH_ASSERT || DEBUG
549 #define vm_map_lock_assert_held(map) \
550 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_HELD)
551 #define vm_map_lock_assert_shared(map) \
552 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_SHARED)
553 #define vm_map_lock_assert_exclusive(map) \
554 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_EXCLUSIVE)
555 #define vm_map_lock_assert_notheld(map) \
556 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_NOTHELD)
557 #else /* MACH_ASSERT || DEBUG */
558 #define vm_map_lock_assert_held(map)
559 #define vm_map_lock_assert_shared(map)
560 #define vm_map_lock_assert_exclusive(map)
561 #define vm_map_lock_assert_notheld(map)
562 #endif /* MACH_ASSERT || DEBUG */
565 * Exported procedures that operate on vm_map_t.
568 /* Initialize the module */
569 extern void vm_map_init(void);
571 extern void vm_kernel_reserved_entry_init(void);
573 /* Allocate a range in the specified virtual address map and
574 * return the entry allocated for that range. */
575 extern kern_return_t
vm_map_find_space(
577 vm_map_address_t
*address
, /* OUT */
579 vm_map_offset_t mask
,
581 vm_map_entry_t
*o_entry
); /* OUT */
583 extern void vm_map_clip_start(
585 vm_map_entry_t entry
,
586 vm_map_offset_t endaddr
);
587 extern void vm_map_clip_end(
589 vm_map_entry_t entry
,
590 vm_map_offset_t endaddr
);
591 extern boolean_t
vm_map_entry_should_cow_for_true_share(
592 vm_map_entry_t entry
);
594 /* Lookup map entry containing or the specified address in the given map */
595 extern boolean_t
vm_map_lookup_entry(
597 vm_map_address_t address
,
598 vm_map_entry_t
*entry
); /* OUT */
600 extern void vm_map_copy_remap(
602 vm_map_entry_t where
,
604 vm_map_offset_t adjustment
,
607 vm_inherit_t inheritance
);
609 /* Find the VM object, offset, and protection for a given virtual address
610 * in the specified map, assuming a page fault of the type specified. */
611 extern kern_return_t
vm_map_lookup_locked(
612 vm_map_t
*var_map
, /* IN/OUT */
613 vm_map_address_t vaddr
,
614 vm_prot_t fault_type
,
615 int object_lock_type
,
616 vm_map_version_t
*out_version
, /* OUT */
617 vm_object_t
*object
, /* OUT */
618 vm_object_offset_t
*offset
, /* OUT */
619 vm_prot_t
*out_prot
, /* OUT */
620 boolean_t
*wired
, /* OUT */
621 vm_object_fault_info_t fault_info
, /* OUT */
622 vm_map_t
*real_map
); /* OUT */
624 /* Verifies that the map has not changed since the given version. */
625 extern boolean_t
vm_map_verify(
627 vm_map_version_t
*version
); /* REF */
629 extern vm_map_entry_t
vm_map_entry_insert(
631 vm_map_entry_t insp_entry
,
632 vm_map_offset_t start
,
635 vm_object_offset_t offset
,
636 boolean_t needs_copy
,
638 boolean_t in_transition
,
639 vm_prot_t cur_protection
,
640 vm_prot_t max_protection
,
641 vm_behavior_t behavior
,
642 vm_inherit_t inheritance
,
643 unsigned wired_count
,
646 unsigned int superpage_size
,
647 boolean_t clear_map_aligned
,
648 boolean_t is_submap
);
652 * Functions implemented as macros
654 #define vm_map_min(map) ((map)->min_offset)
655 /* Lowest valid address in
658 #define vm_map_max(map) ((map)->max_offset)
659 /* Highest valid address */
661 #define vm_map_pmap(map) ((map)->pmap)
662 /* Physical map associated
663 * with this address map */
665 #define vm_map_verify_done(map, version) vm_map_unlock_read(map)
666 /* Operation that required
667 * a verified lookup is
671 * Macros/functions for map residence counts and swapin/out of vm maps
676 /* Gain a reference to an existing map */
677 extern void vm_map_reference(
679 /* Lose a residence count */
680 extern void vm_map_res_deallocate(
682 /* Gain a residence count on a map */
683 extern void vm_map_res_reference(
685 /* Gain reference & residence counts to possibly swapped-out map */
686 extern void vm_map_reference_swap(
689 #else /* MACH_ASSERT */
691 #define vm_map_reference(map) \
693 vm_map_t Map = (map); \
695 lck_mtx_lock(&Map->s_lock); \
698 lck_mtx_unlock(&Map->s_lock); \
702 #define vm_map_res_reference(map) \
704 vm_map_t Lmap = (map); \
705 if (Lmap->res_count == 0) { \
706 lck_mtx_unlock(&Lmap->s_lock);\
708 vm_map_swapin(Lmap); \
709 lck_mtx_lock(&Lmap->s_lock); \
711 vm_map_unlock(Lmap); \
716 #define vm_map_res_deallocate(map) \
718 vm_map_t Map = (map); \
719 if (--Map->res_count == 0) { \
720 lck_mtx_unlock(&Map->s_lock); \
722 vm_map_swapout(Map); \
723 vm_map_unlock(Map); \
724 lck_mtx_lock(&Map->s_lock); \
728 #define vm_map_reference_swap(map) \
730 vm_map_t Map = (map); \
731 lck_mtx_lock(&Map->s_lock); \
733 vm_map_res_reference(Map); \
734 lck_mtx_unlock(&Map->s_lock); \
736 #endif /* MACH_ASSERT */
738 extern void vm_map_swapin(
741 extern void vm_map_swapout(
744 #else /* TASK_SWAPPER */
746 #define vm_map_reference(map) \
748 vm_map_t Map = (map); \
750 lck_mtx_lock(&Map->s_lock); \
752 lck_mtx_unlock(&Map->s_lock); \
756 #define vm_map_reference_swap(map) vm_map_reference(map)
757 #define vm_map_res_reference(map)
758 #define vm_map_res_deallocate(map)
760 #endif /* TASK_SWAPPER */
763 * Submap object. Must be used to create memory to be put
764 * in a submap by vm_map_submap.
766 extern vm_object_t vm_submap_object
;
769 * Wait and wakeup macros for in_transition map entries.
771 #define vm_map_entry_wait(map, interruptible) \
772 ((map)->timestamp++ , \
773 lck_rw_sleep(&(map)->lock, LCK_SLEEP_EXCLUSIVE|LCK_SLEEP_PROMOTED_PRI, \
774 (event_t)&(map)->hdr, interruptible))
777 #define vm_map_entry_wakeup(map) \
778 thread_wakeup((event_t)(&(map)->hdr))
781 #define vm_map_ref_fast(map) \
783 lck_mtx_lock(&map->s_lock); \
785 vm_map_res_reference(map); \
786 lck_mtx_unlock(&map->s_lock); \
789 #define vm_map_dealloc_fast(map) \
793 lck_mtx_lock(&map->s_lock); \
794 c = --map->ref_count; \
796 vm_map_res_deallocate(map); \
797 lck_mtx_unlock(&map->s_lock); \
799 vm_map_destroy(map); \
803 /* simplify map entries */
804 extern void vm_map_simplify_entry(
806 vm_map_entry_t this_entry
);
807 extern void vm_map_simplify(
809 vm_map_offset_t start
);
811 /* Move the information in a map copy object to a new map copy object */
812 extern vm_map_copy_t
vm_map_copy_copy(
815 /* Create a copy object from an object. */
816 extern kern_return_t
vm_map_copyin_object(
818 vm_object_offset_t offset
,
819 vm_object_size_t size
,
820 vm_map_copy_t
*copy_result
); /* OUT */
822 extern kern_return_t
vm_map_random_address_for_size(
824 vm_map_offset_t
*address
,
827 /* Enter a mapping */
828 extern kern_return_t
vm_map_enter(
830 vm_map_offset_t
*address
,
832 vm_map_offset_t mask
,
835 vm_object_offset_t offset
,
836 boolean_t needs_copy
,
837 vm_prot_t cur_protection
,
838 vm_prot_t max_protection
,
839 vm_inherit_t inheritance
);
842 /* XXX should go away - replaced with regular enter of contig object */
843 extern kern_return_t
vm_map_enter_cpm(
845 vm_map_address_t
*addr
,
849 extern kern_return_t
vm_map_remap(
851 vm_map_offset_t
*address
,
853 vm_map_offset_t mask
,
856 vm_map_offset_t memory_address
,
858 vm_prot_t
*cur_protection
,
859 vm_prot_t
*max_protection
,
860 vm_inherit_t inheritance
);
864 * Read and write from a kernel buffer to a specified map.
866 extern kern_return_t
vm_map_write_user(
869 vm_map_offset_t dst_addr
,
872 extern kern_return_t
vm_map_read_user(
874 vm_map_offset_t src_addr
,
878 /* Create a new task map using an existing task map as a template. */
879 extern vm_map_t
vm_map_fork(
883 /* Change inheritance */
884 extern kern_return_t
vm_map_inherit(
886 vm_map_offset_t start
,
888 vm_inherit_t new_inheritance
);
890 /* Add or remove machine-dependent attributes from map regions */
891 extern kern_return_t
vm_map_machine_attribute(
893 vm_map_offset_t start
,
895 vm_machine_attribute_t attribute
,
896 vm_machine_attribute_val_t
* value
); /* IN/OUT */
898 extern kern_return_t
vm_map_msync(
900 vm_map_address_t address
,
902 vm_sync_t sync_flags
);
904 /* Set paging behavior */
905 extern kern_return_t
vm_map_behavior_set(
907 vm_map_offset_t start
,
909 vm_behavior_t new_behavior
);
911 extern kern_return_t
vm_map_purgable_control(
913 vm_map_offset_t address
,
914 vm_purgable_t control
,
917 extern kern_return_t
vm_map_region(
919 vm_map_offset_t
*address
,
921 vm_region_flavor_t flavor
,
922 vm_region_info_t info
,
923 mach_msg_type_number_t
*count
,
924 mach_port_t
*object_name
);
926 extern kern_return_t
vm_map_region_recurse_64(
928 vm_map_offset_t
*address
,
930 natural_t
*nesting_depth
,
931 vm_region_submap_info_64_t info
,
932 mach_msg_type_number_t
*count
);
934 extern kern_return_t
vm_map_page_query_internal(
936 vm_map_offset_t offset
,
940 extern kern_return_t
vm_map_query_volatile(
942 mach_vm_size_t
*volatile_virtual_size_p
,
943 mach_vm_size_t
*volatile_resident_size_p
,
944 mach_vm_size_t
*volatile_compressed_size_p
,
945 mach_vm_size_t
*volatile_pmap_size_p
,
946 mach_vm_size_t
*volatile_compressed_pmap_size_p
);
948 extern kern_return_t
vm_map_submap(
950 vm_map_offset_t start
,
953 vm_map_offset_t offset
,
956 extern void vm_map_submap_pmap_clean(
958 vm_map_offset_t start
,
961 vm_map_offset_t offset
);
963 /* Convert from a map entry port to a map */
964 extern vm_map_t
convert_port_entry_to_map(
967 /* Convert from a port to a vm_object */
968 extern vm_object_t
convert_port_entry_to_object(
972 extern kern_return_t
vm_map_set_cache_attr(
977 /* definitions related to overriding the NX behavior */
979 #define VM_ABI_32 0x1
980 #define VM_ABI_64 0x2
982 extern int override_nx(vm_map_t map
, uint32_t user_tag
);
984 extern int vm_map_purge(vm_map_t map
);
987 /* kext exported versions */
989 extern kern_return_t
vm_map_wire_external(
990 register vm_map_t map
,
991 register vm_map_offset_t start
,
992 register vm_map_offset_t end
,
993 register vm_prot_t caller_prot
,
994 boolean_t user_wire
);
996 extern kern_return_t
vm_map_wire_and_extract_external(
998 vm_map_offset_t start
,
999 vm_prot_t caller_prot
,
1000 boolean_t user_wire
,
1001 ppnum_t
*physpage_p
);
1003 #endif /* MACH_KERNEL_PRIVATE */
1007 /* Create an empty map */
1008 extern vm_map_t
vm_map_create(
1010 vm_map_offset_t min_off
,
1011 vm_map_offset_t max_off
,
1012 boolean_t pageable
);
1014 extern void vm_map_disable_hole_optimization(vm_map_t map
);
1016 /* Get rid of a map */
1017 extern void vm_map_destroy(
1021 /* Lose a reference */
1022 extern void vm_map_deallocate(
1025 extern vm_map_t
vm_map_switch(
1028 /* Change protection */
1029 extern kern_return_t
vm_map_protect(
1031 vm_map_offset_t start
,
1032 vm_map_offset_t end
,
1036 /* Check protection */
1037 extern boolean_t
vm_map_check_protection(
1039 vm_map_offset_t start
,
1040 vm_map_offset_t end
,
1041 vm_prot_t protection
);
1043 /* wire down a region */
1044 extern kern_return_t
vm_map_wire(
1046 vm_map_offset_t start
,
1047 vm_map_offset_t end
,
1048 vm_prot_t access_type
,
1049 boolean_t user_wire
);
1051 extern kern_return_t
vm_map_wire_and_extract(
1053 vm_map_offset_t start
,
1054 vm_prot_t access_type
,
1055 boolean_t user_wire
,
1056 ppnum_t
*physpage_p
);
1058 /* unwire a region */
1059 extern kern_return_t
vm_map_unwire(
1061 vm_map_offset_t start
,
1062 vm_map_offset_t end
,
1063 boolean_t user_wire
);
1065 /* Enter a mapping of a memory object */
1066 extern kern_return_t
vm_map_enter_mem_object(
1068 vm_map_offset_t
*address
,
1070 vm_map_offset_t mask
,
1073 vm_object_offset_t offset
,
1074 boolean_t needs_copy
,
1075 vm_prot_t cur_protection
,
1076 vm_prot_t max_protection
,
1077 vm_inherit_t inheritance
);
1079 /* Enter a mapping of a memory object */
1080 extern kern_return_t
vm_map_enter_mem_object_prefault(
1082 vm_map_offset_t
*address
,
1084 vm_map_offset_t mask
,
1087 vm_object_offset_t offset
,
1088 vm_prot_t cur_protection
,
1089 vm_prot_t max_protection
,
1090 upl_page_list_ptr_t page_list
,
1091 unsigned int page_list_count
);
1093 /* Enter a mapping of a memory object */
1094 extern kern_return_t
vm_map_enter_mem_object_control(
1096 vm_map_offset_t
*address
,
1098 vm_map_offset_t mask
,
1100 memory_object_control_t control
,
1101 vm_object_offset_t offset
,
1102 boolean_t needs_copy
,
1103 vm_prot_t cur_protection
,
1104 vm_prot_t max_protection
,
1105 vm_inherit_t inheritance
);
1107 /* Deallocate a region */
1108 extern kern_return_t
vm_map_remove(
1110 vm_map_offset_t start
,
1111 vm_map_offset_t end
,
1114 /* Discard a copy without using it */
1115 extern void vm_map_copy_discard(
1116 vm_map_copy_t copy
);
1118 /* Overwrite existing memory with a copy */
1119 extern kern_return_t
vm_map_copy_overwrite(
1121 vm_map_address_t dst_addr
,
1123 boolean_t interruptible
);
1125 /* returns TRUE if size of vm_map_copy == size parameter FALSE otherwise */
1126 extern boolean_t
vm_map_copy_validate_size(
1129 vm_map_size_t size
);
1131 /* Place a copy into a map */
1132 extern kern_return_t
vm_map_copyout(
1134 vm_map_address_t
*dst_addr
, /* OUT */
1135 vm_map_copy_t copy
);
1137 extern kern_return_t
vm_map_copyout_internal(
1139 vm_map_address_t
*dst_addr
, /* OUT */
1141 boolean_t consume_on_success
,
1142 vm_prot_t cur_protection
,
1143 vm_prot_t max_protection
,
1144 vm_inherit_t inheritance
);
1146 extern kern_return_t
vm_map_copyin(
1148 vm_map_address_t src_addr
,
1150 boolean_t src_destroy
,
1151 vm_map_copy_t
*copy_result
); /* OUT */
1153 extern kern_return_t
vm_map_copyin_common(
1155 vm_map_address_t src_addr
,
1157 boolean_t src_destroy
,
1158 boolean_t src_volatile
,
1159 vm_map_copy_t
*copy_result
, /* OUT */
1160 boolean_t use_maxprot
);
1162 #define VM_MAP_COPYIN_SRC_DESTROY 0x00000001
1163 #define VM_MAP_COPYIN_USE_MAXPROT 0x00000002
1164 #define VM_MAP_COPYIN_ENTRY_LIST 0x00000004
1165 #define VM_MAP_COPYIN_ALL_FLAGS 0x00000007
1166 extern kern_return_t
vm_map_copyin_internal(
1168 vm_map_address_t src_addr
,
1171 vm_map_copy_t
*copy_result
); /* OUT */
1173 extern kern_return_t
vm_map_copy_extract(
1175 vm_map_address_t src_addr
,
1177 vm_map_copy_t
*copy_result
, /* OUT */
1178 vm_prot_t
*cur_prot
, /* OUT */
1179 vm_prot_t
*max_prot
);
1182 extern void vm_map_disable_NX(
1185 extern void vm_map_disallow_data_exec(
1188 extern void vm_map_set_64bit(
1191 extern void vm_map_set_32bit(
1194 extern boolean_t
vm_map_has_hard_pagezero(
1196 vm_map_offset_t pagezero_size
);
1198 extern boolean_t
vm_map_is_64bit(
1202 extern kern_return_t
vm_map_raise_max_offset(
1204 vm_map_offset_t new_max_offset
);
1206 extern kern_return_t
vm_map_raise_min_offset(
1208 vm_map_offset_t new_min_offset
);
1210 extern vm_map_offset_t
vm_compute_max_offset(
1213 extern uint64_t vm_map_get_max_aslr_slide_pages(
1216 extern void vm_map_set_user_wire_limit(
1220 extern void vm_map_switch_protect(
1224 extern void vm_map_iokit_mapped_region(
1228 extern void vm_map_iokit_unmapped_region(
1233 extern boolean_t
first_free_is_valid(vm_map_t
);
1235 extern int vm_map_page_shift(
1238 extern vm_map_offset_t
vm_map_page_mask(
1241 extern int vm_map_page_size(
1244 extern vm_map_offset_t
vm_map_round_page_mask(
1245 vm_map_offset_t offset
,
1246 vm_map_offset_t mask
);
1248 extern vm_map_offset_t
vm_map_trunc_page_mask(
1249 vm_map_offset_t offset
,
1250 vm_map_offset_t mask
);
1252 extern boolean_t
vm_map_page_aligned(
1253 vm_map_offset_t offset
,
1254 vm_map_offset_t mask
);
1256 #ifdef XNU_KERNEL_PRIVATE
1257 extern kern_return_t
vm_map_page_info(
1259 vm_map_offset_t offset
,
1260 vm_page_info_flavor_t flavor
,
1261 vm_page_info_t info
,
1262 mach_msg_type_number_t
*count
);
1263 #endif /* XNU_KERNEL_PRIVATE */
1266 #ifdef MACH_KERNEL_PRIVATE
1269 * Macros to invoke vm_map_copyin_common. vm_map_copyin is the
1270 * usual form; it handles a copyin based on the current protection
1271 * (current protection == VM_PROT_NONE) is a failure.
1272 * vm_map_copyin_maxprot handles a copyin based on maximum possible
1273 * access. The difference is that a region with no current access
1274 * BUT possible maximum access is rejected by vm_map_copyin(), but
1275 * returned by vm_map_copyin_maxprot.
1277 #define vm_map_copyin(src_map, src_addr, len, src_destroy, copy_result) \
1278 vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
1279 FALSE, copy_result, FALSE)
1281 #define vm_map_copyin_maxprot(src_map, \
1282 src_addr, len, src_destroy, copy_result) \
1283 vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
1284 FALSE, copy_result, TRUE)
1288 * Internal macros for rounding and truncation of vm_map offsets and sizes
1290 #define VM_MAP_ROUND_PAGE(x,pgmask) (((vm_map_offset_t)(x) + (pgmask)) & ~((signed)(pgmask)))
1291 #define VM_MAP_TRUNC_PAGE(x,pgmask) ((vm_map_offset_t)(x) & ~((signed)(pgmask)))
1294 * Macros for rounding and truncation of vm_map offsets and sizes
1296 #define VM_MAP_PAGE_SHIFT(map) ((map) ? (map)->hdr.page_shift : PAGE_SHIFT)
1297 #define VM_MAP_PAGE_SIZE(map) (1 << VM_MAP_PAGE_SHIFT((map)))
1298 #define VM_MAP_PAGE_MASK(map) (VM_MAP_PAGE_SIZE((map)) - 1)
1299 #define VM_MAP_PAGE_ALIGNED(x,pgmask) (((x) & (pgmask)) == 0)
1301 #endif /* MACH_KERNEL_PRIVATE */
1303 #ifdef XNU_KERNEL_PRIVATE
1304 extern kern_return_t
vm_map_set_page_shift(vm_map_t map
, int pageshift
);
1305 #endif /* XNU_KERNEL_PRIVATE */
1307 #define vm_map_round_page(x,pgmask) (((vm_map_offset_t)(x) + (pgmask)) & ~((signed)(pgmask)))
1308 #define vm_map_trunc_page(x,pgmask) ((vm_map_offset_t)(x) & ~((signed)(pgmask)))
1311 * Flags for vm_map_remove() and vm_map_delete()
1313 #define VM_MAP_NO_FLAGS 0x0
1314 #define VM_MAP_REMOVE_KUNWIRE 0x1
1315 #define VM_MAP_REMOVE_INTERRUPTIBLE 0x2
1316 #define VM_MAP_REMOVE_WAIT_FOR_KWIRE 0x4
1317 #define VM_MAP_REMOVE_SAVE_ENTRIES 0x8
1318 #define VM_MAP_REMOVE_NO_PMAP_CLEANUP 0x10
1319 #define VM_MAP_REMOVE_NO_MAP_ALIGN 0x20
1320 #define VM_MAP_REMOVE_NO_UNNESTING 0x40
1322 /* Support for UPLs from vm_maps */
1324 extern kern_return_t
vm_map_get_upl(
1325 vm_map_t target_map
,
1326 vm_map_offset_t map_offset
,
1329 upl_page_info_array_t page_info
,
1330 unsigned int *page_infoCnt
,
1331 upl_control_flags_t
*flags
,
1332 int force_data_sync
);
1335 vm_map_sizes(vm_map_t map
,
1336 vm_map_size_t
* psize
,
1337 vm_map_size_t
* pfree
,
1338 vm_map_size_t
* plargest_free
);
1340 #if CONFIG_DYNAMIC_CODE_SIGNING
1341 extern kern_return_t
vm_map_sign(vm_map_t map
,
1342 vm_map_offset_t start
,
1343 vm_map_offset_t end
);
1346 extern kern_return_t
vm_map_partial_reap(
1348 unsigned int *reclaimed_resident
,
1349 unsigned int *reclaimed_compressed
);
1352 void vm_map_freeze_thaw_init(void);
1353 void vm_map_freeze_thaw(void);
1354 void vm_map_demand_fault(void);
1356 extern kern_return_t
vm_map_freeze_walk(
1358 unsigned int *purgeable_count
,
1359 unsigned int *wired_count
,
1360 unsigned int *clean_count
,
1361 unsigned int *dirty_count
,
1362 unsigned int dirty_budget
,
1363 boolean_t
*has_shared
);
1365 extern kern_return_t
vm_map_freeze(
1367 unsigned int *purgeable_count
,
1368 unsigned int *wired_count
,
1369 unsigned int *clean_count
,
1370 unsigned int *dirty_count
,
1371 unsigned int dirty_budget
,
1372 boolean_t
*has_shared
);
1374 extern kern_return_t
vm_map_thaw(
1380 #endif /* KERNEL_PRIVATE */
1382 #endif /* _VM_VM_MAP_H_ */