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