uvm_map.c revision 1.218 1 /* $NetBSD: uvm_map.c,v 1.218 2006/04/21 14:03:01 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1991, 1993, The Regents of the University of California.
6 *
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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
24 * its contributors.
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.
28 *
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
39 * SUCH DAMAGE.
40 *
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
43 *
44 *
45 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 * All rights reserved.
47 *
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.
53 *
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.
57 *
58 * Carnegie Mellon requests users of this software to return to
59 *
60 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
61 * School of Computer Science
62 * Carnegie Mellon University
63 * Pittsburgh PA 15213-3890
64 *
65 * any improvements or extensions that they make and grant Carnegie the
66 * rights to redistribute these changes.
67 */
68
69 /*
70 * uvm_map.c: uvm map operations
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: uvm_map.c,v 1.218 2006/04/21 14:03:01 yamt Exp $");
75
76 #include "opt_ddb.h"
77 #include "opt_uvmhist.h"
78 #include "opt_uvm.h"
79 #include "opt_sysv.h"
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/mman.h>
84 #include <sys/proc.h>
85 #include <sys/malloc.h>
86 #include <sys/pool.h>
87 #include <sys/kernel.h>
88 #include <sys/mount.h>
89 #include <sys/vnode.h>
90
91 #ifdef SYSVSHM
92 #include <sys/shm.h>
93 #endif
94
95 #include <uvm/uvm.h>
96 #undef RB_AUGMENT
97 #define RB_AUGMENT(x) uvm_rb_augment(x)
98
99 #ifdef DDB
100 #include <uvm/uvm_ddb.h>
101 #endif
102
103 #if defined(UVMMAP_NOCOUNTERS)
104
105 #define UVMMAP_EVCNT_DEFINE(name) /* nothing */
106 #define UVMMAP_EVCNT_INCR(ev) /* nothing */
107 #define UVMMAP_EVCNT_DECR(ev) /* nothing */
108
109 #else /* defined(UVMMAP_NOCOUNTERS) */
110
111 #include <sys/device.h>
112 #define UVMMAP_EVCNT_DEFINE(name) \
113 struct evcnt uvmmap_evcnt_##name = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, \
114 "uvmmap", #name); \
115 EVCNT_ATTACH_STATIC(uvmmap_evcnt_##name);
116 #define UVMMAP_EVCNT_INCR(ev) uvmmap_evcnt_##ev.ev_count++
117 #define UVMMAP_EVCNT_DECR(ev) uvmmap_evcnt_##ev.ev_count--
118
119 #endif /* defined(UVMMAP_NOCOUNTERS) */
120
121 UVMMAP_EVCNT_DEFINE(ubackmerge)
122 UVMMAP_EVCNT_DEFINE(uforwmerge)
123 UVMMAP_EVCNT_DEFINE(ubimerge)
124 UVMMAP_EVCNT_DEFINE(unomerge)
125 UVMMAP_EVCNT_DEFINE(kbackmerge)
126 UVMMAP_EVCNT_DEFINE(kforwmerge)
127 UVMMAP_EVCNT_DEFINE(kbimerge)
128 UVMMAP_EVCNT_DEFINE(knomerge)
129 UVMMAP_EVCNT_DEFINE(map_call)
130 UVMMAP_EVCNT_DEFINE(mlk_call)
131 UVMMAP_EVCNT_DEFINE(mlk_hint)
132
133 UVMMAP_EVCNT_DEFINE(uke_alloc)
134 UVMMAP_EVCNT_DEFINE(uke_free)
135 UVMMAP_EVCNT_DEFINE(ukh_alloc)
136 UVMMAP_EVCNT_DEFINE(ukh_free)
137
138 const char vmmapbsy[] = "vmmapbsy";
139
140 /*
141 * pool for vmspace structures.
142 */
143
144 POOL_INIT(uvm_vmspace_pool, sizeof(struct vmspace), 0, 0, 0, "vmsppl",
145 &pool_allocator_nointr);
146
147 /*
148 * pool for dynamically-allocated map entries.
149 */
150
151 POOL_INIT(uvm_map_entry_pool, sizeof(struct vm_map_entry), 0, 0, 0, "vmmpepl",
152 &pool_allocator_nointr);
153
154 MALLOC_DEFINE(M_VMMAP, "VM map", "VM map structures");
155 MALLOC_DEFINE(M_VMPMAP, "VM pmap", "VM pmap");
156
157 #ifdef PMAP_GROWKERNEL
158 /*
159 * This global represents the end of the kernel virtual address
160 * space. If we want to exceed this, we must grow the kernel
161 * virtual address space dynamically.
162 *
163 * Note, this variable is locked by kernel_map's lock.
164 */
165 vaddr_t uvm_maxkaddr;
166 #endif
167
168 /*
169 * macros
170 */
171
172 /*
173 * VM_MAP_USE_KMAPENT: determine if uvm_kmapent_alloc/free is used
174 * for the vm_map.
175 */
176 extern struct vm_map *pager_map; /* XXX */
177 #define VM_MAP_USE_KMAPENT(map) \
178 (((map)->flags & VM_MAP_INTRSAFE) || (map) == kernel_map)
179
180 /*
181 * UVM_ET_ISCOMPATIBLE: check some requirements for map entry merging
182 */
183
184 #define UVM_ET_ISCOMPATIBLE(ent, type, uobj, meflags, \
185 prot, maxprot, inh, adv, wire) \
186 ((ent)->etype == (type) && \
187 (((ent)->flags ^ (meflags)) & (UVM_MAP_NOMERGE | UVM_MAP_QUANTUM)) \
188 == 0 && \
189 (ent)->object.uvm_obj == (uobj) && \
190 (ent)->protection == (prot) && \
191 (ent)->max_protection == (maxprot) && \
192 (ent)->inheritance == (inh) && \
193 (ent)->advice == (adv) && \
194 (ent)->wired_count == (wire))
195
196 /*
197 * uvm_map_entry_link: insert entry into a map
198 *
199 * => map must be locked
200 */
201 #define uvm_map_entry_link(map, after_where, entry) do { \
202 uvm_mapent_check(entry); \
203 (map)->nentries++; \
204 (entry)->prev = (after_where); \
205 (entry)->next = (after_where)->next; \
206 (entry)->prev->next = (entry); \
207 (entry)->next->prev = (entry); \
208 uvm_rb_insert((map), (entry)); \
209 } while (/*CONSTCOND*/ 0)
210
211 /*
212 * uvm_map_entry_unlink: remove entry from a map
213 *
214 * => map must be locked
215 */
216 #define uvm_map_entry_unlink(map, entry) do { \
217 uvm_mapent_check(entry); \
218 (map)->nentries--; \
219 (entry)->next->prev = (entry)->prev; \
220 (entry)->prev->next = (entry)->next; \
221 uvm_rb_remove((map), (entry)); \
222 } while (/*CONSTCOND*/ 0)
223
224 /*
225 * SAVE_HINT: saves the specified entry as the hint for future lookups.
226 *
227 * => map need not be locked (protected by hint_lock).
228 */
229 #define SAVE_HINT(map,check,value) do { \
230 simple_lock(&(map)->hint_lock); \
231 if ((map)->hint == (check)) \
232 (map)->hint = (value); \
233 simple_unlock(&(map)->hint_lock); \
234 } while (/*CONSTCOND*/ 0)
235
236 /*
237 * VM_MAP_RANGE_CHECK: check and correct range
238 *
239 * => map must at least be read locked
240 */
241
242 #define VM_MAP_RANGE_CHECK(map, start, end) do { \
243 if (start < vm_map_min(map)) \
244 start = vm_map_min(map); \
245 if (end > vm_map_max(map)) \
246 end = vm_map_max(map); \
247 if (start > end) \
248 start = end; \
249 } while (/*CONSTCOND*/ 0)
250
251 /*
252 * local prototypes
253 */
254
255 static struct vm_map_entry *
256 uvm_mapent_alloc(struct vm_map *, int);
257 static struct vm_map_entry *
258 uvm_mapent_alloc_split(struct vm_map *,
259 const struct vm_map_entry *, int,
260 struct uvm_mapent_reservation *);
261 static void uvm_mapent_copy(struct vm_map_entry *, struct vm_map_entry *);
262 static void uvm_mapent_free(struct vm_map_entry *);
263 #if defined(DEBUG)
264 static void _uvm_mapent_check(const struct vm_map_entry *, const char *,
265 int);
266 #define uvm_mapent_check(map) _uvm_mapent_check(map, __FILE__, __LINE__)
267 #else /* defined(DEBUG) */
268 #define uvm_mapent_check(e) /* nothing */
269 #endif /* defined(DEBUG) */
270 static struct vm_map_entry *
271 uvm_kmapent_alloc(struct vm_map *, int);
272 static void uvm_kmapent_free(struct vm_map_entry *);
273 static void uvm_map_entry_unwire(struct vm_map *, struct vm_map_entry *);
274 static void uvm_map_reference_amap(struct vm_map_entry *, int);
275 static int uvm_map_space_avail(vaddr_t *, vsize_t, voff_t, vsize_t, int,
276 struct vm_map_entry *);
277 static void uvm_map_unreference_amap(struct vm_map_entry *, int);
278
279 int _uvm_tree_sanity(struct vm_map *, const char *);
280 static vsize_t uvm_rb_subtree_space(const struct vm_map_entry *);
281
282 static inline int
283 uvm_compare(const struct vm_map_entry *a, const struct vm_map_entry *b)
284 {
285
286 if (a->start < b->start)
287 return (-1);
288 else if (a->start > b->start)
289 return (1);
290
291 return (0);
292 }
293
294 static inline void
295 uvm_rb_augment(struct vm_map_entry *entry)
296 {
297
298 entry->space = uvm_rb_subtree_space(entry);
299 }
300
301 RB_PROTOTYPE(uvm_tree, vm_map_entry, rb_entry, uvm_compare);
302
303 RB_GENERATE(uvm_tree, vm_map_entry, rb_entry, uvm_compare);
304
305 static inline vsize_t
306 uvm_rb_space(const struct vm_map *map, const struct vm_map_entry *entry)
307 {
308 /* XXX map is not used */
309
310 KASSERT(entry->next != NULL);
311 return entry->next->start - entry->end;
312 }
313
314 static vsize_t
315 uvm_rb_subtree_space(const struct vm_map_entry *entry)
316 {
317 vaddr_t space, tmp;
318
319 space = entry->ownspace;
320 if (RB_LEFT(entry, rb_entry)) {
321 tmp = RB_LEFT(entry, rb_entry)->space;
322 if (tmp > space)
323 space = tmp;
324 }
325
326 if (RB_RIGHT(entry, rb_entry)) {
327 tmp = RB_RIGHT(entry, rb_entry)->space;
328 if (tmp > space)
329 space = tmp;
330 }
331
332 return (space);
333 }
334
335 static inline void
336 uvm_rb_fixup(struct vm_map *map, struct vm_map_entry *entry)
337 {
338 /* We need to traverse to the very top */
339 do {
340 entry->ownspace = uvm_rb_space(map, entry);
341 entry->space = uvm_rb_subtree_space(entry);
342 } while ((entry = RB_PARENT(entry, rb_entry)) != NULL);
343 }
344
345 static void
346 uvm_rb_insert(struct vm_map *map, struct vm_map_entry *entry)
347 {
348 vaddr_t space = uvm_rb_space(map, entry);
349 struct vm_map_entry *tmp;
350
351 entry->ownspace = entry->space = space;
352 tmp = RB_INSERT(uvm_tree, &(map)->rbhead, entry);
353 #ifdef DIAGNOSTIC
354 if (tmp != NULL)
355 panic("uvm_rb_insert: duplicate entry?");
356 #endif
357 uvm_rb_fixup(map, entry);
358 if (entry->prev != &map->header)
359 uvm_rb_fixup(map, entry->prev);
360 }
361
362 static void
363 uvm_rb_remove(struct vm_map *map, struct vm_map_entry *entry)
364 {
365 struct vm_map_entry *parent;
366
367 parent = RB_PARENT(entry, rb_entry);
368 RB_REMOVE(uvm_tree, &(map)->rbhead, entry);
369 if (entry->prev != &map->header)
370 uvm_rb_fixup(map, entry->prev);
371 if (parent)
372 uvm_rb_fixup(map, parent);
373 }
374
375 #ifdef DEBUG
376 int uvm_debug_check_rbtree = 0;
377 #define uvm_tree_sanity(x,y) \
378 if (uvm_debug_check_rbtree) \
379 _uvm_tree_sanity(x,y)
380 #else
381 #define uvm_tree_sanity(x,y)
382 #endif
383
384 int
385 _uvm_tree_sanity(struct vm_map *map, const char *name)
386 {
387 struct vm_map_entry *tmp, *trtmp;
388 int n = 0, i = 1;
389
390 RB_FOREACH(tmp, uvm_tree, &map->rbhead) {
391 if (tmp->ownspace != uvm_rb_space(map, tmp)) {
392 printf("%s: %d/%d ownspace %lx != %lx %s\n",
393 name, n + 1, map->nentries,
394 (ulong)tmp->ownspace, (ulong)uvm_rb_space(map, tmp),
395 tmp->next == &map->header ? "(last)" : "");
396 goto error;
397 }
398 }
399 trtmp = NULL;
400 RB_FOREACH(tmp, uvm_tree, &map->rbhead) {
401 if (tmp->space != uvm_rb_subtree_space(tmp)) {
402 printf("%s: space %lx != %lx\n",
403 name, (ulong)tmp->space,
404 (ulong)uvm_rb_subtree_space(tmp));
405 goto error;
406 }
407 if (trtmp != NULL && trtmp->start >= tmp->start) {
408 printf("%s: corrupt: 0x%lx >= 0x%lx\n",
409 name, trtmp->start, tmp->start);
410 goto error;
411 }
412 n++;
413
414 trtmp = tmp;
415 }
416
417 if (n != map->nentries) {
418 printf("%s: nentries: %d vs %d\n",
419 name, n, map->nentries);
420 goto error;
421 }
422
423 for (tmp = map->header.next; tmp && tmp != &map->header;
424 tmp = tmp->next, i++) {
425 trtmp = RB_FIND(uvm_tree, &map->rbhead, tmp);
426 if (trtmp != tmp) {
427 printf("%s: lookup: %d: %p - %p: %p\n",
428 name, i, tmp, trtmp,
429 RB_PARENT(tmp, rb_entry));
430 goto error;
431 }
432 }
433
434 return (0);
435 error:
436 #if defined(DDB) && __GNUC__ < 4
437 /* handy breakpoint location for error case */
438 __asm(".globl treesanity_label\ntreesanity_label:");
439 #endif
440 return (-1);
441 }
442
443 #ifdef DIAGNOSTIC
444 static struct vm_map *uvm_kmapent_map(struct vm_map_entry *);
445 #endif
446
447 /*
448 * uvm_mapent_alloc: allocate a map entry
449 */
450
451 static struct vm_map_entry *
452 uvm_mapent_alloc(struct vm_map *map, int flags)
453 {
454 struct vm_map_entry *me;
455 int pflags = (flags & UVM_FLAG_NOWAIT) ? PR_NOWAIT : PR_WAITOK;
456 UVMHIST_FUNC("uvm_mapent_alloc"); UVMHIST_CALLED(maphist);
457
458 if (VM_MAP_USE_KMAPENT(map)) {
459 me = uvm_kmapent_alloc(map, flags);
460 } else {
461 me = pool_get(&uvm_map_entry_pool, pflags);
462 if (__predict_false(me == NULL))
463 return NULL;
464 me->flags = 0;
465 }
466
467 UVMHIST_LOG(maphist, "<- new entry=0x%x [kentry=%d]", me,
468 ((map->flags & VM_MAP_INTRSAFE) != 0 || map == kernel_map), 0, 0);
469 return (me);
470 }
471
472 /*
473 * uvm_mapent_alloc_split: allocate a map entry for clipping.
474 */
475
476 static struct vm_map_entry *
477 uvm_mapent_alloc_split(struct vm_map *map,
478 const struct vm_map_entry *old_entry, int flags,
479 struct uvm_mapent_reservation *umr)
480 {
481 struct vm_map_entry *me;
482
483 KASSERT(!VM_MAP_USE_KMAPENT(map) ||
484 (old_entry->flags & UVM_MAP_QUANTUM) || !UMR_EMPTY(umr));
485
486 if (old_entry->flags & UVM_MAP_QUANTUM) {
487 int s;
488 struct vm_map_kernel *vmk = vm_map_to_kernel(map);
489
490 s = splvm();
491 simple_lock(&uvm.kentry_lock);
492 me = vmk->vmk_merged_entries;
493 KASSERT(me);
494 vmk->vmk_merged_entries = me->next;
495 simple_unlock(&uvm.kentry_lock);
496 splx(s);
497 KASSERT(me->flags & UVM_MAP_QUANTUM);
498 } else {
499 me = uvm_mapent_alloc(map, flags);
500 }
501
502 return me;
503 }
504
505 /*
506 * uvm_mapent_free: free map entry
507 */
508
509 static void
510 uvm_mapent_free(struct vm_map_entry *me)
511 {
512 UVMHIST_FUNC("uvm_mapent_free"); UVMHIST_CALLED(maphist);
513
514 UVMHIST_LOG(maphist,"<- freeing map entry=0x%x [flags=%d]",
515 me, me->flags, 0, 0);
516 if (me->flags & UVM_MAP_KERNEL) {
517 uvm_kmapent_free(me);
518 } else {
519 pool_put(&uvm_map_entry_pool, me);
520 }
521 }
522
523 /*
524 * uvm_mapent_free_merged: free merged map entry
525 *
526 * => keep the entry if needed.
527 * => caller shouldn't hold map locked if VM_MAP_USE_KMAPENT(map) is true.
528 */
529
530 static void
531 uvm_mapent_free_merged(struct vm_map *map, struct vm_map_entry *me)
532 {
533
534 KASSERT(!(me->flags & UVM_MAP_KERNEL) || uvm_kmapent_map(me) == map);
535
536 if (me->flags & UVM_MAP_QUANTUM) {
537 /*
538 * keep this entry for later splitting.
539 */
540 struct vm_map_kernel *vmk;
541 int s;
542
543 KASSERT(VM_MAP_IS_KERNEL(map));
544 KASSERT(!VM_MAP_USE_KMAPENT(map) ||
545 (me->flags & UVM_MAP_KERNEL));
546
547 vmk = vm_map_to_kernel(map);
548 s = splvm();
549 simple_lock(&uvm.kentry_lock);
550 me->next = vmk->vmk_merged_entries;
551 vmk->vmk_merged_entries = me;
552 simple_unlock(&uvm.kentry_lock);
553 splx(s);
554 } else {
555 uvm_mapent_free(me);
556 }
557 }
558
559 /*
560 * uvm_mapent_copy: copy a map entry, preserving flags
561 */
562
563 static inline void
564 uvm_mapent_copy(struct vm_map_entry *src, struct vm_map_entry *dst)
565 {
566
567 memcpy(dst, src, ((char *)&src->uvm_map_entry_stop_copy) -
568 ((char *)src));
569 }
570
571 #if defined(DEBUG)
572 static void
573 _uvm_mapent_check(const struct vm_map_entry *entry, const char *file, int line)
574 {
575
576 if (entry->start >= entry->end) {
577 goto bad;
578 }
579 if (UVM_ET_ISOBJ(entry)) {
580 if (entry->object.uvm_obj == NULL) {
581 goto bad;
582 }
583 } else if (UVM_ET_ISSUBMAP(entry)) {
584 if (entry->object.sub_map == NULL) {
585 goto bad;
586 }
587 } else {
588 if (entry->object.uvm_obj != NULL ||
589 entry->object.sub_map != NULL) {
590 goto bad;
591 }
592 }
593 if (!UVM_ET_ISOBJ(entry)) {
594 if (entry->offset != 0) {
595 goto bad;
596 }
597 }
598
599 return;
600
601 bad:
602 panic("%s: bad entry %p (%s:%d)", __func__, entry, file, line);
603 }
604 #endif /* defined(DEBUG) */
605
606 /*
607 * uvm_map_entry_unwire: unwire a map entry
608 *
609 * => map should be locked by caller
610 */
611
612 static inline void
613 uvm_map_entry_unwire(struct vm_map *map, struct vm_map_entry *entry)
614 {
615
616 entry->wired_count = 0;
617 uvm_fault_unwire_locked(map, entry->start, entry->end);
618 }
619
620
621 /*
622 * wrapper for calling amap_ref()
623 */
624 static inline void
625 uvm_map_reference_amap(struct vm_map_entry *entry, int flags)
626 {
627
628 amap_ref(entry->aref.ar_amap, entry->aref.ar_pageoff,
629 (entry->end - entry->start) >> PAGE_SHIFT, flags);
630 }
631
632
633 /*
634 * wrapper for calling amap_unref()
635 */
636 static inline void
637 uvm_map_unreference_amap(struct vm_map_entry *entry, int flags)
638 {
639
640 amap_unref(entry->aref.ar_amap, entry->aref.ar_pageoff,
641 (entry->end - entry->start) >> PAGE_SHIFT, flags);
642 }
643
644
645 /*
646 * uvm_map_init: init mapping system at boot time. note that we allocate
647 * and init the static pool of struct vm_map_entry *'s for the kernel here.
648 */
649
650 void
651 uvm_map_init(void)
652 {
653 #if defined(UVMHIST)
654 static struct uvm_history_ent maphistbuf[100];
655 static struct uvm_history_ent pdhistbuf[100];
656 #endif
657
658 /*
659 * first, init logging system.
660 */
661
662 UVMHIST_FUNC("uvm_map_init");
663 UVMHIST_INIT_STATIC(maphist, maphistbuf);
664 UVMHIST_INIT_STATIC(pdhist, pdhistbuf);
665 UVMHIST_CALLED(maphist);
666 UVMHIST_LOG(maphist,"<starting uvm map system>", 0, 0, 0, 0);
667
668 /*
669 * initialize the global lock for kernel map entry.
670 *
671 * XXX is it worth to have per-map lock instead?
672 */
673
674 simple_lock_init(&uvm.kentry_lock);
675 }
676
677 /*
678 * clippers
679 */
680
681 /*
682 * uvm_mapent_splitadj: adjust map entries for splitting, after uvm_mapent_copy.
683 */
684
685 static void
686 uvm_mapent_splitadj(struct vm_map_entry *entry1, struct vm_map_entry *entry2,
687 vaddr_t splitat)
688 {
689 vaddr_t adj;
690
691 KASSERT(entry1->start < splitat);
692 KASSERT(splitat < entry1->end);
693
694 adj = splitat - entry1->start;
695 entry1->end = entry2->start = splitat;
696
697 if (entry1->aref.ar_amap) {
698 amap_splitref(&entry1->aref, &entry2->aref, adj);
699 }
700 if (UVM_ET_ISSUBMAP(entry1)) {
701 /* ... unlikely to happen, but play it safe */
702 uvm_map_reference(entry1->object.sub_map);
703 } else if (UVM_ET_ISOBJ(entry1)) {
704 KASSERT(entry1->object.uvm_obj != NULL); /* suppress coverity */
705 entry2->offset += adj;
706 if (entry1->object.uvm_obj->pgops &&
707 entry1->object.uvm_obj->pgops->pgo_reference)
708 entry1->object.uvm_obj->pgops->pgo_reference(
709 entry1->object.uvm_obj);
710 }
711 }
712
713 /*
714 * uvm_map_clip_start: ensure that the entry begins at or after
715 * the starting address, if it doesn't we split the entry.
716 *
717 * => caller should use UVM_MAP_CLIP_START macro rather than calling
718 * this directly
719 * => map must be locked by caller
720 */
721
722 void
723 uvm_map_clip_start(struct vm_map *map, struct vm_map_entry *entry,
724 vaddr_t start, struct uvm_mapent_reservation *umr)
725 {
726 struct vm_map_entry *new_entry;
727
728 /* uvm_map_simplify_entry(map, entry); */ /* XXX */
729
730 uvm_tree_sanity(map, "clip_start entry");
731 uvm_mapent_check(entry);
732
733 /*
734 * Split off the front portion. note that we must insert the new
735 * entry BEFORE this one, so that this entry has the specified
736 * starting address.
737 */
738 new_entry = uvm_mapent_alloc_split(map, entry, 0, umr);
739 uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
740 uvm_mapent_splitadj(new_entry, entry, start);
741 uvm_map_entry_link(map, entry->prev, new_entry);
742
743 uvm_tree_sanity(map, "clip_start leave");
744 }
745
746 /*
747 * uvm_map_clip_end: ensure that the entry ends at or before
748 * the ending address, if it does't we split the reference
749 *
750 * => caller should use UVM_MAP_CLIP_END macro rather than calling
751 * this directly
752 * => map must be locked by caller
753 */
754
755 void
756 uvm_map_clip_end(struct vm_map *map, struct vm_map_entry *entry, vaddr_t end,
757 struct uvm_mapent_reservation *umr)
758 {
759 struct vm_map_entry *new_entry;
760
761 uvm_tree_sanity(map, "clip_end entry");
762 uvm_mapent_check(entry);
763
764 /*
765 * Create a new entry and insert it
766 * AFTER the specified entry
767 */
768 new_entry = uvm_mapent_alloc_split(map, entry, 0, umr);
769 uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
770 uvm_mapent_splitadj(entry, new_entry, end);
771 uvm_map_entry_link(map, entry, new_entry);
772
773 uvm_tree_sanity(map, "clip_end leave");
774 }
775
776
777 /*
778 * M A P - m a i n e n t r y p o i n t
779 */
780 /*
781 * uvm_map: establish a valid mapping in a map
782 *
783 * => assume startp is page aligned.
784 * => assume size is a multiple of PAGE_SIZE.
785 * => assume sys_mmap provides enough of a "hint" to have us skip
786 * over text/data/bss area.
787 * => map must be unlocked (we will lock it)
788 * => <uobj,uoffset> value meanings (4 cases):
789 * [1] <NULL,uoffset> == uoffset is a hint for PMAP_PREFER
790 * [2] <NULL,UVM_UNKNOWN_OFFSET> == don't PMAP_PREFER
791 * [3] <uobj,uoffset> == normal mapping
792 * [4] <uobj,UVM_UNKNOWN_OFFSET> == uvm_map finds offset based on VA
793 *
794 * case [4] is for kernel mappings where we don't know the offset until
795 * we've found a virtual address. note that kernel object offsets are
796 * always relative to vm_map_min(kernel_map).
797 *
798 * => if `align' is non-zero, we align the virtual address to the specified
799 * alignment.
800 * this is provided as a mechanism for large pages.
801 *
802 * => XXXCDC: need way to map in external amap?
803 */
804
805 int
806 uvm_map(struct vm_map *map, vaddr_t *startp /* IN/OUT */, vsize_t size,
807 struct uvm_object *uobj, voff_t uoffset, vsize_t align, uvm_flag_t flags)
808 {
809 struct uvm_map_args args;
810 struct vm_map_entry *new_entry;
811 int error;
812
813 KASSERT((flags & UVM_FLAG_QUANTUM) == 0 || VM_MAP_IS_KERNEL(map));
814 KASSERT((size & PAGE_MASK) == 0);
815
816 /*
817 * for pager_map, allocate the new entry first to avoid sleeping
818 * for memory while we have the map locked.
819 *
820 * besides, because we allocates entries for in-kernel maps
821 * a bit differently (cf. uvm_kmapent_alloc/free), we need to
822 * allocate them before locking the map.
823 */
824
825 new_entry = NULL;
826 if (VM_MAP_USE_KMAPENT(map) || (flags & UVM_FLAG_QUANTUM) ||
827 map == pager_map) {
828 new_entry = uvm_mapent_alloc(map, (flags & UVM_FLAG_NOWAIT));
829 if (__predict_false(new_entry == NULL))
830 return ENOMEM;
831 if (flags & UVM_FLAG_QUANTUM)
832 new_entry->flags |= UVM_MAP_QUANTUM;
833 }
834 if (map == pager_map)
835 flags |= UVM_FLAG_NOMERGE;
836
837 error = uvm_map_prepare(map, *startp, size, uobj, uoffset, align,
838 flags, &args);
839 if (!error) {
840 error = uvm_map_enter(map, &args, new_entry);
841 *startp = args.uma_start;
842 } else if (new_entry) {
843 uvm_mapent_free(new_entry);
844 }
845
846 #if defined(DEBUG)
847 if (!error && VM_MAP_IS_KERNEL(map)) {
848 uvm_km_check_empty(*startp, *startp + size,
849 (map->flags & VM_MAP_INTRSAFE) != 0);
850 }
851 #endif /* defined(DEBUG) */
852
853 return error;
854 }
855
856 int
857 uvm_map_prepare(struct vm_map *map, vaddr_t start, vsize_t size,
858 struct uvm_object *uobj, voff_t uoffset, vsize_t align, uvm_flag_t flags,
859 struct uvm_map_args *args)
860 {
861 struct vm_map_entry *prev_entry;
862 vm_prot_t prot = UVM_PROTECTION(flags);
863 vm_prot_t maxprot = UVM_MAXPROTECTION(flags);
864
865 UVMHIST_FUNC("uvm_map_prepare");
866 UVMHIST_CALLED(maphist);
867
868 UVMHIST_LOG(maphist, "(map=0x%x, start=0x%x, size=%d, flags=0x%x)",
869 map, start, size, flags);
870 UVMHIST_LOG(maphist, " uobj/offset 0x%x/%d", uobj, uoffset,0,0);
871
872 /*
873 * detect a popular device driver bug.
874 */
875
876 KASSERT(doing_shutdown || curlwp != NULL ||
877 (map->flags & VM_MAP_INTRSAFE));
878
879 /*
880 * zero-sized mapping doesn't make any sense.
881 */
882 KASSERT(size > 0);
883
884 KASSERT((~flags & (UVM_FLAG_NOWAIT | UVM_FLAG_WAITVA)) != 0);
885
886 uvm_tree_sanity(map, "map entry");
887
888 /*
889 * check sanity of protection code
890 */
891
892 if ((prot & maxprot) != prot) {
893 UVMHIST_LOG(maphist, "<- prot. failure: prot=0x%x, max=0x%x",
894 prot, maxprot,0,0);
895 return EACCES;
896 }
897
898 /*
899 * figure out where to put new VM range
900 */
901
902 retry:
903 if (vm_map_lock_try(map) == FALSE) {
904 if (flags & UVM_FLAG_TRYLOCK) {
905 return EAGAIN;
906 }
907 vm_map_lock(map); /* could sleep here */
908 }
909 if ((prev_entry = uvm_map_findspace(map, start, size, &start,
910 uobj, uoffset, align, flags)) == NULL) {
911 unsigned int timestamp;
912
913 if ((flags & UVM_FLAG_WAITVA) == 0) {
914 UVMHIST_LOG(maphist,"<- uvm_map_findspace failed!",
915 0,0,0,0);
916 vm_map_unlock(map);
917 return ENOMEM;
918 }
919 timestamp = map->timestamp;
920 UVMHIST_LOG(maphist,"waiting va timestamp=0x%x",
921 timestamp,0,0,0);
922 simple_lock(&map->flags_lock);
923 map->flags |= VM_MAP_WANTVA;
924 simple_unlock(&map->flags_lock);
925 vm_map_unlock(map);
926
927 /*
928 * wait until someone does unmap.
929 * XXX fragile locking
930 */
931
932 simple_lock(&map->flags_lock);
933 while ((map->flags & VM_MAP_WANTVA) != 0 &&
934 map->timestamp == timestamp) {
935 ltsleep(&map->header, PVM, "vmmapva", 0,
936 &map->flags_lock);
937 }
938 simple_unlock(&map->flags_lock);
939 goto retry;
940 }
941
942 #ifdef PMAP_GROWKERNEL
943 /*
944 * If the kernel pmap can't map the requested space,
945 * then allocate more resources for it.
946 */
947 if (map == kernel_map && uvm_maxkaddr < (start + size))
948 uvm_maxkaddr = pmap_growkernel(start + size);
949 #endif
950
951 UVMMAP_EVCNT_INCR(map_call);
952
953 /*
954 * if uobj is null, then uoffset is either a VAC hint for PMAP_PREFER
955 * [typically from uvm_map_reserve] or it is UVM_UNKNOWN_OFFSET. in
956 * either case we want to zero it before storing it in the map entry
957 * (because it looks strange and confusing when debugging...)
958 *
959 * if uobj is not null
960 * if uoffset is not UVM_UNKNOWN_OFFSET then we have a normal mapping
961 * and we do not need to change uoffset.
962 * if uoffset is UVM_UNKNOWN_OFFSET then we need to find the offset
963 * now (based on the starting address of the map). this case is
964 * for kernel object mappings where we don't know the offset until
965 * the virtual address is found (with uvm_map_findspace). the
966 * offset is the distance we are from the start of the map.
967 */
968
969 if (uobj == NULL) {
970 uoffset = 0;
971 } else {
972 if (uoffset == UVM_UNKNOWN_OFFSET) {
973 KASSERT(UVM_OBJ_IS_KERN_OBJECT(uobj));
974 uoffset = start - vm_map_min(kernel_map);
975 }
976 }
977
978 args->uma_flags = flags;
979 args->uma_prev = prev_entry;
980 args->uma_start = start;
981 args->uma_size = size;
982 args->uma_uobj = uobj;
983 args->uma_uoffset = uoffset;
984
985 return 0;
986 }
987
988 int
989 uvm_map_enter(struct vm_map *map, const struct uvm_map_args *args,
990 struct vm_map_entry *new_entry)
991 {
992 struct vm_map_entry *prev_entry = args->uma_prev;
993 struct vm_map_entry *dead = NULL;
994
995 const uvm_flag_t flags = args->uma_flags;
996 const vm_prot_t prot = UVM_PROTECTION(flags);
997 const vm_prot_t maxprot = UVM_MAXPROTECTION(flags);
998 const vm_inherit_t inherit = UVM_INHERIT(flags);
999 const int amapwaitflag = (flags & UVM_FLAG_NOWAIT) ?
1000 AMAP_EXTEND_NOWAIT : 0;
1001 const int advice = UVM_ADVICE(flags);
1002 const int meflagval = (flags & UVM_FLAG_QUANTUM) ?
1003 UVM_MAP_QUANTUM : 0;
1004
1005 vaddr_t start = args->uma_start;
1006 vsize_t size = args->uma_size;
1007 struct uvm_object *uobj = args->uma_uobj;
1008 voff_t uoffset = args->uma_uoffset;
1009
1010 const int kmap = (vm_map_pmap(map) == pmap_kernel());
1011 int merged = 0;
1012 int error;
1013 int newetype;
1014
1015 UVMHIST_FUNC("uvm_map_enter");
1016 UVMHIST_CALLED(maphist);
1017
1018 UVMHIST_LOG(maphist, "(map=0x%x, start=0x%x, size=%d, flags=0x%x)",
1019 map, start, size, flags);
1020 UVMHIST_LOG(maphist, " uobj/offset 0x%x/%d", uobj, uoffset,0,0);
1021
1022 if (flags & UVM_FLAG_QUANTUM) {
1023 KASSERT(new_entry);
1024 KASSERT(new_entry->flags & UVM_MAP_QUANTUM);
1025 }
1026
1027 if (uobj)
1028 newetype = UVM_ET_OBJ;
1029 else
1030 newetype = 0;
1031
1032 if (flags & UVM_FLAG_COPYONW) {
1033 newetype |= UVM_ET_COPYONWRITE;
1034 if ((flags & UVM_FLAG_OVERLAY) == 0)
1035 newetype |= UVM_ET_NEEDSCOPY;
1036 }
1037
1038 /*
1039 * try and insert in map by extending previous entry, if possible.
1040 * XXX: we don't try and pull back the next entry. might be useful
1041 * for a stack, but we are currently allocating our stack in advance.
1042 */
1043
1044 if (flags & UVM_FLAG_NOMERGE)
1045 goto nomerge;
1046
1047 if (prev_entry->end == start &&
1048 prev_entry != &map->header &&
1049 UVM_ET_ISCOMPATIBLE(prev_entry, newetype, uobj, meflagval,
1050 prot, maxprot, inherit, advice, 0)) {
1051
1052 if (uobj && prev_entry->offset +
1053 (prev_entry->end - prev_entry->start) != uoffset)
1054 goto forwardmerge;
1055
1056 /*
1057 * can't extend a shared amap. note: no need to lock amap to
1058 * look at refs since we don't care about its exact value.
1059 * if it is one (i.e. we have only reference) it will stay there
1060 */
1061
1062 if (prev_entry->aref.ar_amap &&
1063 amap_refs(prev_entry->aref.ar_amap) != 1) {
1064 goto forwardmerge;
1065 }
1066
1067 if (prev_entry->aref.ar_amap) {
1068 error = amap_extend(prev_entry, size,
1069 amapwaitflag | AMAP_EXTEND_FORWARDS);
1070 if (error)
1071 goto nomerge;
1072 }
1073
1074 if (kmap)
1075 UVMMAP_EVCNT_INCR(kbackmerge);
1076 else
1077 UVMMAP_EVCNT_INCR(ubackmerge);
1078 UVMHIST_LOG(maphist," starting back merge", 0, 0, 0, 0);
1079
1080 /*
1081 * drop our reference to uobj since we are extending a reference
1082 * that we already have (the ref count can not drop to zero).
1083 */
1084
1085 if (uobj && uobj->pgops->pgo_detach)
1086 uobj->pgops->pgo_detach(uobj);
1087
1088 prev_entry->end += size;
1089 uvm_rb_fixup(map, prev_entry);
1090
1091 uvm_tree_sanity(map, "map backmerged");
1092
1093 UVMHIST_LOG(maphist,"<- done (via backmerge)!", 0, 0, 0, 0);
1094 merged++;
1095 }
1096
1097 forwardmerge:
1098 if (prev_entry->next->start == (start + size) &&
1099 prev_entry->next != &map->header &&
1100 UVM_ET_ISCOMPATIBLE(prev_entry->next, newetype, uobj, meflagval,
1101 prot, maxprot, inherit, advice, 0)) {
1102
1103 if (uobj && prev_entry->next->offset != uoffset + size)
1104 goto nomerge;
1105
1106 /*
1107 * can't extend a shared amap. note: no need to lock amap to
1108 * look at refs since we don't care about its exact value.
1109 * if it is one (i.e. we have only reference) it will stay there.
1110 *
1111 * note that we also can't merge two amaps, so if we
1112 * merged with the previous entry which has an amap,
1113 * and the next entry also has an amap, we give up.
1114 *
1115 * Interesting cases:
1116 * amap, new, amap -> give up second merge (single fwd extend)
1117 * amap, new, none -> double forward extend (extend again here)
1118 * none, new, amap -> double backward extend (done here)
1119 * uobj, new, amap -> single backward extend (done here)
1120 *
1121 * XXX should we attempt to deal with someone refilling
1122 * the deallocated region between two entries that are
1123 * backed by the same amap (ie, arefs is 2, "prev" and
1124 * "next" refer to it, and adding this allocation will
1125 * close the hole, thus restoring arefs to 1 and
1126 * deallocating the "next" vm_map_entry)? -- @@@
1127 */
1128
1129 if (prev_entry->next->aref.ar_amap &&
1130 (amap_refs(prev_entry->next->aref.ar_amap) != 1 ||
1131 (merged && prev_entry->aref.ar_amap))) {
1132 goto nomerge;
1133 }
1134
1135 if (merged) {
1136 /*
1137 * Try to extend the amap of the previous entry to
1138 * cover the next entry as well. If it doesn't work
1139 * just skip on, don't actually give up, since we've
1140 * already completed the back merge.
1141 */
1142 if (prev_entry->aref.ar_amap) {
1143 if (amap_extend(prev_entry,
1144 prev_entry->next->end -
1145 prev_entry->next->start,
1146 amapwaitflag | AMAP_EXTEND_FORWARDS))
1147 goto nomerge;
1148 }
1149
1150 /*
1151 * Try to extend the amap of the *next* entry
1152 * back to cover the new allocation *and* the
1153 * previous entry as well (the previous merge
1154 * didn't have an amap already otherwise we
1155 * wouldn't be checking here for an amap). If
1156 * it doesn't work just skip on, again, don't
1157 * actually give up, since we've already
1158 * completed the back merge.
1159 */
1160 else if (prev_entry->next->aref.ar_amap) {
1161 if (amap_extend(prev_entry->next,
1162 prev_entry->end -
1163 prev_entry->start,
1164 amapwaitflag | AMAP_EXTEND_BACKWARDS))
1165 goto nomerge;
1166 }
1167 } else {
1168 /*
1169 * Pull the next entry's amap backwards to cover this
1170 * new allocation.
1171 */
1172 if (prev_entry->next->aref.ar_amap) {
1173 error = amap_extend(prev_entry->next, size,
1174 amapwaitflag | AMAP_EXTEND_BACKWARDS);
1175 if (error)
1176 goto nomerge;
1177 }
1178 }
1179
1180 if (merged) {
1181 if (kmap) {
1182 UVMMAP_EVCNT_DECR(kbackmerge);
1183 UVMMAP_EVCNT_INCR(kbimerge);
1184 } else {
1185 UVMMAP_EVCNT_DECR(ubackmerge);
1186 UVMMAP_EVCNT_INCR(ubimerge);
1187 }
1188 } else {
1189 if (kmap)
1190 UVMMAP_EVCNT_INCR(kforwmerge);
1191 else
1192 UVMMAP_EVCNT_INCR(uforwmerge);
1193 }
1194 UVMHIST_LOG(maphist," starting forward merge", 0, 0, 0, 0);
1195
1196 /*
1197 * drop our reference to uobj since we are extending a reference
1198 * that we already have (the ref count can not drop to zero).
1199 * (if merged, we've already detached)
1200 */
1201 if (uobj && uobj->pgops->pgo_detach && !merged)
1202 uobj->pgops->pgo_detach(uobj);
1203
1204 if (merged) {
1205 dead = prev_entry->next;
1206 prev_entry->end = dead->end;
1207 uvm_map_entry_unlink(map, dead);
1208 if (dead->aref.ar_amap != NULL) {
1209 prev_entry->aref = dead->aref;
1210 dead->aref.ar_amap = NULL;
1211 }
1212 } else {
1213 prev_entry->next->start -= size;
1214 if (prev_entry != &map->header)
1215 uvm_rb_fixup(map, prev_entry);
1216 if (uobj)
1217 prev_entry->next->offset = uoffset;
1218 }
1219
1220 uvm_tree_sanity(map, "map forwardmerged");
1221
1222 UVMHIST_LOG(maphist,"<- done forwardmerge", 0, 0, 0, 0);
1223 merged++;
1224 }
1225
1226 nomerge:
1227 if (!merged) {
1228 UVMHIST_LOG(maphist," allocating new map entry", 0, 0, 0, 0);
1229 if (kmap)
1230 UVMMAP_EVCNT_INCR(knomerge);
1231 else
1232 UVMMAP_EVCNT_INCR(unomerge);
1233
1234 /*
1235 * allocate new entry and link it in.
1236 */
1237
1238 if (new_entry == NULL) {
1239 new_entry = uvm_mapent_alloc(map,
1240 (flags & UVM_FLAG_NOWAIT));
1241 if (__predict_false(new_entry == NULL)) {
1242 error = ENOMEM;
1243 goto done;
1244 }
1245 }
1246 new_entry->start = start;
1247 new_entry->end = new_entry->start + size;
1248 new_entry->object.uvm_obj = uobj;
1249 new_entry->offset = uoffset;
1250
1251 new_entry->etype = newetype;
1252
1253 if (flags & UVM_FLAG_NOMERGE) {
1254 new_entry->flags |= UVM_MAP_NOMERGE;
1255 }
1256
1257 new_entry->protection = prot;
1258 new_entry->max_protection = maxprot;
1259 new_entry->inheritance = inherit;
1260 new_entry->wired_count = 0;
1261 new_entry->advice = advice;
1262 if (flags & UVM_FLAG_OVERLAY) {
1263
1264 /*
1265 * to_add: for BSS we overallocate a little since we
1266 * are likely to extend
1267 */
1268
1269 vaddr_t to_add = (flags & UVM_FLAG_AMAPPAD) ?
1270 UVM_AMAP_CHUNK << PAGE_SHIFT : 0;
1271 struct vm_amap *amap = amap_alloc(size, to_add,
1272 (flags & UVM_FLAG_NOWAIT) ? M_NOWAIT : M_WAITOK);
1273 if (__predict_false(amap == NULL)) {
1274 error = ENOMEM;
1275 goto done;
1276 }
1277 new_entry->aref.ar_pageoff = 0;
1278 new_entry->aref.ar_amap = amap;
1279 } else {
1280 new_entry->aref.ar_pageoff = 0;
1281 new_entry->aref.ar_amap = NULL;
1282 }
1283 uvm_map_entry_link(map, prev_entry, new_entry);
1284
1285 /*
1286 * Update the free space hint
1287 */
1288
1289 if ((map->first_free == prev_entry) &&
1290 (prev_entry->end >= new_entry->start))
1291 map->first_free = new_entry;
1292
1293 new_entry = NULL;
1294 }
1295
1296 map->size += size;
1297
1298 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
1299
1300 error = 0;
1301 done:
1302 vm_map_unlock(map);
1303 if (new_entry) {
1304 if (error == 0) {
1305 KDASSERT(merged);
1306 uvm_mapent_free_merged(map, new_entry);
1307 } else {
1308 uvm_mapent_free(new_entry);
1309 }
1310 }
1311 if (dead) {
1312 KDASSERT(merged);
1313 uvm_mapent_free_merged(map, dead);
1314 }
1315 return error;
1316 }
1317
1318 /*
1319 * uvm_map_lookup_entry: find map entry at or before an address
1320 *
1321 * => map must at least be read-locked by caller
1322 * => entry is returned in "entry"
1323 * => return value is true if address is in the returned entry
1324 */
1325
1326 boolean_t
1327 uvm_map_lookup_entry(struct vm_map *map, vaddr_t address,
1328 struct vm_map_entry **entry /* OUT */)
1329 {
1330 struct vm_map_entry *cur;
1331 boolean_t use_tree = FALSE;
1332 UVMHIST_FUNC("uvm_map_lookup_entry");
1333 UVMHIST_CALLED(maphist);
1334
1335 UVMHIST_LOG(maphist,"(map=0x%x,addr=0x%x,ent=0x%x)",
1336 map, address, entry, 0);
1337
1338 /*
1339 * start looking either from the head of the
1340 * list, or from the hint.
1341 */
1342
1343 simple_lock(&map->hint_lock);
1344 cur = map->hint;
1345 simple_unlock(&map->hint_lock);
1346
1347 if (cur == &map->header)
1348 cur = cur->next;
1349
1350 UVMMAP_EVCNT_INCR(mlk_call);
1351 if (address >= cur->start) {
1352
1353 /*
1354 * go from hint to end of list.
1355 *
1356 * but first, make a quick check to see if
1357 * we are already looking at the entry we
1358 * want (which is usually the case).
1359 * note also that we don't need to save the hint
1360 * here... it is the same hint (unless we are
1361 * at the header, in which case the hint didn't
1362 * buy us anything anyway).
1363 */
1364
1365 if (cur != &map->header && cur->end > address) {
1366 UVMMAP_EVCNT_INCR(mlk_hint);
1367 *entry = cur;
1368 UVMHIST_LOG(maphist,"<- got it via hint (0x%x)",
1369 cur, 0, 0, 0);
1370 uvm_mapent_check(*entry);
1371 return (TRUE);
1372 }
1373
1374 if (map->nentries > 30)
1375 use_tree = TRUE;
1376 } else {
1377
1378 /*
1379 * invalid hint. use tree.
1380 */
1381 use_tree = TRUE;
1382 }
1383
1384 uvm_tree_sanity(map, __func__);
1385
1386 if (use_tree) {
1387 struct vm_map_entry *prev = &map->header;
1388 cur = RB_ROOT(&map->rbhead);
1389
1390 /*
1391 * Simple lookup in the tree. Happens when the hint is
1392 * invalid, or nentries reach a threshold.
1393 */
1394 while (cur) {
1395 if (address >= cur->start) {
1396 if (address < cur->end) {
1397 *entry = cur;
1398 goto got;
1399 }
1400 prev = cur;
1401 cur = RB_RIGHT(cur, rb_entry);
1402 } else
1403 cur = RB_LEFT(cur, rb_entry);
1404 }
1405 *entry = prev;
1406 goto failed;
1407 }
1408
1409 /*
1410 * search linearly
1411 */
1412
1413 while (cur != &map->header) {
1414 if (cur->end > address) {
1415 if (address >= cur->start) {
1416 /*
1417 * save this lookup for future
1418 * hints, and return
1419 */
1420
1421 *entry = cur;
1422 got:
1423 SAVE_HINT(map, map->hint, *entry);
1424 UVMHIST_LOG(maphist,"<- search got it (0x%x)",
1425 cur, 0, 0, 0);
1426 KDASSERT((*entry)->start <= address);
1427 KDASSERT(address < (*entry)->end);
1428 uvm_mapent_check(*entry);
1429 return (TRUE);
1430 }
1431 break;
1432 }
1433 cur = cur->next;
1434 }
1435 *entry = cur->prev;
1436 failed:
1437 SAVE_HINT(map, map->hint, *entry);
1438 UVMHIST_LOG(maphist,"<- failed!",0,0,0,0);
1439 KDASSERT((*entry) == &map->header || (*entry)->end <= address);
1440 KDASSERT((*entry)->next == &map->header ||
1441 address < (*entry)->next->start);
1442 return (FALSE);
1443 }
1444
1445 /*
1446 * See if the range between start and start + length fits in the gap
1447 * entry->next->start and entry->end. Returns 1 if fits, 0 if doesn't
1448 * fit, and -1 address wraps around.
1449 */
1450 static int
1451 uvm_map_space_avail(vaddr_t *start, vsize_t length, voff_t uoffset,
1452 vsize_t align, int topdown, struct vm_map_entry *entry)
1453 {
1454 vaddr_t end;
1455
1456 #ifdef PMAP_PREFER
1457 /*
1458 * push start address forward as needed to avoid VAC alias problems.
1459 * we only do this if a valid offset is specified.
1460 */
1461
1462 if (uoffset != UVM_UNKNOWN_OFFSET)
1463 PMAP_PREFER(uoffset, start, length, topdown);
1464 #endif
1465 if (align != 0) {
1466 if ((*start & (align - 1)) != 0) {
1467 if (topdown)
1468 *start &= ~(align - 1);
1469 else
1470 *start = roundup(*start, align);
1471 }
1472 /*
1473 * XXX Should we PMAP_PREFER() here again?
1474 * eh...i think we're okay
1475 */
1476 }
1477
1478 /*
1479 * Find the end of the proposed new region. Be sure we didn't
1480 * wrap around the address; if so, we lose. Otherwise, if the
1481 * proposed new region fits before the next entry, we win.
1482 */
1483
1484 end = *start + length;
1485 if (end < *start)
1486 return (-1);
1487
1488 if (entry->next->start >= end && *start >= entry->end)
1489 return (1);
1490
1491 return (0);
1492 }
1493
1494 /*
1495 * uvm_map_findspace: find "length" sized space in "map".
1496 *
1497 * => "hint" is a hint about where we want it, unless UVM_FLAG_FIXED is
1498 * set in "flags" (in which case we insist on using "hint").
1499 * => "result" is VA returned
1500 * => uobj/uoffset are to be used to handle VAC alignment, if required
1501 * => if "align" is non-zero, we attempt to align to that value.
1502 * => caller must at least have read-locked map
1503 * => returns NULL on failure, or pointer to prev. map entry if success
1504 * => note this is a cross between the old vm_map_findspace and vm_map_find
1505 */
1506
1507 struct vm_map_entry *
1508 uvm_map_findspace(struct vm_map *map, vaddr_t hint, vsize_t length,
1509 vaddr_t *result /* OUT */, struct uvm_object *uobj, voff_t uoffset,
1510 vsize_t align, int flags)
1511 {
1512 struct vm_map_entry *entry;
1513 struct vm_map_entry *child, *prev, *tmp;
1514 vaddr_t orig_hint;
1515 const int topdown = map->flags & VM_MAP_TOPDOWN;
1516 UVMHIST_FUNC("uvm_map_findspace");
1517 UVMHIST_CALLED(maphist);
1518
1519 UVMHIST_LOG(maphist, "(map=0x%x, hint=0x%x, len=%d, flags=0x%x)",
1520 map, hint, length, flags);
1521 KASSERT((align & (align - 1)) == 0);
1522 KASSERT((flags & UVM_FLAG_FIXED) == 0 || align == 0);
1523
1524 uvm_tree_sanity(map, "map_findspace entry");
1525
1526 /*
1527 * remember the original hint. if we are aligning, then we
1528 * may have to try again with no alignment constraint if
1529 * we fail the first time.
1530 */
1531
1532 orig_hint = hint;
1533 if (hint < vm_map_min(map)) { /* check ranges ... */
1534 if (flags & UVM_FLAG_FIXED) {
1535 UVMHIST_LOG(maphist,"<- VA below map range",0,0,0,0);
1536 return (NULL);
1537 }
1538 hint = vm_map_min(map);
1539 }
1540 if (hint > vm_map_max(map)) {
1541 UVMHIST_LOG(maphist,"<- VA 0x%x > range [0x%x->0x%x]",
1542 hint, vm_map_min(map), vm_map_max(map), 0);
1543 return (NULL);
1544 }
1545
1546 /*
1547 * Look for the first possible address; if there's already
1548 * something at this address, we have to start after it.
1549 */
1550
1551 /*
1552 * @@@: there are four, no, eight cases to consider.
1553 *
1554 * 0: found, fixed, bottom up -> fail
1555 * 1: found, fixed, top down -> fail
1556 * 2: found, not fixed, bottom up -> start after entry->end,
1557 * loop up
1558 * 3: found, not fixed, top down -> start before entry->start,
1559 * loop down
1560 * 4: not found, fixed, bottom up -> check entry->next->start, fail
1561 * 5: not found, fixed, top down -> check entry->next->start, fail
1562 * 6: not found, not fixed, bottom up -> check entry->next->start,
1563 * loop up
1564 * 7: not found, not fixed, top down -> check entry->next->start,
1565 * loop down
1566 *
1567 * as you can see, it reduces to roughly five cases, and that
1568 * adding top down mapping only adds one unique case (without
1569 * it, there would be four cases).
1570 */
1571
1572 if ((flags & UVM_FLAG_FIXED) == 0 && hint == vm_map_min(map)) {
1573 entry = map->first_free;
1574 } else {
1575 if (uvm_map_lookup_entry(map, hint, &entry)) {
1576 /* "hint" address already in use ... */
1577 if (flags & UVM_FLAG_FIXED) {
1578 UVMHIST_LOG(maphist, "<- fixed & VA in use",
1579 0, 0, 0, 0);
1580 return (NULL);
1581 }
1582 if (topdown)
1583 /* Start from lower gap. */
1584 entry = entry->prev;
1585 } else if (flags & UVM_FLAG_FIXED) {
1586 if (entry->next->start >= hint + length &&
1587 hint + length > hint)
1588 goto found;
1589
1590 /* "hint" address is gap but too small */
1591 UVMHIST_LOG(maphist, "<- fixed mapping failed",
1592 0, 0, 0, 0);
1593 return (NULL); /* only one shot at it ... */
1594 } else {
1595 /*
1596 * See if given hint fits in this gap.
1597 */
1598 switch (uvm_map_space_avail(&hint, length,
1599 uoffset, align, topdown, entry)) {
1600 case 1:
1601 goto found;
1602 case -1:
1603 goto wraparound;
1604 }
1605
1606 if (topdown) {
1607 /*
1608 * Still there is a chance to fit
1609 * if hint > entry->end.
1610 */
1611 } else {
1612 /* Start from higher gap. */
1613 entry = entry->next;
1614 if (entry == &map->header)
1615 goto notfound;
1616 goto nextgap;
1617 }
1618 }
1619 }
1620
1621 /*
1622 * Note that all UVM_FLAGS_FIXED case is already handled.
1623 */
1624 KDASSERT((flags & UVM_FLAG_FIXED) == 0);
1625
1626 /* Try to find the space in the red-black tree */
1627
1628 /* Check slot before any entry */
1629 hint = topdown ? entry->next->start - length : entry->end;
1630 switch (uvm_map_space_avail(&hint, length, uoffset, align,
1631 topdown, entry)) {
1632 case 1:
1633 goto found;
1634 case -1:
1635 goto wraparound;
1636 }
1637
1638 nextgap:
1639 KDASSERT((flags & UVM_FLAG_FIXED) == 0);
1640 /* If there is not enough space in the whole tree, we fail */
1641 tmp = RB_ROOT(&map->rbhead);
1642 if (tmp == NULL || tmp->space < length)
1643 goto notfound;
1644
1645 prev = NULL; /* previous candidate */
1646
1647 /* Find an entry close to hint that has enough space */
1648 for (; tmp;) {
1649 KASSERT(tmp->next->start == tmp->end + tmp->ownspace);
1650 if (topdown) {
1651 if (tmp->next->start < hint + length &&
1652 (prev == NULL || tmp->end > prev->end)) {
1653 if (tmp->ownspace >= length)
1654 prev = tmp;
1655 else if ((child = RB_LEFT(tmp, rb_entry))
1656 != NULL && child->space >= length)
1657 prev = tmp;
1658 }
1659 } else {
1660 if (tmp->end >= hint &&
1661 (prev == NULL || tmp->end < prev->end)) {
1662 if (tmp->ownspace >= length)
1663 prev = tmp;
1664 else if ((child = RB_RIGHT(tmp, rb_entry))
1665 != NULL && child->space >= length)
1666 prev = tmp;
1667 }
1668 }
1669 if (tmp->next->start < hint + length)
1670 child = RB_RIGHT(tmp, rb_entry);
1671 else if (tmp->end > hint)
1672 child = RB_LEFT(tmp, rb_entry);
1673 else {
1674 if (tmp->ownspace >= length)
1675 break;
1676 if (topdown)
1677 child = RB_LEFT(tmp, rb_entry);
1678 else
1679 child = RB_RIGHT(tmp, rb_entry);
1680 }
1681 if (child == NULL || child->space < length)
1682 break;
1683 tmp = child;
1684 }
1685
1686 if (tmp != NULL && tmp->start < hint && hint < tmp->next->start) {
1687 /*
1688 * Check if the entry that we found satifies the
1689 * space requirement
1690 */
1691 if (topdown) {
1692 if (hint > tmp->next->start - length)
1693 hint = tmp->next->start - length;
1694 } else {
1695 if (hint < tmp->end)
1696 hint = tmp->end;
1697 }
1698 switch (uvm_map_space_avail(&hint, length, uoffset, align,
1699 topdown, tmp)) {
1700 case 1:
1701 entry = tmp;
1702 goto found;
1703 case -1:
1704 goto wraparound;
1705 }
1706 if (tmp->ownspace >= length)
1707 goto listsearch;
1708 }
1709 if (prev == NULL)
1710 goto notfound;
1711
1712 if (topdown) {
1713 KASSERT(orig_hint >= prev->next->start - length ||
1714 prev->next->start - length > prev->next->start);
1715 hint = prev->next->start - length;
1716 } else {
1717 KASSERT(orig_hint <= prev->end);
1718 hint = prev->end;
1719 }
1720 switch (uvm_map_space_avail(&hint, length, uoffset, align,
1721 topdown, prev)) {
1722 case 1:
1723 entry = prev;
1724 goto found;
1725 case -1:
1726 goto wraparound;
1727 }
1728 if (prev->ownspace >= length)
1729 goto listsearch;
1730
1731 if (topdown)
1732 tmp = RB_LEFT(prev, rb_entry);
1733 else
1734 tmp = RB_RIGHT(prev, rb_entry);
1735 for (;;) {
1736 KASSERT(tmp && tmp->space >= length);
1737 if (topdown)
1738 child = RB_RIGHT(tmp, rb_entry);
1739 else
1740 child = RB_LEFT(tmp, rb_entry);
1741 if (child && child->space >= length) {
1742 tmp = child;
1743 continue;
1744 }
1745 if (tmp->ownspace >= length)
1746 break;
1747 if (topdown)
1748 tmp = RB_LEFT(tmp, rb_entry);
1749 else
1750 tmp = RB_RIGHT(tmp, rb_entry);
1751 }
1752
1753 if (topdown) {
1754 KASSERT(orig_hint >= tmp->next->start - length ||
1755 tmp->next->start - length > tmp->next->start);
1756 hint = tmp->next->start - length;
1757 } else {
1758 KASSERT(orig_hint <= tmp->end);
1759 hint = tmp->end;
1760 }
1761 switch (uvm_map_space_avail(&hint, length, uoffset, align,
1762 topdown, tmp)) {
1763 case 1:
1764 entry = tmp;
1765 goto found;
1766 case -1:
1767 goto wraparound;
1768 }
1769
1770 /*
1771 * The tree fails to find an entry because of offset or alignment
1772 * restrictions. Search the list instead.
1773 */
1774 listsearch:
1775 /*
1776 * Look through the rest of the map, trying to fit a new region in
1777 * the gap between existing regions, or after the very last region.
1778 * note: entry->end = base VA of current gap,
1779 * entry->next->start = VA of end of current gap
1780 */
1781
1782 for (;;) {
1783 /* Update hint for current gap. */
1784 hint = topdown ? entry->next->start - length : entry->end;
1785
1786 /* See if it fits. */
1787 switch (uvm_map_space_avail(&hint, length, uoffset, align,
1788 topdown, entry)) {
1789 case 1:
1790 goto found;
1791 case -1:
1792 goto wraparound;
1793 }
1794
1795 /* Advance to next/previous gap */
1796 if (topdown) {
1797 if (entry == &map->header) {
1798 UVMHIST_LOG(maphist, "<- failed (off start)",
1799 0,0,0,0);
1800 goto notfound;
1801 }
1802 entry = entry->prev;
1803 } else {
1804 entry = entry->next;
1805 if (entry == &map->header) {
1806 UVMHIST_LOG(maphist, "<- failed (off end)",
1807 0,0,0,0);
1808 goto notfound;
1809 }
1810 }
1811 }
1812
1813 found:
1814 SAVE_HINT(map, map->hint, entry);
1815 *result = hint;
1816 UVMHIST_LOG(maphist,"<- got it! (result=0x%x)", hint, 0,0,0);
1817 KASSERT( topdown || hint >= orig_hint);
1818 KASSERT(!topdown || hint <= orig_hint);
1819 KASSERT(entry->end <= hint);
1820 KASSERT(hint + length <= entry->next->start);
1821 return (entry);
1822
1823 wraparound:
1824 UVMHIST_LOG(maphist, "<- failed (wrap around)", 0,0,0,0);
1825
1826 return (NULL);
1827
1828 notfound:
1829 UVMHIST_LOG(maphist, "<- failed (notfound)", 0,0,0,0);
1830
1831 return (NULL);
1832 }
1833
1834 /*
1835 * U N M A P - m a i n h e l p e r f u n c t i o n s
1836 */
1837
1838 /*
1839 * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop")
1840 *
1841 * => caller must check alignment and size
1842 * => map must be locked by caller
1843 * => we return a list of map entries that we've remove from the map
1844 * in "entry_list"
1845 */
1846
1847 void
1848 uvm_unmap_remove(struct vm_map *map, vaddr_t start, vaddr_t end,
1849 struct vm_map_entry **entry_list /* OUT */,
1850 struct uvm_mapent_reservation *umr, int flags)
1851 {
1852 struct vm_map_entry *entry, *first_entry, *next;
1853 vaddr_t len;
1854 UVMHIST_FUNC("uvm_unmap_remove"); UVMHIST_CALLED(maphist);
1855
1856 UVMHIST_LOG(maphist,"(map=0x%x, start=0x%x, end=0x%x)",
1857 map, start, end, 0);
1858 VM_MAP_RANGE_CHECK(map, start, end);
1859
1860 uvm_tree_sanity(map, "unmap_remove entry");
1861
1862 /*
1863 * find first entry
1864 */
1865
1866 if (uvm_map_lookup_entry(map, start, &first_entry) == TRUE) {
1867 /* clip and go... */
1868 entry = first_entry;
1869 UVM_MAP_CLIP_START(map, entry, start, umr);
1870 /* critical! prevents stale hint */
1871 SAVE_HINT(map, entry, entry->prev);
1872 } else {
1873 entry = first_entry->next;
1874 }
1875
1876 /*
1877 * Save the free space hint
1878 */
1879
1880 if (map->first_free->start >= start)
1881 map->first_free = entry->prev;
1882
1883 /*
1884 * note: we now re-use first_entry for a different task. we remove
1885 * a number of map entries from the map and save them in a linked
1886 * list headed by "first_entry". once we remove them from the map
1887 * the caller should unlock the map and drop the references to the
1888 * backing objects [c.f. uvm_unmap_detach]. the object is to
1889 * separate unmapping from reference dropping. why?
1890 * [1] the map has to be locked for unmapping
1891 * [2] the map need not be locked for reference dropping
1892 * [3] dropping references may trigger pager I/O, and if we hit
1893 * a pager that does synchronous I/O we may have to wait for it.
1894 * [4] we would like all waiting for I/O to occur with maps unlocked
1895 * so that we don't block other threads.
1896 */
1897
1898 first_entry = NULL;
1899 *entry_list = NULL;
1900
1901 /*
1902 * break up the area into map entry sized regions and unmap. note
1903 * that all mappings have to be removed before we can even consider
1904 * dropping references to amaps or VM objects (otherwise we could end
1905 * up with a mapping to a page on the free list which would be very bad)
1906 */
1907
1908 while ((entry != &map->header) && (entry->start < end)) {
1909 KASSERT((entry->flags & UVM_MAP_FIRST) == 0);
1910
1911 UVM_MAP_CLIP_END(map, entry, end, umr);
1912 next = entry->next;
1913 len = entry->end - entry->start;
1914
1915 /*
1916 * unwire before removing addresses from the pmap; otherwise
1917 * unwiring will put the entries back into the pmap (XXX).
1918 */
1919
1920 if (VM_MAPENT_ISWIRED(entry)) {
1921 uvm_map_entry_unwire(map, entry);
1922 }
1923 if (flags & UVM_FLAG_VAONLY) {
1924
1925 /* nothing */
1926
1927 } else if ((map->flags & VM_MAP_PAGEABLE) == 0) {
1928
1929 /*
1930 * if the map is non-pageable, any pages mapped there
1931 * must be wired and entered with pmap_kenter_pa(),
1932 * and we should free any such pages immediately.
1933 * this is mostly used for kmem_map and mb_map.
1934 */
1935
1936 if ((entry->flags & UVM_MAP_KMAPENT) == 0) {
1937 uvm_km_pgremove_intrsafe(entry->start,
1938 entry->end);
1939 pmap_kremove(entry->start, len);
1940 }
1941 } else if (UVM_ET_ISOBJ(entry) &&
1942 UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)) {
1943 KASSERT(vm_map_pmap(map) == pmap_kernel());
1944
1945 /*
1946 * note: kernel object mappings are currently used in
1947 * two ways:
1948 * [1] "normal" mappings of pages in the kernel object
1949 * [2] uvm_km_valloc'd allocations in which we
1950 * pmap_enter in some non-kernel-object page
1951 * (e.g. vmapbuf).
1952 *
1953 * for case [1], we need to remove the mapping from
1954 * the pmap and then remove the page from the kernel
1955 * object (because, once pages in a kernel object are
1956 * unmapped they are no longer needed, unlike, say,
1957 * a vnode where you might want the data to persist
1958 * until flushed out of a queue).
1959 *
1960 * for case [2], we need to remove the mapping from
1961 * the pmap. there shouldn't be any pages at the
1962 * specified offset in the kernel object [but it
1963 * doesn't hurt to call uvm_km_pgremove just to be
1964 * safe?]
1965 *
1966 * uvm_km_pgremove currently does the following:
1967 * for pages in the kernel object in range:
1968 * - drops the swap slot
1969 * - uvm_pagefree the page
1970 */
1971
1972 /*
1973 * remove mappings from pmap and drop the pages
1974 * from the object. offsets are always relative
1975 * to vm_map_min(kernel_map).
1976 */
1977
1978 pmap_remove(pmap_kernel(), entry->start,
1979 entry->start + len);
1980 uvm_km_pgremove(entry->start, entry->end);
1981
1982 /*
1983 * null out kernel_object reference, we've just
1984 * dropped it
1985 */
1986
1987 entry->etype &= ~UVM_ET_OBJ;
1988 entry->object.uvm_obj = NULL;
1989 } else if (UVM_ET_ISOBJ(entry) || entry->aref.ar_amap) {
1990
1991 /*
1992 * remove mappings the standard way.
1993 */
1994
1995 pmap_remove(map->pmap, entry->start, entry->end);
1996 }
1997
1998 #if defined(DEBUG)
1999 if ((entry->flags & UVM_MAP_KMAPENT) == 0) {
2000
2001 /*
2002 * check if there's remaining mapping,
2003 * which is a bug in caller.
2004 */
2005
2006 vaddr_t va;
2007 for (va = entry->start; va < entry->end;
2008 va += PAGE_SIZE) {
2009 if (pmap_extract(vm_map_pmap(map), va, NULL)) {
2010 panic("uvm_unmap_remove: has mapping");
2011 }
2012 }
2013
2014 if (VM_MAP_IS_KERNEL(map)) {
2015 uvm_km_check_empty(entry->start, entry->end,
2016 (map->flags & VM_MAP_INTRSAFE) != 0);
2017 }
2018 }
2019 #endif /* defined(DEBUG) */
2020
2021 /*
2022 * remove entry from map and put it on our list of entries
2023 * that we've nuked. then go to next entry.
2024 */
2025
2026 UVMHIST_LOG(maphist, " removed map entry 0x%x", entry, 0, 0,0);
2027
2028 /* critical! prevents stale hint */
2029 SAVE_HINT(map, entry, entry->prev);
2030
2031 uvm_map_entry_unlink(map, entry);
2032 KASSERT(map->size >= len);
2033 map->size -= len;
2034 entry->prev = NULL;
2035 entry->next = first_entry;
2036 first_entry = entry;
2037 entry = next;
2038 }
2039 if ((map->flags & VM_MAP_DYING) == 0) {
2040 pmap_update(vm_map_pmap(map));
2041 }
2042
2043 uvm_tree_sanity(map, "unmap_remove leave");
2044
2045 /*
2046 * now we've cleaned up the map and are ready for the caller to drop
2047 * references to the mapped objects.
2048 */
2049
2050 *entry_list = first_entry;
2051 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
2052
2053 simple_lock(&map->flags_lock);
2054 if (map->flags & VM_MAP_WANTVA) {
2055 map->flags &= ~VM_MAP_WANTVA;
2056 wakeup(&map->header);
2057 }
2058 simple_unlock(&map->flags_lock);
2059 }
2060
2061 /*
2062 * uvm_unmap_detach: drop references in a chain of map entries
2063 *
2064 * => we will free the map entries as we traverse the list.
2065 */
2066
2067 void
2068 uvm_unmap_detach(struct vm_map_entry *first_entry, int flags)
2069 {
2070 struct vm_map_entry *next_entry;
2071 UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist);
2072
2073 while (first_entry) {
2074 KASSERT(!VM_MAPENT_ISWIRED(first_entry));
2075 UVMHIST_LOG(maphist,
2076 " detach 0x%x: amap=0x%x, obj=0x%x, submap?=%d",
2077 first_entry, first_entry->aref.ar_amap,
2078 first_entry->object.uvm_obj,
2079 UVM_ET_ISSUBMAP(first_entry));
2080
2081 /*
2082 * drop reference to amap, if we've got one
2083 */
2084
2085 if (first_entry->aref.ar_amap)
2086 uvm_map_unreference_amap(first_entry, flags);
2087
2088 /*
2089 * drop reference to our backing object, if we've got one
2090 */
2091
2092 KASSERT(!UVM_ET_ISSUBMAP(first_entry));
2093 if (UVM_ET_ISOBJ(first_entry) &&
2094 first_entry->object.uvm_obj->pgops->pgo_detach) {
2095 (*first_entry->object.uvm_obj->pgops->pgo_detach)
2096 (first_entry->object.uvm_obj);
2097 }
2098 next_entry = first_entry->next;
2099 uvm_mapent_free(first_entry);
2100 first_entry = next_entry;
2101 }
2102 UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
2103 }
2104
2105 /*
2106 * E X T R A C T I O N F U N C T I O N S
2107 */
2108
2109 /*
2110 * uvm_map_reserve: reserve space in a vm_map for future use.
2111 *
2112 * => we reserve space in a map by putting a dummy map entry in the
2113 * map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE)
2114 * => map should be unlocked (we will write lock it)
2115 * => we return true if we were able to reserve space
2116 * => XXXCDC: should be inline?
2117 */
2118
2119 int
2120 uvm_map_reserve(struct vm_map *map, vsize_t size,
2121 vaddr_t offset /* hint for pmap_prefer */,
2122 vsize_t align /* alignment hint */,
2123 vaddr_t *raddr /* IN:hint, OUT: reserved VA */,
2124 uvm_flag_t flags /* UVM_FLAG_FIXED or 0 */)
2125 {
2126 UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist);
2127
2128 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x, offset=0x%x,addr=0x%x)",
2129 map,size,offset,raddr);
2130
2131 size = round_page(size);
2132
2133 /*
2134 * reserve some virtual space.
2135 */
2136
2137 if (uvm_map(map, raddr, size, NULL, offset, 0,
2138 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
2139 UVM_ADV_RANDOM, UVM_FLAG_NOMERGE|flags)) != 0) {
2140 UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
2141 return (FALSE);
2142 }
2143
2144 UVMHIST_LOG(maphist, "<- done (*raddr=0x%x)", *raddr,0,0,0);
2145 return (TRUE);
2146 }
2147
2148 /*
2149 * uvm_map_replace: replace a reserved (blank) area of memory with
2150 * real mappings.
2151 *
2152 * => caller must WRITE-LOCK the map
2153 * => we return TRUE if replacement was a success
2154 * => we expect the newents chain to have nnewents entrys on it and
2155 * we expect newents->prev to point to the last entry on the list
2156 * => note newents is allowed to be NULL
2157 */
2158
2159 int
2160 uvm_map_replace(struct vm_map *map, vaddr_t start, vaddr_t end,
2161 struct vm_map_entry *newents, int nnewents)
2162 {
2163 struct vm_map_entry *oldent, *last;
2164
2165 uvm_tree_sanity(map, "map_replace entry");
2166
2167 /*
2168 * first find the blank map entry at the specified address
2169 */
2170
2171 if (!uvm_map_lookup_entry(map, start, &oldent)) {
2172 return (FALSE);
2173 }
2174
2175 /*
2176 * check to make sure we have a proper blank entry
2177 */
2178
2179 if (end < oldent->end && !VM_MAP_USE_KMAPENT(map)) {
2180 UVM_MAP_CLIP_END(map, oldent, end, NULL);
2181 }
2182 if (oldent->start != start || oldent->end != end ||
2183 oldent->object.uvm_obj != NULL || oldent->aref.ar_amap != NULL) {
2184 return (FALSE);
2185 }
2186
2187 #ifdef DIAGNOSTIC
2188
2189 /*
2190 * sanity check the newents chain
2191 */
2192
2193 {
2194 struct vm_map_entry *tmpent = newents;
2195 int nent = 0;
2196 vaddr_t cur = start;
2197
2198 while (tmpent) {
2199 nent++;
2200 if (tmpent->start < cur)
2201 panic("uvm_map_replace1");
2202 if (tmpent->start > tmpent->end || tmpent->end > end) {
2203 printf("tmpent->start=0x%lx, tmpent->end=0x%lx, end=0x%lx\n",
2204 tmpent->start, tmpent->end, end);
2205 panic("uvm_map_replace2");
2206 }
2207 cur = tmpent->end;
2208 if (tmpent->next) {
2209 if (tmpent->next->prev != tmpent)
2210 panic("uvm_map_replace3");
2211 } else {
2212 if (newents->prev != tmpent)
2213 panic("uvm_map_replace4");
2214 }
2215 tmpent = tmpent->next;
2216 }
2217 if (nent != nnewents)
2218 panic("uvm_map_replace5");
2219 }
2220 #endif
2221
2222 /*
2223 * map entry is a valid blank! replace it. (this does all the
2224 * work of map entry link/unlink...).
2225 */
2226
2227 if (newents) {
2228 last = newents->prev;
2229
2230 /* critical: flush stale hints out of map */
2231 SAVE_HINT(map, map->hint, newents);
2232 if (map->first_free == oldent)
2233 map->first_free = last;
2234
2235 last->next = oldent->next;
2236 last->next->prev = last;
2237
2238 /* Fix RB tree */
2239 uvm_rb_remove(map, oldent);
2240
2241 newents->prev = oldent->prev;
2242 newents->prev->next = newents;
2243 map->nentries = map->nentries + (nnewents - 1);
2244
2245 /* Fixup the RB tree */
2246 {
2247 int i;
2248 struct vm_map_entry *tmp;
2249
2250 tmp = newents;
2251 for (i = 0; i < nnewents && tmp; i++) {
2252 uvm_rb_insert(map, tmp);
2253 tmp = tmp->next;
2254 }
2255 }
2256 } else {
2257
2258 /* critical: flush stale hints out of map */
2259 SAVE_HINT(map, map->hint, oldent->prev);
2260 if (map->first_free == oldent)
2261 map->first_free = oldent->prev;
2262
2263 /* NULL list of new entries: just remove the old one */
2264 uvm_map_entry_unlink(map, oldent);
2265 }
2266
2267 uvm_tree_sanity(map, "map_replace leave");
2268
2269 /*
2270 * now we can free the old blank entry and return.
2271 */
2272
2273 uvm_mapent_free(oldent);
2274 return (TRUE);
2275 }
2276
2277 /*
2278 * uvm_map_extract: extract a mapping from a map and put it somewhere
2279 * (maybe removing the old mapping)
2280 *
2281 * => maps should be unlocked (we will write lock them)
2282 * => returns 0 on success, error code otherwise
2283 * => start must be page aligned
2284 * => len must be page sized
2285 * => flags:
2286 * UVM_EXTRACT_REMOVE: remove mappings from srcmap
2287 * UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only)
2288 * UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs
2289 * UVM_EXTRACT_FIXPROT: set prot to maxprot as we go
2290 * >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<<
2291 * >>>NOTE: QREF's must be unmapped via the QREF path, thus should only
2292 * be used from within the kernel in a kernel level map <<<
2293 */
2294
2295 int
2296 uvm_map_extract(struct vm_map *srcmap, vaddr_t start, vsize_t len,
2297 struct vm_map *dstmap, vaddr_t *dstaddrp, int flags)
2298 {
2299 vaddr_t dstaddr, end, newend, oldoffset, fudge, orig_fudge;
2300 struct vm_map_entry *chain, *endchain, *entry, *orig_entry, *newentry,
2301 *deadentry, *oldentry;
2302 vsize_t elen;
2303 int nchain, error, copy_ok;
2304 UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist);
2305
2306 UVMHIST_LOG(maphist,"(srcmap=0x%x,start=0x%x, len=0x%x", srcmap, start,
2307 len,0);
2308 UVMHIST_LOG(maphist," ...,dstmap=0x%x, flags=0x%x)", dstmap,flags,0,0);
2309
2310 uvm_tree_sanity(srcmap, "map_extract src enter");
2311 uvm_tree_sanity(dstmap, "map_extract dst enter");
2312
2313 /*
2314 * step 0: sanity check: start must be on a page boundary, length
2315 * must be page sized. can't ask for CONTIG/QREF if you asked for
2316 * REMOVE.
2317 */
2318
2319 KASSERT((start & PAGE_MASK) == 0 && (len & PAGE_MASK) == 0);
2320 KASSERT((flags & UVM_EXTRACT_REMOVE) == 0 ||
2321 (flags & (UVM_EXTRACT_CONTIG|UVM_EXTRACT_QREF)) == 0);
2322
2323 /*
2324 * step 1: reserve space in the target map for the extracted area
2325 */
2326
2327 if ((flags & UVM_EXTRACT_RESERVED) == 0) {
2328 dstaddr = vm_map_min(dstmap);
2329 if (!uvm_map_reserve(dstmap, len, start, 0, &dstaddr, 0))
2330 return (ENOMEM);
2331 *dstaddrp = dstaddr; /* pass address back to caller */
2332 UVMHIST_LOG(maphist, " dstaddr=0x%x", dstaddr,0,0,0);
2333 } else {
2334 dstaddr = *dstaddrp;
2335 }
2336
2337 /*
2338 * step 2: setup for the extraction process loop by init'ing the
2339 * map entry chain, locking src map, and looking up the first useful
2340 * entry in the map.
2341 */
2342
2343 end = start + len;
2344 newend = dstaddr + len;
2345 chain = endchain = NULL;
2346 nchain = 0;
2347 vm_map_lock(srcmap);
2348
2349 if (uvm_map_lookup_entry(srcmap, start, &entry)) {
2350
2351 /* "start" is within an entry */
2352 if (flags & UVM_EXTRACT_QREF) {
2353
2354 /*
2355 * for quick references we don't clip the entry, so
2356 * the entry may map space "before" the starting
2357 * virtual address... this is the "fudge" factor
2358 * (which can be non-zero only the first time
2359 * through the "while" loop in step 3).
2360 */
2361
2362 fudge = start - entry->start;
2363 } else {
2364
2365 /*
2366 * normal reference: we clip the map to fit (thus
2367 * fudge is zero)
2368 */
2369
2370 UVM_MAP_CLIP_START(srcmap, entry, start, NULL);
2371 SAVE_HINT(srcmap, srcmap->hint, entry->prev);
2372 fudge = 0;
2373 }
2374 } else {
2375
2376 /* "start" is not within an entry ... skip to next entry */
2377 if (flags & UVM_EXTRACT_CONTIG) {
2378 error = EINVAL;
2379 goto bad; /* definite hole here ... */
2380 }
2381
2382 entry = entry->next;
2383 fudge = 0;
2384 }
2385
2386 /* save values from srcmap for step 6 */
2387 orig_entry = entry;
2388 orig_fudge = fudge;
2389
2390 /*
2391 * step 3: now start looping through the map entries, extracting
2392 * as we go.
2393 */
2394
2395 while (entry->start < end && entry != &srcmap->header) {
2396
2397 /* if we are not doing a quick reference, clip it */
2398 if ((flags & UVM_EXTRACT_QREF) == 0)
2399 UVM_MAP_CLIP_END(srcmap, entry, end, NULL);
2400
2401 /* clear needs_copy (allow chunking) */
2402 if (UVM_ET_ISNEEDSCOPY(entry)) {
2403 amap_copy(srcmap, entry,
2404 AMAP_COPY_NOWAIT|AMAP_COPY_NOMERGE, start, end);
2405 if (UVM_ET_ISNEEDSCOPY(entry)) { /* failed? */
2406 error = ENOMEM;
2407 goto bad;
2408 }
2409
2410 /* amap_copy could clip (during chunk)! update fudge */
2411 if (fudge) {
2412 fudge = start - entry->start;
2413 orig_fudge = fudge;
2414 }
2415 }
2416
2417 /* calculate the offset of this from "start" */
2418 oldoffset = (entry->start + fudge) - start;
2419
2420 /* allocate a new map entry */
2421 newentry = uvm_mapent_alloc(dstmap, 0);
2422 if (newentry == NULL) {
2423 error = ENOMEM;
2424 goto bad;
2425 }
2426
2427 /* set up new map entry */
2428 newentry->next = NULL;
2429 newentry->prev = endchain;
2430 newentry->start = dstaddr + oldoffset;
2431 newentry->end =
2432 newentry->start + (entry->end - (entry->start + fudge));
2433 if (newentry->end > newend || newentry->end < newentry->start)
2434 newentry->end = newend;
2435 newentry->object.uvm_obj = entry->object.uvm_obj;
2436 if (newentry->object.uvm_obj) {
2437 if (newentry->object.uvm_obj->pgops->pgo_reference)
2438 newentry->object.uvm_obj->pgops->
2439 pgo_reference(newentry->object.uvm_obj);
2440 newentry->offset = entry->offset + fudge;
2441 } else {
2442 newentry->offset = 0;
2443 }
2444 newentry->etype = entry->etype;
2445 newentry->protection = (flags & UVM_EXTRACT_FIXPROT) ?
2446 entry->max_protection : entry->protection;
2447 newentry->max_protection = entry->max_protection;
2448 newentry->inheritance = entry->inheritance;
2449 newentry->wired_count = 0;
2450 newentry->aref.ar_amap = entry->aref.ar_amap;
2451 if (newentry->aref.ar_amap) {
2452 newentry->aref.ar_pageoff =
2453 entry->aref.ar_pageoff + (fudge >> PAGE_SHIFT);
2454 uvm_map_reference_amap(newentry, AMAP_SHARED |
2455 ((flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0));
2456 } else {
2457 newentry->aref.ar_pageoff = 0;
2458 }
2459 newentry->advice = entry->advice;
2460
2461 /* now link it on the chain */
2462 nchain++;
2463 if (endchain == NULL) {
2464 chain = endchain = newentry;
2465 } else {
2466 endchain->next = newentry;
2467 endchain = newentry;
2468 }
2469
2470 /* end of 'while' loop! */
2471 if ((flags & UVM_EXTRACT_CONTIG) && entry->end < end &&
2472 (entry->next == &srcmap->header ||
2473 entry->next->start != entry->end)) {
2474 error = EINVAL;
2475 goto bad;
2476 }
2477 entry = entry->next;
2478 fudge = 0;
2479 }
2480
2481 /*
2482 * step 4: close off chain (in format expected by uvm_map_replace)
2483 */
2484
2485 if (chain)
2486 chain->prev = endchain;
2487
2488 /*
2489 * step 5: attempt to lock the dest map so we can pmap_copy.
2490 * note usage of copy_ok:
2491 * 1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5)
2492 * 0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7
2493 */
2494
2495 if (srcmap == dstmap || vm_map_lock_try(dstmap) == TRUE) {
2496 copy_ok = 1;
2497 if (!uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
2498 nchain)) {
2499 if (srcmap != dstmap)
2500 vm_map_unlock(dstmap);
2501 error = EIO;
2502 goto bad;
2503 }
2504 } else {
2505 copy_ok = 0;
2506 /* replace defered until step 7 */
2507 }
2508
2509 /*
2510 * step 6: traverse the srcmap a second time to do the following:
2511 * - if we got a lock on the dstmap do pmap_copy
2512 * - if UVM_EXTRACT_REMOVE remove the entries
2513 * we make use of orig_entry and orig_fudge (saved in step 2)
2514 */
2515
2516 if (copy_ok || (flags & UVM_EXTRACT_REMOVE)) {
2517
2518 /* purge possible stale hints from srcmap */
2519 if (flags & UVM_EXTRACT_REMOVE) {
2520 SAVE_HINT(srcmap, srcmap->hint, orig_entry->prev);
2521 if (srcmap->first_free->start >= start)
2522 srcmap->first_free = orig_entry->prev;
2523 }
2524
2525 entry = orig_entry;
2526 fudge = orig_fudge;
2527 deadentry = NULL; /* for UVM_EXTRACT_REMOVE */
2528
2529 while (entry->start < end && entry != &srcmap->header) {
2530 if (copy_ok) {
2531 oldoffset = (entry->start + fudge) - start;
2532 elen = MIN(end, entry->end) -
2533 (entry->start + fudge);
2534 pmap_copy(dstmap->pmap, srcmap->pmap,
2535 dstaddr + oldoffset, elen,
2536 entry->start + fudge);
2537 }
2538
2539 /* we advance "entry" in the following if statement */
2540 if (flags & UVM_EXTRACT_REMOVE) {
2541 pmap_remove(srcmap->pmap, entry->start,
2542 entry->end);
2543 oldentry = entry; /* save entry */
2544 entry = entry->next; /* advance */
2545 uvm_map_entry_unlink(srcmap, oldentry);
2546 /* add to dead list */
2547 oldentry->next = deadentry;
2548 deadentry = oldentry;
2549 } else {
2550 entry = entry->next; /* advance */
2551 }
2552
2553 /* end of 'while' loop */
2554 fudge = 0;
2555 }
2556 pmap_update(srcmap->pmap);
2557
2558 /*
2559 * unlock dstmap. we will dispose of deadentry in
2560 * step 7 if needed
2561 */
2562
2563 if (copy_ok && srcmap != dstmap)
2564 vm_map_unlock(dstmap);
2565
2566 } else {
2567 deadentry = NULL;
2568 }
2569
2570 /*
2571 * step 7: we are done with the source map, unlock. if copy_ok
2572 * is 0 then we have not replaced the dummy mapping in dstmap yet
2573 * and we need to do so now.
2574 */
2575
2576 vm_map_unlock(srcmap);
2577 if ((flags & UVM_EXTRACT_REMOVE) && deadentry)
2578 uvm_unmap_detach(deadentry, 0); /* dispose of old entries */
2579
2580 /* now do the replacement if we didn't do it in step 5 */
2581 if (copy_ok == 0) {
2582 vm_map_lock(dstmap);
2583 error = uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
2584 nchain);
2585 vm_map_unlock(dstmap);
2586
2587 if (error == FALSE) {
2588 error = EIO;
2589 goto bad2;
2590 }
2591 }
2592
2593 uvm_tree_sanity(srcmap, "map_extract src leave");
2594 uvm_tree_sanity(dstmap, "map_extract dst leave");
2595
2596 return (0);
2597
2598 /*
2599 * bad: failure recovery
2600 */
2601 bad:
2602 vm_map_unlock(srcmap);
2603 bad2: /* src already unlocked */
2604 if (chain)
2605 uvm_unmap_detach(chain,
2606 (flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0);
2607
2608 uvm_tree_sanity(srcmap, "map_extract src err leave");
2609 uvm_tree_sanity(dstmap, "map_extract dst err leave");
2610
2611 if ((flags & UVM_EXTRACT_RESERVED) == 0) {
2612 uvm_unmap(dstmap, dstaddr, dstaddr+len); /* ??? */
2613 }
2614 return (error);
2615 }
2616
2617 /* end of extraction functions */
2618
2619 /*
2620 * uvm_map_submap: punch down part of a map into a submap
2621 *
2622 * => only the kernel_map is allowed to be submapped
2623 * => the purpose of submapping is to break up the locking granularity
2624 * of a larger map
2625 * => the range specified must have been mapped previously with a uvm_map()
2626 * call [with uobj==NULL] to create a blank map entry in the main map.
2627 * [And it had better still be blank!]
2628 * => maps which contain submaps should never be copied or forked.
2629 * => to remove a submap, use uvm_unmap() on the main map
2630 * and then uvm_map_deallocate() the submap.
2631 * => main map must be unlocked.
2632 * => submap must have been init'd and have a zero reference count.
2633 * [need not be locked as we don't actually reference it]
2634 */
2635
2636 int
2637 uvm_map_submap(struct vm_map *map, vaddr_t start, vaddr_t end,
2638 struct vm_map *submap)
2639 {
2640 struct vm_map_entry *entry;
2641 struct uvm_mapent_reservation umr;
2642 int error;
2643
2644 uvm_mapent_reserve(map, &umr, 2, 0);
2645
2646 vm_map_lock(map);
2647 VM_MAP_RANGE_CHECK(map, start, end);
2648
2649 if (uvm_map_lookup_entry(map, start, &entry)) {
2650 UVM_MAP_CLIP_START(map, entry, start, &umr);
2651 UVM_MAP_CLIP_END(map, entry, end, &umr); /* to be safe */
2652 } else {
2653 entry = NULL;
2654 }
2655
2656 if (entry != NULL &&
2657 entry->start == start && entry->end == end &&
2658 entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL &&
2659 !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) {
2660 entry->etype |= UVM_ET_SUBMAP;
2661 entry->object.sub_map = submap;
2662 entry->offset = 0;
2663 uvm_map_reference(submap);
2664 error = 0;
2665 } else {
2666 error = EINVAL;
2667 }
2668 vm_map_unlock(map);
2669
2670 uvm_mapent_unreserve(map, &umr);
2671
2672 return error;
2673 }
2674
2675 /*
2676 * uvm_map_setup_kernel: init in-kernel map
2677 *
2678 * => map must not be in service yet.
2679 */
2680
2681 void
2682 uvm_map_setup_kernel(struct vm_map_kernel *map,
2683 vaddr_t vmin, vaddr_t vmax, int flags)
2684 {
2685
2686 uvm_map_setup(&map->vmk_map, vmin, vmax, flags);
2687
2688 LIST_INIT(&map->vmk_kentry_free);
2689 map->vmk_merged_entries = NULL;
2690 }
2691
2692
2693 /*
2694 * uvm_map_protect: change map protection
2695 *
2696 * => set_max means set max_protection.
2697 * => map must be unlocked.
2698 */
2699
2700 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
2701 ~VM_PROT_WRITE : VM_PROT_ALL)
2702
2703 int
2704 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
2705 vm_prot_t new_prot, boolean_t set_max)
2706 {
2707 struct vm_map_entry *current, *entry;
2708 int error = 0;
2709 UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist);
2710 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_prot=0x%x)",
2711 map, start, end, new_prot);
2712
2713 vm_map_lock(map);
2714 VM_MAP_RANGE_CHECK(map, start, end);
2715 if (uvm_map_lookup_entry(map, start, &entry)) {
2716 UVM_MAP_CLIP_START(map, entry, start, NULL);
2717 } else {
2718 entry = entry->next;
2719 }
2720
2721 /*
2722 * make a first pass to check for protection violations.
2723 */
2724
2725 current = entry;
2726 while ((current != &map->header) && (current->start < end)) {
2727 if (UVM_ET_ISSUBMAP(current)) {
2728 error = EINVAL;
2729 goto out;
2730 }
2731 if ((new_prot & current->max_protection) != new_prot) {
2732 error = EACCES;
2733 goto out;
2734 }
2735 /*
2736 * Don't allow VM_PROT_EXECUTE to be set on entries that
2737 * point to vnodes that are associated with a NOEXEC file
2738 * system.
2739 */
2740 if (UVM_ET_ISOBJ(current) &&
2741 UVM_OBJ_IS_VNODE(current->object.uvm_obj)) {
2742 struct vnode *vp =
2743 (struct vnode *) current->object.uvm_obj;
2744
2745 if ((new_prot & VM_PROT_EXECUTE) != 0 &&
2746 (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
2747 error = EACCES;
2748 goto out;
2749 }
2750 }
2751 current = current->next;
2752 }
2753
2754 /* go back and fix up protections (no need to clip this time). */
2755
2756 current = entry;
2757 while ((current != &map->header) && (current->start < end)) {
2758 vm_prot_t old_prot;
2759
2760 UVM_MAP_CLIP_END(map, current, end, NULL);
2761 old_prot = current->protection;
2762 if (set_max)
2763 current->protection =
2764 (current->max_protection = new_prot) & old_prot;
2765 else
2766 current->protection = new_prot;
2767
2768 /*
2769 * update physical map if necessary. worry about copy-on-write
2770 * here -- CHECK THIS XXX
2771 */
2772
2773 if (current->protection != old_prot) {
2774 /* update pmap! */
2775 pmap_protect(map->pmap, current->start, current->end,
2776 current->protection & MASK(entry));
2777
2778 /*
2779 * If this entry points at a vnode, and the
2780 * protection includes VM_PROT_EXECUTE, mark
2781 * the vnode as VEXECMAP.
2782 */
2783 if (UVM_ET_ISOBJ(current)) {
2784 struct uvm_object *uobj =
2785 current->object.uvm_obj;
2786
2787 if (UVM_OBJ_IS_VNODE(uobj) &&
2788 (current->protection & VM_PROT_EXECUTE))
2789 vn_markexec((struct vnode *) uobj);
2790 }
2791 }
2792
2793 /*
2794 * If the map is configured to lock any future mappings,
2795 * wire this entry now if the old protection was VM_PROT_NONE
2796 * and the new protection is not VM_PROT_NONE.
2797 */
2798
2799 if ((map->flags & VM_MAP_WIREFUTURE) != 0 &&
2800 VM_MAPENT_ISWIRED(entry) == 0 &&
2801 old_prot == VM_PROT_NONE &&
2802 new_prot != VM_PROT_NONE) {
2803 if (uvm_map_pageable(map, entry->start,
2804 entry->end, FALSE,
2805 UVM_LK_ENTER|UVM_LK_EXIT) != 0) {
2806
2807 /*
2808 * If locking the entry fails, remember the
2809 * error if it's the first one. Note we
2810 * still continue setting the protection in
2811 * the map, but will return the error
2812 * condition regardless.
2813 *
2814 * XXX Ignore what the actual error is,
2815 * XXX just call it a resource shortage
2816 * XXX so that it doesn't get confused
2817 * XXX what uvm_map_protect() itself would
2818 * XXX normally return.
2819 */
2820
2821 error = ENOMEM;
2822 }
2823 }
2824 current = current->next;
2825 }
2826 pmap_update(map->pmap);
2827
2828 out:
2829 vm_map_unlock(map);
2830
2831 UVMHIST_LOG(maphist, "<- done, error=%d",error,0,0,0);
2832 return error;
2833 }
2834
2835 #undef MASK
2836
2837 /*
2838 * uvm_map_inherit: set inheritance code for range of addrs in map.
2839 *
2840 * => map must be unlocked
2841 * => note that the inherit code is used during a "fork". see fork
2842 * code for details.
2843 */
2844
2845 int
2846 uvm_map_inherit(struct vm_map *map, vaddr_t start, vaddr_t end,
2847 vm_inherit_t new_inheritance)
2848 {
2849 struct vm_map_entry *entry, *temp_entry;
2850 UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist);
2851 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_inh=0x%x)",
2852 map, start, end, new_inheritance);
2853
2854 switch (new_inheritance) {
2855 case MAP_INHERIT_NONE:
2856 case MAP_INHERIT_COPY:
2857 case MAP_INHERIT_SHARE:
2858 break;
2859 default:
2860 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
2861 return EINVAL;
2862 }
2863
2864 vm_map_lock(map);
2865 VM_MAP_RANGE_CHECK(map, start, end);
2866 if (uvm_map_lookup_entry(map, start, &temp_entry)) {
2867 entry = temp_entry;
2868 UVM_MAP_CLIP_START(map, entry, start, NULL);
2869 } else {
2870 entry = temp_entry->next;
2871 }
2872 while ((entry != &map->header) && (entry->start < end)) {
2873 UVM_MAP_CLIP_END(map, entry, end, NULL);
2874 entry->inheritance = new_inheritance;
2875 entry = entry->next;
2876 }
2877 vm_map_unlock(map);
2878 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
2879 return 0;
2880 }
2881
2882 /*
2883 * uvm_map_advice: set advice code for range of addrs in map.
2884 *
2885 * => map must be unlocked
2886 */
2887
2888 int
2889 uvm_map_advice(struct vm_map *map, vaddr_t start, vaddr_t end, int new_advice)
2890 {
2891 struct vm_map_entry *entry, *temp_entry;
2892 UVMHIST_FUNC("uvm_map_advice"); UVMHIST_CALLED(maphist);
2893 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_adv=0x%x)",
2894 map, start, end, new_advice);
2895
2896 vm_map_lock(map);
2897 VM_MAP_RANGE_CHECK(map, start, end);
2898 if (uvm_map_lookup_entry(map, start, &temp_entry)) {
2899 entry = temp_entry;
2900 UVM_MAP_CLIP_START(map, entry, start, NULL);
2901 } else {
2902 entry = temp_entry->next;
2903 }
2904
2905 /*
2906 * XXXJRT: disallow holes?
2907 */
2908
2909 while ((entry != &map->header) && (entry->start < end)) {
2910 UVM_MAP_CLIP_END(map, entry, end, NULL);
2911
2912 switch (new_advice) {
2913 case MADV_NORMAL:
2914 case MADV_RANDOM:
2915 case MADV_SEQUENTIAL:
2916 /* nothing special here */
2917 break;
2918
2919 default:
2920 vm_map_unlock(map);
2921 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
2922 return EINVAL;
2923 }
2924 entry->advice = new_advice;
2925 entry = entry->next;
2926 }
2927
2928 vm_map_unlock(map);
2929 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
2930 return 0;
2931 }
2932
2933 /*
2934 * uvm_map_pageable: sets the pageability of a range in a map.
2935 *
2936 * => wires map entries. should not be used for transient page locking.
2937 * for that, use uvm_fault_wire()/uvm_fault_unwire() (see uvm_vslock()).
2938 * => regions specified as not pageable require lock-down (wired) memory
2939 * and page tables.
2940 * => map must never be read-locked
2941 * => if islocked is TRUE, map is already write-locked
2942 * => we always unlock the map, since we must downgrade to a read-lock
2943 * to call uvm_fault_wire()
2944 * => XXXCDC: check this and try and clean it up.
2945 */
2946
2947 int
2948 uvm_map_pageable(struct vm_map *map, vaddr_t start, vaddr_t end,
2949 boolean_t new_pageable, int lockflags)
2950 {
2951 struct vm_map_entry *entry, *start_entry, *failed_entry;
2952 int rv;
2953 #ifdef DIAGNOSTIC
2954 u_int timestamp_save;
2955 #endif
2956 UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist);
2957 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_pageable=0x%x)",
2958 map, start, end, new_pageable);
2959 KASSERT(map->flags & VM_MAP_PAGEABLE);
2960
2961 if ((lockflags & UVM_LK_ENTER) == 0)
2962 vm_map_lock(map);
2963 VM_MAP_RANGE_CHECK(map, start, end);
2964
2965 /*
2966 * only one pageability change may take place at one time, since
2967 * uvm_fault_wire assumes it will be called only once for each
2968 * wiring/unwiring. therefore, we have to make sure we're actually
2969 * changing the pageability for the entire region. we do so before
2970 * making any changes.
2971 */
2972
2973 if (uvm_map_lookup_entry(map, start, &start_entry) == FALSE) {
2974 if ((lockflags & UVM_LK_EXIT) == 0)
2975 vm_map_unlock(map);
2976
2977 UVMHIST_LOG(maphist,"<- done (fault)",0,0,0,0);
2978 return EFAULT;
2979 }
2980 entry = start_entry;
2981
2982 /*
2983 * handle wiring and unwiring separately.
2984 */
2985
2986 if (new_pageable) { /* unwire */
2987 UVM_MAP_CLIP_START(map, entry, start, NULL);
2988
2989 /*
2990 * unwiring. first ensure that the range to be unwired is
2991 * really wired down and that there are no holes.
2992 */
2993
2994 while ((entry != &map->header) && (entry->start < end)) {
2995 if (entry->wired_count == 0 ||
2996 (entry->end < end &&
2997 (entry->next == &map->header ||
2998 entry->next->start > entry->end))) {
2999 if ((lockflags & UVM_LK_EXIT) == 0)
3000 vm_map_unlock(map);
3001 UVMHIST_LOG(maphist, "<- done (INVAL)",0,0,0,0);
3002 return EINVAL;
3003 }
3004 entry = entry->next;
3005 }
3006
3007 /*
3008 * POSIX 1003.1b - a single munlock call unlocks a region,
3009 * regardless of the number of mlock calls made on that
3010 * region.
3011 */
3012
3013 entry = start_entry;
3014 while ((entry != &map->header) && (entry->start < end)) {
3015 UVM_MAP_CLIP_END(map, entry, end, NULL);
3016 if (VM_MAPENT_ISWIRED(entry))
3017 uvm_map_entry_unwire(map, entry);
3018 entry = entry->next;
3019 }
3020 if ((lockflags & UVM_LK_EXIT) == 0)
3021 vm_map_unlock(map);
3022 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
3023 return 0;
3024 }
3025
3026 /*
3027 * wire case: in two passes [XXXCDC: ugly block of code here]
3028 *
3029 * 1: holding the write lock, we create any anonymous maps that need
3030 * to be created. then we clip each map entry to the region to
3031 * be wired and increment its wiring count.
3032 *
3033 * 2: we downgrade to a read lock, and call uvm_fault_wire to fault
3034 * in the pages for any newly wired area (wired_count == 1).
3035 *
3036 * downgrading to a read lock for uvm_fault_wire avoids a possible
3037 * deadlock with another thread that may have faulted on one of
3038 * the pages to be wired (it would mark the page busy, blocking
3039 * us, then in turn block on the map lock that we hold). because
3040 * of problems in the recursive lock package, we cannot upgrade
3041 * to a write lock in vm_map_lookup. thus, any actions that
3042 * require the write lock must be done beforehand. because we
3043 * keep the read lock on the map, the copy-on-write status of the
3044 * entries we modify here cannot change.
3045 */
3046
3047 while ((entry != &map->header) && (entry->start < end)) {
3048 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3049
3050 /*
3051 * perform actions of vm_map_lookup that need the
3052 * write lock on the map: create an anonymous map
3053 * for a copy-on-write region, or an anonymous map
3054 * for a zero-fill region. (XXXCDC: submap case
3055 * ok?)
3056 */
3057
3058 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */
3059 if (UVM_ET_ISNEEDSCOPY(entry) &&
3060 ((entry->max_protection & VM_PROT_WRITE) ||
3061 (entry->object.uvm_obj == NULL))) {
3062 amap_copy(map, entry, 0, start, end);
3063 /* XXXCDC: wait OK? */
3064 }
3065 }
3066 }
3067 UVM_MAP_CLIP_START(map, entry, start, NULL);
3068 UVM_MAP_CLIP_END(map, entry, end, NULL);
3069 entry->wired_count++;
3070
3071 /*
3072 * Check for holes
3073 */
3074
3075 if (entry->protection == VM_PROT_NONE ||
3076 (entry->end < end &&
3077 (entry->next == &map->header ||
3078 entry->next->start > entry->end))) {
3079
3080 /*
3081 * found one. amap creation actions do not need to
3082 * be undone, but the wired counts need to be restored.
3083 */
3084
3085 while (entry != &map->header && entry->end > start) {
3086 entry->wired_count--;
3087 entry = entry->prev;
3088 }
3089 if ((lockflags & UVM_LK_EXIT) == 0)
3090 vm_map_unlock(map);
3091 UVMHIST_LOG(maphist,"<- done (INVALID WIRE)",0,0,0,0);
3092 return EINVAL;
3093 }
3094 entry = entry->next;
3095 }
3096
3097 /*
3098 * Pass 2.
3099 */
3100
3101 #ifdef DIAGNOSTIC
3102 timestamp_save = map->timestamp;
3103 #endif
3104 vm_map_busy(map);
3105 vm_map_downgrade(map);
3106
3107 rv = 0;
3108 entry = start_entry;
3109 while (entry != &map->header && entry->start < end) {
3110 if (entry->wired_count == 1) {
3111 rv = uvm_fault_wire(map, entry->start, entry->end,
3112 entry->max_protection, 1);
3113 if (rv) {
3114
3115 /*
3116 * wiring failed. break out of the loop.
3117 * we'll clean up the map below, once we
3118 * have a write lock again.
3119 */
3120
3121 break;
3122 }
3123 }
3124 entry = entry->next;
3125 }
3126
3127 if (rv) { /* failed? */
3128
3129 /*
3130 * Get back to an exclusive (write) lock.
3131 */
3132
3133 vm_map_upgrade(map);
3134 vm_map_unbusy(map);
3135
3136 #ifdef DIAGNOSTIC
3137 if (timestamp_save != map->timestamp)
3138 panic("uvm_map_pageable: stale map");
3139 #endif
3140
3141 /*
3142 * first drop the wiring count on all the entries
3143 * which haven't actually been wired yet.
3144 */
3145
3146 failed_entry = entry;
3147 while (entry != &map->header && entry->start < end) {
3148 entry->wired_count--;
3149 entry = entry->next;
3150 }
3151
3152 /*
3153 * now, unwire all the entries that were successfully
3154 * wired above.
3155 */
3156
3157 entry = start_entry;
3158 while (entry != failed_entry) {
3159 entry->wired_count--;
3160 if (VM_MAPENT_ISWIRED(entry) == 0)
3161 uvm_map_entry_unwire(map, entry);
3162 entry = entry->next;
3163 }
3164 if ((lockflags & UVM_LK_EXIT) == 0)
3165 vm_map_unlock(map);
3166 UVMHIST_LOG(maphist, "<- done (RV=%d)", rv,0,0,0);
3167 return (rv);
3168 }
3169
3170 /* We are holding a read lock here. */
3171 if ((lockflags & UVM_LK_EXIT) == 0) {
3172 vm_map_unbusy(map);
3173 vm_map_unlock_read(map);
3174 } else {
3175
3176 /*
3177 * Get back to an exclusive (write) lock.
3178 */
3179
3180 vm_map_upgrade(map);
3181 vm_map_unbusy(map);
3182 }
3183
3184 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
3185 return 0;
3186 }
3187
3188 /*
3189 * uvm_map_pageable_all: special case of uvm_map_pageable - affects
3190 * all mapped regions.
3191 *
3192 * => map must not be locked.
3193 * => if no flags are specified, all regions are unwired.
3194 * => XXXJRT: has some of the same problems as uvm_map_pageable() above.
3195 */
3196
3197 int
3198 uvm_map_pageable_all(struct vm_map *map, int flags, vsize_t limit)
3199 {
3200 struct vm_map_entry *entry, *failed_entry;
3201 vsize_t size;
3202 int rv;
3203 #ifdef DIAGNOSTIC
3204 u_int timestamp_save;
3205 #endif
3206 UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist);
3207 UVMHIST_LOG(maphist,"(map=0x%x,flags=0x%x)", map, flags, 0, 0);
3208
3209 KASSERT(map->flags & VM_MAP_PAGEABLE);
3210
3211 vm_map_lock(map);
3212
3213 /*
3214 * handle wiring and unwiring separately.
3215 */
3216
3217 if (flags == 0) { /* unwire */
3218
3219 /*
3220 * POSIX 1003.1b -- munlockall unlocks all regions,
3221 * regardless of how many times mlockall has been called.
3222 */
3223
3224 for (entry = map->header.next; entry != &map->header;
3225 entry = entry->next) {
3226 if (VM_MAPENT_ISWIRED(entry))
3227 uvm_map_entry_unwire(map, entry);
3228 }
3229 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);
3230 vm_map_unlock(map);
3231 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
3232 return 0;
3233 }
3234
3235 if (flags & MCL_FUTURE) {
3236
3237 /*
3238 * must wire all future mappings; remember this.
3239 */
3240
3241 vm_map_modflags(map, VM_MAP_WIREFUTURE, 0);
3242 }
3243
3244 if ((flags & MCL_CURRENT) == 0) {
3245
3246 /*
3247 * no more work to do!
3248 */
3249
3250 UVMHIST_LOG(maphist,"<- done (OK no wire)",0,0,0,0);
3251 vm_map_unlock(map);
3252 return 0;
3253 }
3254
3255 /*
3256 * wire case: in three passes [XXXCDC: ugly block of code here]
3257 *
3258 * 1: holding the write lock, count all pages mapped by non-wired
3259 * entries. if this would cause us to go over our limit, we fail.
3260 *
3261 * 2: still holding the write lock, we create any anonymous maps that
3262 * need to be created. then we increment its wiring count.
3263 *
3264 * 3: we downgrade to a read lock, and call uvm_fault_wire to fault
3265 * in the pages for any newly wired area (wired_count == 1).
3266 *
3267 * downgrading to a read lock for uvm_fault_wire avoids a possible
3268 * deadlock with another thread that may have faulted on one of
3269 * the pages to be wired (it would mark the page busy, blocking
3270 * us, then in turn block on the map lock that we hold). because
3271 * of problems in the recursive lock package, we cannot upgrade
3272 * to a write lock in vm_map_lookup. thus, any actions that
3273 * require the write lock must be done beforehand. because we
3274 * keep the read lock on the map, the copy-on-write status of the
3275 * entries we modify here cannot change.
3276 */
3277
3278 for (size = 0, entry = map->header.next; entry != &map->header;
3279 entry = entry->next) {
3280 if (entry->protection != VM_PROT_NONE &&
3281 VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3282 size += entry->end - entry->start;
3283 }
3284 }
3285
3286 if (atop(size) + uvmexp.wired > uvmexp.wiredmax) {
3287 vm_map_unlock(map);
3288 return ENOMEM;
3289 }
3290
3291 if (limit != 0 &&
3292 (size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit)) {
3293 vm_map_unlock(map);
3294 return ENOMEM;
3295 }
3296
3297 /*
3298 * Pass 2.
3299 */
3300
3301 for (entry = map->header.next; entry != &map->header;
3302 entry = entry->next) {
3303 if (entry->protection == VM_PROT_NONE)
3304 continue;
3305 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3306
3307 /*
3308 * perform actions of vm_map_lookup that need the
3309 * write lock on the map: create an anonymous map
3310 * for a copy-on-write region, or an anonymous map
3311 * for a zero-fill region. (XXXCDC: submap case
3312 * ok?)
3313 */
3314
3315 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */
3316 if (UVM_ET_ISNEEDSCOPY(entry) &&
3317 ((entry->max_protection & VM_PROT_WRITE) ||
3318 (entry->object.uvm_obj == NULL))) {
3319 amap_copy(map, entry, 0, entry->start,
3320 entry->end);
3321 /* XXXCDC: wait OK? */
3322 }
3323 }
3324 }
3325 entry->wired_count++;
3326 }
3327
3328 /*
3329 * Pass 3.
3330 */
3331
3332 #ifdef DIAGNOSTIC
3333 timestamp_save = map->timestamp;
3334 #endif
3335 vm_map_busy(map);
3336 vm_map_downgrade(map);
3337
3338 rv = 0;
3339 for (entry = map->header.next; entry != &map->header;
3340 entry = entry->next) {
3341 if (entry->wired_count == 1) {
3342 rv = uvm_fault_wire(map, entry->start, entry->end,
3343 entry->max_protection, 1);
3344 if (rv) {
3345
3346 /*
3347 * wiring failed. break out of the loop.
3348 * we'll clean up the map below, once we
3349 * have a write lock again.
3350 */
3351
3352 break;
3353 }
3354 }
3355 }
3356
3357 if (rv) {
3358
3359 /*
3360 * Get back an exclusive (write) lock.
3361 */
3362
3363 vm_map_upgrade(map);
3364 vm_map_unbusy(map);
3365
3366 #ifdef DIAGNOSTIC
3367 if (timestamp_save != map->timestamp)
3368 panic("uvm_map_pageable_all: stale map");
3369 #endif
3370
3371 /*
3372 * first drop the wiring count on all the entries
3373 * which haven't actually been wired yet.
3374 *
3375 * Skip VM_PROT_NONE entries like we did above.
3376 */
3377
3378 failed_entry = entry;
3379 for (/* nothing */; entry != &map->header;
3380 entry = entry->next) {
3381 if (entry->protection == VM_PROT_NONE)
3382 continue;
3383 entry->wired_count--;
3384 }
3385
3386 /*
3387 * now, unwire all the entries that were successfully
3388 * wired above.
3389 *
3390 * Skip VM_PROT_NONE entries like we did above.
3391 */
3392
3393 for (entry = map->header.next; entry != failed_entry;
3394 entry = entry->next) {
3395 if (entry->protection == VM_PROT_NONE)
3396 continue;
3397 entry->wired_count--;
3398 if (VM_MAPENT_ISWIRED(entry))
3399 uvm_map_entry_unwire(map, entry);
3400 }
3401 vm_map_unlock(map);
3402 UVMHIST_LOG(maphist,"<- done (RV=%d)", rv,0,0,0);
3403 return (rv);
3404 }
3405
3406 /* We are holding a read lock here. */
3407 vm_map_unbusy(map);
3408 vm_map_unlock_read(map);
3409
3410 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
3411 return 0;
3412 }
3413
3414 /*
3415 * uvm_map_clean: clean out a map range
3416 *
3417 * => valid flags:
3418 * if (flags & PGO_CLEANIT): dirty pages are cleaned first
3419 * if (flags & PGO_SYNCIO): dirty pages are written synchronously
3420 * if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean
3421 * if (flags & PGO_FREE): any cached pages are freed after clean
3422 * => returns an error if any part of the specified range isn't mapped
3423 * => never a need to flush amap layer since the anonymous memory has
3424 * no permanent home, but may deactivate pages there
3425 * => called from sys_msync() and sys_madvise()
3426 * => caller must not write-lock map (read OK).
3427 * => we may sleep while cleaning if SYNCIO [with map read-locked]
3428 */
3429
3430 int
3431 uvm_map_clean(struct vm_map *map, vaddr_t start, vaddr_t end, int flags)
3432 {
3433 struct vm_map_entry *current, *entry;
3434 struct uvm_object *uobj;
3435 struct vm_amap *amap;
3436 struct vm_anon *anon;
3437 struct vm_page *pg;
3438 vaddr_t offset;
3439 vsize_t size;
3440 voff_t uoff;
3441 int error, refs;
3442 UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist);
3443
3444 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,flags=0x%x)",
3445 map, start, end, flags);
3446 KASSERT((flags & (PGO_FREE|PGO_DEACTIVATE)) !=
3447 (PGO_FREE|PGO_DEACTIVATE));
3448
3449 vm_map_lock_read(map);
3450 VM_MAP_RANGE_CHECK(map, start, end);
3451 if (uvm_map_lookup_entry(map, start, &entry) == FALSE) {
3452 vm_map_unlock_read(map);
3453 return EFAULT;
3454 }
3455
3456 /*
3457 * Make a first pass to check for holes and wiring problems.
3458 */
3459
3460 for (current = entry; current->start < end; current = current->next) {
3461 if (UVM_ET_ISSUBMAP(current)) {
3462 vm_map_unlock_read(map);
3463 return EINVAL;
3464 }
3465 if ((flags & PGO_FREE) != 0 && VM_MAPENT_ISWIRED(entry)) {
3466 vm_map_unlock_read(map);
3467 return EBUSY;
3468 }
3469 if (end <= current->end) {
3470 break;
3471 }
3472 if (current->end != current->next->start) {
3473 vm_map_unlock_read(map);
3474 return EFAULT;
3475 }
3476 }
3477
3478 error = 0;
3479 for (current = entry; start < end; current = current->next) {
3480 amap = current->aref.ar_amap; /* top layer */
3481 uobj = current->object.uvm_obj; /* bottom layer */
3482 KASSERT(start >= current->start);
3483
3484 /*
3485 * No amap cleaning necessary if:
3486 *
3487 * (1) There's no amap.
3488 *
3489 * (2) We're not deactivating or freeing pages.
3490 */
3491
3492 if (amap == NULL || (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0)
3493 goto flush_object;
3494
3495 amap_lock(amap);
3496 offset = start - current->start;
3497 size = MIN(end, current->end) - start;
3498 for ( ; size != 0; size -= PAGE_SIZE, offset += PAGE_SIZE) {
3499 anon = amap_lookup(¤t->aref, offset);
3500 if (anon == NULL)
3501 continue;
3502
3503 simple_lock(&anon->an_lock);
3504 pg = anon->an_page;
3505 if (pg == NULL) {
3506 simple_unlock(&anon->an_lock);
3507 continue;
3508 }
3509
3510 switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) {
3511
3512 /*
3513 * In these first 3 cases, we just deactivate the page.
3514 */
3515
3516 case PGO_CLEANIT|PGO_FREE:
3517 case PGO_CLEANIT|PGO_DEACTIVATE:
3518 case PGO_DEACTIVATE:
3519 deactivate_it:
3520 /*
3521 * skip the page if it's loaned or wired,
3522 * since it shouldn't be on a paging queue
3523 * at all in these cases.
3524 */
3525
3526 uvm_lock_pageq();
3527 if (pg->loan_count != 0 ||
3528 pg->wire_count != 0) {
3529 uvm_unlock_pageq();
3530 simple_unlock(&anon->an_lock);
3531 continue;
3532 }
3533 KASSERT(pg->uanon == anon);
3534 pmap_clear_reference(pg);
3535 uvm_pagedeactivate(pg);
3536 uvm_unlock_pageq();
3537 simple_unlock(&anon->an_lock);
3538 continue;
3539
3540 case PGO_FREE:
3541
3542 /*
3543 * If there are multiple references to
3544 * the amap, just deactivate the page.
3545 */
3546
3547 if (amap_refs(amap) > 1)
3548 goto deactivate_it;
3549
3550 /* skip the page if it's wired */
3551 if (pg->wire_count != 0) {
3552 simple_unlock(&anon->an_lock);
3553 continue;
3554 }
3555 amap_unadd(¤t->aref, offset);
3556 refs = --anon->an_ref;
3557 simple_unlock(&anon->an_lock);
3558 if (refs == 0)
3559 uvm_anfree(anon);
3560 continue;
3561 }
3562 }
3563 amap_unlock(amap);
3564
3565 flush_object:
3566 /*
3567 * flush pages if we've got a valid backing object.
3568 * note that we must always clean object pages before
3569 * freeing them since otherwise we could reveal stale
3570 * data from files.
3571 */
3572
3573 uoff = current->offset + (start - current->start);
3574 size = MIN(end, current->end) - start;
3575 if (uobj != NULL) {
3576 simple_lock(&uobj->vmobjlock);
3577 if (uobj->pgops->pgo_put != NULL)
3578 error = (uobj->pgops->pgo_put)(uobj, uoff,
3579 uoff + size, flags | PGO_CLEANIT);
3580 else
3581 error = 0;
3582 }
3583 start += size;
3584 }
3585 vm_map_unlock_read(map);
3586 return (error);
3587 }
3588
3589
3590 /*
3591 * uvm_map_checkprot: check protection in map
3592 *
3593 * => must allow specified protection in a fully allocated region.
3594 * => map must be read or write locked by caller.
3595 */
3596
3597 boolean_t
3598 uvm_map_checkprot(struct vm_map *map, vaddr_t start, vaddr_t end,
3599 vm_prot_t protection)
3600 {
3601 struct vm_map_entry *entry;
3602 struct vm_map_entry *tmp_entry;
3603
3604 if (!uvm_map_lookup_entry(map, start, &tmp_entry)) {
3605 return (FALSE);
3606 }
3607 entry = tmp_entry;
3608 while (start < end) {
3609 if (entry == &map->header) {
3610 return (FALSE);
3611 }
3612
3613 /*
3614 * no holes allowed
3615 */
3616
3617 if (start < entry->start) {
3618 return (FALSE);
3619 }
3620
3621 /*
3622 * check protection associated with entry
3623 */
3624
3625 if ((entry->protection & protection) != protection) {
3626 return (FALSE);
3627 }
3628 start = entry->end;
3629 entry = entry->next;
3630 }
3631 return (TRUE);
3632 }
3633
3634 /*
3635 * uvmspace_alloc: allocate a vmspace structure.
3636 *
3637 * - structure includes vm_map and pmap
3638 * - XXX: no locking on this structure
3639 * - refcnt set to 1, rest must be init'd by caller
3640 */
3641 struct vmspace *
3642 uvmspace_alloc(vaddr_t vmin, vaddr_t vmax)
3643 {
3644 struct vmspace *vm;
3645 UVMHIST_FUNC("uvmspace_alloc"); UVMHIST_CALLED(maphist);
3646
3647 vm = pool_get(&uvm_vmspace_pool, PR_WAITOK);
3648 uvmspace_init(vm, NULL, vmin, vmax);
3649 UVMHIST_LOG(maphist,"<- done (vm=0x%x)", vm,0,0,0);
3650 return (vm);
3651 }
3652
3653 /*
3654 * uvmspace_init: initialize a vmspace structure.
3655 *
3656 * - XXX: no locking on this structure
3657 * - refcnt set to 1, rest must be init'd by caller
3658 */
3659 void
3660 uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t vmin, vaddr_t vmax)
3661 {
3662 UVMHIST_FUNC("uvmspace_init"); UVMHIST_CALLED(maphist);
3663
3664 memset(vm, 0, sizeof(*vm));
3665 uvm_map_setup(&vm->vm_map, vmin, vmax, VM_MAP_PAGEABLE
3666 #ifdef __USING_TOPDOWN_VM
3667 | VM_MAP_TOPDOWN
3668 #endif
3669 );
3670 if (pmap)
3671 pmap_reference(pmap);
3672 else
3673 pmap = pmap_create();
3674 vm->vm_map.pmap = pmap;
3675 vm->vm_refcnt = 1;
3676 UVMHIST_LOG(maphist,"<- done",0,0,0,0);
3677 }
3678
3679 /*
3680 * uvmspace_share: share a vmspace between two processes
3681 *
3682 * - used for vfork, threads(?)
3683 */
3684
3685 void
3686 uvmspace_share(struct proc *p1, struct proc *p2)
3687 {
3688
3689 uvmspace_addref(p1->p_vmspace);
3690 p2->p_vmspace = p1->p_vmspace;
3691 }
3692
3693 /*
3694 * uvmspace_unshare: ensure that process "p" has its own, unshared, vmspace
3695 *
3696 * - XXX: no locking on vmspace
3697 */
3698
3699 void
3700 uvmspace_unshare(struct lwp *l)
3701 {
3702 struct proc *p = l->l_proc;
3703 struct vmspace *nvm, *ovm = p->p_vmspace;
3704
3705 if (ovm->vm_refcnt == 1)
3706 /* nothing to do: vmspace isn't shared in the first place */
3707 return;
3708
3709 /* make a new vmspace, still holding old one */
3710 nvm = uvmspace_fork(ovm);
3711
3712 pmap_deactivate(l); /* unbind old vmspace */
3713 p->p_vmspace = nvm;
3714 pmap_activate(l); /* switch to new vmspace */
3715
3716 uvmspace_free(ovm); /* drop reference to old vmspace */
3717 }
3718
3719 /*
3720 * uvmspace_exec: the process wants to exec a new program
3721 */
3722
3723 void
3724 uvmspace_exec(struct lwp *l, vaddr_t start, vaddr_t end)
3725 {
3726 struct proc *p = l->l_proc;
3727 struct vmspace *nvm, *ovm = p->p_vmspace;
3728 struct vm_map *map = &ovm->vm_map;
3729
3730 #ifdef __sparc__
3731 /* XXX cgd 960926: the sparc #ifdef should be a MD hook */
3732 kill_user_windows(l); /* before stack addresses go away */
3733 #endif
3734
3735 /*
3736 * see if more than one process is using this vmspace...
3737 */
3738
3739 if (ovm->vm_refcnt == 1) {
3740
3741 /*
3742 * if p is the only process using its vmspace then we can safely
3743 * recycle that vmspace for the program that is being exec'd.
3744 */
3745
3746 #ifdef SYSVSHM
3747 /*
3748 * SYSV SHM semantics require us to kill all segments on an exec
3749 */
3750
3751 if (ovm->vm_shm)
3752 shmexit(ovm);
3753 #endif
3754
3755 /*
3756 * POSIX 1003.1b -- "lock future mappings" is revoked
3757 * when a process execs another program image.
3758 */
3759
3760 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);
3761
3762 /*
3763 * now unmap the old program
3764 */
3765
3766 pmap_remove_all(map->pmap);
3767 uvm_unmap(map, vm_map_min(map), vm_map_max(map));
3768 KASSERT(map->header.prev == &map->header);
3769 KASSERT(map->nentries == 0);
3770
3771 /*
3772 * resize the map
3773 */
3774
3775 vm_map_setmin(map, start);
3776 vm_map_setmax(map, end);
3777 } else {
3778
3779 /*
3780 * p's vmspace is being shared, so we can't reuse it for p since
3781 * it is still being used for others. allocate a new vmspace
3782 * for p
3783 */
3784
3785 nvm = uvmspace_alloc(start, end);
3786
3787 /*
3788 * install new vmspace and drop our ref to the old one.
3789 */
3790
3791 pmap_deactivate(l);
3792 p->p_vmspace = nvm;
3793 pmap_activate(l);
3794
3795 uvmspace_free(ovm);
3796 }
3797 }
3798
3799 /*
3800 * uvmspace_addref: add a referece to a vmspace.
3801 */
3802
3803 void
3804 uvmspace_addref(struct vmspace *vm)
3805 {
3806 struct vm_map *map = &vm->vm_map;
3807
3808 KASSERT((map->flags & VM_MAP_DYING) == 0);
3809
3810 simple_lock(&map->ref_lock);
3811 KASSERT(vm->vm_refcnt > 0);
3812 vm->vm_refcnt++;
3813 simple_unlock(&map->ref_lock);
3814 }
3815
3816 /*
3817 * uvmspace_free: free a vmspace data structure
3818 */
3819
3820 void
3821 uvmspace_free(struct vmspace *vm)
3822 {
3823 struct vm_map_entry *dead_entries;
3824 struct vm_map *map = &vm->vm_map;
3825 int n;
3826
3827 UVMHIST_FUNC("uvmspace_free"); UVMHIST_CALLED(maphist);
3828
3829 UVMHIST_LOG(maphist,"(vm=0x%x) ref=%d", vm, vm->vm_refcnt,0,0);
3830 simple_lock(&map->ref_lock);
3831 n = --vm->vm_refcnt;
3832 simple_unlock(&map->ref_lock);
3833 if (n > 0)
3834 return;
3835
3836 /*
3837 * at this point, there should be no other references to the map.
3838 * delete all of the mappings, then destroy the pmap.
3839 */
3840
3841 map->flags |= VM_MAP_DYING;
3842 pmap_remove_all(map->pmap);
3843 #ifdef SYSVSHM
3844 /* Get rid of any SYSV shared memory segments. */
3845 if (vm->vm_shm != NULL)
3846 shmexit(vm);
3847 #endif
3848 if (map->nentries) {
3849 uvm_unmap_remove(map, vm_map_min(map), vm_map_max(map),
3850 &dead_entries, NULL, 0);
3851 if (dead_entries != NULL)
3852 uvm_unmap_detach(dead_entries, 0);
3853 }
3854 KASSERT(map->nentries == 0);
3855 KASSERT(map->size == 0);
3856 pmap_destroy(map->pmap);
3857 pool_put(&uvm_vmspace_pool, vm);
3858 }
3859
3860 /*
3861 * F O R K - m a i n e n t r y p o i n t
3862 */
3863 /*
3864 * uvmspace_fork: fork a process' main map
3865 *
3866 * => create a new vmspace for child process from parent.
3867 * => parent's map must not be locked.
3868 */
3869
3870 struct vmspace *
3871 uvmspace_fork(struct vmspace *vm1)
3872 {
3873 struct vmspace *vm2;
3874 struct vm_map *old_map = &vm1->vm_map;
3875 struct vm_map *new_map;
3876 struct vm_map_entry *old_entry;
3877 struct vm_map_entry *new_entry;
3878 UVMHIST_FUNC("uvmspace_fork"); UVMHIST_CALLED(maphist);
3879
3880 vm_map_lock(old_map);
3881
3882 vm2 = uvmspace_alloc(vm_map_min(old_map), vm_map_max(old_map));
3883 memcpy(&vm2->vm_startcopy, &vm1->vm_startcopy,
3884 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
3885 new_map = &vm2->vm_map; /* XXX */
3886
3887 old_entry = old_map->header.next;
3888 new_map->size = old_map->size;
3889
3890 /*
3891 * go entry-by-entry
3892 */
3893
3894 while (old_entry != &old_map->header) {
3895
3896 /*
3897 * first, some sanity checks on the old entry
3898 */
3899
3900 KASSERT(!UVM_ET_ISSUBMAP(old_entry));
3901 KASSERT(UVM_ET_ISCOPYONWRITE(old_entry) ||
3902 !UVM_ET_ISNEEDSCOPY(old_entry));
3903
3904 switch (old_entry->inheritance) {
3905 case MAP_INHERIT_NONE:
3906
3907 /*
3908 * drop the mapping, modify size
3909 */
3910 new_map->size -= old_entry->end - old_entry->start;
3911 break;
3912
3913 case MAP_INHERIT_SHARE:
3914
3915 /*
3916 * share the mapping: this means we want the old and
3917 * new entries to share amaps and backing objects.
3918 */
3919 /*
3920 * if the old_entry needs a new amap (due to prev fork)
3921 * then we need to allocate it now so that we have
3922 * something we own to share with the new_entry. [in
3923 * other words, we need to clear needs_copy]
3924 */
3925
3926 if (UVM_ET_ISNEEDSCOPY(old_entry)) {
3927 /* get our own amap, clears needs_copy */
3928 amap_copy(old_map, old_entry, AMAP_COPY_NOCHUNK,
3929 0, 0);
3930 /* XXXCDC: WAITOK??? */
3931 }
3932
3933 new_entry = uvm_mapent_alloc(new_map, 0);
3934 /* old_entry -> new_entry */
3935 uvm_mapent_copy(old_entry, new_entry);
3936
3937 /* new pmap has nothing wired in it */
3938 new_entry->wired_count = 0;
3939
3940 /*
3941 * gain reference to object backing the map (can't
3942 * be a submap, already checked this case).
3943 */
3944
3945 if (new_entry->aref.ar_amap)
3946 uvm_map_reference_amap(new_entry, AMAP_SHARED);
3947
3948 if (new_entry->object.uvm_obj &&
3949 new_entry->object.uvm_obj->pgops->pgo_reference)
3950 new_entry->object.uvm_obj->
3951 pgops->pgo_reference(
3952 new_entry->object.uvm_obj);
3953
3954 /* insert entry at end of new_map's entry list */
3955 uvm_map_entry_link(new_map, new_map->header.prev,
3956 new_entry);
3957
3958 break;
3959
3960 case MAP_INHERIT_COPY:
3961
3962 /*
3963 * copy-on-write the mapping (using mmap's
3964 * MAP_PRIVATE semantics)
3965 *
3966 * allocate new_entry, adjust reference counts.
3967 * (note that new references are read-only).
3968 */
3969
3970 new_entry = uvm_mapent_alloc(new_map, 0);
3971 /* old_entry -> new_entry */
3972 uvm_mapent_copy(old_entry, new_entry);
3973
3974 if (new_entry->aref.ar_amap)
3975 uvm_map_reference_amap(new_entry, 0);
3976
3977 if (new_entry->object.uvm_obj &&
3978 new_entry->object.uvm_obj->pgops->pgo_reference)
3979 new_entry->object.uvm_obj->pgops->pgo_reference
3980 (new_entry->object.uvm_obj);
3981
3982 /* new pmap has nothing wired in it */
3983 new_entry->wired_count = 0;
3984
3985 new_entry->etype |=
3986 (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY);
3987 uvm_map_entry_link(new_map, new_map->header.prev,
3988 new_entry);
3989
3990 /*
3991 * the new entry will need an amap. it will either
3992 * need to be copied from the old entry or created
3993 * from scratch (if the old entry does not have an
3994 * amap). can we defer this process until later
3995 * (by setting "needs_copy") or do we need to copy
3996 * the amap now?
3997 *
3998 * we must copy the amap now if any of the following
3999 * conditions hold:
4000 * 1. the old entry has an amap and that amap is
4001 * being shared. this means that the old (parent)
4002 * process is sharing the amap with another
4003 * process. if we do not clear needs_copy here
4004 * we will end up in a situation where both the
4005 * parent and child process are refering to the
4006 * same amap with "needs_copy" set. if the
4007 * parent write-faults, the fault routine will
4008 * clear "needs_copy" in the parent by allocating
4009 * a new amap. this is wrong because the
4010 * parent is supposed to be sharing the old amap
4011 * and the new amap will break that.
4012 *
4013 * 2. if the old entry has an amap and a non-zero
4014 * wire count then we are going to have to call
4015 * amap_cow_now to avoid page faults in the
4016 * parent process. since amap_cow_now requires
4017 * "needs_copy" to be clear we might as well
4018 * clear it here as well.
4019 *
4020 */
4021
4022 if (old_entry->aref.ar_amap != NULL) {
4023 if ((amap_flags(old_entry->aref.ar_amap) &
4024 AMAP_SHARED) != 0 ||
4025 VM_MAPENT_ISWIRED(old_entry)) {
4026
4027 amap_copy(new_map, new_entry,
4028 AMAP_COPY_NOCHUNK, 0, 0);
4029 /* XXXCDC: M_WAITOK ... ok? */
4030 }
4031 }
4032
4033 /*
4034 * if the parent's entry is wired down, then the
4035 * parent process does not want page faults on
4036 * access to that memory. this means that we
4037 * cannot do copy-on-write because we can't write
4038 * protect the old entry. in this case we
4039 * resolve all copy-on-write faults now, using
4040 * amap_cow_now. note that we have already
4041 * allocated any needed amap (above).
4042 */
4043
4044 if (VM_MAPENT_ISWIRED(old_entry)) {
4045
4046 /*
4047 * resolve all copy-on-write faults now
4048 * (note that there is nothing to do if
4049 * the old mapping does not have an amap).
4050 */
4051 if (old_entry->aref.ar_amap)
4052 amap_cow_now(new_map, new_entry);
4053
4054 } else {
4055
4056 /*
4057 * setup mappings to trigger copy-on-write faults
4058 * we must write-protect the parent if it has
4059 * an amap and it is not already "needs_copy"...
4060 * if it is already "needs_copy" then the parent
4061 * has already been write-protected by a previous
4062 * fork operation.
4063 */
4064
4065 if (old_entry->aref.ar_amap &&
4066 !UVM_ET_ISNEEDSCOPY(old_entry)) {
4067 if (old_entry->max_protection & VM_PROT_WRITE) {
4068 pmap_protect(old_map->pmap,
4069 old_entry->start,
4070 old_entry->end,
4071 old_entry->protection &
4072 ~VM_PROT_WRITE);
4073 pmap_update(old_map->pmap);
4074 }
4075 old_entry->etype |= UVM_ET_NEEDSCOPY;
4076 }
4077 }
4078 break;
4079 } /* end of switch statement */
4080 old_entry = old_entry->next;
4081 }
4082
4083 vm_map_unlock(old_map);
4084
4085 #ifdef SYSVSHM
4086 if (vm1->vm_shm)
4087 shmfork(vm1, vm2);
4088 #endif
4089
4090 #ifdef PMAP_FORK
4091 pmap_fork(vm1->vm_map.pmap, vm2->vm_map.pmap);
4092 #endif
4093
4094 UVMHIST_LOG(maphist,"<- done",0,0,0,0);
4095 return (vm2);
4096 }
4097
4098
4099 /*
4100 * in-kernel map entry allocation.
4101 */
4102
4103 struct uvm_kmapent_hdr {
4104 LIST_ENTRY(uvm_kmapent_hdr) ukh_listq;
4105 int ukh_nused;
4106 struct vm_map_entry *ukh_freelist;
4107 struct vm_map *ukh_map;
4108 struct vm_map_entry ukh_entries[0];
4109 };
4110
4111 #define UVM_KMAPENT_CHUNK \
4112 ((PAGE_SIZE - sizeof(struct uvm_kmapent_hdr)) \
4113 / sizeof(struct vm_map_entry))
4114
4115 #define UVM_KHDR_FIND(entry) \
4116 ((struct uvm_kmapent_hdr *)(((vaddr_t)entry) & ~PAGE_MASK))
4117
4118
4119 #ifdef DIAGNOSTIC
4120 static struct vm_map *
4121 uvm_kmapent_map(struct vm_map_entry *entry)
4122 {
4123 const struct uvm_kmapent_hdr *ukh;
4124
4125 ukh = UVM_KHDR_FIND(entry);
4126 return ukh->ukh_map;
4127 }
4128 #endif
4129
4130 static inline struct vm_map_entry *
4131 uvm_kmapent_get(struct uvm_kmapent_hdr *ukh)
4132 {
4133 struct vm_map_entry *entry;
4134
4135 KASSERT(ukh->ukh_nused <= UVM_KMAPENT_CHUNK);
4136 KASSERT(ukh->ukh_nused >= 0);
4137
4138 entry = ukh->ukh_freelist;
4139 if (entry) {
4140 KASSERT((entry->flags & (UVM_MAP_KERNEL | UVM_MAP_KMAPENT))
4141 == UVM_MAP_KERNEL);
4142 ukh->ukh_freelist = entry->next;
4143 ukh->ukh_nused++;
4144 KASSERT(ukh->ukh_nused <= UVM_KMAPENT_CHUNK);
4145 } else {
4146 KASSERT(ukh->ukh_nused == UVM_KMAPENT_CHUNK);
4147 }
4148
4149 return entry;
4150 }
4151
4152 static inline void
4153 uvm_kmapent_put(struct uvm_kmapent_hdr *ukh, struct vm_map_entry *entry)
4154 {
4155
4156 KASSERT((entry->flags & (UVM_MAP_KERNEL | UVM_MAP_KMAPENT))
4157 == UVM_MAP_KERNEL);
4158 KASSERT(ukh->ukh_nused <= UVM_KMAPENT_CHUNK);
4159 KASSERT(ukh->ukh_nused > 0);
4160 KASSERT(ukh->ukh_freelist != NULL ||
4161 ukh->ukh_nused == UVM_KMAPENT_CHUNK);
4162 KASSERT(ukh->ukh_freelist == NULL ||
4163 ukh->ukh_nused < UVM_KMAPENT_CHUNK);
4164
4165 ukh->ukh_nused--;
4166 entry->next = ukh->ukh_freelist;
4167 ukh->ukh_freelist = entry;
4168 }
4169
4170 /*
4171 * uvm_kmapent_alloc: allocate a map entry for in-kernel map
4172 */
4173
4174 static struct vm_map_entry *
4175 uvm_kmapent_alloc(struct vm_map *map, int flags)
4176 {
4177 struct vm_page *pg;
4178 struct uvm_map_args args;
4179 struct uvm_kmapent_hdr *ukh;
4180 struct vm_map_entry *entry;
4181 uvm_flag_t mapflags = UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
4182 UVM_INH_NONE, UVM_ADV_RANDOM, flags | UVM_FLAG_NOMERGE);
4183 vaddr_t va;
4184 int error;
4185 int i;
4186 int s;
4187
4188 KDASSERT(UVM_KMAPENT_CHUNK > 2);
4189 KDASSERT(kernel_map != NULL);
4190 KASSERT(vm_map_pmap(map) == pmap_kernel());
4191
4192 UVMMAP_EVCNT_INCR(uke_alloc);
4193 entry = NULL;
4194 again:
4195 /*
4196 * try to grab an entry from freelist.
4197 */
4198 s = splvm();
4199 simple_lock(&uvm.kentry_lock);
4200 ukh = LIST_FIRST(&vm_map_to_kernel(map)->vmk_kentry_free);
4201 if (ukh) {
4202 entry = uvm_kmapent_get(ukh);
4203 if (ukh->ukh_nused == UVM_KMAPENT_CHUNK)
4204 LIST_REMOVE(ukh, ukh_listq);
4205 }
4206 simple_unlock(&uvm.kentry_lock);
4207 splx(s);
4208
4209 if (entry)
4210 return entry;
4211
4212 /*
4213 * there's no free entry for this vm_map.
4214 * now we need to allocate some vm_map_entry.
4215 * for simplicity, always allocate one page chunk of them at once.
4216 */
4217
4218 pg = uvm_pagealloc(NULL, 0, NULL, 0);
4219 if (__predict_false(pg == NULL)) {
4220 if (flags & UVM_FLAG_NOWAIT)
4221 return NULL;
4222 uvm_wait("kme_alloc");
4223 goto again;
4224 }
4225
4226 error = uvm_map_prepare(map, 0, PAGE_SIZE, NULL, 0, 0, mapflags, &args);
4227 if (error) {
4228 uvm_pagefree(pg);
4229 return NULL;
4230 }
4231
4232 va = args.uma_start;
4233
4234 pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg), VM_PROT_READ|VM_PROT_WRITE);
4235 pmap_update(vm_map_pmap(map));
4236
4237 ukh = (void *)va;
4238
4239 /*
4240 * use the first entry for ukh itsself.
4241 */
4242
4243 entry = &ukh->ukh_entries[0];
4244 entry->flags = UVM_MAP_KERNEL | UVM_MAP_KMAPENT;
4245 error = uvm_map_enter(map, &args, entry);
4246 KASSERT(error == 0);
4247
4248 ukh->ukh_nused = UVM_KMAPENT_CHUNK;
4249 ukh->ukh_map = map;
4250 ukh->ukh_freelist = NULL;
4251 for (i = UVM_KMAPENT_CHUNK - 1; i >= 2; i--) {
4252 struct vm_map_entry *xentry = &ukh->ukh_entries[i];
4253
4254 xentry->flags = UVM_MAP_KERNEL;
4255 uvm_kmapent_put(ukh, xentry);
4256 }
4257 KASSERT(ukh->ukh_nused == 2);
4258
4259 s = splvm();
4260 simple_lock(&uvm.kentry_lock);
4261 LIST_INSERT_HEAD(&vm_map_to_kernel(map)->vmk_kentry_free,
4262 ukh, ukh_listq);
4263 simple_unlock(&uvm.kentry_lock);
4264 splx(s);
4265
4266 /*
4267 * return second entry.
4268 */
4269
4270 entry = &ukh->ukh_entries[1];
4271 entry->flags = UVM_MAP_KERNEL;
4272 UVMMAP_EVCNT_INCR(ukh_alloc);
4273 return entry;
4274 }
4275
4276 /*
4277 * uvm_mapent_free: free map entry for in-kernel map
4278 */
4279
4280 static void
4281 uvm_kmapent_free(struct vm_map_entry *entry)
4282 {
4283 struct uvm_kmapent_hdr *ukh;
4284 struct vm_page *pg;
4285 struct vm_map *map;
4286 struct pmap *pmap;
4287 vaddr_t va;
4288 paddr_t pa;
4289 struct vm_map_entry *deadentry;
4290 int s;
4291
4292 UVMMAP_EVCNT_INCR(uke_free);
4293 ukh = UVM_KHDR_FIND(entry);
4294 map = ukh->ukh_map;
4295
4296 s = splvm();
4297 simple_lock(&uvm.kentry_lock);
4298 uvm_kmapent_put(ukh, entry);
4299 if (ukh->ukh_nused > 1) {
4300 if (ukh->ukh_nused == UVM_KMAPENT_CHUNK - 1)
4301 LIST_INSERT_HEAD(
4302 &vm_map_to_kernel(map)->vmk_kentry_free,
4303 ukh, ukh_listq);
4304 simple_unlock(&uvm.kentry_lock);
4305 splx(s);
4306 return;
4307 }
4308
4309 /*
4310 * now we can free this ukh.
4311 *
4312 * however, keep an empty ukh to avoid ping-pong.
4313 */
4314
4315 if (LIST_FIRST(&vm_map_to_kernel(map)->vmk_kentry_free) == ukh &&
4316 LIST_NEXT(ukh, ukh_listq) == NULL) {
4317 simple_unlock(&uvm.kentry_lock);
4318 splx(s);
4319 return;
4320 }
4321 LIST_REMOVE(ukh, ukh_listq);
4322 simple_unlock(&uvm.kentry_lock);
4323 splx(s);
4324
4325 KASSERT(ukh->ukh_nused == 1);
4326
4327 /*
4328 * remove map entry for ukh itsself.
4329 */
4330
4331 va = (vaddr_t)ukh;
4332 KASSERT((va & PAGE_MASK) == 0);
4333 vm_map_lock(map);
4334 uvm_unmap_remove(map, va, va + PAGE_SIZE, &deadentry, NULL, 0);
4335 KASSERT(deadentry->flags & UVM_MAP_KERNEL);
4336 KASSERT(deadentry->flags & UVM_MAP_KMAPENT);
4337 KASSERT(deadentry->next == NULL);
4338 KASSERT(deadentry == &ukh->ukh_entries[0]);
4339
4340 /*
4341 * unmap the page from pmap and free it.
4342 */
4343
4344 pmap = vm_map_pmap(map);
4345 KASSERT(pmap == pmap_kernel());
4346 if (!pmap_extract(pmap, va, &pa))
4347 panic("%s: no mapping", __func__);
4348 pmap_kremove(va, PAGE_SIZE);
4349 vm_map_unlock(map);
4350 pg = PHYS_TO_VM_PAGE(pa);
4351 uvm_pagefree(pg);
4352 UVMMAP_EVCNT_INCR(ukh_free);
4353 }
4354
4355 /*
4356 * map entry reservation
4357 */
4358
4359 /*
4360 * uvm_mapent_reserve: reserve map entries for clipping before locking map.
4361 *
4362 * => needed when unmapping entries allocated without UVM_FLAG_QUANTUM.
4363 * => caller shouldn't hold map locked.
4364 */
4365 int
4366 uvm_mapent_reserve(struct vm_map *map, struct uvm_mapent_reservation *umr,
4367 int nentries, int flags)
4368 {
4369
4370 umr->umr_nentries = 0;
4371
4372 if ((flags & UVM_FLAG_QUANTUM) != 0)
4373 return 0;
4374
4375 if (!VM_MAP_USE_KMAPENT(map))
4376 return 0;
4377
4378 while (nentries--) {
4379 struct vm_map_entry *ent;
4380 ent = uvm_kmapent_alloc(map, flags);
4381 if (!ent) {
4382 uvm_mapent_unreserve(map, umr);
4383 return ENOMEM;
4384 }
4385 UMR_PUTENTRY(umr, ent);
4386 }
4387
4388 return 0;
4389 }
4390
4391 /*
4392 * uvm_mapent_unreserve:
4393 *
4394 * => caller shouldn't hold map locked.
4395 * => never fail or sleep.
4396 */
4397 void
4398 uvm_mapent_unreserve(struct vm_map *map, struct uvm_mapent_reservation *umr)
4399 {
4400
4401 while (!UMR_EMPTY(umr))
4402 uvm_kmapent_free(UMR_GETENTRY(umr));
4403 }
4404
4405 /*
4406 * uvm_mapent_trymerge: try to merge an entry with its neighbors.
4407 *
4408 * => called with map locked.
4409 * => return non zero if successfully merged.
4410 */
4411
4412 int
4413 uvm_mapent_trymerge(struct vm_map *map, struct vm_map_entry *entry, int flags)
4414 {
4415 struct uvm_object *uobj;
4416 struct vm_map_entry *next;
4417 struct vm_map_entry *prev;
4418 vsize_t size;
4419 int merged = 0;
4420 boolean_t copying;
4421 int newetype;
4422
4423 if (VM_MAP_USE_KMAPENT(map)) {
4424 return 0;
4425 }
4426 if (entry->aref.ar_amap != NULL) {
4427 return 0;
4428 }
4429 if ((entry->flags & UVM_MAP_NOMERGE) != 0) {
4430 return 0;
4431 }
4432
4433 uobj = entry->object.uvm_obj;
4434 size = entry->end - entry->start;
4435 copying = (flags & UVM_MERGE_COPYING) != 0;
4436 newetype = copying ? (entry->etype & ~UVM_ET_NEEDSCOPY) : entry->etype;
4437
4438 next = entry->next;
4439 if (next != &map->header &&
4440 next->start == entry->end &&
4441 ((copying && next->aref.ar_amap != NULL &&
4442 amap_refs(next->aref.ar_amap) == 1) ||
4443 (!copying && next->aref.ar_amap == NULL)) &&
4444 UVM_ET_ISCOMPATIBLE(next, newetype,
4445 uobj, entry->flags, entry->protection,
4446 entry->max_protection, entry->inheritance, entry->advice,
4447 entry->wired_count) &&
4448 (uobj == NULL || entry->offset + size == next->offset)) {
4449 int error;
4450
4451 if (copying) {
4452 error = amap_extend(next, size,
4453 AMAP_EXTEND_NOWAIT|AMAP_EXTEND_BACKWARDS);
4454 } else {
4455 error = 0;
4456 }
4457 if (error == 0) {
4458 if (uobj) {
4459 if (uobj->pgops->pgo_detach) {
4460 uobj->pgops->pgo_detach(uobj);
4461 }
4462 }
4463
4464 entry->end = next->end;
4465 uvm_map_entry_unlink(map, next);
4466 if (copying) {
4467 entry->aref = next->aref;
4468 entry->etype &= ~UVM_ET_NEEDSCOPY;
4469 }
4470 uvm_tree_sanity(map, "trymerge forwardmerge");
4471 uvm_mapent_free_merged(map, next);
4472 merged++;
4473 }
4474 }
4475
4476 prev = entry->prev;
4477 if (prev != &map->header &&
4478 prev->end == entry->start &&
4479 ((copying && !merged && prev->aref.ar_amap != NULL &&
4480 amap_refs(prev->aref.ar_amap) == 1) ||
4481 (!copying && prev->aref.ar_amap == NULL)) &&
4482 UVM_ET_ISCOMPATIBLE(prev, newetype,
4483 uobj, entry->flags, entry->protection,
4484 entry->max_protection, entry->inheritance, entry->advice,
4485 entry->wired_count) &&
4486 (uobj == NULL ||
4487 prev->offset + prev->end - prev->start == entry->offset)) {
4488 int error;
4489
4490 if (copying) {
4491 error = amap_extend(prev, size,
4492 AMAP_EXTEND_NOWAIT|AMAP_EXTEND_FORWARDS);
4493 } else {
4494 error = 0;
4495 }
4496 if (error == 0) {
4497 if (uobj) {
4498 if (uobj->pgops->pgo_detach) {
4499 uobj->pgops->pgo_detach(uobj);
4500 }
4501 entry->offset = prev->offset;
4502 }
4503
4504 entry->start = prev->start;
4505 uvm_map_entry_unlink(map, prev);
4506 if (copying) {
4507 entry->aref = prev->aref;
4508 entry->etype &= ~UVM_ET_NEEDSCOPY;
4509 }
4510 uvm_tree_sanity(map, "trymerge backmerge");
4511 uvm_mapent_free_merged(map, prev);
4512 merged++;
4513 }
4514 }
4515
4516 return merged;
4517 }
4518
4519 #if defined(DDB)
4520
4521 /*
4522 * DDB hooks
4523 */
4524
4525 /*
4526 * uvm_map_printit: actually prints the map
4527 */
4528
4529 void
4530 uvm_map_printit(struct vm_map *map, boolean_t full,
4531 void (*pr)(const char *, ...))
4532 {
4533 struct vm_map_entry *entry;
4534
4535 (*pr)("MAP %p: [0x%lx->0x%lx]\n", map, vm_map_min(map),
4536 vm_map_max(map));
4537 (*pr)("\t#ent=%d, sz=%d, ref=%d, version=%d, flags=0x%x\n",
4538 map->nentries, map->size, map->ref_count, map->timestamp,
4539 map->flags);
4540 (*pr)("\tpmap=%p(resident=%ld, wired=%ld)\n", map->pmap,
4541 pmap_resident_count(map->pmap), pmap_wired_count(map->pmap));
4542 if (!full)
4543 return;
4544 for (entry = map->header.next; entry != &map->header;
4545 entry = entry->next) {
4546 (*pr)(" - %p: 0x%lx->0x%lx: obj=%p/0x%llx, amap=%p/%d\n",
4547 entry, entry->start, entry->end, entry->object.uvm_obj,
4548 (long long)entry->offset, entry->aref.ar_amap,
4549 entry->aref.ar_pageoff);
4550 (*pr)(
4551 "\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, "
4552 "wc=%d, adv=%d\n",
4553 (entry->etype & UVM_ET_SUBMAP) ? 'T' : 'F',
4554 (entry->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F',
4555 (entry->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F',
4556 entry->protection, entry->max_protection,
4557 entry->inheritance, entry->wired_count, entry->advice);
4558 }
4559 }
4560
4561 /*
4562 * uvm_object_printit: actually prints the object
4563 */
4564
4565 void
4566 uvm_object_printit(struct uvm_object *uobj, boolean_t full,
4567 void (*pr)(const char *, ...))
4568 {
4569 struct vm_page *pg;
4570 int cnt = 0;
4571
4572 (*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ",
4573 uobj, uobj->vmobjlock.lock_data, uobj->pgops, uobj->uo_npages);
4574 if (UVM_OBJ_IS_KERN_OBJECT(uobj))
4575 (*pr)("refs=<SYSTEM>\n");
4576 else
4577 (*pr)("refs=%d\n", uobj->uo_refs);
4578
4579 if (!full) {
4580 return;
4581 }
4582 (*pr)(" PAGES <pg,offset>:\n ");
4583 TAILQ_FOREACH(pg, &uobj->memq, listq) {
4584 cnt++;
4585 (*pr)("<%p,0x%llx> ", pg, (long long)pg->offset);
4586 if ((cnt % 3) == 0) {
4587 (*pr)("\n ");
4588 }
4589 }
4590 if ((cnt % 3) != 0) {
4591 (*pr)("\n");
4592 }
4593 }
4594
4595 /*
4596 * uvm_page_printit: actually print the page
4597 */
4598
4599 static const char page_flagbits[] =
4600 "\20\1BUSY\2WANTED\3TABLED\4CLEAN\5PAGEOUT\6RELEASED\7FAKE\10RDONLY"
4601 "\11ZERO\15PAGER1";
4602 static const char page_pqflagbits[] =
4603 "\20\1FREE\2INACTIVE\3ACTIVE\5ANON\6AOBJ";
4604
4605 void
4606 uvm_page_printit(struct vm_page *pg, boolean_t full,
4607 void (*pr)(const char *, ...))
4608 {
4609 struct vm_page *tpg;
4610 struct uvm_object *uobj;
4611 struct pglist *pgl;
4612 char pgbuf[128];
4613 char pqbuf[128];
4614
4615 (*pr)("PAGE %p:\n", pg);
4616 bitmask_snprintf(pg->flags, page_flagbits, pgbuf, sizeof(pgbuf));
4617 bitmask_snprintf(pg->pqflags, page_pqflagbits, pqbuf, sizeof(pqbuf));
4618 (*pr)(" flags=%s, pqflags=%s, wire_count=%d, pa=0x%lx\n",
4619 pgbuf, pqbuf, pg->wire_count, (long)VM_PAGE_TO_PHYS(pg));
4620 (*pr)(" uobject=%p, uanon=%p, offset=0x%llx loan_count=%d\n",
4621 pg->uobject, pg->uanon, (long long)pg->offset, pg->loan_count);
4622 #if defined(UVM_PAGE_TRKOWN)
4623 if (pg->flags & PG_BUSY)
4624 (*pr)(" owning process = %d, tag=%s\n",
4625 pg->owner, pg->owner_tag);
4626 else
4627 (*pr)(" page not busy, no owner\n");
4628 #else
4629 (*pr)(" [page ownership tracking disabled]\n");
4630 #endif
4631
4632 if (!full)
4633 return;
4634
4635 /* cross-verify object/anon */
4636 if ((pg->pqflags & PQ_FREE) == 0) {
4637 if (pg->pqflags & PQ_ANON) {
4638 if (pg->uanon == NULL || pg->uanon->an_page != pg)
4639 (*pr)(" >>> ANON DOES NOT POINT HERE <<< (%p)\n",
4640 (pg->uanon) ? pg->uanon->an_page : NULL);
4641 else
4642 (*pr)(" anon backpointer is OK\n");
4643 } else {
4644 uobj = pg->uobject;
4645 if (uobj) {
4646 (*pr)(" checking object list\n");
4647 TAILQ_FOREACH(tpg, &uobj->memq, listq) {
4648 if (tpg == pg) {
4649 break;
4650 }
4651 }
4652 if (tpg)
4653 (*pr)(" page found on object list\n");
4654 else
4655 (*pr)(" >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n");
4656 }
4657 }
4658 }
4659
4660 /* cross-verify page queue */
4661 if (pg->pqflags & PQ_FREE) {
4662 int fl = uvm_page_lookup_freelist(pg);
4663 int color = VM_PGCOLOR_BUCKET(pg);
4664 pgl = &uvm.page_free[fl].pgfl_buckets[color].pgfl_queues[
4665 ((pg)->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN];
4666 } else if (pg->pqflags & PQ_INACTIVE) {
4667 pgl = &uvm.page_inactive;
4668 } else if (pg->pqflags & PQ_ACTIVE) {
4669 pgl = &uvm.page_active;
4670 } else {
4671 pgl = NULL;
4672 }
4673
4674 if (pgl) {
4675 (*pr)(" checking pageq list\n");
4676 TAILQ_FOREACH(tpg, pgl, pageq) {
4677 if (tpg == pg) {
4678 break;
4679 }
4680 }
4681 if (tpg)
4682 (*pr)(" page found on pageq list\n");
4683 else
4684 (*pr)(" >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n");
4685 }
4686 }
4687
4688 /*
4689 * uvm_pages_printthem - print a summary of all managed pages
4690 */
4691
4692 void
4693 uvm_page_printall(void (*pr)(const char *, ...))
4694 {
4695 unsigned i;
4696 struct vm_page *pg;
4697
4698 (*pr)("%18s %4s %2s %18s %18s"
4699 #ifdef UVM_PAGE_TRKOWN
4700 " OWNER"
4701 #endif
4702 "\n", "PAGE", "FLAG", "PQ", "UOBJECT", "UANON");
4703 for (i = 0; i < vm_nphysseg; i++) {
4704 for (pg = vm_physmem[i].pgs; pg <= vm_physmem[i].lastpg; pg++) {
4705 (*pr)("%18p %04x %02x %18p %18p",
4706 pg, pg->flags, pg->pqflags, pg->uobject,
4707 pg->uanon);
4708 #ifdef UVM_PAGE_TRKOWN
4709 if (pg->flags & PG_BUSY)
4710 (*pr)(" %d [%s]", pg->owner, pg->owner_tag);
4711 #endif
4712 (*pr)("\n");
4713 }
4714 }
4715 }
4716
4717 #endif
4718
4719 /*
4720 * uvm_map_create: create map
4721 */
4722
4723 struct vm_map *
4724 uvm_map_create(pmap_t pmap, vaddr_t vmin, vaddr_t vmax, int flags)
4725 {
4726 struct vm_map *result;
4727
4728 MALLOC(result, struct vm_map *, sizeof(struct vm_map),
4729 M_VMMAP, M_WAITOK);
4730 uvm_map_setup(result, vmin, vmax, flags);
4731 result->pmap = pmap;
4732 return(result);
4733 }
4734
4735 /*
4736 * uvm_map_setup: init map
4737 *
4738 * => map must not be in service yet.
4739 */
4740
4741 void
4742 uvm_map_setup(struct vm_map *map, vaddr_t vmin, vaddr_t vmax, int flags)
4743 {
4744
4745 RB_INIT(&map->rbhead);
4746 map->header.next = map->header.prev = &map->header;
4747 map->nentries = 0;
4748 map->size = 0;
4749 map->ref_count = 1;
4750 vm_map_setmin(map, vmin);
4751 vm_map_setmax(map, vmax);
4752 map->flags = flags;
4753 map->first_free = &map->header;
4754 map->hint = &map->header;
4755 map->timestamp = 0;
4756 lockinit(&map->lock, PVM, "vmmaplk", 0, 0);
4757 simple_lock_init(&map->ref_lock);
4758 simple_lock_init(&map->hint_lock);
4759 simple_lock_init(&map->flags_lock);
4760 }
4761
4762
4763 /*
4764 * U N M A P - m a i n e n t r y p o i n t
4765 */
4766
4767 /*
4768 * uvm_unmap1: remove mappings from a vm_map (from "start" up to "stop")
4769 *
4770 * => caller must check alignment and size
4771 * => map must be unlocked (we will lock it)
4772 * => flags is UVM_FLAG_QUANTUM or 0.
4773 */
4774
4775 void
4776 uvm_unmap1(struct vm_map *map, vaddr_t start, vaddr_t end, int flags)
4777 {
4778 struct vm_map_entry *dead_entries;
4779 struct uvm_mapent_reservation umr;
4780 UVMHIST_FUNC("uvm_unmap"); UVMHIST_CALLED(maphist);
4781
4782 UVMHIST_LOG(maphist, " (map=0x%x, start=0x%x, end=0x%x)",
4783 map, start, end, 0);
4784 /*
4785 * work now done by helper functions. wipe the pmap's and then
4786 * detach from the dead entries...
4787 */
4788 uvm_mapent_reserve(map, &umr, 2, flags);
4789 vm_map_lock(map);
4790 uvm_unmap_remove(map, start, end, &dead_entries, &umr, flags);
4791 vm_map_unlock(map);
4792 uvm_mapent_unreserve(map, &umr);
4793
4794 if (dead_entries != NULL)
4795 uvm_unmap_detach(dead_entries, 0);
4796
4797 UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
4798 }
4799
4800
4801 /*
4802 * uvm_map_reference: add reference to a map
4803 *
4804 * => map need not be locked (we use ref_lock).
4805 */
4806
4807 void
4808 uvm_map_reference(struct vm_map *map)
4809 {
4810 simple_lock(&map->ref_lock);
4811 map->ref_count++;
4812 simple_unlock(&map->ref_lock);
4813 }
4814
4815 struct vm_map_kernel *
4816 vm_map_to_kernel(struct vm_map *map)
4817 {
4818
4819 KASSERT(VM_MAP_IS_KERNEL(map));
4820
4821 return (struct vm_map_kernel *)map;
4822 }
4823