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