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