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