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