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