kernel - Tag vm_map_entry structure, slight optimization to zalloc, misc.
[dragonfly.git] / sys / vm / vm_map.c
blob05ee2ab506ea8665f6e2cbec7e42fb1b8b7fb09b
1 /*
2 * (MPSAFE)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
62 * $FreeBSD: src/sys/vm/vm_map.c,v 1.187.2.19 2003/05/27 00:47:02 alc Exp $
66 * Virtual memory mapping module.
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/proc.h>
73 #include <sys/serialize.h>
74 #include <sys/lock.h>
75 #include <sys/vmmeter.h>
76 #include <sys/mman.h>
77 #include <sys/vnode.h>
78 #include <sys/resourcevar.h>
79 #include <sys/shm.h>
80 #include <sys/tree.h>
81 #include <sys/malloc.h>
82 #include <sys/objcache.h>
84 #include <vm/vm.h>
85 #include <vm/vm_param.h>
86 #include <vm/pmap.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_pager.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_extern.h>
93 #include <vm/swap_pager.h>
94 #include <vm/vm_zone.h>
96 #include <sys/thread2.h>
97 #include <sys/random.h>
98 #include <sys/sysctl.h>
101 * Virtual memory maps provide for the mapping, protection, and sharing
102 * of virtual memory objects. In addition, this module provides for an
103 * efficient virtual copy of memory from one map to another.
105 * Synchronization is required prior to most operations.
107 * Maps consist of an ordered doubly-linked list of simple entries.
108 * A hint and a RB tree is used to speed-up lookups.
110 * Callers looking to modify maps specify start/end addresses which cause
111 * the related map entry to be clipped if necessary, and then later
112 * recombined if the pieces remained compatible.
114 * Virtual copy operations are performed by copying VM object references
115 * from one map to another, and then marking both regions as copy-on-write.
117 static boolean_t vmspace_ctor(void *obj, void *privdata, int ocflags);
118 static void vmspace_dtor(void *obj, void *privdata);
119 static void vmspace_terminate(struct vmspace *vm, int final);
121 MALLOC_DEFINE(M_VMSPACE, "vmspace", "vmspace objcache backingstore");
122 static struct objcache *vmspace_cache;
125 * per-cpu page table cross mappings are initialized in early boot
126 * and might require a considerable number of vm_map_entry structures.
128 #define MAPENTRYBSP_CACHE (MAXCPU+1)
129 #define MAPENTRYAP_CACHE 8
131 static struct vm_zone mapentzone_store, mapzone_store;
132 static vm_zone_t mapentzone, mapzone;
133 static struct vm_object mapentobj, mapobj;
135 static struct vm_map_entry map_entry_init[MAX_MAPENT];
136 static struct vm_map_entry cpu_map_entry_init_bsp[MAPENTRYBSP_CACHE];
137 static struct vm_map_entry cpu_map_entry_init_ap[MAXCPU][MAPENTRYAP_CACHE];
138 static struct vm_map map_init[MAX_KMAP];
140 static int randomize_mmap;
141 SYSCTL_INT(_vm, OID_AUTO, randomize_mmap, CTLFLAG_RW, &randomize_mmap, 0,
142 "Randomize mmap offsets");
143 static int vm_map_relock_enable = 1;
144 SYSCTL_INT(_vm, OID_AUTO, map_relock_enable, CTLFLAG_RW,
145 &vm_map_relock_enable, 0, "Randomize mmap offsets");
147 static void vm_map_entry_shadow(vm_map_entry_t entry, int addref);
148 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
149 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
150 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
151 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
152 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
153 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
154 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
155 vm_map_entry_t);
156 static void vm_map_unclip_range (vm_map_t map, vm_map_entry_t start_entry, vm_offset_t start, vm_offset_t end, int *count, int flags);
159 * Initialize the vm_map module. Must be called before any other vm_map
160 * routines.
162 * Map and entry structures are allocated from the general purpose
163 * memory pool with some exceptions:
165 * - The kernel map is allocated statically.
166 * - Initial kernel map entries are allocated out of a static pool.
167 * - We must set ZONE_SPECIAL here or the early boot code can get
168 * stuck if there are >63 cores.
170 * These restrictions are necessary since malloc() uses the
171 * maps and requires map entries.
173 * Called from the low level boot code only.
175 void
176 vm_map_startup(void)
178 mapzone = &mapzone_store;
179 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
180 map_init, MAX_KMAP);
181 mapentzone = &mapentzone_store;
182 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
183 map_entry_init, MAX_MAPENT);
184 mapentzone_store.zflags |= ZONE_SPECIAL;
188 * Called prior to any vmspace allocations.
190 * Called from the low level boot code only.
192 void
193 vm_init2(void)
195 vmspace_cache = objcache_create_mbacked(M_VMSPACE,
196 sizeof(struct vmspace),
197 0, ncpus * 4,
198 vmspace_ctor, vmspace_dtor,
199 NULL);
200 zinitna(mapentzone, &mapentobj, NULL, 0, 0,
201 ZONE_USE_RESERVE | ZONE_SPECIAL);
202 zinitna(mapzone, &mapobj, NULL, 0, 0, 0);
203 pmap_init2();
204 vm_object_init2();
208 * objcache support. We leave the pmap root cached as long as possible
209 * for performance reasons.
211 static
212 boolean_t
213 vmspace_ctor(void *obj, void *privdata, int ocflags)
215 struct vmspace *vm = obj;
217 bzero(vm, sizeof(*vm));
218 vm->vm_refcnt = (u_int)-1;
220 return 1;
223 static
224 void
225 vmspace_dtor(void *obj, void *privdata)
227 struct vmspace *vm = obj;
229 KKASSERT(vm->vm_refcnt == (u_int)-1);
230 pmap_puninit(vmspace_pmap(vm));
234 * Red black tree functions
236 * The caller must hold the related map lock.
238 static int rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b);
239 RB_GENERATE(vm_map_rb_tree, vm_map_entry, rb_entry, rb_vm_map_compare);
241 /* a->start is address, and the only field has to be initialized */
242 static int
243 rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b)
245 if (a->start < b->start)
246 return(-1);
247 else if (a->start > b->start)
248 return(1);
249 return(0);
253 * Initialize vmspace ref/hold counts vmspace0. There is a holdcnt for
254 * every refcnt.
256 void
257 vmspace_initrefs(struct vmspace *vm)
259 vm->vm_refcnt = 1;
260 vm->vm_holdcnt = 1;
264 * Allocate a vmspace structure, including a vm_map and pmap.
265 * Initialize numerous fields. While the initial allocation is zerod,
266 * subsequence reuse from the objcache leaves elements of the structure
267 * intact (particularly the pmap), so portions must be zerod.
269 * Returns a referenced vmspace.
271 * No requirements.
273 struct vmspace *
274 vmspace_alloc(vm_offset_t min, vm_offset_t max)
276 struct vmspace *vm;
278 vm = objcache_get(vmspace_cache, M_WAITOK);
280 bzero(&vm->vm_startcopy,
281 (char *)&vm->vm_endcopy - (char *)&vm->vm_startcopy);
282 vm_map_init(&vm->vm_map, min, max, NULL); /* initializes token */
285 * NOTE: hold to acquires token for safety.
287 * On return vmspace is referenced (refs=1, hold=1). That is,
288 * each refcnt also has a holdcnt. There can be additional holds
289 * (holdcnt) above and beyond the refcnt. Finalization is handled in
290 * two stages, one on refs 1->0, and the the second on hold 1->0.
292 KKASSERT(vm->vm_holdcnt == 0);
293 KKASSERT(vm->vm_refcnt == (u_int)-1);
294 vmspace_initrefs(vm);
295 vmspace_hold(vm);
296 pmap_pinit(vmspace_pmap(vm)); /* (some fields reused) */
297 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
298 vm->vm_shm = NULL;
299 vm->vm_flags = 0;
300 cpu_vmspace_alloc(vm);
301 vmspace_drop(vm);
303 return (vm);
307 * NOTE: Can return -1 if the vmspace is exiting.
310 vmspace_getrefs(struct vmspace *vm)
312 return ((int)vm->vm_refcnt);
316 * A vmspace object must already have a non-zero hold to be able to gain
317 * further holds on it.
319 static void
320 vmspace_hold_notoken(struct vmspace *vm)
322 KKASSERT(vm->vm_holdcnt != 0);
323 refcount_acquire(&vm->vm_holdcnt);
326 static void
327 vmspace_drop_notoken(struct vmspace *vm)
329 if (refcount_release(&vm->vm_holdcnt)) {
330 if (vm->vm_refcnt == (u_int)-1) {
331 vmspace_terminate(vm, 1);
336 void
337 vmspace_hold(struct vmspace *vm)
339 vmspace_hold_notoken(vm);
340 lwkt_gettoken(&vm->vm_map.token);
343 void
344 vmspace_drop(struct vmspace *vm)
346 lwkt_reltoken(&vm->vm_map.token);
347 vmspace_drop_notoken(vm);
351 * A vmspace object must not be in a terminated state to be able to obtain
352 * additional refs on it.
354 * Ref'ing a vmspace object also increments its hold count.
356 void
357 vmspace_ref(struct vmspace *vm)
359 KKASSERT((int)vm->vm_refcnt >= 0);
360 vmspace_hold_notoken(vm);
361 refcount_acquire(&vm->vm_refcnt);
365 * Release a ref on the vmspace. On the 1->0 transition we do stage-1
366 * termination of the vmspace. Then, on the final drop of the hold we
367 * will do stage-2 final termination.
369 void
370 vmspace_rel(struct vmspace *vm)
372 if (refcount_release(&vm->vm_refcnt)) {
373 vm->vm_refcnt = (u_int)-1; /* no other refs possible */
374 vmspace_terminate(vm, 0);
376 vmspace_drop_notoken(vm);
380 * This is called during exit indicating that the vmspace is no
381 * longer in used by an exiting process, but the process has not yet
382 * been reaped.
384 * We release the refcnt but not the associated holdcnt.
386 * No requirements.
388 void
389 vmspace_relexit(struct vmspace *vm)
391 if (refcount_release(&vm->vm_refcnt)) {
392 vm->vm_refcnt = (u_int)-1; /* no other refs possible */
393 vmspace_terminate(vm, 0);
398 * Called during reap to disconnect the remainder of the vmspace from
399 * the process. On the hold drop the vmspace termination is finalized.
401 * No requirements.
403 void
404 vmspace_exitfree(struct proc *p)
406 struct vmspace *vm;
408 vm = p->p_vmspace;
409 p->p_vmspace = NULL;
410 vmspace_drop_notoken(vm);
414 * Called in two cases:
416 * (1) When the last refcnt is dropped and the vmspace becomes inactive,
417 * called with final == 0. refcnt will be (u_int)-1 at this point,
418 * and holdcnt will still be non-zero.
420 * (2) When holdcnt becomes 0, called with final == 1. There should no
421 * longer be anyone with access to the vmspace.
423 * VMSPACE_EXIT1 flags the primary deactivation
424 * VMSPACE_EXIT2 flags the last reap
426 static void
427 vmspace_terminate(struct vmspace *vm, int final)
429 int count;
431 lwkt_gettoken(&vm->vm_map.token);
432 if (final == 0) {
433 KKASSERT((vm->vm_flags & VMSPACE_EXIT1) == 0);
436 * Get rid of most of the resources. Leave the kernel pmap
437 * intact.
439 * If the pmap does not contain wired pages we can bulk-delete
440 * the pmap as a performance optimization before removing the related mappings.
442 * If the pmap contains wired pages we cannot do this pre-optimization
443 * because currently vm_fault_unwire() expects the pmap pages to exist
444 * and will not decrement p->wire_count if they do not.
446 vm->vm_flags |= VMSPACE_EXIT1;
447 shmexit(vm);
448 if (vmspace_pmap(vm)->pm_stats.wired_count) {
449 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
450 VM_MAX_USER_ADDRESS);
451 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
452 VM_MAX_USER_ADDRESS);
453 } else {
454 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
455 VM_MAX_USER_ADDRESS);
456 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
457 VM_MAX_USER_ADDRESS);
459 lwkt_reltoken(&vm->vm_map.token);
460 } else {
461 KKASSERT((vm->vm_flags & VMSPACE_EXIT1) != 0);
462 KKASSERT((vm->vm_flags & VMSPACE_EXIT2) == 0);
465 * Get rid of remaining basic resources.
467 vm->vm_flags |= VMSPACE_EXIT2;
468 cpu_vmspace_free(vm);
469 shmexit(vm);
472 * Lock the map, to wait out all other references to it.
473 * Delete all of the mappings and pages they hold, then call
474 * the pmap module to reclaim anything left.
476 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
477 vm_map_lock(&vm->vm_map);
478 vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
479 vm->vm_map.max_offset, &count);
480 vm_map_unlock(&vm->vm_map);
481 vm_map_entry_release(count);
483 lwkt_gettoken(&vmspace_pmap(vm)->pm_token);
484 pmap_release(vmspace_pmap(vm));
485 lwkt_reltoken(&vmspace_pmap(vm)->pm_token);
486 lwkt_reltoken(&vm->vm_map.token);
487 objcache_put(vmspace_cache, vm);
492 * Swap useage is determined by taking the proportional swap used by
493 * VM objects backing the VM map. To make up for fractional losses,
494 * if the VM object has any swap use at all the associated map entries
495 * count for at least 1 swap page.
497 * No requirements.
500 vmspace_swap_count(struct vmspace *vm)
502 vm_map_t map = &vm->vm_map;
503 vm_map_entry_t cur;
504 vm_object_t object;
505 int count = 0;
506 int n;
508 vmspace_hold(vm);
509 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
510 switch(cur->maptype) {
511 case VM_MAPTYPE_NORMAL:
512 case VM_MAPTYPE_VPAGETABLE:
513 if ((object = cur->object.vm_object) == NULL)
514 break;
515 if (object->swblock_count) {
516 n = (cur->end - cur->start) / PAGE_SIZE;
517 count += object->swblock_count *
518 SWAP_META_PAGES * n / object->size + 1;
520 break;
521 default:
522 break;
525 vmspace_drop(vm);
527 return(count);
531 * Calculate the approximate number of anonymous pages in use by
532 * this vmspace. To make up for fractional losses, we count each
533 * VM object as having at least 1 anonymous page.
535 * No requirements.
538 vmspace_anonymous_count(struct vmspace *vm)
540 vm_map_t map = &vm->vm_map;
541 vm_map_entry_t cur;
542 vm_object_t object;
543 int count = 0;
545 vmspace_hold(vm);
546 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
547 switch(cur->maptype) {
548 case VM_MAPTYPE_NORMAL:
549 case VM_MAPTYPE_VPAGETABLE:
550 if ((object = cur->object.vm_object) == NULL)
551 break;
552 if (object->type != OBJT_DEFAULT &&
553 object->type != OBJT_SWAP) {
554 break;
556 count += object->resident_page_count;
557 break;
558 default:
559 break;
562 vmspace_drop(vm);
564 return(count);
568 * Creates and returns a new empty VM map with the given physical map
569 * structure, and having the given lower and upper address bounds.
571 * No requirements.
573 vm_map_t
574 vm_map_create(vm_map_t result, pmap_t pmap, vm_offset_t min, vm_offset_t max)
576 if (result == NULL)
577 result = zalloc(mapzone);
578 vm_map_init(result, min, max, pmap);
579 return (result);
583 * Initialize an existing vm_map structure such as that in the vmspace
584 * structure. The pmap is initialized elsewhere.
586 * No requirements.
588 void
589 vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max, pmap_t pmap)
591 map->header.next = map->header.prev = &map->header;
592 RB_INIT(&map->rb_root);
593 map->nentries = 0;
594 map->size = 0;
595 map->system_map = 0;
596 map->min_offset = min;
597 map->max_offset = max;
598 map->pmap = pmap;
599 map->first_free = &map->header;
600 map->hint = &map->header;
601 map->timestamp = 0;
602 map->flags = 0;
603 lwkt_token_init(&map->token, "vm_map");
604 lockinit(&map->lock, "vm_maplk", (hz + 9) / 10, 0);
608 * Shadow the vm_map_entry's object. This typically needs to be done when
609 * a write fault is taken on an entry which had previously been cloned by
610 * fork(). The shared object (which might be NULL) must become private so
611 * we add a shadow layer above it.
613 * Object allocation for anonymous mappings is defered as long as possible.
614 * When creating a shadow, however, the underlying object must be instantiated
615 * so it can be shared.
617 * If the map segment is governed by a virtual page table then it is
618 * possible to address offsets beyond the mapped area. Just allocate
619 * a maximally sized object for this case.
621 * If addref is non-zero an additional reference is added to the returned
622 * entry. This mechanic exists because the additional reference might have
623 * to be added atomically and not after return to prevent a premature
624 * collapse.
626 * The vm_map must be exclusively locked.
627 * No other requirements.
629 static
630 void
631 vm_map_entry_shadow(vm_map_entry_t entry, int addref)
633 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
634 vm_object_shadow(&entry->object.vm_object, &entry->offset,
635 0x7FFFFFFF, addref); /* XXX */
636 } else {
637 vm_object_shadow(&entry->object.vm_object, &entry->offset,
638 atop(entry->end - entry->start), addref);
640 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
644 * Allocate an object for a vm_map_entry.
646 * Object allocation for anonymous mappings is defered as long as possible.
647 * This function is called when we can defer no longer, generally when a map
648 * entry might be split or forked or takes a page fault.
650 * If the map segment is governed by a virtual page table then it is
651 * possible to address offsets beyond the mapped area. Just allocate
652 * a maximally sized object for this case.
654 * The vm_map must be exclusively locked.
655 * No other requirements.
657 void
658 vm_map_entry_allocate_object(vm_map_entry_t entry)
660 vm_object_t obj;
662 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
663 obj = vm_object_allocate(OBJT_DEFAULT, 0x7FFFFFFF); /* XXX */
664 } else {
665 obj = vm_object_allocate(OBJT_DEFAULT,
666 atop(entry->end - entry->start));
668 entry->object.vm_object = obj;
669 entry->offset = 0;
673 * Set an initial negative count so the first attempt to reserve
674 * space preloads a bunch of vm_map_entry's for this cpu. Also
675 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
676 * map a new page for vm_map_entry structures. SMP systems are
677 * particularly sensitive.
679 * This routine is called in early boot so we cannot just call
680 * vm_map_entry_reserve().
682 * Called from the low level boot code only (for each cpu)
684 * WARNING! Take care not to have too-big a static/BSS structure here
685 * as MAXCPU can be 256+, otherwise the loader's 64MB heap
686 * can get blown out by the kernel plus the initrd image.
688 void
689 vm_map_entry_reserve_cpu_init(globaldata_t gd)
691 vm_map_entry_t entry;
692 int count;
693 int i;
695 gd->gd_vme_avail -= MAP_RESERVE_COUNT * 2;
696 if (gd->gd_cpuid == 0) {
697 entry = &cpu_map_entry_init_bsp[0];
698 count = MAPENTRYBSP_CACHE;
699 } else {
700 entry = &cpu_map_entry_init_ap[gd->gd_cpuid][0];
701 count = MAPENTRYAP_CACHE;
703 for (i = 0; i < count; ++i, ++entry) {
704 entry->next = gd->gd_vme_base;
705 gd->gd_vme_base = entry;
710 * Reserves vm_map_entry structures so code later on can manipulate
711 * map_entry structures within a locked map without blocking trying
712 * to allocate a new vm_map_entry.
714 * No requirements.
717 vm_map_entry_reserve(int count)
719 struct globaldata *gd = mycpu;
720 vm_map_entry_t entry;
723 * Make sure we have enough structures in gd_vme_base to handle
724 * the reservation request.
726 * The critical section protects access to the per-cpu gd.
728 crit_enter();
729 while (gd->gd_vme_avail < count) {
730 entry = zalloc(mapentzone);
731 entry->next = gd->gd_vme_base;
732 gd->gd_vme_base = entry;
733 ++gd->gd_vme_avail;
735 gd->gd_vme_avail -= count;
736 crit_exit();
738 return(count);
742 * Releases previously reserved vm_map_entry structures that were not
743 * used. If we have too much junk in our per-cpu cache clean some of
744 * it out.
746 * No requirements.
748 void
749 vm_map_entry_release(int count)
751 struct globaldata *gd = mycpu;
752 vm_map_entry_t entry;
754 crit_enter();
755 gd->gd_vme_avail += count;
756 while (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
757 entry = gd->gd_vme_base;
758 KKASSERT(entry != NULL);
759 gd->gd_vme_base = entry->next;
760 --gd->gd_vme_avail;
761 crit_exit();
762 zfree(mapentzone, entry);
763 crit_enter();
765 crit_exit();
769 * Reserve map entry structures for use in kernel_map itself. These
770 * entries have *ALREADY* been reserved on a per-cpu basis when the map
771 * was inited. This function is used by zalloc() to avoid a recursion
772 * when zalloc() itself needs to allocate additional kernel memory.
774 * This function works like the normal reserve but does not load the
775 * vm_map_entry cache (because that would result in an infinite
776 * recursion). Note that gd_vme_avail may go negative. This is expected.
778 * Any caller of this function must be sure to renormalize after
779 * potentially eating entries to ensure that the reserve supply
780 * remains intact.
782 * No requirements.
785 vm_map_entry_kreserve(int count)
787 struct globaldata *gd = mycpu;
789 crit_enter();
790 gd->gd_vme_avail -= count;
791 crit_exit();
792 KASSERT(gd->gd_vme_base != NULL,
793 ("no reserved entries left, gd_vme_avail = %d",
794 gd->gd_vme_avail));
795 return(count);
799 * Release previously reserved map entries for kernel_map. We do not
800 * attempt to clean up like the normal release function as this would
801 * cause an unnecessary (but probably not fatal) deep procedure call.
803 * No requirements.
805 void
806 vm_map_entry_krelease(int count)
808 struct globaldata *gd = mycpu;
810 crit_enter();
811 gd->gd_vme_avail += count;
812 crit_exit();
816 * Allocates a VM map entry for insertion. No entry fields are filled in.
818 * The entries should have previously been reserved. The reservation count
819 * is tracked in (*countp).
821 * No requirements.
823 static vm_map_entry_t
824 vm_map_entry_create(vm_map_t map, int *countp)
826 struct globaldata *gd = mycpu;
827 vm_map_entry_t entry;
829 KKASSERT(*countp > 0);
830 --*countp;
831 crit_enter();
832 entry = gd->gd_vme_base;
833 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
834 gd->gd_vme_base = entry->next;
835 crit_exit();
837 return(entry);
841 * Dispose of a vm_map_entry that is no longer being referenced.
843 * No requirements.
845 static void
846 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
848 struct globaldata *gd = mycpu;
850 KKASSERT(map->hint != entry);
851 KKASSERT(map->first_free != entry);
853 ++*countp;
854 crit_enter();
855 entry->next = gd->gd_vme_base;
856 gd->gd_vme_base = entry;
857 crit_exit();
862 * Insert/remove entries from maps.
864 * The related map must be exclusively locked.
865 * The caller must hold map->token
866 * No other requirements.
868 static __inline void
869 vm_map_entry_link(vm_map_t map,
870 vm_map_entry_t after_where,
871 vm_map_entry_t entry)
873 ASSERT_VM_MAP_LOCKED(map);
875 map->nentries++;
876 entry->prev = after_where;
877 entry->next = after_where->next;
878 entry->next->prev = entry;
879 after_where->next = entry;
880 if (vm_map_rb_tree_RB_INSERT(&map->rb_root, entry))
881 panic("vm_map_entry_link: dup addr map %p ent %p", map, entry);
884 static __inline void
885 vm_map_entry_unlink(vm_map_t map,
886 vm_map_entry_t entry)
888 vm_map_entry_t prev;
889 vm_map_entry_t next;
891 ASSERT_VM_MAP_LOCKED(map);
893 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
894 panic("vm_map_entry_unlink: attempt to mess with "
895 "locked entry! %p", entry);
897 prev = entry->prev;
898 next = entry->next;
899 next->prev = prev;
900 prev->next = next;
901 vm_map_rb_tree_RB_REMOVE(&map->rb_root, entry);
902 map->nentries--;
906 * Finds the map entry containing (or immediately preceding) the specified
907 * address in the given map. The entry is returned in (*entry).
909 * The boolean result indicates whether the address is actually contained
910 * in the map.
912 * The related map must be locked.
913 * No other requirements.
915 boolean_t
916 vm_map_lookup_entry(vm_map_t map, vm_offset_t address, vm_map_entry_t *entry)
918 vm_map_entry_t tmp;
919 vm_map_entry_t last;
921 ASSERT_VM_MAP_LOCKED(map);
922 #if 0
924 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive
925 * the hint code with the red-black lookup meets with system crashes
926 * and lockups. We do not yet know why.
928 * It is possible that the problem is related to the setting
929 * of the hint during map_entry deletion, in the code specified
930 * at the GGG comment later on in this file.
932 * YYY More likely it's because this function can be called with
933 * a shared lock on the map, resulting in map->hint updates possibly
934 * racing. Fixed now but untested.
937 * Quickly check the cached hint, there's a good chance of a match.
939 tmp = map->hint;
940 cpu_ccfence();
941 if (tmp != &map->header) {
942 if (address >= tmp->start && address < tmp->end) {
943 *entry = tmp;
944 return(TRUE);
947 #endif
950 * Locate the record from the top of the tree. 'last' tracks the
951 * closest prior record and is returned if no match is found, which
952 * in binary tree terms means tracking the most recent right-branch
953 * taken. If there is no prior record, &map->header is returned.
955 last = &map->header;
956 tmp = RB_ROOT(&map->rb_root);
958 while (tmp) {
959 if (address >= tmp->start) {
960 if (address < tmp->end) {
961 *entry = tmp;
962 map->hint = tmp;
963 return(TRUE);
965 last = tmp;
966 tmp = RB_RIGHT(tmp, rb_entry);
967 } else {
968 tmp = RB_LEFT(tmp, rb_entry);
971 *entry = last;
972 return (FALSE);
976 * Inserts the given whole VM object into the target map at the specified
977 * address range. The object's size should match that of the address range.
979 * The map must be exclusively locked.
980 * The object must be held.
981 * The caller must have reserved sufficient vm_map_entry structures.
983 * If object is non-NULL, ref count must be bumped by caller prior to
984 * making call to account for the new entry.
987 vm_map_insert(vm_map_t map, int *countp, void *map_object, void *map_aux,
988 vm_ooffset_t offset, vm_offset_t start, vm_offset_t end,
989 vm_maptype_t maptype, vm_subsys_t id,
990 vm_prot_t prot, vm_prot_t max, int cow)
992 vm_map_entry_t new_entry;
993 vm_map_entry_t prev_entry;
994 vm_map_entry_t temp_entry;
995 vm_eflags_t protoeflags;
996 int must_drop = 0;
997 vm_object_t object;
999 if (maptype == VM_MAPTYPE_UKSMAP)
1000 object = NULL;
1001 else
1002 object = map_object;
1004 ASSERT_VM_MAP_LOCKED(map);
1005 if (object)
1006 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1009 * Check that the start and end points are not bogus.
1011 if ((start < map->min_offset) || (end > map->max_offset) ||
1012 (start >= end))
1013 return (KERN_INVALID_ADDRESS);
1016 * Find the entry prior to the proposed starting address; if it's part
1017 * of an existing entry, this range is bogus.
1019 if (vm_map_lookup_entry(map, start, &temp_entry))
1020 return (KERN_NO_SPACE);
1022 prev_entry = temp_entry;
1025 * Assert that the next entry doesn't overlap the end point.
1028 if ((prev_entry->next != &map->header) &&
1029 (prev_entry->next->start < end))
1030 return (KERN_NO_SPACE);
1032 protoeflags = 0;
1034 if (cow & MAP_COPY_ON_WRITE)
1035 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1037 if (cow & MAP_NOFAULT) {
1038 protoeflags |= MAP_ENTRY_NOFAULT;
1040 KASSERT(object == NULL,
1041 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1043 if (cow & MAP_DISABLE_SYNCER)
1044 protoeflags |= MAP_ENTRY_NOSYNC;
1045 if (cow & MAP_DISABLE_COREDUMP)
1046 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1047 if (cow & MAP_IS_STACK)
1048 protoeflags |= MAP_ENTRY_STACK;
1049 if (cow & MAP_IS_KSTACK)
1050 protoeflags |= MAP_ENTRY_KSTACK;
1052 lwkt_gettoken(&map->token);
1054 if (object) {
1056 * When object is non-NULL, it could be shared with another
1057 * process. We have to set or clear OBJ_ONEMAPPING
1058 * appropriately.
1060 * NOTE: This flag is only applicable to DEFAULT and SWAP
1061 * objects and will already be clear in other types
1062 * of objects, so a shared object lock is ok for
1063 * VNODE objects.
1065 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
1066 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1069 else if ((prev_entry != &map->header) &&
1070 (prev_entry->eflags == protoeflags) &&
1071 (prev_entry->end == start) &&
1072 (prev_entry->wired_count == 0) &&
1073 (prev_entry->id == id) &&
1074 prev_entry->maptype == maptype &&
1075 maptype == VM_MAPTYPE_NORMAL &&
1076 ((prev_entry->object.vm_object == NULL) ||
1077 vm_object_coalesce(prev_entry->object.vm_object,
1078 OFF_TO_IDX(prev_entry->offset),
1079 (vm_size_t)(prev_entry->end - prev_entry->start),
1080 (vm_size_t)(end - prev_entry->end)))) {
1082 * We were able to extend the object. Determine if we
1083 * can extend the previous map entry to include the
1084 * new range as well.
1086 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1087 (prev_entry->protection == prot) &&
1088 (prev_entry->max_protection == max)) {
1089 map->size += (end - prev_entry->end);
1090 prev_entry->end = end;
1091 vm_map_simplify_entry(map, prev_entry, countp);
1092 lwkt_reltoken(&map->token);
1093 return (KERN_SUCCESS);
1097 * If we can extend the object but cannot extend the
1098 * map entry, we have to create a new map entry. We
1099 * must bump the ref count on the extended object to
1100 * account for it. object may be NULL.
1102 * XXX if object is NULL should we set offset to 0 here ?
1104 object = prev_entry->object.vm_object;
1105 offset = prev_entry->offset +
1106 (prev_entry->end - prev_entry->start);
1107 if (object) {
1108 vm_object_hold(object);
1109 vm_object_chain_wait(object, 0);
1110 vm_object_reference_locked(object);
1111 must_drop = 1;
1112 map_object = object;
1117 * NOTE: if conditionals fail, object can be NULL here. This occurs
1118 * in things like the buffer map where we manage kva but do not manage
1119 * backing objects.
1123 * Create a new entry
1126 new_entry = vm_map_entry_create(map, countp);
1127 new_entry->start = start;
1128 new_entry->end = end;
1129 new_entry->id = id;
1131 new_entry->maptype = maptype;
1132 new_entry->eflags = protoeflags;
1133 new_entry->object.map_object = map_object;
1134 new_entry->aux.master_pde = 0; /* in case size is different */
1135 new_entry->aux.map_aux = map_aux;
1136 new_entry->offset = offset;
1138 new_entry->inheritance = VM_INHERIT_DEFAULT;
1139 new_entry->protection = prot;
1140 new_entry->max_protection = max;
1141 new_entry->wired_count = 0;
1144 * Insert the new entry into the list
1147 vm_map_entry_link(map, prev_entry, new_entry);
1148 map->size += new_entry->end - new_entry->start;
1151 * Update the free space hint. Entries cannot overlap.
1152 * An exact comparison is needed to avoid matching
1153 * against the map->header.
1155 if ((map->first_free == prev_entry) &&
1156 (prev_entry->end == new_entry->start)) {
1157 map->first_free = new_entry;
1160 #if 0
1162 * Temporarily removed to avoid MAP_STACK panic, due to
1163 * MAP_STACK being a huge hack. Will be added back in
1164 * when MAP_STACK (and the user stack mapping) is fixed.
1167 * It may be possible to simplify the entry
1169 vm_map_simplify_entry(map, new_entry, countp);
1170 #endif
1173 * Try to pre-populate the page table. Mappings governed by virtual
1174 * page tables cannot be prepopulated without a lot of work, so
1175 * don't try.
1177 if ((cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) &&
1178 maptype != VM_MAPTYPE_VPAGETABLE &&
1179 maptype != VM_MAPTYPE_UKSMAP) {
1180 int dorelock = 0;
1181 if (vm_map_relock_enable && (cow & MAP_PREFAULT_RELOCK)) {
1182 dorelock = 1;
1183 vm_object_lock_swap();
1184 vm_object_drop(object);
1186 pmap_object_init_pt(map->pmap, start, prot,
1187 object, OFF_TO_IDX(offset), end - start,
1188 cow & MAP_PREFAULT_PARTIAL);
1189 if (dorelock) {
1190 vm_object_hold(object);
1191 vm_object_lock_swap();
1194 if (must_drop)
1195 vm_object_drop(object);
1197 lwkt_reltoken(&map->token);
1198 return (KERN_SUCCESS);
1202 * Find sufficient space for `length' bytes in the given map, starting at
1203 * `start'. Returns 0 on success, 1 on no space.
1205 * This function will returned an arbitrarily aligned pointer. If no
1206 * particular alignment is required you should pass align as 1. Note that
1207 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
1208 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
1209 * argument.
1211 * 'align' should be a power of 2 but is not required to be.
1213 * The map must be exclusively locked.
1214 * No other requirements.
1217 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1218 vm_size_t align, int flags, vm_offset_t *addr)
1220 vm_map_entry_t entry, next;
1221 vm_offset_t end;
1222 vm_offset_t align_mask;
1224 if (start < map->min_offset)
1225 start = map->min_offset;
1226 if (start > map->max_offset)
1227 return (1);
1230 * If the alignment is not a power of 2 we will have to use
1231 * a mod/division, set align_mask to a special value.
1233 if ((align | (align - 1)) + 1 != (align << 1))
1234 align_mask = (vm_offset_t)-1;
1235 else
1236 align_mask = align - 1;
1239 * Look for the first possible address; if there's already something
1240 * at this address, we have to start after it.
1242 if (start == map->min_offset) {
1243 if ((entry = map->first_free) != &map->header)
1244 start = entry->end;
1245 } else {
1246 vm_map_entry_t tmp;
1248 if (vm_map_lookup_entry(map, start, &tmp))
1249 start = tmp->end;
1250 entry = tmp;
1254 * Look through the rest of the map, trying to fit a new region in the
1255 * gap between existing regions, or after the very last region.
1257 for (;; start = (entry = next)->end) {
1259 * Adjust the proposed start by the requested alignment,
1260 * be sure that we didn't wrap the address.
1262 if (align_mask == (vm_offset_t)-1)
1263 end = roundup(start, align);
1264 else
1265 end = (start + align_mask) & ~align_mask;
1266 if (end < start)
1267 return (1);
1268 start = end;
1270 * Find the end of the proposed new region. Be sure we didn't
1271 * go beyond the end of the map, or wrap around the address.
1272 * Then check to see if this is the last entry or if the
1273 * proposed end fits in the gap between this and the next
1274 * entry.
1276 end = start + length;
1277 if (end > map->max_offset || end < start)
1278 return (1);
1279 next = entry->next;
1282 * If the next entry's start address is beyond the desired
1283 * end address we may have found a good entry.
1285 * If the next entry is a stack mapping we do not map into
1286 * the stack's reserved space.
1288 * XXX continue to allow mapping into the stack's reserved
1289 * space if doing a MAP_STACK mapping inside a MAP_STACK
1290 * mapping, for backwards compatibility. But the caller
1291 * really should use MAP_STACK | MAP_TRYFIXED if they
1292 * want to do that.
1294 if (next == &map->header)
1295 break;
1296 if (next->start >= end) {
1297 if ((next->eflags & MAP_ENTRY_STACK) == 0)
1298 break;
1299 if (flags & MAP_STACK)
1300 break;
1301 if (next->start - next->aux.avail_ssize >= end)
1302 break;
1305 map->hint = entry;
1308 * Grow the kernel_map if necessary. pmap_growkernel() will panic
1309 * if it fails. The kernel_map is locked and nothing can steal
1310 * our address space if pmap_growkernel() blocks.
1312 * NOTE: This may be unconditionally called for kldload areas on
1313 * x86_64 because these do not bump kernel_vm_end (which would
1314 * fill 128G worth of page tables!). Therefore we must not
1315 * retry.
1317 if (map == &kernel_map) {
1318 vm_offset_t kstop;
1320 kstop = round_page(start + length);
1321 if (kstop > kernel_vm_end)
1322 pmap_growkernel(start, kstop);
1324 *addr = start;
1325 return (0);
1329 * vm_map_find finds an unallocated region in the target address map with
1330 * the given length and allocates it. The search is defined to be first-fit
1331 * from the specified address; the region found is returned in the same
1332 * parameter.
1334 * If object is non-NULL, ref count must be bumped by caller
1335 * prior to making call to account for the new entry.
1337 * No requirements. This function will lock the map temporarily.
1340 vm_map_find(vm_map_t map, void *map_object, void *map_aux,
1341 vm_ooffset_t offset, vm_offset_t *addr,
1342 vm_size_t length, vm_size_t align, boolean_t fitit,
1343 vm_maptype_t maptype, vm_subsys_t id,
1344 vm_prot_t prot, vm_prot_t max, int cow)
1346 vm_offset_t start;
1347 vm_object_t object;
1348 int result;
1349 int count;
1351 if (maptype == VM_MAPTYPE_UKSMAP)
1352 object = NULL;
1353 else
1354 object = map_object;
1356 start = *addr;
1358 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1359 vm_map_lock(map);
1360 if (object)
1361 vm_object_hold_shared(object);
1362 if (fitit) {
1363 if (vm_map_findspace(map, start, length, align, 0, addr)) {
1364 if (object)
1365 vm_object_drop(object);
1366 vm_map_unlock(map);
1367 vm_map_entry_release(count);
1368 return (KERN_NO_SPACE);
1370 start = *addr;
1372 result = vm_map_insert(map, &count, map_object, map_aux,
1373 offset, start, start + length,
1374 maptype, id, prot, max, cow);
1375 if (object)
1376 vm_object_drop(object);
1377 vm_map_unlock(map);
1378 vm_map_entry_release(count);
1380 return (result);
1384 * Simplify the given map entry by merging with either neighbor. This
1385 * routine also has the ability to merge with both neighbors.
1387 * This routine guarentees that the passed entry remains valid (though
1388 * possibly extended). When merging, this routine may delete one or
1389 * both neighbors. No action is taken on entries which have their
1390 * in-transition flag set.
1392 * The map must be exclusively locked.
1394 void
1395 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
1397 vm_map_entry_t next, prev;
1398 vm_size_t prevsize, esize;
1400 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1401 ++mycpu->gd_cnt.v_intrans_coll;
1402 return;
1405 if (entry->maptype == VM_MAPTYPE_SUBMAP)
1406 return;
1407 if (entry->maptype == VM_MAPTYPE_UKSMAP)
1408 return;
1410 prev = entry->prev;
1411 if (prev != &map->header) {
1412 prevsize = prev->end - prev->start;
1413 if ( (prev->end == entry->start) &&
1414 (prev->maptype == entry->maptype) &&
1415 (prev->object.vm_object == entry->object.vm_object) &&
1416 (!prev->object.vm_object ||
1417 (prev->offset + prevsize == entry->offset)) &&
1418 (prev->eflags == entry->eflags) &&
1419 (prev->protection == entry->protection) &&
1420 (prev->max_protection == entry->max_protection) &&
1421 (prev->inheritance == entry->inheritance) &&
1422 (prev->id == entry->id) &&
1423 (prev->wired_count == entry->wired_count)) {
1424 if (map->first_free == prev)
1425 map->first_free = entry;
1426 if (map->hint == prev)
1427 map->hint = entry;
1428 vm_map_entry_unlink(map, prev);
1429 entry->start = prev->start;
1430 entry->offset = prev->offset;
1431 if (prev->object.vm_object)
1432 vm_object_deallocate(prev->object.vm_object);
1433 vm_map_entry_dispose(map, prev, countp);
1437 next = entry->next;
1438 if (next != &map->header) {
1439 esize = entry->end - entry->start;
1440 if ((entry->end == next->start) &&
1441 (next->maptype == entry->maptype) &&
1442 (next->object.vm_object == entry->object.vm_object) &&
1443 (!entry->object.vm_object ||
1444 (entry->offset + esize == next->offset)) &&
1445 (next->eflags == entry->eflags) &&
1446 (next->protection == entry->protection) &&
1447 (next->max_protection == entry->max_protection) &&
1448 (next->inheritance == entry->inheritance) &&
1449 (next->id == entry->id) &&
1450 (next->wired_count == entry->wired_count)) {
1451 if (map->first_free == next)
1452 map->first_free = entry;
1453 if (map->hint == next)
1454 map->hint = entry;
1455 vm_map_entry_unlink(map, next);
1456 entry->end = next->end;
1457 if (next->object.vm_object)
1458 vm_object_deallocate(next->object.vm_object);
1459 vm_map_entry_dispose(map, next, countp);
1465 * Asserts that the given entry begins at or after the specified address.
1466 * If necessary, it splits the entry into two.
1468 #define vm_map_clip_start(map, entry, startaddr, countp) \
1470 if (startaddr > entry->start) \
1471 _vm_map_clip_start(map, entry, startaddr, countp); \
1475 * This routine is called only when it is known that the entry must be split.
1477 * The map must be exclusively locked.
1479 static void
1480 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start,
1481 int *countp)
1483 vm_map_entry_t new_entry;
1486 * Split off the front portion -- note that we must insert the new
1487 * entry BEFORE this one, so that this entry has the specified
1488 * starting address.
1491 vm_map_simplify_entry(map, entry, countp);
1494 * If there is no object backing this entry, we might as well create
1495 * one now. If we defer it, an object can get created after the map
1496 * is clipped, and individual objects will be created for the split-up
1497 * map. This is a bit of a hack, but is also about the best place to
1498 * put this improvement.
1500 if (entry->object.vm_object == NULL && !map->system_map) {
1501 vm_map_entry_allocate_object(entry);
1504 new_entry = vm_map_entry_create(map, countp);
1505 *new_entry = *entry;
1507 new_entry->end = start;
1508 entry->offset += (start - entry->start);
1509 entry->start = start;
1511 vm_map_entry_link(map, entry->prev, new_entry);
1513 switch(entry->maptype) {
1514 case VM_MAPTYPE_NORMAL:
1515 case VM_MAPTYPE_VPAGETABLE:
1516 if (new_entry->object.vm_object) {
1517 vm_object_hold(new_entry->object.vm_object);
1518 vm_object_chain_wait(new_entry->object.vm_object, 0);
1519 vm_object_reference_locked(new_entry->object.vm_object);
1520 vm_object_drop(new_entry->object.vm_object);
1522 break;
1523 default:
1524 break;
1529 * Asserts that the given entry ends at or before the specified address.
1530 * If necessary, it splits the entry into two.
1532 * The map must be exclusively locked.
1534 #define vm_map_clip_end(map, entry, endaddr, countp) \
1536 if (endaddr < entry->end) \
1537 _vm_map_clip_end(map, entry, endaddr, countp); \
1541 * This routine is called only when it is known that the entry must be split.
1543 * The map must be exclusively locked.
1545 static void
1546 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end,
1547 int *countp)
1549 vm_map_entry_t new_entry;
1552 * If there is no object backing this entry, we might as well create
1553 * one now. If we defer it, an object can get created after the map
1554 * is clipped, and individual objects will be created for the split-up
1555 * map. This is a bit of a hack, but is also about the best place to
1556 * put this improvement.
1559 if (entry->object.vm_object == NULL && !map->system_map) {
1560 vm_map_entry_allocate_object(entry);
1564 * Create a new entry and insert it AFTER the specified entry
1567 new_entry = vm_map_entry_create(map, countp);
1568 *new_entry = *entry;
1570 new_entry->start = entry->end = end;
1571 new_entry->offset += (end - entry->start);
1573 vm_map_entry_link(map, entry, new_entry);
1575 switch(entry->maptype) {
1576 case VM_MAPTYPE_NORMAL:
1577 case VM_MAPTYPE_VPAGETABLE:
1578 if (new_entry->object.vm_object) {
1579 vm_object_hold(new_entry->object.vm_object);
1580 vm_object_chain_wait(new_entry->object.vm_object, 0);
1581 vm_object_reference_locked(new_entry->object.vm_object);
1582 vm_object_drop(new_entry->object.vm_object);
1584 break;
1585 default:
1586 break;
1591 * Asserts that the starting and ending region addresses fall within the
1592 * valid range for the map.
1594 #define VM_MAP_RANGE_CHECK(map, start, end) \
1596 if (start < vm_map_min(map)) \
1597 start = vm_map_min(map); \
1598 if (end > vm_map_max(map)) \
1599 end = vm_map_max(map); \
1600 if (start > end) \
1601 start = end; \
1605 * Used to block when an in-transition collison occurs. The map
1606 * is unlocked for the sleep and relocked before the return.
1608 void
1609 vm_map_transition_wait(vm_map_t map)
1611 tsleep_interlock(map, 0);
1612 vm_map_unlock(map);
1613 tsleep(map, PINTERLOCKED, "vment", 0);
1614 vm_map_lock(map);
1618 * When we do blocking operations with the map lock held it is
1619 * possible that a clip might have occured on our in-transit entry,
1620 * requiring an adjustment to the entry in our loop. These macros
1621 * help the pageable and clip_range code deal with the case. The
1622 * conditional costs virtually nothing if no clipping has occured.
1625 #define CLIP_CHECK_BACK(entry, save_start) \
1626 do { \
1627 while (entry->start != save_start) { \
1628 entry = entry->prev; \
1629 KASSERT(entry != &map->header, ("bad entry clip")); \
1631 } while(0)
1633 #define CLIP_CHECK_FWD(entry, save_end) \
1634 do { \
1635 while (entry->end != save_end) { \
1636 entry = entry->next; \
1637 KASSERT(entry != &map->header, ("bad entry clip")); \
1639 } while(0)
1643 * Clip the specified range and return the base entry. The
1644 * range may cover several entries starting at the returned base
1645 * and the first and last entry in the covering sequence will be
1646 * properly clipped to the requested start and end address.
1648 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1649 * flag.
1651 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1652 * covered by the requested range.
1654 * The map must be exclusively locked on entry and will remain locked
1655 * on return. If no range exists or the range contains holes and you
1656 * specified that no holes were allowed, NULL will be returned. This
1657 * routine may temporarily unlock the map in order avoid a deadlock when
1658 * sleeping.
1660 static
1661 vm_map_entry_t
1662 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1663 int *countp, int flags)
1665 vm_map_entry_t start_entry;
1666 vm_map_entry_t entry;
1669 * Locate the entry and effect initial clipping. The in-transition
1670 * case does not occur very often so do not try to optimize it.
1672 again:
1673 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1674 return (NULL);
1675 entry = start_entry;
1676 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1677 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1678 ++mycpu->gd_cnt.v_intrans_coll;
1679 ++mycpu->gd_cnt.v_intrans_wait;
1680 vm_map_transition_wait(map);
1682 * entry and/or start_entry may have been clipped while
1683 * we slept, or may have gone away entirely. We have
1684 * to restart from the lookup.
1686 goto again;
1690 * Since we hold an exclusive map lock we do not have to restart
1691 * after clipping, even though clipping may block in zalloc.
1693 vm_map_clip_start(map, entry, start, countp);
1694 vm_map_clip_end(map, entry, end, countp);
1695 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1698 * Scan entries covered by the range. When working on the next
1699 * entry a restart need only re-loop on the current entry which
1700 * we have already locked, since 'next' may have changed. Also,
1701 * even though entry is safe, it may have been clipped so we
1702 * have to iterate forwards through the clip after sleeping.
1704 while (entry->next != &map->header && entry->next->start < end) {
1705 vm_map_entry_t next = entry->next;
1707 if (flags & MAP_CLIP_NO_HOLES) {
1708 if (next->start > entry->end) {
1709 vm_map_unclip_range(map, start_entry,
1710 start, entry->end, countp, flags);
1711 return(NULL);
1715 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1716 vm_offset_t save_end = entry->end;
1717 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1718 ++mycpu->gd_cnt.v_intrans_coll;
1719 ++mycpu->gd_cnt.v_intrans_wait;
1720 vm_map_transition_wait(map);
1723 * clips might have occured while we blocked.
1725 CLIP_CHECK_FWD(entry, save_end);
1726 CLIP_CHECK_BACK(start_entry, start);
1727 continue;
1730 * No restart necessary even though clip_end may block, we
1731 * are holding the map lock.
1733 vm_map_clip_end(map, next, end, countp);
1734 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1735 entry = next;
1737 if (flags & MAP_CLIP_NO_HOLES) {
1738 if (entry->end != end) {
1739 vm_map_unclip_range(map, start_entry,
1740 start, entry->end, countp, flags);
1741 return(NULL);
1744 return(start_entry);
1748 * Undo the effect of vm_map_clip_range(). You should pass the same
1749 * flags and the same range that you passed to vm_map_clip_range().
1750 * This code will clear the in-transition flag on the entries and
1751 * wake up anyone waiting. This code will also simplify the sequence
1752 * and attempt to merge it with entries before and after the sequence.
1754 * The map must be locked on entry and will remain locked on return.
1756 * Note that you should also pass the start_entry returned by
1757 * vm_map_clip_range(). However, if you block between the two calls
1758 * with the map unlocked please be aware that the start_entry may
1759 * have been clipped and you may need to scan it backwards to find
1760 * the entry corresponding with the original start address. You are
1761 * responsible for this, vm_map_unclip_range() expects the correct
1762 * start_entry to be passed to it and will KASSERT otherwise.
1764 static
1765 void
1766 vm_map_unclip_range(vm_map_t map, vm_map_entry_t start_entry,
1767 vm_offset_t start, vm_offset_t end,
1768 int *countp, int flags)
1770 vm_map_entry_t entry;
1772 entry = start_entry;
1774 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1775 while (entry != &map->header && entry->start < end) {
1776 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1777 ("in-transition flag not set during unclip on: %p",
1778 entry));
1779 KASSERT(entry->end <= end,
1780 ("unclip_range: tail wasn't clipped"));
1781 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1782 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1783 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1784 wakeup(map);
1786 entry = entry->next;
1790 * Simplification does not block so there is no restart case.
1792 entry = start_entry;
1793 while (entry != &map->header && entry->start < end) {
1794 vm_map_simplify_entry(map, entry, countp);
1795 entry = entry->next;
1800 * Mark the given range as handled by a subordinate map.
1802 * This range must have been created with vm_map_find(), and no other
1803 * operations may have been performed on this range prior to calling
1804 * vm_map_submap().
1806 * Submappings cannot be removed.
1808 * No requirements.
1811 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
1813 vm_map_entry_t entry;
1814 int result = KERN_INVALID_ARGUMENT;
1815 int count;
1817 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1818 vm_map_lock(map);
1820 VM_MAP_RANGE_CHECK(map, start, end);
1822 if (vm_map_lookup_entry(map, start, &entry)) {
1823 vm_map_clip_start(map, entry, start, &count);
1824 } else {
1825 entry = entry->next;
1828 vm_map_clip_end(map, entry, end, &count);
1830 if ((entry->start == start) && (entry->end == end) &&
1831 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1832 (entry->object.vm_object == NULL)) {
1833 entry->object.sub_map = submap;
1834 entry->maptype = VM_MAPTYPE_SUBMAP;
1835 result = KERN_SUCCESS;
1837 vm_map_unlock(map);
1838 vm_map_entry_release(count);
1840 return (result);
1844 * Sets the protection of the specified address region in the target map.
1845 * If "set_max" is specified, the maximum protection is to be set;
1846 * otherwise, only the current protection is affected.
1848 * The protection is not applicable to submaps, but is applicable to normal
1849 * maps and maps governed by virtual page tables. For example, when operating
1850 * on a virtual page table our protection basically controls how COW occurs
1851 * on the backing object, whereas the virtual page table abstraction itself
1852 * is an abstraction for userland.
1854 * No requirements.
1857 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1858 vm_prot_t new_prot, boolean_t set_max)
1860 vm_map_entry_t current;
1861 vm_map_entry_t entry;
1862 int count;
1864 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1865 vm_map_lock(map);
1867 VM_MAP_RANGE_CHECK(map, start, end);
1869 if (vm_map_lookup_entry(map, start, &entry)) {
1870 vm_map_clip_start(map, entry, start, &count);
1871 } else {
1872 entry = entry->next;
1876 * Make a first pass to check for protection violations.
1878 current = entry;
1879 while ((current != &map->header) && (current->start < end)) {
1880 if (current->maptype == VM_MAPTYPE_SUBMAP) {
1881 vm_map_unlock(map);
1882 vm_map_entry_release(count);
1883 return (KERN_INVALID_ARGUMENT);
1885 if ((new_prot & current->max_protection) != new_prot) {
1886 vm_map_unlock(map);
1887 vm_map_entry_release(count);
1888 return (KERN_PROTECTION_FAILURE);
1890 current = current->next;
1894 * Go back and fix up protections. [Note that clipping is not
1895 * necessary the second time.]
1897 current = entry;
1899 while ((current != &map->header) && (current->start < end)) {
1900 vm_prot_t old_prot;
1902 vm_map_clip_end(map, current, end, &count);
1904 old_prot = current->protection;
1905 if (set_max) {
1906 current->protection =
1907 (current->max_protection = new_prot) &
1908 old_prot;
1909 } else {
1910 current->protection = new_prot;
1914 * Update physical map if necessary. Worry about copy-on-write
1915 * here -- CHECK THIS XXX
1918 if (current->protection != old_prot) {
1919 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1920 VM_PROT_ALL)
1922 pmap_protect(map->pmap, current->start,
1923 current->end,
1924 current->protection & MASK(current));
1925 #undef MASK
1928 vm_map_simplify_entry(map, current, &count);
1930 current = current->next;
1933 vm_map_unlock(map);
1934 vm_map_entry_release(count);
1935 return (KERN_SUCCESS);
1939 * This routine traverses a processes map handling the madvise
1940 * system call. Advisories are classified as either those effecting
1941 * the vm_map_entry structure, or those effecting the underlying
1942 * objects.
1944 * The <value> argument is used for extended madvise calls.
1946 * No requirements.
1949 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end,
1950 int behav, off_t value)
1952 vm_map_entry_t current, entry;
1953 int modify_map = 0;
1954 int error = 0;
1955 int count;
1958 * Some madvise calls directly modify the vm_map_entry, in which case
1959 * we need to use an exclusive lock on the map and we need to perform
1960 * various clipping operations. Otherwise we only need a read-lock
1961 * on the map.
1964 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1966 switch(behav) {
1967 case MADV_NORMAL:
1968 case MADV_SEQUENTIAL:
1969 case MADV_RANDOM:
1970 case MADV_NOSYNC:
1971 case MADV_AUTOSYNC:
1972 case MADV_NOCORE:
1973 case MADV_CORE:
1974 case MADV_SETMAP:
1975 case MADV_INVAL:
1976 modify_map = 1;
1977 vm_map_lock(map);
1978 break;
1979 case MADV_WILLNEED:
1980 case MADV_DONTNEED:
1981 case MADV_FREE:
1982 vm_map_lock_read(map);
1983 break;
1984 default:
1985 vm_map_entry_release(count);
1986 return (EINVAL);
1990 * Locate starting entry and clip if necessary.
1993 VM_MAP_RANGE_CHECK(map, start, end);
1995 if (vm_map_lookup_entry(map, start, &entry)) {
1996 if (modify_map)
1997 vm_map_clip_start(map, entry, start, &count);
1998 } else {
1999 entry = entry->next;
2002 if (modify_map) {
2004 * madvise behaviors that are implemented in the vm_map_entry.
2006 * We clip the vm_map_entry so that behavioral changes are
2007 * limited to the specified address range.
2009 for (current = entry;
2010 (current != &map->header) && (current->start < end);
2011 current = current->next
2013 if (current->maptype == VM_MAPTYPE_SUBMAP)
2014 continue;
2016 vm_map_clip_end(map, current, end, &count);
2018 switch (behav) {
2019 case MADV_NORMAL:
2020 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2021 break;
2022 case MADV_SEQUENTIAL:
2023 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2024 break;
2025 case MADV_RANDOM:
2026 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2027 break;
2028 case MADV_NOSYNC:
2029 current->eflags |= MAP_ENTRY_NOSYNC;
2030 break;
2031 case MADV_AUTOSYNC:
2032 current->eflags &= ~MAP_ENTRY_NOSYNC;
2033 break;
2034 case MADV_NOCORE:
2035 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2036 break;
2037 case MADV_CORE:
2038 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2039 break;
2040 case MADV_INVAL:
2042 * Invalidate the related pmap entries, used
2043 * to flush portions of the real kernel's
2044 * pmap when the caller has removed or
2045 * modified existing mappings in a virtual
2046 * page table.
2048 pmap_remove(map->pmap,
2049 current->start, current->end);
2050 break;
2051 case MADV_SETMAP:
2053 * Set the page directory page for a map
2054 * governed by a virtual page table. Mark
2055 * the entry as being governed by a virtual
2056 * page table if it is not.
2058 * XXX the page directory page is stored
2059 * in the avail_ssize field if the map_entry.
2061 * XXX the map simplification code does not
2062 * compare this field so weird things may
2063 * happen if you do not apply this function
2064 * to the entire mapping governed by the
2065 * virtual page table.
2067 if (current->maptype != VM_MAPTYPE_VPAGETABLE) {
2068 error = EINVAL;
2069 break;
2071 current->aux.master_pde = value;
2072 pmap_remove(map->pmap,
2073 current->start, current->end);
2074 break;
2075 default:
2076 error = EINVAL;
2077 break;
2079 vm_map_simplify_entry(map, current, &count);
2081 vm_map_unlock(map);
2082 } else {
2083 vm_pindex_t pindex;
2084 int count;
2087 * madvise behaviors that are implemented in the underlying
2088 * vm_object.
2090 * Since we don't clip the vm_map_entry, we have to clip
2091 * the vm_object pindex and count.
2093 * NOTE! We currently do not support these functions on
2094 * virtual page tables.
2096 for (current = entry;
2097 (current != &map->header) && (current->start < end);
2098 current = current->next
2100 vm_offset_t useStart;
2102 if (current->maptype != VM_MAPTYPE_NORMAL)
2103 continue;
2105 pindex = OFF_TO_IDX(current->offset);
2106 count = atop(current->end - current->start);
2107 useStart = current->start;
2109 if (current->start < start) {
2110 pindex += atop(start - current->start);
2111 count -= atop(start - current->start);
2112 useStart = start;
2114 if (current->end > end)
2115 count -= atop(current->end - end);
2117 if (count <= 0)
2118 continue;
2120 vm_object_madvise(current->object.vm_object,
2121 pindex, count, behav);
2124 * Try to populate the page table. Mappings governed
2125 * by virtual page tables cannot be pre-populated
2126 * without a lot of work so don't try.
2128 if (behav == MADV_WILLNEED &&
2129 current->maptype != VM_MAPTYPE_VPAGETABLE) {
2130 pmap_object_init_pt(
2131 map->pmap,
2132 useStart,
2133 current->protection,
2134 current->object.vm_object,
2135 pindex,
2136 (count << PAGE_SHIFT),
2137 MAP_PREFAULT_MADVISE
2141 vm_map_unlock_read(map);
2143 vm_map_entry_release(count);
2144 return(error);
2149 * Sets the inheritance of the specified address range in the target map.
2150 * Inheritance affects how the map will be shared with child maps at the
2151 * time of vm_map_fork.
2154 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2155 vm_inherit_t new_inheritance)
2157 vm_map_entry_t entry;
2158 vm_map_entry_t temp_entry;
2159 int count;
2161 switch (new_inheritance) {
2162 case VM_INHERIT_NONE:
2163 case VM_INHERIT_COPY:
2164 case VM_INHERIT_SHARE:
2165 break;
2166 default:
2167 return (KERN_INVALID_ARGUMENT);
2170 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2171 vm_map_lock(map);
2173 VM_MAP_RANGE_CHECK(map, start, end);
2175 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2176 entry = temp_entry;
2177 vm_map_clip_start(map, entry, start, &count);
2178 } else
2179 entry = temp_entry->next;
2181 while ((entry != &map->header) && (entry->start < end)) {
2182 vm_map_clip_end(map, entry, end, &count);
2184 entry->inheritance = new_inheritance;
2186 vm_map_simplify_entry(map, entry, &count);
2188 entry = entry->next;
2190 vm_map_unlock(map);
2191 vm_map_entry_release(count);
2192 return (KERN_SUCCESS);
2196 * Implement the semantics of mlock
2199 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
2200 boolean_t new_pageable)
2202 vm_map_entry_t entry;
2203 vm_map_entry_t start_entry;
2204 vm_offset_t end;
2205 int rv = KERN_SUCCESS;
2206 int count;
2208 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2209 vm_map_lock(map);
2210 VM_MAP_RANGE_CHECK(map, start, real_end);
2211 end = real_end;
2213 start_entry = vm_map_clip_range(map, start, end, &count,
2214 MAP_CLIP_NO_HOLES);
2215 if (start_entry == NULL) {
2216 vm_map_unlock(map);
2217 vm_map_entry_release(count);
2218 return (KERN_INVALID_ADDRESS);
2221 if (new_pageable == 0) {
2222 entry = start_entry;
2223 while ((entry != &map->header) && (entry->start < end)) {
2224 vm_offset_t save_start;
2225 vm_offset_t save_end;
2228 * Already user wired or hard wired (trivial cases)
2230 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
2231 entry = entry->next;
2232 continue;
2234 if (entry->wired_count != 0) {
2235 entry->wired_count++;
2236 entry->eflags |= MAP_ENTRY_USER_WIRED;
2237 entry = entry->next;
2238 continue;
2242 * A new wiring requires instantiation of appropriate
2243 * management structures and the faulting in of the
2244 * page.
2246 if (entry->maptype == VM_MAPTYPE_NORMAL ||
2247 entry->maptype == VM_MAPTYPE_VPAGETABLE) {
2248 int copyflag = entry->eflags &
2249 MAP_ENTRY_NEEDS_COPY;
2250 if (copyflag && ((entry->protection &
2251 VM_PROT_WRITE) != 0)) {
2252 vm_map_entry_shadow(entry, 0);
2253 } else if (entry->object.vm_object == NULL &&
2254 !map->system_map) {
2255 vm_map_entry_allocate_object(entry);
2258 entry->wired_count++;
2259 entry->eflags |= MAP_ENTRY_USER_WIRED;
2262 * Now fault in the area. Note that vm_fault_wire()
2263 * may release the map lock temporarily, it will be
2264 * relocked on return. The in-transition
2265 * flag protects the entries.
2267 save_start = entry->start;
2268 save_end = entry->end;
2269 rv = vm_fault_wire(map, entry, TRUE, 0);
2270 if (rv) {
2271 CLIP_CHECK_BACK(entry, save_start);
2272 for (;;) {
2273 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
2274 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2275 entry->wired_count = 0;
2276 if (entry->end == save_end)
2277 break;
2278 entry = entry->next;
2279 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2281 end = save_start; /* unwire the rest */
2282 break;
2285 * note that even though the entry might have been
2286 * clipped, the USER_WIRED flag we set prevents
2287 * duplication so we do not have to do a
2288 * clip check.
2290 entry = entry->next;
2294 * If we failed fall through to the unwiring section to
2295 * unwire what we had wired so far. 'end' has already
2296 * been adjusted.
2298 if (rv)
2299 new_pageable = 1;
2302 * start_entry might have been clipped if we unlocked the
2303 * map and blocked. No matter how clipped it has gotten
2304 * there should be a fragment that is on our start boundary.
2306 CLIP_CHECK_BACK(start_entry, start);
2310 * Deal with the unwiring case.
2312 if (new_pageable) {
2314 * This is the unwiring case. We must first ensure that the
2315 * range to be unwired is really wired down. We know there
2316 * are no holes.
2318 entry = start_entry;
2319 while ((entry != &map->header) && (entry->start < end)) {
2320 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2321 rv = KERN_INVALID_ARGUMENT;
2322 goto done;
2324 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
2325 entry = entry->next;
2329 * Now decrement the wiring count for each region. If a region
2330 * becomes completely unwired, unwire its physical pages and
2331 * mappings.
2334 * The map entries are processed in a loop, checking to
2335 * make sure the entry is wired and asserting it has a wired
2336 * count. However, another loop was inserted more-or-less in
2337 * the middle of the unwiring path. This loop picks up the
2338 * "entry" loop variable from the first loop without first
2339 * setting it to start_entry. Naturally, the secound loop
2340 * is never entered and the pages backing the entries are
2341 * never unwired. This can lead to a leak of wired pages.
2343 entry = start_entry;
2344 while ((entry != &map->header) && (entry->start < end)) {
2345 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED,
2346 ("expected USER_WIRED on entry %p", entry));
2347 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2348 entry->wired_count--;
2349 if (entry->wired_count == 0)
2350 vm_fault_unwire(map, entry);
2351 entry = entry->next;
2354 done:
2355 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2356 MAP_CLIP_NO_HOLES);
2357 map->timestamp++;
2358 vm_map_unlock(map);
2359 vm_map_entry_release(count);
2360 return (rv);
2364 * Sets the pageability of the specified address range in the target map.
2365 * Regions specified as not pageable require locked-down physical
2366 * memory and physical page maps.
2368 * The map must not be locked, but a reference must remain to the map
2369 * throughout the call.
2371 * This function may be called via the zalloc path and must properly
2372 * reserve map entries for kernel_map.
2374 * No requirements.
2377 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
2379 vm_map_entry_t entry;
2380 vm_map_entry_t start_entry;
2381 vm_offset_t end;
2382 int rv = KERN_SUCCESS;
2383 int count;
2385 if (kmflags & KM_KRESERVE)
2386 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
2387 else
2388 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2389 vm_map_lock(map);
2390 VM_MAP_RANGE_CHECK(map, start, real_end);
2391 end = real_end;
2393 start_entry = vm_map_clip_range(map, start, end, &count,
2394 MAP_CLIP_NO_HOLES);
2395 if (start_entry == NULL) {
2396 vm_map_unlock(map);
2397 rv = KERN_INVALID_ADDRESS;
2398 goto failure;
2400 if ((kmflags & KM_PAGEABLE) == 0) {
2402 * Wiring.
2404 * 1. Holding the write lock, we create any shadow or zero-fill
2405 * objects that need to be created. Then we clip each map
2406 * entry to the region to be wired and increment its wiring
2407 * count. We create objects before clipping the map entries
2408 * to avoid object proliferation.
2410 * 2. We downgrade to a read lock, and call vm_fault_wire to
2411 * fault in the pages for any newly wired area (wired_count is
2412 * 1).
2414 * Downgrading to a read lock for vm_fault_wire avoids a
2415 * possible deadlock with another process that may have faulted
2416 * on one of the pages to be wired (it would mark the page busy,
2417 * blocking us, then in turn block on the map lock that we
2418 * hold). Because of problems in the recursive lock package,
2419 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2420 * any actions that require the write lock must be done
2421 * beforehand. Because we keep the read lock on the map, the
2422 * copy-on-write status of the entries we modify here cannot
2423 * change.
2425 entry = start_entry;
2426 while ((entry != &map->header) && (entry->start < end)) {
2428 * Trivial case if the entry is already wired
2430 if (entry->wired_count) {
2431 entry->wired_count++;
2432 entry = entry->next;
2433 continue;
2437 * The entry is being newly wired, we have to setup
2438 * appropriate management structures. A shadow
2439 * object is required for a copy-on-write region,
2440 * or a normal object for a zero-fill region. We
2441 * do not have to do this for entries that point to sub
2442 * maps because we won't hold the lock on the sub map.
2444 if (entry->maptype == VM_MAPTYPE_NORMAL ||
2445 entry->maptype == VM_MAPTYPE_VPAGETABLE) {
2446 int copyflag = entry->eflags &
2447 MAP_ENTRY_NEEDS_COPY;
2448 if (copyflag && ((entry->protection &
2449 VM_PROT_WRITE) != 0)) {
2450 vm_map_entry_shadow(entry, 0);
2451 } else if (entry->object.vm_object == NULL &&
2452 !map->system_map) {
2453 vm_map_entry_allocate_object(entry);
2457 entry->wired_count++;
2458 entry = entry->next;
2462 * Pass 2.
2466 * HACK HACK HACK HACK
2468 * vm_fault_wire() temporarily unlocks the map to avoid
2469 * deadlocks. The in-transition flag from vm_map_clip_range
2470 * call should protect us from changes while the map is
2471 * unlocked. T
2473 * NOTE: Previously this comment stated that clipping might
2474 * still occur while the entry is unlocked, but from
2475 * what I can tell it actually cannot.
2477 * It is unclear whether the CLIP_CHECK_*() calls
2478 * are still needed but we keep them in anyway.
2480 * HACK HACK HACK HACK
2483 entry = start_entry;
2484 while (entry != &map->header && entry->start < end) {
2486 * If vm_fault_wire fails for any page we need to undo
2487 * what has been done. We decrement the wiring count
2488 * for those pages which have not yet been wired (now)
2489 * and unwire those that have (later).
2491 vm_offset_t save_start = entry->start;
2492 vm_offset_t save_end = entry->end;
2494 if (entry->wired_count == 1)
2495 rv = vm_fault_wire(map, entry, FALSE, kmflags);
2496 if (rv) {
2497 CLIP_CHECK_BACK(entry, save_start);
2498 for (;;) {
2499 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
2500 entry->wired_count = 0;
2501 if (entry->end == save_end)
2502 break;
2503 entry = entry->next;
2504 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2506 end = save_start;
2507 break;
2509 CLIP_CHECK_FWD(entry, save_end);
2510 entry = entry->next;
2514 * If a failure occured undo everything by falling through
2515 * to the unwiring code. 'end' has already been adjusted
2516 * appropriately.
2518 if (rv)
2519 kmflags |= KM_PAGEABLE;
2522 * start_entry is still IN_TRANSITION but may have been
2523 * clipped since vm_fault_wire() unlocks and relocks the
2524 * map. No matter how clipped it has gotten there should
2525 * be a fragment that is on our start boundary.
2527 CLIP_CHECK_BACK(start_entry, start);
2530 if (kmflags & KM_PAGEABLE) {
2532 * This is the unwiring case. We must first ensure that the
2533 * range to be unwired is really wired down. We know there
2534 * are no holes.
2536 entry = start_entry;
2537 while ((entry != &map->header) && (entry->start < end)) {
2538 if (entry->wired_count == 0) {
2539 rv = KERN_INVALID_ARGUMENT;
2540 goto done;
2542 entry = entry->next;
2546 * Now decrement the wiring count for each region. If a region
2547 * becomes completely unwired, unwire its physical pages and
2548 * mappings.
2550 entry = start_entry;
2551 while ((entry != &map->header) && (entry->start < end)) {
2552 entry->wired_count--;
2553 if (entry->wired_count == 0)
2554 vm_fault_unwire(map, entry);
2555 entry = entry->next;
2558 done:
2559 vm_map_unclip_range(map, start_entry, start, real_end,
2560 &count, MAP_CLIP_NO_HOLES);
2561 map->timestamp++;
2562 vm_map_unlock(map);
2563 failure:
2564 if (kmflags & KM_KRESERVE)
2565 vm_map_entry_krelease(count);
2566 else
2567 vm_map_entry_release(count);
2568 return (rv);
2572 * Mark a newly allocated address range as wired but do not fault in
2573 * the pages. The caller is expected to load the pages into the object.
2575 * The map must be locked on entry and will remain locked on return.
2576 * No other requirements.
2578 void
2579 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size,
2580 int *countp)
2582 vm_map_entry_t scan;
2583 vm_map_entry_t entry;
2585 entry = vm_map_clip_range(map, addr, addr + size,
2586 countp, MAP_CLIP_NO_HOLES);
2587 for (scan = entry;
2588 scan != &map->header && scan->start < addr + size;
2589 scan = scan->next) {
2590 KKASSERT(scan->wired_count == 0);
2591 scan->wired_count = 1;
2593 vm_map_unclip_range(map, entry, addr, addr + size,
2594 countp, MAP_CLIP_NO_HOLES);
2598 * Push any dirty cached pages in the address range to their pager.
2599 * If syncio is TRUE, dirty pages are written synchronously.
2600 * If invalidate is TRUE, any cached pages are freed as well.
2602 * This routine is called by sys_msync()
2604 * Returns an error if any part of the specified range is not mapped.
2606 * No requirements.
2609 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end,
2610 boolean_t syncio, boolean_t invalidate)
2612 vm_map_entry_t current;
2613 vm_map_entry_t entry;
2614 vm_size_t size;
2615 vm_object_t object;
2616 vm_object_t tobj;
2617 vm_ooffset_t offset;
2619 vm_map_lock_read(map);
2620 VM_MAP_RANGE_CHECK(map, start, end);
2621 if (!vm_map_lookup_entry(map, start, &entry)) {
2622 vm_map_unlock_read(map);
2623 return (KERN_INVALID_ADDRESS);
2625 lwkt_gettoken(&map->token);
2628 * Make a first pass to check for holes.
2630 for (current = entry; current->start < end; current = current->next) {
2631 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2632 lwkt_reltoken(&map->token);
2633 vm_map_unlock_read(map);
2634 return (KERN_INVALID_ARGUMENT);
2636 if (end > current->end &&
2637 (current->next == &map->header ||
2638 current->end != current->next->start)) {
2639 lwkt_reltoken(&map->token);
2640 vm_map_unlock_read(map);
2641 return (KERN_INVALID_ADDRESS);
2645 if (invalidate)
2646 pmap_remove(vm_map_pmap(map), start, end);
2649 * Make a second pass, cleaning/uncaching pages from the indicated
2650 * objects as we go.
2652 for (current = entry; current->start < end; current = current->next) {
2653 offset = current->offset + (start - current->start);
2654 size = (end <= current->end ? end : current->end) - start;
2656 switch(current->maptype) {
2657 case VM_MAPTYPE_SUBMAP:
2659 vm_map_t smap;
2660 vm_map_entry_t tentry;
2661 vm_size_t tsize;
2663 smap = current->object.sub_map;
2664 vm_map_lock_read(smap);
2665 vm_map_lookup_entry(smap, offset, &tentry);
2666 tsize = tentry->end - offset;
2667 if (tsize < size)
2668 size = tsize;
2669 object = tentry->object.vm_object;
2670 offset = tentry->offset + (offset - tentry->start);
2671 vm_map_unlock_read(smap);
2672 break;
2674 case VM_MAPTYPE_NORMAL:
2675 case VM_MAPTYPE_VPAGETABLE:
2676 object = current->object.vm_object;
2677 break;
2678 default:
2679 object = NULL;
2680 break;
2683 if (object)
2684 vm_object_hold(object);
2687 * Note that there is absolutely no sense in writing out
2688 * anonymous objects, so we track down the vnode object
2689 * to write out.
2690 * We invalidate (remove) all pages from the address space
2691 * anyway, for semantic correctness.
2693 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2694 * may start out with a NULL object.
2696 while (object && (tobj = object->backing_object) != NULL) {
2697 vm_object_hold(tobj);
2698 if (tobj == object->backing_object) {
2699 vm_object_lock_swap();
2700 offset += object->backing_object_offset;
2701 vm_object_drop(object);
2702 object = tobj;
2703 if (object->size < OFF_TO_IDX(offset + size))
2704 size = IDX_TO_OFF(object->size) -
2705 offset;
2706 break;
2708 vm_object_drop(tobj);
2710 if (object && (object->type == OBJT_VNODE) &&
2711 (current->protection & VM_PROT_WRITE) &&
2712 (object->flags & OBJ_NOMSYNC) == 0) {
2714 * Flush pages if writing is allowed, invalidate them
2715 * if invalidation requested. Pages undergoing I/O
2716 * will be ignored by vm_object_page_remove().
2718 * We cannot lock the vnode and then wait for paging
2719 * to complete without deadlocking against vm_fault.
2720 * Instead we simply call vm_object_page_remove() and
2721 * allow it to block internally on a page-by-page
2722 * basis when it encounters pages undergoing async
2723 * I/O.
2725 int flags;
2727 /* no chain wait needed for vnode objects */
2728 vm_object_reference_locked(object);
2729 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY);
2730 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2731 flags |= invalidate ? OBJPC_INVAL : 0;
2734 * When operating on a virtual page table just
2735 * flush the whole object. XXX we probably ought
2736 * to
2738 switch(current->maptype) {
2739 case VM_MAPTYPE_NORMAL:
2740 vm_object_page_clean(object,
2741 OFF_TO_IDX(offset),
2742 OFF_TO_IDX(offset + size + PAGE_MASK),
2743 flags);
2744 break;
2745 case VM_MAPTYPE_VPAGETABLE:
2746 vm_object_page_clean(object, 0, 0, flags);
2747 break;
2749 vn_unlock(((struct vnode *)object->handle));
2750 vm_object_deallocate_locked(object);
2752 if (object && invalidate &&
2753 ((object->type == OBJT_VNODE) ||
2754 (object->type == OBJT_DEVICE) ||
2755 (object->type == OBJT_MGTDEVICE))) {
2756 int clean_only =
2757 ((object->type == OBJT_DEVICE) ||
2758 (object->type == OBJT_MGTDEVICE)) ? FALSE : TRUE;
2759 /* no chain wait needed for vnode/device objects */
2760 vm_object_reference_locked(object);
2761 switch(current->maptype) {
2762 case VM_MAPTYPE_NORMAL:
2763 vm_object_page_remove(object,
2764 OFF_TO_IDX(offset),
2765 OFF_TO_IDX(offset + size + PAGE_MASK),
2766 clean_only);
2767 break;
2768 case VM_MAPTYPE_VPAGETABLE:
2769 vm_object_page_remove(object, 0, 0, clean_only);
2770 break;
2772 vm_object_deallocate_locked(object);
2774 start += size;
2775 if (object)
2776 vm_object_drop(object);
2779 lwkt_reltoken(&map->token);
2780 vm_map_unlock_read(map);
2782 return (KERN_SUCCESS);
2786 * Make the region specified by this entry pageable.
2788 * The vm_map must be exclusively locked.
2790 static void
2791 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2793 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2794 entry->wired_count = 0;
2795 vm_fault_unwire(map, entry);
2799 * Deallocate the given entry from the target map.
2801 * The vm_map must be exclusively locked.
2803 static void
2804 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2806 vm_map_entry_unlink(map, entry);
2807 map->size -= entry->end - entry->start;
2809 switch(entry->maptype) {
2810 case VM_MAPTYPE_NORMAL:
2811 case VM_MAPTYPE_VPAGETABLE:
2812 case VM_MAPTYPE_SUBMAP:
2813 vm_object_deallocate(entry->object.vm_object);
2814 break;
2815 case VM_MAPTYPE_UKSMAP:
2816 /* XXX TODO */
2817 break;
2818 default:
2819 break;
2822 vm_map_entry_dispose(map, entry, countp);
2826 * Deallocates the given address range from the target map.
2828 * The vm_map must be exclusively locked.
2831 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2833 vm_object_t object;
2834 vm_map_entry_t entry;
2835 vm_map_entry_t first_entry;
2837 ASSERT_VM_MAP_LOCKED(map);
2838 lwkt_gettoken(&map->token);
2839 again:
2841 * Find the start of the region, and clip it. Set entry to point
2842 * at the first record containing the requested address or, if no
2843 * such record exists, the next record with a greater address. The
2844 * loop will run from this point until a record beyond the termination
2845 * address is encountered.
2847 * map->hint must be adjusted to not point to anything we delete,
2848 * so set it to the entry prior to the one being deleted.
2850 * GGG see other GGG comment.
2852 if (vm_map_lookup_entry(map, start, &first_entry)) {
2853 entry = first_entry;
2854 vm_map_clip_start(map, entry, start, countp);
2855 map->hint = entry->prev; /* possible problem XXX */
2856 } else {
2857 map->hint = first_entry; /* possible problem XXX */
2858 entry = first_entry->next;
2862 * If a hole opens up prior to the current first_free then
2863 * adjust first_free. As with map->hint, map->first_free
2864 * cannot be left set to anything we might delete.
2866 if (entry == &map->header) {
2867 map->first_free = &map->header;
2868 } else if (map->first_free->start >= start) {
2869 map->first_free = entry->prev;
2873 * Step through all entries in this region
2875 while ((entry != &map->header) && (entry->start < end)) {
2876 vm_map_entry_t next;
2877 vm_offset_t s, e;
2878 vm_pindex_t offidxstart, offidxend, count;
2881 * If we hit an in-transition entry we have to sleep and
2882 * retry. It's easier (and not really slower) to just retry
2883 * since this case occurs so rarely and the hint is already
2884 * pointing at the right place. We have to reset the
2885 * start offset so as not to accidently delete an entry
2886 * another process just created in vacated space.
2888 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2889 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2890 start = entry->start;
2891 ++mycpu->gd_cnt.v_intrans_coll;
2892 ++mycpu->gd_cnt.v_intrans_wait;
2893 vm_map_transition_wait(map);
2894 goto again;
2896 vm_map_clip_end(map, entry, end, countp);
2898 s = entry->start;
2899 e = entry->end;
2900 next = entry->next;
2902 offidxstart = OFF_TO_IDX(entry->offset);
2903 count = OFF_TO_IDX(e - s);
2905 switch(entry->maptype) {
2906 case VM_MAPTYPE_NORMAL:
2907 case VM_MAPTYPE_VPAGETABLE:
2908 case VM_MAPTYPE_SUBMAP:
2909 object = entry->object.vm_object;
2910 break;
2911 default:
2912 object = NULL;
2913 break;
2917 * Unwire before removing addresses from the pmap; otherwise,
2918 * unwiring will put the entries back in the pmap.
2920 if (entry->wired_count != 0)
2921 vm_map_entry_unwire(map, entry);
2923 offidxend = offidxstart + count;
2925 if (object == &kernel_object) {
2926 vm_object_hold(object);
2927 vm_object_page_remove(object, offidxstart,
2928 offidxend, FALSE);
2929 vm_object_drop(object);
2930 } else if (object && object->type != OBJT_DEFAULT &&
2931 object->type != OBJT_SWAP) {
2933 * vnode object routines cannot be chain-locked,
2934 * but since we aren't removing pages from the
2935 * object here we can use a shared hold.
2937 vm_object_hold_shared(object);
2938 pmap_remove(map->pmap, s, e);
2939 vm_object_drop(object);
2940 } else if (object) {
2941 vm_object_hold(object);
2942 vm_object_chain_acquire(object, 0);
2943 pmap_remove(map->pmap, s, e);
2945 if (object != NULL &&
2946 object->ref_count != 1 &&
2947 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) ==
2948 OBJ_ONEMAPPING &&
2949 (object->type == OBJT_DEFAULT ||
2950 object->type == OBJT_SWAP)) {
2951 vm_object_collapse(object, NULL);
2952 vm_object_page_remove(object, offidxstart,
2953 offidxend, FALSE);
2954 if (object->type == OBJT_SWAP) {
2955 swap_pager_freespace(object,
2956 offidxstart,
2957 count);
2959 if (offidxend >= object->size &&
2960 offidxstart < object->size) {
2961 object->size = offidxstart;
2964 vm_object_chain_release(object);
2965 vm_object_drop(object);
2966 } else if (entry->maptype == VM_MAPTYPE_UKSMAP) {
2967 pmap_remove(map->pmap, s, e);
2971 * Delete the entry (which may delete the object) only after
2972 * removing all pmap entries pointing to its pages.
2973 * (Otherwise, its page frames may be reallocated, and any
2974 * modify bits will be set in the wrong object!)
2976 vm_map_entry_delete(map, entry, countp);
2977 entry = next;
2979 lwkt_reltoken(&map->token);
2980 return (KERN_SUCCESS);
2984 * Remove the given address range from the target map.
2985 * This is the exported form of vm_map_delete.
2987 * No requirements.
2990 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2992 int result;
2993 int count;
2995 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2996 vm_map_lock(map);
2997 VM_MAP_RANGE_CHECK(map, start, end);
2998 result = vm_map_delete(map, start, end, &count);
2999 vm_map_unlock(map);
3000 vm_map_entry_release(count);
3002 return (result);
3006 * Assert that the target map allows the specified privilege on the
3007 * entire address region given. The entire region must be allocated.
3009 * The caller must specify whether the vm_map is already locked or not.
3011 boolean_t
3012 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3013 vm_prot_t protection, boolean_t have_lock)
3015 vm_map_entry_t entry;
3016 vm_map_entry_t tmp_entry;
3017 boolean_t result;
3019 if (have_lock == FALSE)
3020 vm_map_lock_read(map);
3022 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
3023 if (have_lock == FALSE)
3024 vm_map_unlock_read(map);
3025 return (FALSE);
3027 entry = tmp_entry;
3029 result = TRUE;
3030 while (start < end) {
3031 if (entry == &map->header) {
3032 result = FALSE;
3033 break;
3036 * No holes allowed!
3039 if (start < entry->start) {
3040 result = FALSE;
3041 break;
3044 * Check protection associated with entry.
3047 if ((entry->protection & protection) != protection) {
3048 result = FALSE;
3049 break;
3051 /* go to next entry */
3053 start = entry->end;
3054 entry = entry->next;
3056 if (have_lock == FALSE)
3057 vm_map_unlock_read(map);
3058 return (result);
3062 * If appropriate this function shadows the original object with a new object
3063 * and moves the VM pages from the original object to the new object.
3064 * The original object will also be collapsed, if possible.
3066 * We can only do this for normal memory objects with a single mapping, and
3067 * it only makes sense to do it if there are 2 or more refs on the original
3068 * object. i.e. typically a memory object that has been extended into
3069 * multiple vm_map_entry's with non-overlapping ranges.
3071 * This makes it easier to remove unused pages and keeps object inheritance
3072 * from being a negative impact on memory usage.
3074 * On return the (possibly new) entry->object.vm_object will have an
3075 * additional ref on it for the caller to dispose of (usually by cloning
3076 * the vm_map_entry). The additional ref had to be done in this routine
3077 * to avoid racing a collapse. The object's ONEMAPPING flag will also be
3078 * cleared.
3080 * The vm_map must be locked and its token held.
3082 static void
3083 vm_map_split(vm_map_entry_t entry)
3085 /* OPTIMIZED */
3086 vm_object_t oobject, nobject, bobject;
3087 vm_offset_t s, e;
3088 vm_page_t m;
3089 vm_pindex_t offidxstart, offidxend, idx;
3090 vm_size_t size;
3091 vm_ooffset_t offset;
3092 int useshadowlist;
3095 * Optimize away object locks for vnode objects. Important exit/exec
3096 * critical path.
3098 * OBJ_ONEMAPPING doesn't apply to vnode objects but clear the flag
3099 * anyway.
3101 oobject = entry->object.vm_object;
3102 if (oobject->type != OBJT_DEFAULT && oobject->type != OBJT_SWAP) {
3103 vm_object_reference_quick(oobject);
3104 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3105 return;
3109 * Setup. Chain lock the original object throughout the entire
3110 * routine to prevent new page faults from occuring.
3112 * XXX can madvise WILLNEED interfere with us too?
3114 vm_object_hold(oobject);
3115 vm_object_chain_acquire(oobject, 0);
3118 * Original object cannot be split? Might have also changed state.
3120 if (oobject->handle == NULL || (oobject->type != OBJT_DEFAULT &&
3121 oobject->type != OBJT_SWAP)) {
3122 vm_object_chain_release(oobject);
3123 vm_object_reference_locked(oobject);
3124 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3125 vm_object_drop(oobject);
3126 return;
3130 * Collapse original object with its backing store as an
3131 * optimization to reduce chain lengths when possible.
3133 * If ref_count <= 1 there aren't other non-overlapping vm_map_entry's
3134 * for oobject, so there's no point collapsing it.
3136 * Then re-check whether the object can be split.
3138 vm_object_collapse(oobject, NULL);
3140 if (oobject->ref_count <= 1 ||
3141 (oobject->type != OBJT_DEFAULT && oobject->type != OBJT_SWAP) ||
3142 (oobject->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) != OBJ_ONEMAPPING) {
3143 vm_object_chain_release(oobject);
3144 vm_object_reference_locked(oobject);
3145 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3146 vm_object_drop(oobject);
3147 return;
3151 * Acquire the chain lock on the backing object.
3153 * Give bobject an additional ref count for when it will be shadowed
3154 * by nobject.
3156 useshadowlist = 0;
3157 if ((bobject = oobject->backing_object) != NULL) {
3158 if (bobject->type != OBJT_VNODE) {
3159 useshadowlist = 1;
3160 vm_object_hold(bobject);
3161 vm_object_chain_wait(bobject, 0);
3162 /* ref for shadowing below */
3163 vm_object_reference_locked(bobject);
3164 vm_object_chain_acquire(bobject, 0);
3165 KKASSERT(bobject->backing_object == bobject);
3166 KKASSERT((bobject->flags & OBJ_DEAD) == 0);
3167 } else {
3169 * vnodes are not placed on the shadow list but
3170 * they still get another ref for the backing_object
3171 * reference.
3173 vm_object_reference_quick(bobject);
3178 * Calculate the object page range and allocate the new object.
3180 offset = entry->offset;
3181 s = entry->start;
3182 e = entry->end;
3184 offidxstart = OFF_TO_IDX(offset);
3185 offidxend = offidxstart + OFF_TO_IDX(e - s);
3186 size = offidxend - offidxstart;
3188 switch(oobject->type) {
3189 case OBJT_DEFAULT:
3190 nobject = default_pager_alloc(NULL, IDX_TO_OFF(size),
3191 VM_PROT_ALL, 0);
3192 break;
3193 case OBJT_SWAP:
3194 nobject = swap_pager_alloc(NULL, IDX_TO_OFF(size),
3195 VM_PROT_ALL, 0);
3196 break;
3197 default:
3198 /* not reached */
3199 nobject = NULL;
3200 KKASSERT(0);
3203 if (nobject == NULL) {
3204 if (bobject) {
3205 if (useshadowlist) {
3206 vm_object_chain_release(bobject);
3207 vm_object_deallocate(bobject);
3208 vm_object_drop(bobject);
3209 } else {
3210 vm_object_deallocate(bobject);
3213 vm_object_chain_release(oobject);
3214 vm_object_reference_locked(oobject);
3215 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3216 vm_object_drop(oobject);
3217 return;
3221 * The new object will replace entry->object.vm_object so it needs
3222 * a second reference (the caller expects an additional ref).
3224 vm_object_hold(nobject);
3225 vm_object_reference_locked(nobject);
3226 vm_object_chain_acquire(nobject, 0);
3229 * nobject shadows bobject (oobject already shadows bobject).
3231 * Adding an object to bobject's shadow list requires refing bobject
3232 * which we did above in the useshadowlist case.
3234 if (bobject) {
3235 nobject->backing_object_offset =
3236 oobject->backing_object_offset + IDX_TO_OFF(offidxstart);
3237 nobject->backing_object = bobject;
3238 if (useshadowlist) {
3239 bobject->shadow_count++;
3240 bobject->generation++;
3241 LIST_INSERT_HEAD(&bobject->shadow_head,
3242 nobject, shadow_list);
3243 vm_object_clear_flag(bobject, OBJ_ONEMAPPING); /*XXX*/
3244 vm_object_chain_release(bobject);
3245 vm_object_drop(bobject);
3246 vm_object_set_flag(nobject, OBJ_ONSHADOW);
3251 * Move the VM pages from oobject to nobject
3253 for (idx = 0; idx < size; idx++) {
3254 vm_page_t m;
3256 m = vm_page_lookup_busy_wait(oobject, offidxstart + idx,
3257 TRUE, "vmpg");
3258 if (m == NULL)
3259 continue;
3262 * We must wait for pending I/O to complete before we can
3263 * rename the page.
3265 * We do not have to VM_PROT_NONE the page as mappings should
3266 * not be changed by this operation.
3268 * NOTE: The act of renaming a page updates chaingen for both
3269 * objects.
3271 vm_page_rename(m, nobject, idx);
3272 /* page automatically made dirty by rename and cache handled */
3273 /* page remains busy */
3276 if (oobject->type == OBJT_SWAP) {
3277 vm_object_pip_add(oobject, 1);
3279 * copy oobject pages into nobject and destroy unneeded
3280 * pages in shadow object.
3282 swap_pager_copy(oobject, nobject, offidxstart, 0);
3283 vm_object_pip_wakeup(oobject);
3287 * Wakeup the pages we played with. No spl protection is needed
3288 * for a simple wakeup.
3290 for (idx = 0; idx < size; idx++) {
3291 m = vm_page_lookup(nobject, idx);
3292 if (m) {
3293 KKASSERT(m->flags & PG_BUSY);
3294 vm_page_wakeup(m);
3297 entry->object.vm_object = nobject;
3298 entry->offset = 0LL;
3301 * Cleanup
3303 * NOTE: There is no need to remove OBJ_ONEMAPPING from oobject, the
3304 * related pages were moved and are no longer applicable to the
3305 * original object.
3307 * NOTE: Deallocate oobject (due to its entry->object.vm_object being
3308 * replaced by nobject).
3310 vm_object_chain_release(nobject);
3311 vm_object_drop(nobject);
3312 if (bobject && useshadowlist) {
3313 vm_object_chain_release(bobject);
3314 vm_object_drop(bobject);
3316 vm_object_chain_release(oobject);
3317 /*vm_object_clear_flag(oobject, OBJ_ONEMAPPING);*/
3318 vm_object_deallocate_locked(oobject);
3319 vm_object_drop(oobject);
3323 * Copies the contents of the source entry to the destination
3324 * entry. The entries *must* be aligned properly.
3326 * The vm_maps must be exclusively locked.
3327 * The vm_map's token must be held.
3329 * Because the maps are locked no faults can be in progress during the
3330 * operation.
3332 static void
3333 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
3334 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
3336 vm_object_t src_object;
3338 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP ||
3339 dst_entry->maptype == VM_MAPTYPE_UKSMAP)
3340 return;
3341 if (src_entry->maptype == VM_MAPTYPE_SUBMAP ||
3342 src_entry->maptype == VM_MAPTYPE_UKSMAP)
3343 return;
3345 if (src_entry->wired_count == 0) {
3347 * If the source entry is marked needs_copy, it is already
3348 * write-protected.
3350 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
3351 pmap_protect(src_map->pmap,
3352 src_entry->start,
3353 src_entry->end,
3354 src_entry->protection & ~VM_PROT_WRITE);
3358 * Make a copy of the object.
3360 * The object must be locked prior to checking the object type
3361 * and for the call to vm_object_collapse() and vm_map_split().
3362 * We cannot use *_hold() here because the split code will
3363 * probably try to destroy the object. The lock is a pool
3364 * token and doesn't care.
3366 * We must bump src_map->timestamp when setting
3367 * MAP_ENTRY_NEEDS_COPY to force any concurrent fault
3368 * to retry, otherwise the concurrent fault might improperly
3369 * install a RW pte when its supposed to be a RO(COW) pte.
3370 * This race can occur because a vnode-backed fault may have
3371 * to temporarily release the map lock.
3373 if (src_entry->object.vm_object != NULL) {
3374 vm_map_split(src_entry);
3375 src_object = src_entry->object.vm_object;
3376 dst_entry->object.vm_object = src_object;
3377 src_entry->eflags |= (MAP_ENTRY_COW |
3378 MAP_ENTRY_NEEDS_COPY);
3379 dst_entry->eflags |= (MAP_ENTRY_COW |
3380 MAP_ENTRY_NEEDS_COPY);
3381 dst_entry->offset = src_entry->offset;
3382 ++src_map->timestamp;
3383 } else {
3384 dst_entry->object.vm_object = NULL;
3385 dst_entry->offset = 0;
3388 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
3389 dst_entry->end - dst_entry->start, src_entry->start);
3390 } else {
3392 * Of course, wired down pages can't be set copy-on-write.
3393 * Cause wired pages to be copied into the new map by
3394 * simulating faults (the new pages are pageable)
3396 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
3401 * vmspace_fork:
3402 * Create a new process vmspace structure and vm_map
3403 * based on those of an existing process. The new map
3404 * is based on the old map, according to the inheritance
3405 * values on the regions in that map.
3407 * The source map must not be locked.
3408 * No requirements.
3410 static void vmspace_fork_normal_entry(vm_map_t old_map, vm_map_t new_map,
3411 vm_map_entry_t old_entry, int *countp);
3412 static void vmspace_fork_uksmap_entry(vm_map_t old_map, vm_map_t new_map,
3413 vm_map_entry_t old_entry, int *countp);
3415 struct vmspace *
3416 vmspace_fork(struct vmspace *vm1)
3418 struct vmspace *vm2;
3419 vm_map_t old_map = &vm1->vm_map;
3420 vm_map_t new_map;
3421 vm_map_entry_t old_entry;
3422 int count;
3424 lwkt_gettoken(&vm1->vm_map.token);
3425 vm_map_lock(old_map);
3427 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
3428 lwkt_gettoken(&vm2->vm_map.token);
3429 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
3430 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
3431 new_map = &vm2->vm_map; /* XXX */
3432 new_map->timestamp = 1;
3434 vm_map_lock(new_map);
3436 count = 0;
3437 old_entry = old_map->header.next;
3438 while (old_entry != &old_map->header) {
3439 ++count;
3440 old_entry = old_entry->next;
3443 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
3445 old_entry = old_map->header.next;
3446 while (old_entry != &old_map->header) {
3447 switch(old_entry->maptype) {
3448 case VM_MAPTYPE_SUBMAP:
3449 panic("vm_map_fork: encountered a submap");
3450 break;
3451 case VM_MAPTYPE_UKSMAP:
3452 vmspace_fork_uksmap_entry(old_map, new_map,
3453 old_entry, &count);
3454 break;
3455 case VM_MAPTYPE_NORMAL:
3456 case VM_MAPTYPE_VPAGETABLE:
3457 vmspace_fork_normal_entry(old_map, new_map,
3458 old_entry, &count);
3459 break;
3461 old_entry = old_entry->next;
3464 new_map->size = old_map->size;
3465 vm_map_unlock(old_map);
3466 vm_map_unlock(new_map);
3467 vm_map_entry_release(count);
3469 lwkt_reltoken(&vm2->vm_map.token);
3470 lwkt_reltoken(&vm1->vm_map.token);
3472 return (vm2);
3475 static
3476 void
3477 vmspace_fork_normal_entry(vm_map_t old_map, vm_map_t new_map,
3478 vm_map_entry_t old_entry, int *countp)
3480 vm_map_entry_t new_entry;
3481 vm_object_t object;
3483 switch (old_entry->inheritance) {
3484 case VM_INHERIT_NONE:
3485 break;
3486 case VM_INHERIT_SHARE:
3488 * Clone the entry, creating the shared object if
3489 * necessary.
3491 if (old_entry->object.vm_object == NULL)
3492 vm_map_entry_allocate_object(old_entry);
3494 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3496 * Shadow a map_entry which needs a copy,
3497 * replacing its object with a new object
3498 * that points to the old one. Ask the
3499 * shadow code to automatically add an
3500 * additional ref. We can't do it afterwords
3501 * because we might race a collapse. The call
3502 * to vm_map_entry_shadow() will also clear
3503 * OBJ_ONEMAPPING.
3505 vm_map_entry_shadow(old_entry, 1);
3506 } else if (old_entry->object.vm_object) {
3508 * We will make a shared copy of the object,
3509 * and must clear OBJ_ONEMAPPING.
3511 * Optimize vnode objects. OBJ_ONEMAPPING
3512 * is non-applicable but clear it anyway,
3513 * and its terminal so we don'th ave to deal
3514 * with chains. Reduces SMP conflicts.
3516 * XXX assert that object.vm_object != NULL
3517 * since we allocate it above.
3519 object = old_entry->object.vm_object;
3520 if (object->type == OBJT_VNODE) {
3521 vm_object_reference_quick(object);
3522 vm_object_clear_flag(object,
3523 OBJ_ONEMAPPING);
3524 } else {
3525 vm_object_hold(object);
3526 vm_object_chain_wait(object, 0);
3527 vm_object_reference_locked(object);
3528 vm_object_clear_flag(object,
3529 OBJ_ONEMAPPING);
3530 vm_object_drop(object);
3535 * Clone the entry. We've already bumped the ref on
3536 * any vm_object.
3538 new_entry = vm_map_entry_create(new_map, countp);
3539 *new_entry = *old_entry;
3540 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3541 new_entry->wired_count = 0;
3544 * Insert the entry into the new map -- we know we're
3545 * inserting at the end of the new map.
3548 vm_map_entry_link(new_map, new_map->header.prev,
3549 new_entry);
3552 * Update the physical map
3554 pmap_copy(new_map->pmap, old_map->pmap,
3555 new_entry->start,
3556 (old_entry->end - old_entry->start),
3557 old_entry->start);
3558 break;
3559 case VM_INHERIT_COPY:
3561 * Clone the entry and link into the map.
3563 new_entry = vm_map_entry_create(new_map, countp);
3564 *new_entry = *old_entry;
3565 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3566 new_entry->wired_count = 0;
3567 new_entry->object.vm_object = NULL;
3568 vm_map_entry_link(new_map, new_map->header.prev,
3569 new_entry);
3570 vm_map_copy_entry(old_map, new_map, old_entry,
3571 new_entry);
3572 break;
3577 * When forking user-kernel shared maps, the map might change in the
3578 * child so do not try to copy the underlying pmap entries.
3580 static
3581 void
3582 vmspace_fork_uksmap_entry(vm_map_t old_map, vm_map_t new_map,
3583 vm_map_entry_t old_entry, int *countp)
3585 vm_map_entry_t new_entry;
3587 new_entry = vm_map_entry_create(new_map, countp);
3588 *new_entry = *old_entry;
3589 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3590 new_entry->wired_count = 0;
3591 vm_map_entry_link(new_map, new_map->header.prev,
3592 new_entry);
3596 * Create an auto-grow stack entry
3598 * No requirements.
3601 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3602 int flags, vm_prot_t prot, vm_prot_t max, int cow)
3604 vm_map_entry_t prev_entry;
3605 vm_map_entry_t new_stack_entry;
3606 vm_size_t init_ssize;
3607 int rv;
3608 int count;
3609 vm_offset_t tmpaddr;
3611 cow |= MAP_IS_STACK;
3613 if (max_ssize < sgrowsiz)
3614 init_ssize = max_ssize;
3615 else
3616 init_ssize = sgrowsiz;
3618 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3619 vm_map_lock(map);
3622 * Find space for the mapping
3624 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
3625 if (vm_map_findspace(map, addrbos, max_ssize, 1,
3626 flags, &tmpaddr)) {
3627 vm_map_unlock(map);
3628 vm_map_entry_release(count);
3629 return (KERN_NO_SPACE);
3631 addrbos = tmpaddr;
3634 /* If addr is already mapped, no go */
3635 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3636 vm_map_unlock(map);
3637 vm_map_entry_release(count);
3638 return (KERN_NO_SPACE);
3641 #if 0
3642 /* XXX already handled by kern_mmap() */
3643 /* If we would blow our VMEM resource limit, no go */
3644 if (map->size + init_ssize >
3645 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3646 vm_map_unlock(map);
3647 vm_map_entry_release(count);
3648 return (KERN_NO_SPACE);
3650 #endif
3653 * If we can't accomodate max_ssize in the current mapping,
3654 * no go. However, we need to be aware that subsequent user
3655 * mappings might map into the space we have reserved for
3656 * stack, and currently this space is not protected.
3658 * Hopefully we will at least detect this condition
3659 * when we try to grow the stack.
3661 if ((prev_entry->next != &map->header) &&
3662 (prev_entry->next->start < addrbos + max_ssize)) {
3663 vm_map_unlock(map);
3664 vm_map_entry_release(count);
3665 return (KERN_NO_SPACE);
3669 * We initially map a stack of only init_ssize. We will
3670 * grow as needed later. Since this is to be a grow
3671 * down stack, we map at the top of the range.
3673 * Note: we would normally expect prot and max to be
3674 * VM_PROT_ALL, and cow to be 0. Possibly we should
3675 * eliminate these as input parameters, and just
3676 * pass these values here in the insert call.
3678 rv = vm_map_insert(map, &count, NULL, NULL,
3679 0, addrbos + max_ssize - init_ssize,
3680 addrbos + max_ssize,
3681 VM_MAPTYPE_NORMAL,
3682 VM_SUBSYS_STACK, prot, max, cow);
3684 /* Now set the avail_ssize amount */
3685 if (rv == KERN_SUCCESS) {
3686 if (prev_entry != &map->header)
3687 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
3688 new_stack_entry = prev_entry->next;
3689 if (new_stack_entry->end != addrbos + max_ssize ||
3690 new_stack_entry->start != addrbos + max_ssize - init_ssize)
3691 panic ("Bad entry start/end for new stack entry");
3692 else
3693 new_stack_entry->aux.avail_ssize = max_ssize - init_ssize;
3696 vm_map_unlock(map);
3697 vm_map_entry_release(count);
3698 return (rv);
3702 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3703 * desired address is already mapped, or if we successfully grow
3704 * the stack. Also returns KERN_SUCCESS if addr is outside the
3705 * stack range (this is strange, but preserves compatibility with
3706 * the grow function in vm_machdep.c).
3708 * No requirements.
3711 vm_map_growstack (struct proc *p, vm_offset_t addr)
3713 vm_map_entry_t prev_entry;
3714 vm_map_entry_t stack_entry;
3715 vm_map_entry_t new_stack_entry;
3716 struct vmspace *vm = p->p_vmspace;
3717 vm_map_t map = &vm->vm_map;
3718 vm_offset_t end;
3719 int grow_amount;
3720 int rv = KERN_SUCCESS;
3721 int is_procstack;
3722 int use_read_lock = 1;
3723 int count;
3725 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3726 Retry:
3727 if (use_read_lock)
3728 vm_map_lock_read(map);
3729 else
3730 vm_map_lock(map);
3732 /* If addr is already in the entry range, no need to grow.*/
3733 if (vm_map_lookup_entry(map, addr, &prev_entry))
3734 goto done;
3736 if ((stack_entry = prev_entry->next) == &map->header)
3737 goto done;
3738 if (prev_entry == &map->header)
3739 end = stack_entry->start - stack_entry->aux.avail_ssize;
3740 else
3741 end = prev_entry->end;
3744 * This next test mimics the old grow function in vm_machdep.c.
3745 * It really doesn't quite make sense, but we do it anyway
3746 * for compatibility.
3748 * If not growable stack, return success. This signals the
3749 * caller to proceed as he would normally with normal vm.
3751 if (stack_entry->aux.avail_ssize < 1 ||
3752 addr >= stack_entry->start ||
3753 addr < stack_entry->start - stack_entry->aux.avail_ssize) {
3754 goto done;
3757 /* Find the minimum grow amount */
3758 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
3759 if (grow_amount > stack_entry->aux.avail_ssize) {
3760 rv = KERN_NO_SPACE;
3761 goto done;
3765 * If there is no longer enough space between the entries
3766 * nogo, and adjust the available space. Note: this
3767 * should only happen if the user has mapped into the
3768 * stack area after the stack was created, and is
3769 * probably an error.
3771 * This also effectively destroys any guard page the user
3772 * might have intended by limiting the stack size.
3774 if (grow_amount > stack_entry->start - end) {
3775 if (use_read_lock && vm_map_lock_upgrade(map)) {
3776 /* lost lock */
3777 use_read_lock = 0;
3778 goto Retry;
3780 use_read_lock = 0;
3781 stack_entry->aux.avail_ssize = stack_entry->start - end;
3782 rv = KERN_NO_SPACE;
3783 goto done;
3786 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
3788 /* If this is the main process stack, see if we're over the
3789 * stack limit.
3791 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3792 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3793 rv = KERN_NO_SPACE;
3794 goto done;
3797 /* Round up the grow amount modulo SGROWSIZ */
3798 grow_amount = roundup (grow_amount, sgrowsiz);
3799 if (grow_amount > stack_entry->aux.avail_ssize) {
3800 grow_amount = stack_entry->aux.avail_ssize;
3802 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3803 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3804 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
3805 ctob(vm->vm_ssize);
3808 /* If we would blow our VMEM resource limit, no go */
3809 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3810 rv = KERN_NO_SPACE;
3811 goto done;
3814 if (use_read_lock && vm_map_lock_upgrade(map)) {
3815 /* lost lock */
3816 use_read_lock = 0;
3817 goto Retry;
3819 use_read_lock = 0;
3821 /* Get the preliminary new entry start value */
3822 addr = stack_entry->start - grow_amount;
3824 /* If this puts us into the previous entry, cut back our growth
3825 * to the available space. Also, see the note above.
3827 if (addr < end) {
3828 stack_entry->aux.avail_ssize = stack_entry->start - end;
3829 addr = end;
3832 rv = vm_map_insert(map, &count, NULL, NULL,
3833 0, addr, stack_entry->start,
3834 VM_MAPTYPE_NORMAL,
3835 VM_SUBSYS_STACK, VM_PROT_ALL, VM_PROT_ALL, 0);
3837 /* Adjust the available stack space by the amount we grew. */
3838 if (rv == KERN_SUCCESS) {
3839 if (prev_entry != &map->header)
3840 vm_map_clip_end(map, prev_entry, addr, &count);
3841 new_stack_entry = prev_entry->next;
3842 if (new_stack_entry->end != stack_entry->start ||
3843 new_stack_entry->start != addr)
3844 panic ("Bad stack grow start/end in new stack entry");
3845 else {
3846 new_stack_entry->aux.avail_ssize =
3847 stack_entry->aux.avail_ssize -
3848 (new_stack_entry->end - new_stack_entry->start);
3849 if (is_procstack)
3850 vm->vm_ssize += btoc(new_stack_entry->end -
3851 new_stack_entry->start);
3854 if (map->flags & MAP_WIREFUTURE)
3855 vm_map_unwire(map, new_stack_entry->start,
3856 new_stack_entry->end, FALSE);
3859 done:
3860 if (use_read_lock)
3861 vm_map_unlock_read(map);
3862 else
3863 vm_map_unlock(map);
3864 vm_map_entry_release(count);
3865 return (rv);
3869 * Unshare the specified VM space for exec. If other processes are
3870 * mapped to it, then create a new one. The new vmspace is null.
3872 * No requirements.
3874 void
3875 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
3877 struct vmspace *oldvmspace = p->p_vmspace;
3878 struct vmspace *newvmspace;
3879 vm_map_t map = &p->p_vmspace->vm_map;
3882 * If we are execing a resident vmspace we fork it, otherwise
3883 * we create a new vmspace. Note that exitingcnt is not
3884 * copied to the new vmspace.
3886 lwkt_gettoken(&oldvmspace->vm_map.token);
3887 if (vmcopy) {
3888 newvmspace = vmspace_fork(vmcopy);
3889 lwkt_gettoken(&newvmspace->vm_map.token);
3890 } else {
3891 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
3892 lwkt_gettoken(&newvmspace->vm_map.token);
3893 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
3894 (caddr_t)&oldvmspace->vm_endcopy -
3895 (caddr_t)&oldvmspace->vm_startcopy);
3899 * Finish initializing the vmspace before assigning it
3900 * to the process. The vmspace will become the current vmspace
3901 * if p == curproc.
3903 pmap_pinit2(vmspace_pmap(newvmspace));
3904 pmap_replacevm(p, newvmspace, 0);
3905 lwkt_reltoken(&newvmspace->vm_map.token);
3906 lwkt_reltoken(&oldvmspace->vm_map.token);
3907 vmspace_rel(oldvmspace);
3911 * Unshare the specified VM space for forcing COW. This
3912 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3914 void
3915 vmspace_unshare(struct proc *p)
3917 struct vmspace *oldvmspace = p->p_vmspace;
3918 struct vmspace *newvmspace;
3920 lwkt_gettoken(&oldvmspace->vm_map.token);
3921 if (vmspace_getrefs(oldvmspace) == 1) {
3922 lwkt_reltoken(&oldvmspace->vm_map.token);
3923 return;
3925 newvmspace = vmspace_fork(oldvmspace);
3926 lwkt_gettoken(&newvmspace->vm_map.token);
3927 pmap_pinit2(vmspace_pmap(newvmspace));
3928 pmap_replacevm(p, newvmspace, 0);
3929 lwkt_reltoken(&newvmspace->vm_map.token);
3930 lwkt_reltoken(&oldvmspace->vm_map.token);
3931 vmspace_rel(oldvmspace);
3935 * vm_map_hint: return the beginning of the best area suitable for
3936 * creating a new mapping with "prot" protection.
3938 * No requirements.
3940 vm_offset_t
3941 vm_map_hint(struct proc *p, vm_offset_t addr, vm_prot_t prot)
3943 struct vmspace *vms = p->p_vmspace;
3945 if (!randomize_mmap || addr != 0) {
3947 * Set a reasonable start point for the hint if it was
3948 * not specified or if it falls within the heap space.
3949 * Hinted mmap()s do not allocate out of the heap space.
3951 if (addr == 0 ||
3952 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
3953 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz))) {
3954 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
3957 return addr;
3959 addr = (vm_offset_t)vms->vm_daddr + MAXDSIZ;
3960 addr += karc4random() & (MIN((256 * 1024 * 1024), MAXDSIZ) - 1);
3962 return (round_page(addr));
3966 * Finds the VM object, offset, and protection for a given virtual address
3967 * in the specified map, assuming a page fault of the type specified.
3969 * Leaves the map in question locked for read; return values are guaranteed
3970 * until a vm_map_lookup_done call is performed. Note that the map argument
3971 * is in/out; the returned map must be used in the call to vm_map_lookup_done.
3973 * A handle (out_entry) is returned for use in vm_map_lookup_done, to make
3974 * that fast.
3976 * If a lookup is requested with "write protection" specified, the map may
3977 * be changed to perform virtual copying operations, although the data
3978 * referenced will remain the same.
3980 * No requirements.
3983 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3984 vm_offset_t vaddr,
3985 vm_prot_t fault_typea,
3986 vm_map_entry_t *out_entry, /* OUT */
3987 vm_object_t *object, /* OUT */
3988 vm_pindex_t *pindex, /* OUT */
3989 vm_prot_t *out_prot, /* OUT */
3990 boolean_t *wired) /* OUT */
3992 vm_map_entry_t entry;
3993 vm_map_t map = *var_map;
3994 vm_prot_t prot;
3995 vm_prot_t fault_type = fault_typea;
3996 int use_read_lock = 1;
3997 int rv = KERN_SUCCESS;
3999 RetryLookup:
4000 if (use_read_lock)
4001 vm_map_lock_read(map);
4002 else
4003 vm_map_lock(map);
4006 * If the map has an interesting hint, try it before calling full
4007 * blown lookup routine.
4009 entry = map->hint;
4010 cpu_ccfence();
4011 *out_entry = entry;
4012 *object = NULL;
4014 if ((entry == &map->header) ||
4015 (vaddr < entry->start) || (vaddr >= entry->end)) {
4016 vm_map_entry_t tmp_entry;
4019 * Entry was either not a valid hint, or the vaddr was not
4020 * contained in the entry, so do a full lookup.
4022 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
4023 rv = KERN_INVALID_ADDRESS;
4024 goto done;
4027 entry = tmp_entry;
4028 *out_entry = entry;
4032 * Handle submaps.
4034 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
4035 vm_map_t old_map = map;
4037 *var_map = map = entry->object.sub_map;
4038 if (use_read_lock)
4039 vm_map_unlock_read(old_map);
4040 else
4041 vm_map_unlock(old_map);
4042 use_read_lock = 1;
4043 goto RetryLookup;
4047 * Check whether this task is allowed to have this page.
4048 * Note the special case for MAP_ENTRY_COW
4049 * pages with an override. This is to implement a forced
4050 * COW for debuggers.
4053 if (fault_type & VM_PROT_OVERRIDE_WRITE)
4054 prot = entry->max_protection;
4055 else
4056 prot = entry->protection;
4058 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
4059 if ((fault_type & prot) != fault_type) {
4060 rv = KERN_PROTECTION_FAILURE;
4061 goto done;
4064 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
4065 (entry->eflags & MAP_ENTRY_COW) &&
4066 (fault_type & VM_PROT_WRITE) &&
4067 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
4068 rv = KERN_PROTECTION_FAILURE;
4069 goto done;
4073 * If this page is not pageable, we have to get it for all possible
4074 * accesses.
4076 *wired = (entry->wired_count != 0);
4077 if (*wired)
4078 prot = fault_type = entry->protection;
4081 * Virtual page tables may need to update the accessed (A) bit
4082 * in a page table entry. Upgrade the fault to a write fault for
4083 * that case if the map will support it. If the map does not support
4084 * it the page table entry simply will not be updated.
4086 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
4087 if (prot & VM_PROT_WRITE)
4088 fault_type |= VM_PROT_WRITE;
4091 if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace &&
4092 pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) {
4093 if ((prot & VM_PROT_WRITE) == 0)
4094 fault_type |= VM_PROT_WRITE;
4098 * Only NORMAL and VPAGETABLE maps are object-based. UKSMAPs are not.
4100 if (entry->maptype != VM_MAPTYPE_NORMAL &&
4101 entry->maptype != VM_MAPTYPE_VPAGETABLE) {
4102 *object = NULL;
4103 goto skip;
4107 * If the entry was copy-on-write, we either ...
4109 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4111 * If we want to write the page, we may as well handle that
4112 * now since we've got the map locked.
4114 * If we don't need to write the page, we just demote the
4115 * permissions allowed.
4118 if (fault_type & VM_PROT_WRITE) {
4120 * Not allowed if TDF_NOFAULT is set as the shadowing
4121 * operation can deadlock against the faulting
4122 * function due to the copy-on-write.
4124 if (curthread->td_flags & TDF_NOFAULT) {
4125 rv = KERN_FAILURE_NOFAULT;
4126 goto done;
4130 * Make a new object, and place it in the object
4131 * chain. Note that no new references have appeared
4132 * -- one just moved from the map to the new
4133 * object.
4136 if (use_read_lock && vm_map_lock_upgrade(map)) {
4137 /* lost lock */
4138 use_read_lock = 0;
4139 goto RetryLookup;
4141 use_read_lock = 0;
4143 vm_map_entry_shadow(entry, 0);
4144 } else {
4146 * We're attempting to read a copy-on-write page --
4147 * don't allow writes.
4150 prot &= ~VM_PROT_WRITE;
4155 * Create an object if necessary.
4157 if (entry->object.vm_object == NULL && !map->system_map) {
4158 if (use_read_lock && vm_map_lock_upgrade(map)) {
4159 /* lost lock */
4160 use_read_lock = 0;
4161 goto RetryLookup;
4163 use_read_lock = 0;
4164 vm_map_entry_allocate_object(entry);
4168 * Return the object/offset from this entry. If the entry was
4169 * copy-on-write or empty, it has been fixed up.
4171 *object = entry->object.vm_object;
4173 skip:
4174 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4177 * Return whether this is the only map sharing this data. On
4178 * success we return with a read lock held on the map. On failure
4179 * we return with the map unlocked.
4181 *out_prot = prot;
4182 done:
4183 if (rv == KERN_SUCCESS) {
4184 if (use_read_lock == 0)
4185 vm_map_lock_downgrade(map);
4186 } else if (use_read_lock) {
4187 vm_map_unlock_read(map);
4188 } else {
4189 vm_map_unlock(map);
4191 return (rv);
4195 * Releases locks acquired by a vm_map_lookup()
4196 * (according to the handle returned by that lookup).
4198 * No other requirements.
4200 void
4201 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
4204 * Unlock the main-level map
4206 vm_map_unlock_read(map);
4207 if (count)
4208 vm_map_entry_release(count);
4211 #include "opt_ddb.h"
4212 #ifdef DDB
4213 #include <sys/kernel.h>
4215 #include <ddb/ddb.h>
4218 * Debugging only
4220 DB_SHOW_COMMAND(map, vm_map_print)
4222 static int nlines;
4223 /* XXX convert args. */
4224 vm_map_t map = (vm_map_t)addr;
4225 boolean_t full = have_addr;
4227 vm_map_entry_t entry;
4229 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4230 (void *)map,
4231 (void *)map->pmap, map->nentries, map->timestamp);
4232 nlines++;
4234 if (!full && db_indent)
4235 return;
4237 db_indent += 2;
4238 for (entry = map->header.next; entry != &map->header;
4239 entry = entry->next) {
4240 db_iprintf("map entry %p: start=%p, end=%p\n",
4241 (void *)entry, (void *)entry->start, (void *)entry->end);
4242 nlines++;
4244 static char *inheritance_name[4] =
4245 {"share", "copy", "none", "donate_copy"};
4247 db_iprintf(" prot=%x/%x/%s",
4248 entry->protection,
4249 entry->max_protection,
4250 inheritance_name[(int)(unsigned char)entry->inheritance]);
4251 if (entry->wired_count != 0)
4252 db_printf(", wired");
4254 switch(entry->maptype) {
4255 case VM_MAPTYPE_SUBMAP:
4256 /* XXX no %qd in kernel. Truncate entry->offset. */
4257 db_printf(", share=%p, offset=0x%lx\n",
4258 (void *)entry->object.sub_map,
4259 (long)entry->offset);
4260 nlines++;
4261 if ((entry->prev == &map->header) ||
4262 (entry->prev->object.sub_map !=
4263 entry->object.sub_map)) {
4264 db_indent += 2;
4265 vm_map_print((db_expr_t)(intptr_t)
4266 entry->object.sub_map,
4267 full, 0, NULL);
4268 db_indent -= 2;
4270 break;
4271 case VM_MAPTYPE_NORMAL:
4272 case VM_MAPTYPE_VPAGETABLE:
4273 /* XXX no %qd in kernel. Truncate entry->offset. */
4274 db_printf(", object=%p, offset=0x%lx",
4275 (void *)entry->object.vm_object,
4276 (long)entry->offset);
4277 if (entry->eflags & MAP_ENTRY_COW)
4278 db_printf(", copy (%s)",
4279 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4280 db_printf("\n");
4281 nlines++;
4283 if ((entry->prev == &map->header) ||
4284 (entry->prev->object.vm_object !=
4285 entry->object.vm_object)) {
4286 db_indent += 2;
4287 vm_object_print((db_expr_t)(intptr_t)
4288 entry->object.vm_object,
4289 full, 0, NULL);
4290 nlines += 4;
4291 db_indent -= 2;
4293 break;
4294 case VM_MAPTYPE_UKSMAP:
4295 db_printf(", uksmap=%p, offset=0x%lx",
4296 (void *)entry->object.uksmap,
4297 (long)entry->offset);
4298 if (entry->eflags & MAP_ENTRY_COW)
4299 db_printf(", copy (%s)",
4300 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4301 db_printf("\n");
4302 nlines++;
4303 break;
4304 default:
4305 break;
4308 db_indent -= 2;
4309 if (db_indent == 0)
4310 nlines = 0;
4314 * Debugging only
4316 DB_SHOW_COMMAND(procvm, procvm)
4318 struct proc *p;
4320 if (have_addr) {
4321 p = (struct proc *) addr;
4322 } else {
4323 p = curproc;
4326 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4327 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4328 (void *)vmspace_pmap(p->p_vmspace));
4330 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
4333 #endif /* DDB */