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