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