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