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