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