]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_map.h
xnu-123.5.tar.gz
[apple/xnu.git] / osfmk / vm / vm_map.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52
53 /*
54 * File: vm/vm_map.h
55 * Author: Avadis Tevanian, Jr., Michael Wayne Young
56 * Date: 1985
57 *
58 * Virtual memory map module definitions.
59 *
60 * Contributors:
61 * avie, dlb, mwyoung
62 */
63
64 #ifndef _VM_VM_MAP_H_
65 #define _VM_VM_MAP_H_
66
67 #include <mach/mach_types.h>
68 #include <mach/kern_return.h>
69 #include <mach/boolean.h>
70 #include <mach/vm_types.h>
71 #include <mach/vm_prot.h>
72 #include <mach/vm_inherit.h>
73 #include <mach/vm_behavior.h>
74 #include <vm/pmap.h>
75
76 typedef struct vm_map_entry *vm_map_entry_t;
77
78 extern void kernel_vm_map_reference(vm_map_t map);
79
80 #ifndef MACH_KERNEL_PRIVATE
81
82 struct vm_map_entry {};
83
84 extern void vm_map_reference(vm_map_t map);
85 extern vm_map_t current_map(void);
86
87 #else /* MACH_KERNEL_PRIVATE */
88
89 #include <cpus.h>
90 #include <task_swapper.h>
91 #include <mach_assert.h>
92
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
95 #include <kern/lock.h>
96 #include <kern/zalloc.h>
97 #include <kern/macro_help.h>
98
99 #define shared_region_mapping_lock_init(object) \
100 mutex_init(&(object)->Lock, ETAP_VM_OBJ)
101 #define shared_region_mapping_lock(object) mutex_lock(&(object)->Lock)
102 #define shared_region_mapping_unlock(object) mutex_unlock(&(object)->Lock)
103 #include <kern/thread_act.h>
104
105 #define current_map_fast() (current_act_fast()->map)
106 #define current_map() (current_map_fast())
107
108 /*
109 * Types defined:
110 *
111 * vm_map_t the high-level address map data structure.
112 * vm_map_entry_t an entry in an address map.
113 * vm_map_version_t a timestamp of a map, for use with vm_map_lookup
114 * vm_map_copy_t represents memory copied from an address map,
115 * used for inter-map copy operations
116 */
117
118 /*
119 * Type: vm_map_object_t [internal use only]
120 *
121 * Description:
122 * The target of an address mapping, either a virtual
123 * memory object or a sub map (of the kernel map).
124 */
125 typedef union vm_map_object {
126 struct vm_object *vm_object; /* object object */
127 struct vm_map *sub_map; /* belongs to another map */
128 } vm_map_object_t;
129
130 #define named_entry_lock_init(object) mutex_init(&(object)->Lock, ETAP_VM_OBJ)
131 #define named_entry_lock(object) mutex_lock(&(object)->Lock)
132 #define named_entry_unlock(object) mutex_unlock(&(object)->Lock)
133
134 /*
135 * Type: vm_named_entry_t [internal use only]
136 *
137 * Description:
138 * Description of a mapping to a memory cache object.
139 *
140 * Implementation:
141 * While the handle to this object is used as a means to map
142 * and pass around the right to map regions backed by pagers
143 * of all sorts, the named_entry itself is only manipulated
144 * by the kernel. Named entries hold information on the
145 * right to map a region of a cached object. Namely,
146 * the target cache object, the beginning and ending of the
147 * region to be mapped, and the permissions, (read, write)
148 * with which it can be mapped.
149 *
150 */
151
152 struct vm_named_entry {
153 decl_mutex_data(, Lock) /* Synchronization */
154 vm_object_t object; /* object I point to */
155 vm_object_offset_t offset; /* offset into object */
156 union {
157 ipc_port_t pager; /* amo pager port */
158 vm_map_t map; /* map backing submap */
159 } backing;
160 unsigned int size; /* size of region */
161 unsigned int protection; /* access permissions */
162 int ref_count; /* Number of references */
163 unsigned int
164 /* boolean_t */ internal:1, /* is an internal object */
165 /* boolean_t */ is_sub_map:1; /* is object is a submap? */
166 };
167
168 typedef struct vm_named_entry *vm_named_entry_t;
169
170
171 /*
172 * Type: vm_map_entry_t [internal use only]
173 *
174 * Description:
175 * A single mapping within an address map.
176 *
177 * Implementation:
178 * Address map entries consist of start and end addresses,
179 * a VM object (or sub map) and offset into that object,
180 * and user-exported inheritance and protection information.
181 * Control information for virtual copy operations is also
182 * stored in the address map entry.
183 */
184 struct vm_map_links {
185 struct vm_map_entry *prev; /* previous entry */
186 struct vm_map_entry *next; /* next entry */
187 vm_offset_t start; /* start address */
188 vm_offset_t end; /* end address */
189 };
190
191 struct vm_map_entry {
192 struct vm_map_links links; /* links to other entries */
193 #define vme_prev links.prev
194 #define vme_next links.next
195 #define vme_start links.start
196 #define vme_end links.end
197 union vm_map_object object; /* object I point to */
198 vm_object_offset_t offset; /* offset into object */
199 unsigned int
200 /* boolean_t */ is_shared:1, /* region is shared */
201 /* boolean_t */ is_sub_map:1, /* Is "object" a submap? */
202 /* boolean_t */ in_transition:1, /* Entry being changed */
203 /* boolean_t */ needs_wakeup:1, /* Waiters on in_transition */
204 /* vm_behavior_t */ behavior:2, /* user paging behavior hint */
205 /* behavior is not defined for submap type */
206 /* boolean_t */ needs_copy:1, /* object need to be copied? */
207 /* Only in task maps: */
208 /* vm_prot_t */ protection:3, /* protection code */
209 /* vm_prot_t */ max_protection:3,/* maximum protection */
210 /* vm_inherit_t */ inheritance:2, /* inheritance */
211 /* nested pmap */ use_pmap:1, /* nested pmaps */
212 /* user alias */ alias:8;
213 unsigned short wired_count; /* can be paged if = 0 */
214 unsigned short user_wired_count; /* for vm_wire */
215 };
216
217 /*
218 * wired_counts are unsigned short. This value is used to safeguard
219 * against any mishaps due to runaway user programs.
220 */
221 #define MAX_WIRE_COUNT 65535
222
223
224
225 /*
226 * Type: struct vm_map_header
227 *
228 * Description:
229 * Header for a vm_map and a vm_map_copy.
230 */
231 struct vm_map_header {
232 struct vm_map_links links; /* first, last, min, max */
233 int nentries; /* Number of entries */
234 boolean_t entries_pageable;
235 /* are map entries pageable? */
236 };
237
238 /*
239 * Type: vm_map_t [exported; contents invisible]
240 *
241 * Description:
242 * An address map -- a directory relating valid
243 * regions of a task's address space to the corresponding
244 * virtual memory objects.
245 *
246 * Implementation:
247 * Maps are doubly-linked lists of map entries, sorted
248 * by address. One hint is used to start
249 * searches again from the last successful search,
250 * insertion, or removal. Another hint is used to
251 * quickly find free space.
252 */
253 struct vm_map {
254 lock_t lock; /* uni- and smp-lock */
255 struct vm_map_header hdr; /* Map entry header */
256 #define min_offset hdr.links.start /* start of range */
257 #define max_offset hdr.links.end /* end of range */
258 pmap_t pmap; /* Physical map */
259 vm_size_t size; /* virtual size */
260 int ref_count; /* Reference count */
261 #if TASK_SWAPPER
262 int res_count; /* Residence count (swap) */
263 int sw_state; /* Swap state */
264 #endif /* TASK_SWAPPER */
265 decl_mutex_data(, s_lock) /* Lock ref, res, hint fields */
266 vm_map_entry_t hint; /* hint for quick lookups */
267 vm_map_entry_t first_free; /* First free space hint */
268 boolean_t wait_for_space; /* Should callers wait
269 for space? */
270 boolean_t wiring_required;/* All memory wired? */
271 boolean_t no_zero_fill; /* No zero fill absent pages */
272 unsigned int timestamp; /* Version number */
273 } ;
274
275 #define vm_map_to_entry(map) ((struct vm_map_entry *) &(map)->hdr.links)
276 #define vm_map_first_entry(map) ((map)->hdr.links.next)
277 #define vm_map_last_entry(map) ((map)->hdr.links.prev)
278
279 #if TASK_SWAPPER
280 /*
281 * VM map swap states. There are no transition states.
282 */
283 #define MAP_SW_IN 1 /* map is swapped in; residence count > 0 */
284 #define MAP_SW_OUT 2 /* map is out (res_count == 0 */
285 #endif /* TASK_SWAPPER */
286
287 /*
288 * Type: vm_map_version_t [exported; contents invisible]
289 *
290 * Description:
291 * Map versions may be used to quickly validate a previous
292 * lookup operation.
293 *
294 * Usage note:
295 * Because they are bulky objects, map versions are usually
296 * passed by reference.
297 *
298 * Implementation:
299 * Just a timestamp for the main map.
300 */
301 typedef struct vm_map_version {
302 unsigned int main_timestamp;
303 } vm_map_version_t;
304
305 /*
306 * Type: vm_map_copy_t [exported; contents invisible]
307 *
308 * Description:
309 * A map copy object represents a region of virtual memory
310 * that has been copied from an address map but is still
311 * in transit.
312 *
313 * A map copy object may only be used by a single thread
314 * at a time.
315 *
316 * Implementation:
317 * There are three formats for map copy objects.
318 * The first is very similar to the main
319 * address map in structure, and as a result, some
320 * of the internal maintenance functions/macros can
321 * be used with either address maps or map copy objects.
322 *
323 * The map copy object contains a header links
324 * entry onto which the other entries that represent
325 * the region are chained.
326 *
327 * The second format is a single vm object. This is used
328 * primarily in the pageout path. The third format is a
329 * list of vm pages. An optional continuation provides
330 * a hook to be called to obtain more of the memory,
331 * or perform other operations. The continuation takes 3
332 * arguments, a saved arg buffer, a pointer to a new vm_map_copy
333 * (returned) and an abort flag (abort if TRUE).
334 */
335
336 #define VM_MAP_COPY_PAGE_LIST_MAX 20
337 #define VM_MAP_COPY_PAGE_LIST_MAX_SIZE (VM_MAP_COPY_PAGE_LIST_MAX * PAGE_SIZE)
338
339
340 /*
341 * Options for vm_map_copyin_page_list.
342 */
343
344 #define VM_MAP_COPYIN_OPT_VM_PROT 0x7
345 #define VM_MAP_COPYIN_OPT_SRC_DESTROY 0x8
346 #define VM_MAP_COPYIN_OPT_STEAL_PAGES 0x10
347 #define VM_MAP_COPYIN_OPT_PMAP_ENTER 0x20
348 #define VM_MAP_COPYIN_OPT_NO_ZERO_FILL 0x40
349
350 /*
351 * Continuation structures for vm_map_copyin_page_list.
352 */
353 typedef struct {
354 vm_map_t map;
355 vm_offset_t src_addr;
356 vm_size_t src_len;
357 vm_offset_t destroy_addr;
358 vm_size_t destroy_len;
359 int options;
360 } vm_map_copyin_args_data_t, *vm_map_copyin_args_t;
361
362 #define VM_MAP_COPYIN_ARGS_NULL ((vm_map_copyin_args_t) 0)
363
364
365 /* vm_map_copy_cont_t is a type definition/prototype
366 * for the cont function pointer in vm_map_copy structure.
367 */
368 typedef kern_return_t (*vm_map_copy_cont_t)(
369 vm_map_copyin_args_t,
370 vm_map_copy_t *);
371
372 #define VM_MAP_COPY_CONT_NULL ((vm_map_copy_cont_t) 0)
373
374 struct vm_map_copy {
375 int type;
376 #define VM_MAP_COPY_ENTRY_LIST 1
377 #define VM_MAP_COPY_OBJECT 2
378 #define VM_MAP_COPY_PAGE_LIST 3
379 #define VM_MAP_COPY_KERNEL_BUFFER 4
380 vm_object_offset_t offset;
381 vm_size_t size;
382 union {
383 struct vm_map_header hdr; /* ENTRY_LIST */
384 struct { /* OBJECT */
385 vm_object_t object;
386 vm_size_t index; /* record progress as pages
387 * are moved from object to
388 * page list; must be zero
389 * when first invoking
390 * vm_map_object_to_page_list
391 */
392 } c_o;
393 struct { /* PAGE_LIST */
394 int npages;
395 boolean_t page_loose;
396 vm_map_copy_cont_t cont;
397 vm_map_copyin_args_t cont_args;
398 vm_page_t page_list[VM_MAP_COPY_PAGE_LIST_MAX];
399 } c_p;
400 struct { /* KERNEL_BUFFER */
401 vm_offset_t kdata;
402 vm_size_t kalloc_size; /* size of this copy_t */
403 } c_k;
404 } c_u;
405 };
406
407
408 #define cpy_hdr c_u.hdr
409
410 #define cpy_object c_u.c_o.object
411 #define cpy_index c_u.c_o.index
412
413 #define cpy_page_list c_u.c_p.page_list
414 #define cpy_npages c_u.c_p.npages
415 #define cpy_page_loose c_u.c_p.page_loose
416 #define cpy_cont c_u.c_p.cont
417 #define cpy_cont_args c_u.c_p.cont_args
418
419 #define cpy_kdata c_u.c_k.kdata
420 #define cpy_kalloc_size c_u.c_k.kalloc_size
421
422
423 /*
424 * Useful macros for entry list copy objects
425 */
426
427 #define vm_map_copy_to_entry(copy) \
428 ((struct vm_map_entry *) &(copy)->cpy_hdr.links)
429 #define vm_map_copy_first_entry(copy) \
430 ((copy)->cpy_hdr.links.next)
431 #define vm_map_copy_last_entry(copy) \
432 ((copy)->cpy_hdr.links.prev)
433
434 /*
435 * Continuation macros for page list copy objects
436 */
437
438 #define vm_map_copy_invoke_cont(old_copy, new_copy, result) \
439 MACRO_BEGIN \
440 assert(vm_map_copy_cont_is_valid(old_copy)); \
441 vm_map_copy_page_discard(old_copy); \
442 *result = (*((old_copy)->cpy_cont))((old_copy)->cpy_cont_args, \
443 new_copy); \
444 (old_copy)->cpy_cont = VM_MAP_COPY_CONT_NULL; \
445 MACRO_END
446
447 #define vm_map_copy_invoke_extend_cont(old_copy, new_copy, result) \
448 MACRO_BEGIN \
449 assert(vm_map_copy_cont_is_valid(old_copy)); \
450 *result = (*((old_copy)->cpy_cont))((old_copy)->cpy_cont_args, \
451 new_copy); \
452 (old_copy)->cpy_cont = VM_MAP_COPY_CONT_NULL; \
453 MACRO_END
454
455 #define vm_map_copy_abort_cont(old_copy) \
456 MACRO_BEGIN \
457 assert(vm_map_copy_cont_is_valid(old_copy)); \
458 vm_map_copy_page_discard(old_copy); \
459 (*((old_copy)->cpy_cont))((old_copy)->cpy_cont_args, \
460 (vm_map_copy_t *) 0); \
461 (old_copy)->cpy_cont = VM_MAP_COPY_CONT_NULL; \
462 (old_copy)->cpy_cont_args = VM_MAP_COPYIN_ARGS_NULL; \
463 MACRO_END
464
465 #define vm_map_copy_has_cont(copy) \
466 (((copy)->cpy_cont) != VM_MAP_COPY_CONT_NULL)
467
468 /*
469 * Macro to determine number of pages in a page-list copy chain.
470 */
471
472 #define vm_map_copy_page_count(copy) \
473 (round_page(((vm_offset_t)(copy)->offset - trunc_page((vm_offset_t)(copy)->offset)) + (copy)->size) / PAGE_SIZE)
474
475 /*
476 * Macros: vm_map_lock, etc. [internal use only]
477 * Description:
478 * Perform locking on the data portion of a map.
479 * When multiple maps are to be locked, order by map address.
480 * (See vm_map.c::vm_remap())
481 */
482
483 #define vm_map_lock_init(map) \
484 MACRO_BEGIN \
485 lock_init(&(map)->lock, TRUE, ETAP_VM_MAP, ETAP_VM_MAP_I); \
486 (map)->timestamp = 0; \
487 MACRO_END
488 #define vm_map_lock(map) \
489 MACRO_BEGIN \
490 lock_write(&(map)->lock); \
491 (map)->timestamp++; \
492 MACRO_END
493
494 #define vm_map_unlock(map) lock_write_done(&(map)->lock)
495 #define vm_map_lock_read(map) lock_read(&(map)->lock)
496 #define vm_map_unlock_read(map) lock_read_done(&(map)->lock)
497 #define vm_map_lock_write_to_read(map) \
498 lock_write_to_read(&(map)->lock)
499 #define vm_map_lock_read_to_write(map) \
500 (lock_read_to_write(&(map)->lock) || (((map)->timestamp++), 0))
501
502 extern zone_t vm_map_copy_zone; /* zone for vm_map_copy structures */
503
504 /*
505 * Exported procedures that operate on vm_map_t.
506 */
507
508 /* Initialize the module */
509 extern void vm_map_init(void);
510
511 /* Allocate a range in the specified virtual address map and
512 * return the entry allocated for that range. */
513 extern kern_return_t vm_map_find_space(
514 vm_map_t map,
515 vm_offset_t *address, /* OUT */
516 vm_size_t size,
517 vm_offset_t mask,
518 vm_map_entry_t *o_entry); /* OUT */
519
520 /* Lookup map entry containing or the specified address in the given map */
521 extern boolean_t vm_map_lookup_entry(
522 vm_map_t map,
523 vm_offset_t address,
524 vm_map_entry_t *entry); /* OUT */
525
526 /* A version of vm_map_copy_discard that can be called
527 * as a continuation from a vm_map_copy page list. */
528 extern kern_return_t vm_map_copy_discard_cont(
529 vm_map_copyin_args_t cont_args,
530 vm_map_copy_t *copy_result);/* OUT */
531
532 /* Find the VM object, offset, and protection for a given virtual address
533 * in the specified map, assuming a page fault of the type specified. */
534 extern kern_return_t vm_map_lookup_locked(
535 vm_map_t *var_map, /* IN/OUT */
536 vm_offset_t vaddr,
537 vm_prot_t fault_type,
538 vm_map_version_t *out_version, /* OUT */
539 vm_object_t *object, /* OUT */
540 vm_object_offset_t *offset, /* OUT */
541 vm_prot_t *out_prot, /* OUT */
542 boolean_t *wired, /* OUT */
543 int *behavior, /* OUT */
544 vm_object_offset_t *lo_offset, /* OUT */
545 vm_object_offset_t *hi_offset, /* OUT */
546 vm_map_t *pmap_map); /* OUT */
547
548 /* Verifies that the map has not changed since the given version. */
549 extern boolean_t vm_map_verify(
550 vm_map_t map,
551 vm_map_version_t *version); /* REF */
552
553 /* Split a vm_map_entry into 2 entries */
554 extern void _vm_map_clip_start(
555 struct vm_map_header *map_header,
556 vm_map_entry_t entry,
557 vm_offset_t start);
558
559 extern vm_map_entry_t vm_map_entry_insert(
560 vm_map_t map,
561 vm_map_entry_t insp_entry,
562 vm_offset_t start,
563 vm_offset_t end,
564 vm_object_t object,
565 vm_object_offset_t offset,
566 boolean_t needs_copy,
567 boolean_t is_shared,
568 boolean_t in_transition,
569 vm_prot_t cur_protection,
570 vm_prot_t max_protection,
571 vm_behavior_t behavior,
572 vm_inherit_t inheritance,
573 unsigned wired_count);
574
575 extern kern_return_t vm_remap_extract(
576 vm_map_t map,
577 vm_offset_t addr,
578 vm_size_t size,
579 boolean_t copy,
580 struct vm_map_header *map_header,
581 vm_prot_t *cur_protection,
582 vm_prot_t *max_protection,
583 vm_inherit_t inheritance,
584 boolean_t pageable);
585
586 extern kern_return_t vm_remap_range_allocate(
587 vm_map_t map,
588 vm_offset_t *address,
589 vm_size_t size,
590 vm_offset_t mask,
591 boolean_t anywhere,
592 vm_map_entry_t *map_entry);
593
594 extern kern_return_t vm_remap_extract(
595 vm_map_t map,
596 vm_offset_t addr,
597 vm_size_t size,
598 boolean_t copy,
599 struct vm_map_header *map_header,
600 vm_prot_t *cur_protection,
601 vm_prot_t *max_protection,
602 vm_inherit_t inheritance,
603 boolean_t pageable);
604
605 extern kern_return_t vm_remap_range_allocate(
606 vm_map_t map,
607 vm_offset_t *address,
608 vm_size_t size,
609 vm_offset_t mask,
610 boolean_t anywhere,
611 vm_map_entry_t *map_entry);
612
613 /*
614 * Functions implemented as macros
615 */
616 #define vm_map_min(map) ((map)->min_offset)
617 /* Lowest valid address in
618 * a map */
619
620 #define vm_map_max(map) ((map)->max_offset)
621 /* Highest valid address */
622
623 #define vm_map_pmap(map) ((map)->pmap)
624 /* Physical map associated
625 * with this address map */
626
627 #define vm_map_verify_done(map, version) vm_map_unlock_read(map)
628 /* Operation that required
629 * a verified lookup is
630 * now complete */
631
632 /*
633 * Macros/functions for map residence counts and swapin/out of vm maps
634 */
635 #if TASK_SWAPPER
636
637 #if MACH_ASSERT
638 /* Gain a reference to an existing map */
639 extern void vm_map_reference(
640 vm_map_t map);
641 /* Lose a residence count */
642 extern void vm_map_res_deallocate(
643 vm_map_t map);
644 /* Gain a residence count on a map */
645 extern void vm_map_res_reference(
646 vm_map_t map);
647 /* Gain reference & residence counts to possibly swapped-out map */
648 extern void vm_map_reference_swap(
649 vm_map_t map);
650
651 #else /* MACH_ASSERT */
652
653 #define vm_map_reference(map) \
654 MACRO_BEGIN \
655 vm_map_t Map = (map); \
656 if (Map) { \
657 mutex_lock(&Map->s_lock); \
658 Map->res_count++; \
659 Map->ref_count++; \
660 mutex_unlock(&Map->s_lock); \
661 } \
662 MACRO_END
663
664 #define vm_map_res_reference(map) \
665 MACRO_BEGIN \
666 vm_map_t Lmap = (map); \
667 if (Lmap->res_count == 0) { \
668 mutex_unlock(&Lmap->s_lock); \
669 vm_map_lock(Lmap); \
670 vm_map_swapin(Lmap); \
671 mutex_lock(&Lmap->s_lock); \
672 ++Lmap->res_count; \
673 vm_map_unlock(Lmap); \
674 } else \
675 ++Lmap->res_count; \
676 MACRO_END
677
678 #define vm_map_res_deallocate(map) \
679 MACRO_BEGIN \
680 vm_map_t Map = (map); \
681 if (--Map->res_count == 0) { \
682 mutex_unlock(&Map->s_lock); \
683 vm_map_lock(Map); \
684 vm_map_swapout(Map); \
685 vm_map_unlock(Map); \
686 mutex_lock(&Map->s_lock); \
687 } \
688 MACRO_END
689
690 #define vm_map_reference_swap(map) \
691 MACRO_BEGIN \
692 vm_map_t Map = (map); \
693 mutex_lock(&Map->s_lock); \
694 ++Map->ref_count; \
695 vm_map_res_reference(Map); \
696 mutex_unlock(&Map->s_lock); \
697 MACRO_END
698 #endif /* MACH_ASSERT */
699
700 extern void vm_map_swapin(
701 vm_map_t map);
702
703 extern void vm_map_swapout(
704 vm_map_t map);
705
706 #else /* TASK_SWAPPER */
707
708 #define vm_map_reference(map) \
709 MACRO_BEGIN \
710 vm_map_t Map = (map); \
711 if (Map) { \
712 mutex_lock(&Map->s_lock); \
713 Map->ref_count++; \
714 mutex_unlock(&Map->s_lock); \
715 } \
716 MACRO_END
717
718 #define vm_map_reference_swap(map) vm_map_reference(map)
719 #define vm_map_res_reference(map)
720 #define vm_map_res_deallocate(map)
721
722 #endif /* TASK_SWAPPER */
723
724 /*
725 * Submap object. Must be used to create memory to be put
726 * in a submap by vm_map_submap.
727 */
728 extern vm_object_t vm_submap_object;
729
730 /*
731 * Wait and wakeup macros for in_transition map entries.
732 */
733 #define vm_map_entry_wait(map, interruptible) \
734 MACRO_BEGIN \
735 assert_wait((event_t)&(map)->hdr, interruptible); \
736 vm_map_unlock(map); \
737 thread_block((void (*)(void))0); \
738 MACRO_END
739
740 #define vm_map_entry_wakeup(map) thread_wakeup((event_t)(&(map)->hdr))
741
742
743
744 #define vm_map_ref_fast(map) \
745 MACRO_BEGIN \
746 mutex_lock(&map->s_lock); \
747 map->ref_count++; \
748 vm_map_res_reference(map); \
749 mutex_unlock(&map->s_lock); \
750 MACRO_END
751
752 #define vm_map_dealloc_fast(map) \
753 MACRO_BEGIN \
754 register int c; \
755 \
756 mutex_lock(&map->s_lock); \
757 c = --map->ref_count; \
758 if (c > 0) \
759 vm_map_res_deallocate(map); \
760 mutex_unlock(&map->s_lock); \
761 if (c == 0) \
762 vm_map_destroy(map); \
763 MACRO_END
764
765
766 /* simplify map entries */
767 extern void vm_map_simplify(
768 vm_map_t map,
769 vm_offset_t start);
770
771 /* Steal all the pages from a vm_map_copy page_list */
772 extern void vm_map_copy_steal_pages(
773 vm_map_copy_t copy);
774
775 /* Discard a copy without using it */
776 extern void vm_map_copy_discard(
777 vm_map_copy_t copy);
778
779 /* Move the information in a map copy object to a new map copy object */
780 extern vm_map_copy_t vm_map_copy_copy(
781 vm_map_copy_t copy);
782
783 /* Overwrite existing memory with a copy */
784 extern kern_return_t vm_map_copy_overwrite(
785 vm_map_t dst_map,
786 vm_offset_t dst_addr,
787 vm_map_copy_t copy,
788 int interruptible);
789
790 /* Version of vm_map_copyout() for page list vm map copies. */
791 extern kern_return_t vm_map_copyout_page_list(
792 vm_map_t dst_map,
793 vm_offset_t *dst_addr, /* OUT */
794 vm_map_copy_t copy);
795
796 /* Get rid of the pages in a page_list copy. */
797 extern void vm_map_copy_page_discard(
798 vm_map_copy_t copy);
799
800 /* Create a copy object from an object. */
801 extern kern_return_t vm_map_copyin_object(
802 vm_object_t object,
803 vm_object_offset_t offset,
804 vm_object_size_t size,
805 vm_map_copy_t *copy_result); /* OUT */
806
807
808 /* Make a copy of a region */
809 /* Make a copy of a region using a page list copy */
810 extern kern_return_t vm_map_copyin_page_list(
811 vm_map_t src_map,
812 vm_offset_t src_addr,
813 vm_size_t len,
814 int options,
815 vm_map_copy_t *copy_result, /* OUT */
816 boolean_t is_cont);
817
818 extern vm_map_t vm_map_switch(
819 vm_map_t map);
820
821 extern int vm_map_copy_cont_is_valid(
822 vm_map_copy_t copy);
823
824
825
826 #endif /* !MACH_KERNEL_PRIVATE */
827
828 /* Get rid of a map */
829 extern void vm_map_destroy(
830 vm_map_t map);
831 /* Lose a reference */
832 extern void vm_map_deallocate(
833 vm_map_t map);
834
835 /* Create an empty map */
836 extern vm_map_t vm_map_create(
837 pmap_t pmap,
838 vm_offset_t min,
839 vm_offset_t max,
840 boolean_t pageable);
841
842
843 /* Enter a mapping */
844 extern kern_return_t vm_map_enter(
845 vm_map_t map,
846 vm_offset_t *address,
847 vm_size_t size,
848 vm_offset_t mask,
849 int flags,
850 vm_object_t object,
851 vm_object_offset_t offset,
852 boolean_t needs_copy,
853 vm_prot_t cur_protection,
854 vm_prot_t max_protection,
855 vm_inherit_t inheritance);
856
857 extern kern_return_t vm_map_write_user(
858 vm_map_t map,
859 vm_offset_t src_addr,
860 vm_offset_t dst_addr,
861 vm_size_t size);
862
863 extern kern_return_t vm_map_read_user(
864 vm_map_t map,
865 vm_offset_t src_addr,
866 vm_offset_t dst_addr,
867 vm_size_t size);
868
869 /* Create a new task map using an existing task map as a template. */
870 extern vm_map_t vm_map_fork(
871 vm_map_t old_map);
872
873 /* Change protection */
874 extern kern_return_t vm_map_protect(
875 vm_map_t map,
876 vm_offset_t start,
877 vm_offset_t end,
878 vm_prot_t new_prot,
879 boolean_t set_max);
880
881 /* Change inheritance */
882 extern kern_return_t vm_map_inherit(
883 vm_map_t map,
884 vm_offset_t start,
885 vm_offset_t end,
886 vm_inherit_t new_inheritance);
887
888 /* wire down a region */
889 extern kern_return_t vm_map_wire(
890 vm_map_t map,
891 vm_offset_t start,
892 vm_offset_t end,
893 vm_prot_t access_type,
894 boolean_t user_wire);
895
896 /* unwire a region */
897 extern kern_return_t vm_map_unwire(
898 vm_map_t map,
899 vm_offset_t start,
900 vm_offset_t end,
901 boolean_t user_wire);
902
903 /* Deallocate a region */
904 extern kern_return_t vm_map_remove(
905 vm_map_t map,
906 vm_offset_t start,
907 vm_offset_t end,
908 boolean_t flags);
909
910 /* Place a copy into a map */
911 extern kern_return_t vm_map_copyout(
912 vm_map_t dst_map,
913 vm_offset_t *dst_addr, /* OUT */
914 vm_map_copy_t copy);
915
916
917 /* Add or remove machine-dependent attributes from map regions */
918 extern kern_return_t vm_map_machine_attribute(
919 vm_map_t map,
920 vm_offset_t address,
921 vm_size_t size,
922 vm_machine_attribute_t attribute,
923 vm_machine_attribute_val_t* value); /* IN/OUT */
924 /* Set paging behavior */
925 extern kern_return_t vm_map_behavior_set(
926 vm_map_t map,
927 vm_offset_t start,
928 vm_offset_t end,
929 vm_behavior_t new_behavior);
930
931 extern kern_return_t vm_map_copyin_common(
932 vm_map_t src_map,
933 vm_offset_t src_addr,
934 vm_size_t len,
935 boolean_t src_destroy,
936 boolean_t src_volatile,
937 vm_map_copy_t *copy_result, /* OUT */
938 boolean_t use_maxprot);
939
940 extern kern_return_t vm_map_submap(
941 vm_map_t map,
942 vm_offset_t start,
943 vm_offset_t end,
944 vm_map_t submap,
945 vm_offset_t offset,
946 boolean_t use_pmap);
947
948 /*
949 * Macros to invoke vm_map_copyin_common. vm_map_copyin is the
950 * usual form; it handles a copyin based on the current protection
951 * (current protection == VM_PROT_NONE) is a failure.
952 * vm_map_copyin_maxprot handles a copyin based on maximum possible
953 * access. The difference is that a region with no current access
954 * BUT possible maximum access is rejected by vm_map_copyin(), but
955 * returned by vm_map_copyin_maxprot.
956 */
957 #define vm_map_copyin(src_map, src_addr, len, src_destroy, copy_result) \
958 vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
959 FALSE, copy_result, FALSE)
960
961 #define vm_map_copyin_maxprot(src_map, \
962 src_addr, len, src_destroy, copy_result) \
963 vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
964 FALSE, copy_result, TRUE)
965
966 #define VM_MAP_ENTRY_NULL ((vm_map_entry_t) 0)
967
968 /*
969 * Flags for vm_map_remove() and vm_map_delete()
970 */
971 #define VM_MAP_NO_FLAGS 0x0
972 #define VM_MAP_REMOVE_KUNWIRE 0x1
973 #define VM_MAP_REMOVE_INTERRUPTIBLE 0x2
974 #define VM_MAP_REMOVE_WAIT_FOR_KWIRE 0x4
975
976
977 #ifdef MACH_KERNEL_PRIVATE
978
979 /* address space shared region descriptor */
980
981 struct shared_region_mapping {
982 decl_mutex_data(, Lock) /* Synchronization */
983 int ref_count;
984 ipc_port_t text_region;
985 vm_size_t text_size;
986 ipc_port_t data_region;
987 vm_size_t data_size;
988 vm_offset_t region_mappings;
989 vm_offset_t client_base;
990 vm_offset_t alternate_base;
991 vm_offset_t alternate_next;
992 int flags;
993 int depth;
994 struct shared_region_object_chain *object_chain;
995 struct shared_region_mapping *self;
996 struct shared_region_mapping *next;
997 };
998
999 typedef struct shared_region_mapping *shared_region_mapping_t;
1000
1001 struct shared_region_object_chain {
1002 shared_region_mapping_t object_chain_region;
1003 int depth;
1004 struct shared_region_object_chain *next;
1005 };
1006
1007 typedef struct shared_region_object_chain *shared_region_object_chain_t;
1008
1009 #endif /* MACH_KERNEL_PRIVATE */
1010
1011
1012 #endif /* _VM_VM_MAP_H_ */
1013