1 /* $NetBSD: uvm_map.c,v 1.285 2009/12/14 21:19:47 matt Exp $ */
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1991, 1993, The Regents of the University of California.
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Charles D. Cranor,
23 * Washington University, the University of California, Berkeley and
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * @(#)vm_map.c 8.3 (Berkeley) 1/12/94
42 * from: Id: uvm_map.c,v 1.1.2.27 1998/02/07 01:16:54 chs Exp
45 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 * All rights reserved.
48 * Permission to use, copy, modify and distribute this software and
49 * its documentation is hereby granted, provided that both the copyright
50 * notice and this permission notice appear in all copies of the
51 * software, derivative works or modified versions, and any portions
52 * thereof, and that both notices appear in supporting documentation.
54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58 * Carnegie Mellon requests users of this software to return to
60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
61 * School of Computer Science
62 * Carnegie Mellon University
63 * Pittsburgh PA 15213-3890
65 * any improvements or extensions that they make and grant Carnegie the
66 * rights to redistribute these changes.
70 * uvm_map.c: uvm map operations
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: uvm_map.c,v 1.285 2009/12/14 21:19:47 matt Exp $");
77 #include "opt_uvmhist.h"
81 #include <sys/param.h>
82 #include <sys/systm.h>
85 #include <sys/malloc.h>
87 #include <sys/kernel.h>
88 #include <sys/mount.h>
89 #include <sys/vnode.h>
90 #include <sys/lockdebug.h>
91 #include <sys/atomic.h>
98 #include <uvm/uvm_readahead.h>
100 #if defined(DDB) || defined(DEBUGPRINT)
101 #include <uvm/uvm_ddb.h>
104 #if !defined(UVMMAP_COUNTERS)
106 #define UVMMAP_EVCNT_DEFINE(name) /* nothing */
107 #define UVMMAP_EVCNT_INCR(ev) /* nothing */
108 #define UVMMAP_EVCNT_DECR(ev) /* nothing */
110 #else /* defined(UVMMAP_NOCOUNTERS) */
112 #include <sys/evcnt.h>
113 #define UVMMAP_EVCNT_DEFINE(name) \
114 struct evcnt uvmmap_evcnt_##name = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, \
116 EVCNT_ATTACH_STATIC(uvmmap_evcnt_##name);
117 #define UVMMAP_EVCNT_INCR(ev) uvmmap_evcnt_##ev.ev_count++
118 #define UVMMAP_EVCNT_DECR(ev) uvmmap_evcnt_##ev.ev_count--
120 #endif /* defined(UVMMAP_NOCOUNTERS) */
122 UVMMAP_EVCNT_DEFINE(ubackmerge
)
123 UVMMAP_EVCNT_DEFINE(uforwmerge
)
124 UVMMAP_EVCNT_DEFINE(ubimerge
)
125 UVMMAP_EVCNT_DEFINE(unomerge
)
126 UVMMAP_EVCNT_DEFINE(kbackmerge
)
127 UVMMAP_EVCNT_DEFINE(kforwmerge
)
128 UVMMAP_EVCNT_DEFINE(kbimerge
)
129 UVMMAP_EVCNT_DEFINE(knomerge
)
130 UVMMAP_EVCNT_DEFINE(map_call
)
131 UVMMAP_EVCNT_DEFINE(mlk_call
)
132 UVMMAP_EVCNT_DEFINE(mlk_hint
)
133 UVMMAP_EVCNT_DEFINE(mlk_list
)
134 UVMMAP_EVCNT_DEFINE(mlk_tree
)
135 UVMMAP_EVCNT_DEFINE(mlk_treeloop
)
136 UVMMAP_EVCNT_DEFINE(mlk_listloop
)
138 UVMMAP_EVCNT_DEFINE(uke_alloc
)
139 UVMMAP_EVCNT_DEFINE(uke_free
)
140 UVMMAP_EVCNT_DEFINE(ukh_alloc
)
141 UVMMAP_EVCNT_DEFINE(ukh_free
)
143 const char vmmapbsy
[] = "vmmapbsy";
146 * cache for vmspace structures.
149 static struct pool_cache uvm_vmspace_cache
;
152 * cache for dynamically-allocated map entries.
155 static struct pool_cache uvm_map_entry_cache
;
157 MALLOC_DEFINE(M_VMMAP
, "VM map", "VM map structures");
158 MALLOC_DEFINE(M_VMPMAP
, "VM pmap", "VM pmap");
160 #ifdef PMAP_GROWKERNEL
162 * This global represents the end of the kernel virtual address
163 * space. If we want to exceed this, we must grow the kernel
164 * virtual address space dynamically.
166 * Note, this variable is locked by kernel_map's lock.
168 vaddr_t uvm_maxkaddr
;
176 * VM_MAP_USE_KMAPENT: determine if uvm_kmapent_alloc/free is used
179 extern struct vm_map
*pager_map
; /* XXX */
180 #define VM_MAP_USE_KMAPENT_FLAGS(flags) \
181 (((flags) & VM_MAP_INTRSAFE) != 0)
182 #define VM_MAP_USE_KMAPENT(map) \
183 (VM_MAP_USE_KMAPENT_FLAGS((map)->flags) || (map) == kernel_map)
186 * UVM_ET_ISCOMPATIBLE: check some requirements for map entry merging
189 #define UVM_ET_ISCOMPATIBLE(ent, type, uobj, meflags, \
190 prot, maxprot, inh, adv, wire) \
191 ((ent)->etype == (type) && \
192 (((ent)->flags ^ (meflags)) & (UVM_MAP_NOMERGE | UVM_MAP_QUANTUM)) \
194 (ent)->object.uvm_obj == (uobj) && \
195 (ent)->protection == (prot) && \
196 (ent)->max_protection == (maxprot) && \
197 (ent)->inheritance == (inh) && \
198 (ent)->advice == (adv) && \
199 (ent)->wired_count == (wire))
202 * uvm_map_entry_link: insert entry into a map
204 * => map must be locked
206 #define uvm_map_entry_link(map, after_where, entry) do { \
207 uvm_mapent_check(entry); \
209 (entry)->prev = (after_where); \
210 (entry)->next = (after_where)->next; \
211 (entry)->prev->next = (entry); \
212 (entry)->next->prev = (entry); \
213 uvm_rb_insert((map), (entry)); \
214 } while (/*CONSTCOND*/ 0)
217 * uvm_map_entry_unlink: remove entry from a map
219 * => map must be locked
221 #define uvm_map_entry_unlink(map, entry) do { \
222 KASSERT((entry) != (map)->first_free); \
223 KASSERT((entry) != (map)->hint); \
224 uvm_mapent_check(entry); \
226 (entry)->next->prev = (entry)->prev; \
227 (entry)->prev->next = (entry)->next; \
228 uvm_rb_remove((map), (entry)); \
229 } while (/*CONSTCOND*/ 0)
232 * SAVE_HINT: saves the specified entry as the hint for future lookups.
234 * => map need not be locked.
236 #define SAVE_HINT(map, check, value) do { \
237 if ((map)->hint == (check)) \
238 (map)->hint = (value); \
239 } while (/*CONSTCOND*/ 0)
242 * clear_hints: ensure that hints don't point to the entry.
244 * => map must be write-locked.
247 clear_hints(struct vm_map
*map
, struct vm_map_entry
*ent
)
250 SAVE_HINT(map
, ent
, ent
->prev
);
251 if (map
->first_free
== ent
) {
252 map
->first_free
= ent
->prev
;
257 * VM_MAP_RANGE_CHECK: check and correct range
259 * => map must at least be read locked
262 #define VM_MAP_RANGE_CHECK(map, start, end) do { \
263 if (start < vm_map_min(map)) \
264 start = vm_map_min(map); \
265 if (end > vm_map_max(map)) \
266 end = vm_map_max(map); \
269 } while (/*CONSTCOND*/ 0)
275 static struct vm_map_entry
*
276 uvm_mapent_alloc(struct vm_map
*, int);
277 static struct vm_map_entry
*
278 uvm_mapent_alloc_split(struct vm_map
*,
279 const struct vm_map_entry
*, int,
280 struct uvm_mapent_reservation
*);
281 static void uvm_mapent_copy(struct vm_map_entry
*, struct vm_map_entry
*);
282 static void uvm_mapent_free(struct vm_map_entry
*);
284 static void _uvm_mapent_check(const struct vm_map_entry
*, const char *,
286 #define uvm_mapent_check(map) _uvm_mapent_check(map, __FILE__, __LINE__)
287 #else /* defined(DEBUG) */
288 #define uvm_mapent_check(e) /* nothing */
289 #endif /* defined(DEBUG) */
290 static struct vm_map_entry
*
291 uvm_kmapent_alloc(struct vm_map
*, int);
292 static void uvm_kmapent_free(struct vm_map_entry
*);
293 static vsize_t
uvm_kmapent_overhead(vsize_t
);
295 static void uvm_map_entry_unwire(struct vm_map
*, struct vm_map_entry
*);
296 static void uvm_map_reference_amap(struct vm_map_entry
*, int);
297 static int uvm_map_space_avail(vaddr_t
*, vsize_t
, voff_t
, vsize_t
, int,
298 struct vm_map_entry
*);
299 static void uvm_map_unreference_amap(struct vm_map_entry
*, int);
301 int _uvm_map_sanity(struct vm_map
*);
302 int _uvm_tree_sanity(struct vm_map
*);
303 static vsize_t
uvm_rb_maxgap(const struct vm_map_entry
*);
305 CTASSERT(offsetof(struct vm_map_entry
, rb_node
) == 0);
306 #define ROOT_ENTRY(map) ((struct vm_map_entry *)(map)->rb_tree.rbt_root)
307 #define LEFT_ENTRY(entry) ((struct vm_map_entry *)(entry)->rb_node.rb_left)
308 #define RIGHT_ENTRY(entry) ((struct vm_map_entry *)(entry)->rb_node.rb_right)
309 #define PARENT_ENTRY(map, entry) \
310 (ROOT_ENTRY(map) == (entry) \
312 : (struct vm_map_entry *)RB_FATHER(&(entry)->rb_node))
315 uvm_map_compare_nodes(const struct rb_node
*nparent
,
316 const struct rb_node
*nkey
)
318 const struct vm_map_entry
*eparent
= (const void *) nparent
;
319 const struct vm_map_entry
*ekey
= (const void *) nkey
;
321 KASSERT(eparent
->start
< ekey
->start
|| eparent
->start
>= ekey
->end
);
322 KASSERT(ekey
->start
< eparent
->start
|| ekey
->start
>= eparent
->end
);
324 if (ekey
->start
< eparent
->start
)
326 if (ekey
->start
>= eparent
->end
)
332 uvm_map_compare_key(const struct rb_node
*nparent
, const void *vkey
)
334 const struct vm_map_entry
*eparent
= (const void *) nparent
;
335 const vaddr_t va
= *(const vaddr_t
*) vkey
;
337 if (va
< eparent
->start
)
339 if (va
>= eparent
->end
)
344 static const struct rb_tree_ops uvm_map_tree_ops
= {
345 .rbto_compare_nodes
= uvm_map_compare_nodes
,
346 .rbto_compare_key
= uvm_map_compare_key
,
349 static inline vsize_t
350 uvm_rb_gap(const struct vm_map_entry
*entry
)
352 KASSERT(entry
->next
!= NULL
);
353 return entry
->next
->start
- entry
->end
;
357 uvm_rb_maxgap(const struct vm_map_entry
*entry
)
359 struct vm_map_entry
*child
;
360 vsize_t maxgap
= entry
->gap
;
363 * We need maxgap to be the largest gap of us or any of our
364 * descendents. Since each of our children's maxgap is the
365 * cached value of their largest gap of themselves or their
366 * descendents, we can just use that value and avoid recursing
367 * down the tree to calculate it.
369 if ((child
= LEFT_ENTRY(entry
)) != NULL
&& maxgap
< child
->maxgap
)
370 maxgap
= child
->maxgap
;
372 if ((child
= RIGHT_ENTRY(entry
)) != NULL
&& maxgap
< child
->maxgap
)
373 maxgap
= child
->maxgap
;
379 uvm_rb_fixup(struct vm_map
*map
, struct vm_map_entry
*entry
)
381 struct vm_map_entry
*parent
;
383 KASSERT(entry
->gap
== uvm_rb_gap(entry
));
384 entry
->maxgap
= uvm_rb_maxgap(entry
);
386 while ((parent
= PARENT_ENTRY(map
, entry
)) != NULL
) {
387 struct vm_map_entry
*brother
;
388 vsize_t maxgap
= parent
->gap
;
390 KDASSERT(parent
->gap
== uvm_rb_gap(parent
));
391 if (maxgap
< entry
->maxgap
)
392 maxgap
= entry
->maxgap
;
394 * Since we work our towards the root, we know entry's maxgap
395 * value is ok but its brothers may now be out-of-date due
396 * rebalancing. So refresh it.
398 brother
= (struct vm_map_entry
*)parent
->rb_node
.rb_nodes
[RB_POSITION(&entry
->rb_node
) ^ RB_DIR_OTHER
];
399 if (brother
!= NULL
) {
400 KDASSERT(brother
->gap
== uvm_rb_gap(brother
));
401 brother
->maxgap
= uvm_rb_maxgap(brother
);
402 if (maxgap
< brother
->maxgap
)
403 maxgap
= brother
->maxgap
;
406 parent
->maxgap
= maxgap
;
412 uvm_rb_insert(struct vm_map
*map
, struct vm_map_entry
*entry
)
414 entry
->gap
= entry
->maxgap
= uvm_rb_gap(entry
);
415 if (entry
->prev
!= &map
->header
)
416 entry
->prev
->gap
= uvm_rb_gap(entry
->prev
);
418 if (!rb_tree_insert_node(&map
->rb_tree
, &entry
->rb_node
))
419 panic("uvm_rb_insert: map %p: duplicate entry?", map
);
422 * If the previous entry is not our immediate left child, then it's an
423 * ancestor and will be fixed up on the way to the root. We don't
424 * have to check entry->prev against &map->header since &map->header
425 * will never be in the tree.
428 LEFT_ENTRY(entry
) == entry
->prev
? entry
->prev
: entry
);
432 uvm_rb_remove(struct vm_map
*map
, struct vm_map_entry
*entry
)
434 struct vm_map_entry
*prev_parent
= NULL
, *next_parent
= NULL
;
437 * If we are removing an interior node, then an adjacent node will
438 * be used to replace its position in the tree. Therefore we will
439 * need to fixup the tree starting at the parent of the replacement
440 * node. So record their parents for later use.
442 if (entry
->prev
!= &map
->header
)
443 prev_parent
= PARENT_ENTRY(map
, entry
->prev
);
444 if (entry
->next
!= &map
->header
)
445 next_parent
= PARENT_ENTRY(map
, entry
->next
);
447 rb_tree_remove_node(&map
->rb_tree
, &entry
->rb_node
);
450 * If the previous node has a new parent, fixup the tree starting
451 * at the previous node's old parent.
453 if (entry
->prev
!= &map
->header
) {
455 * Update the previous entry's gap due to our absence.
457 entry
->prev
->gap
= uvm_rb_gap(entry
->prev
);
458 uvm_rb_fixup(map
, entry
->prev
);
459 if (prev_parent
!= NULL
460 && prev_parent
!= entry
461 && prev_parent
!= PARENT_ENTRY(map
, entry
->prev
))
462 uvm_rb_fixup(map
, prev_parent
);
466 * If the next node has a new parent, fixup the tree starting
467 * at the next node's old parent.
469 if (entry
->next
!= &map
->header
) {
470 uvm_rb_fixup(map
, entry
->next
);
471 if (next_parent
!= NULL
472 && next_parent
!= entry
473 && next_parent
!= PARENT_ENTRY(map
, entry
->next
))
474 uvm_rb_fixup(map
, next_parent
);
479 int uvm_debug_check_map
= 0;
480 int uvm_debug_check_rbtree
= 0;
481 #define uvm_map_check(map, name) \
482 _uvm_map_check((map), (name), __FILE__, __LINE__)
484 _uvm_map_check(struct vm_map
*map
, const char *name
,
485 const char *file
, int line
)
488 if ((uvm_debug_check_map
&& _uvm_map_sanity(map
)) ||
489 (uvm_debug_check_rbtree
&& _uvm_tree_sanity(map
))) {
490 panic("uvm_map_check failed: \"%s\" map=%p (%s:%d)",
491 name
, map
, file
, line
);
494 #else /* defined(DEBUG) */
495 #define uvm_map_check(map, name) /* nothing */
496 #endif /* defined(DEBUG) */
498 #if defined(DEBUG) || defined(DDB)
500 _uvm_map_sanity(struct vm_map
*map
)
502 bool first_free_found
= false;
503 bool hint_found
= false;
504 const struct vm_map_entry
*e
;
505 struct vm_map_entry
*hint
= map
->hint
;
509 if (map
->first_free
== e
) {
510 first_free_found
= true;
511 } else if (!first_free_found
&& e
->next
->start
> e
->end
) {
512 printf("first_free %p should be %p\n",
521 if (e
== &map
->header
) {
525 if (!first_free_found
) {
526 printf("stale first_free\n");
530 printf("stale hint\n");
537 _uvm_tree_sanity(struct vm_map
*map
)
539 struct vm_map_entry
*tmp
, *trtmp
;
542 for (tmp
= map
->header
.next
; tmp
!= &map
->header
; tmp
= tmp
->next
) {
543 if (tmp
->gap
!= uvm_rb_gap(tmp
)) {
544 printf("%d/%d gap %lx != %lx %s\n",
545 n
+ 1, map
->nentries
,
546 (ulong
)tmp
->gap
, (ulong
)uvm_rb_gap(tmp
),
547 tmp
->next
== &map
->header
? "(last)" : "");
551 * If any entries are out of order, tmp->gap will be unsigned
552 * and will likely exceed the size of the map.
554 if (tmp
->gap
>= vm_map_max(map
) - vm_map_min(map
)) {
555 printf("too large gap %zu\n", (size_t)tmp
->gap
);
561 if (n
!= map
->nentries
) {
562 printf("nentries: %d vs %d\n", n
, map
->nentries
);
567 for (tmp
= map
->header
.next
; tmp
!= &map
->header
; tmp
= tmp
->next
) {
568 if (tmp
->maxgap
!= uvm_rb_maxgap(tmp
)) {
569 printf("maxgap %lx != %lx\n",
571 (ulong
)uvm_rb_maxgap(tmp
));
574 if (trtmp
!= NULL
&& trtmp
->start
>= tmp
->start
) {
575 printf("corrupt: 0x%"PRIxVADDR
"x >= 0x%"PRIxVADDR
"x\n",
576 trtmp
->start
, tmp
->start
);
583 for (tmp
= map
->header
.next
; tmp
!= &map
->header
;
584 tmp
= tmp
->next
, i
++) {
585 trtmp
= (void *) rb_tree_iterate(&map
->rb_tree
, &tmp
->rb_node
,
588 trtmp
= &map
->header
;
589 if (tmp
->prev
!= trtmp
) {
590 printf("lookup: %d: %p->prev=%p: %p\n",
591 i
, tmp
, tmp
->prev
, trtmp
);
594 trtmp
= (void *) rb_tree_iterate(&map
->rb_tree
, &tmp
->rb_node
,
597 trtmp
= &map
->header
;
598 if (tmp
->next
!= trtmp
) {
599 printf("lookup: %d: %p->next=%p: %p\n",
600 i
, tmp
, tmp
->next
, trtmp
);
603 trtmp
= (void *)rb_tree_find_node(&map
->rb_tree
, &tmp
->start
);
605 printf("lookup: %d: %p - %p: %p\n", i
, tmp
, trtmp
,
606 PARENT_ENTRY(map
, tmp
));
615 #endif /* defined(DEBUG) || defined(DDB) */
618 static struct vm_map
*uvm_kmapent_map(struct vm_map_entry
*);
622 * vm_map_lock: acquire an exclusive (write) lock on a map.
624 * => Note that "intrsafe" maps use only exclusive, spin locks.
626 * => The locking protocol provides for guaranteed upgrade from shared ->
627 * exclusive by whichever thread currently has the map marked busy.
628 * See "LOCKING PROTOCOL NOTES" in uvm_map.h. This is horrible; among
629 * other problems, it defeats any fairness guarantees provided by RW
634 vm_map_lock(struct vm_map
*map
)
637 if ((map
->flags
& VM_MAP_INTRSAFE
) != 0) {
638 mutex_spin_enter(&map
->mutex
);
643 rw_enter(&map
->lock
, RW_WRITER
);
644 if (map
->busy
== NULL
)
646 if (map
->busy
== curlwp
)
648 mutex_enter(&map
->misc_lock
);
650 if (map
->busy
!= NULL
)
651 cv_wait(&map
->cv
, &map
->misc_lock
);
652 mutex_exit(&map
->misc_lock
);
659 * vm_map_lock_try: try to lock a map, failing if it is already locked.
663 vm_map_lock_try(struct vm_map
*map
)
666 if ((map
->flags
& VM_MAP_INTRSAFE
) != 0)
667 return mutex_tryenter(&map
->mutex
);
668 if (!rw_tryenter(&map
->lock
, RW_WRITER
))
670 if (map
->busy
!= NULL
) {
680 * vm_map_unlock: release an exclusive lock on a map.
684 vm_map_unlock(struct vm_map
*map
)
687 if ((map
->flags
& VM_MAP_INTRSAFE
) != 0)
688 mutex_spin_exit(&map
->mutex
);
690 KASSERT(rw_write_held(&map
->lock
));
691 KASSERT(map
->busy
== NULL
|| map
->busy
== curlwp
);
697 * vm_map_unbusy: mark the map as unbusy, and wake any waiters that
698 * want an exclusive lock.
702 vm_map_unbusy(struct vm_map
*map
)
705 KASSERT(map
->busy
== curlwp
);
708 * Safe to clear 'busy' and 'waiters' with only a read lock held:
710 * o they can only be set with a write lock held
711 * o writers are blocked out with a read or write hold
712 * o at any time, only one thread owns the set of values
714 mutex_enter(&map
->misc_lock
);
716 cv_broadcast(&map
->cv
);
717 mutex_exit(&map
->misc_lock
);
721 * vm_map_lock_read: acquire a shared (read) lock on a map.
725 vm_map_lock_read(struct vm_map
*map
)
728 KASSERT((map
->flags
& VM_MAP_INTRSAFE
) == 0);
730 rw_enter(&map
->lock
, RW_READER
);
734 * vm_map_unlock_read: release a shared lock on a map.
738 vm_map_unlock_read(struct vm_map
*map
)
741 KASSERT((map
->flags
& VM_MAP_INTRSAFE
) == 0);
747 * vm_map_busy: mark a map as busy.
749 * => the caller must hold the map write locked
753 vm_map_busy(struct vm_map
*map
)
756 KASSERT(rw_write_held(&map
->lock
));
757 KASSERT(map
->busy
== NULL
);
763 * vm_map_locked_p: return true if the map is write locked.
765 * => only for debug purposes like KASSERTs.
766 * => should not be used to verify that a map is not locked.
770 vm_map_locked_p(struct vm_map
*map
)
773 if ((map
->flags
& VM_MAP_INTRSAFE
) != 0) {
774 return mutex_owned(&map
->mutex
);
776 return rw_write_held(&map
->lock
);
781 * uvm_mapent_alloc: allocate a map entry
784 static struct vm_map_entry
*
785 uvm_mapent_alloc(struct vm_map
*map
, int flags
)
787 struct vm_map_entry
*me
;
788 int pflags
= (flags
& UVM_FLAG_NOWAIT
) ? PR_NOWAIT
: PR_WAITOK
;
789 UVMHIST_FUNC("uvm_mapent_alloc"); UVMHIST_CALLED(maphist
);
791 if (VM_MAP_USE_KMAPENT(map
)) {
792 me
= uvm_kmapent_alloc(map
, flags
);
794 me
= pool_cache_get(&uvm_map_entry_cache
, pflags
);
795 if (__predict_false(me
== NULL
))
800 UVMHIST_LOG(maphist
, "<- new entry=0x%x [kentry=%d]", me
,
801 ((map
->flags
& VM_MAP_INTRSAFE
) != 0 || map
== kernel_map
), 0, 0);
806 * uvm_mapent_alloc_split: allocate a map entry for clipping.
808 * => map must be locked by caller if UVM_MAP_QUANTUM is set.
811 static struct vm_map_entry
*
812 uvm_mapent_alloc_split(struct vm_map
*map
,
813 const struct vm_map_entry
*old_entry
, int flags
,
814 struct uvm_mapent_reservation
*umr
)
816 struct vm_map_entry
*me
;
818 KASSERT(!VM_MAP_USE_KMAPENT(map
) ||
819 (old_entry
->flags
& UVM_MAP_QUANTUM
) || !UMR_EMPTY(umr
));
821 if (old_entry
->flags
& UVM_MAP_QUANTUM
) {
822 struct vm_map_kernel
*vmk
= vm_map_to_kernel(map
);
824 KASSERT(vm_map_locked_p(map
));
825 me
= vmk
->vmk_merged_entries
;
827 vmk
->vmk_merged_entries
= me
->next
;
828 KASSERT(me
->flags
& UVM_MAP_QUANTUM
);
830 me
= uvm_mapent_alloc(map
, flags
);
837 * uvm_mapent_free: free map entry
841 uvm_mapent_free(struct vm_map_entry
*me
)
843 UVMHIST_FUNC("uvm_mapent_free"); UVMHIST_CALLED(maphist
);
845 UVMHIST_LOG(maphist
,"<- freeing map entry=0x%x [flags=%d]",
846 me
, me
->flags
, 0, 0);
847 if (me
->flags
& UVM_MAP_KERNEL
) {
848 uvm_kmapent_free(me
);
850 pool_cache_put(&uvm_map_entry_cache
, me
);
855 * uvm_mapent_free_merged: free merged map entry
857 * => keep the entry if needed.
858 * => caller shouldn't hold map locked if VM_MAP_USE_KMAPENT(map) is true.
859 * => map should be locked if UVM_MAP_QUANTUM is set.
863 uvm_mapent_free_merged(struct vm_map
*map
, struct vm_map_entry
*me
)
866 KASSERT(!(me
->flags
& UVM_MAP_KERNEL
) || uvm_kmapent_map(me
) == map
);
868 if (me
->flags
& UVM_MAP_QUANTUM
) {
870 * keep this entry for later splitting.
872 struct vm_map_kernel
*vmk
;
874 KASSERT(vm_map_locked_p(map
));
875 KASSERT(VM_MAP_IS_KERNEL(map
));
876 KASSERT(!VM_MAP_USE_KMAPENT(map
) ||
877 (me
->flags
& UVM_MAP_KERNEL
));
879 vmk
= vm_map_to_kernel(map
);
880 me
->next
= vmk
->vmk_merged_entries
;
881 vmk
->vmk_merged_entries
= me
;
888 * uvm_mapent_copy: copy a map entry, preserving flags
892 uvm_mapent_copy(struct vm_map_entry
*src
, struct vm_map_entry
*dst
)
895 memcpy(dst
, src
, ((char *)&src
->uvm_map_entry_stop_copy
) -
900 * uvm_mapent_overhead: calculate maximum kva overhead necessary for
903 * => size and flags are the same as uvm_km_suballoc's ones.
907 uvm_mapent_overhead(vsize_t size
, int flags
)
910 if (VM_MAP_USE_KMAPENT_FLAGS(flags
)) {
911 return uvm_kmapent_overhead(size
);
918 _uvm_mapent_check(const struct vm_map_entry
*entry
, const char *file
, int line
)
921 if (entry
->start
>= entry
->end
) {
924 if (UVM_ET_ISOBJ(entry
)) {
925 if (entry
->object
.uvm_obj
== NULL
) {
928 } else if (UVM_ET_ISSUBMAP(entry
)) {
929 if (entry
->object
.sub_map
== NULL
) {
933 if (entry
->object
.uvm_obj
!= NULL
||
934 entry
->object
.sub_map
!= NULL
) {
938 if (!UVM_ET_ISOBJ(entry
)) {
939 if (entry
->offset
!= 0) {
947 panic("%s: bad entry %p (%s:%d)", __func__
, entry
, file
, line
);
949 #endif /* defined(DEBUG) */
952 * uvm_map_entry_unwire: unwire a map entry
954 * => map should be locked by caller
958 uvm_map_entry_unwire(struct vm_map
*map
, struct vm_map_entry
*entry
)
961 entry
->wired_count
= 0;
962 uvm_fault_unwire_locked(map
, entry
->start
, entry
->end
);
967 * wrapper for calling amap_ref()
970 uvm_map_reference_amap(struct vm_map_entry
*entry
, int flags
)
973 amap_ref(entry
->aref
.ar_amap
, entry
->aref
.ar_pageoff
,
974 (entry
->end
- entry
->start
) >> PAGE_SHIFT
, flags
);
979 * wrapper for calling amap_unref()
982 uvm_map_unreference_amap(struct vm_map_entry
*entry
, int flags
)
985 amap_unref(entry
->aref
.ar_amap
, entry
->aref
.ar_pageoff
,
986 (entry
->end
- entry
->start
) >> PAGE_SHIFT
, flags
);
991 * uvm_map_init: init mapping system at boot time.
998 static struct uvm_history_ent maphistbuf
[100];
999 static struct uvm_history_ent pdhistbuf
[100];
1003 * first, init logging system.
1006 UVMHIST_FUNC("uvm_map_init");
1007 UVMHIST_INIT_STATIC(maphist
, maphistbuf
);
1008 UVMHIST_INIT_STATIC(pdhist
, pdhistbuf
);
1009 UVMHIST_CALLED(maphist
);
1010 UVMHIST_LOG(maphist
,"<starting uvm map system>", 0, 0, 0, 0);
1013 * initialize the global lock for kernel map entry.
1016 mutex_init(&uvm_kentry_lock
, MUTEX_DRIVER
, IPL_VM
);
1019 * initialize caches.
1022 pool_cache_bootstrap(&uvm_map_entry_cache
, sizeof(struct vm_map_entry
),
1023 0, 0, 0, "vmmpepl", NULL
, IPL_NONE
, NULL
, NULL
, NULL
);
1024 pool_cache_bootstrap(&uvm_vmspace_cache
, sizeof(struct vmspace
),
1025 0, 0, 0, "vmsppl", NULL
, IPL_NONE
, NULL
, NULL
, NULL
);
1033 * uvm_mapent_splitadj: adjust map entries for splitting, after uvm_mapent_copy.
1037 uvm_mapent_splitadj(struct vm_map_entry
*entry1
, struct vm_map_entry
*entry2
,
1042 KASSERT(entry1
->start
< splitat
);
1043 KASSERT(splitat
< entry1
->end
);
1045 adj
= splitat
- entry1
->start
;
1046 entry1
->end
= entry2
->start
= splitat
;
1048 if (entry1
->aref
.ar_amap
) {
1049 amap_splitref(&entry1
->aref
, &entry2
->aref
, adj
);
1051 if (UVM_ET_ISSUBMAP(entry1
)) {
1052 /* ... unlikely to happen, but play it safe */
1053 uvm_map_reference(entry1
->object
.sub_map
);
1054 } else if (UVM_ET_ISOBJ(entry1
)) {
1055 KASSERT(entry1
->object
.uvm_obj
!= NULL
); /* suppress coverity */
1056 entry2
->offset
+= adj
;
1057 if (entry1
->object
.uvm_obj
->pgops
&&
1058 entry1
->object
.uvm_obj
->pgops
->pgo_reference
)
1059 entry1
->object
.uvm_obj
->pgops
->pgo_reference(
1060 entry1
->object
.uvm_obj
);
1065 * uvm_map_clip_start: ensure that the entry begins at or after
1066 * the starting address, if it doesn't we split the entry.
1068 * => caller should use UVM_MAP_CLIP_START macro rather than calling
1070 * => map must be locked by caller
1074 uvm_map_clip_start(struct vm_map
*map
, struct vm_map_entry
*entry
,
1075 vaddr_t start
, struct uvm_mapent_reservation
*umr
)
1077 struct vm_map_entry
*new_entry
;
1079 /* uvm_map_simplify_entry(map, entry); */ /* XXX */
1081 uvm_map_check(map
, "clip_start entry");
1082 uvm_mapent_check(entry
);
1085 * Split off the front portion. note that we must insert the new
1086 * entry BEFORE this one, so that this entry has the specified
1089 new_entry
= uvm_mapent_alloc_split(map
, entry
, 0, umr
);
1090 uvm_mapent_copy(entry
, new_entry
); /* entry -> new_entry */
1091 uvm_mapent_splitadj(new_entry
, entry
, start
);
1092 uvm_map_entry_link(map
, entry
->prev
, new_entry
);
1094 uvm_map_check(map
, "clip_start leave");
1098 * uvm_map_clip_end: ensure that the entry ends at or before
1099 * the ending address, if it does't we split the reference
1101 * => caller should use UVM_MAP_CLIP_END macro rather than calling
1103 * => map must be locked by caller
1107 uvm_map_clip_end(struct vm_map
*map
, struct vm_map_entry
*entry
, vaddr_t end
,
1108 struct uvm_mapent_reservation
*umr
)
1110 struct vm_map_entry
*new_entry
;
1112 uvm_map_check(map
, "clip_end entry");
1113 uvm_mapent_check(entry
);
1116 * Create a new entry and insert it
1117 * AFTER the specified entry
1119 new_entry
= uvm_mapent_alloc_split(map
, entry
, 0, umr
);
1120 uvm_mapent_copy(entry
, new_entry
); /* entry -> new_entry */
1121 uvm_mapent_splitadj(entry
, new_entry
, end
);
1122 uvm_map_entry_link(map
, entry
, new_entry
);
1124 uvm_map_check(map
, "clip_end leave");
1128 vm_map_drain(struct vm_map
*map
, uvm_flag_t flags
)
1131 if (!VM_MAP_IS_KERNEL(map
)) {
1135 uvm_km_va_drain(map
, flags
);
1139 * M A P - m a i n e n t r y p o i n t
1142 * uvm_map: establish a valid mapping in a map
1144 * => assume startp is page aligned.
1145 * => assume size is a multiple of PAGE_SIZE.
1146 * => assume sys_mmap provides enough of a "hint" to have us skip
1147 * over text/data/bss area.
1148 * => map must be unlocked (we will lock it)
1149 * => <uobj,uoffset> value meanings (4 cases):
1150 * [1] <NULL,uoffset> == uoffset is a hint for PMAP_PREFER
1151 * [2] <NULL,UVM_UNKNOWN_OFFSET> == don't PMAP_PREFER
1152 * [3] <uobj,uoffset> == normal mapping
1153 * [4] <uobj,UVM_UNKNOWN_OFFSET> == uvm_map finds offset based on VA
1155 * case [4] is for kernel mappings where we don't know the offset until
1156 * we've found a virtual address. note that kernel object offsets are
1157 * always relative to vm_map_min(kernel_map).
1159 * => if `align' is non-zero, we align the virtual address to the specified
1161 * this is provided as a mechanism for large pages.
1163 * => XXXCDC: need way to map in external amap?
1167 uvm_map(struct vm_map
*map
, vaddr_t
*startp
/* IN/OUT */, vsize_t size
,
1168 struct uvm_object
*uobj
, voff_t uoffset
, vsize_t align
, uvm_flag_t flags
)
1170 struct uvm_map_args args
;
1171 struct vm_map_entry
*new_entry
;
1174 KASSERT((flags
& UVM_FLAG_QUANTUM
) == 0 || VM_MAP_IS_KERNEL(map
));
1175 KASSERT((size
& PAGE_MASK
) == 0);
1178 * for pager_map, allocate the new entry first to avoid sleeping
1179 * for memory while we have the map locked.
1181 * Also, because we allocate entries for in-kernel maps
1182 * a bit differently (cf. uvm_kmapent_alloc/free), we need to
1183 * allocate them before locking the map.
1187 if (VM_MAP_USE_KMAPENT(map
) || (flags
& UVM_FLAG_QUANTUM
) ||
1189 new_entry
= uvm_mapent_alloc(map
, (flags
& UVM_FLAG_NOWAIT
));
1190 if (__predict_false(new_entry
== NULL
))
1192 if (flags
& UVM_FLAG_QUANTUM
)
1193 new_entry
->flags
|= UVM_MAP_QUANTUM
;
1195 if (map
== pager_map
)
1196 flags
|= UVM_FLAG_NOMERGE
;
1198 error
= uvm_map_prepare(map
, *startp
, size
, uobj
, uoffset
, align
,
1201 error
= uvm_map_enter(map
, &args
, new_entry
);
1202 *startp
= args
.uma_start
;
1203 } else if (new_entry
) {
1204 uvm_mapent_free(new_entry
);
1208 if (!error
&& VM_MAP_IS_KERNEL(map
)) {
1209 uvm_km_check_empty(map
, *startp
, *startp
+ size
);
1211 #endif /* defined(DEBUG) */
1217 uvm_map_prepare(struct vm_map
*map
, vaddr_t start
, vsize_t size
,
1218 struct uvm_object
*uobj
, voff_t uoffset
, vsize_t align
, uvm_flag_t flags
,
1219 struct uvm_map_args
*args
)
1221 struct vm_map_entry
*prev_entry
;
1222 vm_prot_t prot
= UVM_PROTECTION(flags
);
1223 vm_prot_t maxprot
= UVM_MAXPROTECTION(flags
);
1225 UVMHIST_FUNC("uvm_map_prepare");
1226 UVMHIST_CALLED(maphist
);
1228 UVMHIST_LOG(maphist
, "(map=0x%x, start=0x%x, size=%d, flags=0x%x)",
1229 map
, start
, size
, flags
);
1230 UVMHIST_LOG(maphist
, " uobj/offset 0x%x/%d", uobj
, uoffset
,0,0);
1233 * detect a popular device driver bug.
1236 KASSERT(doing_shutdown
|| curlwp
!= NULL
||
1237 (map
->flags
& VM_MAP_INTRSAFE
));
1240 * zero-sized mapping doesn't make any sense.
1244 KASSERT((~flags
& (UVM_FLAG_NOWAIT
| UVM_FLAG_WAITVA
)) != 0);
1246 uvm_map_check(map
, "map entry");
1249 * check sanity of protection code
1252 if ((prot
& maxprot
) != prot
) {
1253 UVMHIST_LOG(maphist
, "<- prot. failure: prot=0x%x, max=0x%x",
1259 * figure out where to put new VM range
1263 if (vm_map_lock_try(map
) == false) {
1264 if ((flags
& UVM_FLAG_TRYLOCK
) != 0 &&
1265 (map
->flags
& VM_MAP_INTRSAFE
) == 0) {
1268 vm_map_lock(map
); /* could sleep here */
1270 prev_entry
= uvm_map_findspace(map
, start
, size
, &start
,
1271 uobj
, uoffset
, align
, flags
);
1272 if (prev_entry
== NULL
) {
1273 unsigned int timestamp
;
1275 timestamp
= map
->timestamp
;
1276 UVMHIST_LOG(maphist
,"waiting va timestamp=0x%x",
1278 map
->flags
|= VM_MAP_WANTVA
;
1282 * try to reclaim kva and wait until someone does unmap.
1283 * fragile locking here, so we awaken every second to
1284 * recheck the condition.
1287 vm_map_drain(map
, flags
);
1289 mutex_enter(&map
->misc_lock
);
1290 while ((map
->flags
& VM_MAP_WANTVA
) != 0 &&
1291 map
->timestamp
== timestamp
) {
1292 if ((flags
& UVM_FLAG_WAITVA
) == 0) {
1293 mutex_exit(&map
->misc_lock
);
1294 UVMHIST_LOG(maphist
,
1295 "<- uvm_map_findspace failed!", 0,0,0,0);
1298 cv_timedwait(&map
->cv
, &map
->misc_lock
, hz
);
1301 mutex_exit(&map
->misc_lock
);
1305 #ifdef PMAP_GROWKERNEL
1307 * If the kernel pmap can't map the requested space,
1308 * then allocate more resources for it.
1310 if (map
== kernel_map
&& uvm_maxkaddr
< (start
+ size
))
1311 uvm_maxkaddr
= pmap_growkernel(start
+ size
);
1314 UVMMAP_EVCNT_INCR(map_call
);
1317 * if uobj is null, then uoffset is either a VAC hint for PMAP_PREFER
1318 * [typically from uvm_map_reserve] or it is UVM_UNKNOWN_OFFSET. in
1319 * either case we want to zero it before storing it in the map entry
1320 * (because it looks strange and confusing when debugging...)
1322 * if uobj is not null
1323 * if uoffset is not UVM_UNKNOWN_OFFSET then we have a normal mapping
1324 * and we do not need to change uoffset.
1325 * if uoffset is UVM_UNKNOWN_OFFSET then we need to find the offset
1326 * now (based on the starting address of the map). this case is
1327 * for kernel object mappings where we don't know the offset until
1328 * the virtual address is found (with uvm_map_findspace). the
1329 * offset is the distance we are from the start of the map.
1335 if (uoffset
== UVM_UNKNOWN_OFFSET
) {
1336 KASSERT(UVM_OBJ_IS_KERN_OBJECT(uobj
));
1337 uoffset
= start
- vm_map_min(kernel_map
);
1341 args
->uma_flags
= flags
;
1342 args
->uma_prev
= prev_entry
;
1343 args
->uma_start
= start
;
1344 args
->uma_size
= size
;
1345 args
->uma_uobj
= uobj
;
1346 args
->uma_uoffset
= uoffset
;
1348 UVMHIST_LOG(maphist
, "<- done!", 0,0,0,0);
1353 uvm_map_enter(struct vm_map
*map
, const struct uvm_map_args
*args
,
1354 struct vm_map_entry
*new_entry
)
1356 struct vm_map_entry
*prev_entry
= args
->uma_prev
;
1357 struct vm_map_entry
*dead
= NULL
;
1359 const uvm_flag_t flags
= args
->uma_flags
;
1360 const vm_prot_t prot
= UVM_PROTECTION(flags
);
1361 const vm_prot_t maxprot
= UVM_MAXPROTECTION(flags
);
1362 const vm_inherit_t inherit
= UVM_INHERIT(flags
);
1363 const int amapwaitflag
= (flags
& UVM_FLAG_NOWAIT
) ?
1364 AMAP_EXTEND_NOWAIT
: 0;
1365 const int advice
= UVM_ADVICE(flags
);
1366 const int meflagval
= (flags
& UVM_FLAG_QUANTUM
) ?
1367 UVM_MAP_QUANTUM
: 0;
1369 vaddr_t start
= args
->uma_start
;
1370 vsize_t size
= args
->uma_size
;
1371 struct uvm_object
*uobj
= args
->uma_uobj
;
1372 voff_t uoffset
= args
->uma_uoffset
;
1374 const int kmap
= (vm_map_pmap(map
) == pmap_kernel());
1379 UVMHIST_FUNC("uvm_map_enter");
1380 UVMHIST_CALLED(maphist
);
1382 UVMHIST_LOG(maphist
, "(map=0x%x, start=0x%x, size=%d, flags=0x%x)",
1383 map
, start
, size
, flags
);
1384 UVMHIST_LOG(maphist
, " uobj/offset 0x%x/%d", uobj
, uoffset
,0,0);
1386 KASSERT(map
->hint
== prev_entry
); /* bimerge case assumes this */
1388 if (flags
& UVM_FLAG_QUANTUM
) {
1390 KASSERT(new_entry
->flags
& UVM_MAP_QUANTUM
);
1394 newetype
= UVM_ET_OBJ
;
1398 if (flags
& UVM_FLAG_COPYONW
) {
1399 newetype
|= UVM_ET_COPYONWRITE
;
1400 if ((flags
& UVM_FLAG_OVERLAY
) == 0)
1401 newetype
|= UVM_ET_NEEDSCOPY
;
1405 * try and insert in map by extending previous entry, if possible.
1406 * XXX: we don't try and pull back the next entry. might be useful
1407 * for a stack, but we are currently allocating our stack in advance.
1410 if (flags
& UVM_FLAG_NOMERGE
)
1413 if (prev_entry
->end
== start
&&
1414 prev_entry
!= &map
->header
&&
1415 UVM_ET_ISCOMPATIBLE(prev_entry
, newetype
, uobj
, meflagval
,
1416 prot
, maxprot
, inherit
, advice
, 0)) {
1418 if (uobj
&& prev_entry
->offset
+
1419 (prev_entry
->end
- prev_entry
->start
) != uoffset
)
1423 * can't extend a shared amap. note: no need to lock amap to
1424 * look at refs since we don't care about its exact value.
1425 * if it is one (i.e. we have only reference) it will stay there
1428 if (prev_entry
->aref
.ar_amap
&&
1429 amap_refs(prev_entry
->aref
.ar_amap
) != 1) {
1433 if (prev_entry
->aref
.ar_amap
) {
1434 error
= amap_extend(prev_entry
, size
,
1435 amapwaitflag
| AMAP_EXTEND_FORWARDS
);
1441 UVMMAP_EVCNT_INCR(kbackmerge
);
1443 UVMMAP_EVCNT_INCR(ubackmerge
);
1445 UVMHIST_LOG(maphist
," starting back merge", 0, 0, 0, 0);
1448 * drop our reference to uobj since we are extending a reference
1449 * that we already have (the ref count can not drop to zero).
1452 if (uobj
&& uobj
->pgops
->pgo_detach
)
1453 uobj
->pgops
->pgo_detach(uobj
);
1456 * Now that we've merged the entries, note that we've grown
1457 * and our gap has shrunk. Then fix the tree.
1459 prev_entry
->end
+= size
;
1460 prev_entry
->gap
-= size
;
1461 uvm_rb_fixup(map
, prev_entry
);
1463 uvm_map_check(map
, "map backmerged");
1465 UVMHIST_LOG(maphist
,"<- done (via backmerge)!", 0, 0, 0, 0);
1470 if (prev_entry
->next
->start
== (start
+ size
) &&
1471 prev_entry
->next
!= &map
->header
&&
1472 UVM_ET_ISCOMPATIBLE(prev_entry
->next
, newetype
, uobj
, meflagval
,
1473 prot
, maxprot
, inherit
, advice
, 0)) {
1475 if (uobj
&& prev_entry
->next
->offset
!= uoffset
+ size
)
1479 * can't extend a shared amap. note: no need to lock amap to
1480 * look at refs since we don't care about its exact value.
1481 * if it is one (i.e. we have only reference) it will stay there.
1483 * note that we also can't merge two amaps, so if we
1484 * merged with the previous entry which has an amap,
1485 * and the next entry also has an amap, we give up.
1487 * Interesting cases:
1488 * amap, new, amap -> give up second merge (single fwd extend)
1489 * amap, new, none -> double forward extend (extend again here)
1490 * none, new, amap -> double backward extend (done here)
1491 * uobj, new, amap -> single backward extend (done here)
1493 * XXX should we attempt to deal with someone refilling
1494 * the deallocated region between two entries that are
1495 * backed by the same amap (ie, arefs is 2, "prev" and
1496 * "next" refer to it, and adding this allocation will
1497 * close the hole, thus restoring arefs to 1 and
1498 * deallocating the "next" vm_map_entry)? -- @@@
1501 if (prev_entry
->next
->aref
.ar_amap
&&
1502 (amap_refs(prev_entry
->next
->aref
.ar_amap
) != 1 ||
1503 (merged
&& prev_entry
->aref
.ar_amap
))) {
1509 * Try to extend the amap of the previous entry to
1510 * cover the next entry as well. If it doesn't work
1511 * just skip on, don't actually give up, since we've
1512 * already completed the back merge.
1514 if (prev_entry
->aref
.ar_amap
) {
1515 if (amap_extend(prev_entry
,
1516 prev_entry
->next
->end
-
1517 prev_entry
->next
->start
,
1518 amapwaitflag
| AMAP_EXTEND_FORWARDS
))
1523 * Try to extend the amap of the *next* entry
1524 * back to cover the new allocation *and* the
1525 * previous entry as well (the previous merge
1526 * didn't have an amap already otherwise we
1527 * wouldn't be checking here for an amap). If
1528 * it doesn't work just skip on, again, don't
1529 * actually give up, since we've already
1530 * completed the back merge.
1532 else if (prev_entry
->next
->aref
.ar_amap
) {
1533 if (amap_extend(prev_entry
->next
,
1536 amapwaitflag
| AMAP_EXTEND_BACKWARDS
))
1541 * Pull the next entry's amap backwards to cover this
1544 if (prev_entry
->next
->aref
.ar_amap
) {
1545 error
= amap_extend(prev_entry
->next
, size
,
1546 amapwaitflag
| AMAP_EXTEND_BACKWARDS
);
1554 UVMMAP_EVCNT_DECR(kbackmerge
);
1555 UVMMAP_EVCNT_INCR(kbimerge
);
1557 UVMMAP_EVCNT_DECR(ubackmerge
);
1558 UVMMAP_EVCNT_INCR(ubimerge
);
1562 UVMMAP_EVCNT_INCR(kforwmerge
);
1564 UVMMAP_EVCNT_INCR(uforwmerge
);
1567 UVMHIST_LOG(maphist
," starting forward merge", 0, 0, 0, 0);
1570 * drop our reference to uobj since we are extending a reference
1571 * that we already have (the ref count can not drop to zero).
1572 * (if merged, we've already detached)
1574 if (uobj
&& uobj
->pgops
->pgo_detach
&& !merged
)
1575 uobj
->pgops
->pgo_detach(uobj
);
1578 dead
= prev_entry
->next
;
1579 prev_entry
->end
= dead
->end
;
1580 uvm_map_entry_unlink(map
, dead
);
1581 if (dead
->aref
.ar_amap
!= NULL
) {
1582 prev_entry
->aref
= dead
->aref
;
1583 dead
->aref
.ar_amap
= NULL
;
1586 prev_entry
->next
->start
-= size
;
1587 if (prev_entry
!= &map
->header
) {
1588 prev_entry
->gap
-= size
;
1589 KASSERT(prev_entry
->gap
== uvm_rb_gap(prev_entry
));
1590 uvm_rb_fixup(map
, prev_entry
);
1593 prev_entry
->next
->offset
= uoffset
;
1596 uvm_map_check(map
, "map forwardmerged");
1598 UVMHIST_LOG(maphist
,"<- done forwardmerge", 0, 0, 0, 0);
1604 UVMHIST_LOG(maphist
," allocating new map entry", 0, 0, 0, 0);
1606 UVMMAP_EVCNT_INCR(knomerge
);
1608 UVMMAP_EVCNT_INCR(unomerge
);
1612 * allocate new entry and link it in.
1615 if (new_entry
== NULL
) {
1616 new_entry
= uvm_mapent_alloc(map
,
1617 (flags
& UVM_FLAG_NOWAIT
));
1618 if (__predict_false(new_entry
== NULL
)) {
1623 new_entry
->start
= start
;
1624 new_entry
->end
= new_entry
->start
+ size
;
1625 new_entry
->object
.uvm_obj
= uobj
;
1626 new_entry
->offset
= uoffset
;
1628 new_entry
->etype
= newetype
;
1630 if (flags
& UVM_FLAG_NOMERGE
) {
1631 new_entry
->flags
|= UVM_MAP_NOMERGE
;
1634 new_entry
->protection
= prot
;
1635 new_entry
->max_protection
= maxprot
;
1636 new_entry
->inheritance
= inherit
;
1637 new_entry
->wired_count
= 0;
1638 new_entry
->advice
= advice
;
1639 if (flags
& UVM_FLAG_OVERLAY
) {
1642 * to_add: for BSS we overallocate a little since we
1643 * are likely to extend
1646 vaddr_t to_add
= (flags
& UVM_FLAG_AMAPPAD
) ?
1647 UVM_AMAP_CHUNK
<< PAGE_SHIFT
: 0;
1648 struct vm_amap
*amap
= amap_alloc(size
, to_add
,
1649 (flags
& UVM_FLAG_NOWAIT
));
1650 if (__predict_false(amap
== NULL
)) {
1654 new_entry
->aref
.ar_pageoff
= 0;
1655 new_entry
->aref
.ar_amap
= amap
;
1657 new_entry
->aref
.ar_pageoff
= 0;
1658 new_entry
->aref
.ar_amap
= NULL
;
1660 uvm_map_entry_link(map
, prev_entry
, new_entry
);
1663 * Update the free space hint
1666 if ((map
->first_free
== prev_entry
) &&
1667 (prev_entry
->end
>= new_entry
->start
))
1668 map
->first_free
= new_entry
;
1675 UVMHIST_LOG(maphist
,"<- done!", 0, 0, 0, 0);
1679 if ((flags
& UVM_FLAG_QUANTUM
) == 0) {
1681 * vmk_merged_entries is locked by the map's lock.
1685 if (new_entry
&& error
== 0) {
1687 uvm_mapent_free_merged(map
, new_entry
);
1692 uvm_mapent_free_merged(map
, dead
);
1694 if ((flags
& UVM_FLAG_QUANTUM
) != 0) {
1697 if (new_entry
!= NULL
) {
1698 uvm_mapent_free(new_entry
);
1704 * uvm_map_lookup_entry_bytree: lookup an entry in tree
1708 uvm_map_lookup_entry_bytree(struct vm_map
*map
, vaddr_t address
,
1709 struct vm_map_entry
**entry
/* OUT */)
1711 struct vm_map_entry
*prev
= &map
->header
;
1712 struct vm_map_entry
*cur
= ROOT_ENTRY(map
);
1715 UVMMAP_EVCNT_INCR(mlk_treeloop
);
1716 if (address
>= cur
->start
) {
1717 if (address
< cur
->end
) {
1722 cur
= RIGHT_ENTRY(cur
);
1724 cur
= LEFT_ENTRY(cur
);
1731 * uvm_map_lookup_entry: find map entry at or before an address
1733 * => map must at least be read-locked by caller
1734 * => entry is returned in "entry"
1735 * => return value is true if address is in the returned entry
1739 uvm_map_lookup_entry(struct vm_map
*map
, vaddr_t address
,
1740 struct vm_map_entry
**entry
/* OUT */)
1742 struct vm_map_entry
*cur
;
1743 bool use_tree
= false;
1744 UVMHIST_FUNC("uvm_map_lookup_entry");
1745 UVMHIST_CALLED(maphist
);
1747 UVMHIST_LOG(maphist
,"(map=0x%x,addr=0x%x,ent=0x%x)",
1748 map
, address
, entry
, 0);
1751 * start looking either from the head of the
1752 * list, or from the hint.
1757 if (cur
== &map
->header
)
1760 UVMMAP_EVCNT_INCR(mlk_call
);
1761 if (address
>= cur
->start
) {
1764 * go from hint to end of list.
1766 * but first, make a quick check to see if
1767 * we are already looking at the entry we
1768 * want (which is usually the case).
1769 * note also that we don't need to save the hint
1770 * here... it is the same hint (unless we are
1771 * at the header, in which case the hint didn't
1772 * buy us anything anyway).
1775 if (cur
!= &map
->header
&& cur
->end
> address
) {
1776 UVMMAP_EVCNT_INCR(mlk_hint
);
1778 UVMHIST_LOG(maphist
,"<- got it via hint (0x%x)",
1780 uvm_mapent_check(*entry
);
1784 if (map
->nentries
> 15)
1789 * invalid hint. use tree.
1794 uvm_map_check(map
, __func__
);
1798 * Simple lookup in the tree. Happens when the hint is
1799 * invalid, or nentries reach a threshold.
1801 UVMMAP_EVCNT_INCR(mlk_tree
);
1802 if (uvm_map_lookup_entry_bytree(map
, address
, entry
)) {
1813 UVMMAP_EVCNT_INCR(mlk_list
);
1814 while (cur
!= &map
->header
) {
1815 UVMMAP_EVCNT_INCR(mlk_listloop
);
1816 if (cur
->end
> address
) {
1817 if (address
>= cur
->start
) {
1819 * save this lookup for future
1825 SAVE_HINT(map
, map
->hint
, *entry
);
1826 UVMHIST_LOG(maphist
,"<- search got it (0x%x)",
1828 KDASSERT((*entry
)->start
<= address
);
1829 KDASSERT(address
< (*entry
)->end
);
1830 uvm_mapent_check(*entry
);
1839 SAVE_HINT(map
, map
->hint
, *entry
);
1840 UVMHIST_LOG(maphist
,"<- failed!",0,0,0,0);
1841 KDASSERT((*entry
) == &map
->header
|| (*entry
)->end
<= address
);
1842 KDASSERT((*entry
)->next
== &map
->header
||
1843 address
< (*entry
)->next
->start
);
1848 * See if the range between start and start + length fits in the gap
1849 * entry->next->start and entry->end. Returns 1 if fits, 0 if doesn't
1850 * fit, and -1 address wraps around.
1853 uvm_map_space_avail(vaddr_t
*start
, vsize_t length
, voff_t uoffset
,
1854 vsize_t align
, int topdown
, struct vm_map_entry
*entry
)
1860 * push start address forward as needed to avoid VAC alias problems.
1861 * we only do this if a valid offset is specified.
1864 if (uoffset
!= UVM_UNKNOWN_OFFSET
)
1865 PMAP_PREFER(uoffset
, start
, length
, topdown
);
1868 if ((*start
& (align
- 1)) != 0) {
1870 *start
&= ~(align
- 1);
1872 *start
= roundup(*start
, align
);
1875 * XXX Should we PMAP_PREFER() here again?
1876 * eh...i think we're okay
1881 * Find the end of the proposed new region. Be sure we didn't
1882 * wrap around the address; if so, we lose. Otherwise, if the
1883 * proposed new region fits before the next entry, we win.
1886 end
= *start
+ length
;
1890 if (entry
->next
->start
>= end
&& *start
>= entry
->end
)
1897 * uvm_map_findspace: find "length" sized space in "map".
1899 * => "hint" is a hint about where we want it, unless UVM_FLAG_FIXED is
1900 * set in "flags" (in which case we insist on using "hint").
1901 * => "result" is VA returned
1902 * => uobj/uoffset are to be used to handle VAC alignment, if required
1903 * => if "align" is non-zero, we attempt to align to that value.
1904 * => caller must at least have read-locked map
1905 * => returns NULL on failure, or pointer to prev. map entry if success
1906 * => note this is a cross between the old vm_map_findspace and vm_map_find
1909 struct vm_map_entry
*
1910 uvm_map_findspace(struct vm_map
*map
, vaddr_t hint
, vsize_t length
,
1911 vaddr_t
*result
/* OUT */, struct uvm_object
*uobj
, voff_t uoffset
,
1912 vsize_t align
, int flags
)
1914 struct vm_map_entry
*entry
;
1915 struct vm_map_entry
*child
, *prev
, *tmp
;
1917 const int topdown
= map
->flags
& VM_MAP_TOPDOWN
;
1918 UVMHIST_FUNC("uvm_map_findspace");
1919 UVMHIST_CALLED(maphist
);
1921 UVMHIST_LOG(maphist
, "(map=0x%x, hint=0x%x, len=%d, flags=0x%x)",
1922 map
, hint
, length
, flags
);
1923 KASSERT((align
& (align
- 1)) == 0);
1924 KASSERT((flags
& UVM_FLAG_FIXED
) == 0 || align
== 0);
1926 uvm_map_check(map
, "map_findspace entry");
1929 * remember the original hint. if we are aligning, then we
1930 * may have to try again with no alignment constraint if
1931 * we fail the first time.
1935 if (hint
< vm_map_min(map
)) { /* check ranges ... */
1936 if (flags
& UVM_FLAG_FIXED
) {
1937 UVMHIST_LOG(maphist
,"<- VA below map range",0,0,0,0);
1940 hint
= vm_map_min(map
);
1942 if (hint
> vm_map_max(map
)) {
1943 UVMHIST_LOG(maphist
,"<- VA 0x%x > range [0x%x->0x%x]",
1944 hint
, vm_map_min(map
), vm_map_max(map
), 0);
1949 * Look for the first possible address; if there's already
1950 * something at this address, we have to start after it.
1954 * @@@: there are four, no, eight cases to consider.
1956 * 0: found, fixed, bottom up -> fail
1957 * 1: found, fixed, top down -> fail
1958 * 2: found, not fixed, bottom up -> start after entry->end,
1960 * 3: found, not fixed, top down -> start before entry->start,
1962 * 4: not found, fixed, bottom up -> check entry->next->start, fail
1963 * 5: not found, fixed, top down -> check entry->next->start, fail
1964 * 6: not found, not fixed, bottom up -> check entry->next->start,
1966 * 7: not found, not fixed, top down -> check entry->next->start,
1969 * as you can see, it reduces to roughly five cases, and that
1970 * adding top down mapping only adds one unique case (without
1971 * it, there would be four cases).
1974 if ((flags
& UVM_FLAG_FIXED
) == 0 && hint
== vm_map_min(map
)) {
1975 entry
= map
->first_free
;
1977 if (uvm_map_lookup_entry(map
, hint
, &entry
)) {
1978 /* "hint" address already in use ... */
1979 if (flags
& UVM_FLAG_FIXED
) {
1980 UVMHIST_LOG(maphist
, "<- fixed & VA in use",
1985 /* Start from lower gap. */
1986 entry
= entry
->prev
;
1987 } else if (flags
& UVM_FLAG_FIXED
) {
1988 if (entry
->next
->start
>= hint
+ length
&&
1989 hint
+ length
> hint
)
1992 /* "hint" address is gap but too small */
1993 UVMHIST_LOG(maphist
, "<- fixed mapping failed",
1995 return (NULL
); /* only one shot at it ... */
1998 * See if given hint fits in this gap.
2000 switch (uvm_map_space_avail(&hint
, length
,
2001 uoffset
, align
, topdown
, entry
)) {
2010 * Still there is a chance to fit
2011 * if hint > entry->end.
2014 /* Start from higher gap. */
2015 entry
= entry
->next
;
2016 if (entry
== &map
->header
)
2024 * Note that all UVM_FLAGS_FIXED case is already handled.
2026 KDASSERT((flags
& UVM_FLAG_FIXED
) == 0);
2028 /* Try to find the space in the red-black tree */
2030 /* Check slot before any entry */
2031 hint
= topdown
? entry
->next
->start
- length
: entry
->end
;
2032 switch (uvm_map_space_avail(&hint
, length
, uoffset
, align
,
2041 KDASSERT((flags
& UVM_FLAG_FIXED
) == 0);
2042 /* If there is not enough space in the whole tree, we fail */
2043 tmp
= ROOT_ENTRY(map
);
2044 if (tmp
== NULL
|| tmp
->maxgap
< length
)
2047 prev
= NULL
; /* previous candidate */
2049 /* Find an entry close to hint that has enough space */
2051 KASSERT(tmp
->next
->start
== tmp
->end
+ tmp
->gap
);
2053 if (tmp
->next
->start
< hint
+ length
&&
2054 (prev
== NULL
|| tmp
->end
> prev
->end
)) {
2055 if (tmp
->gap
>= length
)
2057 else if ((child
= LEFT_ENTRY(tmp
)) != NULL
2058 && child
->maxgap
>= length
)
2062 if (tmp
->end
>= hint
&&
2063 (prev
== NULL
|| tmp
->end
< prev
->end
)) {
2064 if (tmp
->gap
>= length
)
2066 else if ((child
= RIGHT_ENTRY(tmp
)) != NULL
2067 && child
->maxgap
>= length
)
2071 if (tmp
->next
->start
< hint
+ length
)
2072 child
= RIGHT_ENTRY(tmp
);
2073 else if (tmp
->end
> hint
)
2074 child
= LEFT_ENTRY(tmp
);
2076 if (tmp
->gap
>= length
)
2079 child
= LEFT_ENTRY(tmp
);
2081 child
= RIGHT_ENTRY(tmp
);
2083 if (child
== NULL
|| child
->maxgap
< length
)
2088 if (tmp
!= NULL
&& tmp
->start
< hint
&& hint
< tmp
->next
->start
) {
2090 * Check if the entry that we found satifies the
2094 if (hint
> tmp
->next
->start
- length
)
2095 hint
= tmp
->next
->start
- length
;
2097 if (hint
< tmp
->end
)
2100 switch (uvm_map_space_avail(&hint
, length
, uoffset
, align
,
2108 if (tmp
->gap
>= length
)
2115 KASSERT(orig_hint
>= prev
->next
->start
- length
||
2116 prev
->next
->start
- length
> prev
->next
->start
);
2117 hint
= prev
->next
->start
- length
;
2119 KASSERT(orig_hint
<= prev
->end
);
2122 switch (uvm_map_space_avail(&hint
, length
, uoffset
, align
,
2130 if (prev
->gap
>= length
)
2134 tmp
= LEFT_ENTRY(prev
);
2136 tmp
= RIGHT_ENTRY(prev
);
2138 KASSERT(tmp
&& tmp
->maxgap
>= length
);
2140 child
= RIGHT_ENTRY(tmp
);
2142 child
= LEFT_ENTRY(tmp
);
2143 if (child
&& child
->maxgap
>= length
) {
2147 if (tmp
->gap
>= length
)
2150 tmp
= LEFT_ENTRY(tmp
);
2152 tmp
= RIGHT_ENTRY(tmp
);
2156 KASSERT(orig_hint
>= tmp
->next
->start
- length
||
2157 tmp
->next
->start
- length
> tmp
->next
->start
);
2158 hint
= tmp
->next
->start
- length
;
2160 KASSERT(orig_hint
<= tmp
->end
);
2163 switch (uvm_map_space_avail(&hint
, length
, uoffset
, align
,
2173 * The tree fails to find an entry because of offset or alignment
2174 * restrictions. Search the list instead.
2178 * Look through the rest of the map, trying to fit a new region in
2179 * the gap between existing regions, or after the very last region.
2180 * note: entry->end = base VA of current gap,
2181 * entry->next->start = VA of end of current gap
2185 /* Update hint for current gap. */
2186 hint
= topdown
? entry
->next
->start
- length
: entry
->end
;
2188 /* See if it fits. */
2189 switch (uvm_map_space_avail(&hint
, length
, uoffset
, align
,
2197 /* Advance to next/previous gap */
2199 if (entry
== &map
->header
) {
2200 UVMHIST_LOG(maphist
, "<- failed (off start)",
2204 entry
= entry
->prev
;
2206 entry
= entry
->next
;
2207 if (entry
== &map
->header
) {
2208 UVMHIST_LOG(maphist
, "<- failed (off end)",
2216 SAVE_HINT(map
, map
->hint
, entry
);
2218 UVMHIST_LOG(maphist
,"<- got it! (result=0x%x)", hint
, 0,0,0);
2219 KASSERT( topdown
|| hint
>= orig_hint
);
2220 KASSERT(!topdown
|| hint
<= orig_hint
);
2221 KASSERT(entry
->end
<= hint
);
2222 KASSERT(hint
+ length
<= entry
->next
->start
);
2226 UVMHIST_LOG(maphist
, "<- failed (wrap around)", 0,0,0,0);
2231 UVMHIST_LOG(maphist
, "<- failed (notfound)", 0,0,0,0);
2237 * U N M A P - m a i n h e l p e r f u n c t i o n s
2241 * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop")
2243 * => caller must check alignment and size
2244 * => map must be locked by caller
2245 * => we return a list of map entries that we've remove from the map
2250 uvm_unmap_remove(struct vm_map
*map
, vaddr_t start
, vaddr_t end
,
2251 struct vm_map_entry
**entry_list
/* OUT */,
2252 struct uvm_mapent_reservation
*umr
, int flags
)
2254 struct vm_map_entry
*entry
, *first_entry
, *next
;
2256 UVMHIST_FUNC("uvm_unmap_remove"); UVMHIST_CALLED(maphist
);
2258 UVMHIST_LOG(maphist
,"(map=0x%x, start=0x%x, end=0x%x)",
2259 map
, start
, end
, 0);
2260 VM_MAP_RANGE_CHECK(map
, start
, end
);
2262 uvm_map_check(map
, "unmap_remove entry");
2268 if (uvm_map_lookup_entry(map
, start
, &first_entry
) == true) {
2269 /* clip and go... */
2270 entry
= first_entry
;
2271 UVM_MAP_CLIP_START(map
, entry
, start
, umr
);
2272 /* critical! prevents stale hint */
2273 SAVE_HINT(map
, entry
, entry
->prev
);
2275 entry
= first_entry
->next
;
2279 * Save the free space hint
2282 if (map
->first_free
!= &map
->header
&& map
->first_free
->start
>= start
)
2283 map
->first_free
= entry
->prev
;
2286 * note: we now re-use first_entry for a different task. we remove
2287 * a number of map entries from the map and save them in a linked
2288 * list headed by "first_entry". once we remove them from the map
2289 * the caller should unlock the map and drop the references to the
2290 * backing objects [c.f. uvm_unmap_detach]. the object is to
2291 * separate unmapping from reference dropping. why?
2292 * [1] the map has to be locked for unmapping
2293 * [2] the map need not be locked for reference dropping
2294 * [3] dropping references may trigger pager I/O, and if we hit
2295 * a pager that does synchronous I/O we may have to wait for it.
2296 * [4] we would like all waiting for I/O to occur with maps unlocked
2297 * so that we don't block other threads.
2304 * break up the area into map entry sized regions and unmap. note
2305 * that all mappings have to be removed before we can even consider
2306 * dropping references to amaps or VM objects (otherwise we could end
2307 * up with a mapping to a page on the free list which would be very bad)
2310 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2311 KASSERT((entry
->flags
& UVM_MAP_FIRST
) == 0);
2313 UVM_MAP_CLIP_END(map
, entry
, end
, umr
);
2315 len
= entry
->end
- entry
->start
;
2318 * unwire before removing addresses from the pmap; otherwise
2319 * unwiring will put the entries back into the pmap (XXX).
2322 if (VM_MAPENT_ISWIRED(entry
)) {
2323 uvm_map_entry_unwire(map
, entry
);
2325 if (flags
& UVM_FLAG_VAONLY
) {
2329 } else if ((map
->flags
& VM_MAP_PAGEABLE
) == 0) {
2332 * if the map is non-pageable, any pages mapped there
2333 * must be wired and entered with pmap_kenter_pa(),
2334 * and we should free any such pages immediately.
2335 * this is mostly used for kmem_map and mb_map.
2338 if ((entry
->flags
& UVM_MAP_KMAPENT
) == 0) {
2339 uvm_km_pgremove_intrsafe(map
, entry
->start
,
2341 pmap_kremove(entry
->start
, len
);
2343 } else if (UVM_ET_ISOBJ(entry
) &&
2344 UVM_OBJ_IS_KERN_OBJECT(entry
->object
.uvm_obj
)) {
2345 KASSERT(vm_map_pmap(map
) == pmap_kernel());
2348 * note: kernel object mappings are currently used in
2350 * [1] "normal" mappings of pages in the kernel object
2351 * [2] uvm_km_valloc'd allocations in which we
2352 * pmap_enter in some non-kernel-object page
2355 * for case [1], we need to remove the mapping from
2356 * the pmap and then remove the page from the kernel
2357 * object (because, once pages in a kernel object are
2358 * unmapped they are no longer needed, unlike, say,
2359 * a vnode where you might want the data to persist
2360 * until flushed out of a queue).
2362 * for case [2], we need to remove the mapping from
2363 * the pmap. there shouldn't be any pages at the
2364 * specified offset in the kernel object [but it
2365 * doesn't hurt to call uvm_km_pgremove just to be
2368 * uvm_km_pgremove currently does the following:
2369 * for pages in the kernel object in range:
2370 * - drops the swap slot
2371 * - uvm_pagefree the page
2375 * remove mappings from pmap and drop the pages
2376 * from the object. offsets are always relative
2377 * to vm_map_min(kernel_map).
2380 pmap_remove(pmap_kernel(), entry
->start
,
2381 entry
->start
+ len
);
2382 uvm_km_pgremove(entry
->start
, entry
->end
);
2385 * null out kernel_object reference, we've just
2389 entry
->etype
&= ~UVM_ET_OBJ
;
2390 entry
->object
.uvm_obj
= NULL
;
2391 } else if (UVM_ET_ISOBJ(entry
) || entry
->aref
.ar_amap
) {
2394 * remove mappings the standard way.
2397 pmap_remove(map
->pmap
, entry
->start
, entry
->end
);
2401 if ((entry
->flags
& UVM_MAP_KMAPENT
) == 0) {
2404 * check if there's remaining mapping,
2405 * which is a bug in caller.
2409 for (va
= entry
->start
; va
< entry
->end
;
2411 if (pmap_extract(vm_map_pmap(map
), va
, NULL
)) {
2412 panic("uvm_unmap_remove: has mapping");
2416 if (VM_MAP_IS_KERNEL(map
)) {
2417 uvm_km_check_empty(map
, entry
->start
,
2421 #endif /* defined(DEBUG) */
2424 * remove entry from map and put it on our list of entries
2425 * that we've nuked. then go to next entry.
2428 UVMHIST_LOG(maphist
, " removed map entry 0x%x", entry
, 0, 0,0);
2430 /* critical! prevents stale hint */
2431 SAVE_HINT(map
, entry
, entry
->prev
);
2433 uvm_map_entry_unlink(map
, entry
);
2434 KASSERT(map
->size
>= len
);
2437 entry
->next
= first_entry
;
2438 first_entry
= entry
;
2441 if ((map
->flags
& VM_MAP_DYING
) == 0) {
2442 pmap_update(vm_map_pmap(map
));
2445 uvm_map_check(map
, "unmap_remove leave");
2448 * now we've cleaned up the map and are ready for the caller to drop
2449 * references to the mapped objects.
2452 *entry_list
= first_entry
;
2453 UVMHIST_LOG(maphist
,"<- done!", 0, 0, 0, 0);
2455 if (map
->flags
& VM_MAP_WANTVA
) {
2456 mutex_enter(&map
->misc_lock
);
2457 map
->flags
&= ~VM_MAP_WANTVA
;
2458 cv_broadcast(&map
->cv
);
2459 mutex_exit(&map
->misc_lock
);
2464 * uvm_unmap_detach: drop references in a chain of map entries
2466 * => we will free the map entries as we traverse the list.
2470 uvm_unmap_detach(struct vm_map_entry
*first_entry
, int flags
)
2472 struct vm_map_entry
*next_entry
;
2473 UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist
);
2475 while (first_entry
) {
2476 KASSERT(!VM_MAPENT_ISWIRED(first_entry
));
2477 UVMHIST_LOG(maphist
,
2478 " detach 0x%x: amap=0x%x, obj=0x%x, submap?=%d",
2479 first_entry
, first_entry
->aref
.ar_amap
,
2480 first_entry
->object
.uvm_obj
,
2481 UVM_ET_ISSUBMAP(first_entry
));
2484 * drop reference to amap, if we've got one
2487 if (first_entry
->aref
.ar_amap
)
2488 uvm_map_unreference_amap(first_entry
, flags
);
2491 * drop reference to our backing object, if we've got one
2494 KASSERT(!UVM_ET_ISSUBMAP(first_entry
));
2495 if (UVM_ET_ISOBJ(first_entry
) &&
2496 first_entry
->object
.uvm_obj
->pgops
->pgo_detach
) {
2497 (*first_entry
->object
.uvm_obj
->pgops
->pgo_detach
)
2498 (first_entry
->object
.uvm_obj
);
2500 next_entry
= first_entry
->next
;
2501 uvm_mapent_free(first_entry
);
2502 first_entry
= next_entry
;
2504 UVMHIST_LOG(maphist
, "<- done", 0,0,0,0);
2508 * E X T R A C T I O N F U N C T I O N S
2512 * uvm_map_reserve: reserve space in a vm_map for future use.
2514 * => we reserve space in a map by putting a dummy map entry in the
2515 * map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE)
2516 * => map should be unlocked (we will write lock it)
2517 * => we return true if we were able to reserve space
2518 * => XXXCDC: should be inline?
2522 uvm_map_reserve(struct vm_map
*map
, vsize_t size
,
2523 vaddr_t offset
/* hint for pmap_prefer */,
2524 vsize_t align
/* alignment */,
2525 vaddr_t
*raddr
/* IN:hint, OUT: reserved VA */,
2526 uvm_flag_t flags
/* UVM_FLAG_FIXED or 0 */)
2528 UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist
);
2530 UVMHIST_LOG(maphist
, "(map=0x%x, size=0x%x, offset=0x%x,addr=0x%x)",
2531 map
,size
,offset
,raddr
);
2533 size
= round_page(size
);
2536 * reserve some virtual space.
2539 if (uvm_map(map
, raddr
, size
, NULL
, offset
, align
,
2540 UVM_MAPFLAG(UVM_PROT_NONE
, UVM_PROT_NONE
, UVM_INH_NONE
,
2541 UVM_ADV_RANDOM
, UVM_FLAG_NOMERGE
|flags
)) != 0) {
2542 UVMHIST_LOG(maphist
, "<- done (no VM)", 0,0,0,0);
2546 UVMHIST_LOG(maphist
, "<- done (*raddr=0x%x)", *raddr
,0,0,0);
2551 * uvm_map_replace: replace a reserved (blank) area of memory with
2554 * => caller must WRITE-LOCK the map
2555 * => we return true if replacement was a success
2556 * => we expect the newents chain to have nnewents entrys on it and
2557 * we expect newents->prev to point to the last entry on the list
2558 * => note newents is allowed to be NULL
2562 uvm_map_replace(struct vm_map
*map
, vaddr_t start
, vaddr_t end
,
2563 struct vm_map_entry
*newents
, int nnewents
, vsize_t nsize
,
2564 struct vm_map_entry
**oldentryp
)
2566 struct vm_map_entry
*oldent
, *last
;
2568 uvm_map_check(map
, "map_replace entry");
2571 * first find the blank map entry at the specified address
2574 if (!uvm_map_lookup_entry(map
, start
, &oldent
)) {
2579 * check to make sure we have a proper blank entry
2582 if (end
< oldent
->end
&& !VM_MAP_USE_KMAPENT(map
)) {
2583 UVM_MAP_CLIP_END(map
, oldent
, end
, NULL
);
2585 if (oldent
->start
!= start
|| oldent
->end
!= end
||
2586 oldent
->object
.uvm_obj
!= NULL
|| oldent
->aref
.ar_amap
!= NULL
) {
2593 * sanity check the newents chain
2597 struct vm_map_entry
*tmpent
= newents
;
2600 vaddr_t cur
= start
;
2604 sz
+= tmpent
->end
- tmpent
->start
;
2605 if (tmpent
->start
< cur
)
2606 panic("uvm_map_replace1");
2607 if (tmpent
->start
>= tmpent
->end
|| tmpent
->end
> end
) {
2608 panic("uvm_map_replace2: "
2609 "tmpent->start=0x%"PRIxVADDR
2610 ", tmpent->end=0x%"PRIxVADDR
2611 ", end=0x%"PRIxVADDR
,
2612 tmpent
->start
, tmpent
->end
, end
);
2616 if (tmpent
->next
->prev
!= tmpent
)
2617 panic("uvm_map_replace3");
2619 if (newents
->prev
!= tmpent
)
2620 panic("uvm_map_replace4");
2622 tmpent
= tmpent
->next
;
2624 if (nent
!= nnewents
)
2625 panic("uvm_map_replace5");
2627 panic("uvm_map_replace6");
2632 * map entry is a valid blank! replace it. (this does all the
2633 * work of map entry link/unlink...).
2637 last
= newents
->prev
;
2639 /* critical: flush stale hints out of map */
2640 SAVE_HINT(map
, map
->hint
, newents
);
2641 if (map
->first_free
== oldent
)
2642 map
->first_free
= last
;
2644 last
->next
= oldent
->next
;
2645 last
->next
->prev
= last
;
2648 uvm_rb_remove(map
, oldent
);
2650 newents
->prev
= oldent
->prev
;
2651 newents
->prev
->next
= newents
;
2652 map
->nentries
= map
->nentries
+ (nnewents
- 1);
2654 /* Fixup the RB tree */
2657 struct vm_map_entry
*tmp
;
2660 for (i
= 0; i
< nnewents
&& tmp
; i
++) {
2661 uvm_rb_insert(map
, tmp
);
2666 /* NULL list of new entries: just remove the old one */
2667 clear_hints(map
, oldent
);
2668 uvm_map_entry_unlink(map
, oldent
);
2670 map
->size
-= end
- start
- nsize
;
2672 uvm_map_check(map
, "map_replace leave");
2675 * now we can free the old blank entry and return.
2678 *oldentryp
= oldent
;
2683 * uvm_map_extract: extract a mapping from a map and put it somewhere
2684 * (maybe removing the old mapping)
2686 * => maps should be unlocked (we will write lock them)
2687 * => returns 0 on success, error code otherwise
2688 * => start must be page aligned
2689 * => len must be page sized
2691 * UVM_EXTRACT_REMOVE: remove mappings from srcmap
2692 * UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only)
2693 * UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs
2694 * UVM_EXTRACT_FIXPROT: set prot to maxprot as we go
2695 * >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<<
2696 * >>>NOTE: QREF's must be unmapped via the QREF path, thus should only
2697 * be used from within the kernel in a kernel level map <<<
2701 uvm_map_extract(struct vm_map
*srcmap
, vaddr_t start
, vsize_t len
,
2702 struct vm_map
*dstmap
, vaddr_t
*dstaddrp
, int flags
)
2704 vaddr_t dstaddr
, end
, newend
, oldoffset
, fudge
, orig_fudge
;
2705 struct vm_map_entry
*chain
, *endchain
, *entry
, *orig_entry
, *newentry
,
2706 *deadentry
, *oldentry
;
2707 struct vm_map_entry
*resentry
= NULL
; /* a dummy reservation entry */
2709 int nchain
, error
, copy_ok
;
2711 UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist
);
2713 UVMHIST_LOG(maphist
,"(srcmap=0x%x,start=0x%x, len=0x%x", srcmap
, start
,
2715 UVMHIST_LOG(maphist
," ...,dstmap=0x%x, flags=0x%x)", dstmap
,flags
,0,0);
2718 * step 0: sanity check: start must be on a page boundary, length
2719 * must be page sized. can't ask for CONTIG/QREF if you asked for
2723 KASSERT((start
& PAGE_MASK
) == 0 && (len
& PAGE_MASK
) == 0);
2724 KASSERT((flags
& UVM_EXTRACT_REMOVE
) == 0 ||
2725 (flags
& (UVM_EXTRACT_CONTIG
|UVM_EXTRACT_QREF
)) == 0);
2728 * step 1: reserve space in the target map for the extracted area
2731 if ((flags
& UVM_EXTRACT_RESERVED
) == 0) {
2732 dstaddr
= vm_map_min(dstmap
);
2733 if (!uvm_map_reserve(dstmap
, len
, start
, 0, &dstaddr
, 0))
2735 *dstaddrp
= dstaddr
; /* pass address back to caller */
2736 UVMHIST_LOG(maphist
, " dstaddr=0x%x", dstaddr
,0,0,0);
2738 dstaddr
= *dstaddrp
;
2742 * step 2: setup for the extraction process loop by init'ing the
2743 * map entry chain, locking src map, and looking up the first useful
2748 newend
= dstaddr
+ len
;
2749 chain
= endchain
= NULL
;
2752 vm_map_lock(srcmap
);
2754 if (uvm_map_lookup_entry(srcmap
, start
, &entry
)) {
2756 /* "start" is within an entry */
2757 if (flags
& UVM_EXTRACT_QREF
) {
2760 * for quick references we don't clip the entry, so
2761 * the entry may map space "before" the starting
2762 * virtual address... this is the "fudge" factor
2763 * (which can be non-zero only the first time
2764 * through the "while" loop in step 3).
2767 fudge
= start
- entry
->start
;
2771 * normal reference: we clip the map to fit (thus
2775 UVM_MAP_CLIP_START(srcmap
, entry
, start
, NULL
);
2776 SAVE_HINT(srcmap
, srcmap
->hint
, entry
->prev
);
2781 /* "start" is not within an entry ... skip to next entry */
2782 if (flags
& UVM_EXTRACT_CONTIG
) {
2784 goto bad
; /* definite hole here ... */
2787 entry
= entry
->next
;
2791 /* save values from srcmap for step 6 */
2796 * step 3: now start looping through the map entries, extracting
2800 while (entry
->start
< end
&& entry
!= &srcmap
->header
) {
2802 /* if we are not doing a quick reference, clip it */
2803 if ((flags
& UVM_EXTRACT_QREF
) == 0)
2804 UVM_MAP_CLIP_END(srcmap
, entry
, end
, NULL
);
2806 /* clear needs_copy (allow chunking) */
2807 if (UVM_ET_ISNEEDSCOPY(entry
)) {
2808 amap_copy(srcmap
, entry
,
2809 AMAP_COPY_NOWAIT
|AMAP_COPY_NOMERGE
, start
, end
);
2810 if (UVM_ET_ISNEEDSCOPY(entry
)) { /* failed? */
2815 /* amap_copy could clip (during chunk)! update fudge */
2817 fudge
= start
- entry
->start
;
2822 /* calculate the offset of this from "start" */
2823 oldoffset
= (entry
->start
+ fudge
) - start
;
2825 /* allocate a new map entry */
2826 newentry
= uvm_mapent_alloc(dstmap
, 0);
2827 if (newentry
== NULL
) {
2832 /* set up new map entry */
2833 newentry
->next
= NULL
;
2834 newentry
->prev
= endchain
;
2835 newentry
->start
= dstaddr
+ oldoffset
;
2837 newentry
->start
+ (entry
->end
- (entry
->start
+ fudge
));
2838 if (newentry
->end
> newend
|| newentry
->end
< newentry
->start
)
2839 newentry
->end
= newend
;
2840 newentry
->object
.uvm_obj
= entry
->object
.uvm_obj
;
2841 if (newentry
->object
.uvm_obj
) {
2842 if (newentry
->object
.uvm_obj
->pgops
->pgo_reference
)
2843 newentry
->object
.uvm_obj
->pgops
->
2844 pgo_reference(newentry
->object
.uvm_obj
);
2845 newentry
->offset
= entry
->offset
+ fudge
;
2847 newentry
->offset
= 0;
2849 newentry
->etype
= entry
->etype
;
2850 newentry
->protection
= (flags
& UVM_EXTRACT_FIXPROT
) ?
2851 entry
->max_protection
: entry
->protection
;
2852 newentry
->max_protection
= entry
->max_protection
;
2853 newentry
->inheritance
= entry
->inheritance
;
2854 newentry
->wired_count
= 0;
2855 newentry
->aref
.ar_amap
= entry
->aref
.ar_amap
;
2856 if (newentry
->aref
.ar_amap
) {
2857 newentry
->aref
.ar_pageoff
=
2858 entry
->aref
.ar_pageoff
+ (fudge
>> PAGE_SHIFT
);
2859 uvm_map_reference_amap(newentry
, AMAP_SHARED
|
2860 ((flags
& UVM_EXTRACT_QREF
) ? AMAP_REFALL
: 0));
2862 newentry
->aref
.ar_pageoff
= 0;
2864 newentry
->advice
= entry
->advice
;
2865 if ((flags
& UVM_EXTRACT_QREF
) != 0) {
2866 newentry
->flags
|= UVM_MAP_NOMERGE
;
2869 /* now link it on the chain */
2871 nsize
+= newentry
->end
- newentry
->start
;
2872 if (endchain
== NULL
) {
2873 chain
= endchain
= newentry
;
2875 endchain
->next
= newentry
;
2876 endchain
= newentry
;
2879 /* end of 'while' loop! */
2880 if ((flags
& UVM_EXTRACT_CONTIG
) && entry
->end
< end
&&
2881 (entry
->next
== &srcmap
->header
||
2882 entry
->next
->start
!= entry
->end
)) {
2886 entry
= entry
->next
;
2891 * step 4: close off chain (in format expected by uvm_map_replace)
2895 chain
->prev
= endchain
;
2898 * step 5: attempt to lock the dest map so we can pmap_copy.
2899 * note usage of copy_ok:
2900 * 1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5)
2901 * 0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7
2904 if (srcmap
== dstmap
|| vm_map_lock_try(dstmap
) == true) {
2906 if (!uvm_map_replace(dstmap
, dstaddr
, dstaddr
+len
, chain
,
2907 nchain
, nsize
, &resentry
)) {
2908 if (srcmap
!= dstmap
)
2909 vm_map_unlock(dstmap
);
2915 /* replace defered until step 7 */
2919 * step 6: traverse the srcmap a second time to do the following:
2920 * - if we got a lock on the dstmap do pmap_copy
2921 * - if UVM_EXTRACT_REMOVE remove the entries
2922 * we make use of orig_entry and orig_fudge (saved in step 2)
2925 if (copy_ok
|| (flags
& UVM_EXTRACT_REMOVE
)) {
2927 /* purge possible stale hints from srcmap */
2928 if (flags
& UVM_EXTRACT_REMOVE
) {
2929 SAVE_HINT(srcmap
, srcmap
->hint
, orig_entry
->prev
);
2930 if (srcmap
->first_free
!= &srcmap
->header
&&
2931 srcmap
->first_free
->start
>= start
)
2932 srcmap
->first_free
= orig_entry
->prev
;
2937 deadentry
= NULL
; /* for UVM_EXTRACT_REMOVE */
2939 while (entry
->start
< end
&& entry
!= &srcmap
->header
) {
2941 oldoffset
= (entry
->start
+ fudge
) - start
;
2942 elen
= MIN(end
, entry
->end
) -
2943 (entry
->start
+ fudge
);
2944 pmap_copy(dstmap
->pmap
, srcmap
->pmap
,
2945 dstaddr
+ oldoffset
, elen
,
2946 entry
->start
+ fudge
);
2949 /* we advance "entry" in the following if statement */
2950 if (flags
& UVM_EXTRACT_REMOVE
) {
2951 pmap_remove(srcmap
->pmap
, entry
->start
,
2953 oldentry
= entry
; /* save entry */
2954 entry
= entry
->next
; /* advance */
2955 uvm_map_entry_unlink(srcmap
, oldentry
);
2956 /* add to dead list */
2957 oldentry
->next
= deadentry
;
2958 deadentry
= oldentry
;
2960 entry
= entry
->next
; /* advance */
2963 /* end of 'while' loop */
2966 pmap_update(srcmap
->pmap
);
2969 * unlock dstmap. we will dispose of deadentry in
2973 if (copy_ok
&& srcmap
!= dstmap
)
2974 vm_map_unlock(dstmap
);
2981 * step 7: we are done with the source map, unlock. if copy_ok
2982 * is 0 then we have not replaced the dummy mapping in dstmap yet
2983 * and we need to do so now.
2986 vm_map_unlock(srcmap
);
2987 if ((flags
& UVM_EXTRACT_REMOVE
) && deadentry
)
2988 uvm_unmap_detach(deadentry
, 0); /* dispose of old entries */
2990 /* now do the replacement if we didn't do it in step 5 */
2992 vm_map_lock(dstmap
);
2993 error
= uvm_map_replace(dstmap
, dstaddr
, dstaddr
+len
, chain
,
2994 nchain
, nsize
, &resentry
);
2995 vm_map_unlock(dstmap
);
2997 if (error
== false) {
3003 if (resentry
!= NULL
)
3004 uvm_mapent_free(resentry
);
3009 * bad: failure recovery
3012 vm_map_unlock(srcmap
);
3013 bad2
: /* src already unlocked */
3015 uvm_unmap_detach(chain
,
3016 (flags
& UVM_EXTRACT_QREF
) ? AMAP_REFALL
: 0);
3018 if (resentry
!= NULL
)
3019 uvm_mapent_free(resentry
);
3021 if ((flags
& UVM_EXTRACT_RESERVED
) == 0) {
3022 uvm_unmap(dstmap
, dstaddr
, dstaddr
+len
); /* ??? */
3027 /* end of extraction functions */
3030 * uvm_map_submap: punch down part of a map into a submap
3032 * => only the kernel_map is allowed to be submapped
3033 * => the purpose of submapping is to break up the locking granularity
3035 * => the range specified must have been mapped previously with a uvm_map()
3036 * call [with uobj==NULL] to create a blank map entry in the main map.
3037 * [And it had better still be blank!]
3038 * => maps which contain submaps should never be copied or forked.
3039 * => to remove a submap, use uvm_unmap() on the main map
3040 * and then uvm_map_deallocate() the submap.
3041 * => main map must be unlocked.
3042 * => submap must have been init'd and have a zero reference count.
3043 * [need not be locked as we don't actually reference it]
3047 uvm_map_submap(struct vm_map
*map
, vaddr_t start
, vaddr_t end
,
3048 struct vm_map
*submap
)
3050 struct vm_map_entry
*entry
;
3051 struct uvm_mapent_reservation umr
;
3054 uvm_mapent_reserve(map
, &umr
, 2, 0);
3057 VM_MAP_RANGE_CHECK(map
, start
, end
);
3059 if (uvm_map_lookup_entry(map
, start
, &entry
)) {
3060 UVM_MAP_CLIP_START(map
, entry
, start
, &umr
);
3061 UVM_MAP_CLIP_END(map
, entry
, end
, &umr
); /* to be safe */
3066 if (entry
!= NULL
&&
3067 entry
->start
== start
&& entry
->end
== end
&&
3068 entry
->object
.uvm_obj
== NULL
&& entry
->aref
.ar_amap
== NULL
&&
3069 !UVM_ET_ISCOPYONWRITE(entry
) && !UVM_ET_ISNEEDSCOPY(entry
)) {
3070 entry
->etype
|= UVM_ET_SUBMAP
;
3071 entry
->object
.sub_map
= submap
;
3073 uvm_map_reference(submap
);
3080 uvm_mapent_unreserve(map
, &umr
);
3086 * uvm_map_setup_kernel: init in-kernel map
3088 * => map must not be in service yet.
3092 uvm_map_setup_kernel(struct vm_map_kernel
*map
,
3093 vaddr_t vmin
, vaddr_t vmax
, int flags
)
3096 uvm_map_setup(&map
->vmk_map
, vmin
, vmax
, flags
);
3097 callback_head_init(&map
->vmk_reclaim_callback
, IPL_VM
);
3098 LIST_INIT(&map
->vmk_kentry_free
);
3099 map
->vmk_merged_entries
= NULL
;
3104 * uvm_map_protect: change map protection
3106 * => set_max means set max_protection.
3107 * => map must be unlocked.
3110 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
3111 ~VM_PROT_WRITE : VM_PROT_ALL)
3114 uvm_map_protect(struct vm_map
*map
, vaddr_t start
, vaddr_t end
,
3115 vm_prot_t new_prot
, bool set_max
)
3117 struct vm_map_entry
*current
, *entry
;
3119 UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist
);
3120 UVMHIST_LOG(maphist
,"(map=0x%x,start=0x%x,end=0x%x,new_prot=0x%x)",
3121 map
, start
, end
, new_prot
);
3124 VM_MAP_RANGE_CHECK(map
, start
, end
);
3125 if (uvm_map_lookup_entry(map
, start
, &entry
)) {
3126 UVM_MAP_CLIP_START(map
, entry
, start
, NULL
);
3128 entry
= entry
->next
;
3132 * make a first pass to check for protection violations.
3136 while ((current
!= &map
->header
) && (current
->start
< end
)) {
3137 if (UVM_ET_ISSUBMAP(current
)) {
3141 if ((new_prot
& current
->max_protection
) != new_prot
) {
3146 * Don't allow VM_PROT_EXECUTE to be set on entries that
3147 * point to vnodes that are associated with a NOEXEC file
3150 if (UVM_ET_ISOBJ(current
) &&
3151 UVM_OBJ_IS_VNODE(current
->object
.uvm_obj
)) {
3153 (struct vnode
*) current
->object
.uvm_obj
;
3155 if ((new_prot
& VM_PROT_EXECUTE
) != 0 &&
3156 (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) != 0) {
3162 current
= current
->next
;
3165 /* go back and fix up protections (no need to clip this time). */
3168 while ((current
!= &map
->header
) && (current
->start
< end
)) {
3171 UVM_MAP_CLIP_END(map
, current
, end
, NULL
);
3172 old_prot
= current
->protection
;
3174 current
->protection
=
3175 (current
->max_protection
= new_prot
) & old_prot
;
3177 current
->protection
= new_prot
;
3180 * update physical map if necessary. worry about copy-on-write
3181 * here -- CHECK THIS XXX
3184 if (current
->protection
!= old_prot
) {
3186 pmap_protect(map
->pmap
, current
->start
, current
->end
,
3187 current
->protection
& MASK(entry
));
3190 * If this entry points at a vnode, and the
3191 * protection includes VM_PROT_EXECUTE, mark
3192 * the vnode as VEXECMAP.
3194 if (UVM_ET_ISOBJ(current
)) {
3195 struct uvm_object
*uobj
=
3196 current
->object
.uvm_obj
;
3198 if (UVM_OBJ_IS_VNODE(uobj
) &&
3199 (current
->protection
& VM_PROT_EXECUTE
)) {
3200 vn_markexec((struct vnode
*) uobj
);
3206 * If the map is configured to lock any future mappings,
3207 * wire this entry now if the old protection was VM_PROT_NONE
3208 * and the new protection is not VM_PROT_NONE.
3211 if ((map
->flags
& VM_MAP_WIREFUTURE
) != 0 &&
3212 VM_MAPENT_ISWIRED(entry
) == 0 &&
3213 old_prot
== VM_PROT_NONE
&&
3214 new_prot
!= VM_PROT_NONE
) {
3215 if (uvm_map_pageable(map
, entry
->start
,
3217 UVM_LK_ENTER
|UVM_LK_EXIT
) != 0) {
3220 * If locking the entry fails, remember the
3221 * error if it's the first one. Note we
3222 * still continue setting the protection in
3223 * the map, but will return the error
3224 * condition regardless.
3226 * XXX Ignore what the actual error is,
3227 * XXX just call it a resource shortage
3228 * XXX so that it doesn't get confused
3229 * XXX what uvm_map_protect() itself would
3230 * XXX normally return.
3236 current
= current
->next
;
3238 pmap_update(map
->pmap
);
3243 UVMHIST_LOG(maphist
, "<- done, error=%d",error
,0,0,0);
3250 * uvm_map_inherit: set inheritance code for range of addrs in map.
3252 * => map must be unlocked
3253 * => note that the inherit code is used during a "fork". see fork
3258 uvm_map_inherit(struct vm_map
*map
, vaddr_t start
, vaddr_t end
,
3259 vm_inherit_t new_inheritance
)
3261 struct vm_map_entry
*entry
, *temp_entry
;
3262 UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist
);
3263 UVMHIST_LOG(maphist
,"(map=0x%x,start=0x%x,end=0x%x,new_inh=0x%x)",
3264 map
, start
, end
, new_inheritance
);
3266 switch (new_inheritance
) {
3267 case MAP_INHERIT_NONE
:
3268 case MAP_INHERIT_COPY
:
3269 case MAP_INHERIT_SHARE
:
3272 UVMHIST_LOG(maphist
,"<- done (INVALID ARG)",0,0,0,0);
3277 VM_MAP_RANGE_CHECK(map
, start
, end
);
3278 if (uvm_map_lookup_entry(map
, start
, &temp_entry
)) {
3280 UVM_MAP_CLIP_START(map
, entry
, start
, NULL
);
3282 entry
= temp_entry
->next
;
3284 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
3285 UVM_MAP_CLIP_END(map
, entry
, end
, NULL
);
3286 entry
->inheritance
= new_inheritance
;
3287 entry
= entry
->next
;
3290 UVMHIST_LOG(maphist
,"<- done (OK)",0,0,0,0);
3295 * uvm_map_advice: set advice code for range of addrs in map.
3297 * => map must be unlocked
3301 uvm_map_advice(struct vm_map
*map
, vaddr_t start
, vaddr_t end
, int new_advice
)
3303 struct vm_map_entry
*entry
, *temp_entry
;
3304 UVMHIST_FUNC("uvm_map_advice"); UVMHIST_CALLED(maphist
);
3305 UVMHIST_LOG(maphist
,"(map=0x%x,start=0x%x,end=0x%x,new_adv=0x%x)",
3306 map
, start
, end
, new_advice
);
3309 VM_MAP_RANGE_CHECK(map
, start
, end
);
3310 if (uvm_map_lookup_entry(map
, start
, &temp_entry
)) {
3312 UVM_MAP_CLIP_START(map
, entry
, start
, NULL
);
3314 entry
= temp_entry
->next
;
3318 * XXXJRT: disallow holes?
3321 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
3322 UVM_MAP_CLIP_END(map
, entry
, end
, NULL
);
3324 switch (new_advice
) {
3327 case MADV_SEQUENTIAL
:
3328 /* nothing special here */
3333 UVMHIST_LOG(maphist
,"<- done (INVALID ARG)",0,0,0,0);
3336 entry
->advice
= new_advice
;
3337 entry
= entry
->next
;
3341 UVMHIST_LOG(maphist
,"<- done (OK)",0,0,0,0);
3346 * uvm_map_willneed: apply MADV_WILLNEED
3350 uvm_map_willneed(struct vm_map
*map
, vaddr_t start
, vaddr_t end
)
3352 struct vm_map_entry
*entry
;
3353 UVMHIST_FUNC("uvm_map_willneed"); UVMHIST_CALLED(maphist
);
3354 UVMHIST_LOG(maphist
,"(map=0x%lx,start=0x%lx,end=0x%lx)",
3355 map
, start
, end
, 0);
3357 vm_map_lock_read(map
);
3358 VM_MAP_RANGE_CHECK(map
, start
, end
);
3359 if (!uvm_map_lookup_entry(map
, start
, &entry
)) {
3360 entry
= entry
->next
;
3362 while (entry
->start
< end
) {
3363 struct vm_amap
* const amap
= entry
->aref
.ar_amap
;
3364 struct uvm_object
* const uobj
= entry
->object
.uvm_obj
;
3366 KASSERT(entry
!= &map
->header
);
3367 KASSERT(start
< entry
->end
);
3370 * Should invent a "weak" mode for uvm_fault()
3371 * which would only do the PGO_LOCKED pgo_get().
3373 * for now, we handle only the easy but common case.
3375 if (UVM_ET_ISOBJ(entry
) && amap
== NULL
&& uobj
!= NULL
) {
3379 offset
= entry
->offset
;
3380 if (start
< entry
->start
) {
3381 offset
+= entry
->start
- start
;
3383 size
= entry
->offset
+ (entry
->end
- entry
->start
);
3384 if (entry
->end
< end
) {
3385 size
-= end
- entry
->end
;
3387 uvm_readahead(uobj
, offset
, size
);
3389 entry
= entry
->next
;
3391 vm_map_unlock_read(map
);
3392 UVMHIST_LOG(maphist
,"<- done (OK)",0,0,0,0);
3397 * uvm_map_pageable: sets the pageability of a range in a map.
3399 * => wires map entries. should not be used for transient page locking.
3400 * for that, use uvm_fault_wire()/uvm_fault_unwire() (see uvm_vslock()).
3401 * => regions specified as not pageable require lock-down (wired) memory
3403 * => map must never be read-locked
3404 * => if islocked is true, map is already write-locked
3405 * => we always unlock the map, since we must downgrade to a read-lock
3406 * to call uvm_fault_wire()
3407 * => XXXCDC: check this and try and clean it up.
3411 uvm_map_pageable(struct vm_map
*map
, vaddr_t start
, vaddr_t end
,
3412 bool new_pageable
, int lockflags
)
3414 struct vm_map_entry
*entry
, *start_entry
, *failed_entry
;
3417 u_int timestamp_save
;
3419 UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist
);
3420 UVMHIST_LOG(maphist
,"(map=0x%x,start=0x%x,end=0x%x,new_pageable=0x%x)",
3421 map
, start
, end
, new_pageable
);
3422 KASSERT(map
->flags
& VM_MAP_PAGEABLE
);
3424 if ((lockflags
& UVM_LK_ENTER
) == 0)
3426 VM_MAP_RANGE_CHECK(map
, start
, end
);
3429 * only one pageability change may take place at one time, since
3430 * uvm_fault_wire assumes it will be called only once for each
3431 * wiring/unwiring. therefore, we have to make sure we're actually
3432 * changing the pageability for the entire region. we do so before
3433 * making any changes.
3436 if (uvm_map_lookup_entry(map
, start
, &start_entry
) == false) {
3437 if ((lockflags
& UVM_LK_EXIT
) == 0)
3440 UVMHIST_LOG(maphist
,"<- done (fault)",0,0,0,0);
3443 entry
= start_entry
;
3446 * handle wiring and unwiring separately.
3449 if (new_pageable
) { /* unwire */
3450 UVM_MAP_CLIP_START(map
, entry
, start
, NULL
);
3453 * unwiring. first ensure that the range to be unwired is
3454 * really wired down and that there are no holes.
3457 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
3458 if (entry
->wired_count
== 0 ||
3459 (entry
->end
< end
&&
3460 (entry
->next
== &map
->header
||
3461 entry
->next
->start
> entry
->end
))) {
3462 if ((lockflags
& UVM_LK_EXIT
) == 0)
3464 UVMHIST_LOG(maphist
, "<- done (INVAL)",0,0,0,0);
3467 entry
= entry
->next
;
3471 * POSIX 1003.1b - a single munlock call unlocks a region,
3472 * regardless of the number of mlock calls made on that
3476 entry
= start_entry
;
3477 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
3478 UVM_MAP_CLIP_END(map
, entry
, end
, NULL
);
3479 if (VM_MAPENT_ISWIRED(entry
))
3480 uvm_map_entry_unwire(map
, entry
);
3481 entry
= entry
->next
;
3483 if ((lockflags
& UVM_LK_EXIT
) == 0)
3485 UVMHIST_LOG(maphist
,"<- done (OK UNWIRE)",0,0,0,0);
3490 * wire case: in two passes [XXXCDC: ugly block of code here]
3492 * 1: holding the write lock, we create any anonymous maps that need
3493 * to be created. then we clip each map entry to the region to
3494 * be wired and increment its wiring count.
3496 * 2: we downgrade to a read lock, and call uvm_fault_wire to fault
3497 * in the pages for any newly wired area (wired_count == 1).
3499 * downgrading to a read lock for uvm_fault_wire avoids a possible
3500 * deadlock with another thread that may have faulted on one of
3501 * the pages to be wired (it would mark the page busy, blocking
3502 * us, then in turn block on the map lock that we hold). because
3503 * of problems in the recursive lock package, we cannot upgrade
3504 * to a write lock in vm_map_lookup. thus, any actions that
3505 * require the write lock must be done beforehand. because we
3506 * keep the read lock on the map, the copy-on-write status of the
3507 * entries we modify here cannot change.
3510 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
3511 if (VM_MAPENT_ISWIRED(entry
) == 0) { /* not already wired? */
3514 * perform actions of vm_map_lookup that need the
3515 * write lock on the map: create an anonymous map
3516 * for a copy-on-write region, or an anonymous map
3517 * for a zero-fill region. (XXXCDC: submap case
3521 if (!UVM_ET_ISSUBMAP(entry
)) { /* not submap */
3522 if (UVM_ET_ISNEEDSCOPY(entry
) &&
3523 ((entry
->max_protection
& VM_PROT_WRITE
) ||
3524 (entry
->object
.uvm_obj
== NULL
))) {
3525 amap_copy(map
, entry
, 0, start
, end
);
3526 /* XXXCDC: wait OK? */
3530 UVM_MAP_CLIP_START(map
, entry
, start
, NULL
);
3531 UVM_MAP_CLIP_END(map
, entry
, end
, NULL
);
3532 entry
->wired_count
++;
3538 if (entry
->protection
== VM_PROT_NONE
||
3539 (entry
->end
< end
&&
3540 (entry
->next
== &map
->header
||
3541 entry
->next
->start
> entry
->end
))) {
3544 * found one. amap creation actions do not need to
3545 * be undone, but the wired counts need to be restored.
3548 while (entry
!= &map
->header
&& entry
->end
> start
) {
3549 entry
->wired_count
--;
3550 entry
= entry
->prev
;
3552 if ((lockflags
& UVM_LK_EXIT
) == 0)
3554 UVMHIST_LOG(maphist
,"<- done (INVALID WIRE)",0,0,0,0);
3557 entry
= entry
->next
;
3565 timestamp_save
= map
->timestamp
;
3571 entry
= start_entry
;
3572 while (entry
!= &map
->header
&& entry
->start
< end
) {
3573 if (entry
->wired_count
== 1) {
3574 rv
= uvm_fault_wire(map
, entry
->start
, entry
->end
,
3575 entry
->max_protection
, 1);
3579 * wiring failed. break out of the loop.
3580 * we'll clean up the map below, once we
3581 * have a write lock again.
3587 entry
= entry
->next
;
3590 if (rv
) { /* failed? */
3593 * Get back to an exclusive (write) lock.
3600 if (timestamp_save
+ 1 != map
->timestamp
)
3601 panic("uvm_map_pageable: stale map");
3605 * first drop the wiring count on all the entries
3606 * which haven't actually been wired yet.
3609 failed_entry
= entry
;
3610 while (entry
!= &map
->header
&& entry
->start
< end
) {
3611 entry
->wired_count
--;
3612 entry
= entry
->next
;
3616 * now, unwire all the entries that were successfully
3620 entry
= start_entry
;
3621 while (entry
!= failed_entry
) {
3622 entry
->wired_count
--;
3623 if (VM_MAPENT_ISWIRED(entry
) == 0)
3624 uvm_map_entry_unwire(map
, entry
);
3625 entry
= entry
->next
;
3627 if ((lockflags
& UVM_LK_EXIT
) == 0)
3629 UVMHIST_LOG(maphist
, "<- done (RV=%d)", rv
,0,0,0);
3633 if ((lockflags
& UVM_LK_EXIT
) == 0) {
3638 * Get back to an exclusive (write) lock.
3645 UVMHIST_LOG(maphist
,"<- done (OK WIRE)",0,0,0,0);
3650 * uvm_map_pageable_all: special case of uvm_map_pageable - affects
3651 * all mapped regions.
3653 * => map must not be locked.
3654 * => if no flags are specified, all regions are unwired.
3655 * => XXXJRT: has some of the same problems as uvm_map_pageable() above.
3659 uvm_map_pageable_all(struct vm_map
*map
, int flags
, vsize_t limit
)
3661 struct vm_map_entry
*entry
, *failed_entry
;
3665 u_int timestamp_save
;
3667 UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist
);
3668 UVMHIST_LOG(maphist
,"(map=0x%x,flags=0x%x)", map
, flags
, 0, 0);
3670 KASSERT(map
->flags
& VM_MAP_PAGEABLE
);
3675 * handle wiring and unwiring separately.
3678 if (flags
== 0) { /* unwire */
3681 * POSIX 1003.1b -- munlockall unlocks all regions,
3682 * regardless of how many times mlockall has been called.
3685 for (entry
= map
->header
.next
; entry
!= &map
->header
;
3686 entry
= entry
->next
) {
3687 if (VM_MAPENT_ISWIRED(entry
))
3688 uvm_map_entry_unwire(map
, entry
);
3690 map
->flags
&= ~VM_MAP_WIREFUTURE
;
3692 UVMHIST_LOG(maphist
,"<- done (OK UNWIRE)",0,0,0,0);
3696 if (flags
& MCL_FUTURE
) {
3699 * must wire all future mappings; remember this.
3702 map
->flags
|= VM_MAP_WIREFUTURE
;
3705 if ((flags
& MCL_CURRENT
) == 0) {
3708 * no more work to do!
3711 UVMHIST_LOG(maphist
,"<- done (OK no wire)",0,0,0,0);
3717 * wire case: in three passes [XXXCDC: ugly block of code here]
3719 * 1: holding the write lock, count all pages mapped by non-wired
3720 * entries. if this would cause us to go over our limit, we fail.
3722 * 2: still holding the write lock, we create any anonymous maps that
3723 * need to be created. then we increment its wiring count.
3725 * 3: we downgrade to a read lock, and call uvm_fault_wire to fault
3726 * in the pages for any newly wired area (wired_count == 1).
3728 * downgrading to a read lock for uvm_fault_wire avoids a possible
3729 * deadlock with another thread that may have faulted on one of
3730 * the pages to be wired (it would mark the page busy, blocking
3731 * us, then in turn block on the map lock that we hold). because
3732 * of problems in the recursive lock package, we cannot upgrade
3733 * to a write lock in vm_map_lookup. thus, any actions that
3734 * require the write lock must be done beforehand. because we
3735 * keep the read lock on the map, the copy-on-write status of the
3736 * entries we modify here cannot change.
3739 for (size
= 0, entry
= map
->header
.next
; entry
!= &map
->header
;
3740 entry
= entry
->next
) {
3741 if (entry
->protection
!= VM_PROT_NONE
&&
3742 VM_MAPENT_ISWIRED(entry
) == 0) { /* not already wired? */
3743 size
+= entry
->end
- entry
->start
;
3747 if (atop(size
) + uvmexp
.wired
> uvmexp
.wiredmax
) {
3753 (size
+ ptoa(pmap_wired_count(vm_map_pmap(map
))) > limit
)) {
3762 for (entry
= map
->header
.next
; entry
!= &map
->header
;
3763 entry
= entry
->next
) {
3764 if (entry
->protection
== VM_PROT_NONE
)
3766 if (VM_MAPENT_ISWIRED(entry
) == 0) { /* not already wired? */
3769 * perform actions of vm_map_lookup that need the
3770 * write lock on the map: create an anonymous map
3771 * for a copy-on-write region, or an anonymous map
3772 * for a zero-fill region. (XXXCDC: submap case
3776 if (!UVM_ET_ISSUBMAP(entry
)) { /* not submap */
3777 if (UVM_ET_ISNEEDSCOPY(entry
) &&
3778 ((entry
->max_protection
& VM_PROT_WRITE
) ||
3779 (entry
->object
.uvm_obj
== NULL
))) {
3780 amap_copy(map
, entry
, 0, entry
->start
,
3782 /* XXXCDC: wait OK? */
3786 entry
->wired_count
++;
3794 timestamp_save
= map
->timestamp
;
3800 for (entry
= map
->header
.next
; entry
!= &map
->header
;
3801 entry
= entry
->next
) {
3802 if (entry
->wired_count
== 1) {
3803 rv
= uvm_fault_wire(map
, entry
->start
, entry
->end
,
3804 entry
->max_protection
, 1);
3808 * wiring failed. break out of the loop.
3809 * we'll clean up the map below, once we
3810 * have a write lock again.
3821 * Get back an exclusive (write) lock.
3828 if (timestamp_save
+ 1 != map
->timestamp
)
3829 panic("uvm_map_pageable_all: stale map");
3833 * first drop the wiring count on all the entries
3834 * which haven't actually been wired yet.
3836 * Skip VM_PROT_NONE entries like we did above.
3839 failed_entry
= entry
;
3840 for (/* nothing */; entry
!= &map
->header
;
3841 entry
= entry
->next
) {
3842 if (entry
->protection
== VM_PROT_NONE
)
3844 entry
->wired_count
--;
3848 * now, unwire all the entries that were successfully
3851 * Skip VM_PROT_NONE entries like we did above.
3854 for (entry
= map
->header
.next
; entry
!= failed_entry
;
3855 entry
= entry
->next
) {
3856 if (entry
->protection
== VM_PROT_NONE
)
3858 entry
->wired_count
--;
3859 if (VM_MAPENT_ISWIRED(entry
))
3860 uvm_map_entry_unwire(map
, entry
);
3863 UVMHIST_LOG(maphist
,"<- done (RV=%d)", rv
,0,0,0);
3869 UVMHIST_LOG(maphist
,"<- done (OK WIRE)",0,0,0,0);
3874 * uvm_map_clean: clean out a map range
3877 * if (flags & PGO_CLEANIT): dirty pages are cleaned first
3878 * if (flags & PGO_SYNCIO): dirty pages are written synchronously
3879 * if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean
3880 * if (flags & PGO_FREE): any cached pages are freed after clean
3881 * => returns an error if any part of the specified range isn't mapped
3882 * => never a need to flush amap layer since the anonymous memory has
3883 * no permanent home, but may deactivate pages there
3884 * => called from sys_msync() and sys_madvise()
3885 * => caller must not write-lock map (read OK).
3886 * => we may sleep while cleaning if SYNCIO [with map read-locked]
3890 uvm_map_clean(struct vm_map
*map
, vaddr_t start
, vaddr_t end
, int flags
)
3892 struct vm_map_entry
*current
, *entry
;
3893 struct uvm_object
*uobj
;
3894 struct vm_amap
*amap
;
3895 struct vm_anon
*anon
;
3901 UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist
);
3903 UVMHIST_LOG(maphist
,"(map=0x%x,start=0x%x,end=0x%x,flags=0x%x)",
3904 map
, start
, end
, flags
);
3905 KASSERT((flags
& (PGO_FREE
|PGO_DEACTIVATE
)) !=
3906 (PGO_FREE
|PGO_DEACTIVATE
));
3908 vm_map_lock_read(map
);
3909 VM_MAP_RANGE_CHECK(map
, start
, end
);
3910 if (uvm_map_lookup_entry(map
, start
, &entry
) == false) {
3911 vm_map_unlock_read(map
);
3916 * Make a first pass to check for holes and wiring problems.
3919 for (current
= entry
; current
->start
< end
; current
= current
->next
) {
3920 if (UVM_ET_ISSUBMAP(current
)) {
3921 vm_map_unlock_read(map
);
3924 if ((flags
& PGO_FREE
) != 0 && VM_MAPENT_ISWIRED(entry
)) {
3925 vm_map_unlock_read(map
);
3928 if (end
<= current
->end
) {
3931 if (current
->end
!= current
->next
->start
) {
3932 vm_map_unlock_read(map
);
3938 for (current
= entry
; start
< end
; current
= current
->next
) {
3939 amap
= current
->aref
.ar_amap
; /* upper layer */
3940 uobj
= current
->object
.uvm_obj
; /* lower layer */
3941 KASSERT(start
>= current
->start
);
3944 * No amap cleaning necessary if:
3946 * (1) There's no amap.
3948 * (2) We're not deactivating or freeing pages.
3951 if (amap
== NULL
|| (flags
& (PGO_DEACTIVATE
|PGO_FREE
)) == 0)
3955 offset
= start
- current
->start
;
3956 size
= MIN(end
, current
->end
) - start
;
3957 for ( ; size
!= 0; size
-= PAGE_SIZE
, offset
+= PAGE_SIZE
) {
3958 anon
= amap_lookup(¤t
->aref
, offset
);
3962 mutex_enter(&anon
->an_lock
);
3965 mutex_exit(&anon
->an_lock
);
3969 switch (flags
& (PGO_CLEANIT
|PGO_FREE
|PGO_DEACTIVATE
)) {
3972 * In these first 3 cases, we just deactivate the page.
3975 case PGO_CLEANIT
|PGO_FREE
:
3976 case PGO_CLEANIT
|PGO_DEACTIVATE
:
3977 case PGO_DEACTIVATE
:
3980 * skip the page if it's loaned or wired,
3981 * since it shouldn't be on a paging queue
3982 * at all in these cases.
3985 mutex_enter(&uvm_pageqlock
);
3986 if (pg
->loan_count
!= 0 ||
3987 pg
->wire_count
!= 0) {
3988 mutex_exit(&uvm_pageqlock
);
3989 mutex_exit(&anon
->an_lock
);
3992 KASSERT(pg
->uanon
== anon
);
3993 uvm_pagedeactivate(pg
);
3994 mutex_exit(&uvm_pageqlock
);
3995 mutex_exit(&anon
->an_lock
);
4001 * If there are multiple references to
4002 * the amap, just deactivate the page.
4005 if (amap_refs(amap
) > 1)
4008 /* skip the page if it's wired */
4009 if (pg
->wire_count
!= 0) {
4010 mutex_exit(&anon
->an_lock
);
4013 amap_unadd(¤t
->aref
, offset
);
4014 refs
= --anon
->an_ref
;
4015 mutex_exit(&anon
->an_lock
);
4025 * flush pages if we've got a valid backing object.
4026 * note that we must always clean object pages before
4027 * freeing them since otherwise we could reveal stale
4031 uoff
= current
->offset
+ (start
- current
->start
);
4032 size
= MIN(end
, current
->end
) - start
;
4034 mutex_enter(&uobj
->vmobjlock
);
4035 if (uobj
->pgops
->pgo_put
!= NULL
)
4036 error
= (uobj
->pgops
->pgo_put
)(uobj
, uoff
,
4037 uoff
+ size
, flags
| PGO_CLEANIT
);
4043 vm_map_unlock_read(map
);
4049 * uvm_map_checkprot: check protection in map
4051 * => must allow specified protection in a fully allocated region.
4052 * => map must be read or write locked by caller.
4056 uvm_map_checkprot(struct vm_map
*map
, vaddr_t start
, vaddr_t end
,
4057 vm_prot_t protection
)
4059 struct vm_map_entry
*entry
;
4060 struct vm_map_entry
*tmp_entry
;
4062 if (!uvm_map_lookup_entry(map
, start
, &tmp_entry
)) {
4066 while (start
< end
) {
4067 if (entry
== &map
->header
) {
4075 if (start
< entry
->start
) {
4080 * check protection associated with entry
4083 if ((entry
->protection
& protection
) != protection
) {
4087 entry
= entry
->next
;
4093 * uvmspace_alloc: allocate a vmspace structure.
4095 * - structure includes vm_map and pmap
4096 * - XXX: no locking on this structure
4097 * - refcnt set to 1, rest must be init'd by caller
4100 uvmspace_alloc(vaddr_t vmin
, vaddr_t vmax
)
4103 UVMHIST_FUNC("uvmspace_alloc"); UVMHIST_CALLED(maphist
);
4105 vm
= pool_cache_get(&uvm_vmspace_cache
, PR_WAITOK
);
4106 uvmspace_init(vm
, NULL
, vmin
, vmax
);
4107 UVMHIST_LOG(maphist
,"<- done (vm=0x%x)", vm
,0,0,0);
4112 * uvmspace_init: initialize a vmspace structure.
4114 * - XXX: no locking on this structure
4115 * - refcnt set to 1, rest must be init'd by caller
4118 uvmspace_init(struct vmspace
*vm
, struct pmap
*pmap
, vaddr_t vmin
, vaddr_t vmax
)
4120 UVMHIST_FUNC("uvmspace_init"); UVMHIST_CALLED(maphist
);
4122 memset(vm
, 0, sizeof(*vm
));
4123 uvm_map_setup(&vm
->vm_map
, vmin
, vmax
, VM_MAP_PAGEABLE
4124 #ifdef __USING_TOPDOWN_VM
4129 pmap_reference(pmap
);
4131 pmap
= pmap_create();
4132 vm
->vm_map
.pmap
= pmap
;
4134 UVMHIST_LOG(maphist
,"<- done",0,0,0,0);
4138 * uvmspace_share: share a vmspace between two processes
4140 * - used for vfork, threads(?)
4144 uvmspace_share(struct proc
*p1
, struct proc
*p2
)
4147 uvmspace_addref(p1
->p_vmspace
);
4148 p2
->p_vmspace
= p1
->p_vmspace
;
4154 * uvmspace_unshare: ensure that process "p" has its own, unshared, vmspace
4156 * - XXX: no locking on vmspace
4160 uvmspace_unshare(struct lwp
*l
)
4162 struct proc
*p
= l
->l_proc
;
4163 struct vmspace
*nvm
, *ovm
= p
->p_vmspace
;
4165 if (ovm
->vm_refcnt
== 1)
4166 /* nothing to do: vmspace isn't shared in the first place */
4169 /* make a new vmspace, still holding old one */
4170 nvm
= uvmspace_fork(ovm
);
4173 pmap_deactivate(l
); /* unbind old vmspace */
4175 pmap_activate(l
); /* switch to new vmspace */
4178 uvmspace_free(ovm
); /* drop reference to old vmspace */
4184 * uvmspace_exec: the process wants to exec a new program
4188 uvmspace_exec(struct lwp
*l
, vaddr_t start
, vaddr_t end
)
4190 struct proc
*p
= l
->l_proc
;
4191 struct vmspace
*nvm
, *ovm
= p
->p_vmspace
;
4192 struct vm_map
*map
= &ovm
->vm_map
;
4195 /* XXX cgd 960926: the sparc #ifdef should be a MD hook */
4196 kill_user_windows(l
); /* before stack addresses go away */
4200 * see if more than one process is using this vmspace...
4203 if (ovm
->vm_refcnt
== 1) {
4206 * if p is the only process using its vmspace then we can safely
4207 * recycle that vmspace for the program that is being exec'd.
4212 * SYSV SHM semantics require us to kill all segments on an exec
4220 * POSIX 1003.1b -- "lock future mappings" is revoked
4221 * when a process execs another program image.
4224 map
->flags
&= ~VM_MAP_WIREFUTURE
;
4227 * now unmap the old program
4230 pmap_remove_all(map
->pmap
);
4231 uvm_unmap(map
, vm_map_min(map
), vm_map_max(map
));
4232 KASSERT(map
->header
.prev
== &map
->header
);
4233 KASSERT(map
->nentries
== 0);
4239 vm_map_setmin(map
, start
);
4240 vm_map_setmax(map
, end
);
4244 * p's vmspace is being shared, so we can't reuse it for p since
4245 * it is still being used for others. allocate a new vmspace
4249 nvm
= uvmspace_alloc(start
, end
);
4252 * install new vmspace and drop our ref to the old one.
4266 * uvmspace_addref: add a referece to a vmspace.
4270 uvmspace_addref(struct vmspace
*vm
)
4272 struct vm_map
*map
= &vm
->vm_map
;
4274 KASSERT((map
->flags
& VM_MAP_DYING
) == 0);
4276 mutex_enter(&map
->misc_lock
);
4277 KASSERT(vm
->vm_refcnt
> 0);
4279 mutex_exit(&map
->misc_lock
);
4283 * uvmspace_free: free a vmspace data structure
4287 uvmspace_free(struct vmspace
*vm
)
4289 struct vm_map_entry
*dead_entries
;
4290 struct vm_map
*map
= &vm
->vm_map
;
4293 UVMHIST_FUNC("uvmspace_free"); UVMHIST_CALLED(maphist
);
4295 UVMHIST_LOG(maphist
,"(vm=0x%x) ref=%d", vm
, vm
->vm_refcnt
,0,0);
4296 mutex_enter(&map
->misc_lock
);
4297 n
= --vm
->vm_refcnt
;
4298 mutex_exit(&map
->misc_lock
);
4303 * at this point, there should be no other references to the map.
4304 * delete all of the mappings, then destroy the pmap.
4307 map
->flags
|= VM_MAP_DYING
;
4308 pmap_remove_all(map
->pmap
);
4310 /* Get rid of any SYSV shared memory segments. */
4311 if (vm
->vm_shm
!= NULL
)
4314 if (map
->nentries
) {
4315 uvm_unmap_remove(map
, vm_map_min(map
), vm_map_max(map
),
4316 &dead_entries
, NULL
, 0);
4317 if (dead_entries
!= NULL
)
4318 uvm_unmap_detach(dead_entries
, 0);
4320 KASSERT(map
->nentries
== 0);
4321 KASSERT(map
->size
== 0);
4322 mutex_destroy(&map
->misc_lock
);
4323 mutex_destroy(&map
->mutex
);
4324 rw_destroy(&map
->lock
);
4325 cv_destroy(&map
->cv
);
4326 pmap_destroy(map
->pmap
);
4327 pool_cache_put(&uvm_vmspace_cache
, vm
);
4331 * F O R K - m a i n e n t r y p o i n t
4334 * uvmspace_fork: fork a process' main map
4336 * => create a new vmspace for child process from parent.
4337 * => parent's map must not be locked.
4341 uvmspace_fork(struct vmspace
*vm1
)
4343 struct vmspace
*vm2
;
4344 struct vm_map
*old_map
= &vm1
->vm_map
;
4345 struct vm_map
*new_map
;
4346 struct vm_map_entry
*old_entry
;
4347 struct vm_map_entry
*new_entry
;
4348 UVMHIST_FUNC("uvmspace_fork"); UVMHIST_CALLED(maphist
);
4350 vm_map_lock(old_map
);
4352 vm2
= uvmspace_alloc(vm_map_min(old_map
), vm_map_max(old_map
));
4353 memcpy(&vm2
->vm_startcopy
, &vm1
->vm_startcopy
,
4354 (char *) (vm1
+ 1) - (char *) &vm1
->vm_startcopy
);
4355 new_map
= &vm2
->vm_map
; /* XXX */
4357 old_entry
= old_map
->header
.next
;
4358 new_map
->size
= old_map
->size
;
4364 while (old_entry
!= &old_map
->header
) {
4367 * first, some sanity checks on the old entry
4370 KASSERT(!UVM_ET_ISSUBMAP(old_entry
));
4371 KASSERT(UVM_ET_ISCOPYONWRITE(old_entry
) ||
4372 !UVM_ET_ISNEEDSCOPY(old_entry
));
4374 switch (old_entry
->inheritance
) {
4375 case MAP_INHERIT_NONE
:
4378 * drop the mapping, modify size
4380 new_map
->size
-= old_entry
->end
- old_entry
->start
;
4383 case MAP_INHERIT_SHARE
:
4386 * share the mapping: this means we want the old and
4387 * new entries to share amaps and backing objects.
4390 * if the old_entry needs a new amap (due to prev fork)
4391 * then we need to allocate it now so that we have
4392 * something we own to share with the new_entry. [in
4393 * other words, we need to clear needs_copy]
4396 if (UVM_ET_ISNEEDSCOPY(old_entry
)) {
4397 /* get our own amap, clears needs_copy */
4398 amap_copy(old_map
, old_entry
, AMAP_COPY_NOCHUNK
,
4400 /* XXXCDC: WAITOK??? */
4403 new_entry
= uvm_mapent_alloc(new_map
, 0);
4404 /* old_entry -> new_entry */
4405 uvm_mapent_copy(old_entry
, new_entry
);
4407 /* new pmap has nothing wired in it */
4408 new_entry
->wired_count
= 0;
4411 * gain reference to object backing the map (can't
4412 * be a submap, already checked this case).
4415 if (new_entry
->aref
.ar_amap
)
4416 uvm_map_reference_amap(new_entry
, AMAP_SHARED
);
4418 if (new_entry
->object
.uvm_obj
&&
4419 new_entry
->object
.uvm_obj
->pgops
->pgo_reference
)
4420 new_entry
->object
.uvm_obj
->
4421 pgops
->pgo_reference(
4422 new_entry
->object
.uvm_obj
);
4424 /* insert entry at end of new_map's entry list */
4425 uvm_map_entry_link(new_map
, new_map
->header
.prev
,
4430 case MAP_INHERIT_COPY
:
4433 * copy-on-write the mapping (using mmap's
4434 * MAP_PRIVATE semantics)
4436 * allocate new_entry, adjust reference counts.
4437 * (note that new references are read-only).
4440 new_entry
= uvm_mapent_alloc(new_map
, 0);
4441 /* old_entry -> new_entry */
4442 uvm_mapent_copy(old_entry
, new_entry
);
4444 if (new_entry
->aref
.ar_amap
)
4445 uvm_map_reference_amap(new_entry
, 0);
4447 if (new_entry
->object
.uvm_obj
&&
4448 new_entry
->object
.uvm_obj
->pgops
->pgo_reference
)
4449 new_entry
->object
.uvm_obj
->pgops
->pgo_reference
4450 (new_entry
->object
.uvm_obj
);
4452 /* new pmap has nothing wired in it */
4453 new_entry
->wired_count
= 0;
4456 (UVM_ET_COPYONWRITE
|UVM_ET_NEEDSCOPY
);
4457 uvm_map_entry_link(new_map
, new_map
->header
.prev
,
4461 * the new entry will need an amap. it will either
4462 * need to be copied from the old entry or created
4463 * from scratch (if the old entry does not have an
4464 * amap). can we defer this process until later
4465 * (by setting "needs_copy") or do we need to copy
4468 * we must copy the amap now if any of the following
4470 * 1. the old entry has an amap and that amap is
4471 * being shared. this means that the old (parent)
4472 * process is sharing the amap with another
4473 * process. if we do not clear needs_copy here
4474 * we will end up in a situation where both the
4475 * parent and child process are refering to the
4476 * same amap with "needs_copy" set. if the
4477 * parent write-faults, the fault routine will
4478 * clear "needs_copy" in the parent by allocating
4479 * a new amap. this is wrong because the
4480 * parent is supposed to be sharing the old amap
4481 * and the new amap will break that.
4483 * 2. if the old entry has an amap and a non-zero
4484 * wire count then we are going to have to call
4485 * amap_cow_now to avoid page faults in the
4486 * parent process. since amap_cow_now requires
4487 * "needs_copy" to be clear we might as well
4488 * clear it here as well.
4492 if (old_entry
->aref
.ar_amap
!= NULL
) {
4493 if ((amap_flags(old_entry
->aref
.ar_amap
) &
4494 AMAP_SHARED
) != 0 ||
4495 VM_MAPENT_ISWIRED(old_entry
)) {
4497 amap_copy(new_map
, new_entry
,
4498 AMAP_COPY_NOCHUNK
, 0, 0);
4499 /* XXXCDC: M_WAITOK ... ok? */
4504 * if the parent's entry is wired down, then the
4505 * parent process does not want page faults on
4506 * access to that memory. this means that we
4507 * cannot do copy-on-write because we can't write
4508 * protect the old entry. in this case we
4509 * resolve all copy-on-write faults now, using
4510 * amap_cow_now. note that we have already
4511 * allocated any needed amap (above).
4514 if (VM_MAPENT_ISWIRED(old_entry
)) {
4517 * resolve all copy-on-write faults now
4518 * (note that there is nothing to do if
4519 * the old mapping does not have an amap).
4521 if (old_entry
->aref
.ar_amap
)
4522 amap_cow_now(new_map
, new_entry
);
4527 * setup mappings to trigger copy-on-write faults
4528 * we must write-protect the parent if it has
4529 * an amap and it is not already "needs_copy"...
4530 * if it is already "needs_copy" then the parent
4531 * has already been write-protected by a previous
4535 if (old_entry
->aref
.ar_amap
&&
4536 !UVM_ET_ISNEEDSCOPY(old_entry
)) {
4537 if (old_entry
->max_protection
& VM_PROT_WRITE
) {
4538 pmap_protect(old_map
->pmap
,
4541 old_entry
->protection
&
4544 old_entry
->etype
|= UVM_ET_NEEDSCOPY
;
4548 } /* end of switch statement */
4549 old_entry
= old_entry
->next
;
4552 pmap_update(old_map
->pmap
);
4553 vm_map_unlock(old_map
);
4561 pmap_fork(vm1
->vm_map
.pmap
, vm2
->vm_map
.pmap
);
4564 UVMHIST_LOG(maphist
,"<- done",0,0,0,0);
4570 * in-kernel map entry allocation.
4573 struct uvm_kmapent_hdr
{
4574 LIST_ENTRY(uvm_kmapent_hdr
) ukh_listq
;
4576 struct vm_map_entry
*ukh_freelist
;
4577 struct vm_map
*ukh_map
;
4578 struct vm_map_entry ukh_entries
[0];
4581 #define UVM_KMAPENT_CHUNK \
4582 ((PAGE_SIZE - sizeof(struct uvm_kmapent_hdr)) \
4583 / sizeof(struct vm_map_entry))
4585 #define UVM_KHDR_FIND(entry) \
4586 ((struct uvm_kmapent_hdr *)(((vaddr_t)entry) & ~PAGE_MASK))
4590 static struct vm_map
*
4591 uvm_kmapent_map(struct vm_map_entry
*entry
)
4593 const struct uvm_kmapent_hdr
*ukh
;
4595 ukh
= UVM_KHDR_FIND(entry
);
4596 return ukh
->ukh_map
;
4600 static inline struct vm_map_entry
*
4601 uvm_kmapent_get(struct uvm_kmapent_hdr
*ukh
)
4603 struct vm_map_entry
*entry
;
4605 KASSERT(ukh
->ukh_nused
<= UVM_KMAPENT_CHUNK
);
4606 KASSERT(ukh
->ukh_nused
>= 0);
4608 entry
= ukh
->ukh_freelist
;
4610 KASSERT((entry
->flags
& (UVM_MAP_KERNEL
| UVM_MAP_KMAPENT
))
4612 ukh
->ukh_freelist
= entry
->next
;
4614 KASSERT(ukh
->ukh_nused
<= UVM_KMAPENT_CHUNK
);
4616 KASSERT(ukh
->ukh_nused
== UVM_KMAPENT_CHUNK
);
4623 uvm_kmapent_put(struct uvm_kmapent_hdr
*ukh
, struct vm_map_entry
*entry
)
4626 KASSERT((entry
->flags
& (UVM_MAP_KERNEL
| UVM_MAP_KMAPENT
))
4628 KASSERT(ukh
->ukh_nused
<= UVM_KMAPENT_CHUNK
);
4629 KASSERT(ukh
->ukh_nused
> 0);
4630 KASSERT(ukh
->ukh_freelist
!= NULL
||
4631 ukh
->ukh_nused
== UVM_KMAPENT_CHUNK
);
4632 KASSERT(ukh
->ukh_freelist
== NULL
||
4633 ukh
->ukh_nused
< UVM_KMAPENT_CHUNK
);
4636 entry
->next
= ukh
->ukh_freelist
;
4637 ukh
->ukh_freelist
= entry
;
4641 * uvm_kmapent_alloc: allocate a map entry for in-kernel map
4644 static struct vm_map_entry
*
4645 uvm_kmapent_alloc(struct vm_map
*map
, int flags
)
4648 struct uvm_kmapent_hdr
*ukh
;
4649 struct vm_map_entry
*entry
;
4650 #ifndef PMAP_MAP_POOLPAGE
4651 struct uvm_map_args args
;
4652 uvm_flag_t mapflags
= UVM_MAPFLAG(UVM_PROT_ALL
, UVM_PROT_ALL
,
4653 UVM_INH_NONE
, UVM_ADV_RANDOM
, flags
| UVM_FLAG_NOMERGE
);
4659 KDASSERT(UVM_KMAPENT_CHUNK
> 2);
4660 KDASSERT(kernel_map
!= NULL
);
4661 KASSERT(vm_map_pmap(map
) == pmap_kernel());
4663 UVMMAP_EVCNT_INCR(uke_alloc
);
4667 * try to grab an entry from freelist.
4669 mutex_spin_enter(&uvm_kentry_lock
);
4670 ukh
= LIST_FIRST(&vm_map_to_kernel(map
)->vmk_kentry_free
);
4672 entry
= uvm_kmapent_get(ukh
);
4673 if (ukh
->ukh_nused
== UVM_KMAPENT_CHUNK
)
4674 LIST_REMOVE(ukh
, ukh_listq
);
4676 mutex_spin_exit(&uvm_kentry_lock
);
4682 * there's no free entry for this vm_map.
4683 * now we need to allocate some vm_map_entry.
4684 * for simplicity, always allocate one page chunk of them at once.
4687 pg
= uvm_pagealloc(NULL
, 0, NULL
,
4688 (flags
& UVM_KMF_NOWAIT
) != 0 ? UVM_PGA_USERESERVE
: 0);
4689 if (__predict_false(pg
== NULL
)) {
4690 if (flags
& UVM_FLAG_NOWAIT
)
4692 uvm_wait("kme_alloc");
4696 #ifdef PMAP_MAP_POOLPAGE
4697 va
= PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg
));
4700 error
= uvm_map_prepare(map
, 0, PAGE_SIZE
, NULL
, UVM_UNKNOWN_OFFSET
,
4701 0, mapflags
, &args
);
4707 va
= args
.uma_start
;
4709 pmap_kenter_pa(va
, VM_PAGE_TO_PHYS(pg
),
4710 VM_PROT_READ
|VM_PROT_WRITE
|PMAP_KMPAGE
, 0);
4711 pmap_update(vm_map_pmap(map
));
4717 * use the last entry for ukh itsself.
4720 i
= UVM_KMAPENT_CHUNK
- 1;
4721 #ifndef PMAP_MAP_POOLPAGE
4722 entry
= &ukh
->ukh_entries
[i
--];
4723 entry
->flags
= UVM_MAP_KERNEL
| UVM_MAP_KMAPENT
;
4724 error
= uvm_map_enter(map
, &args
, entry
);
4725 KASSERT(error
== 0);
4728 ukh
->ukh_nused
= UVM_KMAPENT_CHUNK
;
4730 ukh
->ukh_freelist
= NULL
;
4731 for (; i
>= 1; i
--) {
4732 struct vm_map_entry
*xentry
= &ukh
->ukh_entries
[i
];
4734 xentry
->flags
= UVM_MAP_KERNEL
;
4735 uvm_kmapent_put(ukh
, xentry
);
4737 #ifdef PMAP_MAP_POOLPAGE
4738 KASSERT(ukh
->ukh_nused
== 1);
4740 KASSERT(ukh
->ukh_nused
== 2);
4743 mutex_spin_enter(&uvm_kentry_lock
);
4744 LIST_INSERT_HEAD(&vm_map_to_kernel(map
)->vmk_kentry_free
,
4746 mutex_spin_exit(&uvm_kentry_lock
);
4749 * return first entry.
4752 entry
= &ukh
->ukh_entries
[0];
4753 entry
->flags
= UVM_MAP_KERNEL
;
4754 UVMMAP_EVCNT_INCR(ukh_alloc
);
4760 * uvm_mapent_free: free map entry for in-kernel map
4764 uvm_kmapent_free(struct vm_map_entry
*entry
)
4766 struct uvm_kmapent_hdr
*ukh
;
4769 #ifndef PMAP_UNMAP_POOLPAGE
4771 struct vm_map_entry
*deadentry
;
4776 UVMMAP_EVCNT_INCR(uke_free
);
4777 ukh
= UVM_KHDR_FIND(entry
);
4780 mutex_spin_enter(&uvm_kentry_lock
);
4781 uvm_kmapent_put(ukh
, entry
);
4782 #ifdef PMAP_UNMAP_POOLPAGE
4783 if (ukh
->ukh_nused
> 0) {
4785 if (ukh
->ukh_nused
> 1) {
4787 if (ukh
->ukh_nused
== UVM_KMAPENT_CHUNK
- 1)
4789 &vm_map_to_kernel(map
)->vmk_kentry_free
,
4791 mutex_spin_exit(&uvm_kentry_lock
);
4796 * now we can free this ukh.
4798 * however, keep an empty ukh to avoid ping-pong.
4801 if (LIST_FIRST(&vm_map_to_kernel(map
)->vmk_kentry_free
) == ukh
&&
4802 LIST_NEXT(ukh
, ukh_listq
) == NULL
) {
4803 mutex_spin_exit(&uvm_kentry_lock
);
4806 LIST_REMOVE(ukh
, ukh_listq
);
4807 mutex_spin_exit(&uvm_kentry_lock
);
4811 #ifdef PMAP_UNMAP_POOLPAGE
4812 KASSERT(ukh
->ukh_nused
== 0);
4813 pa
= PMAP_UNMAP_POOLPAGE(va
);
4816 KASSERT(ukh
->ukh_nused
== 1);
4819 * remove map entry for ukh itsself.
4822 KASSERT((va
& PAGE_MASK
) == 0);
4824 uvm_unmap_remove(map
, va
, va
+ PAGE_SIZE
, &deadentry
, NULL
, 0);
4825 KASSERT(deadentry
->flags
& UVM_MAP_KERNEL
);
4826 KASSERT(deadentry
->flags
& UVM_MAP_KMAPENT
);
4827 KASSERT(deadentry
->next
== NULL
);
4828 KASSERT(deadentry
== &ukh
->ukh_entries
[UVM_KMAPENT_CHUNK
- 1]);
4831 * unmap the page from pmap and free it.
4834 pmap
= vm_map_pmap(map
);
4835 KASSERT(pmap
== pmap_kernel());
4836 if (!pmap_extract(pmap
, va
, &pa
))
4837 panic("%s: no mapping", __func__
);
4838 pmap_kremove(va
, PAGE_SIZE
);
4839 pmap_update(vm_map_pmap(map
));
4841 #endif /* !PMAP_UNMAP_POOLPAGE */
4842 pg
= PHYS_TO_VM_PAGE(pa
);
4844 UVMMAP_EVCNT_INCR(ukh_free
);
4848 uvm_kmapent_overhead(vsize_t size
)
4852 * - the max number of unmerged entries is howmany(size, PAGE_SIZE)
4853 * as the min allocation unit is PAGE_SIZE.
4854 * - UVM_KMAPENT_CHUNK "kmapent"s are allocated from a page.
4855 * one of them are used to map the page itself.
4858 return howmany(howmany(size
, PAGE_SIZE
), (UVM_KMAPENT_CHUNK
- 1)) *
4863 * map entry reservation
4867 * uvm_mapent_reserve: reserve map entries for clipping before locking map.
4869 * => needed when unmapping entries allocated without UVM_FLAG_QUANTUM.
4870 * => caller shouldn't hold map locked.
4873 uvm_mapent_reserve(struct vm_map
*map
, struct uvm_mapent_reservation
*umr
,
4874 int nentries
, int flags
)
4877 umr
->umr_nentries
= 0;
4879 if ((flags
& UVM_FLAG_QUANTUM
) != 0)
4882 if (!VM_MAP_USE_KMAPENT(map
))
4885 while (nentries
--) {
4886 struct vm_map_entry
*ent
;
4887 ent
= uvm_kmapent_alloc(map
, flags
);
4889 uvm_mapent_unreserve(map
, umr
);
4892 UMR_PUTENTRY(umr
, ent
);
4899 * uvm_mapent_unreserve:
4901 * => caller shouldn't hold map locked.
4902 * => never fail or sleep.
4905 uvm_mapent_unreserve(struct vm_map
*map
, struct uvm_mapent_reservation
*umr
)
4908 while (!UMR_EMPTY(umr
))
4909 uvm_kmapent_free(UMR_GETENTRY(umr
));
4913 * uvm_mapent_trymerge: try to merge an entry with its neighbors.
4915 * => called with map locked.
4916 * => return non zero if successfully merged.
4920 uvm_mapent_trymerge(struct vm_map
*map
, struct vm_map_entry
*entry
, int flags
)
4922 struct uvm_object
*uobj
;
4923 struct vm_map_entry
*next
;
4924 struct vm_map_entry
*prev
;
4930 if (VM_MAP_USE_KMAPENT(map
)) {
4933 if (entry
->aref
.ar_amap
!= NULL
) {
4936 if ((entry
->flags
& UVM_MAP_NOMERGE
) != 0) {
4940 uobj
= entry
->object
.uvm_obj
;
4941 size
= entry
->end
- entry
->start
;
4942 copying
= (flags
& UVM_MERGE_COPYING
) != 0;
4943 newetype
= copying
? (entry
->etype
& ~UVM_ET_NEEDSCOPY
) : entry
->etype
;
4946 if (next
!= &map
->header
&&
4947 next
->start
== entry
->end
&&
4948 ((copying
&& next
->aref
.ar_amap
!= NULL
&&
4949 amap_refs(next
->aref
.ar_amap
) == 1) ||
4950 (!copying
&& next
->aref
.ar_amap
== NULL
)) &&
4951 UVM_ET_ISCOMPATIBLE(next
, newetype
,
4952 uobj
, entry
->flags
, entry
->protection
,
4953 entry
->max_protection
, entry
->inheritance
, entry
->advice
,
4954 entry
->wired_count
) &&
4955 (uobj
== NULL
|| entry
->offset
+ size
== next
->offset
)) {
4959 error
= amap_extend(next
, size
,
4960 AMAP_EXTEND_NOWAIT
|AMAP_EXTEND_BACKWARDS
);
4966 if (uobj
->pgops
->pgo_detach
) {
4967 uobj
->pgops
->pgo_detach(uobj
);
4971 entry
->end
= next
->end
;
4972 clear_hints(map
, next
);
4973 uvm_map_entry_unlink(map
, next
);
4975 entry
->aref
= next
->aref
;
4976 entry
->etype
&= ~UVM_ET_NEEDSCOPY
;
4978 uvm_map_check(map
, "trymerge forwardmerge");
4979 uvm_mapent_free_merged(map
, next
);
4985 if (prev
!= &map
->header
&&
4986 prev
->end
== entry
->start
&&
4987 ((copying
&& !merged
&& prev
->aref
.ar_amap
!= NULL
&&
4988 amap_refs(prev
->aref
.ar_amap
) == 1) ||
4989 (!copying
&& prev
->aref
.ar_amap
== NULL
)) &&
4990 UVM_ET_ISCOMPATIBLE(prev
, newetype
,
4991 uobj
, entry
->flags
, entry
->protection
,
4992 entry
->max_protection
, entry
->inheritance
, entry
->advice
,
4993 entry
->wired_count
) &&
4995 prev
->offset
+ prev
->end
- prev
->start
== entry
->offset
)) {
4999 error
= amap_extend(prev
, size
,
5000 AMAP_EXTEND_NOWAIT
|AMAP_EXTEND_FORWARDS
);
5006 if (uobj
->pgops
->pgo_detach
) {
5007 uobj
->pgops
->pgo_detach(uobj
);
5009 entry
->offset
= prev
->offset
;
5012 entry
->start
= prev
->start
;
5013 clear_hints(map
, prev
);
5014 uvm_map_entry_unlink(map
, prev
);
5016 entry
->aref
= prev
->aref
;
5017 entry
->etype
&= ~UVM_ET_NEEDSCOPY
;
5019 uvm_map_check(map
, "trymerge backmerge");
5020 uvm_mapent_free_merged(map
, prev
);
5029 * uvm_map_create: create map
5033 uvm_map_create(pmap_t pmap
, vaddr_t vmin
, vaddr_t vmax
, int flags
)
5035 struct vm_map
*result
;
5037 result
= malloc(sizeof(struct vm_map
), M_VMMAP
, M_WAITOK
);
5038 uvm_map_setup(result
, vmin
, vmax
, flags
);
5039 result
->pmap
= pmap
;
5044 * uvm_map_setup: init map
5046 * => map must not be in service yet.
5050 uvm_map_setup(struct vm_map
*map
, vaddr_t vmin
, vaddr_t vmax
, int flags
)
5054 rb_tree_init(&map
->rb_tree
, &uvm_map_tree_ops
);
5055 map
->header
.next
= map
->header
.prev
= &map
->header
;
5059 vm_map_setmin(map
, vmin
);
5060 vm_map_setmax(map
, vmax
);
5062 map
->first_free
= &map
->header
;
5063 map
->hint
= &map
->header
;
5067 if ((flags
& VM_MAP_INTRSAFE
) != 0) {
5073 rw_init(&map
->lock
);
5074 cv_init(&map
->cv
, "vm_map");
5075 mutex_init(&map
->misc_lock
, MUTEX_DRIVER
, ipl
);
5076 mutex_init(&map
->mutex
, MUTEX_DRIVER
, ipl
);
5081 * U N M A P - m a i n e n t r y p o i n t
5085 * uvm_unmap1: remove mappings from a vm_map (from "start" up to "stop")
5087 * => caller must check alignment and size
5088 * => map must be unlocked (we will lock it)
5089 * => flags is UVM_FLAG_QUANTUM or 0.
5093 uvm_unmap1(struct vm_map
*map
, vaddr_t start
, vaddr_t end
, int flags
)
5095 struct vm_map_entry
*dead_entries
;
5096 struct uvm_mapent_reservation umr
;
5097 UVMHIST_FUNC("uvm_unmap"); UVMHIST_CALLED(maphist
);
5099 UVMHIST_LOG(maphist
, " (map=0x%x, start=0x%x, end=0x%x)",
5100 map
, start
, end
, 0);
5101 if (map
== kernel_map
) {
5102 LOCKDEBUG_MEM_CHECK((void *)start
, end
- start
);
5105 * work now done by helper functions. wipe the pmap's and then
5106 * detach from the dead entries...
5108 uvm_mapent_reserve(map
, &umr
, 2, flags
);
5110 uvm_unmap_remove(map
, start
, end
, &dead_entries
, &umr
, flags
);
5112 uvm_mapent_unreserve(map
, &umr
);
5114 if (dead_entries
!= NULL
)
5115 uvm_unmap_detach(dead_entries
, 0);
5117 UVMHIST_LOG(maphist
, "<- done", 0,0,0,0);
5122 * uvm_map_reference: add reference to a map
5124 * => map need not be locked (we use misc_lock).
5128 uvm_map_reference(struct vm_map
*map
)
5130 mutex_enter(&map
->misc_lock
);
5132 mutex_exit(&map
->misc_lock
);
5135 struct vm_map_kernel
*
5136 vm_map_to_kernel(struct vm_map
*map
)
5139 KASSERT(VM_MAP_IS_KERNEL(map
));
5141 return (struct vm_map_kernel
*)map
;
5145 vm_map_starved_p(struct vm_map
*map
)
5148 if ((map
->flags
& VM_MAP_WANTVA
) != 0) {
5152 if ((vm_map_max(map
) - vm_map_min(map
)) / 16 * 15 < map
->size
) {
5158 #if defined(DDB) || defined(DEBUGPRINT)
5161 * uvm_map_printit: actually prints the map
5165 uvm_map_printit(struct vm_map
*map
, bool full
,
5166 void (*pr
)(const char *, ...))
5168 struct vm_map_entry
*entry
;
5170 (*pr
)("MAP %p: [0x%lx->0x%lx]\n", map
, vm_map_min(map
),
5172 (*pr
)("\t#ent=%d, sz=%d, ref=%d, version=%d, flags=0x%x\n",
5173 map
->nentries
, map
->size
, map
->ref_count
, map
->timestamp
,
5175 (*pr
)("\tpmap=%p(resident=%ld, wired=%ld)\n", map
->pmap
,
5176 pmap_resident_count(map
->pmap
), pmap_wired_count(map
->pmap
));
5179 for (entry
= map
->header
.next
; entry
!= &map
->header
;
5180 entry
= entry
->next
) {
5181 (*pr
)(" - %p: 0x%lx->0x%lx: obj=%p/0x%llx, amap=%p/%d\n",
5182 entry
, entry
->start
, entry
->end
, entry
->object
.uvm_obj
,
5183 (long long)entry
->offset
, entry
->aref
.ar_amap
,
5184 entry
->aref
.ar_pageoff
);
5186 "\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, "
5188 (entry
->etype
& UVM_ET_SUBMAP
) ? 'T' : 'F',
5189 (entry
->etype
& UVM_ET_COPYONWRITE
) ? 'T' : 'F',
5190 (entry
->etype
& UVM_ET_NEEDSCOPY
) ? 'T' : 'F',
5191 entry
->protection
, entry
->max_protection
,
5192 entry
->inheritance
, entry
->wired_count
, entry
->advice
);
5197 uvm_whatis(uintptr_t addr
, void (*pr
)(const char *, ...))
5201 for (map
= kernel_map
;;) {
5202 struct vm_map_entry
*entry
;
5204 if (!uvm_map_lookup_entry_bytree(map
, (vaddr_t
)addr
, &entry
)) {
5207 (*pr
)("%p is %p+%zu from VMMAP %p\n",
5208 (void *)addr
, (void *)entry
->start
,
5209 (size_t)(addr
- (uintptr_t)entry
->start
), map
);
5210 if (!UVM_ET_ISSUBMAP(entry
)) {
5213 map
= entry
->object
.sub_map
;
5217 #endif /* DDB || DEBUGPRINT */