]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_map.h
f88bd545d91f33ca51269712ccfb76bdd17616c3
[apple/xnu.git] / osfmk / vm / vm_map.h
1 /*
2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. 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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
80 #include <mach/vm_param.h>
81 #include <vm/pmap.h>
82
83 #ifdef KERNEL_PRIVATE
84
85 #include <sys/cdefs.h>
86
87 __BEGIN_DECLS
88
89 extern void vm_map_reference(vm_map_t map);
90 extern vm_map_t current_map(void);
91
92 /* Setup reserved areas in a new VM map */
93 extern kern_return_t vm_map_exec(
94 vm_map_t new_map,
95 task_t task,
96 void *fsroot,
97 cpu_type_t cpu);
98
99 __END_DECLS
100
101 #ifdef MACH_KERNEL_PRIVATE
102
103 #include <task_swapper.h>
104 #include <mach_assert.h>
105
106 #include <vm/vm_object.h>
107 #include <vm/vm_page.h>
108 #include <kern/lock.h>
109 #include <kern/zalloc.h>
110 #include <kern/macro_help.h>
111
112 #include <kern/thread.h>
113
114 #define current_map_fast() (current_thread()->map)
115 #define current_map() (current_map_fast())
116
117 #include <vm/vm_map_store.h>
118
119
120 /*
121 * Types defined:
122 *
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
128 */
129 typedef struct vm_map_entry *vm_map_entry_t;
130 #define VM_MAP_ENTRY_NULL ((vm_map_entry_t) 0)
131
132
133 /*
134 * Type: vm_map_object_t [internal use only]
135 *
136 * Description:
137 * The target of an address mapping, either a virtual
138 * memory object or a sub map (of the kernel map).
139 */
140 typedef union vm_map_object {
141 vm_object_t vm_object; /* object object */
142 vm_map_t sub_map; /* belongs to another map */
143 } vm_map_object_t;
144
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(object) lck_mtx_lock(&(object)->Lock)
147 #define named_entry_unlock(object) lck_mtx_unlock(&(object)->Lock)
148
149 /*
150 * Type: vm_named_entry_t [internal use only]
151 *
152 * Description:
153 * Description of a mapping to a memory cache object.
154 *
155 * Implementation:
156 * While the handle to this object is used as a means to map
157 * and pass around the right to map regions backed by pagers
158 * of all sorts, the named_entry itself is only manipulated
159 * by the kernel. Named entries hold information on the
160 * right to map a region of a cached object. Namely,
161 * the target cache object, the beginning and ending of the
162 * region to be mapped, and the permissions, (read, write)
163 * with which it can be mapped.
164 *
165 */
166
167 struct vm_named_entry {
168 decl_lck_mtx_data(, Lock) /* Synchronization */
169 union {
170 vm_object_t object; /* object I point to */
171 memory_object_t pager; /* amo pager port */
172 vm_map_t map; /* map backing submap */
173 } backing;
174 vm_object_offset_t offset; /* offset into object */
175 vm_object_size_t size; /* size of region */
176 vm_prot_t protection; /* access permissions */
177 int ref_count; /* Number of references */
178 unsigned int /* Is backing.xxx : */
179 /* boolean_t */ internal:1, /* ... an internal object */
180 /* boolean_t */ is_sub_map:1, /* ... a submap? */
181 /* boolean_t */ is_pager:1; /* ... a pager port */
182 };
183
184 /*
185 * Type: vm_map_entry_t [internal use only]
186 *
187 * Description:
188 * A single mapping within an address map.
189 *
190 * Implementation:
191 * Address map entries consist of start and end addresses,
192 * a VM object (or sub map) and offset into that object,
193 * and user-exported inheritance and protection information.
194 * Control information for virtual copy operations is also
195 * stored in the address map entry.
196 */
197
198 struct vm_map_links {
199 struct vm_map_entry *prev; /* previous entry */
200 struct vm_map_entry *next; /* next entry */
201 vm_map_offset_t start; /* start address */
202 vm_map_offset_t end; /* end address */
203 };
204
205 struct vm_map_entry {
206 struct vm_map_links links; /* links to other entries */
207 #define vme_prev links.prev
208 #define vme_next links.next
209 #define vme_start links.start
210 #define vme_end links.end
211
212 struct vm_map_store store;
213 union vm_map_object object; /* object I point to */
214 vm_object_offset_t offset; /* offset into object */
215 unsigned int
216 /* boolean_t */ is_shared:1, /* region is shared */
217 /* boolean_t */ is_sub_map:1, /* Is "object" a submap? */
218 /* boolean_t */ in_transition:1, /* Entry being changed */
219 /* boolean_t */ needs_wakeup:1, /* Waiters on in_transition */
220 /* vm_behavior_t */ behavior:2, /* user paging behavior hint */
221 /* behavior is not defined for submap type */
222 /* boolean_t */ needs_copy:1, /* object need to be copied? */
223 /* Only in task maps: */
224 /* vm_prot_t */ protection:3, /* protection code */
225 /* vm_prot_t */ max_protection:3,/* maximum protection */
226 /* vm_inherit_t */ inheritance:2, /* inheritance */
227 /* boolean_t */ use_pmap:1, /* nested pmaps */
228 /*
229 * IMPORTANT:
230 * The "alias" field can be updated while holding the VM map lock
231 * "shared". It's OK as along as it's the only field that can be
232 * updated without the VM map "exclusive" lock.
233 */
234 /* unsigned char */ alias:8, /* user alias */
235 /* boolean_t */ no_cache:1, /* should new pages be cached? */
236 /* boolean_t */ permanent:1, /* mapping can not be removed */
237 /* boolean_t */ superpage_size:3,/* use superpages of a certain size */
238 /* boolean_t */ zero_wired_pages:1, /* zero out the wired pages of this entry it is being deleted without unwiring them */
239 /* boolean_t */ used_for_jit:1,
240 /* boolean_t */ from_reserved_zone:1; /* Allocated from
241 * kernel reserved zone */
242 unsigned short wired_count; /* can be paged if = 0 */
243 unsigned short user_wired_count; /* for vm_wire */
244 };
245
246 /*
247 * Convenience macros for dealing with superpages
248 * SUPERPAGE_NBASEPAGES is architecture dependent and defined in pmap.h
249 */
250 #define SUPERPAGE_SIZE (PAGE_SIZE*SUPERPAGE_NBASEPAGES)
251 #define SUPERPAGE_MASK (-SUPERPAGE_SIZE)
252 #define SUPERPAGE_ROUND_DOWN(a) (a & SUPERPAGE_MASK)
253 #define SUPERPAGE_ROUND_UP(a) ((a + SUPERPAGE_SIZE-1) & SUPERPAGE_MASK)
254
255 /*
256 * wired_counts are unsigned short. This value is used to safeguard
257 * against any mishaps due to runaway user programs.
258 */
259 #define MAX_WIRE_COUNT 65535
260
261
262
263 /*
264 * Type: struct vm_map_header
265 *
266 * Description:
267 * Header for a vm_map and a vm_map_copy.
268 */
269
270
271 struct vm_map_header {
272 struct vm_map_links links; /* first, last, min, max */
273 int nentries; /* Number of entries */
274 boolean_t entries_pageable;
275 /* are map entries pageable? */
276 vm_map_offset_t highest_entry_end_addr; /* The ending address of the highest allocated vm_entry_t */
277 #ifdef VM_MAP_STORE_USE_RB
278 struct rb_head rb_head_store;
279 #endif
280 };
281
282 /*
283 * Type: vm_map_t [exported; contents invisible]
284 *
285 * Description:
286 * An address map -- a directory relating valid
287 * regions of a task's address space to the corresponding
288 * virtual memory objects.
289 *
290 * Implementation:
291 * Maps are doubly-linked lists of map entries, sorted
292 * by address. One hint is used to start
293 * searches again from the last successful search,
294 * insertion, or removal. Another hint is used to
295 * quickly find free space.
296 */
297 struct _vm_map {
298 lock_t lock; /* uni- and smp-lock */
299 struct vm_map_header hdr; /* Map entry header */
300 #define min_offset hdr.links.start /* start of range */
301 #define max_offset hdr.links.end /* end of range */
302 #define highest_entry_end hdr.highest_entry_end_addr
303 pmap_t pmap; /* Physical map */
304 vm_map_size_t size; /* virtual size */
305 vm_map_size_t user_wire_limit;/* rlimit on user locked memory */
306 vm_map_size_t user_wire_size; /* current size of user locked memory in this map */
307 int ref_count; /* Reference count */
308 #if TASK_SWAPPER
309 int res_count; /* Residence count (swap) */
310 int sw_state; /* Swap state */
311 #endif /* TASK_SWAPPER */
312 decl_lck_mtx_data(, s_lock) /* Lock ref, res fields */
313 lck_mtx_ext_t s_lock_ext;
314 vm_map_entry_t hint; /* hint for quick lookups */
315 vm_map_entry_t first_free; /* First free space hint */
316 unsigned int
317 /* boolean_t */ wait_for_space:1, /* Should callers wait for space? */
318 /* boolean_t */ wiring_required:1, /* All memory wired? */
319 /* boolean_t */ no_zero_fill:1, /*No zero fill absent pages */
320 /* boolean_t */ mapped:1, /*has this map been mapped */
321 /* boolean_t */ switch_protect:1, /* Protect map from write faults while switched */
322 /* boolean_t */ disable_vmentry_reuse:1, /* All vm entries should keep using newer and higher addresses in the map */
323 /* boolean_t */ map_disallow_data_exec:1, /* Disallow execution from data pages on exec-permissive architectures */
324 /* reserved */ pad:25;
325 unsigned int timestamp; /* Version number */
326 unsigned int color_rr; /* next color (not protected by a lock) */
327 #if CONFIG_FREEZE
328 void *default_freezer_toc;
329 #endif
330 boolean_t jit_entry_exists;
331 } ;
332
333 #define vm_map_to_entry(map) ((struct vm_map_entry *) &(map)->hdr.links)
334 #define vm_map_first_entry(map) ((map)->hdr.links.next)
335 #define vm_map_last_entry(map) ((map)->hdr.links.prev)
336
337 #if TASK_SWAPPER
338 /*
339 * VM map swap states. There are no transition states.
340 */
341 #define MAP_SW_IN 1 /* map is swapped in; residence count > 0 */
342 #define MAP_SW_OUT 2 /* map is out (res_count == 0 */
343 #endif /* TASK_SWAPPER */
344
345 /*
346 * Type: vm_map_version_t [exported; contents invisible]
347 *
348 * Description:
349 * Map versions may be used to quickly validate a previous
350 * lookup operation.
351 *
352 * Usage note:
353 * Because they are bulky objects, map versions are usually
354 * passed by reference.
355 *
356 * Implementation:
357 * Just a timestamp for the main map.
358 */
359 typedef struct vm_map_version {
360 unsigned int main_timestamp;
361 } vm_map_version_t;
362
363 /*
364 * Type: vm_map_copy_t [exported; contents invisible]
365 *
366 * Description:
367 * A map copy object represents a region of virtual memory
368 * that has been copied from an address map but is still
369 * in transit.
370 *
371 * A map copy object may only be used by a single thread
372 * at a time.
373 *
374 * Implementation:
375 * There are three formats for map copy objects.
376 * The first is very similar to the main
377 * address map in structure, and as a result, some
378 * of the internal maintenance functions/macros can
379 * be used with either address maps or map copy objects.
380 *
381 * The map copy object contains a header links
382 * entry onto which the other entries that represent
383 * the region are chained.
384 *
385 * The second format is a single vm object. This was used
386 * primarily in the pageout path - but is not currently used
387 * except for placeholder copy objects (see vm_map_copy_copy()).
388 *
389 * The third format is a kernel buffer copy object - for data
390 * small enough that physical copies were the most efficient
391 * method.
392 */
393
394 struct vm_map_copy {
395 int type;
396 #define VM_MAP_COPY_ENTRY_LIST 1
397 #define VM_MAP_COPY_OBJECT 2
398 #define VM_MAP_COPY_KERNEL_BUFFER 3
399 vm_object_offset_t offset;
400 vm_map_size_t size;
401 union {
402 struct vm_map_header hdr; /* ENTRY_LIST */
403 vm_object_t object; /* OBJECT */
404 struct {
405 void *kdata; /* KERNEL_BUFFER */
406 vm_size_t kalloc_size; /* size of this copy_t */
407 } c_k;
408 } c_u;
409 };
410
411
412 #define cpy_hdr c_u.hdr
413
414 #define cpy_object c_u.object
415
416 #define cpy_kdata c_u.c_k.kdata
417 #define cpy_kalloc_size c_u.c_k.kalloc_size
418
419
420 /*
421 * Useful macros for entry list copy objects
422 */
423
424 #define vm_map_copy_to_entry(copy) \
425 ((struct vm_map_entry *) &(copy)->cpy_hdr.links)
426 #define vm_map_copy_first_entry(copy) \
427 ((copy)->cpy_hdr.links.next)
428 #define vm_map_copy_last_entry(copy) \
429 ((copy)->cpy_hdr.links.prev)
430
431 /*
432 * Macros: vm_map_lock, etc. [internal use only]
433 * Description:
434 * Perform locking on the data portion of a map.
435 * When multiple maps are to be locked, order by map address.
436 * (See vm_map.c::vm_remap())
437 */
438
439 #define vm_map_lock_init(map) \
440 ((map)->timestamp = 0 , \
441 lock_init(&(map)->lock, TRUE, 0, 0))
442
443 #define vm_map_lock(map) lock_write(&(map)->lock)
444 #define vm_map_unlock(map) \
445 ((map)->timestamp++ , lock_write_done(&(map)->lock))
446 #define vm_map_lock_read(map) lock_read(&(map)->lock)
447 #define vm_map_unlock_read(map) lock_read_done(&(map)->lock)
448 #define vm_map_lock_write_to_read(map) \
449 ((map)->timestamp++ , lock_write_to_read(&(map)->lock))
450 /* lock_read_to_write() returns FALSE on failure. Macro evaluates to
451 * zero on success and non-zero value on failure.
452 */
453 #define vm_map_lock_read_to_write(map) (lock_read_to_write(&(map)->lock) != TRUE)
454
455 /*
456 * Exported procedures that operate on vm_map_t.
457 */
458
459 /* Initialize the module */
460 extern void vm_map_init(void) __attribute__((section("__TEXT, initcode")));
461
462 extern void vm_kernel_reserved_entry_init(void) __attribute__((section("__TEXT, initcode")));
463
464 /* Allocate a range in the specified virtual address map and
465 * return the entry allocated for that range. */
466 extern kern_return_t vm_map_find_space(
467 vm_map_t map,
468 vm_map_address_t *address, /* OUT */
469 vm_map_size_t size,
470 vm_map_offset_t mask,
471 int flags,
472 vm_map_entry_t *o_entry); /* OUT */
473
474 extern void vm_map_clip_start(
475 vm_map_t map,
476 vm_map_entry_t entry,
477 vm_map_offset_t endaddr);
478 extern void vm_map_clip_end(
479 vm_map_t map,
480 vm_map_entry_t entry,
481 vm_map_offset_t endaddr);
482 #if !CONFIG_EMBEDDED
483 extern boolean_t vm_map_entry_should_cow_for_true_share(
484 vm_map_entry_t entry);
485 #endif /* !CONFIG_EMBEDDED */
486
487 /* Lookup map entry containing or the specified address in the given map */
488 extern boolean_t vm_map_lookup_entry(
489 vm_map_t map,
490 vm_map_address_t address,
491 vm_map_entry_t *entry); /* OUT */
492
493 /* Find the VM object, offset, and protection for a given virtual address
494 * in the specified map, assuming a page fault of the type specified. */
495 extern kern_return_t vm_map_lookup_locked(
496 vm_map_t *var_map, /* IN/OUT */
497 vm_map_address_t vaddr,
498 vm_prot_t fault_type,
499 int object_lock_type,
500 vm_map_version_t *out_version, /* OUT */
501 vm_object_t *object, /* OUT */
502 vm_object_offset_t *offset, /* OUT */
503 vm_prot_t *out_prot, /* OUT */
504 boolean_t *wired, /* OUT */
505 vm_object_fault_info_t fault_info, /* OUT */
506 vm_map_t *real_map); /* OUT */
507
508 /* Verifies that the map has not changed since the given version. */
509 extern boolean_t vm_map_verify(
510 vm_map_t map,
511 vm_map_version_t *version); /* REF */
512
513 extern vm_map_entry_t vm_map_entry_insert(
514 vm_map_t map,
515 vm_map_entry_t insp_entry,
516 vm_map_offset_t start,
517 vm_map_offset_t end,
518 vm_object_t object,
519 vm_object_offset_t offset,
520 boolean_t needs_copy,
521 boolean_t is_shared,
522 boolean_t in_transition,
523 vm_prot_t cur_protection,
524 vm_prot_t max_protection,
525 vm_behavior_t behavior,
526 vm_inherit_t inheritance,
527 unsigned wired_count,
528 boolean_t no_cache,
529 boolean_t permanent,
530 unsigned int superpage_size);
531
532
533 /*
534 * Functions implemented as macros
535 */
536 #define vm_map_min(map) ((map)->min_offset)
537 /* Lowest valid address in
538 * a map */
539
540 #define vm_map_max(map) ((map)->max_offset)
541 /* Highest valid address */
542
543 #define vm_map_pmap(map) ((map)->pmap)
544 /* Physical map associated
545 * with this address map */
546
547 #define vm_map_verify_done(map, version) vm_map_unlock_read(map)
548 /* Operation that required
549 * a verified lookup is
550 * now complete */
551
552 /*
553 * Macros/functions for map residence counts and swapin/out of vm maps
554 */
555 #if TASK_SWAPPER
556
557 #if MACH_ASSERT
558 /* Gain a reference to an existing map */
559 extern void vm_map_reference(
560 vm_map_t map);
561 /* Lose a residence count */
562 extern void vm_map_res_deallocate(
563 vm_map_t map);
564 /* Gain a residence count on a map */
565 extern void vm_map_res_reference(
566 vm_map_t map);
567 /* Gain reference & residence counts to possibly swapped-out map */
568 extern void vm_map_reference_swap(
569 vm_map_t map);
570
571 #else /* MACH_ASSERT */
572
573 #define vm_map_reference(map) \
574 MACRO_BEGIN \
575 vm_map_t Map = (map); \
576 if (Map) { \
577 lck_mtx_lock(&Map->s_lock); \
578 Map->res_count++; \
579 Map->ref_count++; \
580 lck_mtx_unlock(&Map->s_lock); \
581 } \
582 MACRO_END
583
584 #define vm_map_res_reference(map) \
585 MACRO_BEGIN \
586 vm_map_t Lmap = (map); \
587 if (Lmap->res_count == 0) { \
588 lck_mtx_unlock(&Lmap->s_lock);\
589 vm_map_lock(Lmap); \
590 vm_map_swapin(Lmap); \
591 lck_mtx_lock(&Lmap->s_lock); \
592 ++Lmap->res_count; \
593 vm_map_unlock(Lmap); \
594 } else \
595 ++Lmap->res_count; \
596 MACRO_END
597
598 #define vm_map_res_deallocate(map) \
599 MACRO_BEGIN \
600 vm_map_t Map = (map); \
601 if (--Map->res_count == 0) { \
602 lck_mtx_unlock(&Map->s_lock); \
603 vm_map_lock(Map); \
604 vm_map_swapout(Map); \
605 vm_map_unlock(Map); \
606 lck_mtx_lock(&Map->s_lock); \
607 } \
608 MACRO_END
609
610 #define vm_map_reference_swap(map) \
611 MACRO_BEGIN \
612 vm_map_t Map = (map); \
613 lck_mtx_lock(&Map->s_lock); \
614 ++Map->ref_count; \
615 vm_map_res_reference(Map); \
616 lck_mtx_unlock(&Map->s_lock); \
617 MACRO_END
618 #endif /* MACH_ASSERT */
619
620 extern void vm_map_swapin(
621 vm_map_t map);
622
623 extern void vm_map_swapout(
624 vm_map_t map);
625
626 #else /* TASK_SWAPPER */
627
628 #define vm_map_reference(map) \
629 MACRO_BEGIN \
630 vm_map_t Map = (map); \
631 if (Map) { \
632 lck_mtx_lock(&Map->s_lock); \
633 Map->ref_count++; \
634 lck_mtx_unlock(&Map->s_lock); \
635 } \
636 MACRO_END
637
638 #define vm_map_reference_swap(map) vm_map_reference(map)
639 #define vm_map_res_reference(map)
640 #define vm_map_res_deallocate(map)
641
642 #endif /* TASK_SWAPPER */
643
644 /*
645 * Submap object. Must be used to create memory to be put
646 * in a submap by vm_map_submap.
647 */
648 extern vm_object_t vm_submap_object;
649
650 /*
651 * Wait and wakeup macros for in_transition map entries.
652 */
653 #define vm_map_entry_wait(map, interruptible) \
654 ((map)->timestamp++ , \
655 thread_sleep_lock_write((event_t)&(map)->hdr, \
656 &(map)->lock, interruptible))
657
658
659 #define vm_map_entry_wakeup(map) \
660 thread_wakeup((event_t)(&(map)->hdr))
661
662
663 #define vm_map_ref_fast(map) \
664 MACRO_BEGIN \
665 lck_mtx_lock(&map->s_lock); \
666 map->ref_count++; \
667 vm_map_res_reference(map); \
668 lck_mtx_unlock(&map->s_lock); \
669 MACRO_END
670
671 #define vm_map_dealloc_fast(map) \
672 MACRO_BEGIN \
673 register int c; \
674 \
675 lck_mtx_lock(&map->s_lock); \
676 c = --map->ref_count; \
677 if (c > 0) \
678 vm_map_res_deallocate(map); \
679 lck_mtx_unlock(&map->s_lock); \
680 if (c == 0) \
681 vm_map_destroy(map); \
682 MACRO_END
683
684
685 /* simplify map entries */
686 extern void vm_map_simplify_entry(
687 vm_map_t map,
688 vm_map_entry_t this_entry);
689 extern void vm_map_simplify(
690 vm_map_t map,
691 vm_map_offset_t start);
692
693 /* Move the information in a map copy object to a new map copy object */
694 extern vm_map_copy_t vm_map_copy_copy(
695 vm_map_copy_t copy);
696
697 /* Create a copy object from an object. */
698 extern kern_return_t vm_map_copyin_object(
699 vm_object_t object,
700 vm_object_offset_t offset,
701 vm_object_size_t size,
702 vm_map_copy_t *copy_result); /* OUT */
703
704 /* Enter a mapping */
705 extern kern_return_t vm_map_enter(
706 vm_map_t map,
707 vm_map_offset_t *address,
708 vm_map_size_t size,
709 vm_map_offset_t mask,
710 int flags,
711 vm_object_t object,
712 vm_object_offset_t offset,
713 boolean_t needs_copy,
714 vm_prot_t cur_protection,
715 vm_prot_t max_protection,
716 vm_inherit_t inheritance);
717
718 /* XXX should go away - replaced with regular enter of contig object */
719 extern kern_return_t vm_map_enter_cpm(
720 vm_map_t map,
721 vm_map_address_t *addr,
722 vm_map_size_t size,
723 int flags);
724
725 extern kern_return_t vm_map_remap(
726 vm_map_t target_map,
727 vm_map_offset_t *address,
728 vm_map_size_t size,
729 vm_map_offset_t mask,
730 int flags,
731 vm_map_t src_map,
732 vm_map_offset_t memory_address,
733 boolean_t copy,
734 vm_prot_t *cur_protection,
735 vm_prot_t *max_protection,
736 vm_inherit_t inheritance);
737
738
739 /*
740 * Read and write from a kernel buffer to a specified map.
741 */
742 extern kern_return_t vm_map_write_user(
743 vm_map_t map,
744 void *src_p,
745 vm_map_offset_t dst_addr,
746 vm_size_t size);
747
748 extern kern_return_t vm_map_read_user(
749 vm_map_t map,
750 vm_map_offset_t src_addr,
751 void *dst_p,
752 vm_size_t size);
753
754 /* Create a new task map using an existing task map as a template. */
755 extern vm_map_t vm_map_fork(
756 vm_map_t old_map);
757
758 /* Change inheritance */
759 extern kern_return_t vm_map_inherit(
760 vm_map_t map,
761 vm_map_offset_t start,
762 vm_map_offset_t end,
763 vm_inherit_t new_inheritance);
764
765 /* Add or remove machine-dependent attributes from map regions */
766 extern kern_return_t vm_map_machine_attribute(
767 vm_map_t map,
768 vm_map_offset_t start,
769 vm_map_offset_t end,
770 vm_machine_attribute_t attribute,
771 vm_machine_attribute_val_t* value); /* IN/OUT */
772
773 extern kern_return_t vm_map_msync(
774 vm_map_t map,
775 vm_map_address_t address,
776 vm_map_size_t size,
777 vm_sync_t sync_flags);
778
779 /* Set paging behavior */
780 extern kern_return_t vm_map_behavior_set(
781 vm_map_t map,
782 vm_map_offset_t start,
783 vm_map_offset_t end,
784 vm_behavior_t new_behavior);
785
786 extern kern_return_t vm_map_purgable_control(
787 vm_map_t map,
788 vm_map_offset_t address,
789 vm_purgable_t control,
790 int *state);
791
792 extern kern_return_t vm_map_region(
793 vm_map_t map,
794 vm_map_offset_t *address,
795 vm_map_size_t *size,
796 vm_region_flavor_t flavor,
797 vm_region_info_t info,
798 mach_msg_type_number_t *count,
799 mach_port_t *object_name);
800
801 extern kern_return_t vm_map_region_recurse_64(
802 vm_map_t map,
803 vm_map_offset_t *address,
804 vm_map_size_t *size,
805 natural_t *nesting_depth,
806 vm_region_submap_info_64_t info,
807 mach_msg_type_number_t *count);
808
809 extern kern_return_t vm_map_page_query_internal(
810 vm_map_t map,
811 vm_map_offset_t offset,
812 int *disposition,
813 int *ref_count);
814
815
816 extern kern_return_t vm_map_submap(
817 vm_map_t map,
818 vm_map_offset_t start,
819 vm_map_offset_t end,
820 vm_map_t submap,
821 vm_map_offset_t offset,
822 boolean_t use_pmap);
823
824 extern void vm_map_submap_pmap_clean(
825 vm_map_t map,
826 vm_map_offset_t start,
827 vm_map_offset_t end,
828 vm_map_t sub_map,
829 vm_map_offset_t offset);
830
831 /* Convert from a map entry port to a map */
832 extern vm_map_t convert_port_entry_to_map(
833 ipc_port_t port);
834
835 /* Convert from a port to a vm_object */
836 extern vm_object_t convert_port_entry_to_object(
837 ipc_port_t port);
838
839
840 extern kern_return_t vm_map_set_cache_attr(
841 vm_map_t map,
842 vm_map_offset_t va);
843
844
845 /* definitions related to overriding the NX behavior */
846
847 #define VM_ABI_32 0x1
848 #define VM_ABI_64 0x2
849
850 extern int override_nx(vm_map_t map, uint32_t user_tag);
851
852 #endif /* MACH_KERNEL_PRIVATE */
853
854 __BEGIN_DECLS
855
856 /* Create an empty map */
857 extern vm_map_t vm_map_create(
858 pmap_t pmap,
859 vm_map_offset_t min_off,
860 vm_map_offset_t max_off,
861 boolean_t pageable);
862
863 /* Get rid of a map */
864 extern void vm_map_destroy(
865 vm_map_t map,
866 int flags);
867
868 /* Lose a reference */
869 extern void vm_map_deallocate(
870 vm_map_t map);
871
872 extern vm_map_t vm_map_switch(
873 vm_map_t map);
874
875 /* Change protection */
876 extern kern_return_t vm_map_protect(
877 vm_map_t map,
878 vm_map_offset_t start,
879 vm_map_offset_t end,
880 vm_prot_t new_prot,
881 boolean_t set_max);
882
883 /* Check protection */
884 extern boolean_t vm_map_check_protection(
885 vm_map_t map,
886 vm_map_offset_t start,
887 vm_map_offset_t end,
888 vm_prot_t protection);
889
890 /* wire down a region */
891 extern kern_return_t vm_map_wire(
892 vm_map_t map,
893 vm_map_offset_t start,
894 vm_map_offset_t end,
895 vm_prot_t access_type,
896 boolean_t user_wire);
897
898 /* unwire a region */
899 extern kern_return_t vm_map_unwire(
900 vm_map_t map,
901 vm_map_offset_t start,
902 vm_map_offset_t end,
903 boolean_t user_wire);
904
905 /* Enter a mapping of a memory object */
906 extern kern_return_t vm_map_enter_mem_object(
907 vm_map_t map,
908 vm_map_offset_t *address,
909 vm_map_size_t size,
910 vm_map_offset_t mask,
911 int flags,
912 ipc_port_t port,
913 vm_object_offset_t offset,
914 boolean_t needs_copy,
915 vm_prot_t cur_protection,
916 vm_prot_t max_protection,
917 vm_inherit_t inheritance);
918
919 /* Enter a mapping of a memory object */
920 extern kern_return_t vm_map_enter_mem_object_control(
921 vm_map_t map,
922 vm_map_offset_t *address,
923 vm_map_size_t size,
924 vm_map_offset_t mask,
925 int flags,
926 memory_object_control_t control,
927 vm_object_offset_t offset,
928 boolean_t needs_copy,
929 vm_prot_t cur_protection,
930 vm_prot_t max_protection,
931 vm_inherit_t inheritance);
932
933 /* Deallocate a region */
934 extern kern_return_t vm_map_remove(
935 vm_map_t map,
936 vm_map_offset_t start,
937 vm_map_offset_t end,
938 boolean_t flags);
939
940 /* Discard a copy without using it */
941 extern void vm_map_copy_discard(
942 vm_map_copy_t copy);
943
944 /* Overwrite existing memory with a copy */
945 extern kern_return_t vm_map_copy_overwrite(
946 vm_map_t dst_map,
947 vm_map_address_t dst_addr,
948 vm_map_copy_t copy,
949 boolean_t interruptible);
950
951 /* Place a copy into a map */
952 extern kern_return_t vm_map_copyout(
953 vm_map_t dst_map,
954 vm_map_address_t *dst_addr, /* OUT */
955 vm_map_copy_t copy);
956
957 extern kern_return_t vm_map_copyin(
958 vm_map_t src_map,
959 vm_map_address_t src_addr,
960 vm_map_size_t len,
961 boolean_t src_destroy,
962 vm_map_copy_t *copy_result); /* OUT */
963
964 extern kern_return_t vm_map_copyin_common(
965 vm_map_t src_map,
966 vm_map_address_t src_addr,
967 vm_map_size_t len,
968 boolean_t src_destroy,
969 boolean_t src_volatile,
970 vm_map_copy_t *copy_result, /* OUT */
971 boolean_t use_maxprot);
972
973 extern void vm_map_disable_NX(
974 vm_map_t map);
975
976 extern void vm_map_disallow_data_exec(
977 vm_map_t map);
978
979 extern void vm_map_set_64bit(
980 vm_map_t map);
981
982 extern void vm_map_set_32bit(
983 vm_map_t map);
984
985 extern boolean_t vm_map_is_64bit(
986 vm_map_t map);
987
988 extern boolean_t vm_map_has_4GB_pagezero(
989 vm_map_t map);
990
991 extern void vm_map_set_4GB_pagezero(
992 vm_map_t map);
993
994 extern void vm_map_clear_4GB_pagezero(
995 vm_map_t map);
996
997 extern kern_return_t vm_map_raise_min_offset(
998 vm_map_t map,
999 vm_map_offset_t new_min_offset);
1000
1001 extern vm_map_offset_t vm_compute_max_offset(
1002 unsigned is64);
1003
1004 extern void vm_map_set_user_wire_limit(
1005 vm_map_t map,
1006 vm_size_t limit);
1007
1008 extern void vm_map_switch_protect(
1009 vm_map_t map,
1010 boolean_t val);
1011
1012 extern boolean_t first_free_is_valid(vm_map_t);
1013
1014 #ifdef XNU_KERNEL_PRIVATE
1015 extern kern_return_t vm_map_page_info(
1016 vm_map_t map,
1017 vm_map_offset_t offset,
1018 vm_page_info_flavor_t flavor,
1019 vm_page_info_t info,
1020 mach_msg_type_number_t *count);
1021 #endif /* XNU_KERNEL_PRIVATE */
1022
1023
1024 #ifdef MACH_KERNEL_PRIVATE
1025
1026 /*
1027 * Macros to invoke vm_map_copyin_common. vm_map_copyin is the
1028 * usual form; it handles a copyin based on the current protection
1029 * (current protection == VM_PROT_NONE) is a failure.
1030 * vm_map_copyin_maxprot handles a copyin based on maximum possible
1031 * access. The difference is that a region with no current access
1032 * BUT possible maximum access is rejected by vm_map_copyin(), but
1033 * returned by vm_map_copyin_maxprot.
1034 */
1035 #define vm_map_copyin(src_map, src_addr, len, src_destroy, copy_result) \
1036 vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
1037 FALSE, copy_result, FALSE)
1038
1039 #define vm_map_copyin_maxprot(src_map, \
1040 src_addr, len, src_destroy, copy_result) \
1041 vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
1042 FALSE, copy_result, TRUE)
1043
1044 #endif /* MACH_KERNEL_PRIVATE */
1045
1046 /*
1047 * Macros for rounding and truncation of vm_map offsets and sizes
1048 */
1049 #define vm_map_round_page(x) (((vm_map_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
1050 #define vm_map_trunc_page(x) ((vm_map_offset_t)(x) & ~((signed)PAGE_MASK))
1051
1052 /*
1053 * Flags for vm_map_remove() and vm_map_delete()
1054 */
1055 #define VM_MAP_NO_FLAGS 0x0
1056 #define VM_MAP_REMOVE_KUNWIRE 0x1
1057 #define VM_MAP_REMOVE_INTERRUPTIBLE 0x2
1058 #define VM_MAP_REMOVE_WAIT_FOR_KWIRE 0x4
1059 #define VM_MAP_REMOVE_SAVE_ENTRIES 0x8
1060 #define VM_MAP_REMOVE_NO_PMAP_CLEANUP 0x10
1061
1062 /* Support for UPLs from vm_maps */
1063
1064 extern kern_return_t vm_map_get_upl(
1065 vm_map_t target_map,
1066 vm_map_offset_t map_offset,
1067 upl_size_t *size,
1068 upl_t *upl,
1069 upl_page_info_array_t page_info,
1070 unsigned int *page_infoCnt,
1071 int *flags,
1072 int force_data_sync);
1073
1074 #if CONFIG_DYNAMIC_CODE_SIGNING
1075 extern kern_return_t vm_map_sign(vm_map_t map,
1076 vm_map_offset_t start,
1077 vm_map_offset_t end);
1078 #endif
1079
1080 #if CONFIG_FREEZE
1081 extern kern_return_t vm_map_freeze_walk(
1082 vm_map_t map,
1083 unsigned int *purgeable_count,
1084 unsigned int *wired_count,
1085 unsigned int *clean_count,
1086 unsigned int *dirty_count,
1087 boolean_t *has_shared);
1088
1089 extern kern_return_t vm_map_freeze(
1090 vm_map_t map,
1091 unsigned int *purgeable_count,
1092 unsigned int *wired_count,
1093 unsigned int *clean_count,
1094 unsigned int *dirty_count,
1095 boolean_t *has_shared);
1096
1097 extern void vm_map_thaw(
1098 vm_map_t map);
1099 #endif
1100
1101 __END_DECLS
1102
1103 #endif /* KERNEL_PRIVATE */
1104
1105 #endif /* _VM_VM_MAP_H_ */