uvm_map.c revision 1.134 1 /* $NetBSD: uvm_map.c,v 1.134 2003/03/02 02:55:03 matt 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.134 2003/03/02 02:55:03 matt 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 if (topdown)
1187 hint &= ~(align-1);
1188 else
1189 hint = roundup(hint, align);
1190 }
1191 /*
1192 * XXX Should we PMAP_PREFER() here again?
1193 */
1194 }
1195 end = hint + length;
1196 if (end > map->max_offset || end < hint) {
1197 UVMHIST_LOG(maphist,"<- failed (off end)", 0,0,0,0);
1198 if (align != 0) {
1199 UVMHIST_LOG(maphist,
1200 "calling recursively, no align",
1201 0,0,0,0);
1202 return (uvm_map_findspace(map, orig_hint,
1203 length, result, uobj, uoffset, 0, flags));
1204 }
1205 return (NULL);
1206 }
1207 if (!topdown || next == NULL) {
1208 next = entry->next;
1209 } else
1210 next = entry->prev;
1211 if (next == &map->header ||
1212 (!topdown && next->start >= end) ||
1213 ( topdown && next->end <= hint))
1214 break;
1215 if (flags & UVM_FLAG_FIXED) {
1216 UVMHIST_LOG(maphist,"<- fixed mapping failed", 0,0,0,0);
1217 return(NULL); /* only one shot at it ... */
1218 }
1219 }
1220 quickfind:
1221 SAVE_HINT(map, map->hint, entry);
1222 if (topdown && entry->start == hint + length)
1223 entry = entry->prev;
1224 *result = hint;
1225 UVMHIST_LOG(maphist,"<- got it! (result=0x%x)", hint, 0,0,0);
1226 return (entry);
1227 }
1228
1229 /*
1230 * U N M A P - m a i n h e l p e r f u n c t i o n s
1231 */
1232
1233 /*
1234 * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop")
1235 *
1236 * => caller must check alignment and size
1237 * => map must be locked by caller
1238 * => we return a list of map entries that we've remove from the map
1239 * in "entry_list"
1240 */
1241
1242 void
1243 uvm_unmap_remove(map, start, end, entry_list)
1244 struct vm_map *map;
1245 vaddr_t start, end;
1246 struct vm_map_entry **entry_list; /* OUT */
1247 {
1248 struct vm_map_entry *entry, *first_entry, *next;
1249 vaddr_t len;
1250 UVMHIST_FUNC("uvm_unmap_remove"); UVMHIST_CALLED(maphist);
1251
1252 UVMHIST_LOG(maphist,"(map=0x%x, start=0x%x, end=0x%x)",
1253 map, start, end, 0);
1254 VM_MAP_RANGE_CHECK(map, start, end);
1255
1256 /*
1257 * find first entry
1258 */
1259
1260 if (uvm_map_lookup_entry(map, start, &first_entry) == TRUE) {
1261 /* clip and go... */
1262 entry = first_entry;
1263 UVM_MAP_CLIP_START(map, entry, start);
1264 /* critical! prevents stale hint */
1265 SAVE_HINT(map, entry, entry->prev);
1266 } else {
1267 entry = first_entry->next;
1268 }
1269
1270 /*
1271 * Save the free space hint
1272 */
1273
1274 if (map->first_free->start >= start)
1275 map->first_free = entry->prev;
1276
1277 /*
1278 * note: we now re-use first_entry for a different task. we remove
1279 * a number of map entries from the map and save them in a linked
1280 * list headed by "first_entry". once we remove them from the map
1281 * the caller should unlock the map and drop the references to the
1282 * backing objects [c.f. uvm_unmap_detach]. the object is to
1283 * separate unmapping from reference dropping. why?
1284 * [1] the map has to be locked for unmapping
1285 * [2] the map need not be locked for reference dropping
1286 * [3] dropping references may trigger pager I/O, and if we hit
1287 * a pager that does synchronous I/O we may have to wait for it.
1288 * [4] we would like all waiting for I/O to occur with maps unlocked
1289 * so that we don't block other threads.
1290 */
1291
1292 first_entry = NULL;
1293 *entry_list = NULL;
1294
1295 /*
1296 * break up the area into map entry sized regions and unmap. note
1297 * that all mappings have to be removed before we can even consider
1298 * dropping references to amaps or VM objects (otherwise we could end
1299 * up with a mapping to a page on the free list which would be very bad)
1300 */
1301
1302 while ((entry != &map->header) && (entry->start < end)) {
1303 UVM_MAP_CLIP_END(map, entry, end);
1304 next = entry->next;
1305 len = entry->end - entry->start;
1306
1307 /*
1308 * unwire before removing addresses from the pmap; otherwise
1309 * unwiring will put the entries back into the pmap (XXX).
1310 */
1311
1312 if (VM_MAPENT_ISWIRED(entry)) {
1313 uvm_map_entry_unwire(map, entry);
1314 }
1315 if ((map->flags & VM_MAP_PAGEABLE) == 0) {
1316
1317 /*
1318 * if the map is non-pageable, any pages mapped there
1319 * must be wired and entered with pmap_kenter_pa(),
1320 * and we should free any such pages immediately.
1321 * this is mostly used for kmem_map and mb_map.
1322 */
1323
1324 uvm_km_pgremove_intrsafe(entry->start, entry->end);
1325 pmap_kremove(entry->start, len);
1326 } else if (UVM_ET_ISOBJ(entry) &&
1327 UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)) {
1328 KASSERT(vm_map_pmap(map) == pmap_kernel());
1329
1330 /*
1331 * note: kernel object mappings are currently used in
1332 * two ways:
1333 * [1] "normal" mappings of pages in the kernel object
1334 * [2] uvm_km_valloc'd allocations in which we
1335 * pmap_enter in some non-kernel-object page
1336 * (e.g. vmapbuf).
1337 *
1338 * for case [1], we need to remove the mapping from
1339 * the pmap and then remove the page from the kernel
1340 * object (because, once pages in a kernel object are
1341 * unmapped they are no longer needed, unlike, say,
1342 * a vnode where you might want the data to persist
1343 * until flushed out of a queue).
1344 *
1345 * for case [2], we need to remove the mapping from
1346 * the pmap. there shouldn't be any pages at the
1347 * specified offset in the kernel object [but it
1348 * doesn't hurt to call uvm_km_pgremove just to be
1349 * safe?]
1350 *
1351 * uvm_km_pgremove currently does the following:
1352 * for pages in the kernel object in range:
1353 * - drops the swap slot
1354 * - uvm_pagefree the page
1355 */
1356
1357 /*
1358 * remove mappings from pmap and drop the pages
1359 * from the object. offsets are always relative
1360 * to vm_map_min(kernel_map).
1361 */
1362
1363 pmap_remove(pmap_kernel(), entry->start,
1364 entry->start + len);
1365 uvm_km_pgremove(entry->object.uvm_obj,
1366 entry->start - vm_map_min(kernel_map),
1367 entry->end - vm_map_min(kernel_map));
1368
1369 /*
1370 * null out kernel_object reference, we've just
1371 * dropped it
1372 */
1373
1374 entry->etype &= ~UVM_ET_OBJ;
1375 entry->object.uvm_obj = NULL;
1376 } else if (UVM_ET_ISOBJ(entry) || entry->aref.ar_amap) {
1377
1378 /*
1379 * remove mappings the standard way.
1380 */
1381
1382 pmap_remove(map->pmap, entry->start, entry->end);
1383 }
1384
1385 /*
1386 * remove entry from map and put it on our list of entries
1387 * that we've nuked. then go to next entry.
1388 */
1389
1390 UVMHIST_LOG(maphist, " removed map entry 0x%x", entry, 0, 0,0);
1391
1392 /* critical! prevents stale hint */
1393 SAVE_HINT(map, entry, entry->prev);
1394
1395 uvm_map_entry_unlink(map, entry);
1396 map->size -= len;
1397 entry->prev = NULL;
1398 entry->next = first_entry;
1399 first_entry = entry;
1400 entry = next;
1401 }
1402 if ((map->flags & VM_MAP_DYING) == 0) {
1403 pmap_update(vm_map_pmap(map));
1404 }
1405
1406 /*
1407 * now we've cleaned up the map and are ready for the caller to drop
1408 * references to the mapped objects.
1409 */
1410
1411 *entry_list = first_entry;
1412 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
1413 }
1414
1415 /*
1416 * uvm_unmap_detach: drop references in a chain of map entries
1417 *
1418 * => we will free the map entries as we traverse the list.
1419 */
1420
1421 void
1422 uvm_unmap_detach(first_entry, flags)
1423 struct vm_map_entry *first_entry;
1424 int flags;
1425 {
1426 struct vm_map_entry *next_entry;
1427 UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist);
1428
1429 while (first_entry) {
1430 KASSERT(!VM_MAPENT_ISWIRED(first_entry));
1431 UVMHIST_LOG(maphist,
1432 " detach 0x%x: amap=0x%x, obj=0x%x, submap?=%d",
1433 first_entry, first_entry->aref.ar_amap,
1434 first_entry->object.uvm_obj,
1435 UVM_ET_ISSUBMAP(first_entry));
1436
1437 /*
1438 * drop reference to amap, if we've got one
1439 */
1440
1441 if (first_entry->aref.ar_amap)
1442 uvm_map_unreference_amap(first_entry, flags);
1443
1444 /*
1445 * drop reference to our backing object, if we've got one
1446 */
1447
1448 KASSERT(!UVM_ET_ISSUBMAP(first_entry));
1449 if (UVM_ET_ISOBJ(first_entry) &&
1450 first_entry->object.uvm_obj->pgops->pgo_detach) {
1451 (*first_entry->object.uvm_obj->pgops->pgo_detach)
1452 (first_entry->object.uvm_obj);
1453 }
1454 next_entry = first_entry->next;
1455 uvm_mapent_free(first_entry);
1456 first_entry = next_entry;
1457 }
1458 UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
1459 }
1460
1461 /*
1462 * E X T R A C T I O N F U N C T I O N S
1463 */
1464
1465 /*
1466 * uvm_map_reserve: reserve space in a vm_map for future use.
1467 *
1468 * => we reserve space in a map by putting a dummy map entry in the
1469 * map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE)
1470 * => map should be unlocked (we will write lock it)
1471 * => we return true if we were able to reserve space
1472 * => XXXCDC: should be inline?
1473 */
1474
1475 int
1476 uvm_map_reserve(map, size, offset, align, raddr)
1477 struct vm_map *map;
1478 vsize_t size;
1479 vaddr_t offset; /* hint for pmap_prefer */
1480 vsize_t align; /* alignment hint */
1481 vaddr_t *raddr; /* IN:hint, OUT: reserved VA */
1482 {
1483 UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist);
1484
1485 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x, offset=0x%x,addr=0x%x)",
1486 map,size,offset,raddr);
1487
1488 size = round_page(size);
1489 if (*raddr < vm_map_min(map))
1490 *raddr = vm_map_min(map); /* hint */
1491
1492 /*
1493 * reserve some virtual space.
1494 */
1495
1496 if (uvm_map(map, raddr, size, NULL, offset, 0,
1497 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
1498 UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
1499 UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
1500 return (FALSE);
1501 }
1502
1503 UVMHIST_LOG(maphist, "<- done (*raddr=0x%x)", *raddr,0,0,0);
1504 return (TRUE);
1505 }
1506
1507 /*
1508 * uvm_map_replace: replace a reserved (blank) area of memory with
1509 * real mappings.
1510 *
1511 * => caller must WRITE-LOCK the map
1512 * => we return TRUE if replacement was a success
1513 * => we expect the newents chain to have nnewents entrys on it and
1514 * we expect newents->prev to point to the last entry on the list
1515 * => note newents is allowed to be NULL
1516 */
1517
1518 int
1519 uvm_map_replace(map, start, end, newents, nnewents)
1520 struct vm_map *map;
1521 vaddr_t start, end;
1522 struct vm_map_entry *newents;
1523 int nnewents;
1524 {
1525 struct vm_map_entry *oldent, *last;
1526
1527 /*
1528 * first find the blank map entry at the specified address
1529 */
1530
1531 if (!uvm_map_lookup_entry(map, start, &oldent)) {
1532 return(FALSE);
1533 }
1534
1535 /*
1536 * check to make sure we have a proper blank entry
1537 */
1538
1539 if (oldent->start != start || oldent->end != end ||
1540 oldent->object.uvm_obj != NULL || oldent->aref.ar_amap != NULL) {
1541 return (FALSE);
1542 }
1543
1544 #ifdef DIAGNOSTIC
1545
1546 /*
1547 * sanity check the newents chain
1548 */
1549
1550 {
1551 struct vm_map_entry *tmpent = newents;
1552 int nent = 0;
1553 vaddr_t cur = start;
1554
1555 while (tmpent) {
1556 nent++;
1557 if (tmpent->start < cur)
1558 panic("uvm_map_replace1");
1559 if (tmpent->start > tmpent->end || tmpent->end > end) {
1560 printf("tmpent->start=0x%lx, tmpent->end=0x%lx, end=0x%lx\n",
1561 tmpent->start, tmpent->end, end);
1562 panic("uvm_map_replace2");
1563 }
1564 cur = tmpent->end;
1565 if (tmpent->next) {
1566 if (tmpent->next->prev != tmpent)
1567 panic("uvm_map_replace3");
1568 } else {
1569 if (newents->prev != tmpent)
1570 panic("uvm_map_replace4");
1571 }
1572 tmpent = tmpent->next;
1573 }
1574 if (nent != nnewents)
1575 panic("uvm_map_replace5");
1576 }
1577 #endif
1578
1579 /*
1580 * map entry is a valid blank! replace it. (this does all the
1581 * work of map entry link/unlink...).
1582 */
1583
1584 if (newents) {
1585 last = newents->prev;
1586
1587 /* critical: flush stale hints out of map */
1588 SAVE_HINT(map, map->hint, newents);
1589 if (map->first_free == oldent)
1590 map->first_free = last;
1591
1592 last->next = oldent->next;
1593 last->next->prev = last;
1594 newents->prev = oldent->prev;
1595 newents->prev->next = newents;
1596 map->nentries = map->nentries + (nnewents - 1);
1597
1598 } else {
1599
1600 /* critical: flush stale hints out of map */
1601 SAVE_HINT(map, map->hint, oldent->prev);
1602 if (map->first_free == oldent)
1603 map->first_free = oldent->prev;
1604
1605 /* NULL list of new entries: just remove the old one */
1606 uvm_map_entry_unlink(map, oldent);
1607 }
1608
1609
1610 /*
1611 * now we can free the old blank entry, unlock the map and return.
1612 */
1613
1614 uvm_mapent_free(oldent);
1615 return(TRUE);
1616 }
1617
1618 /*
1619 * uvm_map_extract: extract a mapping from a map and put it somewhere
1620 * (maybe removing the old mapping)
1621 *
1622 * => maps should be unlocked (we will write lock them)
1623 * => returns 0 on success, error code otherwise
1624 * => start must be page aligned
1625 * => len must be page sized
1626 * => flags:
1627 * UVM_EXTRACT_REMOVE: remove mappings from srcmap
1628 * UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only)
1629 * UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs
1630 * UVM_EXTRACT_FIXPROT: set prot to maxprot as we go
1631 * >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<<
1632 * >>>NOTE: QREF's must be unmapped via the QREF path, thus should only
1633 * be used from within the kernel in a kernel level map <<<
1634 */
1635
1636 int
1637 uvm_map_extract(srcmap, start, len, dstmap, dstaddrp, flags)
1638 struct vm_map *srcmap, *dstmap;
1639 vaddr_t start, *dstaddrp;
1640 vsize_t len;
1641 int flags;
1642 {
1643 vaddr_t dstaddr, end, newend, oldoffset, fudge, orig_fudge,
1644 oldstart;
1645 struct vm_map_entry *chain, *endchain, *entry, *orig_entry, *newentry,
1646 *deadentry, *oldentry;
1647 vsize_t elen;
1648 int nchain, error, copy_ok;
1649 UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist);
1650
1651 UVMHIST_LOG(maphist,"(srcmap=0x%x,start=0x%x, len=0x%x", srcmap, start,
1652 len,0);
1653 UVMHIST_LOG(maphist," ...,dstmap=0x%x, flags=0x%x)", dstmap,flags,0,0);
1654
1655 /*
1656 * step 0: sanity check: start must be on a page boundary, length
1657 * must be page sized. can't ask for CONTIG/QREF if you asked for
1658 * REMOVE.
1659 */
1660
1661 KASSERT((start & PAGE_MASK) == 0 && (len & PAGE_MASK) == 0);
1662 KASSERT((flags & UVM_EXTRACT_REMOVE) == 0 ||
1663 (flags & (UVM_EXTRACT_CONTIG|UVM_EXTRACT_QREF)) == 0);
1664
1665 /*
1666 * step 1: reserve space in the target map for the extracted area
1667 */
1668
1669 dstaddr = vm_map_min(dstmap);
1670 if (uvm_map_reserve(dstmap, len, start, 0, &dstaddr) == FALSE)
1671 return(ENOMEM);
1672 *dstaddrp = dstaddr; /* pass address back to caller */
1673 UVMHIST_LOG(maphist, " dstaddr=0x%x", dstaddr,0,0,0);
1674
1675 /*
1676 * step 2: setup for the extraction process loop by init'ing the
1677 * map entry chain, locking src map, and looking up the first useful
1678 * entry in the map.
1679 */
1680
1681 end = start + len;
1682 newend = dstaddr + len;
1683 chain = endchain = NULL;
1684 nchain = 0;
1685 vm_map_lock(srcmap);
1686
1687 if (uvm_map_lookup_entry(srcmap, start, &entry)) {
1688
1689 /* "start" is within an entry */
1690 if (flags & UVM_EXTRACT_QREF) {
1691
1692 /*
1693 * for quick references we don't clip the entry, so
1694 * the entry may map space "before" the starting
1695 * virtual address... this is the "fudge" factor
1696 * (which can be non-zero only the first time
1697 * through the "while" loop in step 3).
1698 */
1699
1700 fudge = start - entry->start;
1701 } else {
1702
1703 /*
1704 * normal reference: we clip the map to fit (thus
1705 * fudge is zero)
1706 */
1707
1708 UVM_MAP_CLIP_START(srcmap, entry, start);
1709 SAVE_HINT(srcmap, srcmap->hint, entry->prev);
1710 fudge = 0;
1711 }
1712 } else {
1713
1714 /* "start" is not within an entry ... skip to next entry */
1715 if (flags & UVM_EXTRACT_CONTIG) {
1716 error = EINVAL;
1717 goto bad; /* definite hole here ... */
1718 }
1719
1720 entry = entry->next;
1721 fudge = 0;
1722 }
1723
1724 /* save values from srcmap for step 6 */
1725 orig_entry = entry;
1726 orig_fudge = fudge;
1727
1728 /*
1729 * step 3: now start looping through the map entries, extracting
1730 * as we go.
1731 */
1732
1733 while (entry->start < end && entry != &srcmap->header) {
1734
1735 /* if we are not doing a quick reference, clip it */
1736 if ((flags & UVM_EXTRACT_QREF) == 0)
1737 UVM_MAP_CLIP_END(srcmap, entry, end);
1738
1739 /* clear needs_copy (allow chunking) */
1740 if (UVM_ET_ISNEEDSCOPY(entry)) {
1741 if (fudge)
1742 oldstart = entry->start;
1743 else
1744 oldstart = 0; /* XXX: gcc */
1745 amap_copy(srcmap, entry, M_NOWAIT, TRUE, start, end);
1746 if (UVM_ET_ISNEEDSCOPY(entry)) { /* failed? */
1747 error = ENOMEM;
1748 goto bad;
1749 }
1750
1751 /* amap_copy could clip (during chunk)! update fudge */
1752 if (fudge) {
1753 fudge = fudge - (entry->start - oldstart);
1754 orig_fudge = fudge;
1755 }
1756 }
1757
1758 /* calculate the offset of this from "start" */
1759 oldoffset = (entry->start + fudge) - start;
1760
1761 /* allocate a new map entry */
1762 newentry = uvm_mapent_alloc(dstmap, 0);
1763 if (newentry == NULL) {
1764 error = ENOMEM;
1765 goto bad;
1766 }
1767
1768 /* set up new map entry */
1769 newentry->next = NULL;
1770 newentry->prev = endchain;
1771 newentry->start = dstaddr + oldoffset;
1772 newentry->end =
1773 newentry->start + (entry->end - (entry->start + fudge));
1774 if (newentry->end > newend || newentry->end < newentry->start)
1775 newentry->end = newend;
1776 newentry->object.uvm_obj = entry->object.uvm_obj;
1777 if (newentry->object.uvm_obj) {
1778 if (newentry->object.uvm_obj->pgops->pgo_reference)
1779 newentry->object.uvm_obj->pgops->
1780 pgo_reference(newentry->object.uvm_obj);
1781 newentry->offset = entry->offset + fudge;
1782 } else {
1783 newentry->offset = 0;
1784 }
1785 newentry->etype = entry->etype;
1786 newentry->protection = (flags & UVM_EXTRACT_FIXPROT) ?
1787 entry->max_protection : entry->protection;
1788 newentry->max_protection = entry->max_protection;
1789 newentry->inheritance = entry->inheritance;
1790 newentry->wired_count = 0;
1791 newentry->aref.ar_amap = entry->aref.ar_amap;
1792 if (newentry->aref.ar_amap) {
1793 newentry->aref.ar_pageoff =
1794 entry->aref.ar_pageoff + (fudge >> PAGE_SHIFT);
1795 uvm_map_reference_amap(newentry, AMAP_SHARED |
1796 ((flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0));
1797 } else {
1798 newentry->aref.ar_pageoff = 0;
1799 }
1800 newentry->advice = entry->advice;
1801
1802 /* now link it on the chain */
1803 nchain++;
1804 if (endchain == NULL) {
1805 chain = endchain = newentry;
1806 } else {
1807 endchain->next = newentry;
1808 endchain = newentry;
1809 }
1810
1811 /* end of 'while' loop! */
1812 if ((flags & UVM_EXTRACT_CONTIG) && entry->end < end &&
1813 (entry->next == &srcmap->header ||
1814 entry->next->start != entry->end)) {
1815 error = EINVAL;
1816 goto bad;
1817 }
1818 entry = entry->next;
1819 fudge = 0;
1820 }
1821
1822 /*
1823 * step 4: close off chain (in format expected by uvm_map_replace)
1824 */
1825
1826 if (chain)
1827 chain->prev = endchain;
1828
1829 /*
1830 * step 5: attempt to lock the dest map so we can pmap_copy.
1831 * note usage of copy_ok:
1832 * 1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5)
1833 * 0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7
1834 */
1835
1836 if (srcmap == dstmap || vm_map_lock_try(dstmap) == TRUE) {
1837 copy_ok = 1;
1838 if (!uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
1839 nchain)) {
1840 if (srcmap != dstmap)
1841 vm_map_unlock(dstmap);
1842 error = EIO;
1843 goto bad;
1844 }
1845 } else {
1846 copy_ok = 0;
1847 /* replace defered until step 7 */
1848 }
1849
1850 /*
1851 * step 6: traverse the srcmap a second time to do the following:
1852 * - if we got a lock on the dstmap do pmap_copy
1853 * - if UVM_EXTRACT_REMOVE remove the entries
1854 * we make use of orig_entry and orig_fudge (saved in step 2)
1855 */
1856
1857 if (copy_ok || (flags & UVM_EXTRACT_REMOVE)) {
1858
1859 /* purge possible stale hints from srcmap */
1860 if (flags & UVM_EXTRACT_REMOVE) {
1861 SAVE_HINT(srcmap, srcmap->hint, orig_entry->prev);
1862 if (srcmap->first_free->start >= start)
1863 srcmap->first_free = orig_entry->prev;
1864 }
1865
1866 entry = orig_entry;
1867 fudge = orig_fudge;
1868 deadentry = NULL; /* for UVM_EXTRACT_REMOVE */
1869
1870 while (entry->start < end && entry != &srcmap->header) {
1871 if (copy_ok) {
1872 oldoffset = (entry->start + fudge) - start;
1873 elen = MIN(end, entry->end) -
1874 (entry->start + fudge);
1875 pmap_copy(dstmap->pmap, srcmap->pmap,
1876 dstaddr + oldoffset, elen,
1877 entry->start + fudge);
1878 }
1879
1880 /* we advance "entry" in the following if statement */
1881 if (flags & UVM_EXTRACT_REMOVE) {
1882 pmap_remove(srcmap->pmap, entry->start,
1883 entry->end);
1884 oldentry = entry; /* save entry */
1885 entry = entry->next; /* advance */
1886 uvm_map_entry_unlink(srcmap, oldentry);
1887 /* add to dead list */
1888 oldentry->next = deadentry;
1889 deadentry = oldentry;
1890 } else {
1891 entry = entry->next; /* advance */
1892 }
1893
1894 /* end of 'while' loop */
1895 fudge = 0;
1896 }
1897 pmap_update(srcmap->pmap);
1898
1899 /*
1900 * unlock dstmap. we will dispose of deadentry in
1901 * step 7 if needed
1902 */
1903
1904 if (copy_ok && srcmap != dstmap)
1905 vm_map_unlock(dstmap);
1906
1907 } else {
1908 deadentry = NULL;
1909 }
1910
1911 /*
1912 * step 7: we are done with the source map, unlock. if copy_ok
1913 * is 0 then we have not replaced the dummy mapping in dstmap yet
1914 * and we need to do so now.
1915 */
1916
1917 vm_map_unlock(srcmap);
1918 if ((flags & UVM_EXTRACT_REMOVE) && deadentry)
1919 uvm_unmap_detach(deadentry, 0); /* dispose of old entries */
1920
1921 /* now do the replacement if we didn't do it in step 5 */
1922 if (copy_ok == 0) {
1923 vm_map_lock(dstmap);
1924 error = uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
1925 nchain);
1926 vm_map_unlock(dstmap);
1927
1928 if (error == FALSE) {
1929 error = EIO;
1930 goto bad2;
1931 }
1932 }
1933 return(0);
1934
1935 /*
1936 * bad: failure recovery
1937 */
1938 bad:
1939 vm_map_unlock(srcmap);
1940 bad2: /* src already unlocked */
1941 if (chain)
1942 uvm_unmap_detach(chain,
1943 (flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0);
1944 uvm_unmap(dstmap, dstaddr, dstaddr+len); /* ??? */
1945 return(error);
1946 }
1947
1948 /* end of extraction functions */
1949
1950 /*
1951 * uvm_map_submap: punch down part of a map into a submap
1952 *
1953 * => only the kernel_map is allowed to be submapped
1954 * => the purpose of submapping is to break up the locking granularity
1955 * of a larger map
1956 * => the range specified must have been mapped previously with a uvm_map()
1957 * call [with uobj==NULL] to create a blank map entry in the main map.
1958 * [And it had better still be blank!]
1959 * => maps which contain submaps should never be copied or forked.
1960 * => to remove a submap, use uvm_unmap() on the main map
1961 * and then uvm_map_deallocate() the submap.
1962 * => main map must be unlocked.
1963 * => submap must have been init'd and have a zero reference count.
1964 * [need not be locked as we don't actually reference it]
1965 */
1966
1967 int
1968 uvm_map_submap(map, start, end, submap)
1969 struct vm_map *map, *submap;
1970 vaddr_t start, end;
1971 {
1972 struct vm_map_entry *entry;
1973 int error;
1974
1975 vm_map_lock(map);
1976 VM_MAP_RANGE_CHECK(map, start, end);
1977
1978 if (uvm_map_lookup_entry(map, start, &entry)) {
1979 UVM_MAP_CLIP_START(map, entry, start);
1980 UVM_MAP_CLIP_END(map, entry, end); /* to be safe */
1981 } else {
1982 entry = NULL;
1983 }
1984
1985 if (entry != NULL &&
1986 entry->start == start && entry->end == end &&
1987 entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL &&
1988 !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) {
1989 entry->etype |= UVM_ET_SUBMAP;
1990 entry->object.sub_map = submap;
1991 entry->offset = 0;
1992 uvm_map_reference(submap);
1993 error = 0;
1994 } else {
1995 error = EINVAL;
1996 }
1997 vm_map_unlock(map);
1998 return error;
1999 }
2000
2001
2002 /*
2003 * uvm_map_protect: change map protection
2004 *
2005 * => set_max means set max_protection.
2006 * => map must be unlocked.
2007 */
2008
2009 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
2010 ~VM_PROT_WRITE : VM_PROT_ALL)
2011
2012 int
2013 uvm_map_protect(map, start, end, new_prot, set_max)
2014 struct vm_map *map;
2015 vaddr_t start, end;
2016 vm_prot_t new_prot;
2017 boolean_t set_max;
2018 {
2019 struct vm_map_entry *current, *entry;
2020 int error = 0;
2021 UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist);
2022 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_prot=0x%x)",
2023 map, start, end, new_prot);
2024
2025 vm_map_lock(map);
2026 VM_MAP_RANGE_CHECK(map, start, end);
2027 if (uvm_map_lookup_entry(map, start, &entry)) {
2028 UVM_MAP_CLIP_START(map, entry, start);
2029 } else {
2030 entry = entry->next;
2031 }
2032
2033 /*
2034 * make a first pass to check for protection violations.
2035 */
2036
2037 current = entry;
2038 while ((current != &map->header) && (current->start < end)) {
2039 if (UVM_ET_ISSUBMAP(current)) {
2040 error = EINVAL;
2041 goto out;
2042 }
2043 if ((new_prot & current->max_protection) != new_prot) {
2044 error = EACCES;
2045 goto out;
2046 }
2047 /*
2048 * Don't allow VM_PROT_EXECUTE to be set on entries that
2049 * point to vnodes that are associated with a NOEXEC file
2050 * system.
2051 */
2052 if (UVM_ET_ISOBJ(current) &&
2053 UVM_OBJ_IS_VNODE(current->object.uvm_obj)) {
2054 struct vnode *vp =
2055 (struct vnode *) current->object.uvm_obj;
2056
2057 if ((new_prot & VM_PROT_EXECUTE) != 0 &&
2058 (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
2059 error = EACCES;
2060 goto out;
2061 }
2062 }
2063 current = current->next;
2064 }
2065
2066 /* go back and fix up protections (no need to clip this time). */
2067
2068 current = entry;
2069 while ((current != &map->header) && (current->start < end)) {
2070 vm_prot_t old_prot;
2071
2072 UVM_MAP_CLIP_END(map, current, end);
2073 old_prot = current->protection;
2074 if (set_max)
2075 current->protection =
2076 (current->max_protection = new_prot) & old_prot;
2077 else
2078 current->protection = new_prot;
2079
2080 /*
2081 * update physical map if necessary. worry about copy-on-write
2082 * here -- CHECK THIS XXX
2083 */
2084
2085 if (current->protection != old_prot) {
2086 /* update pmap! */
2087 pmap_protect(map->pmap, current->start, current->end,
2088 current->protection & MASK(entry));
2089
2090 /*
2091 * If this entry points at a vnode, and the
2092 * protection includes VM_PROT_EXECUTE, mark
2093 * the vnode as VEXECMAP.
2094 */
2095 if (UVM_ET_ISOBJ(current)) {
2096 struct uvm_object *uobj =
2097 current->object.uvm_obj;
2098
2099 if (UVM_OBJ_IS_VNODE(uobj) &&
2100 (current->protection & VM_PROT_EXECUTE))
2101 vn_markexec((struct vnode *) uobj);
2102 }
2103 }
2104
2105 /*
2106 * If the map is configured to lock any future mappings,
2107 * wire this entry now if the old protection was VM_PROT_NONE
2108 * and the new protection is not VM_PROT_NONE.
2109 */
2110
2111 if ((map->flags & VM_MAP_WIREFUTURE) != 0 &&
2112 VM_MAPENT_ISWIRED(entry) == 0 &&
2113 old_prot == VM_PROT_NONE &&
2114 new_prot != VM_PROT_NONE) {
2115 if (uvm_map_pageable(map, entry->start,
2116 entry->end, FALSE,
2117 UVM_LK_ENTER|UVM_LK_EXIT) != 0) {
2118
2119 /*
2120 * If locking the entry fails, remember the
2121 * error if it's the first one. Note we
2122 * still continue setting the protection in
2123 * the map, but will return the error
2124 * condition regardless.
2125 *
2126 * XXX Ignore what the actual error is,
2127 * XXX just call it a resource shortage
2128 * XXX so that it doesn't get confused
2129 * XXX what uvm_map_protect() itself would
2130 * XXX normally return.
2131 */
2132
2133 error = ENOMEM;
2134 }
2135 }
2136 current = current->next;
2137 }
2138 pmap_update(map->pmap);
2139
2140 out:
2141 vm_map_unlock(map);
2142 UVMHIST_LOG(maphist, "<- done, error=%d",error,0,0,0);
2143 return error;
2144 }
2145
2146 #undef MASK
2147
2148 /*
2149 * uvm_map_inherit: set inheritance code for range of addrs in map.
2150 *
2151 * => map must be unlocked
2152 * => note that the inherit code is used during a "fork". see fork
2153 * code for details.
2154 */
2155
2156 int
2157 uvm_map_inherit(map, start, end, new_inheritance)
2158 struct vm_map *map;
2159 vaddr_t start;
2160 vaddr_t end;
2161 vm_inherit_t new_inheritance;
2162 {
2163 struct vm_map_entry *entry, *temp_entry;
2164 UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist);
2165 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_inh=0x%x)",
2166 map, start, end, new_inheritance);
2167
2168 switch (new_inheritance) {
2169 case MAP_INHERIT_NONE:
2170 case MAP_INHERIT_COPY:
2171 case MAP_INHERIT_SHARE:
2172 break;
2173 default:
2174 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
2175 return EINVAL;
2176 }
2177
2178 vm_map_lock(map);
2179 VM_MAP_RANGE_CHECK(map, start, end);
2180 if (uvm_map_lookup_entry(map, start, &temp_entry)) {
2181 entry = temp_entry;
2182 UVM_MAP_CLIP_START(map, entry, start);
2183 } else {
2184 entry = temp_entry->next;
2185 }
2186 while ((entry != &map->header) && (entry->start < end)) {
2187 UVM_MAP_CLIP_END(map, entry, end);
2188 entry->inheritance = new_inheritance;
2189 entry = entry->next;
2190 }
2191 vm_map_unlock(map);
2192 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
2193 return 0;
2194 }
2195
2196 /*
2197 * uvm_map_advice: set advice code for range of addrs in map.
2198 *
2199 * => map must be unlocked
2200 */
2201
2202 int
2203 uvm_map_advice(map, start, end, new_advice)
2204 struct vm_map *map;
2205 vaddr_t start;
2206 vaddr_t end;
2207 int new_advice;
2208 {
2209 struct vm_map_entry *entry, *temp_entry;
2210 UVMHIST_FUNC("uvm_map_advice"); UVMHIST_CALLED(maphist);
2211 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_adv=0x%x)",
2212 map, start, end, new_advice);
2213
2214 vm_map_lock(map);
2215 VM_MAP_RANGE_CHECK(map, start, end);
2216 if (uvm_map_lookup_entry(map, start, &temp_entry)) {
2217 entry = temp_entry;
2218 UVM_MAP_CLIP_START(map, entry, start);
2219 } else {
2220 entry = temp_entry->next;
2221 }
2222
2223 /*
2224 * XXXJRT: disallow holes?
2225 */
2226
2227 while ((entry != &map->header) && (entry->start < end)) {
2228 UVM_MAP_CLIP_END(map, entry, end);
2229
2230 switch (new_advice) {
2231 case MADV_NORMAL:
2232 case MADV_RANDOM:
2233 case MADV_SEQUENTIAL:
2234 /* nothing special here */
2235 break;
2236
2237 default:
2238 vm_map_unlock(map);
2239 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
2240 return EINVAL;
2241 }
2242 entry->advice = new_advice;
2243 entry = entry->next;
2244 }
2245
2246 vm_map_unlock(map);
2247 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
2248 return 0;
2249 }
2250
2251 /*
2252 * uvm_map_pageable: sets the pageability of a range in a map.
2253 *
2254 * => wires map entries. should not be used for transient page locking.
2255 * for that, use uvm_fault_wire()/uvm_fault_unwire() (see uvm_vslock()).
2256 * => regions sepcified as not pageable require lock-down (wired) memory
2257 * and page tables.
2258 * => map must never be read-locked
2259 * => if islocked is TRUE, map is already write-locked
2260 * => we always unlock the map, since we must downgrade to a read-lock
2261 * to call uvm_fault_wire()
2262 * => XXXCDC: check this and try and clean it up.
2263 */
2264
2265 int
2266 uvm_map_pageable(map, start, end, new_pageable, lockflags)
2267 struct vm_map *map;
2268 vaddr_t start, end;
2269 boolean_t new_pageable;
2270 int lockflags;
2271 {
2272 struct vm_map_entry *entry, *start_entry, *failed_entry;
2273 int rv;
2274 #ifdef DIAGNOSTIC
2275 u_int timestamp_save;
2276 #endif
2277 UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist);
2278 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_pageable=0x%x)",
2279 map, start, end, new_pageable);
2280 KASSERT(map->flags & VM_MAP_PAGEABLE);
2281
2282 if ((lockflags & UVM_LK_ENTER) == 0)
2283 vm_map_lock(map);
2284 VM_MAP_RANGE_CHECK(map, start, end);
2285
2286 /*
2287 * only one pageability change may take place at one time, since
2288 * uvm_fault_wire assumes it will be called only once for each
2289 * wiring/unwiring. therefore, we have to make sure we're actually
2290 * changing the pageability for the entire region. we do so before
2291 * making any changes.
2292 */
2293
2294 if (uvm_map_lookup_entry(map, start, &start_entry) == FALSE) {
2295 if ((lockflags & UVM_LK_EXIT) == 0)
2296 vm_map_unlock(map);
2297
2298 UVMHIST_LOG(maphist,"<- done (fault)",0,0,0,0);
2299 return EFAULT;
2300 }
2301 entry = start_entry;
2302
2303 /*
2304 * handle wiring and unwiring separately.
2305 */
2306
2307 if (new_pageable) { /* unwire */
2308 UVM_MAP_CLIP_START(map, entry, start);
2309
2310 /*
2311 * unwiring. first ensure that the range to be unwired is
2312 * really wired down and that there are no holes.
2313 */
2314
2315 while ((entry != &map->header) && (entry->start < end)) {
2316 if (entry->wired_count == 0 ||
2317 (entry->end < end &&
2318 (entry->next == &map->header ||
2319 entry->next->start > entry->end))) {
2320 if ((lockflags & UVM_LK_EXIT) == 0)
2321 vm_map_unlock(map);
2322 UVMHIST_LOG(maphist, "<- done (INVAL)",0,0,0,0);
2323 return EINVAL;
2324 }
2325 entry = entry->next;
2326 }
2327
2328 /*
2329 * POSIX 1003.1b - a single munlock call unlocks a region,
2330 * regardless of the number of mlock calls made on that
2331 * region.
2332 */
2333
2334 entry = start_entry;
2335 while ((entry != &map->header) && (entry->start < end)) {
2336 UVM_MAP_CLIP_END(map, entry, end);
2337 if (VM_MAPENT_ISWIRED(entry))
2338 uvm_map_entry_unwire(map, entry);
2339 entry = entry->next;
2340 }
2341 if ((lockflags & UVM_LK_EXIT) == 0)
2342 vm_map_unlock(map);
2343 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
2344 return 0;
2345 }
2346
2347 /*
2348 * wire case: in two passes [XXXCDC: ugly block of code here]
2349 *
2350 * 1: holding the write lock, we create any anonymous maps that need
2351 * to be created. then we clip each map entry to the region to
2352 * be wired and increment its wiring count.
2353 *
2354 * 2: we downgrade to a read lock, and call uvm_fault_wire to fault
2355 * in the pages for any newly wired area (wired_count == 1).
2356 *
2357 * downgrading to a read lock for uvm_fault_wire avoids a possible
2358 * deadlock with another thread that may have faulted on one of
2359 * the pages to be wired (it would mark the page busy, blocking
2360 * us, then in turn block on the map lock that we hold). because
2361 * of problems in the recursive lock package, we cannot upgrade
2362 * to a write lock in vm_map_lookup. thus, any actions that
2363 * require the write lock must be done beforehand. because we
2364 * keep the read lock on the map, the copy-on-write status of the
2365 * entries we modify here cannot change.
2366 */
2367
2368 while ((entry != &map->header) && (entry->start < end)) {
2369 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
2370
2371 /*
2372 * perform actions of vm_map_lookup that need the
2373 * write lock on the map: create an anonymous map
2374 * for a copy-on-write region, or an anonymous map
2375 * for a zero-fill region. (XXXCDC: submap case
2376 * ok?)
2377 */
2378
2379 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */
2380 if (UVM_ET_ISNEEDSCOPY(entry) &&
2381 ((entry->max_protection & VM_PROT_WRITE) ||
2382 (entry->object.uvm_obj == NULL))) {
2383 amap_copy(map, entry, M_WAITOK, TRUE,
2384 start, end);
2385 /* XXXCDC: wait OK? */
2386 }
2387 }
2388 }
2389 UVM_MAP_CLIP_START(map, entry, start);
2390 UVM_MAP_CLIP_END(map, entry, end);
2391 entry->wired_count++;
2392
2393 /*
2394 * Check for holes
2395 */
2396
2397 if (entry->protection == VM_PROT_NONE ||
2398 (entry->end < end &&
2399 (entry->next == &map->header ||
2400 entry->next->start > entry->end))) {
2401
2402 /*
2403 * found one. amap creation actions do not need to
2404 * be undone, but the wired counts need to be restored.
2405 */
2406
2407 while (entry != &map->header && entry->end > start) {
2408 entry->wired_count--;
2409 entry = entry->prev;
2410 }
2411 if ((lockflags & UVM_LK_EXIT) == 0)
2412 vm_map_unlock(map);
2413 UVMHIST_LOG(maphist,"<- done (INVALID WIRE)",0,0,0,0);
2414 return EINVAL;
2415 }
2416 entry = entry->next;
2417 }
2418
2419 /*
2420 * Pass 2.
2421 */
2422
2423 #ifdef DIAGNOSTIC
2424 timestamp_save = map->timestamp;
2425 #endif
2426 vm_map_busy(map);
2427 vm_map_downgrade(map);
2428
2429 rv = 0;
2430 entry = start_entry;
2431 while (entry != &map->header && entry->start < end) {
2432 if (entry->wired_count == 1) {
2433 rv = uvm_fault_wire(map, entry->start, entry->end,
2434 VM_FAULT_WIREMAX, entry->max_protection);
2435 if (rv) {
2436
2437 /*
2438 * wiring failed. break out of the loop.
2439 * we'll clean up the map below, once we
2440 * have a write lock again.
2441 */
2442
2443 break;
2444 }
2445 }
2446 entry = entry->next;
2447 }
2448
2449 if (rv) { /* failed? */
2450
2451 /*
2452 * Get back to an exclusive (write) lock.
2453 */
2454
2455 vm_map_upgrade(map);
2456 vm_map_unbusy(map);
2457
2458 #ifdef DIAGNOSTIC
2459 if (timestamp_save != map->timestamp)
2460 panic("uvm_map_pageable: stale map");
2461 #endif
2462
2463 /*
2464 * first drop the wiring count on all the entries
2465 * which haven't actually been wired yet.
2466 */
2467
2468 failed_entry = entry;
2469 while (entry != &map->header && entry->start < end) {
2470 entry->wired_count--;
2471 entry = entry->next;
2472 }
2473
2474 /*
2475 * now, unwire all the entries that were successfully
2476 * wired above.
2477 */
2478
2479 entry = start_entry;
2480 while (entry != failed_entry) {
2481 entry->wired_count--;
2482 if (VM_MAPENT_ISWIRED(entry) == 0)
2483 uvm_map_entry_unwire(map, entry);
2484 entry = entry->next;
2485 }
2486 if ((lockflags & UVM_LK_EXIT) == 0)
2487 vm_map_unlock(map);
2488 UVMHIST_LOG(maphist, "<- done (RV=%d)", rv,0,0,0);
2489 return(rv);
2490 }
2491
2492 /* We are holding a read lock here. */
2493 if ((lockflags & UVM_LK_EXIT) == 0) {
2494 vm_map_unbusy(map);
2495 vm_map_unlock_read(map);
2496 } else {
2497
2498 /*
2499 * Get back to an exclusive (write) lock.
2500 */
2501
2502 vm_map_upgrade(map);
2503 vm_map_unbusy(map);
2504 }
2505
2506 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
2507 return 0;
2508 }
2509
2510 /*
2511 * uvm_map_pageable_all: special case of uvm_map_pageable - affects
2512 * all mapped regions.
2513 *
2514 * => map must not be locked.
2515 * => if no flags are specified, all regions are unwired.
2516 * => XXXJRT: has some of the same problems as uvm_map_pageable() above.
2517 */
2518
2519 int
2520 uvm_map_pageable_all(map, flags, limit)
2521 struct vm_map *map;
2522 int flags;
2523 vsize_t limit;
2524 {
2525 struct vm_map_entry *entry, *failed_entry;
2526 vsize_t size;
2527 int rv;
2528 #ifdef DIAGNOSTIC
2529 u_int timestamp_save;
2530 #endif
2531 UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist);
2532 UVMHIST_LOG(maphist,"(map=0x%x,flags=0x%x)", map, flags, 0, 0);
2533
2534 KASSERT(map->flags & VM_MAP_PAGEABLE);
2535
2536 vm_map_lock(map);
2537
2538 /*
2539 * handle wiring and unwiring separately.
2540 */
2541
2542 if (flags == 0) { /* unwire */
2543
2544 /*
2545 * POSIX 1003.1b -- munlockall unlocks all regions,
2546 * regardless of how many times mlockall has been called.
2547 */
2548
2549 for (entry = map->header.next; entry != &map->header;
2550 entry = entry->next) {
2551 if (VM_MAPENT_ISWIRED(entry))
2552 uvm_map_entry_unwire(map, entry);
2553 }
2554 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);
2555 vm_map_unlock(map);
2556 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
2557 return 0;
2558 }
2559
2560 if (flags & MCL_FUTURE) {
2561
2562 /*
2563 * must wire all future mappings; remember this.
2564 */
2565
2566 vm_map_modflags(map, VM_MAP_WIREFUTURE, 0);
2567 }
2568
2569 if ((flags & MCL_CURRENT) == 0) {
2570
2571 /*
2572 * no more work to do!
2573 */
2574
2575 UVMHIST_LOG(maphist,"<- done (OK no wire)",0,0,0,0);
2576 vm_map_unlock(map);
2577 return 0;
2578 }
2579
2580 /*
2581 * wire case: in three passes [XXXCDC: ugly block of code here]
2582 *
2583 * 1: holding the write lock, count all pages mapped by non-wired
2584 * entries. if this would cause us to go over our limit, we fail.
2585 *
2586 * 2: still holding the write lock, we create any anonymous maps that
2587 * need to be created. then we increment its wiring count.
2588 *
2589 * 3: we downgrade to a read lock, and call uvm_fault_wire to fault
2590 * in the pages for any newly wired area (wired_count == 1).
2591 *
2592 * downgrading to a read lock for uvm_fault_wire avoids a possible
2593 * deadlock with another thread that may have faulted on one of
2594 * the pages to be wired (it would mark the page busy, blocking
2595 * us, then in turn block on the map lock that we hold). because
2596 * of problems in the recursive lock package, we cannot upgrade
2597 * to a write lock in vm_map_lookup. thus, any actions that
2598 * require the write lock must be done beforehand. because we
2599 * keep the read lock on the map, the copy-on-write status of the
2600 * entries we modify here cannot change.
2601 */
2602
2603 for (size = 0, entry = map->header.next; entry != &map->header;
2604 entry = entry->next) {
2605 if (entry->protection != VM_PROT_NONE &&
2606 VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
2607 size += entry->end - entry->start;
2608 }
2609 }
2610
2611 if (atop(size) + uvmexp.wired > uvmexp.wiredmax) {
2612 vm_map_unlock(map);
2613 return ENOMEM;
2614 }
2615
2616 /* XXX non-pmap_wired_count case must be handled by caller */
2617 #ifdef pmap_wired_count
2618 if (limit != 0 &&
2619 (size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit)) {
2620 vm_map_unlock(map);
2621 return ENOMEM;
2622 }
2623 #endif
2624
2625 /*
2626 * Pass 2.
2627 */
2628
2629 for (entry = map->header.next; entry != &map->header;
2630 entry = entry->next) {
2631 if (entry->protection == VM_PROT_NONE)
2632 continue;
2633 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
2634
2635 /*
2636 * perform actions of vm_map_lookup that need the
2637 * write lock on the map: create an anonymous map
2638 * for a copy-on-write region, or an anonymous map
2639 * for a zero-fill region. (XXXCDC: submap case
2640 * ok?)
2641 */
2642
2643 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */
2644 if (UVM_ET_ISNEEDSCOPY(entry) &&
2645 ((entry->max_protection & VM_PROT_WRITE) ||
2646 (entry->object.uvm_obj == NULL))) {
2647 amap_copy(map, entry, M_WAITOK, TRUE,
2648 entry->start, entry->end);
2649 /* XXXCDC: wait OK? */
2650 }
2651 }
2652 }
2653 entry->wired_count++;
2654 }
2655
2656 /*
2657 * Pass 3.
2658 */
2659
2660 #ifdef DIAGNOSTIC
2661 timestamp_save = map->timestamp;
2662 #endif
2663 vm_map_busy(map);
2664 vm_map_downgrade(map);
2665
2666 rv = 0;
2667 for (entry = map->header.next; entry != &map->header;
2668 entry = entry->next) {
2669 if (entry->wired_count == 1) {
2670 rv = uvm_fault_wire(map, entry->start, entry->end,
2671 VM_FAULT_WIREMAX, entry->max_protection);
2672 if (rv) {
2673
2674 /*
2675 * wiring failed. break out of the loop.
2676 * we'll clean up the map below, once we
2677 * have a write lock again.
2678 */
2679
2680 break;
2681 }
2682 }
2683 }
2684
2685 if (rv) {
2686
2687 /*
2688 * Get back an exclusive (write) lock.
2689 */
2690
2691 vm_map_upgrade(map);
2692 vm_map_unbusy(map);
2693
2694 #ifdef DIAGNOSTIC
2695 if (timestamp_save != map->timestamp)
2696 panic("uvm_map_pageable_all: stale map");
2697 #endif
2698
2699 /*
2700 * first drop the wiring count on all the entries
2701 * which haven't actually been wired yet.
2702 *
2703 * Skip VM_PROT_NONE entries like we did above.
2704 */
2705
2706 failed_entry = entry;
2707 for (/* nothing */; entry != &map->header;
2708 entry = entry->next) {
2709 if (entry->protection == VM_PROT_NONE)
2710 continue;
2711 entry->wired_count--;
2712 }
2713
2714 /*
2715 * now, unwire all the entries that were successfully
2716 * wired above.
2717 *
2718 * Skip VM_PROT_NONE entries like we did above.
2719 */
2720
2721 for (entry = map->header.next; entry != failed_entry;
2722 entry = entry->next) {
2723 if (entry->protection == VM_PROT_NONE)
2724 continue;
2725 entry->wired_count--;
2726 if (VM_MAPENT_ISWIRED(entry))
2727 uvm_map_entry_unwire(map, entry);
2728 }
2729 vm_map_unlock(map);
2730 UVMHIST_LOG(maphist,"<- done (RV=%d)", rv,0,0,0);
2731 return (rv);
2732 }
2733
2734 /* We are holding a read lock here. */
2735 vm_map_unbusy(map);
2736 vm_map_unlock_read(map);
2737
2738 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
2739 return 0;
2740 }
2741
2742 /*
2743 * uvm_map_clean: clean out a map range
2744 *
2745 * => valid flags:
2746 * if (flags & PGO_CLEANIT): dirty pages are cleaned first
2747 * if (flags & PGO_SYNCIO): dirty pages are written synchronously
2748 * if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean
2749 * if (flags & PGO_FREE): any cached pages are freed after clean
2750 * => returns an error if any part of the specified range isn't mapped
2751 * => never a need to flush amap layer since the anonymous memory has
2752 * no permanent home, but may deactivate pages there
2753 * => called from sys_msync() and sys_madvise()
2754 * => caller must not write-lock map (read OK).
2755 * => we may sleep while cleaning if SYNCIO [with map read-locked]
2756 */
2757
2758 int
2759 uvm_map_clean(map, start, end, flags)
2760 struct vm_map *map;
2761 vaddr_t start, end;
2762 int flags;
2763 {
2764 struct vm_map_entry *current, *entry;
2765 struct uvm_object *uobj;
2766 struct vm_amap *amap;
2767 struct vm_anon *anon;
2768 struct vm_page *pg;
2769 vaddr_t offset;
2770 vsize_t size;
2771 int error, refs;
2772 UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist);
2773
2774 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,flags=0x%x)",
2775 map, start, end, flags);
2776 KASSERT((flags & (PGO_FREE|PGO_DEACTIVATE)) !=
2777 (PGO_FREE|PGO_DEACTIVATE));
2778
2779 vm_map_lock_read(map);
2780 VM_MAP_RANGE_CHECK(map, start, end);
2781 if (uvm_map_lookup_entry(map, start, &entry) == FALSE) {
2782 vm_map_unlock_read(map);
2783 return EFAULT;
2784 }
2785
2786 /*
2787 * Make a first pass to check for holes.
2788 */
2789
2790 for (current = entry; current->start < end; current = current->next) {
2791 if (UVM_ET_ISSUBMAP(current)) {
2792 vm_map_unlock_read(map);
2793 return EINVAL;
2794 }
2795 if (end <= current->end) {
2796 break;
2797 }
2798 if (current->end != current->next->start) {
2799 vm_map_unlock_read(map);
2800 return EFAULT;
2801 }
2802 }
2803
2804 error = 0;
2805 for (current = entry; start < end; current = current->next) {
2806 amap = current->aref.ar_amap; /* top layer */
2807 uobj = current->object.uvm_obj; /* bottom layer */
2808 KASSERT(start >= current->start);
2809
2810 /*
2811 * No amap cleaning necessary if:
2812 *
2813 * (1) There's no amap.
2814 *
2815 * (2) We're not deactivating or freeing pages.
2816 */
2817
2818 if (amap == NULL || (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0)
2819 goto flush_object;
2820
2821 amap_lock(amap);
2822 offset = start - current->start;
2823 size = MIN(end, current->end) - start;
2824 for ( ; size != 0; size -= PAGE_SIZE, offset += PAGE_SIZE) {
2825 anon = amap_lookup(¤t->aref, offset);
2826 if (anon == NULL)
2827 continue;
2828
2829 simple_lock(&anon->an_lock);
2830 pg = anon->u.an_page;
2831 if (pg == NULL) {
2832 simple_unlock(&anon->an_lock);
2833 continue;
2834 }
2835
2836 switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) {
2837
2838 /*
2839 * In these first 3 cases, we just deactivate the page.
2840 */
2841
2842 case PGO_CLEANIT|PGO_FREE:
2843 case PGO_CLEANIT|PGO_DEACTIVATE:
2844 case PGO_DEACTIVATE:
2845 deactivate_it:
2846 /*
2847 * skip the page if it's loaned or wired,
2848 * since it shouldn't be on a paging queue
2849 * at all in these cases.
2850 */
2851
2852 uvm_lock_pageq();
2853 if (pg->loan_count != 0 ||
2854 pg->wire_count != 0) {
2855 uvm_unlock_pageq();
2856 simple_unlock(&anon->an_lock);
2857 continue;
2858 }
2859 KASSERT(pg->uanon == anon);
2860 pmap_clear_reference(pg);
2861 uvm_pagedeactivate(pg);
2862 uvm_unlock_pageq();
2863 simple_unlock(&anon->an_lock);
2864 continue;
2865
2866 case PGO_FREE:
2867
2868 /*
2869 * If there are multiple references to
2870 * the amap, just deactivate the page.
2871 */
2872
2873 if (amap_refs(amap) > 1)
2874 goto deactivate_it;
2875
2876 /* skip the page if it's wired */
2877 if (pg->wire_count != 0) {
2878 simple_unlock(&anon->an_lock);
2879 continue;
2880 }
2881 amap_unadd(¤t->aref, offset);
2882 refs = --anon->an_ref;
2883 simple_unlock(&anon->an_lock);
2884 if (refs == 0)
2885 uvm_anfree(anon);
2886 continue;
2887 }
2888 }
2889 amap_unlock(amap);
2890
2891 flush_object:
2892 /*
2893 * flush pages if we've got a valid backing object.
2894 * note that we must always clean object pages before
2895 * freeing them since otherwise we could reveal stale
2896 * data from files.
2897 */
2898
2899 offset = current->offset + (start - current->start);
2900 size = MIN(end, current->end) - start;
2901 if (uobj != NULL) {
2902 simple_lock(&uobj->vmobjlock);
2903 error = (uobj->pgops->pgo_put)(uobj, offset,
2904 offset + size, flags | PGO_CLEANIT);
2905 }
2906 start += size;
2907 }
2908 vm_map_unlock_read(map);
2909 return (error);
2910 }
2911
2912
2913 /*
2914 * uvm_map_checkprot: check protection in map
2915 *
2916 * => must allow specified protection in a fully allocated region.
2917 * => map must be read or write locked by caller.
2918 */
2919
2920 boolean_t
2921 uvm_map_checkprot(map, start, end, protection)
2922 struct vm_map * map;
2923 vaddr_t start, end;
2924 vm_prot_t protection;
2925 {
2926 struct vm_map_entry *entry;
2927 struct vm_map_entry *tmp_entry;
2928
2929 if (!uvm_map_lookup_entry(map, start, &tmp_entry)) {
2930 return(FALSE);
2931 }
2932 entry = tmp_entry;
2933 while (start < end) {
2934 if (entry == &map->header) {
2935 return(FALSE);
2936 }
2937
2938 /*
2939 * no holes allowed
2940 */
2941
2942 if (start < entry->start) {
2943 return(FALSE);
2944 }
2945
2946 /*
2947 * check protection associated with entry
2948 */
2949
2950 if ((entry->protection & protection) != protection) {
2951 return(FALSE);
2952 }
2953 start = entry->end;
2954 entry = entry->next;
2955 }
2956 return(TRUE);
2957 }
2958
2959 /*
2960 * uvmspace_alloc: allocate a vmspace structure.
2961 *
2962 * - structure includes vm_map and pmap
2963 * - XXX: no locking on this structure
2964 * - refcnt set to 1, rest must be init'd by caller
2965 */
2966 struct vmspace *
2967 uvmspace_alloc(min, max)
2968 vaddr_t min, max;
2969 {
2970 struct vmspace *vm;
2971 UVMHIST_FUNC("uvmspace_alloc"); UVMHIST_CALLED(maphist);
2972
2973 vm = pool_get(&uvm_vmspace_pool, PR_WAITOK);
2974 uvmspace_init(vm, NULL, min, max);
2975 UVMHIST_LOG(maphist,"<- done (vm=0x%x)", vm,0,0,0);
2976 return (vm);
2977 }
2978
2979 /*
2980 * uvmspace_init: initialize a vmspace structure.
2981 *
2982 * - XXX: no locking on this structure
2983 * - refcnt set to 1, rest must be init'd by caller
2984 */
2985 void
2986 uvmspace_init(vm, pmap, min, max)
2987 struct vmspace *vm;
2988 struct pmap *pmap;
2989 vaddr_t min, max;
2990 {
2991 UVMHIST_FUNC("uvmspace_init"); UVMHIST_CALLED(maphist);
2992
2993 memset(vm, 0, sizeof(*vm));
2994 uvm_map_setup(&vm->vm_map, min, max, VM_MAP_PAGEABLE
2995 #ifdef __USING_TOPDOWN_VM
2996 | VM_MAP_TOPDOWN
2997 #endif
2998 );
2999 if (pmap)
3000 pmap_reference(pmap);
3001 else
3002 pmap = pmap_create();
3003 vm->vm_map.pmap = pmap;
3004 vm->vm_refcnt = 1;
3005 UVMHIST_LOG(maphist,"<- done",0,0,0,0);
3006 }
3007
3008 /*
3009 * uvmspace_share: share a vmspace between two proceses
3010 *
3011 * - XXX: no locking on vmspace
3012 * - used for vfork, threads(?)
3013 */
3014
3015 void
3016 uvmspace_share(p1, p2)
3017 struct proc *p1, *p2;
3018 {
3019 p2->p_vmspace = p1->p_vmspace;
3020 p1->p_vmspace->vm_refcnt++;
3021 }
3022
3023 /*
3024 * uvmspace_unshare: ensure that process "p" has its own, unshared, vmspace
3025 *
3026 * - XXX: no locking on vmspace
3027 */
3028
3029 void
3030 uvmspace_unshare(l)
3031 struct lwp *l;
3032 {
3033 struct proc *p = l->l_proc;
3034 struct vmspace *nvm, *ovm = p->p_vmspace;
3035
3036 if (ovm->vm_refcnt == 1)
3037 /* nothing to do: vmspace isn't shared in the first place */
3038 return;
3039
3040 /* make a new vmspace, still holding old one */
3041 nvm = uvmspace_fork(ovm);
3042
3043 pmap_deactivate(l); /* unbind old vmspace */
3044 p->p_vmspace = nvm;
3045 pmap_activate(l); /* switch to new vmspace */
3046
3047 uvmspace_free(ovm); /* drop reference to old vmspace */
3048 }
3049
3050 /*
3051 * uvmspace_exec: the process wants to exec a new program
3052 *
3053 * - XXX: no locking on vmspace
3054 */
3055
3056 void
3057 uvmspace_exec(l, start, end)
3058 struct lwp *l;
3059 vaddr_t start, end;
3060 {
3061 struct proc *p = l->l_proc;
3062 struct vmspace *nvm, *ovm = p->p_vmspace;
3063 struct vm_map *map = &ovm->vm_map;
3064
3065 #ifdef __sparc__
3066 /* XXX cgd 960926: the sparc #ifdef should be a MD hook */
3067 kill_user_windows(l); /* before stack addresses go away */
3068 #endif
3069
3070 /*
3071 * see if more than one process is using this vmspace...
3072 */
3073
3074 if (ovm->vm_refcnt == 1) {
3075
3076 /*
3077 * if p is the only process using its vmspace then we can safely
3078 * recycle that vmspace for the program that is being exec'd.
3079 */
3080
3081 #ifdef SYSVSHM
3082 /*
3083 * SYSV SHM semantics require us to kill all segments on an exec
3084 */
3085
3086 if (ovm->vm_shm)
3087 shmexit(ovm);
3088 #endif
3089
3090 /*
3091 * POSIX 1003.1b -- "lock future mappings" is revoked
3092 * when a process execs another program image.
3093 */
3094
3095 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);
3096
3097 /*
3098 * now unmap the old program
3099 */
3100
3101 pmap_remove_all(map->pmap);
3102 uvm_unmap(map, map->min_offset, map->max_offset);
3103
3104 /*
3105 * resize the map
3106 */
3107
3108 map->min_offset = start;
3109 map->max_offset = end;
3110 } else {
3111
3112 /*
3113 * p's vmspace is being shared, so we can't reuse it for p since
3114 * it is still being used for others. allocate a new vmspace
3115 * for p
3116 */
3117
3118 nvm = uvmspace_alloc(start, end);
3119
3120 /*
3121 * install new vmspace and drop our ref to the old one.
3122 */
3123
3124 pmap_deactivate(l);
3125 p->p_vmspace = nvm;
3126 pmap_activate(l);
3127
3128 uvmspace_free(ovm);
3129 }
3130 }
3131
3132 /*
3133 * uvmspace_free: free a vmspace data structure
3134 *
3135 * - XXX: no locking on vmspace
3136 */
3137
3138 void
3139 uvmspace_free(vm)
3140 struct vmspace *vm;
3141 {
3142 struct vm_map_entry *dead_entries;
3143 struct vm_map *map;
3144 UVMHIST_FUNC("uvmspace_free"); UVMHIST_CALLED(maphist);
3145
3146 UVMHIST_LOG(maphist,"(vm=0x%x) ref=%d", vm, vm->vm_refcnt,0,0);
3147 if (--vm->vm_refcnt > 0) {
3148 return;
3149 }
3150
3151 /*
3152 * at this point, there should be no other references to the map.
3153 * delete all of the mappings, then destroy the pmap.
3154 */
3155
3156 map = &vm->vm_map;
3157 map->flags |= VM_MAP_DYING;
3158 pmap_remove_all(map->pmap);
3159 #ifdef SYSVSHM
3160 /* Get rid of any SYSV shared memory segments. */
3161 if (vm->vm_shm != NULL)
3162 shmexit(vm);
3163 #endif
3164 if (map->nentries) {
3165 uvm_unmap_remove(map, map->min_offset, map->max_offset,
3166 &dead_entries);
3167 if (dead_entries != NULL)
3168 uvm_unmap_detach(dead_entries, 0);
3169 }
3170 pmap_destroy(map->pmap);
3171 pool_put(&uvm_vmspace_pool, vm);
3172 }
3173
3174 /*
3175 * F O R K - m a i n e n t r y p o i n t
3176 */
3177 /*
3178 * uvmspace_fork: fork a process' main map
3179 *
3180 * => create a new vmspace for child process from parent.
3181 * => parent's map must not be locked.
3182 */
3183
3184 struct vmspace *
3185 uvmspace_fork(vm1)
3186 struct vmspace *vm1;
3187 {
3188 struct vmspace *vm2;
3189 struct vm_map *old_map = &vm1->vm_map;
3190 struct vm_map *new_map;
3191 struct vm_map_entry *old_entry;
3192 struct vm_map_entry *new_entry;
3193 UVMHIST_FUNC("uvmspace_fork"); UVMHIST_CALLED(maphist);
3194
3195 vm_map_lock(old_map);
3196
3197 vm2 = uvmspace_alloc(old_map->min_offset, old_map->max_offset);
3198 memcpy(&vm2->vm_startcopy, &vm1->vm_startcopy,
3199 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
3200 new_map = &vm2->vm_map; /* XXX */
3201
3202 old_entry = old_map->header.next;
3203
3204 /*
3205 * go entry-by-entry
3206 */
3207
3208 while (old_entry != &old_map->header) {
3209
3210 /*
3211 * first, some sanity checks on the old entry
3212 */
3213
3214 KASSERT(!UVM_ET_ISSUBMAP(old_entry));
3215 KASSERT(UVM_ET_ISCOPYONWRITE(old_entry) ||
3216 !UVM_ET_ISNEEDSCOPY(old_entry));
3217
3218 switch (old_entry->inheritance) {
3219 case MAP_INHERIT_NONE:
3220
3221 /*
3222 * drop the mapping
3223 */
3224
3225 break;
3226
3227 case MAP_INHERIT_SHARE:
3228
3229 /*
3230 * share the mapping: this means we want the old and
3231 * new entries to share amaps and backing objects.
3232 */
3233 /*
3234 * if the old_entry needs a new amap (due to prev fork)
3235 * then we need to allocate it now so that we have
3236 * something we own to share with the new_entry. [in
3237 * other words, we need to clear needs_copy]
3238 */
3239
3240 if (UVM_ET_ISNEEDSCOPY(old_entry)) {
3241 /* get our own amap, clears needs_copy */
3242 amap_copy(old_map, old_entry, M_WAITOK, FALSE,
3243 0, 0);
3244 /* XXXCDC: WAITOK??? */
3245 }
3246
3247 new_entry = uvm_mapent_alloc(new_map, 0);
3248 /* old_entry -> new_entry */
3249 uvm_mapent_copy(old_entry, new_entry);
3250
3251 /* new pmap has nothing wired in it */
3252 new_entry->wired_count = 0;
3253
3254 /*
3255 * gain reference to object backing the map (can't
3256 * be a submap, already checked this case).
3257 */
3258
3259 if (new_entry->aref.ar_amap)
3260 uvm_map_reference_amap(new_entry, AMAP_SHARED);
3261
3262 if (new_entry->object.uvm_obj &&
3263 new_entry->object.uvm_obj->pgops->pgo_reference)
3264 new_entry->object.uvm_obj->
3265 pgops->pgo_reference(
3266 new_entry->object.uvm_obj);
3267
3268 /* insert entry at end of new_map's entry list */
3269 uvm_map_entry_link(new_map, new_map->header.prev,
3270 new_entry);
3271
3272 break;
3273
3274 case MAP_INHERIT_COPY:
3275
3276 /*
3277 * copy-on-write the mapping (using mmap's
3278 * MAP_PRIVATE semantics)
3279 *
3280 * allocate new_entry, adjust reference counts.
3281 * (note that new references are read-only).
3282 */
3283
3284 new_entry = uvm_mapent_alloc(new_map, 0);
3285 /* old_entry -> new_entry */
3286 uvm_mapent_copy(old_entry, new_entry);
3287
3288 if (new_entry->aref.ar_amap)
3289 uvm_map_reference_amap(new_entry, 0);
3290
3291 if (new_entry->object.uvm_obj &&
3292 new_entry->object.uvm_obj->pgops->pgo_reference)
3293 new_entry->object.uvm_obj->pgops->pgo_reference
3294 (new_entry->object.uvm_obj);
3295
3296 /* new pmap has nothing wired in it */
3297 new_entry->wired_count = 0;
3298
3299 new_entry->etype |=
3300 (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY);
3301 uvm_map_entry_link(new_map, new_map->header.prev,
3302 new_entry);
3303
3304 /*
3305 * the new entry will need an amap. it will either
3306 * need to be copied from the old entry or created
3307 * from scratch (if the old entry does not have an
3308 * amap). can we defer this process until later
3309 * (by setting "needs_copy") or do we need to copy
3310 * the amap now?
3311 *
3312 * we must copy the amap now if any of the following
3313 * conditions hold:
3314 * 1. the old entry has an amap and that amap is
3315 * being shared. this means that the old (parent)
3316 * process is sharing the amap with another
3317 * process. if we do not clear needs_copy here
3318 * we will end up in a situation where both the
3319 * parent and child process are refering to the
3320 * same amap with "needs_copy" set. if the
3321 * parent write-faults, the fault routine will
3322 * clear "needs_copy" in the parent by allocating
3323 * a new amap. this is wrong because the
3324 * parent is supposed to be sharing the old amap
3325 * and the new amap will break that.
3326 *
3327 * 2. if the old entry has an amap and a non-zero
3328 * wire count then we are going to have to call
3329 * amap_cow_now to avoid page faults in the
3330 * parent process. since amap_cow_now requires
3331 * "needs_copy" to be clear we might as well
3332 * clear it here as well.
3333 *
3334 */
3335
3336 if (old_entry->aref.ar_amap != NULL) {
3337 if ((amap_flags(old_entry->aref.ar_amap) &
3338 AMAP_SHARED) != 0 ||
3339 VM_MAPENT_ISWIRED(old_entry)) {
3340
3341 amap_copy(new_map, new_entry, M_WAITOK,
3342 FALSE, 0, 0);
3343 /* XXXCDC: M_WAITOK ... ok? */
3344 }
3345 }
3346
3347 /*
3348 * if the parent's entry is wired down, then the
3349 * parent process does not want page faults on
3350 * access to that memory. this means that we
3351 * cannot do copy-on-write because we can't write
3352 * protect the old entry. in this case we
3353 * resolve all copy-on-write faults now, using
3354 * amap_cow_now. note that we have already
3355 * allocated any needed amap (above).
3356 */
3357
3358 if (VM_MAPENT_ISWIRED(old_entry)) {
3359
3360 /*
3361 * resolve all copy-on-write faults now
3362 * (note that there is nothing to do if
3363 * the old mapping does not have an amap).
3364 */
3365 if (old_entry->aref.ar_amap)
3366 amap_cow_now(new_map, new_entry);
3367
3368 } else {
3369
3370 /*
3371 * setup mappings to trigger copy-on-write faults
3372 * we must write-protect the parent if it has
3373 * an amap and it is not already "needs_copy"...
3374 * if it is already "needs_copy" then the parent
3375 * has already been write-protected by a previous
3376 * fork operation.
3377 */
3378
3379 if (old_entry->aref.ar_amap &&
3380 !UVM_ET_ISNEEDSCOPY(old_entry)) {
3381 if (old_entry->max_protection & VM_PROT_WRITE) {
3382 pmap_protect(old_map->pmap,
3383 old_entry->start,
3384 old_entry->end,
3385 old_entry->protection &
3386 ~VM_PROT_WRITE);
3387 pmap_update(old_map->pmap);
3388 }
3389 old_entry->etype |= UVM_ET_NEEDSCOPY;
3390 }
3391 }
3392 break;
3393 } /* end of switch statement */
3394 old_entry = old_entry->next;
3395 }
3396
3397 new_map->size = old_map->size;
3398 vm_map_unlock(old_map);
3399
3400 #ifdef SYSVSHM
3401 if (vm1->vm_shm)
3402 shmfork(vm1, vm2);
3403 #endif
3404
3405 #ifdef PMAP_FORK
3406 pmap_fork(vm1->vm_map.pmap, vm2->vm_map.pmap);
3407 #endif
3408
3409 UVMHIST_LOG(maphist,"<- done",0,0,0,0);
3410 return(vm2);
3411 }
3412
3413
3414 #if defined(DDB)
3415
3416 /*
3417 * DDB hooks
3418 */
3419
3420 /*
3421 * uvm_map_printit: actually prints the map
3422 */
3423
3424 void
3425 uvm_map_printit(map, full, pr)
3426 struct vm_map *map;
3427 boolean_t full;
3428 void (*pr) __P((const char *, ...));
3429 {
3430 struct vm_map_entry *entry;
3431
3432 (*pr)("MAP %p: [0x%lx->0x%lx]\n", map, map->min_offset,map->max_offset);
3433 (*pr)("\t#ent=%d, sz=%d, ref=%d, version=%d, flags=0x%x\n",
3434 map->nentries, map->size, map->ref_count, map->timestamp,
3435 map->flags);
3436 (*pr)("\tpmap=%p(resident=%d)\n", map->pmap,
3437 pmap_resident_count(map->pmap));
3438 if (!full)
3439 return;
3440 for (entry = map->header.next; entry != &map->header;
3441 entry = entry->next) {
3442 (*pr)(" - %p: 0x%lx->0x%lx: obj=%p/0x%llx, amap=%p/%d\n",
3443 entry, entry->start, entry->end, entry->object.uvm_obj,
3444 (long long)entry->offset, entry->aref.ar_amap,
3445 entry->aref.ar_pageoff);
3446 (*pr)(
3447 "\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, "
3448 "wc=%d, adv=%d\n",
3449 (entry->etype & UVM_ET_SUBMAP) ? 'T' : 'F',
3450 (entry->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F',
3451 (entry->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F',
3452 entry->protection, entry->max_protection,
3453 entry->inheritance, entry->wired_count, entry->advice);
3454 }
3455 }
3456
3457 /*
3458 * uvm_object_printit: actually prints the object
3459 */
3460
3461 void
3462 uvm_object_printit(uobj, full, pr)
3463 struct uvm_object *uobj;
3464 boolean_t full;
3465 void (*pr) __P((const char *, ...));
3466 {
3467 struct vm_page *pg;
3468 int cnt = 0;
3469
3470 (*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ",
3471 uobj, uobj->vmobjlock.lock_data, uobj->pgops, uobj->uo_npages);
3472 if (UVM_OBJ_IS_KERN_OBJECT(uobj))
3473 (*pr)("refs=<SYSTEM>\n");
3474 else
3475 (*pr)("refs=%d\n", uobj->uo_refs);
3476
3477 if (!full) {
3478 return;
3479 }
3480 (*pr)(" PAGES <pg,offset>:\n ");
3481 TAILQ_FOREACH(pg, &uobj->memq, listq) {
3482 cnt++;
3483 (*pr)("<%p,0x%llx> ", pg, (long long)pg->offset);
3484 if ((cnt % 3) == 0) {
3485 (*pr)("\n ");
3486 }
3487 }
3488 if ((cnt % 3) != 0) {
3489 (*pr)("\n");
3490 }
3491 }
3492
3493 /*
3494 * uvm_page_printit: actually print the page
3495 */
3496
3497 static const char page_flagbits[] =
3498 "\20\1BUSY\2WANTED\3TABLED\4CLEAN\5PAGEOUT\6RELEASED\7FAKE\10RDONLY"
3499 "\11ZERO\15PAGER1";
3500 static const char page_pqflagbits[] =
3501 "\20\1FREE\2INACTIVE\3ACTIVE\5ANON\6AOBJ";
3502
3503 void
3504 uvm_page_printit(pg, full, pr)
3505 struct vm_page *pg;
3506 boolean_t full;
3507 void (*pr) __P((const char *, ...));
3508 {
3509 struct vm_page *tpg;
3510 struct uvm_object *uobj;
3511 struct pglist *pgl;
3512 char pgbuf[128];
3513 char pqbuf[128];
3514
3515 (*pr)("PAGE %p:\n", pg);
3516 bitmask_snprintf(pg->flags, page_flagbits, pgbuf, sizeof(pgbuf));
3517 bitmask_snprintf(pg->pqflags, page_pqflagbits, pqbuf, sizeof(pqbuf));
3518 (*pr)(" flags=%s, pqflags=%s, wire_count=%d, pa=0x%lx\n",
3519 pgbuf, pqbuf, pg->wire_count, (long)pg->phys_addr);
3520 (*pr)(" uobject=%p, uanon=%p, offset=0x%llx loan_count=%d\n",
3521 pg->uobject, pg->uanon, (long long)pg->offset, pg->loan_count);
3522 #if defined(UVM_PAGE_TRKOWN)
3523 if (pg->flags & PG_BUSY)
3524 (*pr)(" owning process = %d, tag=%s\n",
3525 pg->owner, pg->owner_tag);
3526 else
3527 (*pr)(" page not busy, no owner\n");
3528 #else
3529 (*pr)(" [page ownership tracking disabled]\n");
3530 #endif
3531
3532 if (!full)
3533 return;
3534
3535 /* cross-verify object/anon */
3536 if ((pg->pqflags & PQ_FREE) == 0) {
3537 if (pg->pqflags & PQ_ANON) {
3538 if (pg->uanon == NULL || pg->uanon->u.an_page != pg)
3539 (*pr)(" >>> ANON DOES NOT POINT HERE <<< (%p)\n",
3540 (pg->uanon) ? pg->uanon->u.an_page : NULL);
3541 else
3542 (*pr)(" anon backpointer is OK\n");
3543 } else {
3544 uobj = pg->uobject;
3545 if (uobj) {
3546 (*pr)(" checking object list\n");
3547 TAILQ_FOREACH(tpg, &uobj->memq, listq) {
3548 if (tpg == pg) {
3549 break;
3550 }
3551 }
3552 if (tpg)
3553 (*pr)(" page found on object list\n");
3554 else
3555 (*pr)(" >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n");
3556 }
3557 }
3558 }
3559
3560 /* cross-verify page queue */
3561 if (pg->pqflags & PQ_FREE) {
3562 int fl = uvm_page_lookup_freelist(pg);
3563 int color = VM_PGCOLOR_BUCKET(pg);
3564 pgl = &uvm.page_free[fl].pgfl_buckets[color].pgfl_queues[
3565 ((pg)->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN];
3566 } else if (pg->pqflags & PQ_INACTIVE) {
3567 pgl = &uvm.page_inactive;
3568 } else if (pg->pqflags & PQ_ACTIVE) {
3569 pgl = &uvm.page_active;
3570 } else {
3571 pgl = NULL;
3572 }
3573
3574 if (pgl) {
3575 (*pr)(" checking pageq list\n");
3576 TAILQ_FOREACH(tpg, pgl, pageq) {
3577 if (tpg == pg) {
3578 break;
3579 }
3580 }
3581 if (tpg)
3582 (*pr)(" page found on pageq list\n");
3583 else
3584 (*pr)(" >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n");
3585 }
3586 }
3587 #endif
3588