uvm_map.c revision 1.365 1 /* $NetBSD: uvm_map.c,v 1.365 2019/11/01 08:26:18 rin 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.365 2019/11/01 08:26:18 rin 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 uvm_map_align_va(&hint, align, topdown);
1909
1910 /*
1911 * Look for the first possible address; if there's already
1912 * something at this address, we have to start after it.
1913 */
1914
1915 /*
1916 * @@@: there are four, no, eight cases to consider.
1917 *
1918 * 0: found, fixed, bottom up -> fail
1919 * 1: found, fixed, top down -> fail
1920 * 2: found, not fixed, bottom up -> start after entry->end,
1921 * loop up
1922 * 3: found, not fixed, top down -> start before entry->start,
1923 * loop down
1924 * 4: not found, fixed, bottom up -> check entry->next->start, fail
1925 * 5: not found, fixed, top down -> check entry->next->start, fail
1926 * 6: not found, not fixed, bottom up -> check entry->next->start,
1927 * loop up
1928 * 7: not found, not fixed, top down -> check entry->next->start,
1929 * loop down
1930 *
1931 * as you can see, it reduces to roughly five cases, and that
1932 * adding top down mapping only adds one unique case (without
1933 * it, there would be four cases).
1934 */
1935
1936 if ((flags & UVM_FLAG_FIXED) == 0 && hint == vm_map_min(map)) {
1937 entry = map->first_free;
1938 } else {
1939 if (uvm_map_lookup_entry(map, hint, &entry)) {
1940 /* "hint" address already in use ... */
1941 if (flags & UVM_FLAG_FIXED) {
1942 UVMHIST_LOG(maphist, "<- fixed & VA in use",
1943 0, 0, 0, 0);
1944 return (NULL);
1945 }
1946 if (topdown)
1947 /* Start from lower gap. */
1948 entry = entry->prev;
1949 } else if (flags & UVM_FLAG_FIXED) {
1950 if (entry->next->start >= hint + length &&
1951 hint + length > hint)
1952 goto found;
1953
1954 /* "hint" address is gap but too small */
1955 UVMHIST_LOG(maphist, "<- fixed mapping failed",
1956 0, 0, 0, 0);
1957 return (NULL); /* only one shot at it ... */
1958 } else {
1959 /*
1960 * See if given hint fits in this gap.
1961 */
1962 switch (uvm_map_space_avail(&hint, length,
1963 uoffset, align, flags, topdown, entry)) {
1964 case 1:
1965 goto found;
1966 case -1:
1967 goto wraparound;
1968 }
1969
1970 if (topdown) {
1971 /*
1972 * Still there is a chance to fit
1973 * if hint > entry->end.
1974 */
1975 } else {
1976 /* Start from higher gap. */
1977 entry = entry->next;
1978 if (entry == &map->header)
1979 goto notfound;
1980 goto nextgap;
1981 }
1982 }
1983 }
1984
1985 /*
1986 * Note that all UVM_FLAGS_FIXED case is already handled.
1987 */
1988 KDASSERT((flags & UVM_FLAG_FIXED) == 0);
1989
1990 /* Try to find the space in the red-black tree */
1991
1992 /* Check slot before any entry */
1993 hint = topdown ? entry->next->start - length : entry->end;
1994 switch (uvm_map_space_avail(&hint, length, uoffset, align, flags,
1995 topdown, entry)) {
1996 case 1:
1997 goto found;
1998 case -1:
1999 goto wraparound;
2000 }
2001
2002 nextgap:
2003 KDASSERT((flags & UVM_FLAG_FIXED) == 0);
2004 /* If there is not enough space in the whole tree, we fail */
2005 tmp = ROOT_ENTRY(map);
2006 if (tmp == NULL || tmp->maxgap < length)
2007 goto notfound;
2008
2009 prev = NULL; /* previous candidate */
2010
2011 /* Find an entry close to hint that has enough space */
2012 for (; tmp;) {
2013 KASSERT(tmp->next->start == tmp->end + tmp->gap);
2014 if (topdown) {
2015 if (tmp->next->start < hint + length &&
2016 (prev == NULL || tmp->end > prev->end)) {
2017 if (tmp->gap >= length)
2018 prev = tmp;
2019 else if ((child = LEFT_ENTRY(tmp)) != NULL
2020 && child->maxgap >= length)
2021 prev = tmp;
2022 }
2023 } else {
2024 if (tmp->end >= hint &&
2025 (prev == NULL || tmp->end < prev->end)) {
2026 if (tmp->gap >= length)
2027 prev = tmp;
2028 else if ((child = RIGHT_ENTRY(tmp)) != NULL
2029 && child->maxgap >= length)
2030 prev = tmp;
2031 }
2032 }
2033 if (tmp->next->start < hint + length)
2034 child = RIGHT_ENTRY(tmp);
2035 else if (tmp->end > hint)
2036 child = LEFT_ENTRY(tmp);
2037 else {
2038 if (tmp->gap >= length)
2039 break;
2040 if (topdown)
2041 child = LEFT_ENTRY(tmp);
2042 else
2043 child = RIGHT_ENTRY(tmp);
2044 }
2045 if (child == NULL || child->maxgap < length)
2046 break;
2047 tmp = child;
2048 }
2049
2050 if (tmp != NULL && tmp->start < hint && hint < tmp->next->start) {
2051 /*
2052 * Check if the entry that we found satifies the
2053 * space requirement
2054 */
2055 if (topdown) {
2056 if (hint > tmp->next->start - length)
2057 hint = tmp->next->start - length;
2058 } else {
2059 if (hint < tmp->end)
2060 hint = tmp->end;
2061 }
2062 switch (uvm_map_space_avail(&hint, length, uoffset, align,
2063 flags, topdown, tmp)) {
2064 case 1:
2065 entry = tmp;
2066 goto found;
2067 case -1:
2068 goto wraparound;
2069 }
2070 if (tmp->gap >= length)
2071 goto listsearch;
2072 }
2073 if (prev == NULL)
2074 goto notfound;
2075
2076 if (topdown) {
2077 KASSERT(orig_hint >= prev->next->start - length ||
2078 prev->next->start - length > prev->next->start);
2079 hint = prev->next->start - length;
2080 } else {
2081 KASSERT(orig_hint <= prev->end);
2082 hint = prev->end;
2083 }
2084 switch (uvm_map_space_avail(&hint, length, uoffset, align,
2085 flags, topdown, prev)) {
2086 case 1:
2087 entry = prev;
2088 goto found;
2089 case -1:
2090 goto wraparound;
2091 }
2092 if (prev->gap >= length)
2093 goto listsearch;
2094
2095 if (topdown)
2096 tmp = LEFT_ENTRY(prev);
2097 else
2098 tmp = RIGHT_ENTRY(prev);
2099 for (;;) {
2100 KASSERT(tmp && tmp->maxgap >= length);
2101 if (topdown)
2102 child = RIGHT_ENTRY(tmp);
2103 else
2104 child = LEFT_ENTRY(tmp);
2105 if (child && child->maxgap >= length) {
2106 tmp = child;
2107 continue;
2108 }
2109 if (tmp->gap >= length)
2110 break;
2111 if (topdown)
2112 tmp = LEFT_ENTRY(tmp);
2113 else
2114 tmp = RIGHT_ENTRY(tmp);
2115 }
2116
2117 if (topdown) {
2118 KASSERT(orig_hint >= tmp->next->start - length ||
2119 tmp->next->start - length > tmp->next->start);
2120 hint = tmp->next->start - length;
2121 } else {
2122 KASSERT(orig_hint <= tmp->end);
2123 hint = tmp->end;
2124 }
2125 switch (uvm_map_space_avail(&hint, length, uoffset, align,
2126 flags, topdown, tmp)) {
2127 case 1:
2128 entry = tmp;
2129 goto found;
2130 case -1:
2131 goto wraparound;
2132 }
2133
2134 /*
2135 * The tree fails to find an entry because of offset or alignment
2136 * restrictions. Search the list instead.
2137 */
2138 listsearch:
2139 /*
2140 * Look through the rest of the map, trying to fit a new region in
2141 * the gap between existing regions, or after the very last region.
2142 * note: entry->end = base VA of current gap,
2143 * entry->next->start = VA of end of current gap
2144 */
2145
2146 for (;;) {
2147 /* Update hint for current gap. */
2148 hint = topdown ? entry->next->start - length : entry->end;
2149
2150 /* See if it fits. */
2151 switch (uvm_map_space_avail(&hint, length, uoffset, align,
2152 flags, topdown, entry)) {
2153 case 1:
2154 goto found;
2155 case -1:
2156 goto wraparound;
2157 }
2158
2159 /* Advance to next/previous gap */
2160 if (topdown) {
2161 if (entry == &map->header) {
2162 UVMHIST_LOG(maphist, "<- failed (off start)",
2163 0,0,0,0);
2164 goto notfound;
2165 }
2166 entry = entry->prev;
2167 } else {
2168 entry = entry->next;
2169 if (entry == &map->header) {
2170 UVMHIST_LOG(maphist, "<- failed (off end)",
2171 0,0,0,0);
2172 goto notfound;
2173 }
2174 }
2175 }
2176
2177 found:
2178 SAVE_HINT(map, map->hint, entry);
2179 *result = hint;
2180 UVMHIST_LOG(maphist,"<- got it! (result=%#jx)", hint, 0,0,0);
2181 KASSERTMSG( topdown || hint >= orig_hint, "hint: %jx, orig_hint: %jx",
2182 (uintmax_t)hint, (uintmax_t)orig_hint);
2183 KASSERTMSG(!topdown || hint <= orig_hint, "hint: %jx, orig_hint: %jx",
2184 (uintmax_t)hint, (uintmax_t)orig_hint);
2185 KASSERT(entry->end <= hint);
2186 KASSERT(hint + length <= entry->next->start);
2187 return (entry);
2188
2189 wraparound:
2190 UVMHIST_LOG(maphist, "<- failed (wrap around)", 0,0,0,0);
2191
2192 return (NULL);
2193
2194 notfound:
2195 UVMHIST_LOG(maphist, "<- failed (notfound)", 0,0,0,0);
2196
2197 return (NULL);
2198 }
2199
2200 /*
2201 * U N M A P - m a i n h e l p e r f u n c t i o n s
2202 */
2203
2204 /*
2205 * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop")
2206 *
2207 * => caller must check alignment and size
2208 * => map must be locked by caller
2209 * => we return a list of map entries that we've remove from the map
2210 * in "entry_list"
2211 */
2212
2213 void
2214 uvm_unmap_remove(struct vm_map *map, vaddr_t start, vaddr_t end,
2215 struct vm_map_entry **entry_list /* OUT */, int flags)
2216 {
2217 struct vm_map_entry *entry, *first_entry, *next;
2218 vaddr_t len;
2219 UVMHIST_FUNC("uvm_unmap_remove"); UVMHIST_CALLED(maphist);
2220
2221 UVMHIST_LOG(maphist,"(map=%#jx, start=%#jx, end=%#jx)",
2222 (uintptr_t)map, start, end, 0);
2223 VM_MAP_RANGE_CHECK(map, start, end);
2224
2225 uvm_map_check(map, "unmap_remove entry");
2226
2227 /*
2228 * find first entry
2229 */
2230
2231 if (uvm_map_lookup_entry(map, start, &first_entry) == true) {
2232 /* clip and go... */
2233 entry = first_entry;
2234 UVM_MAP_CLIP_START(map, entry, start);
2235 /* critical! prevents stale hint */
2236 SAVE_HINT(map, entry, entry->prev);
2237 } else {
2238 entry = first_entry->next;
2239 }
2240
2241 /*
2242 * Save the free space hint
2243 */
2244
2245 if (map->first_free != &map->header && map->first_free->start >= start)
2246 map->first_free = entry->prev;
2247
2248 /*
2249 * note: we now re-use first_entry for a different task. we remove
2250 * a number of map entries from the map and save them in a linked
2251 * list headed by "first_entry". once we remove them from the map
2252 * the caller should unlock the map and drop the references to the
2253 * backing objects [c.f. uvm_unmap_detach]. the object is to
2254 * separate unmapping from reference dropping. why?
2255 * [1] the map has to be locked for unmapping
2256 * [2] the map need not be locked for reference dropping
2257 * [3] dropping references may trigger pager I/O, and if we hit
2258 * a pager that does synchronous I/O we may have to wait for it.
2259 * [4] we would like all waiting for I/O to occur with maps unlocked
2260 * so that we don't block other threads.
2261 */
2262
2263 first_entry = NULL;
2264 *entry_list = NULL;
2265
2266 /*
2267 * break up the area into map entry sized regions and unmap. note
2268 * that all mappings have to be removed before we can even consider
2269 * dropping references to amaps or VM objects (otherwise we could end
2270 * up with a mapping to a page on the free list which would be very bad)
2271 */
2272
2273 while ((entry != &map->header) && (entry->start < end)) {
2274 KASSERT((entry->flags & UVM_MAP_STATIC) == 0);
2275
2276 UVM_MAP_CLIP_END(map, entry, end);
2277 next = entry->next;
2278 len = entry->end - entry->start;
2279
2280 /*
2281 * unwire before removing addresses from the pmap; otherwise
2282 * unwiring will put the entries back into the pmap (XXX).
2283 */
2284
2285 if (VM_MAPENT_ISWIRED(entry)) {
2286 uvm_map_entry_unwire(map, entry);
2287 }
2288 if (flags & UVM_FLAG_VAONLY) {
2289
2290 /* nothing */
2291
2292 } else if ((map->flags & VM_MAP_PAGEABLE) == 0) {
2293
2294 /*
2295 * if the map is non-pageable, any pages mapped there
2296 * must be wired and entered with pmap_kenter_pa(),
2297 * and we should free any such pages immediately.
2298 * this is mostly used for kmem_map.
2299 */
2300 KASSERT(vm_map_pmap(map) == pmap_kernel());
2301
2302 uvm_km_pgremove_intrsafe(map, entry->start, entry->end);
2303 } else if (UVM_ET_ISOBJ(entry) &&
2304 UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)) {
2305 panic("%s: kernel object %p %p\n",
2306 __func__, map, entry);
2307 } else if (UVM_ET_ISOBJ(entry) || entry->aref.ar_amap) {
2308 /*
2309 * remove mappings the standard way. lock object
2310 * and/or amap to ensure vm_page state does not
2311 * change while in pmap_remove().
2312 */
2313
2314 uvm_map_lock_entry(entry);
2315 pmap_remove(map->pmap, entry->start, entry->end);
2316 uvm_map_unlock_entry(entry);
2317 }
2318
2319 #if defined(UVMDEBUG)
2320 /*
2321 * check if there's remaining mapping,
2322 * which is a bug in caller.
2323 */
2324
2325 vaddr_t va;
2326 for (va = entry->start; va < entry->end;
2327 va += PAGE_SIZE) {
2328 if (pmap_extract(vm_map_pmap(map), va, NULL)) {
2329 panic("%s: %#"PRIxVADDR" has mapping",
2330 __func__, va);
2331 }
2332 }
2333
2334 if (VM_MAP_IS_KERNEL(map) && (flags & UVM_FLAG_NOWAIT) == 0) {
2335 uvm_km_check_empty(map, entry->start,
2336 entry->end);
2337 }
2338 #endif /* defined(UVMDEBUG) */
2339
2340 /*
2341 * remove entry from map and put it on our list of entries
2342 * that we've nuked. then go to next entry.
2343 */
2344
2345 UVMHIST_LOG(maphist, " removed map entry %#jx",
2346 (uintptr_t)entry, 0, 0, 0);
2347
2348 /* critical! prevents stale hint */
2349 SAVE_HINT(map, entry, entry->prev);
2350
2351 uvm_map_entry_unlink(map, entry);
2352 KASSERT(map->size >= len);
2353 map->size -= len;
2354 entry->prev = NULL;
2355 entry->next = first_entry;
2356 first_entry = entry;
2357 entry = next;
2358 }
2359
2360 /*
2361 * Note: if map is dying, leave pmap_update() for pmap_destroy(),
2362 * which will be called later.
2363 */
2364 if ((map->flags & VM_MAP_DYING) == 0) {
2365 pmap_update(vm_map_pmap(map));
2366 } else {
2367 KASSERT(vm_map_pmap(map) != pmap_kernel());
2368 }
2369
2370 uvm_map_check(map, "unmap_remove leave");
2371
2372 /*
2373 * now we've cleaned up the map and are ready for the caller to drop
2374 * references to the mapped objects.
2375 */
2376
2377 *entry_list = first_entry;
2378 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
2379
2380 if (map->flags & VM_MAP_WANTVA) {
2381 mutex_enter(&map->misc_lock);
2382 map->flags &= ~VM_MAP_WANTVA;
2383 cv_broadcast(&map->cv);
2384 mutex_exit(&map->misc_lock);
2385 }
2386 }
2387
2388 /*
2389 * uvm_unmap_detach: drop references in a chain of map entries
2390 *
2391 * => we will free the map entries as we traverse the list.
2392 */
2393
2394 void
2395 uvm_unmap_detach(struct vm_map_entry *first_entry, int flags)
2396 {
2397 struct vm_map_entry *next_entry;
2398 UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist);
2399
2400 while (first_entry) {
2401 KASSERT(!VM_MAPENT_ISWIRED(first_entry));
2402 UVMHIST_LOG(maphist,
2403 " detach %#jx: amap=%#jx, obj=%#jx, submap?=%jd",
2404 (uintptr_t)first_entry,
2405 (uintptr_t)first_entry->aref.ar_amap,
2406 (uintptr_t)first_entry->object.uvm_obj,
2407 UVM_ET_ISSUBMAP(first_entry));
2408
2409 /*
2410 * drop reference to amap, if we've got one
2411 */
2412
2413 if (first_entry->aref.ar_amap)
2414 uvm_map_unreference_amap(first_entry, flags);
2415
2416 /*
2417 * drop reference to our backing object, if we've got one
2418 */
2419
2420 KASSERT(!UVM_ET_ISSUBMAP(first_entry));
2421 if (UVM_ET_ISOBJ(first_entry) &&
2422 first_entry->object.uvm_obj->pgops->pgo_detach) {
2423 (*first_entry->object.uvm_obj->pgops->pgo_detach)
2424 (first_entry->object.uvm_obj);
2425 }
2426 next_entry = first_entry->next;
2427 uvm_mapent_free(first_entry);
2428 first_entry = next_entry;
2429 }
2430 UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
2431 }
2432
2433 /*
2434 * E X T R A C T I O N F U N C T I O N S
2435 */
2436
2437 /*
2438 * uvm_map_reserve: reserve space in a vm_map for future use.
2439 *
2440 * => we reserve space in a map by putting a dummy map entry in the
2441 * map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE)
2442 * => map should be unlocked (we will write lock it)
2443 * => we return true if we were able to reserve space
2444 * => XXXCDC: should be inline?
2445 */
2446
2447 int
2448 uvm_map_reserve(struct vm_map *map, vsize_t size,
2449 vaddr_t offset /* hint for pmap_prefer */,
2450 vsize_t align /* alignment */,
2451 vaddr_t *raddr /* IN:hint, OUT: reserved VA */,
2452 uvm_flag_t flags /* UVM_FLAG_FIXED or UVM_FLAG_COLORMATCH or 0 */)
2453 {
2454 UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist);
2455
2456 UVMHIST_LOG(maphist, "(map=%#jx, size=%#jx, offset=%#jx, addr=%#jx)",
2457 (uintptr_t)map, size, offset, (uintptr_t)raddr);
2458
2459 size = round_page(size);
2460
2461 /*
2462 * reserve some virtual space.
2463 */
2464
2465 if (uvm_map(map, raddr, size, NULL, offset, align,
2466 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
2467 UVM_ADV_RANDOM, UVM_FLAG_NOMERGE|flags)) != 0) {
2468 UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
2469 return (false);
2470 }
2471
2472 UVMHIST_LOG(maphist, "<- done (*raddr=%#jx)", *raddr,0,0,0);
2473 return (true);
2474 }
2475
2476 /*
2477 * uvm_map_replace: replace a reserved (blank) area of memory with
2478 * real mappings.
2479 *
2480 * => caller must WRITE-LOCK the map
2481 * => we return true if replacement was a success
2482 * => we expect the newents chain to have nnewents entrys on it and
2483 * we expect newents->prev to point to the last entry on the list
2484 * => note newents is allowed to be NULL
2485 */
2486
2487 static int
2488 uvm_map_replace(struct vm_map *map, vaddr_t start, vaddr_t end,
2489 struct vm_map_entry *newents, int nnewents, vsize_t nsize,
2490 struct vm_map_entry **oldentryp)
2491 {
2492 struct vm_map_entry *oldent, *last;
2493
2494 uvm_map_check(map, "map_replace entry");
2495
2496 /*
2497 * first find the blank map entry at the specified address
2498 */
2499
2500 if (!uvm_map_lookup_entry(map, start, &oldent)) {
2501 return (false);
2502 }
2503
2504 /*
2505 * check to make sure we have a proper blank entry
2506 */
2507
2508 if (end < oldent->end) {
2509 UVM_MAP_CLIP_END(map, oldent, end);
2510 }
2511 if (oldent->start != start || oldent->end != end ||
2512 oldent->object.uvm_obj != NULL || oldent->aref.ar_amap != NULL) {
2513 return (false);
2514 }
2515
2516 #ifdef DIAGNOSTIC
2517
2518 /*
2519 * sanity check the newents chain
2520 */
2521
2522 {
2523 struct vm_map_entry *tmpent = newents;
2524 int nent = 0;
2525 vsize_t sz = 0;
2526 vaddr_t cur = start;
2527
2528 while (tmpent) {
2529 nent++;
2530 sz += tmpent->end - tmpent->start;
2531 if (tmpent->start < cur)
2532 panic("uvm_map_replace1");
2533 if (tmpent->start >= tmpent->end || tmpent->end > end) {
2534 panic("uvm_map_replace2: "
2535 "tmpent->start=%#"PRIxVADDR
2536 ", tmpent->end=%#"PRIxVADDR
2537 ", end=%#"PRIxVADDR,
2538 tmpent->start, tmpent->end, end);
2539 }
2540 cur = tmpent->end;
2541 if (tmpent->next) {
2542 if (tmpent->next->prev != tmpent)
2543 panic("uvm_map_replace3");
2544 } else {
2545 if (newents->prev != tmpent)
2546 panic("uvm_map_replace4");
2547 }
2548 tmpent = tmpent->next;
2549 }
2550 if (nent != nnewents)
2551 panic("uvm_map_replace5");
2552 if (sz != nsize)
2553 panic("uvm_map_replace6");
2554 }
2555 #endif
2556
2557 /*
2558 * map entry is a valid blank! replace it. (this does all the
2559 * work of map entry link/unlink...).
2560 */
2561
2562 if (newents) {
2563 last = newents->prev;
2564
2565 /* critical: flush stale hints out of map */
2566 SAVE_HINT(map, map->hint, newents);
2567 if (map->first_free == oldent)
2568 map->first_free = last;
2569
2570 last->next = oldent->next;
2571 last->next->prev = last;
2572
2573 /* Fix RB tree */
2574 uvm_rb_remove(map, oldent);
2575
2576 newents->prev = oldent->prev;
2577 newents->prev->next = newents;
2578 map->nentries = map->nentries + (nnewents - 1);
2579
2580 /* Fixup the RB tree */
2581 {
2582 int i;
2583 struct vm_map_entry *tmp;
2584
2585 tmp = newents;
2586 for (i = 0; i < nnewents && tmp; i++) {
2587 uvm_rb_insert(map, tmp);
2588 tmp = tmp->next;
2589 }
2590 }
2591 } else {
2592 /* NULL list of new entries: just remove the old one */
2593 clear_hints(map, oldent);
2594 uvm_map_entry_unlink(map, oldent);
2595 }
2596 map->size -= end - start - nsize;
2597
2598 uvm_map_check(map, "map_replace leave");
2599
2600 /*
2601 * now we can free the old blank entry and return.
2602 */
2603
2604 *oldentryp = oldent;
2605 return (true);
2606 }
2607
2608 /*
2609 * uvm_map_extract: extract a mapping from a map and put it somewhere
2610 * (maybe removing the old mapping)
2611 *
2612 * => maps should be unlocked (we will write lock them)
2613 * => returns 0 on success, error code otherwise
2614 * => start must be page aligned
2615 * => len must be page sized
2616 * => flags:
2617 * UVM_EXTRACT_REMOVE: remove mappings from srcmap
2618 * UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only)
2619 * UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs
2620 * UVM_EXTRACT_FIXPROT: set prot to maxprot as we go
2621 * UVM_EXTRACT_PROT_ALL: set prot to UVM_PROT_ALL as we go
2622 * >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<<
2623 * >>>NOTE: QREF's must be unmapped via the QREF path, thus should only
2624 * be used from within the kernel in a kernel level map <<<
2625 */
2626
2627 int
2628 uvm_map_extract(struct vm_map *srcmap, vaddr_t start, vsize_t len,
2629 struct vm_map *dstmap, vaddr_t *dstaddrp, int flags)
2630 {
2631 vaddr_t dstaddr, end, newend, oldoffset, fudge, orig_fudge;
2632 struct vm_map_entry *chain, *endchain, *entry, *orig_entry, *newentry,
2633 *deadentry, *oldentry;
2634 struct vm_map_entry *resentry = NULL; /* a dummy reservation entry */
2635 vsize_t elen __unused;
2636 int nchain, error, copy_ok;
2637 vsize_t nsize;
2638 UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist);
2639
2640 UVMHIST_LOG(maphist,"(srcmap=%#jx,start=%#jx, len=%#jx",
2641 (uintptr_t)srcmap, start, len, 0);
2642 UVMHIST_LOG(maphist," ...,dstmap=%#jx, flags=%#jx)",
2643 (uintptr_t)dstmap, flags, 0, 0);
2644
2645 /*
2646 * step 0: sanity check: start must be on a page boundary, length
2647 * must be page sized. can't ask for CONTIG/QREF if you asked for
2648 * REMOVE.
2649 */
2650
2651 KASSERT((start & PAGE_MASK) == 0 && (len & PAGE_MASK) == 0);
2652 KASSERT((flags & UVM_EXTRACT_REMOVE) == 0 ||
2653 (flags & (UVM_EXTRACT_CONTIG|UVM_EXTRACT_QREF)) == 0);
2654
2655 /*
2656 * step 1: reserve space in the target map for the extracted area
2657 */
2658
2659 if ((flags & UVM_EXTRACT_RESERVED) == 0) {
2660 dstaddr = vm_map_min(dstmap);
2661 if (!uvm_map_reserve(dstmap, len, start,
2662 atop(start) & uvmexp.colormask, &dstaddr,
2663 UVM_FLAG_COLORMATCH))
2664 return (ENOMEM);
2665 KASSERT((atop(start ^ dstaddr) & uvmexp.colormask) == 0);
2666 *dstaddrp = dstaddr; /* pass address back to caller */
2667 UVMHIST_LOG(maphist, " dstaddr=%#jx", dstaddr,0,0,0);
2668 } else {
2669 dstaddr = *dstaddrp;
2670 }
2671
2672 /*
2673 * step 2: setup for the extraction process loop by init'ing the
2674 * map entry chain, locking src map, and looking up the first useful
2675 * entry in the map.
2676 */
2677
2678 end = start + len;
2679 newend = dstaddr + len;
2680 chain = endchain = NULL;
2681 nchain = 0;
2682 nsize = 0;
2683 vm_map_lock(srcmap);
2684
2685 if (uvm_map_lookup_entry(srcmap, start, &entry)) {
2686
2687 /* "start" is within an entry */
2688 if (flags & UVM_EXTRACT_QREF) {
2689
2690 /*
2691 * for quick references we don't clip the entry, so
2692 * the entry may map space "before" the starting
2693 * virtual address... this is the "fudge" factor
2694 * (which can be non-zero only the first time
2695 * through the "while" loop in step 3).
2696 */
2697
2698 fudge = start - entry->start;
2699 } else {
2700
2701 /*
2702 * normal reference: we clip the map to fit (thus
2703 * fudge is zero)
2704 */
2705
2706 UVM_MAP_CLIP_START(srcmap, entry, start);
2707 SAVE_HINT(srcmap, srcmap->hint, entry->prev);
2708 fudge = 0;
2709 }
2710 } else {
2711
2712 /* "start" is not within an entry ... skip to next entry */
2713 if (flags & UVM_EXTRACT_CONTIG) {
2714 error = EINVAL;
2715 goto bad; /* definite hole here ... */
2716 }
2717
2718 entry = entry->next;
2719 fudge = 0;
2720 }
2721
2722 /* save values from srcmap for step 6 */
2723 orig_entry = entry;
2724 orig_fudge = fudge;
2725
2726 /*
2727 * step 3: now start looping through the map entries, extracting
2728 * as we go.
2729 */
2730
2731 while (entry->start < end && entry != &srcmap->header) {
2732
2733 /* if we are not doing a quick reference, clip it */
2734 if ((flags & UVM_EXTRACT_QREF) == 0)
2735 UVM_MAP_CLIP_END(srcmap, entry, end);
2736
2737 /* clear needs_copy (allow chunking) */
2738 if (UVM_ET_ISNEEDSCOPY(entry)) {
2739 amap_copy(srcmap, entry,
2740 AMAP_COPY_NOWAIT|AMAP_COPY_NOMERGE, start, end);
2741 if (UVM_ET_ISNEEDSCOPY(entry)) { /* failed? */
2742 error = ENOMEM;
2743 goto bad;
2744 }
2745
2746 /* amap_copy could clip (during chunk)! update fudge */
2747 if (fudge) {
2748 fudge = start - entry->start;
2749 orig_fudge = fudge;
2750 }
2751 }
2752
2753 /* calculate the offset of this from "start" */
2754 oldoffset = (entry->start + fudge) - start;
2755
2756 /* allocate a new map entry */
2757 newentry = uvm_mapent_alloc(dstmap, 0);
2758 if (newentry == NULL) {
2759 error = ENOMEM;
2760 goto bad;
2761 }
2762
2763 /* set up new map entry */
2764 newentry->next = NULL;
2765 newentry->prev = endchain;
2766 newentry->start = dstaddr + oldoffset;
2767 newentry->end =
2768 newentry->start + (entry->end - (entry->start + fudge));
2769 if (newentry->end > newend || newentry->end < newentry->start)
2770 newentry->end = newend;
2771 newentry->object.uvm_obj = entry->object.uvm_obj;
2772 if (newentry->object.uvm_obj) {
2773 if (newentry->object.uvm_obj->pgops->pgo_reference)
2774 newentry->object.uvm_obj->pgops->
2775 pgo_reference(newentry->object.uvm_obj);
2776 newentry->offset = entry->offset + fudge;
2777 } else {
2778 newentry->offset = 0;
2779 }
2780 newentry->etype = entry->etype;
2781 if (flags & UVM_EXTRACT_PROT_ALL) {
2782 newentry->protection = newentry->max_protection =
2783 UVM_PROT_ALL;
2784 } else {
2785 newentry->protection = (flags & UVM_EXTRACT_FIXPROT) ?
2786 entry->max_protection : entry->protection;
2787 newentry->max_protection = entry->max_protection;
2788 }
2789 newentry->inheritance = entry->inheritance;
2790 newentry->wired_count = 0;
2791 newentry->aref.ar_amap = entry->aref.ar_amap;
2792 if (newentry->aref.ar_amap) {
2793 newentry->aref.ar_pageoff =
2794 entry->aref.ar_pageoff + (fudge >> PAGE_SHIFT);
2795 uvm_map_reference_amap(newentry, AMAP_SHARED |
2796 ((flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0));
2797 } else {
2798 newentry->aref.ar_pageoff = 0;
2799 }
2800 newentry->advice = entry->advice;
2801 if ((flags & UVM_EXTRACT_QREF) != 0) {
2802 newentry->flags |= UVM_MAP_NOMERGE;
2803 }
2804
2805 /* now link it on the chain */
2806 nchain++;
2807 nsize += newentry->end - newentry->start;
2808 if (endchain == NULL) {
2809 chain = endchain = newentry;
2810 } else {
2811 endchain->next = newentry;
2812 endchain = newentry;
2813 }
2814
2815 /* end of 'while' loop! */
2816 if ((flags & UVM_EXTRACT_CONTIG) && entry->end < end &&
2817 (entry->next == &srcmap->header ||
2818 entry->next->start != entry->end)) {
2819 error = EINVAL;
2820 goto bad;
2821 }
2822 entry = entry->next;
2823 fudge = 0;
2824 }
2825
2826 /*
2827 * step 4: close off chain (in format expected by uvm_map_replace)
2828 */
2829
2830 if (chain)
2831 chain->prev = endchain;
2832
2833 /*
2834 * step 5: attempt to lock the dest map so we can pmap_copy.
2835 * note usage of copy_ok:
2836 * 1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5)
2837 * 0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7
2838 */
2839
2840 if (srcmap == dstmap || vm_map_lock_try(dstmap) == true) {
2841 copy_ok = 1;
2842 if (!uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
2843 nchain, nsize, &resentry)) {
2844 if (srcmap != dstmap)
2845 vm_map_unlock(dstmap);
2846 error = EIO;
2847 goto bad;
2848 }
2849 } else {
2850 copy_ok = 0;
2851 /* replace defered until step 7 */
2852 }
2853
2854 /*
2855 * step 6: traverse the srcmap a second time to do the following:
2856 * - if we got a lock on the dstmap do pmap_copy
2857 * - if UVM_EXTRACT_REMOVE remove the entries
2858 * we make use of orig_entry and orig_fudge (saved in step 2)
2859 */
2860
2861 if (copy_ok || (flags & UVM_EXTRACT_REMOVE)) {
2862
2863 /* purge possible stale hints from srcmap */
2864 if (flags & UVM_EXTRACT_REMOVE) {
2865 SAVE_HINT(srcmap, srcmap->hint, orig_entry->prev);
2866 if (srcmap->first_free != &srcmap->header &&
2867 srcmap->first_free->start >= start)
2868 srcmap->first_free = orig_entry->prev;
2869 }
2870
2871 entry = orig_entry;
2872 fudge = orig_fudge;
2873 deadentry = NULL; /* for UVM_EXTRACT_REMOVE */
2874
2875 while (entry->start < end && entry != &srcmap->header) {
2876 if (copy_ok) {
2877 oldoffset = (entry->start + fudge) - start;
2878 elen = MIN(end, entry->end) -
2879 (entry->start + fudge);
2880 pmap_copy(dstmap->pmap, srcmap->pmap,
2881 dstaddr + oldoffset, elen,
2882 entry->start + fudge);
2883 }
2884
2885 /* we advance "entry" in the following if statement */
2886 if (flags & UVM_EXTRACT_REMOVE) {
2887 uvm_map_lock_entry(entry);
2888 pmap_remove(srcmap->pmap, entry->start,
2889 entry->end);
2890 uvm_map_unlock_entry(entry);
2891 oldentry = entry; /* save entry */
2892 entry = entry->next; /* advance */
2893 uvm_map_entry_unlink(srcmap, oldentry);
2894 /* add to dead list */
2895 oldentry->next = deadentry;
2896 deadentry = oldentry;
2897 } else {
2898 entry = entry->next; /* advance */
2899 }
2900
2901 /* end of 'while' loop */
2902 fudge = 0;
2903 }
2904 pmap_update(srcmap->pmap);
2905
2906 /*
2907 * unlock dstmap. we will dispose of deadentry in
2908 * step 7 if needed
2909 */
2910
2911 if (copy_ok && srcmap != dstmap)
2912 vm_map_unlock(dstmap);
2913
2914 } else {
2915 deadentry = NULL;
2916 }
2917
2918 /*
2919 * step 7: we are done with the source map, unlock. if copy_ok
2920 * is 0 then we have not replaced the dummy mapping in dstmap yet
2921 * and we need to do so now.
2922 */
2923
2924 vm_map_unlock(srcmap);
2925 if ((flags & UVM_EXTRACT_REMOVE) && deadentry)
2926 uvm_unmap_detach(deadentry, 0); /* dispose of old entries */
2927
2928 /* now do the replacement if we didn't do it in step 5 */
2929 if (copy_ok == 0) {
2930 vm_map_lock(dstmap);
2931 error = uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
2932 nchain, nsize, &resentry);
2933 vm_map_unlock(dstmap);
2934
2935 if (error == false) {
2936 error = EIO;
2937 goto bad2;
2938 }
2939 }
2940
2941 if (resentry != NULL)
2942 uvm_mapent_free(resentry);
2943
2944 return (0);
2945
2946 /*
2947 * bad: failure recovery
2948 */
2949 bad:
2950 vm_map_unlock(srcmap);
2951 bad2: /* src already unlocked */
2952 if (chain)
2953 uvm_unmap_detach(chain,
2954 (flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0);
2955
2956 if (resentry != NULL)
2957 uvm_mapent_free(resentry);
2958
2959 if ((flags & UVM_EXTRACT_RESERVED) == 0) {
2960 uvm_unmap(dstmap, dstaddr, dstaddr+len); /* ??? */
2961 }
2962 return (error);
2963 }
2964
2965 /* end of extraction functions */
2966
2967 /*
2968 * uvm_map_submap: punch down part of a map into a submap
2969 *
2970 * => only the kernel_map is allowed to be submapped
2971 * => the purpose of submapping is to break up the locking granularity
2972 * of a larger map
2973 * => the range specified must have been mapped previously with a uvm_map()
2974 * call [with uobj==NULL] to create a blank map entry in the main map.
2975 * [And it had better still be blank!]
2976 * => maps which contain submaps should never be copied or forked.
2977 * => to remove a submap, use uvm_unmap() on the main map
2978 * and then uvm_map_deallocate() the submap.
2979 * => main map must be unlocked.
2980 * => submap must have been init'd and have a zero reference count.
2981 * [need not be locked as we don't actually reference it]
2982 */
2983
2984 int
2985 uvm_map_submap(struct vm_map *map, vaddr_t start, vaddr_t end,
2986 struct vm_map *submap)
2987 {
2988 struct vm_map_entry *entry;
2989 int error;
2990
2991 vm_map_lock(map);
2992 VM_MAP_RANGE_CHECK(map, start, end);
2993
2994 if (uvm_map_lookup_entry(map, start, &entry)) {
2995 UVM_MAP_CLIP_START(map, entry, start);
2996 UVM_MAP_CLIP_END(map, entry, end); /* to be safe */
2997 } else {
2998 entry = NULL;
2999 }
3000
3001 if (entry != NULL &&
3002 entry->start == start && entry->end == end &&
3003 entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL &&
3004 !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) {
3005 entry->etype |= UVM_ET_SUBMAP;
3006 entry->object.sub_map = submap;
3007 entry->offset = 0;
3008 uvm_map_reference(submap);
3009 error = 0;
3010 } else {
3011 error = EINVAL;
3012 }
3013 vm_map_unlock(map);
3014
3015 return error;
3016 }
3017
3018 /*
3019 * uvm_map_protect_user: change map protection on behalf of the user.
3020 * Enforces PAX settings as necessary.
3021 */
3022 int
3023 uvm_map_protect_user(struct lwp *l, vaddr_t start, vaddr_t end,
3024 vm_prot_t new_prot)
3025 {
3026 int error;
3027
3028 if ((error = PAX_MPROTECT_VALIDATE(l, new_prot)))
3029 return error;
3030
3031 return uvm_map_protect(&l->l_proc->p_vmspace->vm_map, start, end,
3032 new_prot, false);
3033 }
3034
3035
3036 /*
3037 * uvm_map_protect: change map protection
3038 *
3039 * => set_max means set max_protection.
3040 * => map must be unlocked.
3041 */
3042
3043 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
3044 ~VM_PROT_WRITE : VM_PROT_ALL)
3045
3046 int
3047 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
3048 vm_prot_t new_prot, bool set_max)
3049 {
3050 struct vm_map_entry *current, *entry;
3051 int error = 0;
3052 UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist);
3053 UVMHIST_LOG(maphist,"(map=%#jx,start=%#jx,end=%#jx,new_prot=%#jx)",
3054 (uintptr_t)map, start, end, new_prot);
3055
3056 vm_map_lock(map);
3057 VM_MAP_RANGE_CHECK(map, start, end);
3058 if (uvm_map_lookup_entry(map, start, &entry)) {
3059 UVM_MAP_CLIP_START(map, entry, start);
3060 } else {
3061 entry = entry->next;
3062 }
3063
3064 /*
3065 * make a first pass to check for protection violations.
3066 */
3067
3068 current = entry;
3069 while ((current != &map->header) && (current->start < end)) {
3070 if (UVM_ET_ISSUBMAP(current)) {
3071 error = EINVAL;
3072 goto out;
3073 }
3074 if ((new_prot & current->max_protection) != new_prot) {
3075 error = EACCES;
3076 goto out;
3077 }
3078 /*
3079 * Don't allow VM_PROT_EXECUTE to be set on entries that
3080 * point to vnodes that are associated with a NOEXEC file
3081 * system.
3082 */
3083 if (UVM_ET_ISOBJ(current) &&
3084 UVM_OBJ_IS_VNODE(current->object.uvm_obj)) {
3085 struct vnode *vp =
3086 (struct vnode *) current->object.uvm_obj;
3087
3088 if ((new_prot & VM_PROT_EXECUTE) != 0 &&
3089 (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
3090 error = EACCES;
3091 goto out;
3092 }
3093 }
3094
3095 current = current->next;
3096 }
3097
3098 /* go back and fix up protections (no need to clip this time). */
3099
3100 current = entry;
3101 while ((current != &map->header) && (current->start < end)) {
3102 vm_prot_t old_prot;
3103
3104 UVM_MAP_CLIP_END(map, current, end);
3105 old_prot = current->protection;
3106 if (set_max)
3107 current->protection =
3108 (current->max_protection = new_prot) & old_prot;
3109 else
3110 current->protection = new_prot;
3111
3112 /*
3113 * update physical map if necessary. worry about copy-on-write
3114 * here -- CHECK THIS XXX
3115 */
3116
3117 if (current->protection != old_prot) {
3118 /* update pmap! */
3119 uvm_map_lock_entry(current);
3120 pmap_protect(map->pmap, current->start, current->end,
3121 current->protection & MASK(current));
3122 uvm_map_unlock_entry(current);
3123
3124 /*
3125 * If this entry points at a vnode, and the
3126 * protection includes VM_PROT_EXECUTE, mark
3127 * the vnode as VEXECMAP.
3128 */
3129 if (UVM_ET_ISOBJ(current)) {
3130 struct uvm_object *uobj =
3131 current->object.uvm_obj;
3132
3133 if (UVM_OBJ_IS_VNODE(uobj) &&
3134 (current->protection & VM_PROT_EXECUTE)) {
3135 vn_markexec((struct vnode *) uobj);
3136 }
3137 }
3138 }
3139
3140 /*
3141 * If the map is configured to lock any future mappings,
3142 * wire this entry now if the old protection was VM_PROT_NONE
3143 * and the new protection is not VM_PROT_NONE.
3144 */
3145
3146 if ((map->flags & VM_MAP_WIREFUTURE) != 0 &&
3147 VM_MAPENT_ISWIRED(current) == 0 &&
3148 old_prot == VM_PROT_NONE &&
3149 new_prot != VM_PROT_NONE) {
3150
3151 /*
3152 * We must call pmap_update() here because the
3153 * pmap_protect() call above might have removed some
3154 * pmap entries and uvm_map_pageable() might create
3155 * some new pmap entries that rely on the prior
3156 * removals being completely finished.
3157 */
3158
3159 pmap_update(map->pmap);
3160
3161 if (uvm_map_pageable(map, current->start,
3162 current->end, false,
3163 UVM_LK_ENTER|UVM_LK_EXIT) != 0) {
3164
3165 /*
3166 * If locking the entry fails, remember the
3167 * error if it's the first one. Note we
3168 * still continue setting the protection in
3169 * the map, but will return the error
3170 * condition regardless.
3171 *
3172 * XXX Ignore what the actual error is,
3173 * XXX just call it a resource shortage
3174 * XXX so that it doesn't get confused
3175 * XXX what uvm_map_protect() itself would
3176 * XXX normally return.
3177 */
3178
3179 error = ENOMEM;
3180 }
3181 }
3182 current = current->next;
3183 }
3184 pmap_update(map->pmap);
3185
3186 out:
3187 vm_map_unlock(map);
3188
3189 UVMHIST_LOG(maphist, "<- done, error=%jd",error,0,0,0);
3190 return error;
3191 }
3192
3193 #undef MASK
3194
3195 /*
3196 * uvm_map_inherit: set inheritance code for range of addrs in map.
3197 *
3198 * => map must be unlocked
3199 * => note that the inherit code is used during a "fork". see fork
3200 * code for details.
3201 */
3202
3203 int
3204 uvm_map_inherit(struct vm_map *map, vaddr_t start, vaddr_t end,
3205 vm_inherit_t new_inheritance)
3206 {
3207 struct vm_map_entry *entry, *temp_entry;
3208 UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist);
3209 UVMHIST_LOG(maphist,"(map=%#jx,start=%#jx,end=%#jx,new_inh=%#jx)",
3210 (uintptr_t)map, start, end, new_inheritance);
3211
3212 switch (new_inheritance) {
3213 case MAP_INHERIT_NONE:
3214 case MAP_INHERIT_COPY:
3215 case MAP_INHERIT_SHARE:
3216 case MAP_INHERIT_ZERO:
3217 break;
3218 default:
3219 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
3220 return EINVAL;
3221 }
3222
3223 vm_map_lock(map);
3224 VM_MAP_RANGE_CHECK(map, start, end);
3225 if (uvm_map_lookup_entry(map, start, &temp_entry)) {
3226 entry = temp_entry;
3227 UVM_MAP_CLIP_START(map, entry, start);
3228 } else {
3229 entry = temp_entry->next;
3230 }
3231 while ((entry != &map->header) && (entry->start < end)) {
3232 UVM_MAP_CLIP_END(map, entry, end);
3233 entry->inheritance = new_inheritance;
3234 entry = entry->next;
3235 }
3236 vm_map_unlock(map);
3237 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
3238 return 0;
3239 }
3240
3241 /*
3242 * uvm_map_advice: set advice code for range of addrs in map.
3243 *
3244 * => map must be unlocked
3245 */
3246
3247 int
3248 uvm_map_advice(struct vm_map *map, vaddr_t start, vaddr_t end, int new_advice)
3249 {
3250 struct vm_map_entry *entry, *temp_entry;
3251 UVMHIST_FUNC("uvm_map_advice"); UVMHIST_CALLED(maphist);
3252 UVMHIST_LOG(maphist,"(map=%#jx,start=%#jx,end=%#jx,new_adv=%#jx)",
3253 (uintptr_t)map, start, end, new_advice);
3254
3255 vm_map_lock(map);
3256 VM_MAP_RANGE_CHECK(map, start, end);
3257 if (uvm_map_lookup_entry(map, start, &temp_entry)) {
3258 entry = temp_entry;
3259 UVM_MAP_CLIP_START(map, entry, start);
3260 } else {
3261 entry = temp_entry->next;
3262 }
3263
3264 /*
3265 * XXXJRT: disallow holes?
3266 */
3267
3268 while ((entry != &map->header) && (entry->start < end)) {
3269 UVM_MAP_CLIP_END(map, entry, end);
3270
3271 switch (new_advice) {
3272 case MADV_NORMAL:
3273 case MADV_RANDOM:
3274 case MADV_SEQUENTIAL:
3275 /* nothing special here */
3276 break;
3277
3278 default:
3279 vm_map_unlock(map);
3280 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
3281 return EINVAL;
3282 }
3283 entry->advice = new_advice;
3284 entry = entry->next;
3285 }
3286
3287 vm_map_unlock(map);
3288 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
3289 return 0;
3290 }
3291
3292 /*
3293 * uvm_map_willneed: apply MADV_WILLNEED
3294 */
3295
3296 int
3297 uvm_map_willneed(struct vm_map *map, vaddr_t start, vaddr_t end)
3298 {
3299 struct vm_map_entry *entry;
3300 UVMHIST_FUNC("uvm_map_willneed"); UVMHIST_CALLED(maphist);
3301 UVMHIST_LOG(maphist,"(map=%#jx,start=%#jx,end=%#jx)",
3302 (uintptr_t)map, start, end, 0);
3303
3304 vm_map_lock_read(map);
3305 VM_MAP_RANGE_CHECK(map, start, end);
3306 if (!uvm_map_lookup_entry(map, start, &entry)) {
3307 entry = entry->next;
3308 }
3309 while (entry->start < end) {
3310 struct vm_amap * const amap = entry->aref.ar_amap;
3311 struct uvm_object * const uobj = entry->object.uvm_obj;
3312
3313 KASSERT(entry != &map->header);
3314 KASSERT(start < entry->end);
3315 /*
3316 * For now, we handle only the easy but commonly-requested case.
3317 * ie. start prefetching of backing uobj pages.
3318 *
3319 * XXX It might be useful to pmap_enter() the already-in-core
3320 * pages by inventing a "weak" mode for uvm_fault() which would
3321 * only do the PGO_LOCKED pgo_get().
3322 */
3323 if (UVM_ET_ISOBJ(entry) && amap == NULL && uobj != NULL) {
3324 off_t offset;
3325 off_t size;
3326
3327 offset = entry->offset;
3328 if (start < entry->start) {
3329 offset += entry->start - start;
3330 }
3331 size = entry->offset + (entry->end - entry->start);
3332 if (entry->end < end) {
3333 size -= end - entry->end;
3334 }
3335 uvm_readahead(uobj, offset, size);
3336 }
3337 entry = entry->next;
3338 }
3339 vm_map_unlock_read(map);
3340 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
3341 return 0;
3342 }
3343
3344 /*
3345 * uvm_map_pageable: sets the pageability of a range in a map.
3346 *
3347 * => wires map entries. should not be used for transient page locking.
3348 * for that, use uvm_fault_wire()/uvm_fault_unwire() (see uvm_vslock()).
3349 * => regions specified as not pageable require lock-down (wired) memory
3350 * and page tables.
3351 * => map must never be read-locked
3352 * => if islocked is true, map is already write-locked
3353 * => we always unlock the map, since we must downgrade to a read-lock
3354 * to call uvm_fault_wire()
3355 * => XXXCDC: check this and try and clean it up.
3356 */
3357
3358 int
3359 uvm_map_pageable(struct vm_map *map, vaddr_t start, vaddr_t end,
3360 bool new_pageable, int lockflags)
3361 {
3362 struct vm_map_entry *entry, *start_entry, *failed_entry;
3363 int rv;
3364 #ifdef DIAGNOSTIC
3365 u_int timestamp_save;
3366 #endif
3367 UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist);
3368 UVMHIST_LOG(maphist,"(map=%#jx,start=%#jx,end=%#jx,new_pageable=%ju)",
3369 (uintptr_t)map, start, end, new_pageable);
3370 KASSERT(map->flags & VM_MAP_PAGEABLE);
3371
3372 if ((lockflags & UVM_LK_ENTER) == 0)
3373 vm_map_lock(map);
3374 VM_MAP_RANGE_CHECK(map, start, end);
3375
3376 /*
3377 * only one pageability change may take place at one time, since
3378 * uvm_fault_wire assumes it will be called only once for each
3379 * wiring/unwiring. therefore, we have to make sure we're actually
3380 * changing the pageability for the entire region. we do so before
3381 * making any changes.
3382 */
3383
3384 if (uvm_map_lookup_entry(map, start, &start_entry) == false) {
3385 if ((lockflags & UVM_LK_EXIT) == 0)
3386 vm_map_unlock(map);
3387
3388 UVMHIST_LOG(maphist,"<- done (fault)",0,0,0,0);
3389 return EFAULT;
3390 }
3391 entry = start_entry;
3392
3393 if (start == end) { /* nothing required */
3394 if ((lockflags & UVM_LK_EXIT) == 0)
3395 vm_map_unlock(map);
3396
3397 UVMHIST_LOG(maphist,"<- done (nothing)",0,0,0,0);
3398 return 0;
3399 }
3400
3401 /*
3402 * handle wiring and unwiring separately.
3403 */
3404
3405 if (new_pageable) { /* unwire */
3406 UVM_MAP_CLIP_START(map, entry, start);
3407
3408 /*
3409 * unwiring. first ensure that the range to be unwired is
3410 * really wired down and that there are no holes.
3411 */
3412
3413 while ((entry != &map->header) && (entry->start < end)) {
3414 if (entry->wired_count == 0 ||
3415 (entry->end < end &&
3416 (entry->next == &map->header ||
3417 entry->next->start > entry->end))) {
3418 if ((lockflags & UVM_LK_EXIT) == 0)
3419 vm_map_unlock(map);
3420 UVMHIST_LOG(maphist, "<- done (INVAL)",0,0,0,0);
3421 return EINVAL;
3422 }
3423 entry = entry->next;
3424 }
3425
3426 /*
3427 * POSIX 1003.1b - a single munlock call unlocks a region,
3428 * regardless of the number of mlock calls made on that
3429 * region.
3430 */
3431
3432 entry = start_entry;
3433 while ((entry != &map->header) && (entry->start < end)) {
3434 UVM_MAP_CLIP_END(map, entry, end);
3435 if (VM_MAPENT_ISWIRED(entry))
3436 uvm_map_entry_unwire(map, entry);
3437 entry = entry->next;
3438 }
3439 if ((lockflags & UVM_LK_EXIT) == 0)
3440 vm_map_unlock(map);
3441 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
3442 return 0;
3443 }
3444
3445 /*
3446 * wire case: in two passes [XXXCDC: ugly block of code here]
3447 *
3448 * 1: holding the write lock, we create any anonymous maps that need
3449 * to be created. then we clip each map entry to the region to
3450 * be wired and increment its wiring count.
3451 *
3452 * 2: we downgrade to a read lock, and call uvm_fault_wire to fault
3453 * in the pages for any newly wired area (wired_count == 1).
3454 *
3455 * downgrading to a read lock for uvm_fault_wire avoids a possible
3456 * deadlock with another thread that may have faulted on one of
3457 * the pages to be wired (it would mark the page busy, blocking
3458 * us, then in turn block on the map lock that we hold). because
3459 * of problems in the recursive lock package, we cannot upgrade
3460 * to a write lock in vm_map_lookup. thus, any actions that
3461 * require the write lock must be done beforehand. because we
3462 * keep the read lock on the map, the copy-on-write status of the
3463 * entries we modify here cannot change.
3464 */
3465
3466 while ((entry != &map->header) && (entry->start < end)) {
3467 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3468
3469 /*
3470 * perform actions of vm_map_lookup that need the
3471 * write lock on the map: create an anonymous map
3472 * for a copy-on-write region, or an anonymous map
3473 * for a zero-fill region. (XXXCDC: submap case
3474 * ok?)
3475 */
3476
3477 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */
3478 if (UVM_ET_ISNEEDSCOPY(entry) &&
3479 ((entry->max_protection & VM_PROT_WRITE) ||
3480 (entry->object.uvm_obj == NULL))) {
3481 amap_copy(map, entry, 0, start, end);
3482 /* XXXCDC: wait OK? */
3483 }
3484 }
3485 }
3486 UVM_MAP_CLIP_START(map, entry, start);
3487 UVM_MAP_CLIP_END(map, entry, end);
3488 entry->wired_count++;
3489
3490 /*
3491 * Check for holes
3492 */
3493
3494 if (entry->protection == VM_PROT_NONE ||
3495 (entry->end < end &&
3496 (entry->next == &map->header ||
3497 entry->next->start > entry->end))) {
3498
3499 /*
3500 * found one. amap creation actions do not need to
3501 * be undone, but the wired counts need to be restored.
3502 */
3503
3504 while (entry != &map->header && entry->end > start) {
3505 entry->wired_count--;
3506 entry = entry->prev;
3507 }
3508 if ((lockflags & UVM_LK_EXIT) == 0)
3509 vm_map_unlock(map);
3510 UVMHIST_LOG(maphist,"<- done (INVALID WIRE)",0,0,0,0);
3511 return EINVAL;
3512 }
3513 entry = entry->next;
3514 }
3515
3516 /*
3517 * Pass 2.
3518 */
3519
3520 #ifdef DIAGNOSTIC
3521 timestamp_save = map->timestamp;
3522 #endif
3523 vm_map_busy(map);
3524 vm_map_unlock(map);
3525
3526 rv = 0;
3527 entry = start_entry;
3528 while (entry != &map->header && entry->start < end) {
3529 if (entry->wired_count == 1) {
3530 rv = uvm_fault_wire(map, entry->start, entry->end,
3531 entry->max_protection, 1);
3532 if (rv) {
3533
3534 /*
3535 * wiring failed. break out of the loop.
3536 * we'll clean up the map below, once we
3537 * have a write lock again.
3538 */
3539
3540 break;
3541 }
3542 }
3543 entry = entry->next;
3544 }
3545
3546 if (rv) { /* failed? */
3547
3548 /*
3549 * Get back to an exclusive (write) lock.
3550 */
3551
3552 vm_map_lock(map);
3553 vm_map_unbusy(map);
3554
3555 #ifdef DIAGNOSTIC
3556 if (timestamp_save + 1 != map->timestamp)
3557 panic("uvm_map_pageable: stale map");
3558 #endif
3559
3560 /*
3561 * first drop the wiring count on all the entries
3562 * which haven't actually been wired yet.
3563 */
3564
3565 failed_entry = entry;
3566 while (entry != &map->header && entry->start < end) {
3567 entry->wired_count--;
3568 entry = entry->next;
3569 }
3570
3571 /*
3572 * now, unwire all the entries that were successfully
3573 * wired above.
3574 */
3575
3576 entry = start_entry;
3577 while (entry != failed_entry) {
3578 entry->wired_count--;
3579 if (VM_MAPENT_ISWIRED(entry) == 0)
3580 uvm_map_entry_unwire(map, entry);
3581 entry = entry->next;
3582 }
3583 if ((lockflags & UVM_LK_EXIT) == 0)
3584 vm_map_unlock(map);
3585 UVMHIST_LOG(maphist, "<- done (RV=%jd)", rv,0,0,0);
3586 return (rv);
3587 }
3588
3589 if ((lockflags & UVM_LK_EXIT) == 0) {
3590 vm_map_unbusy(map);
3591 } else {
3592
3593 /*
3594 * Get back to an exclusive (write) lock.
3595 */
3596
3597 vm_map_lock(map);
3598 vm_map_unbusy(map);
3599 }
3600
3601 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
3602 return 0;
3603 }
3604
3605 /*
3606 * uvm_map_pageable_all: special case of uvm_map_pageable - affects
3607 * all mapped regions.
3608 *
3609 * => map must not be locked.
3610 * => if no flags are specified, all regions are unwired.
3611 * => XXXJRT: has some of the same problems as uvm_map_pageable() above.
3612 */
3613
3614 int
3615 uvm_map_pageable_all(struct vm_map *map, int flags, vsize_t limit)
3616 {
3617 struct vm_map_entry *entry, *failed_entry;
3618 vsize_t size;
3619 int rv;
3620 #ifdef DIAGNOSTIC
3621 u_int timestamp_save;
3622 #endif
3623 UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist);
3624 UVMHIST_LOG(maphist,"(map=%#jx,flags=%#jx)", (uintptr_t)map, flags,
3625 0, 0);
3626
3627 KASSERT(map->flags & VM_MAP_PAGEABLE);
3628
3629 vm_map_lock(map);
3630
3631 /*
3632 * handle wiring and unwiring separately.
3633 */
3634
3635 if (flags == 0) { /* unwire */
3636
3637 /*
3638 * POSIX 1003.1b -- munlockall unlocks all regions,
3639 * regardless of how many times mlockall has been called.
3640 */
3641
3642 for (entry = map->header.next; entry != &map->header;
3643 entry = entry->next) {
3644 if (VM_MAPENT_ISWIRED(entry))
3645 uvm_map_entry_unwire(map, entry);
3646 }
3647 map->flags &= ~VM_MAP_WIREFUTURE;
3648 vm_map_unlock(map);
3649 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
3650 return 0;
3651 }
3652
3653 if (flags & MCL_FUTURE) {
3654
3655 /*
3656 * must wire all future mappings; remember this.
3657 */
3658
3659 map->flags |= VM_MAP_WIREFUTURE;
3660 }
3661
3662 if ((flags & MCL_CURRENT) == 0) {
3663
3664 /*
3665 * no more work to do!
3666 */
3667
3668 UVMHIST_LOG(maphist,"<- done (OK no wire)",0,0,0,0);
3669 vm_map_unlock(map);
3670 return 0;
3671 }
3672
3673 /*
3674 * wire case: in three passes [XXXCDC: ugly block of code here]
3675 *
3676 * 1: holding the write lock, count all pages mapped by non-wired
3677 * entries. if this would cause us to go over our limit, we fail.
3678 *
3679 * 2: still holding the write lock, we create any anonymous maps that
3680 * need to be created. then we increment its wiring count.
3681 *
3682 * 3: we downgrade to a read lock, and call uvm_fault_wire to fault
3683 * in the pages for any newly wired area (wired_count == 1).
3684 *
3685 * downgrading to a read lock for uvm_fault_wire avoids a possible
3686 * deadlock with another thread that may have faulted on one of
3687 * the pages to be wired (it would mark the page busy, blocking
3688 * us, then in turn block on the map lock that we hold). because
3689 * of problems in the recursive lock package, we cannot upgrade
3690 * to a write lock in vm_map_lookup. thus, any actions that
3691 * require the write lock must be done beforehand. because we
3692 * keep the read lock on the map, the copy-on-write status of the
3693 * entries we modify here cannot change.
3694 */
3695
3696 for (size = 0, entry = map->header.next; entry != &map->header;
3697 entry = entry->next) {
3698 if (entry->protection != VM_PROT_NONE &&
3699 VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3700 size += entry->end - entry->start;
3701 }
3702 }
3703
3704 if (atop(size) + uvmexp.wired > uvmexp.wiredmax) {
3705 vm_map_unlock(map);
3706 return ENOMEM;
3707 }
3708
3709 if (limit != 0 &&
3710 (size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit)) {
3711 vm_map_unlock(map);
3712 return ENOMEM;
3713 }
3714
3715 /*
3716 * Pass 2.
3717 */
3718
3719 for (entry = map->header.next; entry != &map->header;
3720 entry = entry->next) {
3721 if (entry->protection == VM_PROT_NONE)
3722 continue;
3723 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3724
3725 /*
3726 * perform actions of vm_map_lookup that need the
3727 * write lock on the map: create an anonymous map
3728 * for a copy-on-write region, or an anonymous map
3729 * for a zero-fill region. (XXXCDC: submap case
3730 * ok?)
3731 */
3732
3733 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */
3734 if (UVM_ET_ISNEEDSCOPY(entry) &&
3735 ((entry->max_protection & VM_PROT_WRITE) ||
3736 (entry->object.uvm_obj == NULL))) {
3737 amap_copy(map, entry, 0, entry->start,
3738 entry->end);
3739 /* XXXCDC: wait OK? */
3740 }
3741 }
3742 }
3743 entry->wired_count++;
3744 }
3745
3746 /*
3747 * Pass 3.
3748 */
3749
3750 #ifdef DIAGNOSTIC
3751 timestamp_save = map->timestamp;
3752 #endif
3753 vm_map_busy(map);
3754 vm_map_unlock(map);
3755
3756 rv = 0;
3757 for (entry = map->header.next; entry != &map->header;
3758 entry = entry->next) {
3759 if (entry->wired_count == 1) {
3760 rv = uvm_fault_wire(map, entry->start, entry->end,
3761 entry->max_protection, 1);
3762 if (rv) {
3763
3764 /*
3765 * wiring failed. break out of the loop.
3766 * we'll clean up the map below, once we
3767 * have a write lock again.
3768 */
3769
3770 break;
3771 }
3772 }
3773 }
3774
3775 if (rv) {
3776
3777 /*
3778 * Get back an exclusive (write) lock.
3779 */
3780
3781 vm_map_lock(map);
3782 vm_map_unbusy(map);
3783
3784 #ifdef DIAGNOSTIC
3785 if (timestamp_save + 1 != map->timestamp)
3786 panic("uvm_map_pageable_all: stale map");
3787 #endif
3788
3789 /*
3790 * first drop the wiring count on all the entries
3791 * which haven't actually been wired yet.
3792 *
3793 * Skip VM_PROT_NONE entries like we did above.
3794 */
3795
3796 failed_entry = entry;
3797 for (/* nothing */; entry != &map->header;
3798 entry = entry->next) {
3799 if (entry->protection == VM_PROT_NONE)
3800 continue;
3801 entry->wired_count--;
3802 }
3803
3804 /*
3805 * now, unwire all the entries that were successfully
3806 * wired above.
3807 *
3808 * Skip VM_PROT_NONE entries like we did above.
3809 */
3810
3811 for (entry = map->header.next; entry != failed_entry;
3812 entry = entry->next) {
3813 if (entry->protection == VM_PROT_NONE)
3814 continue;
3815 entry->wired_count--;
3816 if (VM_MAPENT_ISWIRED(entry))
3817 uvm_map_entry_unwire(map, entry);
3818 }
3819 vm_map_unlock(map);
3820 UVMHIST_LOG(maphist,"<- done (RV=%jd)", rv,0,0,0);
3821 return (rv);
3822 }
3823
3824 vm_map_unbusy(map);
3825
3826 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
3827 return 0;
3828 }
3829
3830 /*
3831 * uvm_map_clean: clean out a map range
3832 *
3833 * => valid flags:
3834 * if (flags & PGO_CLEANIT): dirty pages are cleaned first
3835 * if (flags & PGO_SYNCIO): dirty pages are written synchronously
3836 * if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean
3837 * if (flags & PGO_FREE): any cached pages are freed after clean
3838 * => returns an error if any part of the specified range isn't mapped
3839 * => never a need to flush amap layer since the anonymous memory has
3840 * no permanent home, but may deactivate pages there
3841 * => called from sys_msync() and sys_madvise()
3842 * => caller must not write-lock map (read OK).
3843 * => we may sleep while cleaning if SYNCIO [with map read-locked]
3844 */
3845
3846 int
3847 uvm_map_clean(struct vm_map *map, vaddr_t start, vaddr_t end, int flags)
3848 {
3849 struct vm_map_entry *current, *entry;
3850 struct uvm_object *uobj;
3851 struct vm_amap *amap;
3852 struct vm_anon *anon, *anon_tofree;
3853 struct vm_page *pg;
3854 vaddr_t offset;
3855 vsize_t size;
3856 voff_t uoff;
3857 int error, refs;
3858 UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist);
3859
3860 UVMHIST_LOG(maphist,"(map=%#jx,start=%#jx,end=%#jx,flags=%#jx)",
3861 (uintptr_t)map, start, end, flags);
3862 KASSERT((flags & (PGO_FREE|PGO_DEACTIVATE)) !=
3863 (PGO_FREE|PGO_DEACTIVATE));
3864
3865 vm_map_lock_read(map);
3866 VM_MAP_RANGE_CHECK(map, start, end);
3867 if (uvm_map_lookup_entry(map, start, &entry) == false) {
3868 vm_map_unlock_read(map);
3869 return EFAULT;
3870 }
3871
3872 /*
3873 * Make a first pass to check for holes and wiring problems.
3874 */
3875
3876 for (current = entry; current->start < end; current = current->next) {
3877 if (UVM_ET_ISSUBMAP(current)) {
3878 vm_map_unlock_read(map);
3879 return EINVAL;
3880 }
3881 if ((flags & PGO_FREE) != 0 && VM_MAPENT_ISWIRED(entry)) {
3882 vm_map_unlock_read(map);
3883 return EBUSY;
3884 }
3885 if (end <= current->end) {
3886 break;
3887 }
3888 if (current->end != current->next->start) {
3889 vm_map_unlock_read(map);
3890 return EFAULT;
3891 }
3892 }
3893
3894 error = 0;
3895 for (current = entry; start < end; current = current->next) {
3896 amap = current->aref.ar_amap; /* upper layer */
3897 uobj = current->object.uvm_obj; /* lower layer */
3898 KASSERT(start >= current->start);
3899
3900 /*
3901 * No amap cleaning necessary if:
3902 *
3903 * (1) There's no amap.
3904 *
3905 * (2) We're not deactivating or freeing pages.
3906 */
3907
3908 if (amap == NULL || (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0)
3909 goto flush_object;
3910
3911 offset = start - current->start;
3912 size = MIN(end, current->end) - start;
3913 anon_tofree = NULL;
3914
3915 amap_lock(amap);
3916 for ( ; size != 0; size -= PAGE_SIZE, offset += PAGE_SIZE) {
3917 anon = amap_lookup(¤t->aref, offset);
3918 if (anon == NULL)
3919 continue;
3920
3921 KASSERT(anon->an_lock == amap->am_lock);
3922 pg = anon->an_page;
3923 if (pg == NULL) {
3924 continue;
3925 }
3926 if (pg->flags & PG_BUSY) {
3927 continue;
3928 }
3929
3930 switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) {
3931
3932 /*
3933 * In these first 3 cases, we just deactivate the page.
3934 */
3935
3936 case PGO_CLEANIT|PGO_FREE:
3937 case PGO_CLEANIT|PGO_DEACTIVATE:
3938 case PGO_DEACTIVATE:
3939 deactivate_it:
3940 /*
3941 * skip the page if it's loaned or wired,
3942 * since it shouldn't be on a paging queue
3943 * at all in these cases.
3944 */
3945
3946 mutex_enter(&uvm_pageqlock);
3947 if (pg->loan_count != 0 ||
3948 pg->wire_count != 0) {
3949 mutex_exit(&uvm_pageqlock);
3950 continue;
3951 }
3952 KASSERT(pg->uanon == anon);
3953 uvm_pagedeactivate(pg);
3954 mutex_exit(&uvm_pageqlock);
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 referece 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