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