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