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