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