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