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