FreeBSD/Linux Kernel Cross Reference
sys/uvm/uvm_map.c
1 /* $NetBSD: uvm_map.c,v 1.164.2.3 2004/05/09 09:01:32 jdc 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Charles D. Cranor,
23 * Washington University, the University of California, Berkeley and
24 * its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * @(#)vm_map.c 8.3 (Berkeley) 1/12/94
42 * from: Id: uvm_map.c,v 1.1.2.27 1998/02/07 01:16:54 chs Exp
43 *
44 *
45 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 * All rights reserved.
47 *
48 * Permission to use, copy, modify and distribute this software and
49 * its documentation is hereby granted, provided that both the copyright
50 * notice and this permission notice appear in all copies of the
51 * software, derivative works or modified versions, and any portions
52 * thereof, and that both notices appear in supporting documentation.
53 *
54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57 *
58 * Carnegie Mellon requests users of this software to return to
59 *
60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
61 * School of Computer Science
62 * Carnegie Mellon University
63 * Pittsburgh PA 15213-3890
64 *
65 * any improvements or extensions that they make and grant Carnegie the
66 * rights to redistribute these changes.
67 */
68
69 /*
70 * uvm_map.c: uvm map operations
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: uvm_map.c,v 1.164.2.3 2004/05/09 09:01:32 jdc Exp $");
75
76 #include "opt_ddb.h"
77 #include "opt_uvmhist.h"
78 #include "opt_uvm.h"
79 #include "opt_sysv.h"
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/mman.h>
84 #include <sys/proc.h>
85 #include <sys/malloc.h>
86 #include <sys/pool.h>
87 #include <sys/kernel.h>
88 #include <sys/mount.h>
89 #include <sys/vnode.h>
90
91 #ifdef SYSVSHM
92 #include <sys/shm.h>
93 #endif
94
95 #define UVM_MAP
96 #include <uvm/uvm.h>
97 #undef RB_AUGMENT
98 #define RB_AUGMENT(x) uvm_rb_augment(x)
99
100 #ifdef DDB
101 #include <uvm/uvm_ddb.h>
102 #endif
103
104 extern struct vm_map *pager_map;
105
106 #ifndef UVMMAP_NOCOUNTERS
107 #include <sys/device.h>
108 struct evcnt map_ubackmerge = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
109 "uvmmap", "ubackmerge");
110 struct evcnt map_uforwmerge = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
111 "uvmmap", "uforwmerge");
112 struct evcnt map_ubimerge = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
113 "uvmmap", "ubimerge");
114 struct evcnt map_unomerge = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
115 "uvmmap", "unomerge");
116 struct evcnt map_kbackmerge = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
117 "uvmmap", "kbackmerge");
118 struct evcnt map_kforwmerge = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
119 "uvmmap", "kforwmerge");
120 struct evcnt map_kbimerge = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
121 "uvmmap", "kbimerge");
122 struct evcnt map_knomerge = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
123 "uvmmap", "knomerge");
124 struct evcnt uvm_map_call = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
125 "uvmmap", "map_call");
126 struct evcnt uvm_mlk_call = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
127 "uvmmap", "mlk_call");
128 struct evcnt uvm_mlk_hint = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL,
129 "uvmmap", "mlk_hint");
130
131 EVCNT_ATTACH_STATIC(map_ubackmerge);
132 EVCNT_ATTACH_STATIC(map_uforwmerge);
133 EVCNT_ATTACH_STATIC(map_ubimerge);
134 EVCNT_ATTACH_STATIC(map_unomerge);
135 EVCNT_ATTACH_STATIC(map_kbackmerge);
136 EVCNT_ATTACH_STATIC(map_kforwmerge);
137 EVCNT_ATTACH_STATIC(map_kbimerge);
138 EVCNT_ATTACH_STATIC(map_knomerge);
139 EVCNT_ATTACH_STATIC(uvm_map_call);
140 EVCNT_ATTACH_STATIC(uvm_mlk_call);
141 EVCNT_ATTACH_STATIC(uvm_mlk_hint);
142
143 #define UVMCNT_INCR(ev) ev.ev_count++
144 #define UVMCNT_DECR(ev) ev.ev_count--
145 #else
146 #define UVMCNT_INCR(ev)
147 #define UVMCNT_DECR(ev)
148 #endif
149
150 const char vmmapbsy[] = "vmmapbsy";
151
152 /*
153 * pool for vmspace structures.
154 */
155
156 struct pool uvm_vmspace_pool;
157
158 /*
159 * pool for dynamically-allocated map entries.
160 */
161
162 struct pool uvm_map_entry_pool;
163 struct pool uvm_map_entry_kmem_pool;
164
165 MALLOC_DEFINE(M_VMMAP, "VM map", "VM map structures");
166 MALLOC_DEFINE(M_VMPMAP, "VM pmap", "VM pmap");
167
168 #ifdef PMAP_GROWKERNEL
169 /*
170 * This global represents the end of the kernel virtual address
171 * space. If we want to exceed this, we must grow the kernel
172 * virtual address space dynamically.
173 *
174 * Note, this variable is locked by kernel_map's lock.
175 */
176 vaddr_t uvm_maxkaddr;
177 #endif
178
179 /*
180 * macros
181 */
182
183 /*
184 * uvm_map_entry_link: insert entry into a map
185 *
186 * => map must be locked
187 */
188 #define uvm_map_entry_link(map, after_where, entry) do { \
189 KASSERT(entry->start < entry->end); \
190 (map)->nentries++; \
191 (entry)->prev = (after_where); \
192 (entry)->next = (after_where)->next; \
193 (entry)->prev->next = (entry); \
194 (entry)->next->prev = (entry); \
195 uvm_rb_insert((map), (entry)); \
196 } while (/*CONSTCOND*/ 0)
197
198 /*
199 * uvm_map_entry_unlink: remove entry from a map
200 *
201 * => map must be locked
202 */
203 #define uvm_map_entry_unlink(map, entry) do { \
204 (map)->nentries--; \
205 (entry)->next->prev = (entry)->prev; \
206 (entry)->prev->next = (entry)->next; \
207 uvm_rb_remove((map), (entry)); \
208 } while (/*CONSTCOND*/ 0)
209
210 /*
211 * SAVE_HINT: saves the specified entry as the hint for future lookups.
212 *
213 * => map need not be locked (protected by hint_lock).
214 */
215 #define SAVE_HINT(map,check,value) do { \
216 simple_lock(&(map)->hint_lock); \
217 if ((map)->hint == (check)) \
218 (map)->hint = (value); \
219 simple_unlock(&(map)->hint_lock); \
220 } while (/*CONSTCOND*/ 0)
221
222 /*
223 * VM_MAP_RANGE_CHECK: check and correct range
224 *
225 * => map must at least be read locked
226 */
227
228 #define VM_MAP_RANGE_CHECK(map, start, end) do { \
229 if (start < vm_map_min(map)) \
230 start = vm_map_min(map); \
231 if (end > vm_map_max(map)) \
232 end = vm_map_max(map); \
233 if (start > end) \
234 start = end; \
235 } while (/*CONSTCOND*/ 0)
236
237 /*
238 * local prototypes
239 */
240
241 static struct vm_map_entry *
242 uvm_mapent_alloc(struct vm_map *, int);
243 static void uvm_mapent_copy(struct vm_map_entry *, struct vm_map_entry *);
244 static void uvm_mapent_free(struct vm_map_entry *);
245 static void uvm_map_entry_unwire(struct vm_map *, struct vm_map_entry *);
246 static void uvm_map_reference_amap(struct vm_map_entry *, int);
247 static int uvm_map_space_avail(vaddr_t *, vsize_t, voff_t, vsize_t, int,
248 struct vm_map_entry *);
249 static void uvm_map_unreference_amap(struct vm_map_entry *, int);
250
251 int _uvm_tree_sanity(struct vm_map *, const char *);
252 static vsize_t uvm_rb_subtree_space(const struct vm_map_entry *);
253
254 static __inline int
255 uvm_compare(const struct vm_map_entry *a, const struct vm_map_entry *b)
256 {
257
258 if (a->start < b->start)
259 return (-1);
260 else if (a->start > b->start)
261 return (1);
262
263 return (0);
264 }
265
266 static __inline void
267 uvm_rb_augment(struct vm_map_entry *entry)
268 {
269
270 entry->space = uvm_rb_subtree_space(entry);
271 }
272
273 RB_PROTOTYPE(uvm_tree, vm_map_entry, rb_entry, uvm_compare);
274
275 RB_GENERATE(uvm_tree, vm_map_entry, rb_entry, uvm_compare);
276
277 static __inline vsize_t
278 uvm_rb_space(const struct vm_map *map, const struct vm_map_entry *entry)
279 {
280 /* XXX map is not used */
281
282 KASSERT(entry->next != NULL);
283 return entry->next->start - entry->end;
284 }
285
286 static vsize_t
287 uvm_rb_subtree_space(const struct vm_map_entry *entry)
288 {
289 vaddr_t space, tmp;
290
291 space = entry->ownspace;
292 if (RB_LEFT(entry, rb_entry)) {
293 tmp = RB_LEFT(entry, rb_entry)->space;
294 if (tmp > space)
295 space = tmp;
296 }
297
298 if (RB_RIGHT(entry, rb_entry)) {
299 tmp = RB_RIGHT(entry, rb_entry)->space;
300 if (tmp > space)
301 space = tmp;
302 }
303
304 return (space);
305 }
306
307 static __inline void
308 uvm_rb_fixup(struct vm_map *map, struct vm_map_entry *entry)
309 {
310 /* We need to traverse to the very top */
311 do {
312 entry->ownspace = uvm_rb_space(map, entry);
313 entry->space = uvm_rb_subtree_space(entry);
314 } while ((entry = RB_PARENT(entry, rb_entry)) != NULL);
315 }
316
317 static __inline void
318 uvm_rb_insert(struct vm_map *map, struct vm_map_entry *entry)
319 {
320 vaddr_t space = uvm_rb_space(map, entry);
321 struct vm_map_entry *tmp;
322
323 entry->ownspace = entry->space = space;
324 tmp = RB_INSERT(uvm_tree, &(map)->rbhead, entry);
325 #ifdef DIAGNOSTIC
326 if (tmp != NULL)
327 panic("uvm_rb_insert: duplicate entry?");
328 #endif
329 uvm_rb_fixup(map, entry);
330 if (entry->prev != &map->header)
331 uvm_rb_fixup(map, entry->prev);
332 }
333
334 static __inline void
335 uvm_rb_remove(struct vm_map *map, struct vm_map_entry *entry)
336 {
337 struct vm_map_entry *parent;
338
339 parent = RB_PARENT(entry, rb_entry);
340 RB_REMOVE(uvm_tree, &(map)->rbhead, entry);
341 if (entry->prev != &map->header)
342 uvm_rb_fixup(map, entry->prev);
343 if (parent)
344 uvm_rb_fixup(map, parent);
345 }
346
347 #ifdef DEBUG
348 int uvm_debug_check_rbtree = 0;
349 #define uvm_tree_sanity(x,y) \
350 if (uvm_debug_check_rbtree) \
351 _uvm_tree_sanity(x,y)
352 #else
353 #define uvm_tree_sanity(x,y)
354 #endif
355
356 int
357 _uvm_tree_sanity(struct vm_map *map, const char *name)
358 {
359 struct vm_map_entry *tmp, *trtmp;
360 int n = 0, i = 1;
361
362 RB_FOREACH(tmp, uvm_tree, &map->rbhead) {
363 if (tmp->ownspace != uvm_rb_space(map, tmp)) {
364 printf("%s: %d/%d ownspace %lx != %lx %s\n",
365 name, n + 1, map->nentries,
366 (ulong)tmp->ownspace, (ulong)uvm_rb_space(map, tmp),
367 tmp->next == &map->header ? "(last)" : "");
368 goto error;
369 }
370 }
371 trtmp = NULL;
372 RB_FOREACH(tmp, uvm_tree, &map->rbhead) {
373 if (tmp->space != uvm_rb_subtree_space(tmp)) {
374 printf("%s: space %lx != %lx\n",
375 name, (ulong)tmp->space,
376 (ulong)uvm_rb_subtree_space(tmp));
377 goto error;
378 }
379 if (trtmp != NULL && trtmp->start >= tmp->start) {
380 printf("%s: corrupt: 0x%lx >= 0x%lx\n",
381 name, trtmp->start, tmp->start);
382 goto error;
383 }
384 n++;
385
386 trtmp = tmp;
387 }
388
389 if (n != map->nentries) {
390 printf("%s: nentries: %d vs %d\n",
391 name, n, map->nentries);
392 goto error;
393 }
394
395 for (tmp = map->header.next; tmp && tmp != &map->header;
396 tmp = tmp->next, i++) {
397 trtmp = RB_FIND(uvm_tree, &map->rbhead, tmp);
398 if (trtmp != tmp) {
399 printf("%s: lookup: %d: %p - %p: %p\n",
400 name, i, tmp, trtmp,
401 RB_PARENT(tmp, rb_entry));
402 goto error;
403 }
404 }
405
406 return (0);
407 error:
408 #ifdef DDB
409 /* handy breakpoint location for error case */
410 __asm(".globl treesanity_label\ntreesanity_label:");
411 #endif
412 return (-1);
413 }
414
415 /*
416 * local inlines
417 */
418
419 /*
420 * uvm_mapent_alloc: allocate a map entry
421 */
422
423 static __inline struct vm_map_entry *
424 uvm_mapent_alloc(struct vm_map *map, int flags)
425 {
426 struct vm_map_entry *me;
427 int s;
428 int pflags = (flags & UVM_FLAG_NOWAIT) ? PR_NOWAIT : PR_WAITOK;
429 UVMHIST_FUNC("uvm_mapent_alloc"); UVMHIST_CALLED(maphist);
430
431 if (map->flags & VM_MAP_INTRSAFE || cold) {
432 s = splvm();
433 simple_lock(&uvm.kentry_lock);
434 me = uvm.kentry_free;
435 if (me)
436 uvm.kentry_free = me->next;
437 simple_unlock(&uvm.kentry_lock);
438 splx(s);
439 if (__predict_false(me == NULL)) {
440 panic("uvm_mapent_alloc: out of static map entries, "
441 "check MAX_KMAPENT (currently %d)",
442 MAX_KMAPENT);
443 }
444 me->flags = UVM_MAP_STATIC;
445 } else if (map == kernel_map) {
446 me = pool_get(&uvm_map_entry_kmem_pool, pflags);
447 if (__predict_false(me == NULL))
448 return NULL;
449 me->flags = UVM_MAP_KMEM;
450 } else {
451 me = pool_get(&uvm_map_entry_pool, pflags);
452 if (__predict_false(me == NULL))
453 return NULL;
454 me->flags = 0;
455 }
456
457 UVMHIST_LOG(maphist, "<- new entry=0x%x [kentry=%d]", me,
458 ((map->flags & VM_MAP_INTRSAFE) != 0 || map == kernel_map), 0, 0);
459 return (me);
460 }
461
462 /*
463 * uvm_mapent_free: free map entry
464 */
465
466 static __inline void
467 uvm_mapent_free(struct vm_map_entry *me)
468 {
469 int s;
470 UVMHIST_FUNC("uvm_mapent_free"); UVMHIST_CALLED(maphist);
471
472 UVMHIST_LOG(maphist,"<- freeing map entry=0x%x [flags=%d]",
473 me, me->flags, 0, 0);
474 if (me->flags & UVM_MAP_STATIC) {
475 s = splvm();
476 simple_lock(&uvm.kentry_lock);
477 me->next = uvm.kentry_free;
478 uvm.kentry_free = me;
479 simple_unlock(&uvm.kentry_lock);
480 splx(s);
481 } else if (me->flags & UVM_MAP_KMEM) {
482 pool_put(&uvm_map_entry_kmem_pool, me);
483 } else {
484 pool_put(&uvm_map_entry_pool, me);
485 }
486 }
487
488 /*
489 * uvm_mapent_copy: copy a map entry, preserving flags
490 */
491
492 static __inline void
493 uvm_mapent_copy(struct vm_map_entry *src, struct vm_map_entry *dst)
494 {
495
496 memcpy(dst, src, ((char *)&src->uvm_map_entry_stop_copy) -
497 ((char *)src));
498 }
499
500 /*
501 * uvm_map_entry_unwire: unwire a map entry
502 *
503 * => map should be locked by caller
504 */
505
506 static __inline void
507 uvm_map_entry_unwire(struct vm_map *map, struct vm_map_entry *entry)
508 {
509
510 entry->wired_count = 0;
511 uvm_fault_unwire_locked(map, entry->start, entry->end);
512 }
513
514
515 /*
516 * wrapper for calling amap_ref()
517 */
518 static __inline void
519 uvm_map_reference_amap(struct vm_map_entry *entry, int flags)
520 {
521
522 amap_ref(entry->aref.ar_amap, entry->aref.ar_pageoff,
523 (entry->end - entry->start) >> PAGE_SHIFT, flags);
524 }
525
526
527 /*
528 * wrapper for calling amap_unref()
529 */
530 static __inline void
531 uvm_map_unreference_amap(struct vm_map_entry *entry, int flags)
532 {
533
534 amap_unref(entry->aref.ar_amap, entry->aref.ar_pageoff,
535 (entry->end - entry->start) >> PAGE_SHIFT, flags);
536 }
537
538
539 /*
540 * uvm_map_init: init mapping system at boot time. note that we allocate
541 * and init the static pool of struct vm_map_entry *'s for the kernel here.
542 */
543
544 void
545 uvm_map_init(void)
546 {
547 static struct vm_map_entry kernel_map_entry[MAX_KMAPENT];
548 #if defined(UVMHIST)
549 static struct uvm_history_ent maphistbuf[100];
550 static struct uvm_history_ent pdhistbuf[100];
551 #endif
552 int lcv;
553
554 /*
555 * first, init logging system.
556 */
557
558 UVMHIST_FUNC("uvm_map_init");
559 UVMHIST_INIT_STATIC(maphist, maphistbuf);
560 UVMHIST_INIT_STATIC(pdhist, pdhistbuf);
561 UVMHIST_CALLED(maphist);
562 UVMHIST_LOG(maphist,"<starting uvm map system>", 0, 0, 0, 0);
563
564 /*
565 * now set up static pool of kernel map entrys ...
566 */
567
568 simple_lock_init(&uvm.kentry_lock);
569 uvm.kentry_free = NULL;
570 for (lcv = 0 ; lcv < MAX_KMAPENT ; lcv++) {
571 kernel_map_entry[lcv].next = uvm.kentry_free;
572 uvm.kentry_free = &kernel_map_entry[lcv];
573 }
574
575 /*
576 * initialize the map-related pools.
577 */
578 pool_init(&uvm_vmspace_pool, sizeof(struct vmspace),
579 0, 0, 0, "vmsppl", &pool_allocator_nointr);
580 pool_init(&uvm_map_entry_pool, sizeof(struct vm_map_entry),
581 0, 0, 0, "vmmpepl", &pool_allocator_nointr);
582 pool_init(&uvm_map_entry_kmem_pool, sizeof(struct vm_map_entry),
583 0, 0, 0, "vmmpekpl", NULL);
584 }
585
586 /*
587 * clippers
588 */
589
590 /*
591 * uvm_map_clip_start: ensure that the entry begins at or after
592 * the starting address, if it doesn't we split the entry.
593 *
594 * => caller should use UVM_MAP_CLIP_START macro rather than calling
595 * this directly
596 * => map must be locked by caller
597 */
598
599 void
600 uvm_map_clip_start(struct vm_map *map, struct vm_map_entry *entry,
601 vaddr_t start)
602 {
603 struct vm_map_entry *new_entry;
604 vaddr_t new_adj;
605
606 /* uvm_map_simplify_entry(map, entry); */ /* XXX */
607
608 uvm_tree_sanity(map, "clip_start entry");
609
610 /*
611 * Split off the front portion. note that we must insert the new
612 * entry BEFORE this one, so that this entry has the specified
613 * starting address.
614 */
615
616 new_entry = uvm_mapent_alloc(map, 0);
617 uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
618
619 new_entry->end = start;
620 new_adj = start - new_entry->start;
621 if (entry->object.uvm_obj)
622 entry->offset += new_adj; /* shift start over */
623
624 /* Does not change order for the RB tree */
625 entry->start = start;
626
627 if (new_entry->aref.ar_amap) {
628 amap_splitref(&new_entry->aref, &entry->aref, new_adj);
629 }
630
631 uvm_map_entry_link(map, entry->prev, new_entry);
632
633 if (UVM_ET_ISSUBMAP(entry)) {
634 /* ... unlikely to happen, but play it safe */
635 uvm_map_reference(new_entry->object.sub_map);
636 } else {
637 if (UVM_ET_ISOBJ(entry) &&
638 entry->object.uvm_obj->pgops &&
639 entry->object.uvm_obj->pgops->pgo_reference)
640 entry->object.uvm_obj->pgops->pgo_reference(
641 entry->object.uvm_obj);
642 }
643
644 uvm_tree_sanity(map, "clip_start leave");
645 }
646
647 /*
648 * uvm_map_clip_end: ensure that the entry ends at or before
649 * the ending address, if it does't we split the reference
650 *
651 * => caller should use UVM_MAP_CLIP_END macro rather than calling
652 * this directly
653 * => map must be locked by caller
654 */
655
656 void
657 uvm_map_clip_end(struct vm_map *map, struct vm_map_entry *entry, vaddr_t end)
658 {
659 struct vm_map_entry * new_entry;
660 vaddr_t new_adj; /* #bytes we move start forward */
661
662 uvm_tree_sanity(map, "clip_end entry");
663 /*
664 * Create a new entry and insert it
665 * AFTER the specified entry
666 */
667
668 new_entry = uvm_mapent_alloc(map, 0);
669 uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
670
671 new_entry->start = entry->end = end;
672 new_adj = end - entry->start;
673 if (new_entry->object.uvm_obj)
674 new_entry->offset += new_adj;
675
676 if (entry->aref.ar_amap)
677 amap_splitref(&entry->aref, &new_entry->aref, new_adj);
678
679 uvm_rb_fixup(map, entry);
680
681 uvm_map_entry_link(map, entry, new_entry);
682
683 if (UVM_ET_ISSUBMAP(entry)) {
684 /* ... unlikely to happen, but play it safe */
685 uvm_map_reference(new_entry->object.sub_map);
686 } else {
687 if (UVM_ET_ISOBJ(entry) &&
688 entry->object.uvm_obj->pgops &&
689 entry->object.uvm_obj->pgops->pgo_reference)
690 entry->object.uvm_obj->pgops->pgo_reference(
691 entry->object.uvm_obj);
692 }
693
694 uvm_tree_sanity(map, "clip_end leave");
695 }
696
697
698 /*
699 * M A P - m a i n e n t r y p o i n t
700 */
701 /*
702 * uvm_map: establish a valid mapping in a map
703 *
704 * => assume startp is page aligned.
705 * => assume size is a multiple of PAGE_SIZE.
706 * => assume sys_mmap provides enough of a "hint" to have us skip
707 * over text/data/bss area.
708 * => map must be unlocked (we will lock it)
709 * => <uobj,uoffset> value meanings (4 cases):
710 * [1] <NULL,uoffset> == uoffset is a hint for PMAP_PREFER
711 * [2] <NULL,UVM_UNKNOWN_OFFSET> == don't PMAP_PREFER
712 * [3] <uobj,uoffset> == normal mapping
713 * [4] <uobj,UVM_UNKNOWN_OFFSET> == uvm_map finds offset based on VA
714 *
715 * case [4] is for kernel mappings where we don't know the offset until
716 * we've found a virtual address. note that kernel object offsets are
717 * always relative to vm_map_min(kernel_map).
718 *
719 * => if `align' is non-zero, we align the virtual address to the specified
720 * alignment.
721 * this is provided as a mechanism for large pages.
722 *
723 * => XXXCDC: need way to map in external amap?
724 */
725
726 int
727 uvm_map(struct vm_map *map, vaddr_t *startp /* IN/OUT */, vsize_t size,
728 struct uvm_object *uobj, voff_t uoffset, vsize_t align, uvm_flag_t flags)
729 {
730 struct vm_map_entry *prev_entry, *new_entry;
731 const int amapwaitflag = (flags & UVM_FLAG_NOWAIT) ?
732 AMAP_EXTEND_NOWAIT : 0;
733 vm_prot_t prot = UVM_PROTECTION(flags), maxprot =
734 UVM_MAXPROTECTION(flags);
735 vm_inherit_t inherit = UVM_INHERIT(flags);
736 int advice = UVM_ADVICE(flags);
737 int error, merged = 0, kmap = (vm_map_pmap(map) == pmap_kernel());
738 UVMHIST_FUNC("uvm_map");
739 UVMHIST_CALLED(maphist);
740
741 UVMHIST_LOG(maphist, "(map=0x%x, *startp=0x%x, size=%d, flags=0x%x)",
742 map, *startp, size, flags);
743 UVMHIST_LOG(maphist, " uobj/offset 0x%x/%d", uobj, uoffset,0,0);
744
745 /*
746 * detect a popular device driver bug.
747 */
748
749 KASSERT(doing_shutdown || curlwp != NULL ||
750 (map->flags & VM_MAP_INTRSAFE));
751
752 /*
753 * zero-sized mapping doesn't make any sense.
754 */
755 KASSERT(size > 0);
756
757 uvm_tree_sanity(map, "map entry");
758
759 /*
760 * check sanity of protection code
761 */
762
763 if ((prot & maxprot) != prot) {
764 UVMHIST_LOG(maphist, "<- prot. failure: prot=0x%x, max=0x%x",
765 prot, maxprot,0,0);
766 return EACCES;
767 }
768
769 /*
770 * for pager_map, allocate the new entry first to avoid sleeping
771 * for memory while we have the map locked.
772 */
773
774 new_entry = NULL;
775 if (map == pager_map) {
776 new_entry = uvm_mapent_alloc(map, (flags & UVM_FLAG_NOWAIT));
777 if (__predict_false(new_entry == NULL))
778 return ENOMEM;
779 }
780
781 /*
782 * figure out where to put new VM range
783 */
784
785 if (vm_map_lock_try(map) == FALSE) {
786 if (flags & UVM_FLAG_TRYLOCK) {
787 if (new_entry) {
788 uvm_mapent_free(new_entry);
789 }
790 return EAGAIN;
791 }
792 vm_map_lock(map); /* could sleep here */
793 }
794 if ((prev_entry = uvm_map_findspace(map, *startp, size, startp,
795 uobj, uoffset, align, flags)) == NULL) {
796 UVMHIST_LOG(maphist,"<- uvm_map_findspace failed!",0,0,0,0);
797 vm_map_unlock(map);
798 if (new_entry) {
799 uvm_mapent_free(new_entry);
800 }
801 return ENOMEM;
802 }
803
804 #ifdef PMAP_GROWKERNEL
805 /*
806 * If the kernel pmap can't map the requested space,
807 * then allocate more resources for it.
808 */
809 if (map == kernel_map && uvm_maxkaddr < (*startp + size))
810 uvm_maxkaddr = pmap_growkernel(*startp + size);
811 #endif
812
813 UVMCNT_INCR(uvm_map_call);
814
815 /*
816 * if uobj is null, then uoffset is either a VAC hint for PMAP_PREFER
817 * [typically from uvm_map_reserve] or it is UVM_UNKNOWN_OFFSET. in
818 * either case we want to zero it before storing it in the map entry
819 * (because it looks strange and confusing when debugging...)
820 *
821 * if uobj is not null
822 * if uoffset is not UVM_UNKNOWN_OFFSET then we have a normal mapping
823 * and we do not need to change uoffset.
824 * if uoffset is UVM_UNKNOWN_OFFSET then we need to find the offset
825 * now (based on the starting address of the map). this case is
826 * for kernel object mappings where we don't know the offset until
827 * the virtual address is found (with uvm_map_findspace). the
828 * offset is the distance we are from the start of the map.
829 */
830
831 if (uobj == NULL) {
832 uoffset = 0;
833 } else {
834 if (uoffset == UVM_UNKNOWN_OFFSET) {
835 KASSERT(UVM_OBJ_IS_KERN_OBJECT(uobj));
836 uoffset = *startp - vm_map_min(kernel_map);
837 }
838 }
839
840 /*
841 * try and insert in map by extending previous entry, if possible.
842 * XXX: we don't try and pull back the next entry. might be useful
843 * for a stack, but we are currently allocating our stack in advance.
844 */
845
846 if (flags & UVM_FLAG_NOMERGE)
847 goto nomerge;
848
849 if (prev_entry->end == *startp &&
850 prev_entry != &map->header &&
851 prev_entry->object.uvm_obj == uobj) {
852
853 if (prev_entry->flags & UVM_MAP_NOMERGE)
854 goto forwardmerge;
855
856 if (uobj && prev_entry->offset +
857 (prev_entry->end - prev_entry->start) != uoffset)
858 goto forwardmerge;
859
860 if (UVM_ET_ISSUBMAP(prev_entry))
861 goto forwardmerge;
862
863 if (prev_entry->protection != prot ||
864 prev_entry->max_protection != maxprot)
865 goto forwardmerge;
866
867 if (prev_entry->inheritance != inherit ||
868 prev_entry->advice != advice)
869 goto forwardmerge;
870
871 /* wiring status must match (new area is unwired) */
872 if (VM_MAPENT_ISWIRED(prev_entry))
873 goto forwardmerge;
874
875 /*
876 * can't extend a shared amap. note: no need to lock amap to
877 * look at refs since we don't care about its exact value.
878 * if it is one (i.e. we have only reference) it will stay there
879 */
880
881 if (prev_entry->aref.ar_amap &&
882 amap_refs(prev_entry->aref.ar_amap) != 1) {
883 goto forwardmerge;
884 }
885
886 if (prev_entry->aref.ar_amap) {
887 error = amap_extend(prev_entry, size,
888 amapwaitflag | AMAP_EXTEND_FORWARDS);
889 if (error) {
890 vm_map_unlock(map);
891 if (new_entry) {
892 uvm_mapent_free(new_entry);
893 }
894 return error;
895 }
896 }
897
898 if (kmap)
899 UVMCNT_INCR(map_kbackmerge);
900 else
901 UVMCNT_INCR(map_ubackmerge);
902 UVMHIST_LOG(maphist," starting back merge", 0, 0, 0, 0);
903
904 /*
905 * drop our reference to uobj since we are extending a reference
906 * that we already have (the ref count can not drop to zero).
907 */
908
909 if (uobj && uobj->pgops->pgo_detach)
910 uobj->pgops->pgo_detach(uobj);
911
912 prev_entry->end += size;
913 uvm_rb_fixup(map, prev_entry);
914
915 uvm_tree_sanity(map, "map backmerged");
916
917 UVMHIST_LOG(maphist,"<- done (via backmerge)!", 0, 0, 0, 0);
918 if (new_entry) {
919 uvm_mapent_free(new_entry);
920 new_entry = NULL;
921 }
922 merged++;
923 }
924
925 forwardmerge:
926 if (prev_entry->next->start == (*startp + size) &&
927 prev_entry->next != &map->header &&
928 prev_entry->next->object.uvm_obj == uobj) {
929
930 if (prev_entry->next->flags & UVM_MAP_NOMERGE)
931 goto nomerge;
932
933 if (uobj && prev_entry->next->offset != uoffset + size)
934 goto nomerge;
935
936 if (UVM_ET_ISSUBMAP(prev_entry->next))
937 goto nomerge;
938
939 if (prev_entry->next->protection != prot ||
940 prev_entry->next->max_protection != maxprot)
941 goto nomerge;
942
943 if (prev_entry->next->inheritance != inherit ||
944 prev_entry->next->advice != advice)
945 goto nomerge;
946
947 /* wiring status must match (new area is unwired) */
948 if (VM_MAPENT_ISWIRED(prev_entry->next))
949 goto nomerge;
950
951 /*
952 * can't extend a shared amap. note: no need to lock amap to
953 * look at refs since we don't care about its exact value.
954 * if it is one (i.e. we have only reference) it will stay there.
955 *
956 * note that we also can't merge two amaps, so if we
957 * merged with the previous entry which has an amap,
958 * and the next entry also has an amap, we give up.
959 *
960 * Interesting cases:
961 * amap, new, amap -> give up second merge (single fwd extend)
962 * amap, new, none -> double forward extend (extend again here)
963 * none, new, amap -> double backward extend (done here)
964 * uobj, new, amap -> single backward extend (done here)
965 *
966 * XXX should we attempt to deal with someone refilling
967 * the deallocated region between two entries that are
968 * backed by the same amap (ie, arefs is 2, "prev" and
969 * "next" refer to it, and adding this allocation will
970 * close the hole, thus restoring arefs to 1 and
971 * deallocating the "next" vm_map_entry)? -- @@@
972 */
973
974 if (prev_entry->next->aref.ar_amap &&
975 (amap_refs(prev_entry->next->aref.ar_amap) != 1 ||
976 (merged && prev_entry->aref.ar_amap))) {
977 goto nomerge;
978 }
979
980 if (merged) {
981 /*
982 * Try to extend the amap of the previous entry to
983 * cover the next entry as well. If it doesn't work
984 * just skip on, don't actually give up, since we've
985 * already completed the back merge.
986 */
987 if (prev_entry->aref.ar_amap) {
988 if (amap_extend(prev_entry,
989 prev_entry->next->end -
990 prev_entry->next->start,
991 amapwaitflag | AMAP_EXTEND_FORWARDS))
992 goto nomerge;
993 }
994
995 /*
996 * Try to extend the amap of the *next* entry
997 * back to cover the new allocation *and* the
998 * previous entry as well (the previous merge
999 * didn't have an amap already otherwise we
1000 * wouldn't be checking here for an amap). If
1001 * it doesn't work just skip on, again, don't
1002 * actually give up, since we've already
1003 * completed the back merge.
1004 */
1005 else if (prev_entry->next->aref.ar_amap) {
1006 if (amap_extend(prev_entry->next,
1007 prev_entry->end -
1008 prev_entry->start,
1009 amapwaitflag | AMAP_EXTEND_BACKWARDS))
1010 goto nomerge;
1011 }
1012 } else {
1013 /*
1014 * Pull the next entry's amap backwards to cover this
1015 * new allocation.
1016 */
1017 if (prev_entry->next->aref.ar_amap) {
1018 error = amap_extend(prev_entry->next, size,
1019 amapwaitflag | AMAP_EXTEND_BACKWARDS);
1020 if (error) {
1021 vm_map_unlock(map);
1022 if (new_entry) {
1023 uvm_mapent_free(new_entry);
1024 }
1025 return error;
1026 }
1027 }
1028 }
1029
1030 if (merged) {
1031 if (kmap) {
1032 UVMCNT_DECR(map_kbackmerge);
1033 UVMCNT_INCR(map_kbimerge);
1034 } else {
1035 UVMCNT_DECR(map_ubackmerge);
1036 UVMCNT_INCR(map_ubimerge);
1037 }
1038 } else {
1039 if (kmap)
1040 UVMCNT_INCR(map_kforwmerge);
1041 else
1042 UVMCNT_INCR(map_uforwmerge);
1043 }
1044 UVMHIST_LOG(maphist," starting forward merge", 0, 0, 0, 0);
1045
1046 /*
1047 * drop our reference to uobj since we are extending a reference
1048 * that we already have (the ref count can not drop to zero).
1049 * (if merged, we've already detached)
1050 */
1051 if (uobj && uobj->pgops->pgo_detach && !merged)
1052 uobj->pgops->pgo_detach(uobj);
1053
1054 if (merged) {
1055 struct vm_map_entry *dead = prev_entry->next;
1056 prev_entry->end = dead->end;
1057 uvm_map_entry_unlink(map, dead);
1058 if (dead->aref.ar_amap != NULL) {
1059 prev_entry->aref = dead->aref;
1060 dead->aref.ar_amap = NULL;
1061 }
1062 uvm_mapent_free(dead);
1063 } else {
1064 prev_entry->next->start -= size;
1065 if (prev_entry != &map->header)
1066 uvm_rb_fixup(map, prev_entry);
1067 if (uobj)
1068 prev_entry->next->offset = uoffset;
1069 }
1070
1071 uvm_tree_sanity(map, "map forwardmerged");
1072
1073 UVMHIST_LOG(maphist,"<- done forwardmerge", 0, 0, 0, 0);
1074 if (new_entry) {
1075 uvm_mapent_free(new_entry);
1076 new_entry = NULL;
1077 }
1078 merged++;
1079 }
1080
1081 nomerge:
1082 if (!merged) {
1083 UVMHIST_LOG(maphist," allocating new map entry", 0, 0, 0, 0);
1084 if (kmap)
1085 UVMCNT_INCR(map_knomerge);
1086 else
1087 UVMCNT_INCR(map_unomerge);
1088
1089 /*
1090 * allocate new entry and link it in.
1091 */
1092
1093 if (new_entry == NULL) {
1094 new_entry = uvm_mapent_alloc(map,
1095 (flags & UVM_FLAG_NOWAIT));
1096 if (__predict_false(new_entry == NULL)) {
1097 vm_map_unlock(map);
1098 return ENOMEM;
1099 }
1100 }
1101 new_entry->start = *startp;
1102 new_entry->end = new_entry->start + size;
1103 new_entry->object.uvm_obj = uobj;
1104 new_entry->offset = uoffset;
1105
1106 if (uobj)
1107 new_entry->etype = UVM_ET_OBJ;
1108 else
1109 new_entry->etype = 0;
1110
1111 if (flags & UVM_FLAG_COPYONW) {
1112 new_entry->etype |= UVM_ET_COPYONWRITE;
1113 if ((flags & UVM_FLAG_OVERLAY) == 0)
1114 new_entry->etype |= UVM_ET_NEEDSCOPY;
1115 }
1116 if (flags & UVM_FLAG_NOMERGE) {
1117 new_entry->flags |= UVM_MAP_NOMERGE;
1118 }
1119
1120 new_entry->protection = prot;
1121 new_entry->max_protection = maxprot;
1122 new_entry->inheritance = inherit;
1123 new_entry->wired_count = 0;
1124 new_entry->advice = advice;
1125 if (flags & UVM_FLAG_OVERLAY) {
1126
1127 /*
1128 * to_add: for BSS we overallocate a little since we
1129 * are likely to extend
1130 */
1131
1132 vaddr_t to_add = (flags & UVM_FLAG_AMAPPAD) ?
1133 UVM_AMAP_CHUNK << PAGE_SHIFT : 0;
1134 struct vm_amap *amap = amap_alloc(size, to_add,
1135 (flags & UVM_FLAG_NOWAIT) ? M_NOWAIT : M_WAITOK);
1136 if (__predict_false(amap == NULL)) {
1137 vm_map_unlock(map);
1138 uvm_mapent_free(new_entry);
1139 return ENOMEM;
1140 }
1141 new_entry->aref.ar_pageoff = 0;
1142 new_entry->aref.ar_amap = amap;
1143 } else {
1144 new_entry->aref.ar_pageoff = 0;
1145 new_entry->aref.ar_amap = NULL;
1146 }
1147 uvm_map_entry_link(map, prev_entry, new_entry);
1148
1149 /*
1150 * Update the free space hint
1151 */
1152
1153 if ((map->first_free == prev_entry) &&
1154 (prev_entry->end >= new_entry->start))
1155 map->first_free = new_entry;
1156 }
1157
1158 map->size += size;
1159
1160 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
1161 vm_map_unlock(map);
1162 return 0;
1163 }
1164
1165 /*
1166 * uvm_map_lookup_entry: find map entry at or before an address
1167 *
1168 * => map must at least be read-locked by caller
1169 * => entry is returned in "entry"
1170 * => return value is true if address is in the returned entry
1171 */
1172
1173 boolean_t
1174 uvm_map_lookup_entry(struct vm_map *map, vaddr_t address,
1175 struct vm_map_entry **entry /* OUT */)
1176 {
1177 struct vm_map_entry *cur;
1178 boolean_t use_tree = FALSE;
1179 UVMHIST_FUNC("uvm_map_lookup_entry");
1180 UVMHIST_CALLED(maphist);
1181
1182 UVMHIST_LOG(maphist,"(map=0x%x,addr=0x%x,ent=0x%x)",
1183 map, address, entry, 0);
1184
1185 /*
1186 * start looking either from the head of the
1187 * list, or from the hint.
1188 */
1189
1190 simple_lock(&map->hint_lock);
1191 cur = map->hint;
1192 simple_unlock(&map->hint_lock);
1193
1194 if (cur == &map->header)
1195 cur = cur->next;
1196
1197 UVMCNT_INCR(uvm_mlk_call);
1198 if (address >= cur->start) {
1199
1200 /*
1201 * go from hint to end of list.
1202 *
1203 * but first, make a quick check to see if
1204 * we are already looking at the entry we
1205 * want (which is usually the case).
1206 * note also that we don't need to save the hint
1207 * here... it is the same hint (unless we are
1208 * at the header, in which case the hint didn't
1209 * buy us anything anyway).
1210 */
1211
1212 if (cur != &map->header && cur->end > address) {
1213 UVMCNT_INCR(uvm_mlk_hint);
1214 *entry = cur;
1215 UVMHIST_LOG(maphist,"<- got it via hint (0x%x)",
1216 cur, 0, 0, 0);
1217 return (TRUE);
1218 }
1219
1220 if (map->nentries > 30)
1221 use_tree = TRUE;
1222 } else {
1223
1224 /*
1225 * invalid hint. use tree.
1226 */
1227 use_tree = TRUE;
1228 }
1229
1230 uvm_tree_sanity(map, __func__);
1231
1232 if (use_tree) {
1233 struct vm_map_entry *prev = &map->header;
1234 cur = RB_ROOT(&map->rbhead);
1235
1236 /*
1237 * Simple lookup in the tree. Happens when the hint is
1238 * invalid, or nentries reach a threshold.
1239 */
1240 while (cur) {
1241 if (address >= cur->start) {
1242 if (address < cur->end) {
1243 *entry = cur;
1244 goto got;
1245 }
1246 prev = cur;
1247 cur = RB_RIGHT(cur, rb_entry);
1248 } else
1249 cur = RB_LEFT(cur, rb_entry);
1250 }
1251 *entry = prev;
1252 goto failed;
1253 }
1254
1255 /*
1256 * search linearly
1257 */
1258
1259 while (cur != &map->header) {
1260 if (cur->end > address) {
1261 if (address >= cur->start) {
1262 /*
1263 * save this lookup for future
1264 * hints, and return
1265 */
1266
1267 *entry = cur;
1268 got:
1269 SAVE_HINT(map, map->hint, *entry);
1270 UVMHIST_LOG(maphist,"<- search got it (0x%x)",
1271 cur, 0, 0, 0);
1272 KDASSERT((*entry)->start <= address);
1273 KDASSERT(address < (*entry)->end);
1274 return (TRUE);
1275 }
1276 break;
1277 }
1278 cur = cur->next;
1279 }
1280 *entry = cur->prev;
1281 failed:
1282 SAVE_HINT(map, map->hint, *entry);
1283 UVMHIST_LOG(maphist,"<- failed!",0,0,0,0);
1284 KDASSERT((*entry) == &map->header || (*entry)->end <= address);
1285 KDASSERT((*entry)->next == &map->header ||
1286 address < (*entry)->next->start);
1287 return (FALSE);
1288 }
1289
1290 /*
1291 * See if the range between start and start + length fits in the gap
1292 * entry->next->start and entry->end. Returns 1 if fits, 0 if doesn't
1293 * fit, and -1 address wraps around.
1294 */
1295 static __inline int
1296 uvm_map_space_avail(vaddr_t *start, vsize_t length, voff_t uoffset,
1297 vsize_t align, int topdown, struct vm_map_entry *entry)
1298 {
1299 vaddr_t end;
1300
1301 #ifdef PMAP_PREFER
1302 /*
1303 * push start address forward as needed to avoid VAC alias problems.
1304 * we only do this if a valid offset is specified.
1305 */
1306
1307 if (uoffset != UVM_UNKNOWN_OFFSET)
1308 PMAP_PREFER(uoffset, start);
1309 #endif
1310 if (align != 0) {
1311 if ((*start & (align - 1)) != 0) {
1312 if (topdown)
1313 *start &= ~(align - 1);
1314 else
1315 *start = roundup(*start, align);
1316 }
1317 /*
1318 * XXX Should we PMAP_PREFER() here again?
1319 */
1320 }
1321
1322 /*
1323 * Find the end of the proposed new region. Be sure we didn't
1324 * wrap around the address; if so, we lose. Otherwise, if the
1325 * proposed new region fits before the next entry, we win.
1326 */
1327
1328 end = *start + length;
1329 if (end < *start)
1330 return (-1);
1331
1332 if (entry->next->start >= end && *start >= entry->end)
1333 return (1);
1334
1335 return (0);
1336 }
1337
1338 /*
1339 * uvm_map_findspace: find "length" sized space in "map".
1340 *
1341 * => "hint" is a hint about where we want it, unless FINDSPACE_FIXED is
1342 * set (in which case we insist on using "hint").
1343 * => "result" is VA returned
1344 * => uobj/uoffset are to be used to handle VAC alignment, if required
1345 * => if `align' is non-zero, we attempt to align to that value.
1346 * => caller must at least have read-locked map
1347 * => returns NULL on failure, or pointer to prev. map entry if success
1348 * => note this is a cross between the old vm_map_findspace and vm_map_find
1349 */
1350
1351 struct vm_map_entry *
1352 uvm_map_findspace(struct vm_map *map, vaddr_t hint, vsize_t length,
1353 vaddr_t *result /* OUT */, struct uvm_object *uobj, voff_t uoffset,
1354 vsize_t align, int flags)
1355 {
1356 struct vm_map_entry *entry;
1357 struct vm_map_entry *child, *prev, *tmp;
1358 vaddr_t orig_hint;
1359 const int topdown = map->flags & VM_MAP_TOPDOWN;
1360 UVMHIST_FUNC("uvm_map_findspace");
1361 UVMHIST_CALLED(maphist);
1362
1363 UVMHIST_LOG(maphist, "(map=0x%x, hint=0x%x, len=%d, flags=0x%x)",
1364 map, hint, length, flags);
1365 KASSERT((align & (align - 1)) == 0);
1366 KASSERT((flags & UVM_FLAG_FIXED) == 0 || align == 0);
1367
1368 uvm_tree_sanity(map, "map_findspace entry");
1369
1370 /*
1371 * remember the original hint. if we are aligning, then we
1372 * may have to try again with no alignment constraint if
1373 * we fail the first time.
1374 */
1375
1376 orig_hint = hint;
1377 if (hint < map->min_offset) { /* check ranges ... */
1378 if (flags & UVM_FLAG_FIXED) {
1379 UVMHIST_LOG(maphist,"<- VA below map range",0,0,0,0);
1380 return (NULL);
1381 }
1382 hint = map->min_offset;
1383 }
1384 if (hint > map->max_offset) {
1385 UVMHIST_LOG(maphist,"<- VA 0x%x > range [0x%x->0x%x]",
1386 hint, map->min_offset, map->max_offset, 0);
1387 return (NULL);
1388 }
1389
1390 /*
1391 * Look for the first possible address; if there's already
1392 * something at this address, we have to start after it.
1393 */
1394
1395 /*
1396 * @@@: there are four, no, eight cases to consider.
1397 *
1398 * 0: found, fixed, bottom up -> fail
1399 * 1: found, fixed, top down -> fail
1400 * 2: found, not fixed, bottom up -> start after entry->end,
1401 * loop up
1402 * 3: found, not fixed, top down -> start before entry->start,
1403 * loop down
1404 * 4: not found, fixed, bottom up -> check entry->next->start, fail
1405 * 5: not found, fixed, top down -> check entry->next->start, fail
1406 * 6: not found, not fixed, bottom up -> check entry->next->start,
1407 * loop up
1408 * 7: not found, not fixed, top down -> check entry->next->start,
1409 * loop down
1410 *
1411 * as you can see, it reduces to roughly five cases, and that
1412 * adding top down mapping only adds one unique case (without
1413 * it, there would be four cases).
1414 */
1415
1416 if ((flags & UVM_FLAG_FIXED) == 0 && hint == map->min_offset) {
1417 entry = map->first_free;
1418 } else {
1419 if (uvm_map_lookup_entry(map, hint, &entry)) {
1420 /* "hint" address already in use ... */
1421 if (flags & UVM_FLAG_FIXED) {
1422 UVMHIST_LOG(maphist, "<- fixed & VA in use",
1423 0, 0, 0, 0);
1424 return (NULL);
1425 }
1426 if (topdown)
1427 /* Start from lower gap. */
1428 entry = entry->prev;
1429 } else if (flags & UVM_FLAG_FIXED) {
1430 if (entry->next->start >= hint + length &&
1431 hint + length > hint)
1432 goto found;
1433
1434 /* "hint" address is gap but too small */
1435 UVMHIST_LOG(maphist, "<- fixed mapping failed",
1436 0, 0, 0, 0);
1437 return (NULL); /* only one shot at it ... */
1438 } else {
1439 /*
1440 * See if given hint fits in this gap.
1441 */
1442 switch (uvm_map_space_avail(&hint, length,
1443 uoffset, align, topdown, entry)) {
1444 case 1:
1445 goto found;
1446 case -1:
1447 goto wraparound;
1448 }
1449
1450 if (topdown) {
1451 /*
1452 * Still there is a chance to fit
1453 * if hint > entry->end.
1454 */
1455 } else {
1456 /* Start from higer gap. */
1457 entry = entry->next;
1458 if (entry == &map->header)
1459 goto notfound;
1460 goto nextgap;
1461 }
1462 }
1463 }
1464
1465 /*
1466 * Note that all UVM_FLAGS_FIXED case is already handled.
1467 */
1468 KDASSERT((flags & UVM_FLAG_FIXED) == 0);
1469
1470 /* Try to find the space in the red-black tree */
1471
1472 /* Check slot before any entry */
1473 hint = topdown ? entry->next->start - length : entry->end;
1474 switch (uvm_map_space_avail(&hint, length, uoffset, align,
1475 topdown, entry)) {
1476 case 1:
1477 goto found;
1478 case -1:
1479 goto wraparound;
1480 }
1481
1482 nextgap:
1483 KDASSERT((flags & UVM_FLAG_FIXED) == 0);
1484 /* If there is not enough space in the whole tree, we fail */
1485 tmp = RB_ROOT(&map->rbhead);
1486 if (tmp == NULL || tmp->space < length)
1487 goto notfound;
1488
1489 prev = NULL; /* previous candidate */
1490
1491 /* Find an entry close to hint that has enough space */
1492 for (; tmp;) {
1493 KASSERT(tmp->next->start == tmp->end + tmp->ownspace);
1494 if (topdown) {
1495 if (tmp->next->start < hint + length &&
1496 (prev == NULL || tmp->end > prev->end)) {
1497 if (tmp->ownspace >= length)
1498 prev = tmp;
1499 else if ((child = RB_LEFT(tmp, rb_entry))
1500 != NULL && child->space >= length)
1501 prev = tmp;
1502 }
1503 } else {
1504 if (tmp->end >= hint &&
1505 (prev == NULL || tmp->end < prev->end)) {
1506 if (tmp->ownspace >= length)
1507 prev = tmp;
1508 else if ((child = RB_RIGHT(tmp, rb_entry))
1509 != NULL && child->space >= length)
1510 prev = tmp;
1511 }
1512 }
1513 if (tmp->next->start < hint + length)
1514 child = RB_RIGHT(tmp, rb_entry);
1515 else if (tmp->end > hint)
1516 child = RB_LEFT(tmp, rb_entry);
1517 else {
1518 if (tmp->ownspace >= length)
1519 break;
1520 if (topdown)
1521 child = RB_LEFT(tmp, rb_entry);
1522 else
1523 child = RB_RIGHT(tmp, rb_entry);
1524 }
1525 if (child == NULL || child->space < length)
1526 break;
1527 tmp = child;
1528 }
1529
1530 if (tmp != NULL && tmp->start < hint && hint < tmp->next->start) {
1531 /*
1532 * Check if the entry that we found satifies the
1533 * space requirement
1534 */
1535 if (topdown) {
1536 if (hint > tmp->next->start - length)
1537 hint = tmp->next->start - length;
1538 } else {
1539 if (hint < tmp->end)
1540 hint = tmp->end;
1541 }
1542 switch (uvm_map_space_avail(&hint, length, uoffset, align,
1543 topdown, tmp)) {
1544 case 1:
1545 entry = tmp;
1546 goto found;
1547 case -1:
1548 goto wraparound;
1549 }
1550 if (tmp->ownspace >= length)
1551 goto listsearch;
1552 }
1553 if (prev == NULL)
1554 goto notfound;
1555
1556 if (topdown) {
1557 KASSERT(orig_hint >= prev->next->start - length ||
1558 prev->next->start - length > prev->next->start);
1559 hint = prev->next->start - length;
1560 } else {
1561 KASSERT(orig_hint <= prev->end);
1562 hint = prev->end;
1563 }
1564 switch (uvm_map_space_avail(&hint, length, uoffset, align,
1565 topdown, prev)) {
1566 case 1:
1567 entry = prev;
1568 goto found;
1569 case -1:
1570 goto wraparound;
1571 }
1572 if (prev->ownspace >= length)
1573 goto listsearch;
1574
1575 if (topdown)
1576 tmp = RB_LEFT(prev, rb_entry);
1577 else
1578 tmp = RB_RIGHT(prev, rb_entry);
1579 for (;;) {
1580 KASSERT(tmp && tmp->space >= length);
1581 if (topdown)
1582 child = RB_RIGHT(tmp, rb_entry);
1583 else
1584 child = RB_LEFT(tmp, rb_entry);
1585 if (child && child->space >= length) {
1586 tmp = child;
1587 continue;
1588 }
1589 if (tmp->ownspace >= length)
1590 break;
1591 if (topdown)
1592 tmp = RB_LEFT(tmp, rb_entry);
1593 else
1594 tmp = RB_RIGHT(tmp, rb_entry);
1595 }
1596
1597 if (topdown) {
1598 KASSERT(orig_hint >= tmp->next->start - length ||
1599 tmp->next->start - length > tmp->next->start);
1600 hint = tmp->next->start - length;
1601 } else {
1602 KASSERT(orig_hint <= tmp->end);
1603 hint = tmp->end;
1604 }
1605 switch (uvm_map_space_avail(&hint, length, uoffset, align,
1606 topdown, tmp)) {
1607 case 1:
1608 entry = tmp;
1609 goto found;
1610 case -1:
1611 goto wraparound;
1612 }
1613
1614 /*
1615 * The tree fails to find an entry because of offset or alignment
1616 * restrictions. Search the list instead.
1617 */
1618 listsearch:
1619 /*
1620 * Look through the rest of the map, trying to fit a new region in
1621 * the gap between existing regions, or after the very last region.
1622 * note: entry->end = base VA of current gap,
1623 * entry->next->start = VA of end of current gap
1624 */
1625
1626 for (;;) {
1627 /* Update hint for current gap. */
1628 hint = topdown ? entry->next->start - length : entry->end;
1629
1630 /* See if it fits. */
1631 switch (uvm_map_space_avail(&hint, length, uoffset, align,
1632 topdown, entry)) {
1633 case 1:
1634 goto found;
1635 case -1:
1636 goto wraparound;
1637 }
1638
1639 /* Advance to next/previous gap */
1640 if (topdown) {
1641 if (entry == &map->header) {
1642 UVMHIST_LOG(maphist, "<- failed (off start)",
1643 0,0,0,0);
1644 goto notfound;
1645 }
1646 entry = entry->prev;
1647 } else {
1648 entry = entry->next;
1649 if (entry == &map->header) {
1650 UVMHIST_LOG(maphist, "<- failed (off end)",
1651 0,0,0,0);
1652 goto notfound;
1653 }
1654 }
1655 }
1656
1657 found:
1658 SAVE_HINT(map, map->hint, entry);
1659 *result = hint;
1660 UVMHIST_LOG(maphist,"<- got it! (result=0x%x)", hint, 0,0,0);
1661 KASSERT( topdown || hint >= orig_hint);
1662 KASSERT(!topdown || hint <= orig_hint);
1663 KASSERT(entry->end <= hint);
1664 KASSERT(hint + length <= entry->next->start);
1665 return (entry);
1666
1667 wraparound:
1668 UVMHIST_LOG(maphist, "<- failed (wrap around)", 0,0,0,0);
1669
1670 return (NULL);
1671
1672 notfound:
1673 UVMHIST_LOG(maphist, "<- failed (notfound)", 0,0,0,0);
1674
1675 return (NULL);
1676 }
1677
1678 /*
1679 * U N M A P - m a i n h e l p e r f u n c t i o n s
1680 */
1681
1682 /*
1683 * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop")
1684 *
1685 * => caller must check alignment and size
1686 * => map must be locked by caller
1687 * => we return a list of map entries that we've remove from the map
1688 * in "entry_list"
1689 */
1690
1691 void
1692 uvm_unmap_remove(struct vm_map *map, vaddr_t start, vaddr_t end,
1693 struct vm_map_entry **entry_list /* OUT */)
1694 {
1695 struct vm_map_entry *entry, *first_entry, *next;
1696 vaddr_t len;
1697 UVMHIST_FUNC("uvm_unmap_remove"); UVMHIST_CALLED(maphist);
1698
1699 UVMHIST_LOG(maphist,"(map=0x%x, start=0x%x, end=0x%x)",
1700 map, start, end, 0);
1701 VM_MAP_RANGE_CHECK(map, start, end);
1702
1703 uvm_tree_sanity(map, "unmap_remove entry");
1704
1705 /*
1706 * find first entry
1707 */
1708
1709 if (uvm_map_lookup_entry(map, start, &first_entry) == TRUE) {
1710 /* clip and go... */
1711 entry = first_entry;
1712 UVM_MAP_CLIP_START(map, entry, start);
1713 /* critical! prevents stale hint */
1714 SAVE_HINT(map, entry, entry->prev);
1715 } else {
1716 entry = first_entry->next;
1717 }
1718
1719 /*
1720 * Save the free space hint
1721 */
1722
1723 if (map->first_free->start >= start)
1724 map->first_free = entry->prev;
1725
1726 /*
1727 * note: we now re-use first_entry for a different task. we remove
1728 * a number of map entries from the map and save them in a linked
1729 * list headed by "first_entry". once we remove them from the map
1730 * the caller should unlock the map and drop the references to the
1731 * backing objects [c.f. uvm_unmap_detach]. the object is to
1732 * separate unmapping from reference dropping. why?
1733 * [1] the map has to be locked for unmapping
1734 * [2] the map need not be locked for reference dropping
1735 * [3] dropping references may trigger pager I/O, and if we hit
1736 * a pager that does synchronous I/O we may have to wait for it.
1737 * [4] we would like all waiting for I/O to occur with maps unlocked
1738 * so that we don't block other threads.
1739 */
1740
1741 first_entry = NULL;
1742 *entry_list = NULL;
1743
1744 /*
1745 * break up the area into map entry sized regions and unmap. note
1746 * that all mappings have to be removed before we can even consider
1747 * dropping references to amaps or VM objects (otherwise we could end
1748 * up with a mapping to a page on the free list which would be very bad)
1749 */
1750
1751 while ((entry != &map->header) && (entry->start < end)) {
1752 UVM_MAP_CLIP_END(map, entry, end);
1753 next = entry->next;
1754 len = entry->end - entry->start;
1755
1756 /*
1757 * unwire before removing addresses from the pmap; otherwise
1758 * unwiring will put the entries back into the pmap (XXX).
1759 */
1760
1761 if (VM_MAPENT_ISWIRED(entry)) {
1762 uvm_map_entry_unwire(map, entry);
1763 }
1764 if ((map->flags & VM_MAP_PAGEABLE) == 0) {
1765
1766 /*
1767 * if the map is non-pageable, any pages mapped there
1768 * must be wired and entered with pmap_kenter_pa(),
1769 * and we should free any such pages immediately.
1770 * this is mostly used for kmem_map and mb_map.
1771 */
1772
1773 uvm_km_pgremove_intrsafe(entry->start, entry->end);
1774 pmap_kremove(entry->start, len);
1775 } else if (UVM_ET_ISOBJ(entry) &&
1776 UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)) {
1777 KASSERT(vm_map_pmap(map) == pmap_kernel());
1778
1779 /*
1780 * note: kernel object mappings are currently used in
1781 * two ways:
1782 * [1] "normal" mappings of pages in the kernel object
1783 * [2] uvm_km_valloc'd allocations in which we
1784 * pmap_enter in some non-kernel-object page
1785 * (e.g. vmapbuf).
1786 *
1787 * for case [1], we need to remove the mapping from
1788 * the pmap and then remove the page from the kernel
1789 * object (because, once pages in a kernel object are
1790 * unmapped they are no longer needed, unlike, say,
1791 * a vnode where you might want the data to persist
1792 * until flushed out of a queue).
1793 *
1794 * for case [2], we need to remove the mapping from
1795 * the pmap. there shouldn't be any pages at the
1796 * specified offset in the kernel object [but it
1797 * doesn't hurt to call uvm_km_pgremove just to be
1798 * safe?]
1799 *
1800 * uvm_km_pgremove currently does the following:
1801 * for pages in the kernel object in range:
1802 * - drops the swap slot
1803 * - uvm_pagefree the page
1804 */
1805
1806 /*
1807 * remove mappings from pmap and drop the pages
1808 * from the object. offsets are always relative
1809 * to vm_map_min(kernel_map).
1810 */
1811
1812 pmap_remove(pmap_kernel(), entry->start,
1813 entry->start + len);
1814 uvm_km_pgremove(entry->object.uvm_obj,
1815 entry->start - vm_map_min(kernel_map),
1816 entry->end - vm_map_min(kernel_map));
1817
1818 /*
1819 * null out kernel_object reference, we've just
1820 * dropped it
1821 */
1822
1823 entry->etype &= ~UVM_ET_OBJ;
1824 entry->object.uvm_obj = NULL;
1825 } else if (UVM_ET_ISOBJ(entry) || entry->aref.ar_amap) {
1826
1827 /*
1828 * remove mappings the standard way.
1829 */
1830
1831 pmap_remove(map->pmap, entry->start, entry->end);
1832 }
1833
1834 /*
1835 * remove entry from map and put it on our list of entries
1836 * that we've nuked. then go to next entry.
1837 */
1838
1839 UVMHIST_LOG(maphist, " removed map entry 0x%x", entry, 0, 0,0);
1840
1841 /* critical! prevents stale hint */
1842 SAVE_HINT(map, entry, entry->prev);
1843
1844 uvm_map_entry_unlink(map, entry);
1845 KASSERT(map->size >= len);
1846 map->size -= len;
1847 entry->prev = NULL;
1848 entry->next = first_entry;
1849 first_entry = entry;
1850 entry = next;
1851 }
1852 if ((map->flags & VM_MAP_DYING) == 0) {
1853 pmap_update(vm_map_pmap(map));
1854 }
1855
1856 uvm_tree_sanity(map, "unmap_remove leave");
1857
1858 /*
1859 * now we've cleaned up the map and are ready for the caller to drop
1860 * references to the mapped objects.
1861 */
1862
1863 *entry_list = first_entry;
1864 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
1865 }
1866
1867 /*
1868 * uvm_unmap_detach: drop references in a chain of map entries
1869 *
1870 * => we will free the map entries as we traverse the list.
1871 */
1872
1873 void
1874 uvm_unmap_detach(struct vm_map_entry *first_entry, int flags)
1875 {
1876 struct vm_map_entry *next_entry;
1877 UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist);
1878
1879 while (first_entry) {
1880 KASSERT(!VM_MAPENT_ISWIRED(first_entry));
1881 UVMHIST_LOG(maphist,
1882 " detach 0x%x: amap=0x%x, obj=0x%x, submap?=%d",
1883 first_entry, first_entry->aref.ar_amap,
1884 first_entry->object.uvm_obj,
1885 UVM_ET_ISSUBMAP(first_entry));
1886
1887 /*
1888 * drop reference to amap, if we've got one
1889 */
1890
1891 if (first_entry->aref.ar_amap)
1892 uvm_map_unreference_amap(first_entry, flags);
1893
1894 /*
1895 * drop reference to our backing object, if we've got one
1896 */
1897
1898 KASSERT(!UVM_ET_ISSUBMAP(first_entry));
1899 if (UVM_ET_ISOBJ(first_entry) &&
1900 first_entry->object.uvm_obj->pgops->pgo_detach) {
1901 (*first_entry->object.uvm_obj->pgops->pgo_detach)
1902 (first_entry->object.uvm_obj);
1903 }
1904 next_entry = first_entry->next;
1905 uvm_mapent_free(first_entry);
1906 first_entry = next_entry;
1907 }
1908 UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
1909 }
1910
1911 /*
1912 * E X T R A C T I O N F U N C T I O N S
1913 */
1914
1915 /*
1916 * uvm_map_reserve: reserve space in a vm_map for future use.
1917 *
1918 * => we reserve space in a map by putting a dummy map entry in the
1919 * map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE)
1920 * => map should be unlocked (we will write lock it)
1921 * => we return true if we were able to reserve space
1922 * => XXXCDC: should be inline?
1923 */
1924
1925 int
1926 uvm_map_reserve(struct vm_map *map, vsize_t size,
1927 vaddr_t offset /* hint for pmap_prefer */,
1928 vsize_t align /* alignment hint */,
1929 vaddr_t *raddr /* IN:hint, OUT: reserved VA */)
1930 {
1931 UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist);
1932
1933 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x, offset=0x%x,addr=0x%x)",
1934 map,size,offset,raddr);
1935
1936 size = round_page(size);
1937 if (*raddr < vm_map_min(map))
1938 *raddr = vm_map_min(map); /* hint */
1939
1940 /*
1941 * reserve some virtual space.
1942 */
1943
1944 if (uvm_map(map, raddr, size, NULL, offset, 0,
1945 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
1946 UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
1947 UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
1948 return (FALSE);
1949 }
1950
1951 UVMHIST_LOG(maphist, "<- done (*raddr=0x%x)", *raddr,0,0,0);
1952 return (TRUE);
1953 }
1954
1955 /*
1956 * uvm_map_replace: replace a reserved (blank) area of memory with
1957 * real mappings.
1958 *
1959 * => caller must WRITE-LOCK the map
1960 * => we return TRUE if replacement was a success
1961 * => we expect the newents chain to have nnewents entrys on it and
1962 * we expect newents->prev to point to the last entry on the list
1963 * => note newents is allowed to be NULL
1964 */
1965
1966 int
1967 uvm_map_replace(struct vm_map *map, vaddr_t start, vaddr_t end,
1968 struct vm_map_entry *newents, int nnewents)
1969 {
1970 struct vm_map_entry *oldent, *last;
1971
1972 uvm_tree_sanity(map, "map_replace entry");
1973
1974 /*
1975 * first find the blank map entry at the specified address
1976 */
1977
1978 if (!uvm_map_lookup_entry(map, start, &oldent)) {
1979 return (FALSE);
1980 }
1981
1982 /*
1983 * check to make sure we have a proper blank entry
1984 */
1985
1986 if (oldent->start != start || oldent->end != end ||
1987 oldent->object.uvm_obj != NULL || oldent->aref.ar_amap != NULL) {
1988 return (FALSE);
1989 }
1990
1991 #ifdef DIAGNOSTIC
1992
1993 /*
1994 * sanity check the newents chain
1995 */
1996
1997 {
1998 struct vm_map_entry *tmpent = newents;
1999 int nent = 0;
2000 vaddr_t cur = start;
2001
2002 while (tmpent) {
2003 nent++;
2004 if (tmpent->start < cur)
2005 panic("uvm_map_replace1");
2006 if (tmpent->start > tmpent->end || tmpent->end > end) {
2007 printf("tmpent->start=0x%lx, tmpent->end=0x%lx, end=0x%lx\n",
2008 tmpent->start, tmpent->end, end);
2009 panic("uvm_map_replace2");
2010 }
2011 cur = tmpent->end;
2012 if (tmpent->next) {
2013 if (tmpent->next->prev != tmpent)
2014 panic("uvm_map_replace3");
2015 } else {
2016 if (newents->prev != tmpent)
2017 panic("uvm_map_replace4");
2018 }
2019 tmpent = tmpent->next;
2020 }
2021 if (nent != nnewents)
2022 panic("uvm_map_replace5");
2023 }
2024 #endif
2025
2026 /*
2027 * map entry is a valid blank! replace it. (this does all the
2028 * work of map entry link/unlink...).
2029 */
2030
2031 if (newents) {
2032 last = newents->prev;
2033
2034 /* critical: flush stale hints out of map */
2035 SAVE_HINT(map, map->hint, newents);
2036 if (map->first_free == oldent)
2037 map->first_free = last;
2038
2039 last->next = oldent->next;
2040 last->next->prev = last;
2041
2042 /* Fix RB tree */
2043 uvm_rb_remove(map, oldent);
2044
2045 newents->prev = oldent->prev;
2046 newents->prev->next = newents;
2047 map->nentries = map->nentries + (nnewents - 1);
2048
2049 /* Fixup the RB tree */
2050 {
2051 int i;
2052 struct vm_map_entry *tmp;
2053
2054 tmp = newents;
2055 for (i = 0; i < nnewents && tmp; i++) {
2056 uvm_rb_insert(map, tmp);
2057 tmp = tmp->next;
2058 }
2059 }
2060 } else {
2061
2062 /* critical: flush stale hints out of map */
2063 SAVE_HINT(map, map->hint, oldent->prev);
2064 if (map->first_free == oldent)
2065 map->first_free = oldent->prev;
2066
2067 /* NULL list of new entries: just remove the old one */
2068 uvm_map_entry_unlink(map, oldent);
2069 }
2070
2071 uvm_tree_sanity(map, "map_replace leave");
2072
2073 /*
2074 * now we can free the old blank entry, unlock the map and return.
2075 */
2076
2077 uvm_mapent_free(oldent);
2078 return (TRUE);
2079 }
2080
2081 /*
2082 * uvm_map_extract: extract a mapping from a map and put it somewhere
2083 * (maybe removing the old mapping)
2084 *
2085 * => maps should be unlocked (we will write lock them)
2086 * => returns 0 on success, error code otherwise
2087 * => start must be page aligned
2088 * => len must be page sized
2089 * => flags:
2090 * UVM_EXTRACT_REMOVE: remove mappings from srcmap
2091 * UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only)
2092 * UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs
2093 * UVM_EXTRACT_FIXPROT: set prot to maxprot as we go
2094 * >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<<
2095 * >>>NOTE: QREF's must be unmapped via the QREF path, thus should only
2096 * be used from within the kernel in a kernel level map <<<
2097 */
2098
2099 int
2100 uvm_map_extract(struct vm_map *srcmap, vaddr_t start, vsize_t len,
2101 struct vm_map *dstmap, vaddr_t *dstaddrp, int flags)
2102 {
2103 vaddr_t dstaddr, end, newend, oldoffset, fudge, orig_fudge;
2104 struct vm_map_entry *chain, *endchain, *entry, *orig_entry, *newentry,
2105 *deadentry, *oldentry;
2106 vsize_t elen;
2107 int nchain, error, copy_ok;
2108 UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist);
2109
2110 UVMHIST_LOG(maphist,"(srcmap=0x%x,start=0x%x, len=0x%x", srcmap, start,
2111 len,0);
2112 UVMHIST_LOG(maphist," ...,dstmap=0x%x, flags=0x%x)", dstmap,flags,0,0);
2113
2114 uvm_tree_sanity(srcmap, "map_extract src enter");
2115 uvm_tree_sanity(dstmap, "map_extract dst enter");
2116
2117 /*
2118 * step 0: sanity check: start must be on a page boundary, length
2119 * must be page sized. can't ask for CONTIG/QREF if you asked for
2120 * REMOVE.
2121 */
2122
2123 KASSERT((start & PAGE_MASK) == 0 && (len & PAGE_MASK) == 0);
2124 KASSERT((flags & UVM_EXTRACT_REMOVE) == 0 ||
2125 (flags & (UVM_EXTRACT_CONTIG|UVM_EXTRACT_QREF)) == 0);
2126
2127 /*
2128 * step 1: reserve space in the target map for the extracted area
2129 */
2130
2131 dstaddr = vm_map_min(dstmap);
2132 if (uvm_map_reserve(dstmap, len, start, 0, &dstaddr) == FALSE)
2133 return (ENOMEM);
2134 *dstaddrp = dstaddr; /* pass address back to caller */
2135 UVMHIST_LOG(maphist, " dstaddr=0x%x", dstaddr,0,0,0);
2136
2137 /*
2138 * step 2: setup for the extraction process loop by init'ing the
2139 * map entry chain, locking src map, and looking up the first useful
2140 * entry in the map.
2141 */
2142
2143 end = start + len;
2144 newend = dstaddr + len;
2145 chain = endchain = NULL;
2146 nchain = 0;
2147 vm_map_lock(srcmap);
2148
2149 if (uvm_map_lookup_entry(srcmap, start, &entry)) {
2150
2151 /* "start" is within an entry */
2152 if (flags & UVM_EXTRACT_QREF) {
2153
2154 /*
2155 * for quick references we don't clip the entry, so
2156 * the entry may map space "before" the starting
2157 * virtual address... this is the "fudge" factor
2158 * (which can be non-zero only the first time
2159 * through the "while" loop in step 3).
2160 */
2161
2162 fudge = start - entry->start;
2163 } else {
2164
2165 /*
2166 * normal reference: we clip the map to fit (thus
2167 * fudge is zero)
2168 */
2169
2170 UVM_MAP_CLIP_START(srcmap, entry, start);
2171 SAVE_HINT(srcmap, srcmap->hint, entry->prev);
2172 fudge = 0;
2173 }
2174 } else {
2175
2176 /* "start" is not within an entry ... skip to next entry */
2177 if (flags & UVM_EXTRACT_CONTIG) {
2178 error = EINVAL;
2179 goto bad; /* definite hole here ... */
2180 }
2181
2182 entry = entry->next;
2183 fudge = 0;
2184 }
2185
2186 /* save values from srcmap for step 6 */
2187 orig_entry = entry;
2188 orig_fudge = fudge;
2189
2190 /*
2191 * step 3: now start looping through the map entries, extracting
2192 * as we go.
2193 */
2194
2195 while (entry->start < end && entry != &srcmap->header) {
2196
2197 /* if we are not doing a quick reference, clip it */
2198 if ((flags & UVM_EXTRACT_QREF) == 0)
2199 UVM_MAP_CLIP_END(srcmap, entry, end);
2200
2201 /* clear needs_copy (allow chunking) */
2202 if (UVM_ET_ISNEEDSCOPY(entry)) {
2203 amap_copy(srcmap, entry, M_NOWAIT, TRUE, start, end);
2204 if (UVM_ET_ISNEEDSCOPY(entry)) { /* failed? */
2205 error = ENOMEM;
2206 goto bad;
2207 }
2208
2209 /* amap_copy could clip (during chunk)! update fudge */
2210 if (fudge) {
2211 fudge = start - entry->start;
2212 orig_fudge = fudge;
2213 }
2214 }
2215
2216 /* calculate the offset of this from "start" */
2217 oldoffset = (entry->start + fudge) - start;
2218
2219 /* allocate a new map entry */
2220 newentry = uvm_mapent_alloc(dstmap, 0);
2221 if (newentry == NULL) {
2222 error = ENOMEM;
2223 goto bad;
2224 }
2225
2226 /* set up new map entry */
2227 newentry->next = NULL;
2228 newentry->prev = endchain;
2229 newentry->start = dstaddr + oldoffset;
2230 newentry->end =
2231 newentry->start + (entry->end - (entry->start + fudge));
2232 if (newentry->end > newend || newentry->end < newentry->start)
2233 newentry->end = newend;
2234 newentry->object.uvm_obj = entry->object.uvm_obj;
2235 if (newentry->object.uvm_obj) {
2236 if (newentry->object.uvm_obj->pgops->pgo_reference)
2237 newentry->object.uvm_obj->pgops->
2238 pgo_reference(newentry->object.uvm_obj);
2239 newentry->offset = entry->offset + fudge;
2240 } else {
2241 newentry->offset = 0;
2242 }
2243 newentry->etype = entry->etype;
2244 newentry->protection = (flags & UVM_EXTRACT_FIXPROT) ?
2245 entry->max_protection : entry->protection;
2246 newentry->max_protection = entry->max_protection;
2247 newentry->inheritance = entry->inheritance;
2248 newentry->wired_count = 0;
2249 newentry->aref.ar_amap = entry->aref.ar_amap;
2250 if (newentry->aref.ar_amap) {
2251 newentry->aref.ar_pageoff =
2252 entry->aref.ar_pageoff + (fudge >> PAGE_SHIFT);
2253 uvm_map_reference_amap(newentry, AMAP_SHARED |
2254 ((flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0));
2255 } else {
2256 newentry->aref.ar_pageoff = 0;
2257 }
2258 newentry->advice = entry->advice;
2259
2260 /* now link it on the chain */
2261 nchain++;
2262 if (endchain == NULL) {
2263 chain = endchain = newentry;
2264 } else {
2265 endchain->next = newentry;
2266 endchain = newentry;
2267 }
2268
2269 /* end of 'while' loop! */
2270 if ((flags & UVM_EXTRACT_CONTIG) && entry->end < end &&
2271 (entry->next == &srcmap->header ||
2272 entry->next->start != entry->end)) {
2273 error = EINVAL;
2274 goto bad;
2275 }
2276 entry = entry->next;
2277 fudge = 0;
2278 }
2279
2280 /*
2281 * step 4: close off chain (in format expected by uvm_map_replace)
2282 */
2283
2284 if (chain)
2285 chain->prev = endchain;
2286
2287 /*
2288 * step 5: attempt to lock the dest map so we can pmap_copy.
2289 * note usage of copy_ok:
2290 * 1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5)
2291 * 0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7
2292 */
2293
2294 if (srcmap == dstmap || vm_map_lock_try(dstmap) == TRUE) {
2295 copy_ok = 1;
2296 if (!uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
2297 nchain)) {
2298 if (srcmap != dstmap)
2299 vm_map_unlock(dstmap);
2300 error = EIO;
2301 goto bad;
2302 }
2303 } else {
2304 copy_ok = 0;
2305 /* replace defered until step 7 */
2306 }
2307
2308 /*
2309 * step 6: traverse the srcmap a second time to do the following:
2310 * - if we got a lock on the dstmap do pmap_copy
2311 * - if UVM_EXTRACT_REMOVE remove the entries
2312 * we make use of orig_entry and orig_fudge (saved in step 2)
2313 */
2314
2315 if (copy_ok || (flags & UVM_EXTRACT_REMOVE)) {
2316
2317 /* purge possible stale hints from srcmap */
2318 if (flags & UVM_EXTRACT_REMOVE) {
2319 SAVE_HINT(srcmap, srcmap->hint, orig_entry->prev);
2320 if (srcmap->first_free->start >= start)
2321 srcmap->first_free = orig_entry->prev;
2322 }
2323
2324 entry = orig_entry;
2325 fudge = orig_fudge;
2326 deadentry = NULL; /* for UVM_EXTRACT_REMOVE */
2327
2328 while (entry->start < end && entry != &srcmap->header) {
2329 if (copy_ok) {
2330 oldoffset = (entry->start + fudge) - start;
2331 elen = MIN(end, entry->end) -
2332 (entry->start + fudge);
2333 pmap_copy(dstmap->pmap, srcmap->pmap,
2334 dstaddr + oldoffset, elen,
2335 entry->start + fudge);
2336 }
2337
2338 /* we advance "entry" in the following if statement */
2339 if (flags & UVM_EXTRACT_REMOVE) {
2340 pmap_remove(srcmap->pmap, entry->start,
2341 entry->end);
2342 oldentry = entry; /* save entry */
2343 entry = entry->next; /* advance */
2344 uvm_map_entry_unlink(srcmap, oldentry);
2345 /* add to dead list */
2346 oldentry->next = deadentry;
2347 deadentry = oldentry;
2348 } else {
2349 entry = entry->next; /* advance */
2350 }
2351
2352 /* end of 'while' loop */
2353 fudge = 0;
2354 }
2355 pmap_update(srcmap->pmap);
2356
2357 /*
2358 * unlock dstmap. we will dispose of deadentry in
2359 * step 7 if needed
2360 */
2361
2362 if (copy_ok && srcmap != dstmap)
2363 vm_map_unlock(dstmap);
2364
2365 } else {
2366 deadentry = NULL;
2367 }
2368
2369 /*
2370 * step 7: we are done with the source map, unlock. if copy_ok
2371 * is 0 then we have not replaced the dummy mapping in dstmap yet
2372 * and we need to do so now.
2373 */
2374
2375 vm_map_unlock(srcmap);
2376 if ((flags & UVM_EXTRACT_REMOVE) && deadentry)
2377 uvm_unmap_detach(deadentry, 0); /* dispose of old entries */
2378
2379 /* now do the replacement if we didn't do it in step 5 */
2380 if (copy_ok == 0) {
2381 vm_map_lock(dstmap);
2382 error = uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
2383 nchain);
2384 vm_map_unlock(dstmap);
2385
2386 if (error == FALSE) {
2387 error = EIO;
2388 goto bad2;
2389 }
2390 }
2391
2392 uvm_tree_sanity(srcmap, "map_extract src leave");
2393 uvm_tree_sanity(dstmap, "map_extract dst leave");
2394
2395 return (0);
2396
2397 /*
2398 * bad: failure recovery
2399 */
2400 bad:
2401 vm_map_unlock(srcmap);
2402 bad2: /* src already unlocked */
2403 if (chain)
2404 uvm_unmap_detach(chain,
2405 (flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0);
2406
2407 uvm_tree_sanity(srcmap, "map_extract src err leave");
2408 uvm_tree_sanity(dstmap, "map_extract dst err leave");
2409
2410 uvm_unmap(dstmap, dstaddr, dstaddr+len); /* ??? */
2411 return (error);
2412 }
2413
2414 /* end of extraction functions */
2415
2416 /*
2417 * uvm_map_submap: punch down part of a map into a submap
2418 *
2419 * => only the kernel_map is allowed to be submapped
2420 * => the purpose of submapping is to break up the locking granularity
2421 * of a larger map
2422 * => the range specified must have been mapped previously with a uvm_map()
2423 * call [with uobj==NULL] to create a blank map entry in the main map.
2424 * [And it had better still be blank!]
2425 * => maps which contain submaps should never be copied or forked.
2426 * => to remove a submap, use uvm_unmap() on the main map
2427 * and then uvm_map_deallocate() the submap.
2428 * => main map must be unlocked.
2429 * => submap must have been init'd and have a zero reference count.
2430 * [need not be locked as we don't actually reference it]
2431 */
2432
2433 int
2434 uvm_map_submap(struct vm_map *map, vaddr_t start, vaddr_t end,
2435 struct vm_map *submap)
2436 {
2437 struct vm_map_entry *entry;
2438 int error;
2439
2440 vm_map_lock(map);
2441 VM_MAP_RANGE_CHECK(map, start, end);
2442
2443 if (uvm_map_lookup_entry(map, start, &entry)) {
2444 UVM_MAP_CLIP_START(map, entry, start);
2445 UVM_MAP_CLIP_END(map, entry, end); /* to be safe */
2446 } else {
2447 entry = NULL;
2448 }
2449
2450 if (entry != NULL &&
2451 entry->start == start && entry->end == end &&
2452 entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL &&
2453 !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) {
2454 entry->etype |= UVM_ET_SUBMAP;
2455 entry->object.sub_map = submap;
2456 entry->offset = 0;
2457 uvm_map_reference(submap);
2458 error = 0;
2459 } else {
2460 error = EINVAL;
2461 }
2462 vm_map_unlock(map);
2463 return error;
2464 }
2465
2466
2467 /*
2468 * uvm_map_protect: change map protection
2469 *
2470 * => set_max means set max_protection.
2471 * => map must be unlocked.
2472 */
2473
2474 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
2475 ~VM_PROT_WRITE : VM_PROT_ALL)
2476
2477 int
2478 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
2479 vm_prot_t new_prot, boolean_t set_max)
2480 {
2481 struct vm_map_entry *current, *entry;
2482 int error = 0;
2483 UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist);
2484 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_prot=0x%x)",
2485 map, start, end, new_prot);
2486
2487 vm_map_lock(map);
2488 VM_MAP_RANGE_CHECK(map, start, end);
2489 if (uvm_map_lookup_entry(map, start, &entry)) {
2490 UVM_MAP_CLIP_START(map, entry, start);
2491 } else {
2492 entry = entry->next;
2493 }
2494
2495 /*
2496 * make a first pass to check for protection violations.
2497 */
2498
2499 current = entry;
2500 while ((current != &map->header) && (current->start < end)) {
2501 if (UVM_ET_ISSUBMAP(current)) {
2502 error = EINVAL;
2503 goto out;
2504 }
2505 if ((new_prot & current->max_protection) != new_prot) {
2506 error = EACCES;
2507 goto out;
2508 }
2509 /*
2510 * Don't allow VM_PROT_EXECUTE to be set on entries that
2511 * point to vnodes that are associated with a NOEXEC file
2512 * system.
2513 */
2514 if (UVM_ET_ISOBJ(current) &&
2515 UVM_OBJ_IS_VNODE(current->object.uvm_obj)) {
2516 struct vnode *vp =
2517 (struct vnode *) current->object.uvm_obj;
2518
2519 if ((new_prot & VM_PROT_EXECUTE) != 0 &&
2520 (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
2521 error = EACCES;
2522 goto out;
2523 }
2524 }
2525 current = current->next;
2526 }
2527
2528 /* go back and fix up protections (no need to clip this time). */
2529
2530 current = entry;
2531 while ((current != &map->header) && (current->start < end)) {
2532 vm_prot_t old_prot;
2533
2534 UVM_MAP_CLIP_END(map, current, end);
2535 old_prot = current->protection;
2536 if (set_max)
2537 current->protection =
2538 (current->max_protection = new_prot) & old_prot;
2539 else
2540 current->protection = new_prot;
2541
2542 /*
2543 * update physical map if necessary. worry about copy-on-write
2544 * here -- CHECK THIS XXX
2545 */
2546
2547 if (current->protection != old_prot) {
2548 /* update pmap! */
2549 pmap_protect(map->pmap, current->start, current->end,
2550 current->protection & MASK(entry));
2551
2552 /*
2553 * If this entry points at a vnode, and the
2554 * protection includes VM_PROT_EXECUTE, mark
2555 * the vnode as VEXECMAP.
2556 */
2557 if (UVM_ET_ISOBJ(current)) {
2558 struct uvm_object *uobj =
2559 current->object.uvm_obj;
2560
2561 if (UVM_OBJ_IS_VNODE(uobj) &&
2562 (current->protection & VM_PROT_EXECUTE))
2563 vn_markexec((struct vnode *) uobj);
2564 }
2565 }
2566
2567 /*
2568 * If the map is configured to lock any future mappings,
2569 * wire this entry now if the old protection was VM_PROT_NONE
2570 * and the new protection is not VM_PROT_NONE.
2571 */
2572
2573 if ((map->flags & VM_MAP_WIREFUTURE) != 0 &&
2574 VM_MAPENT_ISWIRED(entry) == 0 &&
2575 old_prot == VM_PROT_NONE &&
2576 new_prot != VM_PROT_NONE) {
2577 if (uvm_map_pageable(map, entry->start,
2578 entry->end, FALSE,
2579 UVM_LK_ENTER|UVM_LK_EXIT) != 0) {
2580
2581 /*
2582 * If locking the entry fails, remember the
2583 * error if it's the first one. Note we
2584 * still continue setting the protection in
2585 * the map, but will return the error
2586 * condition regardless.
2587 *
2588 * XXX Ignore what the actual error is,
2589 * XXX just call it a resource shortage
2590 * XXX so that it doesn't get confused
2591 * XXX what uvm_map_protect() itself would
2592 * XXX normally return.
2593 */
2594
2595 error = ENOMEM;
2596 }
2597 }
2598 current = current->next;
2599 }
2600 pmap_update(map->pmap);
2601
2602 out:
2603 vm_map_unlock(map);
2604 UVMHIST_LOG(maphist, "<- done, error=%d",error,0,0,0);
2605 return error;
2606 }
2607
2608 #undef MASK
2609
2610 /*
2611 * uvm_map_inherit: set inheritance code for range of addrs in map.
2612 *
2613 * => map must be unlocked
2614 * => note that the inherit code is used during a "fork". see fork
2615 * code for details.
2616 */
2617
2618 int
2619 uvm_map_inherit(struct vm_map *map, vaddr_t start, vaddr_t end,
2620 vm_inherit_t new_inheritance)
2621 {
2622 struct vm_map_entry *entry, *temp_entry;
2623 UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist);
2624 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_inh=0x%x)",
2625 map, start, end, new_inheritance);
2626
2627 switch (new_inheritance) {
2628 case MAP_INHERIT_NONE:
2629 case MAP_INHERIT_COPY:
2630 case MAP_INHERIT_SHARE:
2631 break;
2632 default:
2633 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
2634 return EINVAL;
2635 }
2636
2637 vm_map_lock(map);
2638 VM_MAP_RANGE_CHECK(map, start, end);
2639 if (uvm_map_lookup_entry(map, start, &temp_entry)) {
2640 entry = temp_entry;
2641 UVM_MAP_CLIP_START(map, entry, start);
2642 } else {
2643 entry = temp_entry->next;
2644 }
2645 while ((entry != &map->header) && (entry->start < end)) {
2646 UVM_MAP_CLIP_END(map, entry, end);
2647 entry->inheritance = new_inheritance;
2648 entry = entry->next;
2649 }
2650 vm_map_unlock(map);
2651 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
2652 return 0;
2653 }
2654
2655 /*
2656 * uvm_map_advice: set advice code for range of addrs in map.
2657 *
2658 * => map must be unlocked
2659 */
2660
2661 int
2662 uvm_map_advice(struct vm_map *map, vaddr_t start, vaddr_t end, int new_advice)
2663 {
2664 struct vm_map_entry *entry, *temp_entry;
2665 UVMHIST_FUNC("uvm_map_advice"); UVMHIST_CALLED(maphist);
2666 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_adv=0x%x)",
2667 map, start, end, new_advice);
2668
2669 vm_map_lock(map);
2670 VM_MAP_RANGE_CHECK(map, start, end);
2671 if (uvm_map_lookup_entry(map, start, &temp_entry)) {
2672 entry = temp_entry;
2673 UVM_MAP_CLIP_START(map, entry, start);
2674 } else {
2675 entry = temp_entry->next;
2676 }
2677
2678 /*
2679 * XXXJRT: disallow holes?
2680 */
2681
2682 while ((entry != &map->header) && (entry->start < end)) {
2683 UVM_MAP_CLIP_END(map, entry, end);
2684
2685 switch (new_advice) {
2686 case MADV_NORMAL:
2687 case MADV_RANDOM:
2688 case MADV_SEQUENTIAL:
2689 /* nothing special here */
2690 break;
2691
2692 default:
2693 vm_map_unlock(map);
2694 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
2695 return EINVAL;
2696 }
2697 entry->advice = new_advice;
2698 entry = entry->next;
2699 }
2700
2701 vm_map_unlock(map);
2702 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
2703 return 0;
2704 }
2705
2706 /*
2707 * uvm_map_pageable: sets the pageability of a range in a map.
2708 *
2709 * => wires map entries. should not be used for transient page locking.
2710 * for that, use uvm_fault_wire()/uvm_fault_unwire() (see uvm_vslock()).
2711 * => regions sepcified as not pageable require lock-down (wired) memory
2712 * and page tables.
2713 * => map must never be read-locked
2714 * => if islocked is TRUE, map is already write-locked
2715 * => we always unlock the map, since we must downgrade to a read-lock
2716 * to call uvm_fault_wire()
2717 * => XXXCDC: check this and try and clean it up.
2718 */
2719
2720 int
2721 uvm_map_pageable(struct vm_map *map, vaddr_t start, vaddr_t end,
2722 boolean_t new_pageable, int lockflags)
2723 {
2724 struct vm_map_entry *entry, *start_entry, *failed_entry;
2725 int rv;
2726 #ifdef DIAGNOSTIC
2727 u_int timestamp_save;
2728 #endif
2729 UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist);
2730 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_pageable=0x%x)",
2731 map, start, end, new_pageable);
2732 KASSERT(map->flags & VM_MAP_PAGEABLE);
2733
2734 if ((lockflags & UVM_LK_ENTER) == 0)
2735 vm_map_lock(map);
2736 VM_MAP_RANGE_CHECK(map, start, end);
2737
2738 /*
2739 * only one pageability change may take place at one time, since
2740 * uvm_fault_wire assumes it will be called only once for each
2741 * wiring/unwiring. therefore, we have to make sure we're actually
2742 * changing the pageability for the entire region. we do so before
2743 * making any changes.
2744 */
2745
2746 if (uvm_map_lookup_entry(map, start, &start_entry) == FALSE) {
2747 if ((lockflags & UVM_LK_EXIT) == 0)
2748 vm_map_unlock(map);
2749
2750 UVMHIST_LOG(maphist,"<- done (fault)",0,0,0,0);
2751 return EFAULT;
2752 }
2753 entry = start_entry;
2754
2755 /*
2756 * handle wiring and unwiring separately.
2757 */
2758
2759 if (new_pageable) { /* unwire */
2760 UVM_MAP_CLIP_START(map, entry, start);
2761
2762 /*
2763 * unwiring. first ensure that the range to be unwired is
2764 * really wired down and that there are no holes.
2765 */
2766
2767 while ((entry != &map->header) && (entry->start < end)) {
2768 if (entry->wired_count == 0 ||
2769 (entry->end < end &&
2770 (entry->next == &map->header ||
2771 entry->next->start > entry->end))) {
2772 if ((lockflags & UVM_LK_EXIT) == 0)
2773 vm_map_unlock(map);
2774 UVMHIST_LOG(maphist, "<- done (INVAL)",0,0,0,0);
2775 return EINVAL;
2776 }
2777 entry = entry->next;
2778 }
2779
2780 /*
2781 * POSIX 1003.1b - a single munlock call unlocks a region,
2782 * regardless of the number of mlock calls made on that
2783 * region.
2784 */
2785
2786 entry = start_entry;
2787 while ((entry != &map->header) && (entry->start < end)) {
2788 UVM_MAP_CLIP_END(map, entry, end);
2789 if (VM_MAPENT_ISWIRED(entry))
2790 uvm_map_entry_unwire(map, entry);
2791 entry = entry->next;
2792 }
2793 if ((lockflags & UVM_LK_EXIT) == 0)
2794 vm_map_unlock(map);
2795 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
2796 return 0;
2797 }
2798
2799 /*
2800 * wire case: in two passes [XXXCDC: ugly block of code here]
2801 *
2802 * 1: holding the write lock, we create any anonymous maps that need
2803 * to be created. then we clip each map entry to the region to
2804 * be wired and increment its wiring count.
2805 *
2806 * 2: we downgrade to a read lock, and call uvm_fault_wire to fault
2807 * in the pages for any newly wired area (wired_count == 1).
2808 *
2809 * downgrading to a read lock for uvm_fault_wire avoids a possible
2810 * deadlock with another thread that may have faulted on one of
2811 * the pages to be wired (it would mark the page busy, blocking
2812 * us, then in turn block on the map lock that we hold). because
2813 * of problems in the recursive lock package, we cannot upgrade
2814 * to a write lock in vm_map_lookup. thus, any actions that
2815 * require the write lock must be done beforehand. because we
2816 * keep the read lock on the map, the copy-on-write status of the
2817 * entries we modify here cannot change.
2818 */
2819
2820 while ((entry != &map->header) && (entry->start < end)) {
2821 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
2822
2823 /*
2824 * perform actions of vm_map_lookup that need the
2825 * write lock on the map: create an anonymous map
2826 * for a copy-on-write region, or an anonymous map
2827 * for a zero-fill region. (XXXCDC: submap case
2828 * ok?)
2829 */
2830
2831 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */
2832 if (UVM_ET_ISNEEDSCOPY(entry) &&
2833 ((entry->max_protection & VM_PROT_WRITE) ||
2834 (entry->object.uvm_obj == NULL))) {
2835 amap_copy(map, entry, M_WAITOK, TRUE,
2836 start, end);
2837 /* XXXCDC: wait OK? */
2838 }
2839 }
2840 }
2841 UVM_MAP_CLIP_START(map, entry, start);
2842 UVM_MAP_CLIP_END(map, entry, end);
2843 entry->wired_count++;
2844
2845 /*
2846 * Check for holes
2847 */
2848
2849 if (entry->protection == VM_PROT_NONE ||
2850 (entry->end < end &&
2851 (entry->next == &map->header ||
2852 entry->next->start > entry->end))) {
2853
2854 /*
2855 * found one. amap creation actions do not need to
2856 * be undone, but the wired counts need to be restored.
2857 */
2858
2859 while (entry != &map->header && entry->end > start) {
2860 entry->wired_count--;
2861 entry = entry->prev;
2862 }
2863 if ((lockflags & UVM_LK_EXIT) == 0)
2864 vm_map_unlock(map);
2865 UVMHIST_LOG(maphist,"<- done (INVALID WIRE)",0,0,0,0);
2866 return EINVAL;
2867 }
2868 entry = entry->next;
2869 }
2870
2871 /*
2872 * Pass 2.
2873 */
2874
2875 #ifdef DIAGNOSTIC
2876 timestamp_save = map->timestamp;
2877 #endif
2878 vm_map_busy(map);
2879 vm_map_downgrade(map);
2880
2881 rv = 0;
2882 entry = start_entry;
2883 while (entry != &map->header && entry->start < end) {
2884 if (entry->wired_count == 1) {
2885 rv = uvm_fault_wire(map, entry->start, entry->end,
2886 VM_FAULT_WIREMAX, entry->max_protection);
2887 if (rv) {
2888
2889 /*
2890 * wiring failed. break out of the loop.
2891 * we'll clean up the map below, once we
2892 * have a write lock again.
2893 */
2894
2895 break;
2896 }
2897 }
2898 entry = entry->next;
2899 }
2900
2901 if (rv) { /* failed? */
2902
2903 /*
2904 * Get back to an exclusive (write) lock.
2905 */
2906
2907 vm_map_upgrade(map);
2908 vm_map_unbusy(map);
2909
2910 #ifdef DIAGNOSTIC
2911 if (timestamp_save != map->timestamp)
2912 panic("uvm_map_pageable: stale map");
2913 #endif
2914
2915 /*
2916 * first drop the wiring count on all the entries
2917 * which haven't actually been wired yet.
2918 */
2919
2920 failed_entry = entry;
2921 while (entry != &map->header && entry->start < end) {
2922 entry->wired_count--;
2923 entry = entry->next;
2924 }
2925
2926 /*
2927 * now, unwire all the entries that were successfully
2928 * wired above.
2929 */
2930
2931 entry = start_entry;
2932 while (entry != failed_entry) {
2933 entry->wired_count--;
2934 if (VM_MAPENT_ISWIRED(entry) == 0)
2935 uvm_map_entry_unwire(map, entry);
2936 entry = entry->next;
2937 }
2938 if ((lockflags & UVM_LK_EXIT) == 0)
2939 vm_map_unlock(map);
2940 UVMHIST_LOG(maphist, "<- done (RV=%d)", rv,0,0,0);
2941 return (rv);
2942 }
2943
2944 /* We are holding a read lock here. */
2945 if ((lockflags & UVM_LK_EXIT) == 0) {
2946 vm_map_unbusy(map);
2947 vm_map_unlock_read(map);
2948 } else {
2949
2950 /*
2951 * Get back to an exclusive (write) lock.
2952 */
2953
2954 vm_map_upgrade(map);
2955 vm_map_unbusy(map);
2956 }
2957
2958 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
2959 return 0;
2960 }
2961
2962 /*
2963 * uvm_map_pageable_all: special case of uvm_map_pageable - affects
2964 * all mapped regions.
2965 *
2966 * => map must not be locked.
2967 * => if no flags are specified, all regions are unwired.
2968 * => XXXJRT: has some of the same problems as uvm_map_pageable() above.
2969 */
2970
2971 int
2972 uvm_map_pageable_all(struct vm_map *map, int flags, vsize_t limit)
2973 {
2974 struct vm_map_entry *entry, *failed_entry;
2975 vsize_t size;
2976 int rv;
2977 #ifdef DIAGNOSTIC
2978 u_int timestamp_save;
2979 #endif
2980 UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist);
2981 UVMHIST_LOG(maphist,"(map=0x%x,flags=0x%x)", map, flags, 0, 0);
2982
2983 KASSERT(map->flags & VM_MAP_PAGEABLE);
2984
2985 vm_map_lock(map);
2986
2987 /*
2988 * handle wiring and unwiring separately.
2989 */
2990
2991 if (flags == 0) { /* unwire */
2992
2993 /*
2994 * POSIX 1003.1b -- munlockall unlocks all regions,
2995 * regardless of how many times mlockall has been called.
2996 */
2997
2998 for (entry = map->header.next; entry != &map->header;
2999 entry = entry->next) {
3000 if (VM_MAPENT_ISWIRED(entry))
3001 uvm_map_entry_unwire(map, entry);
3002 }
3003 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);
3004 vm_map_unlock(map);
3005 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
3006 return 0;
3007 }
3008
3009 if (flags & MCL_FUTURE) {
3010
3011 /*
3012 * must wire all future mappings; remember this.
3013 */
3014
3015 vm_map_modflags(map, VM_MAP_WIREFUTURE, 0);
3016 }
3017
3018 if ((flags & MCL_CURRENT) == 0) {
3019
3020 /*
3021 * no more work to do!
3022 */
3023
3024 UVMHIST_LOG(maphist,"<- done (OK no wire)",0,0,0,0);
3025 vm_map_unlock(map);
3026 return 0;
3027 }
3028
3029 /*
3030 * wire case: in three passes [XXXCDC: ugly block of code here]
3031 *
3032 * 1: holding the write lock, count all pages mapped by non-wired
3033 * entries. if this would cause us to go over our limit, we fail.
3034 *
3035 * 2: still holding the write lock, we create any anonymous maps that
3036 * need to be created. then we increment its wiring count.
3037 *
3038 * 3: we downgrade to a read lock, and call uvm_fault_wire to fault
3039 * in the pages for any newly wired area (wired_count == 1).
3040 *
3041 * downgrading to a read lock for uvm_fault_wire avoids a possible
3042 * deadlock with another thread that may have faulted on one of
3043 * the pages to be wired (it would mark the page busy, blocking
3044 * us, then in turn block on the map lock that we hold). because
3045 * of problems in the recursive lock package, we cannot upgrade
3046 * to a write lock in vm_map_lookup. thus, any actions that
3047 * require the write lock must be done beforehand. because we
3048 * keep the read lock on the map, the copy-on-write status of the
3049 * entries we modify here cannot change.
3050 */
3051
3052 for (size = 0, entry = map->header.next; entry != &map->header;
3053 entry = entry->next) {
3054 if (entry->protection != VM_PROT_NONE &&
3055 VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3056 size += entry->end - entry->start;
3057 }
3058 }
3059
3060 if (atop(size) + uvmexp.wired > uvmexp.wiredmax) {
3061 vm_map_unlock(map);
3062 return ENOMEM;
3063 }
3064
3065 /* XXX non-pmap_wired_count case must be handled by caller */
3066 #ifdef pmap_wired_count
3067 if (limit != 0 &&
3068 (size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit)) {
3069 vm_map_unlock(map);
3070 return ENOMEM;
3071 }
3072 #endif
3073
3074 /*
3075 * Pass 2.
3076 */
3077
3078 for (entry = map->header.next; entry != &map->header;
3079 entry = entry->next) {
3080 if (entry->protection == VM_PROT_NONE)
3081 continue;
3082 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3083
3084 /*
3085 * perform actions of vm_map_lookup that need the
3086 * write lock on the map: create an anonymous map
3087 * for a copy-on-write region, or an anonymous map
3088 * for a zero-fill region. (XXXCDC: submap case
3089 * ok?)
3090 */
3091
3092 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */
3093 if (UVM_ET_ISNEEDSCOPY(entry) &&
3094 ((entry->max_protection & VM_PROT_WRITE) ||
3095 (entry->object.uvm_obj == NULL))) {
3096 amap_copy(map, entry, M_WAITOK, TRUE,
3097 entry->start, entry->end);
3098 /* XXXCDC: wait OK? */
3099 }
3100 }
3101 }
3102 entry->wired_count++;
3103 }
3104
3105 /*
3106 * Pass 3.
3107 */
3108
3109 #ifdef DIAGNOSTIC
3110 timestamp_save = map->timestamp;
3111 #endif
3112 vm_map_busy(map);
3113 vm_map_downgrade(map);
3114
3115 rv = 0;
3116 for (entry = map->header.next; entry != &map->header;
3117 entry = entry->next) {
3118 if (entry->wired_count == 1) {
3119 rv = uvm_fault_wire(map, entry->start, entry->end,
3120 VM_FAULT_WIREMAX, entry->max_protection);
3121 if (rv) {
3122
3123 /*
3124 * wiring failed. break out of the loop.
3125 * we'll clean up the map below, once we
3126 * have a write lock again.
3127 */
3128
3129 break;
3130 }
3131 }
3132 }
3133
3134 if (rv) {
3135
3136 /*
3137 * Get back an exclusive (write) lock.
3138 */
3139
3140 vm_map_upgrade(map);
3141 vm_map_unbusy(map);
3142
3143 #ifdef DIAGNOSTIC
3144 if (timestamp_save != map->timestamp)
3145 panic("uvm_map_pageable_all: stale map");
3146 #endif
3147
3148 /*
3149 * first drop the wiring count on all the entries
3150 * which haven't actually been wired yet.
3151 *
3152 * Skip VM_PROT_NONE entries like we did above.
3153 */
3154
3155 failed_entry = entry;
3156 for (/* nothing */; entry != &map->header;
3157 entry = entry->next) {
3158 if (entry->protection == VM_PROT_NONE)
3159 continue;
3160 entry->wired_count--;
3161 }
3162
3163 /*
3164 * now, unwire all the entries that were successfully
3165 * wired above.
3166 *
3167 * Skip VM_PROT_NONE entries like we did above.
3168 */
3169
3170 for (entry = map->header.next; entry != failed_entry;
3171 entry = entry->next) {
3172 if (entry->protection == VM_PROT_NONE)
3173 continue;
3174 entry->wired_count--;
3175 if (VM_MAPENT_ISWIRED(entry))
3176 uvm_map_entry_unwire(map, entry);
3177 }
3178 vm_map_unlock(map);
3179 UVMHIST_LOG(maphist,"<- done (RV=%d)", rv,0,0,0);
3180 return (rv);
3181 }
3182
3183 /* We are holding a read lock here. */
3184 vm_map_unbusy(map);
3185 vm_map_unlock_read(map);
3186
3187 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
3188 return 0;
3189 }
3190
3191 /*
3192 * uvm_map_clean: clean out a map range
3193 *
3194 * => valid flags:
3195 * if (flags & PGO_CLEANIT): dirty pages are cleaned first
3196 * if (flags & PGO_SYNCIO): dirty pages are written synchronously
3197 * if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean
3198 * if (flags & PGO_FREE): any cached pages are freed after clean
3199 * => returns an error if any part of the specified range isn't mapped
3200 * => never a need to flush amap layer since the anonymous memory has
3201 * no permanent home, but may deactivate pages there
3202 * => called from sys_msync() and sys_madvise()
3203 * => caller must not write-lock map (read OK).
3204 * => we may sleep while cleaning if SYNCIO [with map read-locked]
3205 */
3206
3207 int
3208 uvm_map_clean(struct vm_map *map, vaddr_t start, vaddr_t end, int flags)
3209 {
3210 struct vm_map_entry *current, *entry;
3211 struct uvm_object *uobj;
3212 struct vm_amap *amap;
3213 struct vm_anon *anon;
3214 struct vm_page *pg;
3215 vaddr_t offset;
3216 vsize_t size;
3217 int error, refs;
3218 UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist);
3219
3220 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,flags=0x%x)",
3221 map, start, end, flags);
3222 KASSERT((flags & (PGO_FREE|PGO_DEACTIVATE)) !=
3223 (PGO_FREE|PGO_DEACTIVATE));
3224
3225 vm_map_lock_read(map);
3226 VM_MAP_RANGE_CHECK(map, start, end);
3227 if (uvm_map_lookup_entry(map, start, &entry) == FALSE) {
3228 vm_map_unlock_read(map);
3229 return EFAULT;
3230 }
3231
3232 /*
3233 * Make a first pass to check for holes.
3234 */
3235
3236 for (current = entry; current->start < end; current = current->next) {
3237 if (UVM_ET_ISSUBMAP(current)) {
3238 vm_map_unlock_read(map);
3239 return EINVAL;
3240 }
3241 if (end <= current->end) {
3242 break;
3243 }
3244 if (current->end != current->next->start) {
3245 vm_map_unlock_read(map);
3246 return EFAULT;
3247 }
3248 }
3249
3250 error = 0;
3251 for (current = entry; start < end; current = current->next) {
3252 amap = current->aref.ar_amap; /* top layer */
3253 uobj = current->object.uvm_obj; /* bottom layer */
3254 KASSERT(start >= current->start);
3255
3256 /*
3257 * No amap cleaning necessary if:
3258 *
3259 * (1) There's no amap.
3260 *
3261 * (2) We're not deactivating or freeing pages.
3262 */
3263
3264 if (amap == NULL || (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0)
3265 goto flush_object;
3266
3267 amap_lock(amap);
3268 offset = start - current->start;
3269 size = MIN(end, current->end) - start;
3270 for ( ; size != 0; size -= PAGE_SIZE, offset += PAGE_SIZE) {
3271 anon = amap_lookup(¤t->aref, offset);
3272 if (anon == NULL)
3273 continue;
3274
3275 simple_lock(&anon->an_lock);
3276 pg = anon->u.an_page;
3277 if (pg == NULL) {
3278 simple_unlock(&anon->an_lock);
3279 continue;
3280 }
3281
3282 switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) {
3283
3284 /*
3285 * In these first 3 cases, we just deactivate the page.
3286 */
3287
3288 case PGO_CLEANIT|PGO_FREE:
3289 case PGO_CLEANIT|PGO_DEACTIVATE:
3290 case PGO_DEACTIVATE:
3291 deactivate_it:
3292 /*
3293 * skip the page if it's loaned or wired,
3294 * since it shouldn't be on a paging queue
3295 * at all in these cases.
3296 */
3297
3298 uvm_lock_pageq();
3299 if (pg->loan_count != 0 ||
3300 pg->wire_count != 0) {
3301 uvm_unlock_pageq();
3302 simple_unlock(&anon->an_lock);
3303 continue;
3304 }
3305 KASSERT(pg->uanon == anon);
3306 pmap_clear_reference(pg);
3307 uvm_pagedeactivate(pg);
3308 uvm_unlock_pageq();
3309 simple_unlock(&anon->an_lock);
3310 continue;
3311
3312 case PGO_FREE:
3313
3314 /*
3315 * If there are multiple references to
3316 * the amap, just deactivate the page.
3317 */
3318
3319 if (amap_refs(amap) > 1)
3320 goto deactivate_it;
3321
3322 /* skip the page if it's wired */
3323 if (pg->wire_count != 0) {
3324 simple_unlock(&anon->an_lock);
3325 continue;
3326 }
3327 amap_unadd(¤t->aref, offset);
3328 refs = --anon->an_ref;
3329 simple_unlock(&anon->an_lock);
3330 if (refs == 0)
3331 uvm_anfree(anon);
3332 continue;
3333 }
3334 }
3335 amap_unlock(amap);
3336
3337 flush_object:
3338 /*
3339 * flush pages if we've got a valid backing object.
3340 * note that we must always clean object pages before
3341 * freeing them since otherwise we could reveal stale
3342 * data from files.
3343 */
3344
3345 offset = current->offset + (start - current->start);
3346 size = MIN(end, current->end) - start;
3347 if (uobj != NULL) {
3348 simple_lock(&uobj->vmobjlock);
3349 if (uobj->pgops->pgo_put != NULL)
3350 error = (uobj->pgops->pgo_put)(uobj, offset,
3351 offset + size, flags | PGO_CLEANIT);
3352 else
3353 error = 0;
3354 }
3355 start += size;
3356 }
3357 vm_map_unlock_read(map);
3358 return (error);
3359 }
3360
3361
3362 /*
3363 * uvm_map_checkprot: check protection in map
3364 *
3365 * => must allow specified protection in a fully allocated region.
3366 * => map must be read or write locked by caller.
3367 */
3368
3369 boolean_t
3370 uvm_map_checkprot(struct vm_map *map, vaddr_t start, vaddr_t end,
3371 vm_prot_t protection)
3372 {
3373 struct vm_map_entry *entry;
3374 struct vm_map_entry *tmp_entry;
3375
3376 if (!uvm_map_lookup_entry(map, start, &tmp_entry)) {
3377 return (FALSE);
3378 }
3379 entry = tmp_entry;
3380 while (start < end) {
3381 if (entry == &map->header) {
3382 return (FALSE);
3383 }
3384
3385 /*
3386 * no holes allowed
3387 */
3388
3389 if (start < entry->start) {
3390 return (FALSE);
3391 }
3392
3393 /*
3394 * check protection associated with entry
3395 */
3396
3397 if ((entry->protection & protection) != protection) {
3398 return (FALSE);
3399 }
3400 start = entry->end;
3401 entry = entry->next;
3402 }
3403 return (TRUE);
3404 }
3405
3406 /*
3407 * uvmspace_alloc: allocate a vmspace structure.
3408 *
3409 * - structure includes vm_map and pmap
3410 * - XXX: no locking on this structure
3411 * - refcnt set to 1, rest must be init'd by caller
3412 */
3413 struct vmspace *
3414 uvmspace_alloc(vaddr_t min, vaddr_t max)
3415 {
3416 struct vmspace *vm;
3417 UVMHIST_FUNC("uvmspace_alloc"); UVMHIST_CALLED(maphist);
3418
3419 vm = pool_get(&uvm_vmspace_pool, PR_WAITOK);
3420 uvmspace_init(vm, NULL, min, max);
3421 UVMHIST_LOG(maphist,"<- done (vm=0x%x)", vm,0,0,0);
3422 return (vm);
3423 }
3424
3425 /*
3426 * uvmspace_init: initialize a vmspace structure.
3427 *
3428 * - XXX: no locking on this structure
3429 * - refcnt set to 1, rest must be init'd by caller
3430 */
3431 void
3432 uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t min, vaddr_t max)
3433 {
3434 UVMHIST_FUNC("uvmspace_init"); UVMHIST_CALLED(maphist);
3435
3436 memset(vm, 0, sizeof(*vm));
3437 uvm_map_setup(&vm->vm_map, min, max, VM_MAP_PAGEABLE
3438 #ifdef __USING_TOPDOWN_VM
3439 | VM_MAP_TOPDOWN
3440 #endif
3441 );
3442 if (pmap)
3443 pmap_reference(pmap);
3444 else
3445 pmap = pmap_create();
3446 vm->vm_map.pmap = pmap;
3447 vm->vm_refcnt = 1;
3448 UVMHIST_LOG(maphist,"<- done",0,0,0,0);
3449 }
3450
3451 /*
3452 * uvmspace_share: share a vmspace between two proceses
3453 *
3454 * - XXX: no locking on vmspace
3455 * - used for vfork, threads(?)
3456 */
3457
3458 void
3459 uvmspace_share(struct proc *p1, struct proc *p2)
3460 {
3461
3462 p2->p_vmspace = p1->p_vmspace;
3463 p1->p_vmspace->vm_refcnt++;
3464 }
3465
3466 /*
3467 * uvmspace_unshare: ensure that process "p" has its own, unshared, vmspace
3468 *
3469 * - XXX: no locking on vmspace
3470 */
3471
3472 void
3473 uvmspace_unshare(struct lwp *l)
3474 {
3475 struct proc *p = l->l_proc;
3476 struct vmspace *nvm, *ovm = p->p_vmspace;
3477
3478 if (ovm->vm_refcnt == 1)
3479 /* nothing to do: vmspace isn't shared in the first place */
3480 return;
3481
3482 /* make a new vmspace, still holding old one */
3483 nvm = uvmspace_fork(ovm);
3484
3485 pmap_deactivate(l); /* unbind old vmspace */
3486 p->p_vmspace = nvm;
3487 pmap_activate(l); /* switch to new vmspace */
3488
3489 uvmspace_free(ovm); /* drop reference to old vmspace */
3490 }
3491
3492 /*
3493 * uvmspace_exec: the process wants to exec a new program
3494 *
3495 * - XXX: no locking on vmspace
3496 */
3497
3498 void
3499 uvmspace_exec(struct lwp *l, vaddr_t start, vaddr_t end)
3500 {
3501 struct proc *p = l->l_proc;
3502 struct vmspace *nvm, *ovm = p->p_vmspace;
3503 struct vm_map *map = &ovm->vm_map;
3504
3505 #ifdef __sparc__
3506 /* XXX cgd 960926: the sparc #ifdef should be a MD hook */
3507 kill_user_windows(l); /* before stack addresses go away */
3508 #endif
3509
3510 /*
3511 * see if more than one process is using this vmspace...
3512 */
3513
3514 if (ovm->vm_refcnt == 1) {
3515
3516 /*
3517 * if p is the only process using its vmspace then we can safely
3518 * recycle that vmspace for the program that is being exec'd.
3519 */
3520
3521 #ifdef SYSVSHM
3522 /*
3523 * SYSV SHM semantics require us to kill all segments on an exec
3524 */
3525
3526 if (ovm->vm_shm)
3527 shmexit(ovm);
3528 #endif
3529
3530 /*
3531 * POSIX 1003.1b -- "lock future mappings" is revoked
3532 * when a process execs another program image.
3533 */
3534
3535 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);
3536
3537 /*
3538 * now unmap the old program
3539 */
3540
3541 pmap_remove_all(map->pmap);
3542 uvm_unmap(map, map->min_offset, map->max_offset);
3543 KASSERT(map->header.prev == &map->header);
3544 KASSERT(map->nentries == 0);
3545
3546 /*
3547 * resize the map
3548 */
3549
3550 map->min_offset = start;
3551 map->max_offset = end;
3552 } else {
3553
3554 /*
3555 * p's vmspace is being shared, so we can't reuse it for p since
3556 * it is still being used for others. allocate a new vmspace
3557 * for p
3558 */
3559
3560 nvm = uvmspace_alloc(start, end);
3561
3562 /*
3563 * install new vmspace and drop our ref to the old one.
3564 */
3565
3566 pmap_deactivate(l);
3567 p->p_vmspace = nvm;
3568 pmap_activate(l);
3569
3570 uvmspace_free(ovm);
3571 }
3572 }
3573
3574 /*
3575 * uvmspace_free: free a vmspace data structure
3576 *
3577 * - XXX: no locking on vmspace
3578 */
3579
3580 void
3581 uvmspace_free(struct vmspace *vm)
3582 {
3583 struct vm_map_entry *dead_entries;
3584 struct vm_map *map;
3585 UVMHIST_FUNC("uvmspace_free"); UVMHIST_CALLED(maphist);
3586
3587 UVMHIST_LOG(maphist,"(vm=0x%x) ref=%d", vm, vm->vm_refcnt,0,0);
3588 if (--vm->vm_refcnt > 0) {
3589 return;
3590 }
3591
3592 /*
3593 * at this point, there should be no other references to the map.
3594 * delete all of the mappings, then destroy the pmap.
3595 */
3596
3597 map = &vm->vm_map;
3598 map->flags |= VM_MAP_DYING;
3599 pmap_remove_all(map->pmap);
3600 #ifdef SYSVSHM
3601 /* Get rid of any SYSV shared memory segments. */
3602 if (vm->vm_shm != NULL)
3603 shmexit(vm);
3604 #endif
3605 if (map->nentries) {
3606 uvm_unmap_remove(map, map->min_offset, map->max_offset,
3607 &dead_entries);
3608 if (dead_entries != NULL)
3609 uvm_unmap_detach(dead_entries, 0);
3610 }
3611 KASSERT(map->nentries == 0);
3612 KASSERT(map->size == 0);
3613 pmap_destroy(map->pmap);
3614 pool_put(&uvm_vmspace_pool, vm);
3615 }
3616
3617 /*
3618 * F O R K - m a i n e n t r y p o i n t
3619 */
3620 /*
3621 * uvmspace_fork: fork a process' main map
3622 *
3623 * => create a new vmspace for child process from parent.
3624 * => parent's map must not be locked.
3625 */
3626
3627 struct vmspace *
3628 uvmspace_fork(struct vmspace *vm1)
3629 {
3630 struct vmspace *vm2;
3631 struct vm_map *old_map = &vm1->vm_map;
3632 struct vm_map *new_map;
3633 struct vm_map_entry *old_entry;
3634 struct vm_map_entry *new_entry;
3635 UVMHIST_FUNC("uvmspace_fork"); UVMHIST_CALLED(maphist);
3636
3637 vm_map_lock(old_map);
3638
3639 vm2 = uvmspace_alloc(old_map->min_offset, old_map->max_offset);
3640 memcpy(&vm2->vm_startcopy, &vm1->vm_startcopy,
3641 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
3642 new_map = &vm2->vm_map; /* XXX */
3643
3644 old_entry = old_map->header.next;
3645 new_map->size = old_map->size;
3646
3647 /*
3648 * go entry-by-entry
3649 */
3650
3651 while (old_entry != &old_map->header) {
3652
3653 /*
3654 * first, some sanity checks on the old entry
3655 */
3656
3657 KASSERT(!UVM_ET_ISSUBMAP(old_entry));
3658 KASSERT(UVM_ET_ISCOPYONWRITE(old_entry) ||
3659 !UVM_ET_ISNEEDSCOPY(old_entry));
3660
3661 switch (old_entry->inheritance) {
3662 case MAP_INHERIT_NONE:
3663
3664 /*
3665 * drop the mapping, modify size
3666 */
3667 new_map->size -= old_entry->end - old_entry->start;
3668 break;
3669
3670 case MAP_INHERIT_SHARE:
3671
3672 /*
3673 * share the mapping: this means we want the old and
3674 * new entries to share amaps and backing objects.
3675 */
3676 /*
3677 * if the old_entry needs a new amap (due to prev fork)
3678 * then we need to allocate it now so that we have
3679 * something we own to share with the new_entry. [in
3680 * other words, we need to clear needs_copy]
3681 */
3682
3683 if (UVM_ET_ISNEEDSCOPY(old_entry)) {
3684 /* get our own amap, clears needs_copy */
3685 amap_copy(old_map, old_entry, M_WAITOK, FALSE,
3686 0, 0);
3687 /* XXXCDC: WAITOK??? */
3688 }
3689
3690 new_entry = uvm_mapent_alloc(new_map, 0);
3691 /* old_entry -> new_entry */
3692 uvm_mapent_copy(old_entry, new_entry);
3693
3694 /* new pmap has nothing wired in it */
3695 new_entry->wired_count = 0;
3696
3697 /*
3698 * gain reference to object backing the map (can't
3699 * be a submap, already checked this case).
3700 */
3701
3702 if (new_entry->aref.ar_amap)
3703 uvm_map_reference_amap(new_entry, AMAP_SHARED);
3704
3705 if (new_entry->object.uvm_obj &&
3706 new_entry->object.uvm_obj->pgops->pgo_reference)
3707 new_entry->object.uvm_obj->
3708 pgops->pgo_reference(
3709 new_entry->object.uvm_obj);
3710
3711 /* insert entry at end of new_map's entry list */
3712 uvm_map_entry_link(new_map, new_map->header.prev,
3713 new_entry);
3714
3715 break;
3716
3717 case MAP_INHERIT_COPY:
3718
3719 /*
3720 * copy-on-write the mapping (using mmap's
3721 * MAP_PRIVATE semantics)
3722 *
3723 * allocate new_entry, adjust reference counts.
3724 * (note that new references are read-only).
3725 */
3726
3727 new_entry = uvm_mapent_alloc(new_map, 0);
3728 /* old_entry -> new_entry */
3729 uvm_mapent_copy(old_entry, new_entry);
3730
3731 if (new_entry->aref.ar_amap)
3732 uvm_map_reference_amap(new_entry, 0);
3733
3734 if (new_entry->object.uvm_obj &&
3735 new_entry->object.uvm_obj->pgops->pgo_reference)
3736 new_entry->object.uvm_obj->pgops->pgo_reference
3737 (new_entry->object.uvm_obj);
3738
3739 /* new pmap has nothing wired in it */
3740 new_entry->wired_count = 0;
3741
3742 new_entry->etype |=
3743 (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY);
3744 uvm_map_entry_link(new_map, new_map->header.prev,
3745 new_entry);
3746
3747 /*
3748 * the new entry will need an amap. it will either
3749 * need to be copied from the old entry or created
3750 * from scratch (if the old entry does not have an
3751 * amap). can we defer this process until later
3752 * (by setting "needs_copy") or do we need to copy
3753 * the amap now?
3754 *
3755 * we must copy the amap now if any of the following
3756 * conditions hold:
3757 * 1. the old entry has an amap and that amap is
3758 * being shared. this means that the old (parent)
3759 * process is sharing the amap with another
3760 * process. if we do not clear needs_copy here
3761 * we will end up in a situation where both the
3762 * parent and child process are refering to the
3763 * same amap with "needs_copy" set. if the
3764 * parent write-faults, the fault routine will
3765 * clear "needs_copy" in the parent by allocating
3766 * a new amap. this is wrong because the
3767 * parent is supposed to be sharing the old amap
3768 * and the new amap will break that.
3769 *
3770 * 2. if the old entry has an amap and a non-zero
3771 * wire count then we are going to have to call
3772 * amap_cow_now to avoid page faults in the
3773 * parent process. since amap_cow_now requires
3774 * "needs_copy" to be clear we might as well
3775 * clear it here as well.
3776 *
3777 */
3778
3779 if (old_entry->aref.ar_amap != NULL) {
3780 if ((amap_flags(old_entry->aref.ar_amap) &
3781 AMAP_SHARED) != 0 ||
3782 VM_MAPENT_ISWIRED(old_entry)) {
3783
3784 amap_copy(new_map, new_entry, M_WAITOK,
3785 FALSE, 0, 0);
3786 /* XXXCDC: M_WAITOK ... ok? */
3787 }
3788 }
3789
3790 /*
3791 * if the parent's entry is wired down, then the
3792 * parent process does not want page faults on
3793 * access to that memory. this means that we
3794 * cannot do copy-on-write because we can't write
3795 * protect the old entry. in this case we
3796 * resolve all copy-on-write faults now, using
3797 * amap_cow_now. note that we have already
3798 * allocated any needed amap (above).
3799 */
3800
3801 if (VM_MAPENT_ISWIRED(old_entry)) {
3802
3803 /*
3804 * resolve all copy-on-write faults now
3805 * (note that there is nothing to do if
3806 * the old mapping does not have an amap).
3807 */
3808 if (old_entry->aref.ar_amap)
3809 amap_cow_now(new_map, new_entry);
3810
3811 } else {
3812
3813 /*
3814 * setup mappings to trigger copy-on-write faults
3815 * we must write-protect the parent if it has
3816 * an amap and it is not already "needs_copy"...
3817 * if it is already "needs_copy" then the parent
3818 * has already been write-protected by a previous
3819 * fork operation.
3820 */
3821
3822 if (old_entry->aref.ar_amap &&
3823 !UVM_ET_ISNEEDSCOPY(old_entry)) {
3824 if (old_entry->max_protection & VM_PROT_WRITE) {
3825 pmap_protect(old_map->pmap,
3826 old_entry->start,
3827 old_entry->end,
3828 old_entry->protection &
3829 ~VM_PROT_WRITE);
3830 pmap_update(old_map->pmap);
3831 }
3832 old_entry->etype |= UVM_ET_NEEDSCOPY;
3833 }
3834 }
3835 break;
3836 } /* end of switch statement */
3837 old_entry = old_entry->next;
3838 }
3839
3840 vm_map_unlock(old_map);
3841
3842 #ifdef SYSVSHM
3843 if (vm1->vm_shm)
3844 shmfork(vm1, vm2);
3845 #endif
3846
3847 #ifdef PMAP_FORK
3848 pmap_fork(vm1->vm_map.pmap, vm2->vm_map.pmap);
3849 #endif
3850
3851 UVMHIST_LOG(maphist,"<- done",0,0,0,0);
3852 return (vm2);
3853 }
3854
3855
3856 #if defined(DDB)
3857
3858 /*
3859 * DDB hooks
3860 */
3861
3862 /*
3863 * uvm_map_printit: actually prints the map
3864 */
3865
3866 void
3867 uvm_map_printit(struct vm_map *map, boolean_t full,
3868 void (*pr)(const char *, ...))
3869 {
3870 struct vm_map_entry *entry;
3871
3872 (*pr)("MAP %p: [0x%lx->0x%lx]\n", map, map->min_offset,map->max_offset);
3873 (*pr)("\t#ent=%d, sz=%d, ref=%d, version=%d, flags=0x%x\n",
3874 map->nentries, map->size, map->ref_count, map->timestamp,
3875 map->flags);
3876 (*pr)("\tpmap=%p(resident=%d)\n", map->pmap,
3877 pmap_resident_count(map->pmap));
3878 if (!full)
3879 return;
3880 for (entry = map->header.next; entry != &map->header;
3881 entry = entry->next) {
3882 (*pr)(" - %p: 0x%lx->0x%lx: obj=%p/0x%llx, amap=%p/%d\n",
3883 entry, entry->start, entry->end, entry->object.uvm_obj,
3884 (long long)entry->offset, entry->aref.ar_amap,
3885 entry->aref.ar_pageoff);
3886 (*pr)(
3887 "\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, "
3888 "wc=%d, adv=%d\n",
3889 (entry->etype & UVM_ET_SUBMAP) ? 'T' : 'F',
3890 (entry->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F',
3891 (entry->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F',
3892 entry->protection, entry->max_protection,
3893 entry->inheritance, entry->wired_count, entry->advice);
3894 }
3895 }
3896
3897 /*
3898 * uvm_object_printit: actually prints the object
3899 */
3900
3901 void
3902 uvm_object_printit(struct uvm_object *uobj, boolean_t full,
3903 void (*pr)(const char *, ...))
3904 {
3905 struct vm_page *pg;
3906 int cnt = 0;
3907
3908 (*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ",
3909 uobj, uobj->vmobjlock.lock_data, uobj->pgops, uobj->uo_npages);
3910 if (UVM_OBJ_IS_KERN_OBJECT(uobj))
3911 (*pr)("refs=<SYSTEM>\n");
3912 else
3913 (*pr)("refs=%d\n", uobj->uo_refs);
3914
3915 if (!full) {
3916 return;
3917 }
3918 (*pr)(" PAGES <pg,offset>:\n ");
3919 TAILQ_FOREACH(pg, &uobj->memq, listq) {
3920 cnt++;
3921 (*pr)("<%p,0x%llx> ", pg, (long long)pg->offset);
3922 if ((cnt % 3) == 0) {
3923 (*pr)("\n ");
3924 }
3925 }
3926 if ((cnt % 3) != 0) {
3927 (*pr)("\n");
3928 }
3929 }
3930
3931 /*
3932 * uvm_page_printit: actually print the page
3933 */
3934
3935 static const char page_flagbits[] =
3936 "\2\1BUSY\2WANTED\3TABLED\4CLEAN\5PAGEOUT\6RELEASED\7FAKE\10RDONLY"
3937 "\11ZERO\15PAGER1";
3938 static const char page_pqflagbits[] =
3939 "\2\1FREE\2INACTIVE\3ACTIVE\5ANON\6AOBJ";
3940
3941 void
3942 uvm_page_printit(struct vm_page *pg, boolean_t full,
3943 void (*pr)(const char *, ...))
3944 {
3945 struct vm_page *tpg;
3946 struct uvm_object *uobj;
3947 struct pglist *pgl;
3948 char pgbuf[128];
3949 char pqbuf[128];
3950
3951 (*pr)("PAGE %p:\n", pg);
3952 bitmask_snprintf(pg->flags, page_flagbits, pgbuf, sizeof(pgbuf));
3953 bitmask_snprintf(pg->pqflags, page_pqflagbits, pqbuf, sizeof(pqbuf));
3954 (*pr)(" flags=%s, pqflags=%s, wire_count=%d, pa=0x%lx\n",
3955 pgbuf, pqbuf, pg->wire_count, (long)VM_PAGE_TO_PHYS(pg));
3956 (*pr)(" uobject=%p, uanon=%p, offset=0x%llx loan_count=%d\n",
3957 pg->uobject, pg->uanon, (long long)pg->offset, pg->loan_count);
3958 #if defined(UVM_PAGE_TRKOWN)
3959 if (pg->flags & PG_BUSY)
3960 (*pr)(" owning process = %d, tag=%s\n",
3961 pg->owner, pg->owner_tag);
3962 else
3963 (*pr)(" page not busy, no owner\n");
3964 #else
3965 (*pr)(" [page ownership tracking disabled]\n");
3966 #endif
3967
3968 if (!full)
3969 return;
3970
3971 /* cross-verify object/anon */
3972 if ((pg->pqflags & PQ_FREE) == 0) {
3973 if (pg->pqflags & PQ_ANON) {
3974 if (pg->uanon == NULL || pg->uanon->u.an_page != pg)
3975 (*pr)(" >>> ANON DOES NOT POINT HERE <<< (%p)\n",
3976 (pg->uanon) ? pg->uanon->u.an_page : NULL);
3977 else
3978 (*pr)(" anon backpointer is OK\n");
3979 } else {
3980 uobj = pg->uobject;
3981 if (uobj) {
3982 (*pr)(" checking object list\n");
3983 TAILQ_FOREACH(tpg, &uobj->memq, listq) {
3984 if (tpg == pg) {
3985 break;
3986 }
3987 }
3988 if (tpg)
3989 (*pr)(" page found on object list\n");
3990 else
3991 (*pr)(" >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n");
3992 }
3993 }
3994 }
3995
3996 /* cross-verify page queue */
3997 if (pg->pqflags & PQ_FREE) {
3998 int fl = uvm_page_lookup_freelist(pg);
3999 int color = VM_PGCOLOR_BUCKET(pg);
4000 pgl = &uvm.page_free[fl].pgfl_buckets[color].pgfl_queues[
4001 ((pg)->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN];
4002 } else if (pg->pqflags & PQ_INACTIVE) {
4003 pgl = &uvm.page_inactive;
4004 } else if (pg->pqflags & PQ_ACTIVE) {
4005 pgl = &uvm.page_active;
4006 } else {
4007 pgl = NULL;
4008 }
4009
4010 if (pgl) {
4011 (*pr)(" checking pageq list\n");
4012 TAILQ_FOREACH(tpg, pgl, pageq) {
4013 if (tpg == pg) {
4014 break;
4015 }
4016 }
4017 if (tpg)
4018 (*pr)(" page found on pageq list\n");
4019 else
4020 (*pr)(" >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n");
4021 }
4022 }
4023 #endif
Cache object: 23ecec3fc1842cddae43416acdc05cfa
|