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