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