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