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