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