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