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