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