uvm_map.c revision 1.37 1 /* $NetBSD: uvm_map.c,v 1.37 1999/04/19 14:43:46 chs 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 #include "opt_ddb.h"
70 #include "opt_uvmhist.h"
71 #include "opt_pmap_new.h"
72 #include "opt_sysv.h"
73
74 /*
75 * uvm_map.c: uvm map operations
76 */
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/mman.h>
81 #include <sys/proc.h>
82 #include <sys/malloc.h>
83 #include <sys/pool.h>
84
85 #ifdef SYSVSHM
86 #include <sys/shm.h>
87 #endif
88
89 #include <vm/vm.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_kern.h>
92
93 #define UVM_MAP
94 #include <uvm/uvm.h>
95
96 #ifdef DDB
97 #include <uvm/uvm_ddb.h>
98 #endif
99
100
101 struct uvm_cnt uvm_map_call, map_backmerge, map_forwmerge;
102 struct uvm_cnt uvm_mlk_call, uvm_mlk_hint;
103
104 /*
105 * pool for vmspace structures.
106 */
107
108 struct pool uvm_vmspace_pool;
109
110 /*
111 * pool for dynamically-allocated map entries.
112 */
113
114 struct pool uvm_map_entry_pool;
115
116 /*
117 * macros
118 */
119
120 /*
121 * uvm_map_entry_link: insert entry into a map
122 *
123 * => map must be locked
124 */
125 #define uvm_map_entry_link(map, after_where, entry) do { \
126 (map)->nentries++; \
127 (entry)->prev = (after_where); \
128 (entry)->next = (after_where)->next; \
129 (entry)->prev->next = (entry); \
130 (entry)->next->prev = (entry); \
131 } while (0)
132
133 /*
134 * uvm_map_entry_unlink: remove entry from a map
135 *
136 * => map must be locked
137 */
138 #define uvm_map_entry_unlink(map, entry) do { \
139 (map)->nentries--; \
140 (entry)->next->prev = (entry)->prev; \
141 (entry)->prev->next = (entry)->next; \
142 } while (0)
143
144 /*
145 * SAVE_HINT: saves the specified entry as the hint for future lookups.
146 *
147 * => map need not be locked (protected by hint_lock).
148 */
149 #define SAVE_HINT(map,value) do { \
150 simple_lock(&(map)->hint_lock); \
151 (map)->hint = (value); \
152 simple_unlock(&(map)->hint_lock); \
153 } while (0)
154
155 /*
156 * VM_MAP_RANGE_CHECK: check and correct range
157 *
158 * => map must at least be read locked
159 */
160
161 #define VM_MAP_RANGE_CHECK(map, start, end) do { \
162 if (start < vm_map_min(map)) \
163 start = vm_map_min(map); \
164 if (end > vm_map_max(map)) \
165 end = vm_map_max(map); \
166 if (start > end) \
167 start = end; \
168 } while (0)
169
170 /*
171 * local prototypes
172 */
173
174 static vm_map_entry_t uvm_mapent_alloc __P((vm_map_t));
175 static void uvm_mapent_copy __P((vm_map_entry_t,vm_map_entry_t));
176 static void uvm_mapent_free __P((vm_map_entry_t));
177 static void uvm_map_entry_unwire __P((vm_map_t, vm_map_entry_t));
178
179 /*
180 * local inlines
181 */
182
183 /*
184 * uvm_mapent_alloc: allocate a map entry
185 *
186 * => XXX: static pool for kernel map?
187 */
188
189 static __inline vm_map_entry_t
190 uvm_mapent_alloc(map)
191 vm_map_t map;
192 {
193 vm_map_entry_t me;
194 int s;
195 UVMHIST_FUNC("uvm_mapent_alloc");
196 UVMHIST_CALLED(maphist);
197
198 if (map->entries_pageable) {
199 me = pool_get(&uvm_map_entry_pool, PR_WAITOK);
200 me->flags = 0;
201 /* me can't be null, wait ok */
202
203 } else {
204 s = splimp(); /* protect kentry_free list with splimp */
205 simple_lock(&uvm.kentry_lock);
206 me = uvm.kentry_free;
207 if (me) uvm.kentry_free = me->next;
208 simple_unlock(&uvm.kentry_lock);
209 splx(s);
210 if (!me)
211 panic("mapent_alloc: out of kernel map entries, check MAX_KMAPENT");
212 me->flags = UVM_MAP_STATIC;
213 }
214
215 UVMHIST_LOG(maphist, "<- new entry=0x%x [pageable=%d]",
216 me, map->entries_pageable, 0, 0);
217 return(me);
218
219 }
220
221 /*
222 * uvm_mapent_free: free map entry
223 *
224 * => XXX: static pool for kernel map?
225 */
226
227 static __inline void
228 uvm_mapent_free(me)
229 vm_map_entry_t me;
230 {
231 int s;
232 UVMHIST_FUNC("uvm_mapent_free");
233 UVMHIST_CALLED(maphist);
234 UVMHIST_LOG(maphist,"<- freeing map entry=0x%x [flags=%d]",
235 me, me->flags, 0, 0);
236 if ((me->flags & UVM_MAP_STATIC) == 0) {
237 pool_put(&uvm_map_entry_pool, me);
238 } else {
239 s = splimp(); /* protect kentry_free list with splimp */
240 simple_lock(&uvm.kentry_lock);
241 me->next = uvm.kentry_free;
242 uvm.kentry_free = me;
243 simple_unlock(&uvm.kentry_lock);
244 splx(s);
245 }
246 }
247
248 /*
249 * uvm_mapent_copy: copy a map entry, preserving flags
250 */
251
252 static __inline void
253 uvm_mapent_copy(src, dst)
254 vm_map_entry_t src;
255 vm_map_entry_t dst;
256 {
257
258 memcpy(dst, src, ((char *)&src->uvm_map_entry_stop_copy) - ((char*)src));
259 }
260
261 /*
262 * uvm_map_entry_unwire: unwire a map entry
263 *
264 * => map should be locked by caller
265 */
266
267 static __inline void
268 uvm_map_entry_unwire(map, entry)
269 vm_map_t map;
270 vm_map_entry_t entry;
271 {
272
273 uvm_fault_unwire(map->pmap, entry->start, entry->end);
274 entry->wired_count = 0;
275 }
276
277 /*
278 * uvm_map_init: init mapping system at boot time. note that we allocate
279 * and init the static pool of vm_map_entry_t's for the kernel here.
280 */
281
282 void
283 uvm_map_init()
284 {
285 static struct vm_map_entry kernel_map_entry[MAX_KMAPENT];
286 #if defined(UVMHIST)
287 static struct uvm_history_ent maphistbuf[100];
288 static struct uvm_history_ent pdhistbuf[100];
289 #endif
290 int lcv;
291
292 /*
293 * first, init logging system.
294 */
295
296 UVMHIST_FUNC("uvm_map_init");
297 UVMHIST_INIT_STATIC(maphist, maphistbuf);
298 UVMHIST_INIT_STATIC(pdhist, pdhistbuf);
299 UVMHIST_CALLED(maphist);
300 UVMHIST_LOG(maphist,"<starting uvm map system>", 0, 0, 0, 0);
301 UVMCNT_INIT(uvm_map_call, UVMCNT_CNT, 0,
302 "# uvm_map() successful calls", 0);
303 UVMCNT_INIT(map_backmerge, UVMCNT_CNT, 0, "# uvm_map() back merges", 0);
304 UVMCNT_INIT(map_forwmerge, UVMCNT_CNT, 0, "# uvm_map() missed forward",
305 0);
306 UVMCNT_INIT(uvm_mlk_call, UVMCNT_CNT, 0, "# map lookup calls", 0);
307 UVMCNT_INIT(uvm_mlk_hint, UVMCNT_CNT, 0, "# map lookup hint hits", 0);
308
309 /*
310 * now set up static pool of kernel map entrys ...
311 */
312
313 simple_lock_init(&uvm.kentry_lock);
314 uvm.kentry_free = NULL;
315 for (lcv = 0 ; lcv < MAX_KMAPENT ; lcv++) {
316 kernel_map_entry[lcv].next = uvm.kentry_free;
317 uvm.kentry_free = &kernel_map_entry[lcv];
318 }
319
320 /*
321 * initialize the map-related pools.
322 */
323 pool_init(&uvm_vmspace_pool, sizeof(struct vmspace),
324 0, 0, 0, "vmsppl", 0,
325 pool_page_alloc_nointr, pool_page_free_nointr, M_VMMAP);
326 pool_init(&uvm_map_entry_pool, sizeof(struct vm_map_entry),
327 0, 0, 0, "vmmpepl", 0,
328 pool_page_alloc_nointr, pool_page_free_nointr, M_VMMAP);
329 }
330
331 /*
332 * clippers
333 */
334
335 /*
336 * uvm_map_clip_start: ensure that the entry begins at or after
337 * the starting address, if it doesn't we split the entry.
338 *
339 * => caller should use UVM_MAP_CLIP_START macro rather than calling
340 * this directly
341 * => map must be locked by caller
342 */
343
344 void uvm_map_clip_start(map, entry, start)
345 vm_map_t map;
346 vm_map_entry_t entry;
347 vaddr_t start;
348 {
349 vm_map_entry_t new_entry;
350 vaddr_t new_adj;
351
352 /* uvm_map_simplify_entry(map, entry); */ /* XXX */
353
354 /*
355 * Split off the front portion. note that we must insert the new
356 * entry BEFORE this one, so that this entry has the specified
357 * starting address.
358 */
359
360 new_entry = uvm_mapent_alloc(map);
361 uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
362
363 new_entry->end = start;
364 new_adj = start - new_entry->start;
365 if (entry->object.uvm_obj)
366 entry->offset += new_adj; /* shift start over */
367 entry->start = start;
368
369 if (new_entry->aref.ar_amap) {
370 amap_splitref(&new_entry->aref, &entry->aref, new_adj);
371 }
372
373 uvm_map_entry_link(map, entry->prev, new_entry);
374
375 if (UVM_ET_ISSUBMAP(entry)) {
376 /* ... unlikely to happen, but play it safe */
377 uvm_map_reference(new_entry->object.sub_map);
378 } else {
379 if (UVM_ET_ISOBJ(entry) &&
380 entry->object.uvm_obj->pgops &&
381 entry->object.uvm_obj->pgops->pgo_reference)
382 entry->object.uvm_obj->pgops->pgo_reference(
383 entry->object.uvm_obj);
384 }
385 }
386
387 /*
388 * uvm_map_clip_end: ensure that the entry ends at or before
389 * the ending address, if it does't we split the reference
390 *
391 * => caller should use UVM_MAP_CLIP_END macro rather than calling
392 * this directly
393 * => map must be locked by caller
394 */
395
396 void
397 uvm_map_clip_end(map, entry, end)
398 vm_map_t map;
399 vm_map_entry_t entry;
400 vaddr_t end;
401 {
402 vm_map_entry_t new_entry;
403 vaddr_t new_adj; /* #bytes we move start forward */
404
405 /*
406 * Create a new entry and insert it
407 * AFTER the specified entry
408 */
409
410 new_entry = uvm_mapent_alloc(map);
411 uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
412
413 new_entry->start = entry->end = end;
414 new_adj = end - entry->start;
415 if (new_entry->object.uvm_obj)
416 new_entry->offset += new_adj;
417
418 if (entry->aref.ar_amap)
419 amap_splitref(&entry->aref, &new_entry->aref, new_adj);
420
421 uvm_map_entry_link(map, entry, new_entry);
422
423 if (UVM_ET_ISSUBMAP(entry)) {
424 /* ... unlikely to happen, but play it safe */
425 uvm_map_reference(new_entry->object.sub_map);
426 } else {
427 if (UVM_ET_ISOBJ(entry) &&
428 entry->object.uvm_obj->pgops &&
429 entry->object.uvm_obj->pgops->pgo_reference)
430 entry->object.uvm_obj->pgops->pgo_reference(
431 entry->object.uvm_obj);
432 }
433 }
434
435
436 /*
437 * M A P - m a i n e n t r y p o i n t
438 */
439 /*
440 * uvm_map: establish a valid mapping in a map
441 *
442 * => assume startp is page aligned.
443 * => assume size is a multiple of PAGE_SIZE.
444 * => assume sys_mmap provides enough of a "hint" to have us skip
445 * over text/data/bss area.
446 * => map must be unlocked (we will lock it)
447 * => <uobj,uoffset> value meanings (4 cases):
448 * [1] <NULL,uoffset> == uoffset is a hint for PMAP_PREFER
449 * [2] <NULL,UVM_UNKNOWN_OFFSET> == don't PMAP_PREFER
450 * [3] <uobj,uoffset> == normal mapping
451 * [4] <uobj,UVM_UNKNOWN_OFFSET> == uvm_map finds offset based on VA
452 *
453 * case [4] is for kernel mappings where we don't know the offset until
454 * we've found a virtual address. note that kernel object offsets are
455 * always relative to vm_map_min(kernel_map).
456 * => XXXCDC: need way to map in external amap?
457 */
458
459 int
460 uvm_map(map, startp, size, uobj, uoffset, flags)
461 vm_map_t map;
462 vaddr_t *startp; /* IN/OUT */
463 vsize_t size;
464 struct uvm_object *uobj;
465 vaddr_t uoffset;
466 uvm_flag_t flags;
467 {
468 vm_map_entry_t prev_entry, new_entry;
469 vm_prot_t prot = UVM_PROTECTION(flags), maxprot =
470 UVM_MAXPROTECTION(flags);
471 vm_inherit_t inherit = UVM_INHERIT(flags);
472 int advice = UVM_ADVICE(flags);
473 UVMHIST_FUNC("uvm_map");
474 UVMHIST_CALLED(maphist);
475
476 UVMHIST_LOG(maphist, "(map=0x%x, *startp=0x%x, size=%d, flags=0x%x)",
477 map, *startp, size, flags);
478 UVMHIST_LOG(maphist, " uobj/offset 0x%x/%d", uobj, uoffset,0,0);
479
480 /*
481 * step 0: sanity check of protection code
482 */
483
484 if ((prot & maxprot) != prot) {
485 UVMHIST_LOG(maphist, "<- prot. failure: prot=0x%x, max=0x%x",
486 prot, maxprot,0,0);
487 return(KERN_PROTECTION_FAILURE);
488 }
489
490 /*
491 * step 1: figure out where to put new VM range
492 */
493
494 if (vm_map_lock_try(map) == FALSE) {
495 if (flags & UVM_FLAG_TRYLOCK)
496 return(KERN_FAILURE);
497 vm_map_lock(map); /* could sleep here */
498 }
499 if ((prev_entry = uvm_map_findspace(map, *startp, size, startp,
500 uobj, uoffset, flags & UVM_FLAG_FIXED)) == NULL) {
501 UVMHIST_LOG(maphist,"<- uvm_map_findspace failed!",0,0,0,0);
502 vm_map_unlock(map);
503 return (KERN_NO_SPACE);
504 }
505
506 #if defined(PMAP_GROWKERNEL) /* hack */
507 {
508 /* locked by kernel_map lock */
509 static vaddr_t maxkaddr = 0;
510
511 /*
512 * hack: grow kernel PTPs in advance.
513 */
514 if (map == kernel_map && maxkaddr < (*startp + size)) {
515 pmap_growkernel(*startp + size);
516 maxkaddr = *startp + size;
517 }
518 }
519 #endif
520
521 UVMCNT_INCR(uvm_map_call);
522
523 /*
524 * if uobj is null, then uoffset is either a VAC hint for PMAP_PREFER
525 * [typically from uvm_map_reserve] or it is UVM_UNKNOWN_OFFSET. in
526 * either case we want to zero it before storing it in the map entry
527 * (because it looks strange and confusing when debugging...)
528 *
529 * if uobj is not null
530 * if uoffset is not UVM_UNKNOWN_OFFSET then we have a normal mapping
531 * and we do not need to change uoffset.
532 * if uoffset is UVM_UNKNOWN_OFFSET then we need to find the offset
533 * now (based on the starting address of the map). this case is
534 * for kernel object mappings where we don't know the offset until
535 * the virtual address is found (with uvm_map_findspace). the
536 * offset is the distance we are from the start of the map.
537 */
538
539 if (uobj == NULL) {
540 uoffset = 0;
541 } else {
542 if (uoffset == UVM_UNKNOWN_OFFSET) {
543 #ifdef DIAGNOSTIC
544 if (uobj->uo_refs != UVM_OBJ_KERN)
545 panic("uvm_map: unknown offset with non-kernel object");
546 #endif
547 uoffset = *startp - vm_map_min(kernel_map);
548 }
549 }
550
551 /*
552 * step 2: try and insert in map by extending previous entry, if
553 * possible
554 * XXX: we don't try and pull back the next entry. might be useful
555 * for a stack, but we are currently allocating our stack in advance.
556 */
557
558 if ((flags & UVM_FLAG_NOMERGE) == 0 &&
559 prev_entry->end == *startp && prev_entry != &map->header &&
560 prev_entry->object.uvm_obj == uobj) {
561
562 if (uobj && prev_entry->offset +
563 (prev_entry->end - prev_entry->start) != uoffset)
564 goto step3;
565
566 if (UVM_ET_ISSUBMAP(prev_entry))
567 goto step3;
568
569 if (prev_entry->protection != prot ||
570 prev_entry->max_protection != maxprot)
571 goto step3;
572
573 if (prev_entry->inheritance != inherit ||
574 prev_entry->advice != advice)
575 goto step3;
576
577 /* wired_count's must match (new area is unwired) */
578 if (prev_entry->wired_count)
579 goto step3;
580
581 /*
582 * can't extend a shared amap. note: no need to lock amap to
583 * look at refs since we don't care about its exact value.
584 * if it is one (i.e. we have only reference) it will stay there
585 */
586
587 if (prev_entry->aref.ar_amap &&
588 amap_refs(prev_entry->aref.ar_amap) != 1) {
589 goto step3;
590 }
591
592 /* got it! */
593
594 UVMCNT_INCR(map_backmerge);
595 UVMHIST_LOG(maphist," starting back merge", 0, 0, 0, 0);
596
597 /*
598 * drop our reference to uobj since we are extending a reference
599 * that we already have (the ref count can not drop to zero).
600 */
601 if (uobj && uobj->pgops->pgo_detach)
602 uobj->pgops->pgo_detach(uobj);
603
604 if (prev_entry->aref.ar_amap) {
605 amap_extend(prev_entry, size);
606 }
607
608 prev_entry->end += size;
609 map->size += size;
610
611 UVMHIST_LOG(maphist,"<- done (via backmerge)!", 0, 0, 0, 0);
612 vm_map_unlock(map);
613 return (KERN_SUCCESS);
614
615 }
616 step3:
617 UVMHIST_LOG(maphist," allocating new map entry", 0, 0, 0, 0);
618
619 /*
620 * check for possible forward merge (which we don't do) and count
621 * the number of times we missed a *possible* chance to merge more
622 */
623
624 if ((flags & UVM_FLAG_NOMERGE) == 0 &&
625 prev_entry->next != &map->header &&
626 prev_entry->next->start == (*startp + size))
627 UVMCNT_INCR(map_forwmerge);
628
629 /*
630 * step 3: allocate new entry and link it in
631 */
632
633 new_entry = uvm_mapent_alloc(map);
634 new_entry->start = *startp;
635 new_entry->end = new_entry->start + size;
636 new_entry->object.uvm_obj = uobj;
637 new_entry->offset = uoffset;
638
639 if (uobj)
640 new_entry->etype = UVM_ET_OBJ;
641 else
642 new_entry->etype = 0;
643
644 if (flags & UVM_FLAG_COPYONW) {
645 new_entry->etype |= UVM_ET_COPYONWRITE;
646 if ((flags & UVM_FLAG_OVERLAY) == 0)
647 new_entry->etype |= UVM_ET_NEEDSCOPY;
648 }
649
650 new_entry->protection = prot;
651 new_entry->max_protection = maxprot;
652 new_entry->inheritance = inherit;
653 new_entry->wired_count = 0;
654 new_entry->advice = advice;
655 if (flags & UVM_FLAG_OVERLAY) {
656 /*
657 * to_add: for BSS we overallocate a little since we
658 * are likely to extend
659 */
660 vaddr_t to_add = (flags & UVM_FLAG_AMAPPAD) ?
661 UVM_AMAP_CHUNK << PAGE_SHIFT : 0;
662 struct vm_amap *amap = amap_alloc(size, to_add, M_WAITOK);
663 new_entry->aref.ar_pageoff = 0;
664 new_entry->aref.ar_amap = amap;
665 } else {
666 new_entry->aref.ar_amap = NULL;
667 }
668
669 uvm_map_entry_link(map, prev_entry, new_entry);
670
671 map->size += size;
672
673 /*
674 * Update the free space hint
675 */
676
677 if ((map->first_free == prev_entry) &&
678 (prev_entry->end >= new_entry->start))
679 map->first_free = new_entry;
680
681 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
682 vm_map_unlock(map);
683 return(KERN_SUCCESS);
684 }
685
686 /*
687 * uvm_map_lookup_entry: find map entry at or before an address
688 *
689 * => map must at least be read-locked by caller
690 * => entry is returned in "entry"
691 * => return value is true if address is in the returned entry
692 */
693
694 boolean_t
695 uvm_map_lookup_entry(map, address, entry)
696 vm_map_t map;
697 vaddr_t address;
698 vm_map_entry_t *entry; /* OUT */
699 {
700 vm_map_entry_t cur;
701 vm_map_entry_t last;
702 UVMHIST_FUNC("uvm_map_lookup_entry");
703 UVMHIST_CALLED(maphist);
704
705 UVMHIST_LOG(maphist,"(map=0x%x,addr=0x%x,ent=0x%x)",
706 map, address, entry, 0);
707
708 /*
709 * start looking either from the head of the
710 * list, or from the hint.
711 */
712
713 simple_lock(&map->hint_lock);
714 cur = map->hint;
715 simple_unlock(&map->hint_lock);
716
717 if (cur == &map->header)
718 cur = cur->next;
719
720 UVMCNT_INCR(uvm_mlk_call);
721 if (address >= cur->start) {
722 /*
723 * go from hint to end of list.
724 *
725 * but first, make a quick check to see if
726 * we are already looking at the entry we
727 * want (which is usually the case).
728 * note also that we don't need to save the hint
729 * here... it is the same hint (unless we are
730 * at the header, in which case the hint didn't
731 * buy us anything anyway).
732 */
733 last = &map->header;
734 if ((cur != last) && (cur->end > address)) {
735 UVMCNT_INCR(uvm_mlk_hint);
736 *entry = cur;
737 UVMHIST_LOG(maphist,"<- got it via hint (0x%x)",
738 cur, 0, 0, 0);
739 return (TRUE);
740 }
741 } else {
742 /*
743 * go from start to hint, *inclusively*
744 */
745 last = cur->next;
746 cur = map->header.next;
747 }
748
749 /*
750 * search linearly
751 */
752
753 while (cur != last) {
754 if (cur->end > address) {
755 if (address >= cur->start) {
756 /*
757 * save this lookup for future
758 * hints, and return
759 */
760
761 *entry = cur;
762 SAVE_HINT(map, cur);
763 UVMHIST_LOG(maphist,"<- search got it (0x%x)",
764 cur, 0, 0, 0);
765 return (TRUE);
766 }
767 break;
768 }
769 cur = cur->next;
770 }
771 *entry = cur->prev;
772 SAVE_HINT(map, *entry);
773 UVMHIST_LOG(maphist,"<- failed!",0,0,0,0);
774 return (FALSE);
775 }
776
777
778 /*
779 * uvm_map_findspace: find "length" sized space in "map".
780 *
781 * => "hint" is a hint about where we want it, unless fixed is true
782 * (in which case we insist on using "hint").
783 * => "result" is VA returned
784 * => uobj/uoffset are to be used to handle VAC alignment, if required
785 * => caller must at least have read-locked map
786 * => returns NULL on failure, or pointer to prev. map entry if success
787 * => note this is a cross between the old vm_map_findspace and vm_map_find
788 */
789
790 vm_map_entry_t
791 uvm_map_findspace(map, hint, length, result, uobj, uoffset, fixed)
792 vm_map_t map;
793 vaddr_t hint;
794 vsize_t length;
795 vaddr_t *result; /* OUT */
796 struct uvm_object *uobj;
797 vaddr_t uoffset;
798 boolean_t fixed;
799 {
800 vm_map_entry_t entry, next, tmp;
801 vaddr_t end;
802 UVMHIST_FUNC("uvm_map_findspace");
803 UVMHIST_CALLED(maphist);
804
805 UVMHIST_LOG(maphist, "(map=0x%x, hint=0x%x, len=%d, fixed=%d)",
806 map, hint, length, fixed);
807
808 if (hint < map->min_offset) { /* check ranges ... */
809 if (fixed) {
810 UVMHIST_LOG(maphist,"<- VA below map range",0,0,0,0);
811 return(NULL);
812 }
813 hint = map->min_offset;
814 }
815 if (hint > map->max_offset) {
816 UVMHIST_LOG(maphist,"<- VA 0x%x > range [0x%x->0x%x]",
817 hint, map->min_offset, map->max_offset, 0);
818 return(NULL);
819 }
820
821 /*
822 * Look for the first possible address; if there's already
823 * something at this address, we have to start after it.
824 */
825
826 if (!fixed && hint == map->min_offset) {
827 if ((entry = map->first_free) != &map->header)
828 hint = entry->end;
829 } else {
830 if (uvm_map_lookup_entry(map, hint, &tmp)) {
831 /* "hint" address already in use ... */
832 if (fixed) {
833 UVMHIST_LOG(maphist,"<- fixed & VA in use",
834 0, 0, 0, 0);
835 return(NULL);
836 }
837 hint = tmp->end;
838 }
839 entry = tmp;
840 }
841
842 /*
843 * Look through the rest of the map, trying to fit a new region in
844 * the gap between existing regions, or after the very last region.
845 * note: entry->end = base VA of current gap,
846 * next->start = VA of end of current gap
847 */
848 for (;; hint = (entry = next)->end) {
849 /*
850 * Find the end of the proposed new region. Be sure we didn't
851 * go beyond the end of the map, or wrap around the address;
852 * if so, we lose. Otherwise, if this is the last entry, or
853 * if the proposed new region fits before the next entry, we
854 * win.
855 */
856
857 #ifdef PMAP_PREFER
858 /*
859 * push hint forward as needed to avoid VAC alias problems.
860 * we only do this if a valid offset is specified.
861 */
862 if (!fixed && uoffset != UVM_UNKNOWN_OFFSET)
863 PMAP_PREFER(uoffset, &hint);
864 #endif
865 end = hint + length;
866 if (end > map->max_offset || end < hint) {
867 UVMHIST_LOG(maphist,"<- failed (off end)", 0,0,0,0);
868 return (NULL);
869 }
870 next = entry->next;
871 if (next == &map->header || next->start >= end)
872 break;
873 if (fixed) {
874 UVMHIST_LOG(maphist,"<- fixed mapping failed", 0,0,0,0);
875 return(NULL); /* only one shot at it ... */
876 }
877 }
878 SAVE_HINT(map, entry);
879 *result = hint;
880 UVMHIST_LOG(maphist,"<- got it! (result=0x%x)", hint, 0,0,0);
881 return (entry);
882 }
883
884 /*
885 * U N M A P - m a i n h e l p e r f u n c t i o n s
886 */
887
888 /*
889 * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop")
890 *
891 * => caller must check alignment and size
892 * => map must be locked by caller
893 * => we return a list of map entries that we've remove from the map
894 * in "entry_list"
895 */
896
897 int
898 uvm_unmap_remove(map, start, end, entry_list)
899 vm_map_t map;
900 vaddr_t start,end;
901 vm_map_entry_t *entry_list; /* OUT */
902 {
903 vm_map_entry_t entry, first_entry, next;
904 vaddr_t len;
905 UVMHIST_FUNC("uvm_unmap_remove");
906 UVMHIST_CALLED(maphist);
907
908 UVMHIST_LOG(maphist,"(map=0x%x, start=0x%x, end=0x%x)",
909 map, start, end, 0);
910
911 VM_MAP_RANGE_CHECK(map, start, end);
912
913 /*
914 * find first entry
915 */
916 if (uvm_map_lookup_entry(map, start, &first_entry) == TRUE) {
917 /* clip and go... */
918 entry = first_entry;
919 UVM_MAP_CLIP_START(map, entry, start);
920 /* critical! prevents stale hint */
921 SAVE_HINT(map, entry->prev);
922
923 } else {
924 entry = first_entry->next;
925 }
926
927 /*
928 * Save the free space hint
929 */
930
931 if (map->first_free->start >= start)
932 map->first_free = entry->prev;
933
934 /*
935 * note: we now re-use first_entry for a different task. we remove
936 * a number of map entries from the map and save them in a linked
937 * list headed by "first_entry". once we remove them from the map
938 * the caller should unlock the map and drop the references to the
939 * backing objects [c.f. uvm_unmap_detach]. the object is to
940 * seperate unmapping from reference dropping. why?
941 * [1] the map has to be locked for unmapping
942 * [2] the map need not be locked for reference dropping
943 * [3] dropping references may trigger pager I/O, and if we hit
944 * a pager that does synchronous I/O we may have to wait for it.
945 * [4] we would like all waiting for I/O to occur with maps unlocked
946 * so that we don't block other threads.
947 */
948 first_entry = NULL;
949 *entry_list = NULL; /* to be safe */
950
951 /*
952 * break up the area into map entry sized regions and unmap. note
953 * that all mappings have to be removed before we can even consider
954 * dropping references to amaps or VM objects (otherwise we could end
955 * up with a mapping to a page on the free list which would be very bad)
956 */
957
958 while ((entry != &map->header) && (entry->start < end)) {
959
960 UVM_MAP_CLIP_END(map, entry, end);
961 next = entry->next;
962 len = entry->end - entry->start;
963
964 /*
965 * unwire before removing addresses from the pmap; otherwise
966 * unwiring will put the entries back into the pmap (XXX).
967 */
968
969 if (entry->wired_count)
970 uvm_map_entry_unwire(map, entry);
971
972 /*
973 * special case: handle mappings to anonymous kernel objects.
974 * we want to free these pages right away...
975 */
976 if (UVM_ET_ISOBJ(entry) &&
977 entry->object.uvm_obj->uo_refs == UVM_OBJ_KERN) {
978
979 #ifdef DIAGNOSTIC
980 if (vm_map_pmap(map) != pmap_kernel())
981 panic("uvm_unmap_remove: kernel object mapped by non-kernel map");
982 #endif
983
984 /*
985 * note: kernel object mappings are currently used in
986 * two ways:
987 * [1] "normal" mappings of pages in the kernel object
988 * [2] uvm_km_valloc'd allocations in which we
989 * pmap_enter in some non-kernel-object page
990 * (e.g. vmapbuf).
991 *
992 * for case [1], we need to remove the mapping from
993 * the pmap and then remove the page from the kernel
994 * object (because, once pages in a kernel object are
995 * unmapped they are no longer needed, unlike, say,
996 * a vnode where you might want the data to persist
997 * until flushed out of a queue).
998 *
999 * for case [2], we need to remove the mapping from
1000 * the pmap. there shouldn't be any pages at the
1001 * specified offset in the kernel object [but it
1002 * doesn't hurt to call uvm_km_pgremove just to be
1003 * safe?]
1004 *
1005 * uvm_km_pgremove currently does the following:
1006 * for pages in the kernel object in range:
1007 * - pmap_page_protect them out of all pmaps
1008 * - uvm_pagefree the page
1009 *
1010 * note that in case [1] the pmap_page_protect call
1011 * in uvm_km_pgremove may very well be redundant
1012 * because we have already removed the mappings
1013 * beforehand with pmap_remove (or pmap_kremove).
1014 * in the PMAP_NEW case, the pmap_page_protect call
1015 * may not do anything, since PMAP_NEW allows the
1016 * kernel to enter/remove kernel mappings without
1017 * bothing to keep track of the mappings (e.g. via
1018 * pv_entry lists). XXX: because of this, in the
1019 * future we should consider removing the
1020 * pmap_page_protect from uvm_km_pgremove some time
1021 * in the future.
1022 */
1023
1024 /*
1025 * remove mappings from pmap
1026 */
1027 #if defined(PMAP_NEW)
1028 pmap_kremove(entry->start, len);
1029 #else
1030 pmap_remove(pmap_kernel(), entry->start,
1031 entry->start+len);
1032 #endif
1033
1034 /*
1035 * remove pages from a kernel object (offsets are
1036 * always relative to vm_map_min(kernel_map)).
1037 */
1038 uvm_km_pgremove(entry->object.uvm_obj,
1039 entry->start - vm_map_min(kernel_map),
1040 entry->end - vm_map_min(kernel_map));
1041
1042 /*
1043 * null out kernel_object reference, we've just
1044 * dropped it
1045 */
1046 entry->etype &= ~UVM_ET_OBJ;
1047 entry->object.uvm_obj = NULL; /* to be safe */
1048
1049 } else {
1050 /*
1051 * remove mappings the standard way.
1052 */
1053 pmap_remove(map->pmap, entry->start, entry->end);
1054 }
1055
1056 /*
1057 * remove entry from map and put it on our list of entries
1058 * that we've nuked. then go do next entry.
1059 */
1060 UVMHIST_LOG(maphist, " removed map entry 0x%x", entry, 0, 0,0);
1061 uvm_map_entry_unlink(map, entry);
1062 map->size -= len;
1063 entry->next = first_entry;
1064 first_entry = entry;
1065 entry = next; /* next entry, please */
1066 }
1067
1068 /*
1069 * now we've cleaned up the map and are ready for the caller to drop
1070 * references to the mapped objects.
1071 */
1072
1073 *entry_list = first_entry;
1074 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
1075 return(KERN_SUCCESS);
1076 }
1077
1078 /*
1079 * uvm_unmap_detach: drop references in a chain of map entries
1080 *
1081 * => we will free the map entries as we traverse the list.
1082 */
1083
1084 void
1085 uvm_unmap_detach(first_entry, amap_unref_flags)
1086 vm_map_entry_t first_entry;
1087 int amap_unref_flags;
1088 {
1089 vm_map_entry_t next_entry;
1090 UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist);
1091
1092 while (first_entry) {
1093
1094 #ifdef DIAGNOSTIC
1095 /*
1096 * sanity check
1097 */
1098 /* was part of vm_map_entry_delete() */
1099 if (first_entry->wired_count)
1100 panic("unmap: still wired!");
1101 #endif
1102
1103 UVMHIST_LOG(maphist,
1104 " detach 0x%x: amap=0x%x, obj=0x%x, submap?=%d",
1105 first_entry, first_entry->aref.ar_amap,
1106 first_entry->object.uvm_obj,
1107 UVM_ET_ISSUBMAP(first_entry));
1108
1109 /*
1110 * drop reference to amap, if we've got one
1111 */
1112
1113 if (first_entry->aref.ar_amap)
1114 amap_unref(first_entry, amap_unref_flags);
1115
1116 /*
1117 * drop reference to our backing object, if we've got one
1118 */
1119
1120 if (UVM_ET_ISSUBMAP(first_entry)) {
1121 /* ... unlikely to happen, but play it safe */
1122 uvm_map_deallocate(first_entry->object.sub_map);
1123 } else {
1124 if (UVM_ET_ISOBJ(first_entry) &&
1125 first_entry->object.uvm_obj->pgops->pgo_detach)
1126 first_entry->object.uvm_obj->pgops->
1127 pgo_detach(first_entry->object.uvm_obj);
1128 }
1129
1130 /*
1131 * next entry
1132 */
1133 next_entry = first_entry->next;
1134 uvm_mapent_free(first_entry);
1135 first_entry = next_entry;
1136 }
1137
1138 /*
1139 * done!
1140 */
1141 UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
1142 return;
1143 }
1144
1145 /*
1146 * E X T R A C T I O N F U N C T I O N S
1147 */
1148
1149 /*
1150 * uvm_map_reserve: reserve space in a vm_map for future use.
1151 *
1152 * => we reserve space in a map by putting a dummy map entry in the
1153 * map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE)
1154 * => map should be unlocked (we will write lock it)
1155 * => we return true if we were able to reserve space
1156 * => XXXCDC: should be inline?
1157 */
1158
1159 int
1160 uvm_map_reserve(map, size, offset, raddr)
1161 vm_map_t map;
1162 vsize_t size;
1163 vaddr_t offset; /* hint for pmap_prefer */
1164 vaddr_t *raddr; /* OUT: reserved VA */
1165 {
1166 UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist);
1167
1168 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x, offset=0x%x,addr=0x%x)",
1169 map,size,offset,raddr);
1170
1171 size = round_page(size);
1172 if (*raddr < vm_map_min(map))
1173 *raddr = vm_map_min(map); /* hint */
1174
1175 /*
1176 * reserve some virtual space.
1177 */
1178
1179 if (uvm_map(map, raddr, size, NULL, offset,
1180 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
1181 UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != KERN_SUCCESS) {
1182 UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
1183 return (FALSE);
1184 }
1185
1186 UVMHIST_LOG(maphist, "<- done (*raddr=0x%x)", *raddr,0,0,0);
1187 return (TRUE);
1188 }
1189
1190 /*
1191 * uvm_map_replace: replace a reserved (blank) area of memory with
1192 * real mappings.
1193 *
1194 * => caller must WRITE-LOCK the map
1195 * => we return TRUE if replacement was a success
1196 * => we expect the newents chain to have nnewents entrys on it and
1197 * we expect newents->prev to point to the last entry on the list
1198 * => note newents is allowed to be NULL
1199 */
1200
1201 int
1202 uvm_map_replace(map, start, end, newents, nnewents)
1203 struct vm_map *map;
1204 vaddr_t start, end;
1205 vm_map_entry_t newents;
1206 int nnewents;
1207 {
1208 vm_map_entry_t oldent, last;
1209 UVMHIST_FUNC("uvm_map_replace");
1210 UVMHIST_CALLED(maphist);
1211
1212 /*
1213 * first find the blank map entry at the specified address
1214 */
1215
1216 if (!uvm_map_lookup_entry(map, start, &oldent)) {
1217 return(FALSE);
1218 }
1219
1220 /*
1221 * check to make sure we have a proper blank entry
1222 */
1223
1224 if (oldent->start != start || oldent->end != end ||
1225 oldent->object.uvm_obj != NULL || oldent->aref.ar_amap != NULL) {
1226 return (FALSE);
1227 }
1228
1229 #ifdef DIAGNOSTIC
1230 /*
1231 * sanity check the newents chain
1232 */
1233 {
1234 vm_map_entry_t tmpent = newents;
1235 int nent = 0;
1236 vaddr_t cur = start;
1237
1238 while (tmpent) {
1239 nent++;
1240 if (tmpent->start < cur)
1241 panic("uvm_map_replace1");
1242 if (tmpent->start > tmpent->end || tmpent->end > end) {
1243 printf("tmpent->start=0x%lx, tmpent->end=0x%lx, end=0x%lx\n",
1244 tmpent->start, tmpent->end, end);
1245 panic("uvm_map_replace2");
1246 }
1247 cur = tmpent->end;
1248 if (tmpent->next) {
1249 if (tmpent->next->prev != tmpent)
1250 panic("uvm_map_replace3");
1251 } else {
1252 if (newents->prev != tmpent)
1253 panic("uvm_map_replace4");
1254 }
1255 tmpent = tmpent->next;
1256 }
1257 if (nent != nnewents)
1258 panic("uvm_map_replace5");
1259 }
1260 #endif
1261
1262 /*
1263 * map entry is a valid blank! replace it. (this does all the
1264 * work of map entry link/unlink...).
1265 */
1266
1267 if (newents) {
1268
1269 last = newents->prev; /* we expect this */
1270
1271 /* critical: flush stale hints out of map */
1272 SAVE_HINT(map, newents);
1273 if (map->first_free == oldent)
1274 map->first_free = last;
1275
1276 last->next = oldent->next;
1277 last->next->prev = last;
1278 newents->prev = oldent->prev;
1279 newents->prev->next = newents;
1280 map->nentries = map->nentries + (nnewents - 1);
1281
1282 } else {
1283
1284 /* critical: flush stale hints out of map */
1285 SAVE_HINT(map, oldent->prev);
1286 if (map->first_free == oldent)
1287 map->first_free = oldent->prev;
1288
1289 /* NULL list of new entries: just remove the old one */
1290 uvm_map_entry_unlink(map, oldent);
1291 }
1292
1293
1294 /*
1295 * now we can free the old blank entry, unlock the map and return.
1296 */
1297
1298 uvm_mapent_free(oldent);
1299 return(TRUE);
1300 }
1301
1302 /*
1303 * uvm_map_extract: extract a mapping from a map and put it somewhere
1304 * (maybe removing the old mapping)
1305 *
1306 * => maps should be unlocked (we will write lock them)
1307 * => returns 0 on success, error code otherwise
1308 * => start must be page aligned
1309 * => len must be page sized
1310 * => flags:
1311 * UVM_EXTRACT_REMOVE: remove mappings from srcmap
1312 * UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only)
1313 * UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs
1314 * UVM_EXTRACT_FIXPROT: set prot to maxprot as we go
1315 * >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<<
1316 * >>>NOTE: QREF's must be unmapped via the QREF path, thus should only
1317 * be used from within the kernel in a kernel level map <<<
1318 */
1319
1320 int
1321 uvm_map_extract(srcmap, start, len, dstmap, dstaddrp, flags)
1322 vm_map_t srcmap, dstmap;
1323 vaddr_t start, *dstaddrp;
1324 vsize_t len;
1325 int flags;
1326 {
1327 vaddr_t dstaddr, end, newend, oldoffset, fudge, orig_fudge,
1328 oldstart;
1329 vm_map_entry_t chain, endchain, entry, orig_entry, newentry, deadentry;
1330 vm_map_entry_t oldentry;
1331 vsize_t elen;
1332 int nchain, error, copy_ok;
1333 UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist);
1334 UVMHIST_LOG(maphist,"(srcmap=0x%x,start=0x%x, len=0x%x", srcmap, start,
1335 len,0);
1336 UVMHIST_LOG(maphist," ...,dstmap=0x%x, flags=0x%x)", dstmap,flags,0,0);
1337
1338 #ifdef DIAGNOSTIC
1339 /*
1340 * step 0: sanity check: start must be on a page boundary, length
1341 * must be page sized. can't ask for CONTIG/QREF if you asked for
1342 * REMOVE.
1343 */
1344 if ((start & PAGE_MASK) || (len & PAGE_MASK))
1345 panic("uvm_map_extract1");
1346 if (flags & UVM_EXTRACT_REMOVE)
1347 if (flags & (UVM_EXTRACT_CONTIG|UVM_EXTRACT_QREF))
1348 panic("uvm_map_extract2");
1349 #endif
1350
1351
1352 /*
1353 * step 1: reserve space in the target map for the extracted area
1354 */
1355
1356 dstaddr = *dstaddrp;
1357 if (uvm_map_reserve(dstmap, len, start, &dstaddr) == FALSE)
1358 return(ENOMEM);
1359 *dstaddrp = dstaddr; /* pass address back to caller */
1360 UVMHIST_LOG(maphist, " dstaddr=0x%x", dstaddr,0,0,0);
1361
1362
1363 /*
1364 * step 2: setup for the extraction process loop by init'ing the
1365 * map entry chain, locking src map, and looking up the first useful
1366 * entry in the map.
1367 */
1368
1369 end = start + len;
1370 newend = dstaddr + len;
1371 chain = endchain = NULL;
1372 nchain = 0;
1373 vm_map_lock(srcmap);
1374
1375 if (uvm_map_lookup_entry(srcmap, start, &entry)) {
1376
1377 /* "start" is within an entry */
1378 if (flags & UVM_EXTRACT_QREF) {
1379 /*
1380 * for quick references we don't clip the entry, so
1381 * the entry may map space "before" the starting
1382 * virtual address... this is the "fudge" factor
1383 * (which can be non-zero only the first time
1384 * through the "while" loop in step 3).
1385 */
1386 fudge = start - entry->start;
1387 } else {
1388 /*
1389 * normal reference: we clip the map to fit (thus
1390 * fudge is zero)
1391 */
1392 UVM_MAP_CLIP_START(srcmap, entry, start);
1393 SAVE_HINT(srcmap, entry->prev);
1394 fudge = 0;
1395 }
1396
1397 } else {
1398
1399 /* "start" is not within an entry ... skip to next entry */
1400 if (flags & UVM_EXTRACT_CONTIG) {
1401 error = EINVAL;
1402 goto bad; /* definite hole here ... */
1403 }
1404
1405 entry = entry->next;
1406 fudge = 0;
1407 }
1408 /* save values from srcmap for step 6 */
1409 orig_entry = entry;
1410 orig_fudge = fudge;
1411
1412
1413 /*
1414 * step 3: now start looping through the map entries, extracting
1415 * as we go.
1416 */
1417
1418 while (entry->start < end && entry != &srcmap->header) {
1419
1420 /* if we are not doing a quick reference, clip it */
1421 if ((flags & UVM_EXTRACT_QREF) == 0)
1422 UVM_MAP_CLIP_END(srcmap, entry, end);
1423
1424 /* clear needs_copy (allow chunking) */
1425 if (UVM_ET_ISNEEDSCOPY(entry)) {
1426 if (fudge)
1427 oldstart = entry->start;
1428 else
1429 oldstart = 0; /* XXX: gcc */
1430 amap_copy(srcmap, entry, M_NOWAIT, TRUE, start, end);
1431 if (UVM_ET_ISNEEDSCOPY(entry)) { /* failed? */
1432 error = ENOMEM;
1433 goto bad;
1434 }
1435 /* amap_copy could clip (during chunk)! update fudge */
1436 if (fudge) {
1437 fudge = fudge - (entry->start - oldstart);
1438 orig_fudge = fudge;
1439 }
1440 }
1441
1442 /* calculate the offset of this from "start" */
1443 oldoffset = (entry->start + fudge) - start;
1444
1445 /* allocate a new map entry */
1446 newentry = uvm_mapent_alloc(dstmap);
1447 if (newentry == NULL) {
1448 error = ENOMEM;
1449 goto bad;
1450 }
1451
1452 /* set up new map entry */
1453 newentry->next = NULL;
1454 newentry->prev = endchain;
1455 newentry->start = dstaddr + oldoffset;
1456 newentry->end =
1457 newentry->start + (entry->end - (entry->start + fudge));
1458 if (newentry->end > newend || newentry->end < newentry->start)
1459 newentry->end = newend;
1460 newentry->object.uvm_obj = entry->object.uvm_obj;
1461 if (newentry->object.uvm_obj) {
1462 if (newentry->object.uvm_obj->pgops->pgo_reference)
1463 newentry->object.uvm_obj->pgops->
1464 pgo_reference(newentry->object.uvm_obj);
1465 newentry->offset = entry->offset + fudge;
1466 } else {
1467 newentry->offset = 0;
1468 }
1469 newentry->etype = entry->etype;
1470 newentry->protection = (flags & UVM_EXTRACT_FIXPROT) ?
1471 entry->max_protection : entry->protection;
1472 newentry->max_protection = entry->max_protection;
1473 newentry->inheritance = entry->inheritance;
1474 newentry->wired_count = 0;
1475 newentry->aref.ar_amap = entry->aref.ar_amap;
1476 if (newentry->aref.ar_amap) {
1477 newentry->aref.ar_pageoff =
1478 entry->aref.ar_pageoff + (fudge >> PAGE_SHIFT);
1479 amap_ref(newentry, AMAP_SHARED |
1480 ((flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0));
1481 } else {
1482 newentry->aref.ar_pageoff = 0;
1483 }
1484 newentry->advice = entry->advice;
1485
1486 /* now link it on the chain */
1487 nchain++;
1488 if (endchain == NULL) {
1489 chain = endchain = newentry;
1490 } else {
1491 endchain->next = newentry;
1492 endchain = newentry;
1493 }
1494
1495 /* end of 'while' loop! */
1496 if ((flags & UVM_EXTRACT_CONTIG) && entry->end < end &&
1497 (entry->next == &srcmap->header ||
1498 entry->next->start != entry->end)) {
1499 error = EINVAL;
1500 goto bad;
1501 }
1502 entry = entry->next;
1503 fudge = 0;
1504 }
1505
1506
1507 /*
1508 * step 4: close off chain (in format expected by uvm_map_replace)
1509 */
1510
1511 if (chain)
1512 chain->prev = endchain;
1513
1514
1515 /*
1516 * step 5: attempt to lock the dest map so we can pmap_copy.
1517 * note usage of copy_ok:
1518 * 1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5)
1519 * 0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7
1520 */
1521
1522 if (srcmap == dstmap || vm_map_lock_try(dstmap) == TRUE) {
1523
1524 copy_ok = 1;
1525 if (!uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
1526 nchain)) {
1527 if (srcmap != dstmap)
1528 vm_map_unlock(dstmap);
1529 error = EIO;
1530 goto bad;
1531 }
1532
1533 } else {
1534
1535 copy_ok = 0;
1536 /* replace defered until step 7 */
1537
1538 }
1539
1540
1541 /*
1542 * step 6: traverse the srcmap a second time to do the following:
1543 * - if we got a lock on the dstmap do pmap_copy
1544 * - if UVM_EXTRACT_REMOVE remove the entries
1545 * we make use of orig_entry and orig_fudge (saved in step 2)
1546 */
1547
1548 if (copy_ok || (flags & UVM_EXTRACT_REMOVE)) {
1549
1550 /* purge possible stale hints from srcmap */
1551 if (flags & UVM_EXTRACT_REMOVE) {
1552 SAVE_HINT(srcmap, orig_entry->prev);
1553 if (srcmap->first_free->start >= start)
1554 srcmap->first_free = orig_entry->prev;
1555 }
1556
1557 entry = orig_entry;
1558 fudge = orig_fudge;
1559 deadentry = NULL; /* for UVM_EXTRACT_REMOVE */
1560
1561 while (entry->start < end && entry != &srcmap->header) {
1562
1563 if (copy_ok) {
1564 oldoffset = (entry->start + fudge) - start;
1565 elen = min(end, entry->end) - (entry->start + fudge);
1566 pmap_copy(dstmap->pmap, srcmap->pmap, dstaddr + oldoffset,
1567 elen, entry->start + fudge);
1568 }
1569
1570 /* we advance "entry" in the following if statement */
1571 if (flags & UVM_EXTRACT_REMOVE) {
1572 pmap_remove(srcmap->pmap, entry->start,
1573 entry->end);
1574 oldentry = entry; /* save entry */
1575 entry = entry->next; /* advance */
1576 uvm_map_entry_unlink(srcmap, oldentry);
1577 /* add to dead list */
1578 oldentry->next = deadentry;
1579 deadentry = oldentry;
1580 } else {
1581 entry = entry->next; /* advance */
1582 }
1583
1584 /* end of 'while' loop */
1585 fudge = 0;
1586 }
1587
1588 /*
1589 * unlock dstmap. we will dispose of deadentry in
1590 * step 7 if needed
1591 */
1592 if (copy_ok && srcmap != dstmap)
1593 vm_map_unlock(dstmap);
1594
1595 }
1596 else
1597 deadentry = NULL; /* XXX: gcc */
1598
1599 /*
1600 * step 7: we are done with the source map, unlock. if copy_ok
1601 * is 0 then we have not replaced the dummy mapping in dstmap yet
1602 * and we need to do so now.
1603 */
1604
1605 vm_map_unlock(srcmap);
1606 if ((flags & UVM_EXTRACT_REMOVE) && deadentry)
1607 uvm_unmap_detach(deadentry, 0); /* dispose of old entries */
1608
1609 /* now do the replacement if we didn't do it in step 5 */
1610 if (copy_ok == 0) {
1611 vm_map_lock(dstmap);
1612 error = uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
1613 nchain);
1614 vm_map_unlock(dstmap);
1615
1616 if (error == FALSE) {
1617 error = EIO;
1618 goto bad2;
1619 }
1620 }
1621
1622 /*
1623 * done!
1624 */
1625 return(0);
1626
1627 /*
1628 * bad: failure recovery
1629 */
1630 bad:
1631 vm_map_unlock(srcmap);
1632 bad2: /* src already unlocked */
1633 if (chain)
1634 uvm_unmap_detach(chain,
1635 (flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0);
1636 uvm_unmap(dstmap, dstaddr, dstaddr+len); /* ??? */
1637 return(error);
1638 }
1639
1640 /* end of extraction functions */
1641
1642 /*
1643 * uvm_map_submap: punch down part of a map into a submap
1644 *
1645 * => only the kernel_map is allowed to be submapped
1646 * => the purpose of submapping is to break up the locking granularity
1647 * of a larger map
1648 * => the range specified must have been mapped previously with a uvm_map()
1649 * call [with uobj==NULL] to create a blank map entry in the main map.
1650 * [And it had better still be blank!]
1651 * => maps which contain submaps should never be copied or forked.
1652 * => to remove a submap, use uvm_unmap() on the main map
1653 * and then uvm_map_deallocate() the submap.
1654 * => main map must be unlocked.
1655 * => submap must have been init'd and have a zero reference count.
1656 * [need not be locked as we don't actually reference it]
1657 */
1658
1659 int
1660 uvm_map_submap(map, start, end, submap)
1661 vm_map_t map, submap;
1662 vaddr_t start, end;
1663 {
1664 vm_map_entry_t entry;
1665 int result;
1666 UVMHIST_FUNC("uvm_map_submap"); UVMHIST_CALLED(maphist);
1667
1668 vm_map_lock(map);
1669
1670 VM_MAP_RANGE_CHECK(map, start, end);
1671
1672 if (uvm_map_lookup_entry(map, start, &entry)) {
1673 UVM_MAP_CLIP_START(map, entry, start);
1674 UVM_MAP_CLIP_END(map, entry, end); /* to be safe */
1675 }
1676 else {
1677 entry = NULL;
1678 }
1679
1680 if (entry != NULL &&
1681 entry->start == start && entry->end == end &&
1682 entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL &&
1683 !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) {
1684
1685 /*
1686 * doit!
1687 */
1688 entry->etype |= UVM_ET_SUBMAP;
1689 entry->object.sub_map = submap;
1690 entry->offset = 0;
1691 uvm_map_reference(submap);
1692 result = KERN_SUCCESS;
1693 } else {
1694 result = KERN_INVALID_ARGUMENT;
1695 }
1696 vm_map_unlock(map);
1697
1698 return(result);
1699 }
1700
1701
1702 /*
1703 * uvm_map_protect: change map protection
1704 *
1705 * => set_max means set max_protection.
1706 * => map must be unlocked.
1707 * => XXXCDC: does not work properly with share maps. rethink.
1708 */
1709
1710 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
1711 ~VM_PROT_WRITE : VM_PROT_ALL)
1712 #define max(a,b) ((a) > (b) ? (a) : (b))
1713
1714 int
1715 uvm_map_protect(map, start, end, new_prot, set_max)
1716 vm_map_t map;
1717 vaddr_t start, end;
1718 vm_prot_t new_prot;
1719 boolean_t set_max;
1720 {
1721 vm_map_entry_t current, entry;
1722 UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist);
1723 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_prot=0x%x)",
1724 map, start, end, new_prot);
1725
1726 vm_map_lock(map);
1727
1728 VM_MAP_RANGE_CHECK(map, start, end);
1729
1730 if (uvm_map_lookup_entry(map, start, &entry)) {
1731 UVM_MAP_CLIP_START(map, entry, start);
1732 } else {
1733 entry = entry->next;
1734 }
1735
1736 /*
1737 * make a first pass to check for protection violations.
1738 */
1739
1740 current = entry;
1741 while ((current != &map->header) && (current->start < end)) {
1742 if (UVM_ET_ISSUBMAP(current))
1743 return(KERN_INVALID_ARGUMENT);
1744 if ((new_prot & current->max_protection) != new_prot) {
1745 vm_map_unlock(map);
1746 return(KERN_PROTECTION_FAILURE);
1747 }
1748 current = current->next;
1749 }
1750
1751 /* go back and fix up protections (no need to clip this time). */
1752
1753 current = entry;
1754
1755 while ((current != &map->header) && (current->start < end)) {
1756 vm_prot_t old_prot;
1757
1758 UVM_MAP_CLIP_END(map, current, end);
1759
1760 old_prot = current->protection;
1761 if (set_max)
1762 current->protection =
1763 (current->max_protection = new_prot) & old_prot;
1764 else
1765 current->protection = new_prot;
1766
1767 /*
1768 * update physical map if necessary. worry about copy-on-write
1769 * here -- CHECK THIS XXX
1770 */
1771
1772 if (current->protection != old_prot) {
1773
1774 /* update pmap! */
1775 pmap_protect(map->pmap, current->start, current->end,
1776 current->protection & MASK(entry));
1777
1778 }
1779 current = current->next;
1780 }
1781
1782 vm_map_unlock(map);
1783 UVMHIST_LOG(maphist, "<- done",0,0,0,0);
1784 return(KERN_SUCCESS);
1785 }
1786
1787 #undef max
1788 #undef MASK
1789
1790 /*
1791 * uvm_map_inherit: set inheritance code for range of addrs in map.
1792 *
1793 * => map must be unlocked
1794 * => note that the inherit code is used during a "fork". see fork
1795 * code for details.
1796 * => XXXCDC: currently only works in main map. what about share map?
1797 */
1798
1799 int
1800 uvm_map_inherit(map, start, end, new_inheritance)
1801 vm_map_t map;
1802 vaddr_t start;
1803 vaddr_t end;
1804 vm_inherit_t new_inheritance;
1805 {
1806 vm_map_entry_t entry, temp_entry;
1807 UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist);
1808 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_inh=0x%x)",
1809 map, start, end, new_inheritance);
1810
1811 switch (new_inheritance) {
1812 case VM_INHERIT_NONE:
1813 case VM_INHERIT_COPY:
1814 case VM_INHERIT_SHARE:
1815 break;
1816 default:
1817 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
1818 return(KERN_INVALID_ARGUMENT);
1819 }
1820
1821 vm_map_lock(map);
1822
1823 VM_MAP_RANGE_CHECK(map, start, end);
1824
1825 if (uvm_map_lookup_entry(map, start, &temp_entry)) {
1826 entry = temp_entry;
1827 UVM_MAP_CLIP_START(map, entry, start);
1828 } else {
1829 entry = temp_entry->next;
1830 }
1831
1832 while ((entry != &map->header) && (entry->start < end)) {
1833 UVM_MAP_CLIP_END(map, entry, end);
1834
1835 entry->inheritance = new_inheritance;
1836
1837 entry = entry->next;
1838 }
1839
1840 vm_map_unlock(map);
1841 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
1842 return(KERN_SUCCESS);
1843 }
1844
1845 /*
1846 * uvm_map_pageable: sets the pageability of a range in a map.
1847 *
1848 * => regions sepcified as not pageable require lock-down (wired) memory
1849 * and page tables.
1850 * => map must not be locked.
1851 * => XXXCDC: check this and try and clean it up.
1852 */
1853
1854 int
1855 uvm_map_pageable(map, start, end, new_pageable)
1856 vm_map_t map;
1857 vaddr_t start, end;
1858 boolean_t new_pageable;
1859 {
1860 vm_map_entry_t entry, start_entry;
1861 vaddr_t failed = 0;
1862 int rv;
1863 UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist);
1864 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_pageable=0x%x)",
1865 map, start, end, new_pageable);
1866
1867 vm_map_lock(map);
1868 VM_MAP_RANGE_CHECK(map, start, end);
1869
1870 /*
1871 * only one pageability change may take place at one time, since
1872 * uvm_fault_wire assumes it will be called only once for each
1873 * wiring/unwiring. therefore, we have to make sure we're actually
1874 * changing the pageability for the entire region. we do so before
1875 * making any changes.
1876 */
1877
1878 if (uvm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1879 vm_map_unlock(map);
1880
1881 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
1882 return (KERN_INVALID_ADDRESS);
1883 }
1884 entry = start_entry;
1885
1886 /*
1887 * handle wiring and unwiring seperately.
1888 */
1889
1890 if (new_pageable) { /* unwire */
1891
1892 UVM_MAP_CLIP_START(map, entry, start);
1893
1894 /*
1895 * unwiring. first ensure that the range to be unwired is
1896 * really wired down and that there are no holes.
1897 */
1898 while ((entry != &map->header) && (entry->start < end)) {
1899
1900 if (entry->wired_count == 0 ||
1901 (entry->end < end &&
1902 (entry->next == &map->header ||
1903 entry->next->start > entry->end))) {
1904 vm_map_unlock(map);
1905 UVMHIST_LOG(maphist,
1906 "<- done (INVALID UNWIRE ARG)",0,0,0,0);
1907 return (KERN_INVALID_ARGUMENT);
1908 }
1909 entry = entry->next;
1910 }
1911
1912 /*
1913 * now decrement the wiring count for each region. if a region
1914 * becomes completely unwired, unwire its physical pages and
1915 * mappings.
1916 */
1917 #if 0 /* not necessary: uvm_fault_unwire does not lock */
1918 lock_set_recursive(&map->lock);
1919 #endif /* XXXCDC */
1920
1921 entry = start_entry;
1922 while ((entry != &map->header) && (entry->start < end)) {
1923 UVM_MAP_CLIP_END(map, entry, end);
1924
1925 entry->wired_count--;
1926 if (entry->wired_count == 0)
1927 uvm_map_entry_unwire(map, entry);
1928
1929 entry = entry->next;
1930 }
1931 #if 0 /* XXXCDC: not necessary, see above */
1932 lock_clear_recursive(&map->lock);
1933 #endif
1934 vm_map_unlock(map);
1935 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
1936 return(KERN_SUCCESS);
1937
1938 /*
1939 * end of unwire case!
1940 */
1941 }
1942
1943 /*
1944 * wire case: in two passes [XXXCDC: ugly block of code here]
1945 *
1946 * 1: holding the write lock, we create any anonymous maps that need
1947 * to be created. then we clip each map entry to the region to
1948 * be wired and increment its wiring count.
1949 *
1950 * 2: we downgrade to a read lock, and call uvm_fault_wire to fault
1951 * in the pages for any newly wired area (wired_count is 1).
1952 *
1953 * downgrading to a read lock for uvm_fault_wire avoids a possible
1954 * deadlock with another thread that may have faulted on one of
1955 * the pages to be wired (it would mark the page busy, blocking
1956 * us, then in turn block on the map lock that we hold). because
1957 * of problems in the recursive lock package, we cannot upgrade
1958 * to a write lock in vm_map_lookup. thus, any actions that
1959 * require the write lock must be done beforehand. because we
1960 * keep the read lock on the map, the copy-on-write status of the
1961 * entries we modify here cannot change.
1962 */
1963
1964 while ((entry != &map->header) && (entry->start < end)) {
1965
1966 if (entry->wired_count == 0) { /* not already wired? */
1967
1968 /*
1969 * perform actions of vm_map_lookup that need the
1970 * write lock on the map: create an anonymous map
1971 * for a copy-on-write region, or an anonymous map
1972 * for a zero-fill region. (XXXCDC: submap case
1973 * ok?)
1974 */
1975
1976 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */
1977 /*
1978 * XXXCDC: protection vs. max_protection??
1979 * (wirefault uses max?)
1980 * XXXCDC: used to do it always if
1981 * uvm_obj == NULL (wrong?)
1982 */
1983 if ( UVM_ET_ISNEEDSCOPY(entry) &&
1984 (entry->protection & VM_PROT_WRITE) != 0) {
1985 amap_copy(map, entry, M_WAITOK, TRUE,
1986 start, end);
1987 /* XXXCDC: wait OK? */
1988 }
1989 }
1990 } /* wired_count == 0 */
1991 UVM_MAP_CLIP_START(map, entry, start);
1992 UVM_MAP_CLIP_END(map, entry, end);
1993 entry->wired_count++;
1994
1995 /*
1996 * Check for holes
1997 */
1998 if (entry->end < end && (entry->next == &map->header ||
1999 entry->next->start > entry->end)) {
2000 /*
2001 * found one. amap creation actions do not need to
2002 * be undone, but the wired counts need to be restored.
2003 */
2004 while (entry != &map->header && entry->end > start) {
2005 entry->wired_count--;
2006 entry = entry->prev;
2007 }
2008 vm_map_unlock(map);
2009 UVMHIST_LOG(maphist,"<- done (INVALID WIRE)",0,0,0,0);
2010 return(KERN_INVALID_ARGUMENT);
2011 }
2012 entry = entry->next;
2013 }
2014
2015 /*
2016 * Pass 2.
2017 */
2018 /*
2019 * HACK HACK HACK HACK
2020 *
2021 * if we are wiring in the kernel map or a submap of it, unlock the
2022 * map to avoid deadlocks. we trust that the kernel threads are
2023 * well-behaved, and therefore will not do anything destructive to
2024 * this region of the map while we have it unlocked. we cannot
2025 * trust user threads to do the same.
2026 *
2027 * HACK HACK HACK HACK
2028 */
2029 if (vm_map_pmap(map) == pmap_kernel()) {
2030 vm_map_unlock(map); /* trust me ... */
2031 } else {
2032 vm_map_set_recursive(&map->lock);
2033 lockmgr(&map->lock, LK_DOWNGRADE, (void *)0);
2034 }
2035
2036 rv = 0;
2037 entry = start_entry;
2038 while (entry != &map->header && entry->start < end) {
2039 /*
2040 * if uvm_fault_wire fails for any page we need to undo what has
2041 * been done. we decrement the wiring count for those pages
2042 * which have not yet been wired (now) and unwire those that
2043 * have * (later).
2044 *
2045 * XXX this violates the locking protocol on the map, needs to
2046 * be fixed. [because we only have a read lock on map we
2047 * shouldn't be changing wired_count?]
2048 */
2049 if (rv) {
2050 entry->wired_count--;
2051 } else if (entry->wired_count == 1) {
2052 rv = uvm_fault_wire(map, entry->start, entry->end);
2053 if (rv) {
2054 failed = entry->start;
2055 entry->wired_count--;
2056 }
2057 }
2058 entry = entry->next;
2059 }
2060
2061 if (vm_map_pmap(map) == pmap_kernel()) {
2062 vm_map_lock(map); /* relock */
2063 } else {
2064 vm_map_clear_recursive(&map->lock);
2065 }
2066
2067 if (rv) { /* failed? */
2068 vm_map_unlock(map);
2069 (void) uvm_map_pageable(map, start, failed, TRUE);
2070 UVMHIST_LOG(maphist, "<- done (RV=%d)", rv,0,0,0);
2071 return(rv);
2072 }
2073 vm_map_unlock(map);
2074
2075 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
2076 return(KERN_SUCCESS);
2077 }
2078
2079 /*
2080 * uvm_map_clean: push dirty pages off to backing store.
2081 *
2082 * => valid flags:
2083 * if (flags & PGO_SYNCIO): dirty pages are written synchronously
2084 * if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean
2085 * if (flags & PGO_FREE): any cached pages are freed after clean
2086 * => returns an error if any part of the specified range isn't mapped
2087 * => never a need to flush amap layer since the anonymous memory has
2088 * no permanent home...
2089 * => called from sys_msync()
2090 * => caller must not write-lock map (read OK).
2091 * => we may sleep while cleaning if SYNCIO [with map read-locked]
2092 * => XXX: does this handle share maps properly?
2093 */
2094
2095 int
2096 uvm_map_clean(map, start, end, flags)
2097 vm_map_t map;
2098 vaddr_t start, end;
2099 int flags;
2100 {
2101 vm_map_entry_t current;
2102 vm_map_entry_t entry;
2103 vsize_t size;
2104 struct uvm_object *object;
2105 vaddr_t offset;
2106 UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist);
2107 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,flags=0x%x)",
2108 map, start, end, flags);
2109
2110 vm_map_lock_read(map);
2111 VM_MAP_RANGE_CHECK(map, start, end);
2112 if (!uvm_map_lookup_entry(map, start, &entry)) {
2113 vm_map_unlock_read(map);
2114 return(KERN_INVALID_ADDRESS);
2115 }
2116
2117 /*
2118 * Make a first pass to check for holes.
2119 */
2120 for (current = entry; current->start < end; current = current->next) {
2121 if (UVM_ET_ISSUBMAP(current)) {
2122 vm_map_unlock_read(map);
2123 return(KERN_INVALID_ARGUMENT);
2124 }
2125 if (end > current->end && (current->next == &map->header ||
2126 current->end != current->next->start)) {
2127 vm_map_unlock_read(map);
2128 return(KERN_INVALID_ADDRESS);
2129 }
2130 }
2131
2132 /*
2133 * add "cleanit" flag to flags (for generic flush routine).
2134 * then make a second pass, cleaning/uncaching pages from
2135 * the indicated objects as we go.
2136 */
2137 flags = flags | PGO_CLEANIT;
2138 for (current = entry; current->start < end; current = current->next) {
2139 offset = current->offset + (start - current->start);
2140 size = (end <= current->end ? end : current->end) - start;
2141
2142 /*
2143 * get object/offset. can't be submap (checked above).
2144 */
2145 object = current->object.uvm_obj;
2146 simple_lock(&object->vmobjlock);
2147
2148 /*
2149 * flush pages if we've got a valid backing object.
2150 * note that object is locked.
2151 * XXX should we continue on an error?
2152 */
2153
2154 if (object && object->pgops) {
2155 if (!object->pgops->pgo_flush(object, offset,
2156 offset+size, flags)) {
2157 simple_unlock(&object->vmobjlock);
2158 vm_map_unlock_read(map);
2159 return (KERN_FAILURE);
2160 }
2161 }
2162 simple_unlock(&object->vmobjlock);
2163 start += size;
2164 }
2165 vm_map_unlock_read(map);
2166 return(KERN_SUCCESS);
2167 }
2168
2169
2170 /*
2171 * uvm_map_checkprot: check protection in map
2172 *
2173 * => must allow specified protection in a fully allocated region.
2174 * => map must be read or write locked by caller.
2175 */
2176
2177 boolean_t
2178 uvm_map_checkprot(map, start, end, protection)
2179 vm_map_t map;
2180 vaddr_t start, end;
2181 vm_prot_t protection;
2182 {
2183 vm_map_entry_t entry;
2184 vm_map_entry_t tmp_entry;
2185
2186 if (!uvm_map_lookup_entry(map, start, &tmp_entry)) {
2187 return(FALSE);
2188 }
2189
2190 entry = tmp_entry;
2191
2192 while (start < end) {
2193 if (entry == &map->header) {
2194 return(FALSE);
2195 }
2196
2197 /*
2198 * no holes allowed
2199 */
2200
2201 if (start < entry->start) {
2202 return(FALSE);
2203 }
2204
2205 /*
2206 * check protection associated with entry
2207 */
2208
2209 if ((entry->protection & protection) != protection) {
2210 return(FALSE);
2211 }
2212
2213 /* go to next entry */
2214
2215 start = entry->end;
2216 entry = entry->next;
2217 }
2218 return(TRUE);
2219 }
2220
2221 /*
2222 * uvmspace_alloc: allocate a vmspace structure.
2223 *
2224 * - structure includes vm_map and pmap
2225 * - XXX: no locking on this structure
2226 * - refcnt set to 1, rest must be init'd by caller
2227 */
2228 struct vmspace *
2229 uvmspace_alloc(min, max, pageable)
2230 vaddr_t min, max;
2231 int pageable;
2232 {
2233 struct vmspace *vm;
2234 UVMHIST_FUNC("uvmspace_alloc"); UVMHIST_CALLED(maphist);
2235
2236 vm = pool_get(&uvm_vmspace_pool, PR_WAITOK);
2237 uvmspace_init(vm, NULL, min, max, pageable);
2238 UVMHIST_LOG(maphist,"<- done (vm=0x%x)", vm,0,0,0);
2239 return (vm);
2240 }
2241
2242 /*
2243 * uvmspace_init: initialize a vmspace structure.
2244 *
2245 * - XXX: no locking on this structure
2246 * - refcnt set to 1, rest must me init'd by caller
2247 */
2248 void
2249 uvmspace_init(vm, pmap, min, max, pageable)
2250 struct vmspace *vm;
2251 struct pmap *pmap;
2252 vaddr_t min, max;
2253 boolean_t pageable;
2254 {
2255 UVMHIST_FUNC("uvmspace_init"); UVMHIST_CALLED(maphist);
2256
2257 memset(vm, 0, sizeof(*vm));
2258
2259 uvm_map_setup(&vm->vm_map, min, max, pageable);
2260
2261 if (pmap)
2262 pmap_reference(pmap);
2263 else
2264 #if defined(PMAP_NEW)
2265 pmap = pmap_create();
2266 #else
2267 pmap = pmap_create(0);
2268 #endif
2269 vm->vm_map.pmap = pmap;
2270
2271 vm->vm_refcnt = 1;
2272 UVMHIST_LOG(maphist,"<- done",0,0,0,0);
2273 }
2274
2275 /*
2276 * uvmspace_share: share a vmspace between two proceses
2277 *
2278 * - XXX: no locking on vmspace
2279 * - used for vfork, threads(?)
2280 */
2281
2282 void
2283 uvmspace_share(p1, p2)
2284 struct proc *p1, *p2;
2285 {
2286 p2->p_vmspace = p1->p_vmspace;
2287 p1->p_vmspace->vm_refcnt++;
2288 }
2289
2290 /*
2291 * uvmspace_unshare: ensure that process "p" has its own, unshared, vmspace
2292 *
2293 * - XXX: no locking on vmspace
2294 */
2295
2296 void
2297 uvmspace_unshare(p)
2298 struct proc *p;
2299 {
2300 struct vmspace *nvm, *ovm = p->p_vmspace;
2301 int s;
2302
2303 if (ovm->vm_refcnt == 1)
2304 /* nothing to do: vmspace isn't shared in the first place */
2305 return;
2306
2307 /* make a new vmspace, still holding old one */
2308 nvm = uvmspace_fork(ovm);
2309
2310 s = splhigh(); /* make this `atomic' */
2311 pmap_deactivate(p); /* unbind old vmspace */
2312 p->p_vmspace = nvm;
2313 pmap_activate(p); /* switch to new vmspace */
2314 splx(s); /* end of critical section */
2315
2316 uvmspace_free(ovm); /* drop reference to old vmspace */
2317 }
2318
2319 /*
2320 * uvmspace_exec: the process wants to exec a new program
2321 *
2322 * - XXX: no locking on vmspace
2323 */
2324
2325 void
2326 uvmspace_exec(p)
2327 struct proc *p;
2328 {
2329 struct vmspace *nvm, *ovm = p->p_vmspace;
2330 vm_map_t map = &ovm->vm_map;
2331 int s;
2332
2333 #ifdef sparc
2334 /* XXX cgd 960926: the sparc #ifdef should be a MD hook */
2335 kill_user_windows(p); /* before stack addresses go away */
2336 #endif
2337
2338 /*
2339 * see if more than one process is using this vmspace...
2340 */
2341
2342 if (ovm->vm_refcnt == 1) {
2343
2344 /*
2345 * if p is the only process using its vmspace then we can safely
2346 * recycle that vmspace for the program that is being exec'd.
2347 */
2348
2349 #ifdef SYSVSHM
2350 /*
2351 * SYSV SHM semantics require us to kill all segments on an exec
2352 */
2353 if (ovm->vm_shm)
2354 shmexit(ovm);
2355 #endif
2356
2357 /*
2358 * now unmap the old program
2359 */
2360 uvm_unmap(map, VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
2361
2362 } else {
2363
2364 /*
2365 * p's vmspace is being shared, so we can't reuse it for p since
2366 * it is still being used for others. allocate a new vmspace
2367 * for p
2368 */
2369 nvm = uvmspace_alloc(map->min_offset, map->max_offset,
2370 map->entries_pageable);
2371
2372 #if (defined(i386) || defined(pc532)) && !defined(PMAP_NEW)
2373 /*
2374 * allocate zero fill area in the new vmspace's map for user
2375 * page tables for ports that have old style pmaps that keep
2376 * user page tables in the top part of the process' address
2377 * space.
2378 *
2379 * XXXCDC: this should go away once all pmaps are fixed
2380 */
2381 {
2382 vaddr_t addr = VM_MAXUSER_ADDRESS;
2383 if (uvm_map(&nvm->vm_map, &addr, VM_MAX_ADDRESS - addr,
2384 NULL, UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL,
2385 UVM_PROT_ALL, UVM_INH_NONE, UVM_ADV_NORMAL,
2386 UVM_FLAG_FIXED|UVM_FLAG_COPYONW)) != KERN_SUCCESS)
2387 panic("vm_allocate of PT page area failed");
2388 }
2389 #endif
2390
2391 /*
2392 * install new vmspace and drop our ref to the old one.
2393 */
2394
2395 s = splhigh();
2396 pmap_deactivate(p);
2397 p->p_vmspace = nvm;
2398 pmap_activate(p);
2399 splx(s);
2400
2401 uvmspace_free(ovm);
2402 }
2403 }
2404
2405 /*
2406 * uvmspace_free: free a vmspace data structure
2407 *
2408 * - XXX: no locking on vmspace
2409 */
2410
2411 void
2412 uvmspace_free(vm)
2413 struct vmspace *vm;
2414 {
2415 vm_map_entry_t dead_entries;
2416 UVMHIST_FUNC("uvmspace_free"); UVMHIST_CALLED(maphist);
2417
2418 UVMHIST_LOG(maphist,"(vm=0x%x) ref=%d", vm, vm->vm_refcnt,0,0);
2419 if (--vm->vm_refcnt == 0) {
2420 /*
2421 * lock the map, to wait out all other references to it. delete
2422 * all of the mappings and pages they hold, then call the pmap
2423 * module to reclaim anything left.
2424 */
2425 vm_map_lock(&vm->vm_map);
2426 if (vm->vm_map.nentries) {
2427 (void)uvm_unmap_remove(&vm->vm_map,
2428 vm->vm_map.min_offset, vm->vm_map.max_offset,
2429 &dead_entries);
2430 if (dead_entries != NULL)
2431 uvm_unmap_detach(dead_entries, 0);
2432 }
2433 pmap_destroy(vm->vm_map.pmap);
2434 vm->vm_map.pmap = NULL;
2435 pool_put(&uvm_vmspace_pool, vm);
2436 }
2437 UVMHIST_LOG(maphist,"<- done", 0,0,0,0);
2438 }
2439
2440 /*
2441 * F O R K - m a i n e n t r y p o i n t
2442 */
2443 /*
2444 * uvmspace_fork: fork a process' main map
2445 *
2446 * => create a new vmspace for child process from parent.
2447 * => parent's map must not be locked.
2448 */
2449
2450 struct vmspace *
2451 uvmspace_fork(vm1)
2452 struct vmspace *vm1;
2453 {
2454 struct vmspace *vm2;
2455 vm_map_t old_map = &vm1->vm_map;
2456 vm_map_t new_map;
2457 vm_map_entry_t old_entry;
2458 vm_map_entry_t new_entry;
2459 pmap_t new_pmap;
2460 boolean_t protect_child;
2461 UVMHIST_FUNC("uvmspace_fork"); UVMHIST_CALLED(maphist);
2462
2463 #if (defined(i386) || defined(pc532)) && !defined(PMAP_NEW)
2464 /*
2465 * avoid copying any of the parent's pagetables or other per-process
2466 * objects that reside in the map by marking all of them non-inheritable
2467 * XXXCDC: should go away
2468 */
2469 (void) uvm_map_inherit(old_map, VM_MAXUSER_ADDRESS, VM_MAX_ADDRESS,
2470 VM_INHERIT_NONE);
2471 #endif
2472
2473 vm_map_lock(old_map);
2474
2475 vm2 = uvmspace_alloc(old_map->min_offset, old_map->max_offset,
2476 old_map->entries_pageable);
2477 memcpy(&vm2->vm_startcopy, &vm1->vm_startcopy,
2478 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
2479 new_map = &vm2->vm_map; /* XXX */
2480 new_pmap = new_map->pmap;
2481
2482 old_entry = old_map->header.next;
2483
2484 /*
2485 * go entry-by-entry
2486 */
2487
2488 while (old_entry != &old_map->header) {
2489
2490 /*
2491 * first, some sanity checks on the old entry
2492 */
2493 if (UVM_ET_ISSUBMAP(old_entry))
2494 panic("fork: encountered a submap during fork (illegal)");
2495
2496 if (!UVM_ET_ISCOPYONWRITE(old_entry) &&
2497 UVM_ET_ISNEEDSCOPY(old_entry))
2498 panic("fork: non-copy_on_write map entry marked needs_copy (illegal)");
2499
2500
2501 switch (old_entry->inheritance) {
2502 case VM_INHERIT_NONE:
2503 /*
2504 * drop the mapping
2505 */
2506 break;
2507
2508 case VM_INHERIT_SHARE:
2509 /*
2510 * share the mapping: this means we want the old and
2511 * new entries to share amaps and backing objects.
2512 */
2513
2514 /*
2515 * if the old_entry needs a new amap (due to prev fork)
2516 * then we need to allocate it now so that we have
2517 * something we own to share with the new_entry. [in
2518 * other words, we need to clear needs_copy]
2519 */
2520
2521 if (UVM_ET_ISNEEDSCOPY(old_entry)) {
2522 /* get our own amap, clears needs_copy */
2523 amap_copy(old_map, old_entry, M_WAITOK, FALSE,
2524 0, 0);
2525 /* XXXCDC: WAITOK??? */
2526 }
2527
2528 new_entry = uvm_mapent_alloc(new_map);
2529 /* old_entry -> new_entry */
2530 uvm_mapent_copy(old_entry, new_entry);
2531
2532 /* new pmap has nothing wired in it */
2533 new_entry->wired_count = 0;
2534
2535 /*
2536 * gain reference to object backing the map (can't
2537 * be a submap, already checked this case).
2538 */
2539 if (new_entry->aref.ar_amap)
2540 /* share reference */
2541 amap_ref(new_entry, AMAP_SHARED);
2542
2543 if (new_entry->object.uvm_obj &&
2544 new_entry->object.uvm_obj->pgops->pgo_reference)
2545 new_entry->object.uvm_obj->
2546 pgops->pgo_reference(
2547 new_entry->object.uvm_obj);
2548
2549 /* insert entry at end of new_map's entry list */
2550 uvm_map_entry_link(new_map, new_map->header.prev,
2551 new_entry);
2552
2553 /*
2554 * pmap_copy the mappings: this routine is optional
2555 * but if it is there it will reduce the number of
2556 * page faults in the new proc.
2557 */
2558
2559 pmap_copy(new_pmap, old_map->pmap, new_entry->start,
2560 (old_entry->end - old_entry->start),
2561 old_entry->start);
2562
2563 break;
2564
2565 case VM_INHERIT_COPY:
2566
2567 /*
2568 * copy-on-write the mapping (using mmap's
2569 * MAP_PRIVATE semantics)
2570 *
2571 * allocate new_entry, adjust reference counts.
2572 * (note that new references are read-only).
2573 */
2574
2575 new_entry = uvm_mapent_alloc(new_map);
2576 /* old_entry -> new_entry */
2577 uvm_mapent_copy(old_entry, new_entry);
2578
2579 if (new_entry->aref.ar_amap)
2580 amap_ref(new_entry, 0);
2581
2582 if (new_entry->object.uvm_obj &&
2583 new_entry->object.uvm_obj->pgops->pgo_reference)
2584 new_entry->object.uvm_obj->pgops->pgo_reference
2585 (new_entry->object.uvm_obj);
2586
2587 /* new pmap has nothing wired in it */
2588 new_entry->wired_count = 0;
2589
2590 new_entry->etype |=
2591 (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY);
2592 uvm_map_entry_link(new_map, new_map->header.prev,
2593 new_entry);
2594
2595 /*
2596 * the new entry will need an amap. it will either
2597 * need to be copied from the old entry or created
2598 * from scratch (if the old entry does not have an
2599 * amap). can we defer this process until later
2600 * (by setting "needs_copy") or do we need to copy
2601 * the amap now?
2602 *
2603 * we must copy the amap now if any of the following
2604 * conditions hold:
2605 * 1. the old entry has an amap and that amap is
2606 * being shared. this means that the old (parent)
2607 * process is sharing the amap with another
2608 * process. if we do not clear needs_copy here
2609 * we will end up in a situation where both the
2610 * parent and child process are refering to the
2611 * same amap with "needs_copy" set. if the
2612 * parent write-faults, the fault routine will
2613 * clear "needs_copy" in the parent by allocating
2614 * a new amap. this is wrong because the
2615 * parent is supposed to be sharing the old amap
2616 * and the new amap will break that.
2617 *
2618 * 2. if the old entry has an amap and a non-zero
2619 * wire count then we are going to have to call
2620 * amap_cow_now to avoid page faults in the
2621 * parent process. since amap_cow_now requires
2622 * "needs_copy" to be clear we might as well
2623 * clear it here as well.
2624 *
2625 */
2626
2627 if (old_entry->aref.ar_amap != NULL) {
2628
2629 if ((amap_flags(old_entry->aref.ar_amap) &
2630 AMAP_SHARED) != 0 ||
2631 old_entry->wired_count != 0) {
2632
2633 amap_copy(new_map, new_entry, M_WAITOK, FALSE,
2634 0, 0);
2635 /* XXXCDC: M_WAITOK ... ok? */
2636 }
2637 }
2638
2639 /*
2640 * if the parent's entry is wired down, then the
2641 * parent process does not want page faults on
2642 * access to that memory. this means that we
2643 * cannot do copy-on-write because we can't write
2644 * protect the old entry. in this case we
2645 * resolve all copy-on-write faults now, using
2646 * amap_cow_now. note that we have already
2647 * allocated any needed amap (above).
2648 */
2649
2650 if (old_entry->wired_count != 0) {
2651
2652 /*
2653 * resolve all copy-on-write faults now
2654 * (note that there is nothing to do if
2655 * the old mapping does not have an amap).
2656 * XXX: is it worthwhile to bother with pmap_copy
2657 * in this case?
2658 */
2659 if (old_entry->aref.ar_amap)
2660 amap_cow_now(new_map, new_entry);
2661
2662 } else {
2663
2664 /*
2665 * setup mappings to trigger copy-on-write faults
2666 * we must write-protect the parent if it has
2667 * an amap and it is not already "needs_copy"...
2668 * if it is already "needs_copy" then the parent
2669 * has already been write-protected by a previous
2670 * fork operation.
2671 *
2672 * if we do not write-protect the parent, then
2673 * we must be sure to write-protect the child
2674 * after the pmap_copy() operation.
2675 *
2676 * XXX: pmap_copy should have some way of telling
2677 * us that it didn't do anything so we can avoid
2678 * calling pmap_protect needlessly.
2679 */
2680
2681 if (old_entry->aref.ar_amap) {
2682
2683 if (!UVM_ET_ISNEEDSCOPY(old_entry)) {
2684 if (old_entry->max_protection & VM_PROT_WRITE) {
2685 pmap_protect(old_map->pmap,
2686 old_entry->start,
2687 old_entry->end,
2688 old_entry->protection &
2689 ~VM_PROT_WRITE);
2690 }
2691 old_entry->etype |= UVM_ET_NEEDSCOPY;
2692 }
2693
2694 /*
2695 * parent must now be write-protected
2696 */
2697 protect_child = FALSE;
2698 } else {
2699
2700 /*
2701 * we only need to protect the child if the
2702 * parent has write access.
2703 */
2704 if (old_entry->max_protection & VM_PROT_WRITE)
2705 protect_child = TRUE;
2706 else
2707 protect_child = FALSE;
2708
2709 }
2710
2711 /*
2712 * copy the mappings
2713 * XXX: need a way to tell if this does anything
2714 */
2715
2716 pmap_copy(new_pmap, old_map->pmap,
2717 new_entry->start,
2718 (old_entry->end - old_entry->start),
2719 old_entry->start);
2720
2721 /*
2722 * protect the child's mappings if necessary
2723 */
2724 if (protect_child) {
2725 pmap_protect(new_pmap, new_entry->start,
2726 new_entry->end,
2727 new_entry->protection &
2728 ~VM_PROT_WRITE);
2729 }
2730
2731 }
2732 break;
2733 } /* end of switch statement */
2734 old_entry = old_entry->next;
2735 }
2736
2737 new_map->size = old_map->size;
2738 vm_map_unlock(old_map);
2739
2740 #if (defined(i386) || defined(pc532)) && !defined(PMAP_NEW)
2741 /*
2742 * allocate zero fill area in the new vmspace's map for user
2743 * page tables for ports that have old style pmaps that keep
2744 * user page tables in the top part of the process' address
2745 * space.
2746 *
2747 * XXXCDC: this should go away once all pmaps are fixed
2748 */
2749 {
2750 vaddr_t addr = VM_MAXUSER_ADDRESS;
2751 if (uvm_map(new_map, &addr, VM_MAX_ADDRESS - addr, NULL,
2752 UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL,
2753 UVM_PROT_ALL, UVM_INH_NONE, UVM_ADV_NORMAL,
2754 UVM_FLAG_FIXED|UVM_FLAG_COPYONW)) != KERN_SUCCESS)
2755 panic("vm_allocate of PT page area failed");
2756 }
2757 #endif
2758
2759 #ifdef SYSVSHM
2760 if (vm1->vm_shm)
2761 shmfork(vm1, vm2);
2762 #endif
2763
2764 UVMHIST_LOG(maphist,"<- done",0,0,0,0);
2765 return(vm2);
2766 }
2767
2768
2769 #if defined(DDB)
2770
2771 /*
2772 * DDB hooks
2773 */
2774
2775 /*
2776 * uvm_map_print: print out a map
2777 */
2778
2779 void
2780 uvm_map_print(map, full)
2781 vm_map_t map;
2782 boolean_t full;
2783 {
2784
2785 uvm_map_printit(map, full, printf);
2786 }
2787
2788 /*
2789 * uvm_map_printit: actually prints the map
2790 */
2791
2792 void
2793 uvm_map_printit(map, full, pr)
2794 vm_map_t map;
2795 boolean_t full;
2796 void (*pr) __P((const char *, ...));
2797 {
2798 vm_map_entry_t entry;
2799
2800 (*pr)("MAP %p: [0x%lx->0x%lx]\n", map, map->min_offset,map->max_offset);
2801 (*pr)("\t#ent=%d, sz=%d, ref=%d, version=%d\n",
2802 map->nentries, map->size, map->ref_count, map->timestamp);
2803 #ifdef pmap_resident_count
2804 (*pr)("\tpmap=%p(resident=%d)\n", map->pmap,
2805 pmap_resident_count(map->pmap));
2806 #else
2807 /* XXXCDC: this should be required ... */
2808 (*pr)("\tpmap=%p(resident=<<NOT SUPPORTED!!!>>)\n", map->pmap);
2809 #endif
2810 if (!full)
2811 return;
2812 for (entry = map->header.next; entry != &map->header;
2813 entry = entry->next) {
2814 (*pr)(" - %p: 0x%lx->0x%lx: obj=%p/0x%x, amap=%p/%d\n",
2815 entry, entry->start, entry->end, entry->object.uvm_obj,
2816 entry->offset, entry->aref.ar_amap, entry->aref.ar_pageoff);
2817 (*pr)(
2818 "\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, wc=%d, adv=%d\n",
2819 (entry->etype & UVM_ET_SUBMAP) ? 'T' : 'F',
2820 (entry->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F',
2821 (entry->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F',
2822 entry->protection, entry->max_protection,
2823 entry->inheritance, entry->wired_count, entry->advice);
2824 }
2825 }
2826
2827 /*
2828 * uvm_object_print: print out an object
2829 */
2830
2831 void
2832 uvm_object_print(uobj, full)
2833 struct uvm_object *uobj;
2834 boolean_t full;
2835 {
2836
2837 uvm_object_printit(uobj, full, printf);
2838 }
2839
2840 /*
2841 * uvm_object_printit: actually prints the object
2842 */
2843
2844 void
2845 uvm_object_printit(uobj, full, pr)
2846 struct uvm_object *uobj;
2847 boolean_t full;
2848 void (*pr) __P((const char *, ...));
2849 {
2850 struct vm_page *pg;
2851 int cnt = 0;
2852
2853 (*pr)("OBJECT %p: pgops=%p, npages=%d, ", uobj, uobj->pgops,
2854 uobj->uo_npages);
2855 if (uobj->uo_refs == UVM_OBJ_KERN)
2856 (*pr)("refs=<SYSTEM>\n");
2857 else
2858 (*pr)("refs=%d\n", uobj->uo_refs);
2859
2860 if (!full) return;
2861 (*pr)(" PAGES <pg,offset>:\n ");
2862 for (pg = uobj->memq.tqh_first ; pg ; pg = pg->listq.tqe_next, cnt++) {
2863 (*pr)("<%p,0x%lx> ", pg, pg->offset);
2864 if ((cnt % 3) == 2) (*pr)("\n ");
2865 }
2866 if ((cnt % 3) != 2) (*pr)("\n");
2867 }
2868
2869 /*
2870 * uvm_page_print: print out a page
2871 */
2872
2873 void
2874 uvm_page_print(pg, full)
2875 struct vm_page *pg;
2876 boolean_t full;
2877 {
2878
2879 uvm_page_printit(pg, full, printf);
2880 }
2881
2882 /*
2883 * uvm_page_printit: actually print the page
2884 */
2885
2886 void
2887 uvm_page_printit(pg, full, pr)
2888 struct vm_page *pg;
2889 boolean_t full;
2890 void (*pr) __P((const char *, ...));
2891 {
2892 struct vm_page *lcv;
2893 struct uvm_object *uobj;
2894 struct pglist *pgl;
2895
2896 (*pr)("PAGE %p:\n", pg);
2897 (*pr)(" flags=0x%x, pqflags=0x%x, vers=%d, wire_count=%d, pa=0x%lx\n",
2898 pg->flags, pg->pqflags, pg->version, pg->wire_count, (long)pg->phys_addr);
2899 (*pr)(" uobject=%p, uanon=%p, offset=0x%lx loan_count=%d\n",
2900 pg->uobject, pg->uanon, pg->offset, pg->loan_count);
2901 #if defined(UVM_PAGE_TRKOWN)
2902 if (pg->flags & PG_BUSY)
2903 (*pr)(" owning process = %d, tag=%s\n",
2904 pg->owner, pg->owner_tag);
2905 else
2906 (*pr)(" page not busy, no owner\n");
2907 #else
2908 (*pr)(" [page ownership tracking disabled]\n");
2909 #endif
2910
2911 if (!full)
2912 return;
2913
2914 /* cross-verify object/anon */
2915 if ((pg->pqflags & PQ_FREE) == 0) {
2916 if (pg->pqflags & PQ_ANON) {
2917 if (pg->uanon == NULL || pg->uanon->u.an_page != pg)
2918 (*pr)(" >>> ANON DOES NOT POINT HERE <<< (%p)\n",
2919 (pg->uanon) ? pg->uanon->u.an_page : NULL);
2920 else
2921 (*pr)(" anon backpointer is OK\n");
2922 } else {
2923 uobj = pg->uobject;
2924 if (uobj) {
2925 (*pr)(" checking object list\n");
2926 for (lcv = uobj->memq.tqh_first ; lcv ;
2927 lcv = lcv->listq.tqe_next) {
2928 if (lcv == pg) break;
2929 }
2930 if (lcv)
2931 (*pr)(" page found on object list\n");
2932 else
2933 (*pr)(" >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n");
2934 }
2935 }
2936 }
2937
2938 /* cross-verify page queue */
2939 if (pg->pqflags & PQ_FREE)
2940 pgl = &uvm.page_free[uvm_page_lookup_freelist(pg)];
2941 else if (pg->pqflags & PQ_INACTIVE)
2942 pgl = (pg->pqflags & PQ_SWAPBACKED) ?
2943 &uvm.page_inactive_swp : &uvm.page_inactive_obj;
2944 else if (pg->pqflags & PQ_ACTIVE)
2945 pgl = &uvm.page_active;
2946 else
2947 pgl = NULL;
2948
2949 if (pgl) {
2950 (*pr)(" checking pageq list\n");
2951 for (lcv = pgl->tqh_first ; lcv ; lcv = lcv->pageq.tqe_next) {
2952 if (lcv == pg) break;
2953 }
2954 if (lcv)
2955 (*pr)(" page found on pageq list\n");
2956 else
2957 (*pr)(" >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n");
2958 }
2959 }
2960 #endif
2961