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