]> git.saurik.com Git - apple/xnu.git/blame - osfmk/vm/vm_map.h
xnu-4570.41.2.tar.gz
[apple/xnu.git] / osfmk / vm / vm_map.h
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58
59/*
60 * File: vm/vm_map.h
61 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * Date: 1985
63 *
64 * Virtual memory map module definitions.
65 *
66 * Contributors:
67 * avie, dlb, mwyoung
68 */
69
70#ifndef _VM_VM_MAP_H_
71#define _VM_VM_MAP_H_
72
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>
91447636 80#include <mach/vm_param.h>
1c79356b
A
81#include <vm/pmap.h>
82
91447636 83#ifdef KERNEL_PRIVATE
1c79356b 84
91447636 85#include <sys/cdefs.h>
1c79356b 86
91447636 87__BEGIN_DECLS
1c79356b
A
88
89extern void vm_map_reference(vm_map_t map);
90extern vm_map_t current_map(void);
91
2d21ac55
A
92/* Setup reserved areas in a new VM map */
93extern kern_return_t vm_map_exec(
94 vm_map_t new_map,
95 task_t task,
39037602 96 boolean_t is64bit,
2d21ac55
A
97 void *fsroot,
98 cpu_type_t cpu);
99
91447636
A
100__END_DECLS
101
102#ifdef MACH_KERNEL_PRIVATE
1c79356b 103
1c79356b
A
104#include <task_swapper.h>
105#include <mach_assert.h>
106
107#include <vm/vm_object.h>
108#include <vm/vm_page.h>
fe8ab488 109#include <kern/locks.h>
1c79356b
A
110#include <kern/zalloc.h>
111#include <kern/macro_help.h>
112
91447636 113#include <kern/thread.h>
1c79356b 114
91447636 115#define current_map_fast() (current_thread()->map)
1c79356b
A
116#define current_map() (current_map_fast())
117
6d2010ae
A
118#include <vm/vm_map_store.h>
119
120
1c79356b
A
121/*
122 * Types defined:
123 *
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
129 */
9bccf70c 130typedef struct vm_map_entry *vm_map_entry_t;
91447636 131#define VM_MAP_ENTRY_NULL ((vm_map_entry_t) 0)
9bccf70c 132
1c79356b
A
133
134/*
135 * Type: vm_map_object_t [internal use only]
136 *
137 * Description:
138 * The target of an address mapping, either a virtual
139 * memory object or a sub map (of the kernel map).
140 */
141typedef union vm_map_object {
3e170ce0
A
142 vm_object_t vmo_object; /* object object */
143 vm_map_t vmo_submap; /* belongs to another map */
1c79356b
A
144} vm_map_object_t;
145
b0d623f7 146#define named_entry_lock_init(object) lck_mtx_init(&(object)->Lock, &vm_object_lck_grp, &vm_object_lck_attr)
316670eb 147#define named_entry_lock_destroy(object) lck_mtx_destroy(&(object)->Lock, &vm_object_lck_grp)
b0d623f7
A
148#define named_entry_lock(object) lck_mtx_lock(&(object)->Lock)
149#define named_entry_unlock(object) lck_mtx_unlock(&(object)->Lock)
1c79356b
A
150
151/*
152 * Type: vm_named_entry_t [internal use only]
153 *
154 * Description:
155 * Description of a mapping to a memory cache object.
156 *
157 * Implementation:
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.
166 *
167 */
168
169struct vm_named_entry {
b0d623f7 170 decl_lck_mtx_data(, Lock) /* Synchronization */
1c79356b 171 union {
91447636 172 vm_object_t object; /* object I point to */
91447636 173 vm_map_t map; /* map backing submap */
39236c6e 174 vm_map_copy_t copy; /* a VM map copy */
1c79356b 175 } backing;
91447636
A
176 vm_object_offset_t offset; /* offset into object */
177 vm_object_size_t size; /* size of region */
39236c6e 178 vm_object_offset_t data_offset; /* offset to first byte of data */
91447636 179 vm_prot_t protection; /* access permissions */
1c79356b 180 int ref_count; /* Number of references */
91447636
A
181 unsigned int /* Is backing.xxx : */
182 /* boolean_t */ internal:1, /* ... an internal object */
183 /* boolean_t */ is_sub_map:1, /* ... a submap? */
39236c6e 184 /* boolean_t */ is_copy:1; /* ... a VM map copy */
1c79356b
A
185};
186
1c79356b
A
187/*
188 * Type: vm_map_entry_t [internal use only]
189 *
190 * Description:
191 * A single mapping within an address map.
192 *
193 * Implementation:
194 * Address map entries consist of start and end addresses,
195 * a VM object (or sub map) and offset into that object,
196 * and user-exported inheritance and protection information.
197 * Control information for virtual copy operations is also
198 * stored in the address map entry.
199 */
6d2010ae 200
1c79356b
A
201struct vm_map_links {
202 struct vm_map_entry *prev; /* previous entry */
203 struct vm_map_entry *next; /* next entry */
91447636
A
204 vm_map_offset_t start; /* start address */
205 vm_map_offset_t end; /* end address */
1c79356b
A
206};
207
3e170ce0
A
208/*
209 * IMPORTANT:
210 * The "alias" field can be updated while holding the VM map lock
211 * "shared". It's OK as along as it's the only field that can be
212 * updated without the VM map "exclusive" lock.
213 */
214#define VME_OBJECT(entry) ((entry)->vme_object.vmo_object)
215#define VME_OBJECT_SET(entry, object) \
216 MACRO_BEGIN \
217 (entry)->vme_object.vmo_object = (object); \
218 MACRO_END
219#define VME_SUBMAP(entry) ((entry)->vme_object.vmo_submap)
220#define VME_SUBMAP_SET(entry, submap) \
221 MACRO_BEGIN \
222 (entry)->vme_object.vmo_submap = (submap); \
223 MACRO_END
224#define VME_OFFSET(entry) ((entry)->vme_offset & ~PAGE_MASK)
225#define VME_OFFSET_SET(entry, offset) \
226 MACRO_BEGIN \
227 int __alias; \
228 __alias = VME_ALIAS((entry)); \
229 assert((offset & PAGE_MASK) == 0); \
230 (entry)->vme_offset = offset | __alias; \
231 MACRO_END
232#define VME_OBJECT_SHADOW(entry, length) \
233 MACRO_BEGIN \
234 vm_object_t __object; \
235 vm_object_offset_t __offset; \
236 __object = VME_OBJECT((entry)); \
237 __offset = VME_OFFSET((entry)); \
238 vm_object_shadow(&__object, &__offset, (length)); \
239 if (__object != VME_OBJECT((entry))) { \
240 VME_OBJECT_SET((entry), __object); \
241 } \
242 if (__offset != VME_OFFSET((entry))) { \
243 VME_OFFSET_SET((entry), __offset); \
244 } \
245 MACRO_END
246
247#define VME_ALIAS_MASK (PAGE_MASK)
248#define VME_ALIAS(entry) ((unsigned int)((entry)->vme_offset & VME_ALIAS_MASK))
249#define VME_ALIAS_SET(entry, alias) \
250 MACRO_BEGIN \
251 vm_map_offset_t __offset; \
252 __offset = VME_OFFSET((entry)); \
253 (entry)->vme_offset = __offset | ((alias) & VME_ALIAS_MASK); \
254 MACRO_END
255
1c79356b
A
256struct vm_map_entry {
257 struct vm_map_links links; /* links to other entries */
258#define vme_prev links.prev
259#define vme_next links.next
260#define vme_start links.start
261#define vme_end links.end
6d2010ae
A
262
263 struct vm_map_store store;
3e170ce0
A
264 union vm_map_object vme_object; /* object I point to */
265 vm_object_offset_t vme_offset; /* offset into object */
266
1c79356b 267 unsigned int
3e170ce0
A
268 /* boolean_t */ is_shared:1, /* region is shared */
269 /* boolean_t */ is_sub_map:1, /* Is "object" a submap? */
270 /* boolean_t */ in_transition:1, /* Entry being changed */
271 /* boolean_t */ needs_wakeup:1, /* Waiters on in_transition */
272 /* vm_behavior_t */ behavior:2, /* user paging behavior hint */
1c79356b 273 /* behavior is not defined for submap type */
3e170ce0
A
274 /* boolean_t */ needs_copy:1, /* object need to be copied? */
275
1c79356b 276 /* Only in task maps: */
3e170ce0
A
277 /* vm_prot_t */ protection:3, /* protection code */
278 /* vm_prot_t */ max_protection:3, /* maximum protection */
279 /* vm_inherit_t */ inheritance:2, /* inheritance */
280 /* boolean_t */ use_pmap:1, /*
281 * use_pmap is overloaded:
282 * if "is_sub_map":
283 * use a nested pmap?
284 * else (i.e. if object):
285 * use pmap accounting
286 * for footprint?
287 */
288 /* boolean_t */ no_cache:1, /* should new pages be cached? */
289 /* boolean_t */ permanent:1, /* mapping can not be removed */
290 /* boolean_t */ superpage_size:1, /* use superpages of a certain size */
291 /* boolean_t */ map_aligned:1, /* align to map's page size */
292 /* boolean_t */ zero_wired_pages:1, /* zero out the wired pages of
293 * this entry it is being deleted
294 * without unwiring them */
295 /* boolean_t */ used_for_jit:1,
296 /* boolean_t */ from_reserved_zone:1, /* Allocated from
297 * kernel reserved zone */
fe8ab488
A
298
299 /* iokit accounting: use the virtual size rather than resident size: */
3e170ce0
A
300 /* boolean_t */ iokit_acct:1,
301 /* boolean_t */ vme_resilient_codesign:1,
302 /* boolean_t */ vme_resilient_media:1,
39037602
A
303 /* boolean_t */ vme_atomic:1, /* entry cannot be split/coalesced */
304 __unused:5;
3e170ce0 305;
fe8ab488 306
1c79356b
A
307 unsigned short wired_count; /* can be paged if = 0 */
308 unsigned short user_wired_count; /* for vm_wire */
316670eb
A
309#if DEBUG
310#define MAP_ENTRY_CREATION_DEBUG (1)
39236c6e 311#define MAP_ENTRY_INSERTION_DEBUG (1)
316670eb
A
312#endif
313#if MAP_ENTRY_CREATION_DEBUG
39236c6e
A
314 struct vm_map_header *vme_creation_maphdr;
315 uintptr_t vme_creation_bt[16];
316#endif
317#if MAP_ENTRY_INSERTION_DEBUG
318 uintptr_t vme_insertion_bt[16];
316670eb 319#endif
1c79356b
A
320};
321
b0d623f7
A
322/*
323 * Convenience macros for dealing with superpages
324 * SUPERPAGE_NBASEPAGES is architecture dependent and defined in pmap.h
325 */
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)
330
1c79356b
A
331/*
332 * wired_counts are unsigned short. This value is used to safeguard
333 * against any mishaps due to runaway user programs.
334 */
335#define MAX_WIRE_COUNT 65535
336
337
338
339/*
340 * Type: struct vm_map_header
341 *
342 * Description:
343 * Header for a vm_map and a vm_map_copy.
344 */
6d2010ae
A
345
346
1c79356b
A
347struct 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? */
6d2010ae
A
352#ifdef VM_MAP_STORE_USE_RB
353 struct rb_head rb_head_store;
354#endif
39236c6e 355 int page_shift; /* page shift */
1c79356b
A
356};
357
39236c6e
A
358#define VM_MAP_HDR_PAGE_SHIFT(hdr) ((hdr)->page_shift)
359#define VM_MAP_HDR_PAGE_SIZE(hdr) (1 << VM_MAP_HDR_PAGE_SHIFT((hdr)))
360#define VM_MAP_HDR_PAGE_MASK(hdr) (VM_MAP_HDR_PAGE_SIZE((hdr)) - 1)
361
1c79356b
A
362/*
363 * Type: vm_map_t [exported; contents invisible]
364 *
365 * Description:
366 * An address map -- a directory relating valid
367 * regions of a task's address space to the corresponding
368 * virtual memory objects.
369 *
370 * Implementation:
371 * Maps are doubly-linked lists of map entries, sorted
372 * by address. One hint is used to start
373 * searches again from the last successful search,
374 * insertion, or removal. Another hint is used to
375 * quickly find free space.
376 */
2d21ac55 377struct _vm_map {
fe8ab488 378 lck_rw_t lock; /* map lock */
1c79356b
A
379 struct vm_map_header hdr; /* Map entry header */
380#define min_offset hdr.links.start /* start of range */
381#define max_offset hdr.links.end /* end of range */
382 pmap_t pmap; /* Physical map */
91447636 383 vm_map_size_t size; /* virtual size */
2d21ac55
A
384 vm_map_size_t user_wire_limit;/* rlimit on user locked memory */
385 vm_map_size_t user_wire_size; /* current size of user locked memory in this map */
5ba3f43e
A
386#if __x86_64__
387 vm_map_offset_t vmmap_high_start;
388#endif /* __x86_64__ */
39037602
A
389
390 union {
391 /*
392 * If map->disable_vmentry_reuse == TRUE:
393 * the end address of the highest allocated vm_map_entry_t.
394 */
395 vm_map_offset_t vmu1_highest_entry_end;
396 /*
397 * For a nested VM map:
398 * the lowest address in this nested VM map that we would
399 * expect to be unnested under normal operation (i.e. for
400 * regular copy-on-write on DATA section).
401 */
402 vm_map_offset_t vmu1_lowest_unnestable_start;
403 } vmu1;
404#define highest_entry_end vmu1.vmu1_highest_entry_end
405#define lowest_unnestable_start vmu1.vmu1_lowest_unnestable_start
406
1c79356b
A
407 int ref_count; /* Reference count */
408#if TASK_SWAPPER
409 int res_count; /* Residence count (swap) */
410 int sw_state; /* Swap state */
411#endif /* TASK_SWAPPER */
b0d623f7
A
412 decl_lck_mtx_data(, s_lock) /* Lock ref, res fields */
413 lck_mtx_ext_t s_lock_ext;
1c79356b 414 vm_map_entry_t hint; /* hint for quick lookups */
3e170ce0
A
415 struct vm_map_links* hole_hint; /* hint for quick hole lookups */
416 union{
417 vm_map_entry_t _first_free; /* First free space hint */
418 struct vm_map_links* _holes; /* links all holes between entries */
419 }f_s; /* Union for free space data structures being used */
420
421#define first_free f_s._first_free
422#define holes_list f_s._holes
423
6d2010ae
A
424 unsigned int
425 /* boolean_t */ wait_for_space:1, /* Should callers wait for space? */
426 /* boolean_t */ wiring_required:1, /* All memory wired? */
427 /* boolean_t */ no_zero_fill:1, /*No zero fill absent pages */
316670eb 428 /* boolean_t */ mapped_in_other_pmaps:1, /*has this submap been mapped in maps that use a different pmap */
6d2010ae
A
429 /* boolean_t */ switch_protect:1, /* Protect map from write faults while switched */
430 /* boolean_t */ disable_vmentry_reuse:1, /* All vm entries should keep using newer and higher addresses in the map */
431 /* boolean_t */ map_disallow_data_exec:1, /* Disallow execution from data pages on exec-permissive architectures */
3e170ce0 432 /* boolean_t */ holelistenabled:1,
39037602
A
433 /* boolean_t */ is_nested_map:1,
434 /* reserved */ pad:23;
1c79356b 435 unsigned int timestamp; /* Version number */
2d21ac55 436 unsigned int color_rr; /* next color (not protected by a lock) */
39037602 437
6d2010ae 438 boolean_t jit_entry_exists;
1c79356b
A
439} ;
440
441#define vm_map_to_entry(map) ((struct vm_map_entry *) &(map)->hdr.links)
442#define vm_map_first_entry(map) ((map)->hdr.links.next)
443#define vm_map_last_entry(map) ((map)->hdr.links.prev)
444
445#if TASK_SWAPPER
446/*
447 * VM map swap states. There are no transition states.
448 */
449#define MAP_SW_IN 1 /* map is swapped in; residence count > 0 */
450#define MAP_SW_OUT 2 /* map is out (res_count == 0 */
451#endif /* TASK_SWAPPER */
452
453/*
454 * Type: vm_map_version_t [exported; contents invisible]
455 *
456 * Description:
457 * Map versions may be used to quickly validate a previous
458 * lookup operation.
459 *
460 * Usage note:
461 * Because they are bulky objects, map versions are usually
462 * passed by reference.
463 *
464 * Implementation:
465 * Just a timestamp for the main map.
466 */
467typedef struct vm_map_version {
468 unsigned int main_timestamp;
469} vm_map_version_t;
470
471/*
472 * Type: vm_map_copy_t [exported; contents invisible]
473 *
474 * Description:
475 * A map copy object represents a region of virtual memory
476 * that has been copied from an address map but is still
477 * in transit.
478 *
479 * A map copy object may only be used by a single thread
480 * at a time.
481 *
482 * Implementation:
483 * There are three formats for map copy objects.
484 * The first is very similar to the main
485 * address map in structure, and as a result, some
486 * of the internal maintenance functions/macros can
487 * be used with either address maps or map copy objects.
488 *
489 * The map copy object contains a header links
490 * entry onto which the other entries that represent
491 * the region are chained.
492 *
91447636
A
493 * The second format is a single vm object. This was used
494 * primarily in the pageout path - but is not currently used
495 * except for placeholder copy objects (see vm_map_copy_copy()).
496 *
497 * The third format is a kernel buffer copy object - for data
498 * small enough that physical copies were the most efficient
3e170ce0
A
499 * method. This method uses a zero-sized array unioned with
500 * other format-specific data in the 'c_u' member. This unsized
501 * array overlaps the other elements and allows us to use this
502 * extra structure space for physical memory copies. On 64-bit
503 * systems this saves ~64 bytes per vm_map_copy.
1c79356b 504 */
1c79356b
A
505
506struct vm_map_copy {
507 int type;
508#define VM_MAP_COPY_ENTRY_LIST 1
509#define VM_MAP_COPY_OBJECT 2
0b4e3aa0 510#define VM_MAP_COPY_KERNEL_BUFFER 3
1c79356b 511 vm_object_offset_t offset;
91447636 512 vm_map_size_t size;
1c79356b 513 union {
3e170ce0
A
514 struct vm_map_header hdr; /* ENTRY_LIST */
515 vm_object_t object; /* OBJECT */
516 uint8_t kdata[0]; /* KERNEL_BUFFER */
1c79356b
A
517 } c_u;
518};
519
520
521#define cpy_hdr c_u.hdr
522
91447636 523#define cpy_object c_u.object
3e170ce0
A
524#define cpy_kdata c_u.kdata
525#define cpy_kdata_hdr_sz (offsetof(struct vm_map_copy, c_u.kdata))
1c79356b 526
39236c6e
A
527#define VM_MAP_COPY_PAGE_SHIFT(copy) ((copy)->cpy_hdr.page_shift)
528#define VM_MAP_COPY_PAGE_SIZE(copy) (1 << VM_MAP_COPY_PAGE_SHIFT((copy)))
529#define VM_MAP_COPY_PAGE_MASK(copy) (VM_MAP_COPY_PAGE_SIZE((copy)) - 1)
1c79356b
A
530
531/*
532 * Useful macros for entry list copy objects
533 */
534
535#define vm_map_copy_to_entry(copy) \
536 ((struct vm_map_entry *) &(copy)->cpy_hdr.links)
537#define vm_map_copy_first_entry(copy) \
538 ((copy)->cpy_hdr.links.next)
539#define vm_map_copy_last_entry(copy) \
540 ((copy)->cpy_hdr.links.prev)
541
1c79356b
A
542/*
543 * Macros: vm_map_lock, etc. [internal use only]
544 * Description:
545 * Perform locking on the data portion of a map.
546 * When multiple maps are to be locked, order by map address.
547 * (See vm_map.c::vm_remap())
548 */
549
550#define vm_map_lock_init(map) \
9bccf70c 551 ((map)->timestamp = 0 , \
fe8ab488 552 lck_rw_init(&(map)->lock, &vm_map_lck_grp, &vm_map_lck_rw_attr))
9bccf70c 553
fe8ab488 554#define vm_map_lock(map) lck_rw_lock_exclusive(&(map)->lock)
9bccf70c 555#define vm_map_unlock(map) \
fe8ab488
A
556 ((map)->timestamp++ , lck_rw_done(&(map)->lock))
557#define vm_map_lock_read(map) lck_rw_lock_shared(&(map)->lock)
558#define vm_map_unlock_read(map) lck_rw_done(&(map)->lock)
1c79356b 559#define vm_map_lock_write_to_read(map) \
fe8ab488 560 ((map)->timestamp++ , lck_rw_lock_exclusive_to_shared(&(map)->lock))
2d21ac55
A
561/* lock_read_to_write() returns FALSE on failure. Macro evaluates to
562 * zero on success and non-zero value on failure.
563 */
fe8ab488 564#define vm_map_lock_read_to_write(map) (lck_rw_lock_shared_to_exclusive(&(map)->lock) != TRUE)
1c79356b 565
5ba3f43e
A
566#define vm_map_try_lock(map) lck_rw_try_lock_exclusive(&(map)->lock)
567#define vm_map_try_lock_read(map) lck_rw_try_lock_shared(&(map)->lock)
568
3e170ce0
A
569#if MACH_ASSERT || DEBUG
570#define vm_map_lock_assert_held(map) \
571 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_HELD)
572#define vm_map_lock_assert_shared(map) \
573 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_SHARED)
574#define vm_map_lock_assert_exclusive(map) \
575 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_EXCLUSIVE)
576#define vm_map_lock_assert_notheld(map) \
577 lck_rw_assert(&(map)->lock, LCK_RW_ASSERT_NOTHELD)
578#else /* MACH_ASSERT || DEBUG */
579#define vm_map_lock_assert_held(map)
580#define vm_map_lock_assert_shared(map)
581#define vm_map_lock_assert_exclusive(map)
582#define vm_map_lock_assert_notheld(map)
583#endif /* MACH_ASSERT || DEBUG */
584
1c79356b
A
585/*
586 * Exported procedures that operate on vm_map_t.
587 */
588
589/* Initialize the module */
39236c6e 590extern void vm_map_init(void);
1c79356b 591
39236c6e 592extern void vm_kernel_reserved_entry_init(void);
7ddcb079 593
1c79356b
A
594/* Allocate a range in the specified virtual address map and
595 * return the entry allocated for that range. */
596extern kern_return_t vm_map_find_space(
91447636
A
597 vm_map_t map,
598 vm_map_address_t *address, /* OUT */
599 vm_map_size_t size,
600 vm_map_offset_t mask,
0c530ab8 601 int flags,
5ba3f43e
A
602 vm_map_kernel_flags_t vmk_flags,
603 vm_tag_t tag,
91447636 604 vm_map_entry_t *o_entry); /* OUT */
1c79356b 605
e2d2fc5c
A
606extern void vm_map_clip_start(
607 vm_map_t map,
608 vm_map_entry_t entry,
609 vm_map_offset_t endaddr);
610extern void vm_map_clip_end(
611 vm_map_t map,
612 vm_map_entry_t entry,
613 vm_map_offset_t endaddr);
e2d2fc5c
A
614extern boolean_t vm_map_entry_should_cow_for_true_share(
615 vm_map_entry_t entry);
e2d2fc5c 616
1c79356b
A
617/* Lookup map entry containing or the specified address in the given map */
618extern boolean_t vm_map_lookup_entry(
91447636
A
619 vm_map_t map,
620 vm_map_address_t address,
621 vm_map_entry_t *entry); /* OUT */
1c79356b 622
39236c6e
A
623extern void vm_map_copy_remap(
624 vm_map_t map,
625 vm_map_entry_t where,
626 vm_map_copy_t copy,
627 vm_map_offset_t adjustment,
628 vm_prot_t cur_prot,
629 vm_prot_t max_prot,
630 vm_inherit_t inheritance);
631
1c79356b
A
632/* Find the VM object, offset, and protection for a given virtual address
633 * in the specified map, assuming a page fault of the type specified. */
634extern kern_return_t vm_map_lookup_locked(
91447636
A
635 vm_map_t *var_map, /* IN/OUT */
636 vm_map_address_t vaddr,
637 vm_prot_t fault_type,
2d21ac55 638 int object_lock_type,
91447636
A
639 vm_map_version_t *out_version, /* OUT */
640 vm_object_t *object, /* OUT */
641 vm_object_offset_t *offset, /* OUT */
642 vm_prot_t *out_prot, /* OUT */
643 boolean_t *wired, /* OUT */
2d21ac55 644 vm_object_fault_info_t fault_info, /* OUT */
91447636 645 vm_map_t *real_map); /* OUT */
1c79356b
A
646
647/* Verifies that the map has not changed since the given version. */
648extern boolean_t vm_map_verify(
91447636
A
649 vm_map_t map,
650 vm_map_version_t *version); /* REF */
1c79356b
A
651
652extern vm_map_entry_t vm_map_entry_insert(
653 vm_map_t map,
654 vm_map_entry_t insp_entry,
91447636
A
655 vm_map_offset_t start,
656 vm_map_offset_t end,
1c79356b
A
657 vm_object_t object,
658 vm_object_offset_t offset,
659 boolean_t needs_copy,
660 boolean_t is_shared,
661 boolean_t in_transition,
662 vm_prot_t cur_protection,
663 vm_prot_t max_protection,
664 vm_behavior_t behavior,
665 vm_inherit_t inheritance,
2d21ac55 666 unsigned wired_count,
b0d623f7
A
667 boolean_t no_cache,
668 boolean_t permanent,
39236c6e 669 unsigned int superpage_size,
fe8ab488 670 boolean_t clear_map_aligned,
5ba3f43e
A
671 boolean_t is_submap,
672 boolean_t used_for_jit,
673 int alias);
1c79356b 674
1c79356b
A
675
676/*
677 * Functions implemented as macros
678 */
91447636 679#define vm_map_min(map) ((map)->min_offset)
1c79356b
A
680 /* Lowest valid address in
681 * a map */
682
91447636 683#define vm_map_max(map) ((map)->max_offset)
1c79356b
A
684 /* Highest valid address */
685
686#define vm_map_pmap(map) ((map)->pmap)
687 /* Physical map associated
688 * with this address map */
689
1c79356b
A
690/*
691 * Macros/functions for map residence counts and swapin/out of vm maps
692 */
693#if TASK_SWAPPER
694
695#if MACH_ASSERT
696/* Gain a reference to an existing map */
697extern void vm_map_reference(
698 vm_map_t map);
699/* Lose a residence count */
700extern void vm_map_res_deallocate(
701 vm_map_t map);
702/* Gain a residence count on a map */
703extern void vm_map_res_reference(
704 vm_map_t map);
705/* Gain reference & residence counts to possibly swapped-out map */
706extern void vm_map_reference_swap(
707 vm_map_t map);
708
709#else /* MACH_ASSERT */
710
91447636 711#define vm_map_reference(map) \
1c79356b 712MACRO_BEGIN \
91447636 713 vm_map_t Map = (map); \
1c79356b 714 if (Map) { \
b0d623f7 715 lck_mtx_lock(&Map->s_lock); \
1c79356b
A
716 Map->res_count++; \
717 Map->ref_count++; \
b0d623f7 718 lck_mtx_unlock(&Map->s_lock); \
1c79356b
A
719 } \
720MACRO_END
721
722#define vm_map_res_reference(map) \
723MACRO_BEGIN \
91447636 724 vm_map_t Lmap = (map); \
1c79356b 725 if (Lmap->res_count == 0) { \
b0d623f7 726 lck_mtx_unlock(&Lmap->s_lock);\
1c79356b
A
727 vm_map_lock(Lmap); \
728 vm_map_swapin(Lmap); \
b0d623f7 729 lck_mtx_lock(&Lmap->s_lock); \
1c79356b
A
730 ++Lmap->res_count; \
731 vm_map_unlock(Lmap); \
732 } else \
733 ++Lmap->res_count; \
734MACRO_END
735
736#define vm_map_res_deallocate(map) \
737MACRO_BEGIN \
91447636
A
738 vm_map_t Map = (map); \
739 if (--Map->res_count == 0) { \
b0d623f7 740 lck_mtx_unlock(&Map->s_lock); \
1c79356b
A
741 vm_map_lock(Map); \
742 vm_map_swapout(Map); \
743 vm_map_unlock(Map); \
b0d623f7 744 lck_mtx_lock(&Map->s_lock); \
1c79356b
A
745 } \
746MACRO_END
747
748#define vm_map_reference_swap(map) \
749MACRO_BEGIN \
750 vm_map_t Map = (map); \
b0d623f7 751 lck_mtx_lock(&Map->s_lock); \
1c79356b
A
752 ++Map->ref_count; \
753 vm_map_res_reference(Map); \
b0d623f7 754 lck_mtx_unlock(&Map->s_lock); \
1c79356b
A
755MACRO_END
756#endif /* MACH_ASSERT */
757
758extern void vm_map_swapin(
759 vm_map_t map);
760
761extern void vm_map_swapout(
762 vm_map_t map);
763
764#else /* TASK_SWAPPER */
765
766#define vm_map_reference(map) \
767MACRO_BEGIN \
768 vm_map_t Map = (map); \
769 if (Map) { \
b0d623f7 770 lck_mtx_lock(&Map->s_lock); \
1c79356b 771 Map->ref_count++; \
b0d623f7 772 lck_mtx_unlock(&Map->s_lock); \
1c79356b
A
773 } \
774MACRO_END
775
776#define vm_map_reference_swap(map) vm_map_reference(map)
777#define vm_map_res_reference(map)
778#define vm_map_res_deallocate(map)
779
780#endif /* TASK_SWAPPER */
781
782/*
783 * Submap object. Must be used to create memory to be put
784 * in a submap by vm_map_submap.
785 */
786extern vm_object_t vm_submap_object;
787
788/*
789 * Wait and wakeup macros for in_transition map entries.
790 */
791#define vm_map_entry_wait(map, interruptible) \
9bccf70c 792 ((map)->timestamp++ , \
fe8ab488
A
793 lck_rw_sleep(&(map)->lock, LCK_SLEEP_EXCLUSIVE|LCK_SLEEP_PROMOTED_PRI, \
794 (event_t)&(map)->hdr, interruptible))
1c79356b 795
1c79356b 796
91447636
A
797#define vm_map_entry_wakeup(map) \
798 thread_wakeup((event_t)(&(map)->hdr))
1c79356b
A
799
800
91447636 801#define vm_map_ref_fast(map) \
1c79356b 802 MACRO_BEGIN \
b0d623f7 803 lck_mtx_lock(&map->s_lock); \
1c79356b
A
804 map->ref_count++; \
805 vm_map_res_reference(map); \
b0d623f7 806 lck_mtx_unlock(&map->s_lock); \
1c79356b
A
807 MACRO_END
808
91447636 809#define vm_map_dealloc_fast(map) \
1c79356b 810 MACRO_BEGIN \
39037602 811 int c; \
1c79356b 812 \
b0d623f7 813 lck_mtx_lock(&map->s_lock); \
91447636 814 c = --map->ref_count; \
1c79356b
A
815 if (c > 0) \
816 vm_map_res_deallocate(map); \
b0d623f7 817 lck_mtx_unlock(&map->s_lock); \
1c79356b
A
818 if (c == 0) \
819 vm_map_destroy(map); \
820 MACRO_END
821
822
823/* simplify map entries */
5353443c
A
824extern void vm_map_simplify_entry(
825 vm_map_t map,
826 vm_map_entry_t this_entry);
1c79356b 827extern void vm_map_simplify(
91447636
A
828 vm_map_t map,
829 vm_map_offset_t start);
1c79356b
A
830
831/* Move the information in a map copy object to a new map copy object */
832extern vm_map_copy_t vm_map_copy_copy(
91447636 833 vm_map_copy_t copy);
1c79356b 834
1c79356b
A
835/* Create a copy object from an object. */
836extern kern_return_t vm_map_copyin_object(
837 vm_object_t object,
838 vm_object_offset_t offset,
839 vm_object_size_t size,
840 vm_map_copy_t *copy_result); /* OUT */
841
316670eb
A
842extern kern_return_t vm_map_random_address_for_size(
843 vm_map_t map,
844 vm_map_offset_t *address,
845 vm_map_size_t size);
846
1c79356b
A
847/* Enter a mapping */
848extern kern_return_t vm_map_enter(
849 vm_map_t map,
91447636
A
850 vm_map_offset_t *address,
851 vm_map_size_t size,
852 vm_map_offset_t mask,
1c79356b 853 int flags,
5ba3f43e
A
854 vm_map_kernel_flags_t vmk_flags,
855 vm_tag_t tag,
1c79356b
A
856 vm_object_t object,
857 vm_object_offset_t offset,
858 boolean_t needs_copy,
859 vm_prot_t cur_protection,
860 vm_prot_t max_protection,
861 vm_inherit_t inheritance);
862
5ba3f43e
A
863#if __arm64__
864extern kern_return_t vm_map_enter_fourk(
865 vm_map_t map,
866 vm_map_offset_t *address,
867 vm_map_size_t size,
868 vm_map_offset_t mask,
869 int flags,
870 vm_map_kernel_flags_t vmk_flags,
871 vm_tag_t tag,
872 vm_object_t object,
873 vm_object_offset_t offset,
874 boolean_t needs_copy,
875 vm_prot_t cur_protection,
876 vm_prot_t max_protection,
877 vm_inherit_t inheritance);
878#endif /* __arm64__ */
3e170ce0 879
91447636
A
880/* XXX should go away - replaced with regular enter of contig object */
881extern kern_return_t vm_map_enter_cpm(
882 vm_map_t map,
883 vm_map_address_t *addr,
884 vm_map_size_t size,
885 int flags);
886
887extern kern_return_t vm_map_remap(
888 vm_map_t target_map,
889 vm_map_offset_t *address,
890 vm_map_size_t size,
891 vm_map_offset_t mask,
060df5ea 892 int flags,
5ba3f43e
A
893 vm_map_kernel_flags_t vmk_flags,
894 vm_tag_t tag,
91447636
A
895 vm_map_t src_map,
896 vm_map_offset_t memory_address,
897 boolean_t copy,
898 vm_prot_t *cur_protection,
899 vm_prot_t *max_protection,
900 vm_inherit_t inheritance);
901
902
903/*
904 * Read and write from a kernel buffer to a specified map.
905 */
1c79356b 906extern kern_return_t vm_map_write_user(
91447636
A
907 vm_map_t map,
908 void *src_p,
909 vm_map_offset_t dst_addr,
910 vm_size_t size);
1c79356b
A
911
912extern kern_return_t vm_map_read_user(
91447636
A
913 vm_map_t map,
914 vm_map_offset_t src_addr,
915 void *dst_p,
916 vm_size_t size);
1c79356b
A
917
918/* Create a new task map using an existing task map as a template. */
919extern vm_map_t vm_map_fork(
316670eb 920 ledger_t ledger,
39037602
A
921 vm_map_t old_map,
922 int options);
923#define VM_MAP_FORK_SHARE_IF_INHERIT_NONE 0x00000001
924#define VM_MAP_FORK_PRESERVE_PURGEABLE 0x00000002
1c79356b 925
9bccf70c
A
926/* Change inheritance */
927extern kern_return_t vm_map_inherit(
91447636
A
928 vm_map_t map,
929 vm_map_offset_t start,
930 vm_map_offset_t end,
931 vm_inherit_t new_inheritance);
1c79356b 932
9bccf70c
A
933/* Add or remove machine-dependent attributes from map regions */
934extern kern_return_t vm_map_machine_attribute(
91447636
A
935 vm_map_t map,
936 vm_map_offset_t start,
937 vm_map_offset_t end,
9bccf70c
A
938 vm_machine_attribute_t attribute,
939 vm_machine_attribute_val_t* value); /* IN/OUT */
91447636
A
940
941extern kern_return_t vm_map_msync(
942 vm_map_t map,
943 vm_map_address_t address,
944 vm_map_size_t size,
945 vm_sync_t sync_flags);
946
9bccf70c
A
947/* Set paging behavior */
948extern kern_return_t vm_map_behavior_set(
91447636
A
949 vm_map_t map,
950 vm_map_offset_t start,
951 vm_map_offset_t end,
952 vm_behavior_t new_behavior);
953
91447636
A
954extern kern_return_t vm_map_region(
955 vm_map_t map,
956 vm_map_offset_t *address,
957 vm_map_size_t *size,
958 vm_region_flavor_t flavor,
959 vm_region_info_t info,
960 mach_msg_type_number_t *count,
961 mach_port_t *object_name);
962
963extern kern_return_t vm_map_region_recurse_64(
964 vm_map_t map,
965 vm_map_offset_t *address,
966 vm_map_size_t *size,
967 natural_t *nesting_depth,
968 vm_region_submap_info_64_t info,
969 mach_msg_type_number_t *count);
970
b0d623f7 971extern kern_return_t vm_map_page_query_internal(
91447636
A
972 vm_map_t map,
973 vm_map_offset_t offset,
974 int *disposition,
975 int *ref_count);
9bccf70c 976
39236c6e
A
977extern kern_return_t vm_map_query_volatile(
978 vm_map_t map,
979 mach_vm_size_t *volatile_virtual_size_p,
980 mach_vm_size_t *volatile_resident_size_p,
3e170ce0
A
981 mach_vm_size_t *volatile_compressed_size_p,
982 mach_vm_size_t *volatile_pmap_size_p,
983 mach_vm_size_t *volatile_compressed_pmap_size_p);
b0d623f7 984
9bccf70c 985extern kern_return_t vm_map_submap(
91447636
A
986 vm_map_t map,
987 vm_map_offset_t start,
988 vm_map_offset_t end,
989 vm_map_t submap,
990 vm_map_offset_t offset,
991 boolean_t use_pmap);
992
993extern void vm_map_submap_pmap_clean(
994 vm_map_t map,
995 vm_map_offset_t start,
996 vm_map_offset_t end,
997 vm_map_t sub_map,
998 vm_map_offset_t offset);
999
1000/* Convert from a map entry port to a map */
1001extern vm_map_t convert_port_entry_to_map(
1002 ipc_port_t port);
1003
1004/* Convert from a port to a vm_object */
1005extern vm_object_t convert_port_entry_to_object(
1006 ipc_port_t port);
9bccf70c
A
1007
1008
6d2010ae
A
1009extern kern_return_t vm_map_set_cache_attr(
1010 vm_map_t map,
1011 vm_map_offset_t va);
1012
1013
2d21ac55
A
1014/* definitions related to overriding the NX behavior */
1015
1016#define VM_ABI_32 0x1
1017#define VM_ABI_64 0x2
1018
1019extern int override_nx(vm_map_t map, uint32_t user_tag);
1020
9bccf70c
A
1021#endif /* MACH_KERNEL_PRIVATE */
1022
91447636
A
1023__BEGIN_DECLS
1024
9bccf70c
A
1025/* Create an empty map */
1026extern vm_map_t vm_map_create(
91447636
A
1027 pmap_t pmap,
1028 vm_map_offset_t min_off,
1029 vm_map_offset_t max_off,
1030 boolean_t pageable);
9bccf70c 1031
3e170ce0
A
1032extern void vm_map_disable_hole_optimization(vm_map_t map);
1033
9bccf70c
A
1034/* Get rid of a map */
1035extern void vm_map_destroy(
2d21ac55
A
1036 vm_map_t map,
1037 int flags);
1038
9bccf70c
A
1039/* Lose a reference */
1040extern void vm_map_deallocate(
91447636
A
1041 vm_map_t map);
1042
1043extern vm_map_t vm_map_switch(
1044 vm_map_t map);
9bccf70c
A
1045
1046/* Change protection */
1047extern kern_return_t vm_map_protect(
91447636
A
1048 vm_map_t map,
1049 vm_map_offset_t start,
1050 vm_map_offset_t end,
1051 vm_prot_t new_prot,
1052 boolean_t set_max);
1053
1054/* Check protection */
1055extern boolean_t vm_map_check_protection(
1056 vm_map_t map,
1057 vm_map_offset_t start,
1058 vm_map_offset_t end,
1059 vm_prot_t protection);
1c79356b
A
1060
1061/* wire down a region */
5ba3f43e
A
1062
1063#ifdef XNU_KERNEL_PRIVATE
1064
1065extern kern_return_t vm_map_wire_kernel(
1066 vm_map_t map,
1067 vm_map_offset_t start,
1068 vm_map_offset_t end,
1069 vm_prot_t access_type,
1070 vm_tag_t tag,
1071 boolean_t user_wire);
1072
1073extern kern_return_t vm_map_wire_and_extract_kernel(
1074 vm_map_t map,
1075 vm_map_offset_t start,
1076 vm_prot_t access_type,
1077 vm_tag_t tag,
1078 boolean_t user_wire,
1079 ppnum_t *physpage_p);
1080
1081/* kext exported versions */
1082
1083extern kern_return_t vm_map_wire_external(
1084 vm_map_t map,
1085 vm_map_offset_t start,
1086 vm_map_offset_t end,
1087 vm_prot_t access_type,
1088 boolean_t user_wire);
1089
1090extern kern_return_t vm_map_wire_and_extract_external(
1091 vm_map_t map,
1092 vm_map_offset_t start,
1093 vm_prot_t access_type,
1094 boolean_t user_wire,
1095 ppnum_t *physpage_p);
1096
1097#else /* XNU_KERNEL_PRIVATE */
1098
1c79356b 1099extern kern_return_t vm_map_wire(
91447636
A
1100 vm_map_t map,
1101 vm_map_offset_t start,
1102 vm_map_offset_t end,
1103 vm_prot_t access_type,
1104 boolean_t user_wire);
1c79356b 1105
fe8ab488
A
1106extern kern_return_t vm_map_wire_and_extract(
1107 vm_map_t map,
1108 vm_map_offset_t start,
1109 vm_prot_t access_type,
1110 boolean_t user_wire,
1111 ppnum_t *physpage_p);
1112
5ba3f43e
A
1113#endif /* !XNU_KERNEL_PRIVATE */
1114
1c79356b
A
1115/* unwire a region */
1116extern kern_return_t vm_map_unwire(
91447636
A
1117 vm_map_t map,
1118 vm_map_offset_t start,
1119 vm_map_offset_t end,
1120 boolean_t user_wire);
1c79356b 1121
5ba3f43e
A
1122#ifdef XNU_KERNEL_PRIVATE
1123
2d21ac55
A
1124/* Enter a mapping of a memory object */
1125extern kern_return_t vm_map_enter_mem_object(
1126 vm_map_t map,
1127 vm_map_offset_t *address,
1128 vm_map_size_t size,
1129 vm_map_offset_t mask,
1130 int flags,
5ba3f43e
A
1131 vm_map_kernel_flags_t vmk_flags,
1132 vm_tag_t tag,
2d21ac55
A
1133 ipc_port_t port,
1134 vm_object_offset_t offset,
1135 boolean_t needs_copy,
1136 vm_prot_t cur_protection,
1137 vm_prot_t max_protection,
1138 vm_inherit_t inheritance);
1139
fe8ab488
A
1140/* Enter a mapping of a memory object */
1141extern kern_return_t vm_map_enter_mem_object_prefault(
1142 vm_map_t map,
1143 vm_map_offset_t *address,
1144 vm_map_size_t size,
1145 vm_map_offset_t mask,
1146 int flags,
5ba3f43e
A
1147 vm_map_kernel_flags_t vmk_flags,
1148 vm_tag_t tag,
fe8ab488
A
1149 ipc_port_t port,
1150 vm_object_offset_t offset,
1151 vm_prot_t cur_protection,
1152 vm_prot_t max_protection,
1153 upl_page_list_ptr_t page_list,
1154 unsigned int page_list_count);
1155
b0d623f7
A
1156/* Enter a mapping of a memory object */
1157extern kern_return_t vm_map_enter_mem_object_control(
1158 vm_map_t map,
1159 vm_map_offset_t *address,
1160 vm_map_size_t size,
1161 vm_map_offset_t mask,
1162 int flags,
5ba3f43e
A
1163 vm_map_kernel_flags_t vmk_flags,
1164 vm_tag_t tag,
b0d623f7
A
1165 memory_object_control_t control,
1166 vm_object_offset_t offset,
1167 boolean_t needs_copy,
1168 vm_prot_t cur_protection,
1169 vm_prot_t max_protection,
1170 vm_inherit_t inheritance);
1171
5ba3f43e
A
1172#endif /* !XNU_KERNEL_PRIVATE */
1173
1c79356b
A
1174/* Deallocate a region */
1175extern kern_return_t vm_map_remove(
91447636
A
1176 vm_map_t map,
1177 vm_map_offset_t start,
1178 vm_map_offset_t end,
1179 boolean_t flags);
1180
39037602
A
1181/* Deallocate a region when the map is already locked */
1182extern kern_return_t vm_map_remove_locked(
1183 vm_map_t map,
1184 vm_map_offset_t start,
1185 vm_map_offset_t end,
1186 boolean_t flags);
1187
91447636
A
1188/* Discard a copy without using it */
1189extern void vm_map_copy_discard(
1190 vm_map_copy_t copy);
1191
1192/* Overwrite existing memory with a copy */
1193extern kern_return_t vm_map_copy_overwrite(
1194 vm_map_t dst_map,
1195 vm_map_address_t dst_addr,
1196 vm_map_copy_t copy,
b0d623f7 1197 boolean_t interruptible);
1c79356b 1198
2dced7af
A
1199/* returns TRUE if size of vm_map_copy == size parameter FALSE otherwise */
1200extern boolean_t vm_map_copy_validate_size(
1201 vm_map_t dst_map,
1202 vm_map_copy_t copy,
39037602 1203 vm_map_size_t *size);
2dced7af 1204
1c79356b
A
1205/* Place a copy into a map */
1206extern kern_return_t vm_map_copyout(
91447636
A
1207 vm_map_t dst_map,
1208 vm_map_address_t *dst_addr, /* OUT */
1209 vm_map_copy_t copy);
1c79356b 1210
39037602
A
1211extern kern_return_t vm_map_copyout_size(
1212 vm_map_t dst_map,
1213 vm_map_address_t *dst_addr, /* OUT */
1214 vm_map_copy_t copy,
1215 vm_map_size_t copy_size);
1216
39236c6e
A
1217extern kern_return_t vm_map_copyout_internal(
1218 vm_map_t dst_map,
1219 vm_map_address_t *dst_addr, /* OUT */
1220 vm_map_copy_t copy,
39037602 1221 vm_map_size_t copy_size,
39236c6e
A
1222 boolean_t consume_on_success,
1223 vm_prot_t cur_protection,
1224 vm_prot_t max_protection,
1225 vm_inherit_t inheritance);
1226
2d21ac55
A
1227extern kern_return_t vm_map_copyin(
1228 vm_map_t src_map,
1229 vm_map_address_t src_addr,
1230 vm_map_size_t len,
1231 boolean_t src_destroy,
1232 vm_map_copy_t *copy_result); /* OUT */
1233
1c79356b 1234extern kern_return_t vm_map_copyin_common(
91447636
A
1235 vm_map_t src_map,
1236 vm_map_address_t src_addr,
1237 vm_map_size_t len,
1238 boolean_t src_destroy,
1239 boolean_t src_volatile,
1240 vm_map_copy_t *copy_result, /* OUT */
1241 boolean_t use_maxprot);
55e303ae 1242
4bd07ac2
A
1243#define VM_MAP_COPYIN_SRC_DESTROY 0x00000001
1244#define VM_MAP_COPYIN_USE_MAXPROT 0x00000002
1245#define VM_MAP_COPYIN_ENTRY_LIST 0x00000004
39037602
A
1246#define VM_MAP_COPYIN_PRESERVE_PURGEABLE 0x00000008
1247#define VM_MAP_COPYIN_ALL_FLAGS 0x0000000F
4bd07ac2
A
1248extern kern_return_t vm_map_copyin_internal(
1249 vm_map_t src_map,
1250 vm_map_address_t src_addr,
1251 vm_map_size_t len,
1252 int flags,
1253 vm_map_copy_t *copy_result); /* OUT */
1254
39236c6e
A
1255extern kern_return_t vm_map_copy_extract(
1256 vm_map_t src_map,
1257 vm_map_address_t src_addr,
1258 vm_map_size_t len,
1259 vm_map_copy_t *copy_result, /* OUT */
1260 vm_prot_t *cur_prot, /* OUT */
1261 vm_prot_t *max_prot);
1262
1263
0c530ab8
A
1264extern void vm_map_disable_NX(
1265 vm_map_t map);
1266
6d2010ae
A
1267extern void vm_map_disallow_data_exec(
1268 vm_map_t map);
1269
0c530ab8
A
1270extern void vm_map_set_64bit(
1271 vm_map_t map);
1272
1273extern void vm_map_set_32bit(
1274 vm_map_t map);
1275
813fb2f6
A
1276extern void vm_map_set_jumbo(
1277 vm_map_t map);
1278
316670eb
A
1279extern boolean_t vm_map_has_hard_pagezero(
1280 vm_map_t map,
1281 vm_map_offset_t pagezero_size);
39037602 1282extern void vm_commit_pagezero_status(vm_map_t tmap);
316670eb 1283
5ba3f43e
A
1284#ifdef __arm__
1285static inline boolean_t vm_map_is_64bit(__unused vm_map_t map) { return 0; }
1286#else
2d21ac55
A
1287extern boolean_t vm_map_is_64bit(
1288 vm_map_t map);
5ba3f43e 1289#endif
2d21ac55 1290
0c530ab8 1291
316670eb
A
1292extern kern_return_t vm_map_raise_max_offset(
1293 vm_map_t map,
1294 vm_map_offset_t new_max_offset);
1295
0c530ab8
A
1296extern kern_return_t vm_map_raise_min_offset(
1297 vm_map_t map,
1298 vm_map_offset_t new_min_offset);
5ba3f43e
A
1299#if __x86_64__
1300extern void vm_map_set_high_start(
1301 vm_map_t map,
1302 vm_map_offset_t high_start);
1303#endif /* __x86_64__ */
0c530ab8
A
1304
1305extern vm_map_offset_t vm_compute_max_offset(
3e170ce0 1306 boolean_t is64);
0c530ab8 1307
5ba3f43e
A
1308extern void vm_map_get_max_aslr_slide_section(
1309 vm_map_t map,
1310 int64_t *max_sections,
1311 int64_t *section_size);
1312
39236c6e
A
1313extern uint64_t vm_map_get_max_aslr_slide_pages(
1314 vm_map_t map);
5ba3f43e
A
1315
1316extern uint64_t vm_map_get_max_loader_aslr_slide_pages(
1317 vm_map_t map);
1318
2d21ac55
A
1319extern void vm_map_set_user_wire_limit(
1320 vm_map_t map,
1321 vm_size_t limit);
1322
b0d623f7 1323extern void vm_map_switch_protect(
5ba3f43e 1324 vm_map_t map,
b0d623f7
A
1325 boolean_t val);
1326
39236c6e
A
1327extern void vm_map_iokit_mapped_region(
1328 vm_map_t map,
1329 vm_size_t bytes);
1330
1331extern void vm_map_iokit_unmapped_region(
1332 vm_map_t map,
1333 vm_size_t bytes);
1334
1335
6d2010ae
A
1336extern boolean_t first_free_is_valid(vm_map_t);
1337
39236c6e
A
1338extern int vm_map_page_shift(
1339 vm_map_t map);
1340
3e170ce0 1341extern vm_map_offset_t vm_map_page_mask(
39236c6e
A
1342 vm_map_t map);
1343
1344extern int vm_map_page_size(
1345 vm_map_t map);
1346
1347extern vm_map_offset_t vm_map_round_page_mask(
1348 vm_map_offset_t offset,
1349 vm_map_offset_t mask);
1350
1351extern vm_map_offset_t vm_map_trunc_page_mask(
1352 vm_map_offset_t offset,
1353 vm_map_offset_t mask);
1354
3e170ce0
A
1355extern boolean_t vm_map_page_aligned(
1356 vm_map_offset_t offset,
1357 vm_map_offset_t mask);
1358
b0d623f7
A
1359#ifdef XNU_KERNEL_PRIVATE
1360extern kern_return_t vm_map_page_info(
1361 vm_map_t map,
1362 vm_map_offset_t offset,
1363 vm_page_info_flavor_t flavor,
1364 vm_page_info_t info,
1365 mach_msg_type_number_t *count);
5ba3f43e
A
1366extern kern_return_t vm_map_page_range_info_internal(
1367 vm_map_t map,
1368 vm_map_offset_t start_offset,
1369 vm_map_offset_t end_offset,
1370 vm_page_info_flavor_t flavor,
1371 vm_page_info_t info,
1372 mach_msg_type_number_t *count);
b0d623f7
A
1373#endif /* XNU_KERNEL_PRIVATE */
1374
593a1d5f 1375
2d21ac55
A
1376#ifdef MACH_KERNEL_PRIVATE
1377
1c79356b
A
1378/*
1379 * Macros to invoke vm_map_copyin_common. vm_map_copyin is the
1380 * usual form; it handles a copyin based on the current protection
1381 * (current protection == VM_PROT_NONE) is a failure.
1382 * vm_map_copyin_maxprot handles a copyin based on maximum possible
1383 * access. The difference is that a region with no current access
1384 * BUT possible maximum access is rejected by vm_map_copyin(), but
1385 * returned by vm_map_copyin_maxprot.
1386 */
1387#define vm_map_copyin(src_map, src_addr, len, src_destroy, copy_result) \
1388 vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
1389 FALSE, copy_result, FALSE)
1390
1391#define vm_map_copyin_maxprot(src_map, \
1392 src_addr, len, src_destroy, copy_result) \
1393 vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
1394 FALSE, copy_result, TRUE)
1395
39236c6e
A
1396
1397/*
1398 * Internal macros for rounding and truncation of vm_map offsets and sizes
1399 */
1400#define VM_MAP_ROUND_PAGE(x,pgmask) (((vm_map_offset_t)(x) + (pgmask)) & ~((signed)(pgmask)))
1401#define VM_MAP_TRUNC_PAGE(x,pgmask) ((vm_map_offset_t)(x) & ~((signed)(pgmask)))
2d21ac55 1402
91447636
A
1403/*
1404 * Macros for rounding and truncation of vm_map offsets and sizes
1405 */
39236c6e
A
1406#define VM_MAP_PAGE_SHIFT(map) ((map) ? (map)->hdr.page_shift : PAGE_SHIFT)
1407#define VM_MAP_PAGE_SIZE(map) (1 << VM_MAP_PAGE_SHIFT((map)))
1408#define VM_MAP_PAGE_MASK(map) (VM_MAP_PAGE_SIZE((map)) - 1)
1409#define VM_MAP_PAGE_ALIGNED(x,pgmask) (((x) & (pgmask)) == 0)
1410
5ba3f43e
A
1411static inline void vm_prot_to_wimg(unsigned int prot, unsigned int *wimg)
1412{
1413 switch (prot) {
1414 case MAP_MEM_NOOP: break;
1415 case MAP_MEM_IO: *wimg = VM_WIMG_IO; break;
1416 case MAP_MEM_COPYBACK: *wimg = VM_WIMG_USE_DEFAULT; break;
1417 case MAP_MEM_INNERWBACK: *wimg = VM_WIMG_INNERWBACK; break;
1418 case MAP_MEM_POSTED: *wimg = VM_WIMG_POSTED; break;
1419 case MAP_MEM_WTHRU: *wimg = VM_WIMG_WTHRU; break;
1420 case MAP_MEM_WCOMB: *wimg = VM_WIMG_WCOMB; break;
1421 default:
1422 panic("Unrecognized mapping type %u\n", prot);
1423 }
1424}
1425
39236c6e
A
1426#endif /* MACH_KERNEL_PRIVATE */
1427
1428#ifdef XNU_KERNEL_PRIVATE
1429extern kern_return_t vm_map_set_page_shift(vm_map_t map, int pageshift);
1430#endif /* XNU_KERNEL_PRIVATE */
1431
1432#define vm_map_round_page(x,pgmask) (((vm_map_offset_t)(x) + (pgmask)) & ~((signed)(pgmask)))
1433#define vm_map_trunc_page(x,pgmask) ((vm_map_offset_t)(x) & ~((signed)(pgmask)))
91447636 1434
1c79356b
A
1435/*
1436 * Flags for vm_map_remove() and vm_map_delete()
1437 */
1438#define VM_MAP_NO_FLAGS 0x0
1439#define VM_MAP_REMOVE_KUNWIRE 0x1
1440#define VM_MAP_REMOVE_INTERRUPTIBLE 0x2
1441#define VM_MAP_REMOVE_WAIT_FOR_KWIRE 0x4
91447636 1442#define VM_MAP_REMOVE_SAVE_ENTRIES 0x8
2d21ac55 1443#define VM_MAP_REMOVE_NO_PMAP_CLEANUP 0x10
fe8ab488 1444#define VM_MAP_REMOVE_NO_MAP_ALIGN 0x20
3e170ce0 1445#define VM_MAP_REMOVE_NO_UNNESTING 0x40
5ba3f43e 1446#define VM_MAP_REMOVE_IMMUTABLE 0x80
91447636
A
1447
1448/* Support for UPLs from vm_maps */
1449
5ba3f43e
A
1450#ifdef XNU_KERNEL_PRIVATE
1451
91447636
A
1452extern kern_return_t vm_map_get_upl(
1453 vm_map_t target_map,
cc9f6e38 1454 vm_map_offset_t map_offset,
b0d623f7 1455 upl_size_t *size,
91447636
A
1456 upl_t *upl,
1457 upl_page_info_array_t page_info,
3e170ce0
A
1458 unsigned int *page_infoCnt,
1459 upl_control_flags_t *flags,
5ba3f43e 1460 vm_tag_t tag,
3e170ce0
A
1461 int force_data_sync);
1462
5ba3f43e
A
1463#endif /* XNU_KERNEL_PRIVATE */
1464
3e170ce0
A
1465extern void
1466vm_map_sizes(vm_map_t map,
1467 vm_map_size_t * psize,
1468 vm_map_size_t * pfree,
1469 vm_map_size_t * plargest_free);
91447636 1470
b7266188
A
1471#if CONFIG_DYNAMIC_CODE_SIGNING
1472extern kern_return_t vm_map_sign(vm_map_t map,
1473 vm_map_offset_t start,
1474 vm_map_offset_t end);
1475#endif
1476
fe8ab488
A
1477extern kern_return_t vm_map_partial_reap(
1478 vm_map_t map,
1479 unsigned int *reclaimed_resident,
1480 unsigned int *reclaimed_compressed);
1481
316670eb 1482
39037602
A
1483#if DEVELOPMENT || DEBUG
1484
1485extern int vm_map_disconnect_page_mappings(
1486 vm_map_t map,
1487 boolean_t);
1488#endif
1489
1490
1491#if CONFIG_FREEZE
6d2010ae
A
1492
1493extern kern_return_t vm_map_freeze(
1494 vm_map_t map,
1495 unsigned int *purgeable_count,
1496 unsigned int *wired_count,
1497 unsigned int *clean_count,
1498 unsigned int *dirty_count,
316670eb 1499 unsigned int dirty_budget,
6d2010ae 1500 boolean_t *has_shared);
6d2010ae
A
1501#endif
1502
91447636
A
1503__END_DECLS
1504
1505#endif /* KERNEL_PRIVATE */
1c79356b 1506
1c79356b 1507#endif /* _VM_VM_MAP_H_ */