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