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