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