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