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