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