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