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