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