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