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