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