kernel - TMPFS - Stabilization pass, fix vnode list corruption
[dragonfly.git] / sys / vm / vm_map.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
37 *
38 *
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
41 *
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43 *
44 * Permission to use, copy, modify and distribute this software and
45 * its documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
49 *
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53 *
54 * Carnegie Mellon requests users of this software to return to
55 *
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
60 *
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
63 *
64 * $FreeBSD: src/sys/vm/vm_map.c,v 1.187.2.19 2003/05/27 00:47:02 alc Exp $
e3161323 65 * $DragonFly: src/sys/vm/vm_map.c,v 1.56 2007/04/29 18:25:41 dillon Exp $
984263bc
MD
66 */
67
68/*
69 * Virtual memory mapping module.
70 */
71
72#include <sys/param.h>
73#include <sys/systm.h>
e3161323 74#include <sys/kernel.h>
984263bc 75#include <sys/proc.h>
fef0fdf2 76#include <sys/lock.h>
984263bc
MD
77#include <sys/vmmeter.h>
78#include <sys/mman.h>
79#include <sys/vnode.h>
80#include <sys/resourcevar.h>
fef0fdf2 81#include <sys/shm.h>
686dbf64 82#include <sys/tree.h>
e3161323 83#include <sys/malloc.h>
984263bc
MD
84
85#include <vm/vm.h>
86#include <vm/vm_param.h>
984263bc
MD
87#include <vm/pmap.h>
88#include <vm/vm_map.h>
89#include <vm/vm_page.h>
90#include <vm/vm_object.h>
91#include <vm/vm_pager.h>
92#include <vm/vm_kern.h>
93#include <vm/vm_extern.h>
94#include <vm/swap_pager.h>
95#include <vm/vm_zone.h>
96
a108bf71 97#include <sys/thread2.h>
e3161323 98#include <sys/sysref2.h>
a108bf71 99
984263bc
MD
100/*
101 * Virtual memory maps provide for the mapping, protection,
102 * and sharing of virtual memory objects. In addition,
103 * this module provides for an efficient virtual copy of
104 * memory from one map to another.
105 *
106 * Synchronization is required prior to most operations.
107 *
108 * Maps consist of an ordered doubly-linked list of simple
109 * entries; a single hint is used to speed up lookups.
110 *
111 * Since portions of maps are specified by start/end addresses,
112 * which may not align with existing map entries, all
113 * routines merely "clip" entries to these start/end values.
114 * [That is, an entry is split into two, bordering at a
115 * start or end value.] Note that these clippings may not
116 * always be necessary (as the two resulting entries are then
117 * not changed); however, the clipping is done for convenience.
118 *
119 * As mentioned above, virtual copy operations are performed
120 * by copying VM object references from one map to
121 * another, and then marking both regions as copy-on-write.
122 */
123
e3161323 124static void vmspace_terminate(struct vmspace *vm);
e654922c
MD
125static void vmspace_lock(struct vmspace *vm);
126static void vmspace_unlock(struct vmspace *vm);
e3161323
MD
127static void vmspace_dtor(void *obj, void *private);
128
129MALLOC_DEFINE(M_VMSPACE, "vmspace", "vmspace objcache backingstore");
130
131struct sysref_class vmspace_sysref_class = {
132 .name = "vmspace",
133 .mtype = M_VMSPACE,
134 .proto = SYSREF_PROTO_VMSPACE,
135 .offset = offsetof(struct vmspace, vm_sysref),
136 .objsize = sizeof(struct vmspace),
137 .mag_capacity = 32,
138 .flags = SRC_MANAGEDINIT,
139 .dtor = vmspace_dtor,
140 .ops = {
e654922c
MD
141 .terminate = (sysref_terminate_func_t)vmspace_terminate,
142 .lock = (sysref_lock_func_t)vmspace_lock,
143 .unlock = (sysref_lock_func_t)vmspace_unlock
e3161323
MD
144 }
145};
984263bc 146
c4ae567f
MD
147#define VMEPERCPU 2
148
a108bf71 149static struct vm_zone mapentzone_store, mapzone_store;
e3161323 150static vm_zone_t mapentzone, mapzone;
a108bf71 151static struct vm_object mapentobj, mapobj;
984263bc
MD
152
153static struct vm_map_entry map_entry_init[MAX_MAPENT];
c4ae567f 154static struct vm_map_entry cpu_map_entry_init[MAXCPU][VMEPERCPU];
984263bc
MD
155static struct vm_map map_init[MAX_KMAP];
156
53025830 157static void vm_map_entry_shadow(vm_map_entry_t entry);
a108bf71
MD
158static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
159static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
160static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
161static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
162static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
1388df65
RG
163static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
164static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
165 vm_map_entry_t);
166static void vm_map_split (vm_map_entry_t);
a108bf71 167static void vm_map_unclip_range (vm_map_t map, vm_map_entry_t start_entry, vm_offset_t start, vm_offset_t end, int *count, int flags);
984263bc 168
e3161323
MD
169/*
170 * vm_map_startup:
171 *
172 * Initialize the vm_map module. Must be called before
173 * any other vm_map routines.
174 *
175 * Map and entry structures are allocated from the general
176 * purpose memory pool with some exceptions:
177 *
178 * - The kernel map and kmem submap are allocated statically.
179 * - Kernel map entries are allocated out of a static pool.
180 *
181 * These restrictions are necessary since malloc() uses the
182 * maps and requires map entries.
183 */
984263bc 184void
57e43348 185vm_map_startup(void)
984263bc
MD
186{
187 mapzone = &mapzone_store;
188 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
189 map_init, MAX_KMAP);
984263bc
MD
190 mapentzone = &mapentzone_store;
191 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
192 map_entry_init, MAX_MAPENT);
193}
194
e3161323
MD
195/*
196 * vm_init2 - called prior to any vmspace allocations
197 */
198void
199vm_init2(void)
200{
201 zinitna(mapentzone, &mapentobj, NULL, 0, 0,
202 ZONE_USE_RESERVE | ZONE_SPECIAL, 1);
203 zinitna(mapzone, &mapobj, NULL, 0, 0, 0, 1);
204 pmap_init2();
205 vm_object_init2();
206}
207
208
686dbf64
MD
209/*
210 * Red black tree functions
211 */
212static int rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b);
213RB_GENERATE(vm_map_rb_tree, vm_map_entry, rb_entry, rb_vm_map_compare);
214
215/* a->start is address, and the only field has to be initialized */
216static int
217rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b)
218{
219 if (a->start < b->start)
220 return(-1);
221 else if (a->start > b->start)
222 return(1);
223 return(0);
224}
225
984263bc 226/*
e3161323
MD
227 * Allocate a vmspace structure, including a vm_map and pmap.
228 * Initialize numerous fields. While the initial allocation is zerod,
229 * subsequence reuse from the objcache leaves elements of the structure
230 * intact (particularly the pmap), so portions must be zerod.
231 *
232 * The structure is not considered activated until we call sysref_activate().
984263bc
MD
233 */
234struct vmspace *
57e43348 235vmspace_alloc(vm_offset_t min, vm_offset_t max)
984263bc
MD
236{
237 struct vmspace *vm;
238
e3161323 239 vm = sysref_alloc(&vmspace_sysref_class);
54a764e8 240 bzero(&vm->vm_startcopy,
e3161323 241 (char *)&vm->vm_endcopy - (char *)&vm->vm_startcopy);
e4846942 242 vm_map_init(&vm->vm_map, min, max, NULL);
e3161323 243 pmap_pinit(vmspace_pmap(vm)); /* (some fields reused) */
984263bc 244 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
984263bc
MD
245 vm->vm_shm = NULL;
246 vm->vm_exitingcnt = 0;
135d7199 247 cpu_vmspace_alloc(vm);
e3161323 248 sysref_activate(&vm->vm_sysref);
984263bc
MD
249 return (vm);
250}
251
e3161323
MD
252/*
253 * dtor function - Some elements of the pmap are retained in the
254 * free-cached vmspaces to improve performance. We have to clean them up
255 * here before returning the vmspace to the memory pool.
256 */
257static void
258vmspace_dtor(void *obj, void *private)
a108bf71 259{
e3161323
MD
260 struct vmspace *vm = obj;
261
262 pmap_puninit(vmspace_pmap(vm));
984263bc
MD
263}
264
e3161323
MD
265/*
266 * Called in two cases:
267 *
268 * (1) When the last sysref is dropped, but exitingcnt might still be
269 * non-zero.
270 *
271 * (2) When there are no sysrefs (i.e. refcnt is negative) left and the
272 * exitingcnt becomes zero
273 *
274 * sysref will not scrap the object until we call sysref_put() once more
275 * after the last ref has been dropped.
276 */
277static void
278vmspace_terminate(struct vmspace *vm)
984263bc 279{
a108bf71
MD
280 int count;
281
e3161323
MD
282 /*
283 * If exitingcnt is non-zero we can't get rid of the entire vmspace
284 * yet, but we can scrap user memory.
285 */
286 if (vm->vm_exitingcnt) {
287 shmexit(vm);
288 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
289 VM_MAX_USER_ADDRESS);
290 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
291 VM_MAX_USER_ADDRESS);
292
293 return;
294 }
135d7199
MD
295 cpu_vmspace_free(vm);
296
fef0fdf2
MD
297 /*
298 * Make sure any SysV shm is freed, it might not have in
299 * exit1()
300 */
301 shmexit(vm);
302
a722be49
MD
303 KKASSERT(vm->vm_upcalls == NULL);
304
984263bc
MD
305 /*
306 * Lock the map, to wait out all other references to it.
307 * Delete all of the mappings and pages they hold, then call
308 * the pmap module to reclaim anything left.
309 */
a108bf71 310 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc 311 vm_map_lock(&vm->vm_map);
a108bf71
MD
312 vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
313 vm->vm_map.max_offset, &count);
984263bc 314 vm_map_unlock(&vm->vm_map);
a108bf71 315 vm_map_entry_release(count);
984263bc
MD
316
317 pmap_release(vmspace_pmap(vm));
e3161323 318 sysref_put(&vm->vm_sysref);
984263bc
MD
319}
320
e654922c
MD
321static void
322vmspace_lock(struct vmspace *vm __unused)
323{
324}
325
326static void
327vmspace_unlock(struct vmspace *vm __unused)
328{
329}
330
e3161323
MD
331/*
332 * This is called in the wait*() handling code. The vmspace can be terminated
333 * after the last wait is finished using it.
334 */
984263bc
MD
335void
336vmspace_exitfree(struct proc *p)
337{
338 struct vmspace *vm;
339
340 vm = p->p_vmspace;
341 p->p_vmspace = NULL;
342
e3161323
MD
343 if (--vm->vm_exitingcnt == 0 && sysref_isinactive(&vm->vm_sysref))
344 vmspace_terminate(vm);
984263bc
MD
345}
346
347/*
20479584 348 * vmspace_swap_count()
984263bc
MD
349 *
350 * Swap useage is determined by taking the proportional swap used by
351 * VM objects backing the VM map. To make up for fractional losses,
352 * if the VM object has any swap use at all the associated map entries
353 * count for at least 1 swap page.
354 */
355int
356vmspace_swap_count(struct vmspace *vmspace)
357{
358 vm_map_t map = &vmspace->vm_map;
359 vm_map_entry_t cur;
1b874851 360 vm_object_t object;
984263bc 361 int count = 0;
1b874851 362 int n;
984263bc
MD
363
364 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
1b874851
MD
365 switch(cur->maptype) {
366 case VM_MAPTYPE_NORMAL:
367 case VM_MAPTYPE_VPAGETABLE:
368 if ((object = cur->object.vm_object) == NULL)
369 break;
96adc753
MD
370 if (object->swblock_count) {
371 n = (cur->end - cur->start) / PAGE_SIZE;
372 count += object->swblock_count *
984263bc
MD
373 SWAP_META_PAGES * n / object->size + 1;
374 }
1b874851
MD
375 break;
376 default:
377 break;
984263bc
MD
378 }
379 }
380 return(count);
381}
382
20479584
MD
383/*
384 * vmspace_anonymous_count()
385 *
386 * Calculate the approximate number of anonymous pages in use by
387 * this vmspace. To make up for fractional losses, we count each
388 * VM object as having at least 1 anonymous page.
389 */
390int
391vmspace_anonymous_count(struct vmspace *vmspace)
392{
393 vm_map_t map = &vmspace->vm_map;
394 vm_map_entry_t cur;
395 vm_object_t object;
396 int count = 0;
397
398 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
399 switch(cur->maptype) {
400 case VM_MAPTYPE_NORMAL:
401 case VM_MAPTYPE_VPAGETABLE:
402 if ((object = cur->object.vm_object) == NULL)
403 break;
404 if (object->type != OBJT_DEFAULT &&
405 object->type != OBJT_SWAP) {
406 break;
407 }
408 count += object->resident_page_count;
409 break;
410 default:
411 break;
412 }
413 }
414 return(count);
415}
416
417
418
984263bc
MD
419
420/*
421 * vm_map_create:
422 *
423 * Creates and returns a new empty VM map with
424 * the given physical map structure, and having
425 * the given lower and upper address bounds.
426 */
427vm_map_t
e4846942 428vm_map_create(vm_map_t result, pmap_t pmap, vm_offset_t min, vm_offset_t max)
984263bc 429{
e4846942
MD
430 if (result == NULL)
431 result = zalloc(mapzone);
432 vm_map_init(result, min, max, pmap);
984263bc
MD
433 return (result);
434}
435
436/*
437 * Initialize an existing vm_map structure
438 * such as that in the vmspace structure.
439 * The pmap is set elsewhere.
440 */
441void
e4846942 442vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max, pmap_t pmap)
984263bc
MD
443{
444 map->header.next = map->header.prev = &map->header;
686dbf64 445 RB_INIT(&map->rb_root);
984263bc
MD
446 map->nentries = 0;
447 map->size = 0;
448 map->system_map = 0;
449 map->infork = 0;
450 map->min_offset = min;
451 map->max_offset = max;
e4846942 452 map->pmap = pmap;
984263bc
MD
453 map->first_free = &map->header;
454 map->hint = &map->header;
455 map->timestamp = 0;
ab6f251b 456 lockinit(&map->lock, "thrd_sleep", 0, 0);
984263bc
MD
457}
458
53025830
MD
459/*
460 * Shadow the vm_map_entry's object. This typically needs to be done when
461 * a write fault is taken on an entry which had previously been cloned by
462 * fork(). The shared object (which might be NULL) must become private so
463 * we add a shadow layer above it.
464 *
465 * Object allocation for anonymous mappings is defered as long as possible.
466 * When creating a shadow, however, the underlying object must be instantiated
467 * so it can be shared.
468 *
469 * If the map segment is governed by a virtual page table then it is
470 * possible to address offsets beyond the mapped area. Just allocate
471 * a maximally sized object for this case.
472 */
473static
474void
475vm_map_entry_shadow(vm_map_entry_t entry)
476{
477 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
478 vm_object_shadow(&entry->object.vm_object, &entry->offset,
479 0x7FFFFFFF); /* XXX */
480 } else {
481 vm_object_shadow(&entry->object.vm_object, &entry->offset,
482 atop(entry->end - entry->start));
483 }
484 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
485}
486
487/*
488 * Allocate an object for a vm_map_entry.
489 *
490 * Object allocation for anonymous mappings is defered as long as possible.
491 * This function is called when we can defer no longer, generally when a map
492 * entry might be split or forked or takes a page fault.
493 *
494 * If the map segment is governed by a virtual page table then it is
495 * possible to address offsets beyond the mapped area. Just allocate
496 * a maximally sized object for this case.
497 */
498void
499vm_map_entry_allocate_object(vm_map_entry_t entry)
500{
501 vm_object_t obj;
502
503 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
504 obj = vm_object_allocate(OBJT_DEFAULT, 0x7FFFFFFF); /* XXX */
505 } else {
506 obj = vm_object_allocate(OBJT_DEFAULT,
507 atop(entry->end - entry->start));
508 }
509 entry->object.vm_object = obj;
510 entry->offset = 0;
511}
512
41a01a4d 513/*
c4ae567f 514 * vm_map_entry_reserve_cpu_init:
41a01a4d
MD
515 *
516 * Set an initial negative count so the first attempt to reserve
c4ae567f
MD
517 * space preloads a bunch of vm_map_entry's for this cpu. Also
518 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
519 * map a new page for vm_map_entry structures. SMP systems are
520 * particularly sensitive.
521 *
522 * This routine is called in early boot so we cannot just call
41a01a4d
MD
523 * vm_map_entry_reserve().
524 *
c4ae567f
MD
525 * May be called for a gd other then mycpu, but may only be called
526 * during early boot.
41a01a4d
MD
527 */
528void
529vm_map_entry_reserve_cpu_init(globaldata_t gd)
530{
c4ae567f
MD
531 vm_map_entry_t entry;
532 int i;
533
41a01a4d 534 gd->gd_vme_avail -= MAP_RESERVE_COUNT * 2;
c4ae567f
MD
535 entry = &cpu_map_entry_init[gd->gd_cpuid][0];
536 for (i = 0; i < VMEPERCPU; ++i, ++entry) {
537 entry->next = gd->gd_vme_base;
538 gd->gd_vme_base = entry;
539 }
41a01a4d
MD
540}
541
a108bf71
MD
542/*
543 * vm_map_entry_reserve:
544 *
e1359933
MD
545 * Reserves vm_map_entry structures so code later on can manipulate
546 * map_entry structures within a locked map without blocking trying
547 * to allocate a new vm_map_entry.
a108bf71
MD
548 */
549int
550vm_map_entry_reserve(int count)
551{
552 struct globaldata *gd = mycpu;
553 vm_map_entry_t entry;
554
555 crit_enter();
a108bf71
MD
556
557 /*
558 * Make sure we have enough structures in gd_vme_base to handle
559 * the reservation request.
560 */
ac13eccd 561 while (gd->gd_vme_avail < count) {
a108bf71
MD
562 entry = zalloc(mapentzone);
563 entry->next = gd->gd_vme_base;
564 gd->gd_vme_base = entry;
565 ++gd->gd_vme_avail;
566 }
ac13eccd 567 gd->gd_vme_avail -= count;
a108bf71
MD
568 crit_exit();
569 return(count);
570}
571
572/*
573 * vm_map_entry_release:
574 *
575 * Releases previously reserved vm_map_entry structures that were not
576 * used. If we have too much junk in our per-cpu cache clean some of
577 * it out.
578 */
579void
580vm_map_entry_release(int count)
581{
582 struct globaldata *gd = mycpu;
583 vm_map_entry_t entry;
584
585 crit_enter();
586 gd->gd_vme_avail += count;
587 while (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
588 entry = gd->gd_vme_base;
589 KKASSERT(entry != NULL);
590 gd->gd_vme_base = entry->next;
591 --gd->gd_vme_avail;
592 crit_exit();
593 zfree(mapentzone, entry);
594 crit_enter();
595 }
596 crit_exit();
597}
598
599/*
600 * vm_map_entry_kreserve:
601 *
c4ae567f
MD
602 * Reserve map entry structures for use in kernel_map itself. These
603 * entries have *ALREADY* been reserved on a per-cpu basis when the map
604 * was inited. This function is used by zalloc() to avoid a recursion
605 * when zalloc() itself needs to allocate additional kernel memory.
606 *
607 * This function works like the normal reserve but does not load the
608 * vm_map_entry cache (because that would result in an infinite
609 * recursion). Note that gd_vme_avail may go negative. This is expected.
a108bf71 610 *
c4ae567f
MD
611 * Any caller of this function must be sure to renormalize after
612 * potentially eating entries to ensure that the reserve supply
613 * remains intact.
a108bf71
MD
614 */
615int
616vm_map_entry_kreserve(int count)
617{
618 struct globaldata *gd = mycpu;
619
620 crit_enter();
c4ae567f 621 gd->gd_vme_avail -= count;
a108bf71 622 crit_exit();
c4ae567f 623 KASSERT(gd->gd_vme_base != NULL, ("no reserved entries left, gd_vme_avail = %d\n", gd->gd_vme_avail));
a108bf71
MD
624 return(count);
625}
626
627/*
628 * vm_map_entry_krelease:
629 *
c4ae567f
MD
630 * Release previously reserved map entries for kernel_map. We do not
631 * attempt to clean up like the normal release function as this would
632 * cause an unnecessary (but probably not fatal) deep procedure call.
a108bf71
MD
633 */
634void
635vm_map_entry_krelease(int count)
636{
637 struct globaldata *gd = mycpu;
638
639 crit_enter();
c4ae567f 640 gd->gd_vme_avail += count;
a108bf71
MD
641 crit_exit();
642}
643
984263bc 644/*
8a8d5d85 645 * vm_map_entry_create: [ internal use only ]
984263bc 646 *
8a8d5d85 647 * Allocates a VM map entry for insertion. No entry fields are filled
a108bf71
MD
648 * in.
649 *
650 * This routine may be called from an interrupt thread but not a FAST
651 * interrupt. This routine may recurse the map lock.
984263bc 652 */
8a8d5d85 653static vm_map_entry_t
a108bf71 654vm_map_entry_create(vm_map_t map, int *countp)
984263bc 655{
a108bf71
MD
656 struct globaldata *gd = mycpu;
657 vm_map_entry_t entry;
8a8d5d85 658
a108bf71
MD
659 KKASSERT(*countp > 0);
660 --*countp;
661 crit_enter();
662 entry = gd->gd_vme_base;
663 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
664 gd->gd_vme_base = entry->next;
665 crit_exit();
666 return(entry);
984263bc
MD
667}
668
669/*
8a8d5d85 670 * vm_map_entry_dispose: [ internal use only ]
984263bc 671 *
8a8d5d85
MD
672 * Dispose of a vm_map_entry that is no longer being referenced. This
673 * function may be called from an interrupt.
984263bc 674 */
8a8d5d85 675static void
a108bf71 676vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
984263bc 677{
a108bf71
MD
678 struct globaldata *gd = mycpu;
679
686dbf64
MD
680 KKASSERT(map->hint != entry);
681 KKASSERT(map->first_free != entry);
682
a108bf71
MD
683 ++*countp;
684 crit_enter();
685 entry->next = gd->gd_vme_base;
686 gd->gd_vme_base = entry;
687 crit_exit();
984263bc
MD
688}
689
8a8d5d85 690
984263bc
MD
691/*
692 * vm_map_entry_{un,}link:
693 *
694 * Insert/remove entries from maps.
695 */
696static __inline void
697vm_map_entry_link(vm_map_t map,
698 vm_map_entry_t after_where,
699 vm_map_entry_t entry)
700{
701 map->nentries++;
702 entry->prev = after_where;
703 entry->next = after_where->next;
704 entry->next->prev = entry;
705 after_where->next = entry;
0cd275af
MD
706 if (vm_map_rb_tree_RB_INSERT(&map->rb_root, entry))
707 panic("vm_map_entry_link: dup addr map %p ent %p", map, entry);
984263bc
MD
708}
709
710static __inline void
711vm_map_entry_unlink(vm_map_t map,
712 vm_map_entry_t entry)
713{
714 vm_map_entry_t prev;
715 vm_map_entry_t next;
716
717 if (entry->eflags & MAP_ENTRY_IN_TRANSITION)
718 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry);
719 prev = entry->prev;
720 next = entry->next;
721 next->prev = prev;
722 prev->next = next;
686dbf64 723 vm_map_rb_tree_RB_REMOVE(&map->rb_root, entry);
984263bc
MD
724 map->nentries--;
725}
726
984263bc
MD
727/*
728 * vm_map_lookup_entry: [ internal use only ]
729 *
730 * Finds the map entry containing (or
731 * immediately preceding) the specified address
732 * in the given map; the entry is returned
733 * in the "entry" parameter. The boolean
734 * result indicates whether the address is
735 * actually contained in the map.
736 */
737boolean_t
57e43348
MD
738vm_map_lookup_entry(vm_map_t map, vm_offset_t address,
739 vm_map_entry_t *entry /* OUT */)
984263bc 740{
686dbf64 741 vm_map_entry_t tmp;
984263bc
MD
742 vm_map_entry_t last;
743
686dbf64 744#if 0
984263bc 745 /*
686dbf64
MD
746 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive
747 * the hint code with the red-black lookup meets with system crashes
748 * and lockups. We do not yet know why.
749 *
750 * It is possible that the problem is related to the setting
751 * of the hint during map_entry deletion, in the code specified
752 * at the GGG comment later on in this file.
984263bc 753 */
686dbf64
MD
754 /*
755 * Quickly check the cached hint, there's a good chance of a match.
756 */
757 if (map->hint != &map->header) {
758 tmp = map->hint;
759 if (address >= tmp->start && address < tmp->end) {
760 *entry = tmp;
761 return(TRUE);
984263bc 762 }
984263bc 763 }
686dbf64 764#endif
984263bc
MD
765
766 /*
686dbf64
MD
767 * Locate the record from the top of the tree. 'last' tracks the
768 * closest prior record and is returned if no match is found, which
769 * in binary tree terms means tracking the most recent right-branch
770 * taken. If there is no prior record, &map->header is returned.
984263bc 771 */
686dbf64
MD
772 last = &map->header;
773 tmp = RB_ROOT(&map->rb_root);
774
775 while (tmp) {
776 if (address >= tmp->start) {
777 if (address < tmp->end) {
778 *entry = tmp;
779 map->hint = tmp;
780 return(TRUE);
984263bc 781 }
686dbf64
MD
782 last = tmp;
783 tmp = RB_RIGHT(tmp, rb_entry);
784 } else {
785 tmp = RB_LEFT(tmp, rb_entry);
984263bc 786 }
984263bc 787 }
686dbf64 788 *entry = last;
984263bc
MD
789 return (FALSE);
790}
791
792/*
793 * vm_map_insert:
794 *
795 * Inserts the given whole VM object into the target
796 * map at the specified address range. The object's
797 * size should match that of the address range.
798 *
a108bf71
MD
799 * Requires that the map be locked, and leaves it so. Requires that
800 * sufficient vm_map_entry structures have been reserved and tracks
801 * the use via countp.
984263bc
MD
802 *
803 * If object is non-NULL, ref count must be bumped by caller
804 * prior to making call to account for the new entry.
805 */
806int
a108bf71
MD
807vm_map_insert(vm_map_t map, int *countp,
808 vm_object_t object, vm_ooffset_t offset,
1b874851
MD
809 vm_offset_t start, vm_offset_t end,
810 vm_maptype_t maptype,
811 vm_prot_t prot, vm_prot_t max,
984263bc
MD
812 int cow)
813{
814 vm_map_entry_t new_entry;
815 vm_map_entry_t prev_entry;
816 vm_map_entry_t temp_entry;
817 vm_eflags_t protoeflags;
818
819 /*
820 * Check that the start and end points are not bogus.
821 */
822
823 if ((start < map->min_offset) || (end > map->max_offset) ||
824 (start >= end))
825 return (KERN_INVALID_ADDRESS);
826
827 /*
828 * Find the entry prior to the proposed starting address; if it's part
829 * of an existing entry, this range is bogus.
830 */
831
832 if (vm_map_lookup_entry(map, start, &temp_entry))
833 return (KERN_NO_SPACE);
834
835 prev_entry = temp_entry;
836
837 /*
838 * Assert that the next entry doesn't overlap the end point.
839 */
840
841 if ((prev_entry->next != &map->header) &&
842 (prev_entry->next->start < end))
843 return (KERN_NO_SPACE);
844
845 protoeflags = 0;
846
847 if (cow & MAP_COPY_ON_WRITE)
848 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
849
850 if (cow & MAP_NOFAULT) {
851 protoeflags |= MAP_ENTRY_NOFAULT;
852
853 KASSERT(object == NULL,
854 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
855 }
856 if (cow & MAP_DISABLE_SYNCER)
857 protoeflags |= MAP_ENTRY_NOSYNC;
858 if (cow & MAP_DISABLE_COREDUMP)
859 protoeflags |= MAP_ENTRY_NOCOREDUMP;
c809941b
MD
860 if (cow & MAP_IS_STACK)
861 protoeflags |= MAP_ENTRY_STACK;
984263bc
MD
862
863 if (object) {
864 /*
865 * When object is non-NULL, it could be shared with another
866 * process. We have to set or clear OBJ_ONEMAPPING
867 * appropriately.
868 */
869 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
870 vm_object_clear_flag(object, OBJ_ONEMAPPING);
871 }
872 }
873 else if ((prev_entry != &map->header) &&
874 (prev_entry->eflags == protoeflags) &&
875 (prev_entry->end == start) &&
876 (prev_entry->wired_count == 0) &&
1b874851 877 prev_entry->maptype == maptype &&
984263bc
MD
878 ((prev_entry->object.vm_object == NULL) ||
879 vm_object_coalesce(prev_entry->object.vm_object,
880 OFF_TO_IDX(prev_entry->offset),
881 (vm_size_t)(prev_entry->end - prev_entry->start),
882 (vm_size_t)(end - prev_entry->end)))) {
883 /*
884 * We were able to extend the object. Determine if we
885 * can extend the previous map entry to include the
886 * new range as well.
887 */
888 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
889 (prev_entry->protection == prot) &&
890 (prev_entry->max_protection == max)) {
891 map->size += (end - prev_entry->end);
892 prev_entry->end = end;
a108bf71 893 vm_map_simplify_entry(map, prev_entry, countp);
984263bc
MD
894 return (KERN_SUCCESS);
895 }
896
897 /*
898 * If we can extend the object but cannot extend the
899 * map entry, we have to create a new map entry. We
900 * must bump the ref count on the extended object to
901 * account for it. object may be NULL.
902 */
903 object = prev_entry->object.vm_object;
904 offset = prev_entry->offset +
905 (prev_entry->end - prev_entry->start);
906 vm_object_reference(object);
907 }
908
909 /*
910 * NOTE: if conditionals fail, object can be NULL here. This occurs
911 * in things like the buffer map where we manage kva but do not manage
912 * backing objects.
913 */
914
915 /*
916 * Create a new entry
917 */
918
a108bf71 919 new_entry = vm_map_entry_create(map, countp);
984263bc
MD
920 new_entry->start = start;
921 new_entry->end = end;
922
1b874851 923 new_entry->maptype = maptype;
984263bc
MD
924 new_entry->eflags = protoeflags;
925 new_entry->object.vm_object = object;
926 new_entry->offset = offset;
afeabdca 927 new_entry->aux.master_pde = 0;
984263bc
MD
928
929 new_entry->inheritance = VM_INHERIT_DEFAULT;
930 new_entry->protection = prot;
931 new_entry->max_protection = max;
932 new_entry->wired_count = 0;
933
934 /*
935 * Insert the new entry into the list
936 */
937
938 vm_map_entry_link(map, prev_entry, new_entry);
939 map->size += new_entry->end - new_entry->start;
940
941 /*
791c6551
MD
942 * Update the free space hint. Entries cannot overlap.
943 * An exact comparison is needed to avoid matching
944 * against the map->header.
984263bc
MD
945 */
946 if ((map->first_free == prev_entry) &&
791c6551 947 (prev_entry->end == new_entry->start)) {
984263bc
MD
948 map->first_free = new_entry;
949 }
950
951#if 0
952 /*
953 * Temporarily removed to avoid MAP_STACK panic, due to
954 * MAP_STACK being a huge hack. Will be added back in
955 * when MAP_STACK (and the user stack mapping) is fixed.
956 */
957 /*
958 * It may be possible to simplify the entry
959 */
a108bf71 960 vm_map_simplify_entry(map, new_entry, countp);
984263bc
MD
961#endif
962
afeabdca
MD
963 /*
964 * Try to pre-populate the page table. Mappings governed by virtual
965 * page tables cannot be prepopulated without a lot of work, so
966 * don't try.
967 */
968 if ((cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) &&
969 maptype != VM_MAPTYPE_VPAGETABLE) {
083a7402 970 pmap_object_init_pt(map->pmap, start, prot,
984263bc
MD
971 object, OFF_TO_IDX(offset), end - start,
972 cow & MAP_PREFAULT_PARTIAL);
973 }
974
975 return (KERN_SUCCESS);
976}
977
978/*
979 * Find sufficient space for `length' bytes in the given map, starting at
980 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
e9bb90e8
MD
981 *
982 * This function will returned an arbitrarily aligned pointer. If no
983 * particular alignment is required you should pass align as 1. Note that
984 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
985 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
986 * argument.
987 *
988 * 'align' should be a power of 2 but is not required to be.
984263bc
MD
989 */
990int
c809941b
MD
991vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
992 vm_offset_t align, int flags, vm_offset_t *addr)
984263bc
MD
993{
994 vm_map_entry_t entry, next;
995 vm_offset_t end;
e9bb90e8 996 vm_offset_t align_mask;
984263bc
MD
997
998 if (start < map->min_offset)
999 start = map->min_offset;
1000 if (start > map->max_offset)
1001 return (1);
1002
e9bb90e8
MD
1003 /*
1004 * If the alignment is not a power of 2 we will have to use
1005 * a mod/division, set align_mask to a special value.
1006 */
1007 if ((align | (align - 1)) + 1 != (align << 1))
1008 align_mask = (vm_offset_t)-1;
1009 else
1010 align_mask = align - 1;
1011
a108bf71 1012retry:
984263bc
MD
1013 /*
1014 * Look for the first possible address; if there's already something
1015 * at this address, we have to start after it.
1016 */
1017 if (start == map->min_offset) {
1018 if ((entry = map->first_free) != &map->header)
1019 start = entry->end;
1020 } else {
1021 vm_map_entry_t tmp;
1022
1023 if (vm_map_lookup_entry(map, start, &tmp))
1024 start = tmp->end;
1025 entry = tmp;
1026 }
1027
1028 /*
1029 * Look through the rest of the map, trying to fit a new region in the
1030 * gap between existing regions, or after the very last region.
1031 */
1032 for (;; start = (entry = next)->end) {
e9bb90e8
MD
1033 /*
1034 * Adjust the proposed start by the requested alignment,
1035 * be sure that we didn't wrap the address.
1036 */
1037 if (align_mask == (vm_offset_t)-1)
1038 end = ((start + align - 1) / align) * align;
1039 else
1040 end = (start + align_mask) & ~align_mask;
1041 if (end < start)
1042 return (1);
1043 start = end;
984263bc
MD
1044 /*
1045 * Find the end of the proposed new region. Be sure we didn't
e9bb90e8
MD
1046 * go beyond the end of the map, or wrap around the address.
1047 * Then check to see if this is the last entry or if the
1048 * proposed end fits in the gap between this and the next
1049 * entry.
984263bc
MD
1050 */
1051 end = start + length;
1052 if (end > map->max_offset || end < start)
1053 return (1);
1054 next = entry->next;
c809941b
MD
1055
1056 /*
1057 * If the next entry's start address is beyond the desired
1058 * end address we may have found a good entry.
1059 *
1060 * If the next entry is a stack mapping we do not map into
1061 * the stack's reserved space.
1062 *
1063 * XXX continue to allow mapping into the stack's reserved
1064 * space if doing a MAP_STACK mapping inside a MAP_STACK
1065 * mapping, for backwards compatibility. But the caller
1066 * really should use MAP_STACK | MAP_TRYFIXED if they
1067 * want to do that.
1068 */
1069 if (next == &map->header)
984263bc 1070 break;
c809941b
MD
1071 if (next->start >= end) {
1072 if ((next->eflags & MAP_ENTRY_STACK) == 0)
1073 break;
1074 if (flags & MAP_STACK)
1075 break;
1076 if (next->start - next->aux.avail_ssize >= end)
1077 break;
1078 }
984263bc 1079 }
686dbf64 1080 map->hint = entry;
e4846942 1081 if (map == &kernel_map) {
984263bc
MD
1082 vm_offset_t ksize;
1083 if ((ksize = round_page(start + length)) > kernel_vm_end) {
1084 pmap_growkernel(ksize);
a108bf71 1085 goto retry;
984263bc
MD
1086 }
1087 }
a108bf71 1088 *addr = start;
984263bc
MD
1089 return (0);
1090}
1091
1092/*
1093 * vm_map_find finds an unallocated region in the target address
1094 * map with the given length. The search is defined to be
1095 * first-fit from the specified address; the region found is
1096 * returned in the same parameter.
1097 *
1098 * If object is non-NULL, ref count must be bumped by caller
1099 * prior to making call to account for the new entry.
1100 */
1101int
1102vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1b874851 1103 vm_offset_t *addr, vm_size_t length,
c809941b 1104 boolean_t fitit,
1b874851
MD
1105 vm_maptype_t maptype,
1106 vm_prot_t prot, vm_prot_t max,
1107 int cow)
984263bc
MD
1108{
1109 vm_offset_t start;
03aa8d99 1110 int result;
a108bf71 1111 int count;
984263bc
MD
1112
1113 start = *addr;
1114
a108bf71 1115 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc 1116 vm_map_lock(map);
c809941b
MD
1117 if (fitit) {
1118 if (vm_map_findspace(map, start, length, 1, 0, addr)) {
984263bc 1119 vm_map_unlock(map);
a108bf71 1120 vm_map_entry_release(count);
984263bc
MD
1121 return (KERN_NO_SPACE);
1122 }
1123 start = *addr;
1124 }
a108bf71 1125 result = vm_map_insert(map, &count, object, offset,
1b874851
MD
1126 start, start + length,
1127 maptype,
1128 prot, max,
1129 cow);
984263bc 1130 vm_map_unlock(map);
a108bf71 1131 vm_map_entry_release(count);
984263bc 1132
984263bc
MD
1133 return (result);
1134}
1135
1136/*
1137 * vm_map_simplify_entry:
1138 *
1139 * Simplify the given map entry by merging with either neighbor. This
1140 * routine also has the ability to merge with both neighbors.
1141 *
1142 * The map must be locked.
1143 *
1144 * This routine guarentees that the passed entry remains valid (though
1145 * possibly extended). When merging, this routine may delete one or
1146 * both neighbors. No action is taken on entries which have their
1147 * in-transition flag set.
1148 */
1149void
a108bf71 1150vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
984263bc
MD
1151{
1152 vm_map_entry_t next, prev;
1153 vm_size_t prevsize, esize;
1154
1b874851 1155 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
12e4aaff 1156 ++mycpu->gd_cnt.v_intrans_coll;
984263bc
MD
1157 return;
1158 }
1159
1b874851
MD
1160 if (entry->maptype == VM_MAPTYPE_SUBMAP)
1161 return;
1162
984263bc
MD
1163 prev = entry->prev;
1164 if (prev != &map->header) {
1165 prevsize = prev->end - prev->start;
1166 if ( (prev->end == entry->start) &&
1b874851 1167 (prev->maptype == entry->maptype) &&
984263bc
MD
1168 (prev->object.vm_object == entry->object.vm_object) &&
1169 (!prev->object.vm_object ||
1170 (prev->offset + prevsize == entry->offset)) &&
1171 (prev->eflags == entry->eflags) &&
1172 (prev->protection == entry->protection) &&
1173 (prev->max_protection == entry->max_protection) &&
1174 (prev->inheritance == entry->inheritance) &&
1175 (prev->wired_count == entry->wired_count)) {
1176 if (map->first_free == prev)
1177 map->first_free = entry;
1178 if (map->hint == prev)
1179 map->hint = entry;
1180 vm_map_entry_unlink(map, prev);
1181 entry->start = prev->start;
1182 entry->offset = prev->offset;
1183 if (prev->object.vm_object)
1184 vm_object_deallocate(prev->object.vm_object);
a108bf71 1185 vm_map_entry_dispose(map, prev, countp);
984263bc
MD
1186 }
1187 }
1188
1189 next = entry->next;
1190 if (next != &map->header) {
1191 esize = entry->end - entry->start;
1192 if ((entry->end == next->start) &&
1b874851 1193 (next->maptype == entry->maptype) &&
984263bc
MD
1194 (next->object.vm_object == entry->object.vm_object) &&
1195 (!entry->object.vm_object ||
1196 (entry->offset + esize == next->offset)) &&
1197 (next->eflags == entry->eflags) &&
1198 (next->protection == entry->protection) &&
1199 (next->max_protection == entry->max_protection) &&
1200 (next->inheritance == entry->inheritance) &&
1201 (next->wired_count == entry->wired_count)) {
1202 if (map->first_free == next)
1203 map->first_free = entry;
1204 if (map->hint == next)
1205 map->hint = entry;
1206 vm_map_entry_unlink(map, next);
1207 entry->end = next->end;
1208 if (next->object.vm_object)
1209 vm_object_deallocate(next->object.vm_object);
a108bf71 1210 vm_map_entry_dispose(map, next, countp);
984263bc
MD
1211 }
1212 }
1213}
1214/*
1215 * vm_map_clip_start: [ internal use only ]
1216 *
1217 * Asserts that the given entry begins at or after
1218 * the specified address; if necessary,
1219 * it splits the entry into two.
1220 */
a108bf71 1221#define vm_map_clip_start(map, entry, startaddr, countp) \
984263bc
MD
1222{ \
1223 if (startaddr > entry->start) \
a108bf71 1224 _vm_map_clip_start(map, entry, startaddr, countp); \
984263bc
MD
1225}
1226
1227/*
1228 * This routine is called only when it is known that
1229 * the entry must be split.
1230 */
1231static void
a108bf71 1232_vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start, int *countp)
984263bc
MD
1233{
1234 vm_map_entry_t new_entry;
1235
1236 /*
1237 * Split off the front portion -- note that we must insert the new
1238 * entry BEFORE this one, so that this entry has the specified
1239 * starting address.
1240 */
1241
a108bf71 1242 vm_map_simplify_entry(map, entry, countp);
984263bc
MD
1243
1244 /*
1245 * If there is no object backing this entry, we might as well create
1246 * one now. If we defer it, an object can get created after the map
1247 * is clipped, and individual objects will be created for the split-up
1248 * map. This is a bit of a hack, but is also about the best place to
1249 * put this improvement.
1250 */
984263bc 1251 if (entry->object.vm_object == NULL && !map->system_map) {
53025830 1252 vm_map_entry_allocate_object(entry);
984263bc
MD
1253 }
1254
a108bf71 1255 new_entry = vm_map_entry_create(map, countp);
984263bc
MD
1256 *new_entry = *entry;
1257
1258 new_entry->end = start;
1259 entry->offset += (start - entry->start);
1260 entry->start = start;
1261
1262 vm_map_entry_link(map, entry->prev, new_entry);
1263
1b874851
MD
1264 switch(entry->maptype) {
1265 case VM_MAPTYPE_NORMAL:
1266 case VM_MAPTYPE_VPAGETABLE:
984263bc 1267 vm_object_reference(new_entry->object.vm_object);
1b874851
MD
1268 break;
1269 default:
1270 break;
984263bc
MD
1271 }
1272}
1273
1274/*
1275 * vm_map_clip_end: [ internal use only ]
1276 *
1277 * Asserts that the given entry ends at or before
1278 * the specified address; if necessary,
1279 * it splits the entry into two.
1280 */
1281
a108bf71 1282#define vm_map_clip_end(map, entry, endaddr, countp) \
984263bc
MD
1283{ \
1284 if (endaddr < entry->end) \
a108bf71 1285 _vm_map_clip_end(map, entry, endaddr, countp); \
984263bc
MD
1286}
1287
1288/*
1289 * This routine is called only when it is known that
1290 * the entry must be split.
1291 */
1292static void
a108bf71 1293_vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end, int *countp)
984263bc
MD
1294{
1295 vm_map_entry_t new_entry;
1296
1297 /*
1298 * If there is no object backing this entry, we might as well create
1299 * one now. If we defer it, an object can get created after the map
1300 * is clipped, and individual objects will be created for the split-up
1301 * map. This is a bit of a hack, but is also about the best place to
1302 * put this improvement.
1303 */
1304
1305 if (entry->object.vm_object == NULL && !map->system_map) {
53025830 1306 vm_map_entry_allocate_object(entry);
984263bc
MD
1307 }
1308
1309 /*
1310 * Create a new entry and insert it AFTER the specified entry
1311 */
1312
a108bf71 1313 new_entry = vm_map_entry_create(map, countp);
984263bc
MD
1314 *new_entry = *entry;
1315
1316 new_entry->start = entry->end = end;
1317 new_entry->offset += (end - entry->start);
1318
1319 vm_map_entry_link(map, entry, new_entry);
1320
1b874851
MD
1321 switch(entry->maptype) {
1322 case VM_MAPTYPE_NORMAL:
1323 case VM_MAPTYPE_VPAGETABLE:
984263bc 1324 vm_object_reference(new_entry->object.vm_object);
1b874851
MD
1325 break;
1326 default:
1327 break;
984263bc
MD
1328 }
1329}
1330
1331/*
1332 * VM_MAP_RANGE_CHECK: [ internal use only ]
1333 *
1334 * Asserts that the starting and ending region
1335 * addresses fall within the valid range of the map.
1336 */
1337#define VM_MAP_RANGE_CHECK(map, start, end) \
1338 { \
1339 if (start < vm_map_min(map)) \
1340 start = vm_map_min(map); \
1341 if (end > vm_map_max(map)) \
1342 end = vm_map_max(map); \
1343 if (start > end) \
1344 start = end; \
1345 }
1346
1347/*
1348 * vm_map_transition_wait: [ kernel use only ]
1349 *
1350 * Used to block when an in-transition collison occurs. The map
1351 * is unlocked for the sleep and relocked before the return.
1352 */
1353static
1354void
1355vm_map_transition_wait(vm_map_t map)
1356{
1357 vm_map_unlock(map);
377d4740 1358 tsleep(map, 0, "vment", 0);
984263bc
MD
1359 vm_map_lock(map);
1360}
1361
1362/*
1363 * CLIP_CHECK_BACK
1364 * CLIP_CHECK_FWD
1365 *
1366 * When we do blocking operations with the map lock held it is
1367 * possible that a clip might have occured on our in-transit entry,
1368 * requiring an adjustment to the entry in our loop. These macros
1369 * help the pageable and clip_range code deal with the case. The
1370 * conditional costs virtually nothing if no clipping has occured.
1371 */
1372
1373#define CLIP_CHECK_BACK(entry, save_start) \
1374 do { \
1375 while (entry->start != save_start) { \
1376 entry = entry->prev; \
1377 KASSERT(entry != &map->header, ("bad entry clip")); \
1378 } \
1379 } while(0)
1380
1381#define CLIP_CHECK_FWD(entry, save_end) \
1382 do { \
1383 while (entry->end != save_end) { \
1384 entry = entry->next; \
1385 KASSERT(entry != &map->header, ("bad entry clip")); \
1386 } \
1387 } while(0)
1388
1389
1390/*
1391 * vm_map_clip_range: [ kernel use only ]
1392 *
1393 * Clip the specified range and return the base entry. The
1394 * range may cover several entries starting at the returned base
1395 * and the first and last entry in the covering sequence will be
1396 * properly clipped to the requested start and end address.
1397 *
1398 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1399 * flag.
1400 *
1401 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1402 * covered by the requested range.
1403 *
1404 * The map must be exclusively locked on entry and will remain locked
1405 * on return. If no range exists or the range contains holes and you
1406 * specified that no holes were allowed, NULL will be returned. This
1407 * routine may temporarily unlock the map in order avoid a deadlock when
1408 * sleeping.
1409 */
1410static
1411vm_map_entry_t
a108bf71
MD
1412vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1413 int *countp, int flags)
984263bc
MD
1414{
1415 vm_map_entry_t start_entry;
1416 vm_map_entry_t entry;
1417
1418 /*
1419 * Locate the entry and effect initial clipping. The in-transition
1420 * case does not occur very often so do not try to optimize it.
1421 */
1422again:
1423 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1424 return (NULL);
1425 entry = start_entry;
1426 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1427 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
12e4aaff
MD
1428 ++mycpu->gd_cnt.v_intrans_coll;
1429 ++mycpu->gd_cnt.v_intrans_wait;
984263bc
MD
1430 vm_map_transition_wait(map);
1431 /*
1432 * entry and/or start_entry may have been clipped while
1433 * we slept, or may have gone away entirely. We have
1434 * to restart from the lookup.
1435 */
1436 goto again;
1437 }
1438 /*
1439 * Since we hold an exclusive map lock we do not have to restart
1440 * after clipping, even though clipping may block in zalloc.
1441 */
a108bf71
MD
1442 vm_map_clip_start(map, entry, start, countp);
1443 vm_map_clip_end(map, entry, end, countp);
984263bc
MD
1444 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1445
1446 /*
1447 * Scan entries covered by the range. When working on the next
1448 * entry a restart need only re-loop on the current entry which
1449 * we have already locked, since 'next' may have changed. Also,
1450 * even though entry is safe, it may have been clipped so we
1451 * have to iterate forwards through the clip after sleeping.
1452 */
1453 while (entry->next != &map->header && entry->next->start < end) {
1454 vm_map_entry_t next = entry->next;
1455
1456 if (flags & MAP_CLIP_NO_HOLES) {
1457 if (next->start > entry->end) {
1458 vm_map_unclip_range(map, start_entry,
a108bf71 1459 start, entry->end, countp, flags);
984263bc
MD
1460 return(NULL);
1461 }
1462 }
1463
1464 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1465 vm_offset_t save_end = entry->end;
1466 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
12e4aaff
MD
1467 ++mycpu->gd_cnt.v_intrans_coll;
1468 ++mycpu->gd_cnt.v_intrans_wait;
984263bc
MD
1469 vm_map_transition_wait(map);
1470
1471 /*
1472 * clips might have occured while we blocked.
1473 */
1474 CLIP_CHECK_FWD(entry, save_end);
1475 CLIP_CHECK_BACK(start_entry, start);
1476 continue;
1477 }
1478 /*
1479 * No restart necessary even though clip_end may block, we
1480 * are holding the map lock.
1481 */
a108bf71 1482 vm_map_clip_end(map, next, end, countp);
984263bc
MD
1483 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1484 entry = next;
1485 }
1486 if (flags & MAP_CLIP_NO_HOLES) {
1487 if (entry->end != end) {
1488 vm_map_unclip_range(map, start_entry,
a108bf71 1489 start, entry->end, countp, flags);
984263bc
MD
1490 return(NULL);
1491 }
1492 }
1493 return(start_entry);
1494}
1495
1496/*
1497 * vm_map_unclip_range: [ kernel use only ]
1498 *
1499 * Undo the effect of vm_map_clip_range(). You should pass the same
1500 * flags and the same range that you passed to vm_map_clip_range().
1501 * This code will clear the in-transition flag on the entries and
1502 * wake up anyone waiting. This code will also simplify the sequence
1503 * and attempt to merge it with entries before and after the sequence.
1504 *
1505 * The map must be locked on entry and will remain locked on return.
1506 *
1507 * Note that you should also pass the start_entry returned by
1508 * vm_map_clip_range(). However, if you block between the two calls
1509 * with the map unlocked please be aware that the start_entry may
1510 * have been clipped and you may need to scan it backwards to find
1511 * the entry corresponding with the original start address. You are
1512 * responsible for this, vm_map_unclip_range() expects the correct
1513 * start_entry to be passed to it and will KASSERT otherwise.
1514 */
1515static
1516void
1517vm_map_unclip_range(
1518 vm_map_t map,
1519 vm_map_entry_t start_entry,
1520 vm_offset_t start,
1521 vm_offset_t end,
a108bf71 1522 int *countp,
984263bc
MD
1523 int flags)
1524{
1525 vm_map_entry_t entry;
1526
1527 entry = start_entry;
1528
1529 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1530 while (entry != &map->header && entry->start < end) {
1531 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, ("in-transition flag not set during unclip on: %p", entry));
1532 KASSERT(entry->end <= end, ("unclip_range: tail wasn't clipped"));
1533 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1534 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1535 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1536 wakeup(map);
1537 }
1538 entry = entry->next;
1539 }
1540
1541 /*
1542 * Simplification does not block so there is no restart case.
1543 */
1544 entry = start_entry;
1545 while (entry != &map->header && entry->start < end) {
a108bf71 1546 vm_map_simplify_entry(map, entry, countp);
984263bc
MD
1547 entry = entry->next;
1548 }
1549}
1550
1551/*
1552 * vm_map_submap: [ kernel use only ]
1553 *
1554 * Mark the given range as handled by a subordinate map.
1555 *
1556 * This range must have been created with vm_map_find,
1557 * and no other operations may have been performed on this
1558 * range prior to calling vm_map_submap.
1559 *
1560 * Only a limited number of operations can be performed
1561 * within this rage after calling vm_map_submap:
1562 * vm_fault
1563 * [Don't try vm_map_copy!]
1564 *
1565 * To remove a submapping, one must first remove the
1566 * range from the superior map, and then destroy the
1567 * submap (if desired). [Better yet, don't try it.]
1568 */
1569int
a108bf71 1570vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
984263bc
MD
1571{
1572 vm_map_entry_t entry;
1573 int result = KERN_INVALID_ARGUMENT;
a108bf71 1574 int count;
984263bc 1575
a108bf71 1576 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc
MD
1577 vm_map_lock(map);
1578
1579 VM_MAP_RANGE_CHECK(map, start, end);
1580
1581 if (vm_map_lookup_entry(map, start, &entry)) {
a108bf71 1582 vm_map_clip_start(map, entry, start, &count);
984263bc
MD
1583 } else {
1584 entry = entry->next;
1585 }
1586
a108bf71 1587 vm_map_clip_end(map, entry, end, &count);
984263bc
MD
1588
1589 if ((entry->start == start) && (entry->end == end) &&
1590 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1591 (entry->object.vm_object == NULL)) {
1592 entry->object.sub_map = submap;
1b874851 1593 entry->maptype = VM_MAPTYPE_SUBMAP;
984263bc
MD
1594 result = KERN_SUCCESS;
1595 }
1596 vm_map_unlock(map);
a108bf71 1597 vm_map_entry_release(count);
984263bc
MD
1598
1599 return (result);
1600}
1601
1602/*
1b874851 1603 * vm_map_protect:
984263bc 1604 *
1b874851
MD
1605 * Sets the protection of the specified address region in the target map.
1606 * If "set_max" is specified, the maximum protection is to be set;
1607 * otherwise, only the current protection is affected.
1608 *
1609 * The protection is not applicable to submaps, but is applicable to normal
1610 * maps and maps governed by virtual page tables. For example, when operating
1611 * on a virtual page table our protection basically controls how COW occurs
1612 * on the backing object, whereas the virtual page table abstraction itself
1613 * is an abstraction for userland.
984263bc
MD
1614 */
1615int
1616vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1617 vm_prot_t new_prot, boolean_t set_max)
1618{
1619 vm_map_entry_t current;
1620 vm_map_entry_t entry;
a108bf71 1621 int count;
984263bc 1622
a108bf71 1623 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc
MD
1624 vm_map_lock(map);
1625
1626 VM_MAP_RANGE_CHECK(map, start, end);
1627
1628 if (vm_map_lookup_entry(map, start, &entry)) {
a108bf71 1629 vm_map_clip_start(map, entry, start, &count);
984263bc
MD
1630 } else {
1631 entry = entry->next;
1632 }
1633
1634 /*
1635 * Make a first pass to check for protection violations.
1636 */
984263bc
MD
1637 current = entry;
1638 while ((current != &map->header) && (current->start < end)) {
1b874851 1639 if (current->maptype == VM_MAPTYPE_SUBMAP) {
984263bc 1640 vm_map_unlock(map);
a108bf71 1641 vm_map_entry_release(count);
984263bc
MD
1642 return (KERN_INVALID_ARGUMENT);
1643 }
1644 if ((new_prot & current->max_protection) != new_prot) {
1645 vm_map_unlock(map);
a108bf71 1646 vm_map_entry_release(count);
984263bc
MD
1647 return (KERN_PROTECTION_FAILURE);
1648 }
1649 current = current->next;
1650 }
1651
1652 /*
1653 * Go back and fix up protections. [Note that clipping is not
1654 * necessary the second time.]
1655 */
984263bc
MD
1656 current = entry;
1657
1658 while ((current != &map->header) && (current->start < end)) {
1659 vm_prot_t old_prot;
1660
a108bf71 1661 vm_map_clip_end(map, current, end, &count);
984263bc
MD
1662
1663 old_prot = current->protection;
1b874851 1664 if (set_max) {
984263bc
MD
1665 current->protection =
1666 (current->max_protection = new_prot) &
1667 old_prot;
1b874851 1668 } else {
984263bc 1669 current->protection = new_prot;
1b874851 1670 }
984263bc
MD
1671
1672 /*
1673 * Update physical map if necessary. Worry about copy-on-write
1674 * here -- CHECK THIS XXX
1675 */
1676
1677 if (current->protection != old_prot) {
1678#define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1679 VM_PROT_ALL)
1680
1681 pmap_protect(map->pmap, current->start,
1682 current->end,
1683 current->protection & MASK(current));
1684#undef MASK
1685 }
1686
a108bf71 1687 vm_map_simplify_entry(map, current, &count);
984263bc
MD
1688
1689 current = current->next;
1690 }
1691
1692 vm_map_unlock(map);
a108bf71 1693 vm_map_entry_release(count);
984263bc
MD
1694 return (KERN_SUCCESS);
1695}
1696
1697/*
1698 * vm_map_madvise:
1699 *
1700 * This routine traverses a processes map handling the madvise
1701 * system call. Advisories are classified as either those effecting
1702 * the vm_map_entry structure, or those effecting the underlying
1703 * objects.
afeabdca
MD
1704 *
1705 * The <value> argument is used for extended madvise calls.
984263bc 1706 */
984263bc 1707int
afeabdca
MD
1708vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end,
1709 int behav, off_t value)
984263bc
MD
1710{
1711 vm_map_entry_t current, entry;
1712 int modify_map = 0;
afeabdca 1713 int error = 0;
a108bf71 1714 int count;
984263bc
MD
1715
1716 /*
1717 * Some madvise calls directly modify the vm_map_entry, in which case
1718 * we need to use an exclusive lock on the map and we need to perform
1719 * various clipping operations. Otherwise we only need a read-lock
1720 * on the map.
1721 */
1722
a108bf71
MD
1723 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1724
984263bc
MD
1725 switch(behav) {
1726 case MADV_NORMAL:
1727 case MADV_SEQUENTIAL:
1728 case MADV_RANDOM:
1729 case MADV_NOSYNC:
1730 case MADV_AUTOSYNC:
1731 case MADV_NOCORE:
1732 case MADV_CORE:
afeabdca
MD
1733 case MADV_SETMAP:
1734 case MADV_INVAL:
984263bc
MD
1735 modify_map = 1;
1736 vm_map_lock(map);
1737 break;
1738 case MADV_WILLNEED:
1739 case MADV_DONTNEED:
1740 case MADV_FREE:
1741 vm_map_lock_read(map);
1742 break;
1743 default:
a108bf71 1744 vm_map_entry_release(count);
afeabdca 1745 return (EINVAL);
984263bc
MD
1746 }
1747
1748 /*
1749 * Locate starting entry and clip if necessary.
1750 */
1751
1752 VM_MAP_RANGE_CHECK(map, start, end);
1753
1754 if (vm_map_lookup_entry(map, start, &entry)) {
1755 if (modify_map)
a108bf71 1756 vm_map_clip_start(map, entry, start, &count);
984263bc
MD
1757 } else {
1758 entry = entry->next;
1759 }
1760
1761 if (modify_map) {
1762 /*
1763 * madvise behaviors that are implemented in the vm_map_entry.
1764 *
1765 * We clip the vm_map_entry so that behavioral changes are
1766 * limited to the specified address range.
1767 */
1768 for (current = entry;
1769 (current != &map->header) && (current->start < end);
1770 current = current->next
1771 ) {
1b874851 1772 if (current->maptype == VM_MAPTYPE_SUBMAP)
984263bc
MD
1773 continue;
1774
a108bf71 1775 vm_map_clip_end(map, current, end, &count);
984263bc
MD
1776
1777 switch (behav) {
1778 case MADV_NORMAL:
1779 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1780 break;
1781 case MADV_SEQUENTIAL:
1782 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1783 break;
1784 case MADV_RANDOM:
1785 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1786 break;
1787 case MADV_NOSYNC:
1788 current->eflags |= MAP_ENTRY_NOSYNC;
1789 break;
1790 case MADV_AUTOSYNC:
1791 current->eflags &= ~MAP_ENTRY_NOSYNC;
1792 break;
1793 case MADV_NOCORE:
1794 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1795 break;
1796 case MADV_CORE:
1797 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1798 break;
afeabdca
MD
1799 case MADV_INVAL:
1800 /*
1801 * Invalidate the related pmap entries, used
1802 * to flush portions of the real kernel's
1803 * pmap when the caller has removed or
1804 * modified existing mappings in a virtual
1805 * page table.
1806 */
1807 pmap_remove(map->pmap,
1808 current->start, current->end);
1809 break;
1810 case MADV_SETMAP:
1811 /*
1812 * Set the page directory page for a map
1813 * governed by a virtual page table. Mark
1814 * the entry as being governed by a virtual
1815 * page table if it is not.
1816 *
1817 * XXX the page directory page is stored
1818 * in the avail_ssize field if the map_entry.
1819 *
1820 * XXX the map simplification code does not
1821 * compare this field so weird things may
1822 * happen if you do not apply this function
1823 * to the entire mapping governed by the
1824 * virtual page table.
1825 */
1826 if (current->maptype != VM_MAPTYPE_VPAGETABLE) {
1827 error = EINVAL;
1828 break;
1829 }
1830 current->aux.master_pde = value;
1831 pmap_remove(map->pmap,
1832 current->start, current->end);
1833 break;
984263bc 1834 default:
afeabdca 1835 error = EINVAL;
984263bc
MD
1836 break;
1837 }
a108bf71 1838 vm_map_simplify_entry(map, current, &count);
984263bc
MD
1839 }
1840 vm_map_unlock(map);
1841 } else {
1842 vm_pindex_t pindex;
1843 int count;
1844
1845 /*
1846 * madvise behaviors that are implemented in the underlying
1847 * vm_object.
1848 *
1849 * Since we don't clip the vm_map_entry, we have to clip
1850 * the vm_object pindex and count.
1b874851
MD
1851 *
1852 * NOTE! We currently do not support these functions on
1853 * virtual page tables.
984263bc
MD
1854 */
1855 for (current = entry;
1856 (current != &map->header) && (current->start < end);
1857 current = current->next
1858 ) {
1859 vm_offset_t useStart;
1860
1b874851 1861 if (current->maptype != VM_MAPTYPE_NORMAL)
984263bc
MD
1862 continue;
1863
1864 pindex = OFF_TO_IDX(current->offset);
1865 count = atop(current->end - current->start);
1866 useStart = current->start;
1867
1868 if (current->start < start) {
1869 pindex += atop(start - current->start);
1870 count -= atop(start - current->start);
1871 useStart = start;
1872 }
1873 if (current->end > end)
1874 count -= atop(current->end - end);
1875
1876 if (count <= 0)
1877 continue;
1878
1879 vm_object_madvise(current->object.vm_object,
1880 pindex, count, behav);
afeabdca
MD
1881
1882 /*
1883 * Try to populate the page table. Mappings governed
1884 * by virtual page tables cannot be pre-populated
1885 * without a lot of work so don't try.
1886 */
1887 if (behav == MADV_WILLNEED &&
1888 current->maptype != VM_MAPTYPE_VPAGETABLE) {
984263bc
MD
1889 pmap_object_init_pt(
1890 map->pmap,
1891 useStart,
083a7402 1892 current->protection,
984263bc
MD
1893 current->object.vm_object,
1894 pindex,
1895 (count << PAGE_SHIFT),
1896 MAP_PREFAULT_MADVISE
1897 );
1898 }
1899 }
1900 vm_map_unlock_read(map);
1901 }
a108bf71 1902 vm_map_entry_release(count);
afeabdca 1903 return(error);
984263bc
MD
1904}
1905
1906
1907/*
1908 * vm_map_inherit:
1909 *
1910 * Sets the inheritance of the specified address
1911 * range in the target map. Inheritance
1912 * affects how the map will be shared with
1913 * child maps at the time of vm_map_fork.
1914 */
1915int
1916vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1917 vm_inherit_t new_inheritance)
1918{
1919 vm_map_entry_t entry;
1920 vm_map_entry_t temp_entry;
a108bf71 1921 int count;
984263bc
MD
1922
1923 switch (new_inheritance) {
1924 case VM_INHERIT_NONE:
1925 case VM_INHERIT_COPY:
1926 case VM_INHERIT_SHARE:
1927 break;
1928 default:
1929 return (KERN_INVALID_ARGUMENT);
1930 }
1931
a108bf71 1932 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc
MD
1933 vm_map_lock(map);
1934
1935 VM_MAP_RANGE_CHECK(map, start, end);
1936
1937 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1938 entry = temp_entry;
a108bf71 1939 vm_map_clip_start(map, entry, start, &count);
984263bc
MD
1940 } else
1941 entry = temp_entry->next;
1942
1943 while ((entry != &map->header) && (entry->start < end)) {
a108bf71 1944 vm_map_clip_end(map, entry, end, &count);
984263bc
MD
1945
1946 entry->inheritance = new_inheritance;
1947
a108bf71 1948 vm_map_simplify_entry(map, entry, &count);
984263bc
MD
1949
1950 entry = entry->next;
1951 }
984263bc 1952 vm_map_unlock(map);
a108bf71 1953 vm_map_entry_release(count);
984263bc
MD
1954 return (KERN_SUCCESS);
1955}
1956
1957/*
1958 * Implement the semantics of mlock
1959 */
1960int
57e43348
MD
1961vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
1962 boolean_t new_pageable)
984263bc
MD
1963{
1964 vm_map_entry_t entry;
1965 vm_map_entry_t start_entry;
1966 vm_offset_t end;
1967 int rv = KERN_SUCCESS;
a108bf71 1968 int count;
984263bc 1969
a108bf71 1970 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc
MD
1971 vm_map_lock(map);
1972 VM_MAP_RANGE_CHECK(map, start, real_end);
1973 end = real_end;
1974
a108bf71 1975 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
984263bc
MD
1976 if (start_entry == NULL) {
1977 vm_map_unlock(map);
a108bf71 1978 vm_map_entry_release(count);
984263bc
MD
1979 return (KERN_INVALID_ADDRESS);
1980 }
1981
1982 if (new_pageable == 0) {
1983 entry = start_entry;
1984 while ((entry != &map->header) && (entry->start < end)) {
1985 vm_offset_t save_start;
1986 vm_offset_t save_end;
1987
1988 /*
1989 * Already user wired or hard wired (trivial cases)
1990 */
1991 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1992 entry = entry->next;
1993 continue;
1994 }
1995 if (entry->wired_count != 0) {
1996 entry->wired_count++;
1997 entry->eflags |= MAP_ENTRY_USER_WIRED;
1998 entry = entry->next;
1999 continue;
2000 }
2001
2002 /*
2003 * A new wiring requires instantiation of appropriate
2004 * management structures and the faulting in of the
2005 * page.
2006 */
1b874851 2007 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
984263bc
MD
2008 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
2009 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
53025830 2010 vm_map_entry_shadow(entry);
984263bc
MD
2011 } else if (entry->object.vm_object == NULL &&
2012 !map->system_map) {
53025830 2013 vm_map_entry_allocate_object(entry);
984263bc
MD
2014 }
2015 }
2016 entry->wired_count++;
2017 entry->eflags |= MAP_ENTRY_USER_WIRED;
2018
2019 /*
f2d22ebf
MD
2020 * Now fault in the area. Note that vm_fault_wire()
2021 * may release the map lock temporarily, it will be
2022 * relocked on return. The in-transition
984263bc
MD
2023 * flag protects the entries.
2024 */
2025 save_start = entry->start;
2026 save_end = entry->end;
f2d22ebf 2027 rv = vm_fault_wire(map, entry, TRUE);
984263bc
MD
2028 if (rv) {
2029 CLIP_CHECK_BACK(entry, save_start);
2030 for (;;) {
2031 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
2032 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2033 entry->wired_count = 0;
2034 if (entry->end == save_end)
2035 break;
2036 entry = entry->next;
2037 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2038 }
2039 end = save_start; /* unwire the rest */
2040 break;
2041 }
2042 /*
2043 * note that even though the entry might have been
2044 * clipped, the USER_WIRED flag we set prevents
2045 * duplication so we do not have to do a
2046 * clip check.
2047 */
2048 entry = entry->next;
2049 }
2050
2051 /*
2052 * If we failed fall through to the unwiring section to
2053 * unwire what we had wired so far. 'end' has already
2054 * been adjusted.
2055 */
2056 if (rv)
2057 new_pageable = 1;
2058
2059 /*
2060 * start_entry might have been clipped if we unlocked the
2061 * map and blocked. No matter how clipped it has gotten
2062 * there should be a fragment that is on our start boundary.
2063 */
2064 CLIP_CHECK_BACK(start_entry, start);
2065 }
2066
2067 /*
2068 * Deal with the unwiring case.
2069 */
2070 if (new_pageable) {
2071 /*
2072 * This is the unwiring case. We must first ensure that the
2073 * range to be unwired is really wired down. We know there
2074 * are no holes.
2075 */
2076 entry = start_entry;
2077 while ((entry != &map->header) && (entry->start < end)) {
2078 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2079 rv = KERN_INVALID_ARGUMENT;
2080 goto done;
2081 }
2082 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
2083 entry = entry->next;
2084 }
2085
2086 /*
2087 * Now decrement the wiring count for each region. If a region
2088 * becomes completely unwired, unwire its physical pages and
2089 * mappings.
2090 */
b4eddbac
DR
2091 /*
2092 * The map entries are processed in a loop, checking to
2093 * make sure the entry is wired and asserting it has a wired
2094 * count. However, another loop was inserted more-or-less in
2095 * the middle of the unwiring path. This loop picks up the
2096 * "entry" loop variable from the first loop without first
2097 * setting it to start_entry. Naturally, the secound loop
2098 * is never entered and the pages backing the entries are
2099 * never unwired. This can lead to a leak of wired pages.
2100 */
2101 entry = start_entry;
984263bc 2102 while ((entry != &map->header) && (entry->start < end)) {
f2d22ebf
MD
2103 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED,
2104 ("expected USER_WIRED on entry %p", entry));
984263bc
MD
2105 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2106 entry->wired_count--;
2107 if (entry->wired_count == 0)
f2d22ebf 2108 vm_fault_unwire(map, entry);
984263bc
MD
2109 entry = entry->next;
2110 }
2111 }
2112done:
a108bf71 2113 vm_map_unclip_range(map, start_entry, start, real_end, &count,
984263bc
MD
2114 MAP_CLIP_NO_HOLES);
2115 map->timestamp++;
2116 vm_map_unlock(map);
a108bf71 2117 vm_map_entry_release(count);
984263bc
MD
2118 return (rv);
2119}
2120
2121/*
cde87949 2122 * vm_map_wire:
984263bc
MD
2123 *
2124 * Sets the pageability of the specified address
2125 * range in the target map. Regions specified
2126 * as not pageable require locked-down physical
2127 * memory and physical page maps.
2128 *
2129 * The map must not be locked, but a reference
2130 * must remain to the map throughout the call.
a108bf71
MD
2131 *
2132 * This function may be called via the zalloc path and must properly
2133 * reserve map entries for kernel_map.
984263bc
MD
2134 */
2135int
e1359933 2136vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
984263bc
MD
2137{
2138 vm_map_entry_t entry;
2139 vm_map_entry_t start_entry;
2140 vm_offset_t end;
2141 int rv = KERN_SUCCESS;
a108bf71 2142 int count;
984263bc 2143
e1359933 2144 if (kmflags & KM_KRESERVE)
a108bf71 2145 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
a108bf71
MD
2146 else
2147 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc
MD
2148 vm_map_lock(map);
2149 VM_MAP_RANGE_CHECK(map, start, real_end);
2150 end = real_end;
2151
a108bf71 2152 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
984263bc
MD
2153 if (start_entry == NULL) {
2154 vm_map_unlock(map);
a108bf71
MD
2155 rv = KERN_INVALID_ADDRESS;
2156 goto failure;
984263bc 2157 }
e1359933 2158 if ((kmflags & KM_PAGEABLE) == 0) {
984263bc
MD
2159 /*
2160 * Wiring.
2161 *
2162 * 1. Holding the write lock, we create any shadow or zero-fill
2163 * objects that need to be created. Then we clip each map
2164 * entry to the region to be wired and increment its wiring
2165 * count. We create objects before clipping the map entries
2166 * to avoid object proliferation.
2167 *
2168 * 2. We downgrade to a read lock, and call vm_fault_wire to
2169 * fault in the pages for any newly wired area (wired_count is
2170 * 1).
2171 *
2172 * Downgrading to a read lock for vm_fault_wire avoids a
2173 * possible deadlock with another process that may have faulted
2174 * on one of the pages to be wired (it would mark the page busy,
2175 * blocking us, then in turn block on the map lock that we
2176 * hold). Because of problems in the recursive lock package,
2177 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2178 * any actions that require the write lock must be done
2179 * beforehand. Because we keep the read lock on the map, the
2180 * copy-on-write status of the entries we modify here cannot
2181 * change.
2182 */
2183
2184 entry = start_entry;
2185 while ((entry != &map->header) && (entry->start < end)) {
2186 /*
2187 * Trivial case if the entry is already wired
2188 */
2189 if (entry->wired_count) {
2190 entry->wired_count++;
2191 entry = entry->next;
2192 continue;
2193 }
2194
2195 /*
2196 * The entry is being newly wired, we have to setup
2197 * appropriate management structures. A shadow
2198 * object is required for a copy-on-write region,
2199 * or a normal object for a zero-fill region. We
2200 * do not have to do this for entries that point to sub
2201 * maps because we won't hold the lock on the sub map.
2202 */
1b874851 2203 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
984263bc
MD
2204 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
2205 if (copyflag &&
2206 ((entry->protection & VM_PROT_WRITE) != 0)) {
53025830 2207 vm_map_entry_shadow(entry);
984263bc
MD
2208 } else if (entry->object.vm_object == NULL &&
2209 !map->system_map) {
53025830 2210 vm_map_entry_allocate_object(entry);
984263bc
MD
2211 }
2212 }
2213
2214 entry->wired_count++;
2215 entry = entry->next;
2216 }
2217
2218 /*
2219 * Pass 2.
2220 */
2221
2222 /*
2223 * HACK HACK HACK HACK
2224 *
2225 * Unlock the map to avoid deadlocks. The in-transit flag
2226 * protects us from most changes but note that
2227 * clipping may still occur. To prevent clipping from
2228 * occuring after the unlock, except for when we are
5fd012e0
MD
2229 * blocking in vm_fault_wire, we must run in a critical
2230 * section, otherwise our accesses to entry->start and
2231 * entry->end could be corrupted. We have to enter the
2232 * critical section prior to unlocking so start_entry does
2233 * not change out from under us at the very beginning of the
2234 * loop.
984263bc
MD
2235 *
2236 * HACK HACK HACK HACK
2237 */
2238
5fd012e0 2239 crit_enter();
984263bc
MD
2240
2241 entry = start_entry;
2242 while (entry != &map->header && entry->start < end) {
2243 /*
2244 * If vm_fault_wire fails for any page we need to undo
2245 * what has been done. We decrement the wiring count
2246 * for those pages which have not yet been wired (now)
2247 * and unwire those that have (later).
2248 */
2249 vm_offset_t save_start = entry->start;
2250 vm_offset_t save_end = entry->end;
2251
2252 if (entry->wired_count == 1)
f2d22ebf 2253 rv = vm_fault_wire(map, entry, FALSE);
984263bc
MD
2254 if (rv) {
2255 CLIP_CHECK_BACK(entry, save_start);
2256 for (;;) {
2257 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
2258 entry->wired_count = 0;
2259 if (entry->end == save_end)
2260 break;
2261 entry = entry->next;
2262 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2263 }
2264 end = save_start;
2265 break;
2266 }
2267 CLIP_CHECK_FWD(entry, save_end);
2268 entry = entry->next;
2269 }
5fd012e0 2270 crit_exit();
984263bc 2271
984263bc
MD
2272 /*
2273 * If a failure occured undo everything by falling through
2274 * to the unwiring code. 'end' has already been adjusted
2275 * appropriately.
2276 */
2277 if (rv)
e1359933 2278 kmflags |= KM_PAGEABLE;
984263bc
MD
2279
2280 /*
f2d22ebf
MD
2281 * start_entry is still IN_TRANSITION but may have been
2282 * clipped since vm_fault_wire() unlocks and relocks the
2283 * map. No matter how clipped it has gotten there should
2284 * be a fragment that is on our start boundary.
984263bc
MD
2285 */
2286 CLIP_CHECK_BACK(start_entry, start);
2287 }
2288
e1359933 2289 if (kmflags & KM_PAGEABLE) {
984263bc
MD
2290 /*
2291 * This is the unwiring case. We must first ensure that the
2292 * range to be unwired is really wired down. We know there
2293 * are no holes.
2294 */
2295 entry = start_entry;
2296 while ((entry != &map->header) && (entry->start < end)) {
2297 if (entry->wired_count == 0) {
2298 rv = KERN_INVALID_ARGUMENT;
2299 goto done;
2300 }
2301 entry = entry->next;
2302 }
2303
2304 /*
2305 * Now decrement the wiring count for each region. If a region
2306 * becomes completely unwired, unwire its physical pages and
2307 * mappings.
2308 */
2309 entry = start_entry;
2310 while ((entry != &map->header) && (entry->start < end)) {
2311 entry->wired_count--;
2312 if (entry->wired_count == 0)
f2d22ebf 2313 vm_fault_unwire(map, entry);
984263bc
MD
2314 entry = entry->next;
2315 }
2316 }
2317done:
a108bf71 2318 vm_map_unclip_range(map, start_entry, start, real_end, &count,
984263bc
MD
2319 MAP_CLIP_NO_HOLES);
2320 map->timestamp++;
2321 vm_map_unlock(map);
a108bf71 2322failure:
e1359933 2323 if (kmflags & KM_KRESERVE)
a108bf71 2324 vm_map_entry_krelease(count);
a108bf71
MD
2325 else
2326 vm_map_entry_release(count);
984263bc
MD
2327 return (rv);
2328}
2329
a108bf71
MD
2330/*
2331 * vm_map_set_wired_quick()
2332 *
2333 * Mark a newly allocated address range as wired but do not fault in
2334 * the pages. The caller is expected to load the pages into the object.
2335 *
2336 * The map must be locked on entry and will remain locked on return.
2337 */
2338void
2339vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size, int *countp)
2340{
2341 vm_map_entry_t scan;
2342 vm_map_entry_t entry;
2343
2344 entry = vm_map_clip_range(map, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2345 for (scan = entry; scan != &map->header && scan->start < addr + size; scan = scan->next) {
2346 KKASSERT(entry->wired_count == 0);
2347 entry->wired_count = 1;
2348 }
2349 vm_map_unclip_range(map, entry, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2350}
2351
984263bc
MD
2352/*
2353 * vm_map_clean
2354 *
2355 * Push any dirty cached pages in the address range to their pager.
2356 * If syncio is TRUE, dirty pages are written synchronously.
2357 * If invalidate is TRUE, any cached pages are freed as well.
2358 *
2359 * Returns an error if any part of the specified range is not mapped.
2360 */
2361int
57e43348
MD
2362vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end, boolean_t syncio,
2363 boolean_t invalidate)
984263bc
MD
2364{
2365 vm_map_entry_t current;
2366 vm_map_entry_t entry;
2367 vm_size_t size;
2368 vm_object_t object;
2369 vm_ooffset_t offset;
2370
2371 vm_map_lock_read(map);
2372 VM_MAP_RANGE_CHECK(map, start, end);
2373 if (!vm_map_lookup_entry(map, start, &entry)) {
2374 vm_map_unlock_read(map);
2375 return (KERN_INVALID_ADDRESS);
2376 }
2377 /*
2378 * Make a first pass to check for holes.
2379 */
2380 for (current = entry; current->start < end; current = current->next) {
1b874851 2381 if (current->maptype == VM_MAPTYPE_SUBMAP) {
984263bc
MD
2382 vm_map_unlock_read(map);
2383 return (KERN_INVALID_ARGUMENT);
2384 }
2385 if (end > current->end &&
2386 (current->next == &map->header ||
2387 current->end != current->next->start)) {
2388 vm_map_unlock_read(map);
2389 return (KERN_INVALID_ADDRESS);
2390 }
2391 }
2392
2393 if (invalidate)
2394 pmap_remove(vm_map_pmap(map), start, end);
2395 /*
2396 * Make a second pass, cleaning/uncaching pages from the indicated
2397 * objects as we go.
2398 */
2399 for (current = entry; current->start < end; current = current->next) {
2400 offset = current->offset + (start - current->start);
2401 size = (end <= current->end ? end : current->end) - start;
1b874851 2402 if (current->maptype == VM_MAPTYPE_SUBMAP) {
984263bc
MD
2403 vm_map_t smap;
2404 vm_map_entry_t tentry;
2405 vm_size_t tsize;
2406
2407 smap = current->object.sub_map;
2408 vm_map_lock_read(smap);
418ff780 2409 vm_map_lookup_entry(smap, offset, &tentry);
984263bc
MD
2410 tsize = tentry->end - offset;
2411 if (tsize < size)
2412 size = tsize;
2413 object = tentry->object.vm_object;
2414 offset = tentry->offset + (offset - tentry->start);
2415 vm_map_unlock_read(smap);
2416 } else {
2417 object = current->object.vm_object;
2418 }
2419 /*
2420 * Note that there is absolutely no sense in writing out
2421 * anonymous objects, so we track down the vnode object
2422 * to write out.
2423 * We invalidate (remove) all pages from the address space
2424 * anyway, for semantic correctness.
2425 *
2426 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2427 * may start out with a NULL object.
2428 */
2429 while (object && object->backing_object) {
984263bc 2430 offset += object->backing_object_offset;
7cad6903 2431 object = object->backing_object;
984263bc
MD
2432 if (object->size < OFF_TO_IDX( offset + size))
2433 size = IDX_TO_OFF(object->size) - offset;
2434 }
2435 if (object && (object->type == OBJT_VNODE) &&
2436 (current->protection & VM_PROT_WRITE)) {
2437 /*
2438 * Flush pages if writing is allowed, invalidate them
2439 * if invalidation requested. Pages undergoing I/O
2440 * will be ignored by vm_object_page_remove().
2441 *
2442 * We cannot lock the vnode and then wait for paging
2443 * to complete without deadlocking against vm_fault.
2444 * Instead we simply call vm_object_page_remove() and
2445 * allow it to block internally on a page-by-page
2446 * basis when it encounters pages undergoing async
2447 * I/O.
2448 */
2449 int flags;
2450
2451 vm_object_reference(object);
ca466bae 2452 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY);
984263bc
MD
2453 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2454 flags |= invalidate ? OBJPC_INVAL : 0;
1b874851
MD
2455
2456 /*
2457 * When operating on a virtual page table just
2458 * flush the whole object. XXX we probably ought
2459 * to
2460 */
2461 switch(current->maptype) {
2462 case VM_MAPTYPE_NORMAL:
2463 vm_object_page_clean(object,
2464 OFF_TO_IDX(offset),
2465 OFF_TO_IDX(offset + size + PAGE_MASK),
2466 flags);
2467 break;
2468 case VM_MAPTYPE_VPAGETABLE:
2469 vm_object_page_clean(object, 0, 0, flags);
2470 break;
2471 }
a11aaa81 2472 vn_unlock(((struct vnode *)object->handle));
984263bc
MD
2473 vm_object_deallocate(object);
2474 }
2475 if (object && invalidate &&
2476 ((object->type == OBJT_VNODE) ||
2477 (object->type == OBJT_DEVICE))) {
2f1821ca
MD
2478 int clean_only =
2479 (object->type == OBJT_DEVICE) ? FALSE : TRUE;
984263bc 2480 vm_object_reference(object);
1b874851
MD
2481 switch(current->maptype) {
2482 case VM_MAPTYPE_NORMAL:
2483 vm_object_page_remove(object,
2484 OFF_TO_IDX(offset),
2485 OFF_TO_IDX(offset + size + PAGE_MASK),
2486 clean_only);
2487 break;
2488 case VM_MAPTYPE_VPAGETABLE:
2489 vm_object_page_remove(object, 0, 0, clean_only);
2490 break;
2491 }
984263bc
MD
2492 vm_object_deallocate(object);
2493 }
2494 start += size;
2495 }
2496
2497 vm_map_unlock_read(map);
2498 return (KERN_SUCCESS);
2499}
2500
2501/*
2502 * vm_map_entry_unwire: [ internal use only ]
2503 *
2504 * Make the region specified by this entry pageable.
2505 *
2506 * The map in question should be locked.
2507 * [This is the reason for this routine's existence.]
2508 */
2509static void
a108bf71 2510vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
984263bc 2511{
f2d22ebf 2512 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
984263bc 2513 entry->wired_count = 0;
f2d22ebf 2514 vm_fault_unwire(map, entry);
984263bc
MD
2515}
2516
2517/*
2518 * vm_map_entry_delete: [ internal use only ]
2519 *
2520 * Deallocate the given entry from the target map.
2521 */
2522static void
a108bf71 2523vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
984263bc
MD
2524{
2525 vm_map_entry_unlink(map, entry);
2526 map->size -= entry->end - entry->start;
2527
1b874851
MD
2528 switch(entry->maptype) {
2529 case VM_MAPTYPE_NORMAL:
2530 case VM_MAPTYPE_VPAGETABLE:
984263bc 2531 vm_object_deallocate(entry->object.vm_object);
1b874851
MD
2532 break;
2533 default:
2534 break;
984263bc
MD
2535 }
2536
a108bf71 2537 vm_map_entry_dispose(map, entry, countp);
984263bc
MD
2538}
2539
2540/*
2541 * vm_map_delete: [ internal use only ]
2542 *
2543 * Deallocates the given address range from the target
2544 * map.
2545 */
2546int
a108bf71 2547vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
984263bc
MD
2548{
2549 vm_object_t object;
2550 vm_map_entry_t entry;
2551 vm_map_entry_t first_entry;
2552
686dbf64 2553again:
984263bc 2554 /*
686dbf64
MD
2555 * Find the start of the region, and clip it. Set entry to point
2556 * at the first record containing the requested address or, if no
2557 * such record exists, the next record with a greater address. The
2558 * loop will run from this point until a record beyond the termination
2559 * address is encountered.
2560 *
2561 * map->hint must be adjusted to not point to anything we delete,
2562 * so set it to the entry prior to the one being deleted.
2563 *
2564 * GGG see other GGG comment.
984263bc 2565 */
686dbf64 2566 if (vm_map_lookup_entry(map, start, &first_entry)) {
984263bc 2567 entry = first_entry;
a108bf71 2568 vm_map_clip_start(map, entry, start, countp);
686dbf64
MD
2569 map->hint = entry->prev; /* possible problem XXX */
2570 } else {
2571 map->hint = first_entry; /* possible problem XXX */
2572 entry = first_entry->next;
984263bc
MD
2573 }
2574
2575 /*
686dbf64
MD
2576 * If a hole opens up prior to the current first_free then
2577 * adjust first_free. As with map->hint, map->first_free
2578 * cannot be left set to anything we might delete.
984263bc 2579 */
984263bc
MD
2580 if (entry == &map->header) {
2581 map->first_free = &map->header;
2582 } else if (map->first_free->start >= start) {
2583 map->first_free = entry->prev;
2584 }
2585
2586 /*
2587 * Step through all entries in this region
2588 */
2589
2590 while ((entry != &map->header) && (entry->start < end)) {
2591 vm_map_entry_t next;
2592 vm_offset_t s, e;
2593 vm_pindex_t offidxstart, offidxend, count;
2594
2595 /*
2596 * If we hit an in-transition entry we have to sleep and
2597 * retry. It's easier (and not really slower) to just retry
2598 * since this case occurs so rarely and the hint is already
2599 * pointing at the right place. We have to reset the
2600 * start offset so as not to accidently delete an entry
2601 * another process just created in vacated space.
2602 */
2603 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2604 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2605 start = entry->start;
12e4aaff
MD
2606 ++mycpu->gd_cnt.v_intrans_coll;
2607 ++mycpu->gd_cnt.v_intrans_wait;
984263bc
MD
2608 vm_map_transition_wait(map);
2609 goto again;
2610 }
a108bf71 2611 vm_map_clip_end(map, entry, end, countp);
984263bc
MD
2612
2613 s = entry->start;
2614 e = entry->end;
2615 next = entry->next;
2616
2617 offidxstart = OFF_TO_IDX(entry->offset);
2618 count = OFF_TO_IDX(e - s);
2619 object = entry->object.vm_object;
2620
2621 /*
2622 * Unwire before removing addresses from the pmap; otherwise,
2623 * unwiring will put the entries back in the pmap.
2624 */
f2d22ebf 2625 if (entry->wired_count != 0)
984263bc 2626 vm_map_entry_unwire(map, entry);
984263bc
MD
2627
2628 offidxend = offidxstart + count;
2629
c439ad8f 2630 if (object == &kernel_object) {
984263bc
MD
2631 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2632 } else {
2633 pmap_remove(map->pmap, s, e);
2634 if (object != NULL &&
2635 object->ref_count != 1 &&
2636 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2637 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2638 vm_object_collapse(object);
2639 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2640 if (object->type == OBJT_SWAP) {
2641 swap_pager_freespace(object, offidxstart, count);
2642 }
2643 if (offidxend >= object->size &&
2644 offidxstart < object->size) {
2645 object->size = offidxstart;
2646 }
2647 }
2648 }
2649
2650 /*
2651 * Delete the entry (which may delete the object) only after
2652 * removing all pmap entries pointing to its pages.
2653 * (Otherwise, its page frames may be reallocated, and any
2654 * modify bits will be set in the wrong object!)
2655 */
a108bf71 2656 vm_map_entry_delete(map, entry, countp);
984263bc
MD
2657 entry = next;
2658 }
2659 return (KERN_SUCCESS);
2660}
2661
2662/*
2663 * vm_map_remove:
2664 *
2665 * Remove the given address range from the target map.
2666 * This is the exported form of vm_map_delete.
2667 */
2668int
a108bf71 2669vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
984263bc 2670{
03aa8d99 2671 int result;
a108bf71 2672 int count;
984263bc 2673
a108bf71 2674 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc
MD
2675 vm_map_lock(map);
2676 VM_MAP_RANGE_CHECK(map, start, end);
a108bf71 2677 result = vm_map_delete(map, start, end, &count);
984263bc 2678 vm_map_unlock(map);
a108bf71 2679 vm_map_entry_release(count);
984263bc 2680
984263bc
MD
2681 return (result);
2682}
2683
2684/*
2685 * vm_map_check_protection:
2686 *
2687 * Assert that the target map allows the specified
2688 * privilege on the entire address region given.
2689 * The entire region must be allocated.
2690 */
2691boolean_t
2692vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2693 vm_prot_t protection)
2694{
2695 vm_map_entry_t entry;
2696 vm_map_entry_t tmp_entry;
2697
2698 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2699 return (FALSE);
2700 }
2701 entry = tmp_entry;
2702
2703 while (start < end) {
2704 if (entry == &map->header) {
2705 return (FALSE);
2706 }
2707 /*
2708 * No holes allowed!
2709 */
2710
2711 if (start < entry->start) {
2712 return (FALSE);
2713 }
2714 /*
2715 * Check protection associated with entry.
2716 */
2717
2718 if ((entry->protection & protection) != protection) {
2719 return (FALSE);
2720 }
2721 /* go to next entry */
2722
2723 start = entry->end;
2724 entry = entry->next;
2725 }
2726 return (TRUE);
2727}
2728
2729/*
2730 * Split the pages in a map entry into a new object. This affords
2731 * easier removal of unused pages, and keeps object inheritance from
2732 * being a negative impact on memory usage.
2733 */
2734static void
a108bf71 2735vm_map_split(vm_map_entry_t entry)
984263bc
MD
2736{
2737 vm_page_t m;
2738 vm_object_t orig_object, new_object, source;
2739 vm_offset_t s, e;
2740 vm_pindex_t offidxstart, offidxend, idx;
2741 vm_size_t size;
2742 vm_ooffset_t offset;
2743
2744 orig_object = entry->object.vm_object;
2745 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2746 return;
2747 if (orig_object->ref_count <= 1)
2748 return;
2749
2750 offset = entry->offset;
2751 s = entry->start;
2752 e = entry->end;
2753
2754 offidxstart = OFF_TO_IDX(offset);
2755 offidxend = offidxstart + OFF_TO_IDX(e - s);
2756 size = offidxend - offidxstart;
2757
57f7b636
MD
2758 new_object = vm_pager_allocate(orig_object->type, NULL,
2759 IDX_TO_OFF(size), VM_PROT_ALL, 0);
984263bc
MD
2760 if (new_object == NULL)
2761 return;
2762
2763 source = orig_object->backing_object;
2764 if (source != NULL) {
2765 vm_object_reference(source); /* Referenced by new_object */
2766 LIST_INSERT_HEAD(&source->shadow_head,
2767 new_object, shadow_list);
2768 vm_object_clear_flag(source, OBJ_ONEMAPPING);
2769 new_object->backing_object_offset =
2770 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2771 new_object->backing_object = source;
2772 source->shadow_count++;
2773 source->generation++;
2774 }
2775
2776 for (idx = 0; idx < size; idx++) {
2777 vm_page_t m;
2778
06ecca5a 2779 /*
5fd012e0 2780 * A critical section is required to avoid a race between
06ecca5a
MD
2781 * the lookup and an interrupt/unbusy/free and our busy
2782 * check.
2783 */
5fd012e0 2784 crit_enter();
984263bc
MD
2785 retry:
2786 m = vm_page_lookup(orig_object, offidxstart + idx);
06ecca5a 2787 if (m == NULL) {
5fd012e0 2788 crit_exit();
984263bc 2789 continue;
06ecca5a 2790 }
984263bc
MD
2791
2792 /*
2793 * We must wait for pending I/O to complete before we can
2794 * rename the page.
2795 *
2796 * We do not have to VM_PROT_NONE the page as mappings should
2797 * not be changed by this operation.
2798 */
2799 if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2800 goto retry;
984263bc
MD
2801 vm_page_busy(m);
2802 vm_page_rename(m, new_object, idx);
2803 /* page automatically made dirty by rename and cache handled */
2804 vm_page_busy(m);
5fd012e0 2805 crit_exit();
984263bc
MD
2806 }
2807
2808 if (orig_object->type == OBJT_SWAP) {
2809 vm_object_pip_add(orig_object, 1);
2810 /*
2811 * copy orig_object pages into new_object
2812 * and destroy unneeded pages in
2813 * shadow object.
2814 */
2815 swap_pager_copy(orig_object, new_object, offidxstart, 0);
2816 vm_object_pip_wakeup(orig_object);
2817 }
2818
06ecca5a
MD
2819 /*
2820 * Wakeup the pages we played with. No spl protection is needed
2821 * for a simple wakeup.
2822 */
984263bc
MD
2823 for (idx = 0; idx < size; idx++) {
2824 m = vm_page_lookup(new_object, idx);
06ecca5a 2825 if (m)
984263bc 2826 vm_page_wakeup(m);
984263bc
MD
2827 }
2828
2829 entry->object.vm_object = new_object;
2830 entry->offset = 0LL;
2831 vm_object_deallocate(orig_object);
2832}
2833
2834/*
2835 * vm_map_copy_entry:
2836 *
2837 * Copies the contents of the source entry to the destination
2838 * entry. The entries *must* be aligned properly.
2839 */
2840static void
a108bf71
MD
2841vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
2842 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
984263bc
MD
2843{
2844 vm_object_t src_object;
2845
1b874851
MD
2846 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP)
2847 return;
2848 if (src_entry->maptype == VM_MAPTYPE_SUBMAP)
984263bc
MD
2849 return;
2850
2851 if (src_entry->wired_count == 0) {
984263bc
MD
2852 /*
2853 * If the source entry is marked needs_copy, it is already
2854 * write-protected.
2855 */
2856 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2857 pmap_protect(src_map->pmap,
2858 src_entry->start,
2859 src_entry->end,
2860 src_entry->protection & ~VM_PROT_WRITE);
2861 }
2862
2863 /*
2864 * Make a copy of the object.
2865 */
2866 if ((src_object = src_entry->object.vm_object) != NULL) {
984263bc
MD
2867 if ((src_object->handle == NULL) &&
2868 (src_object->type == OBJT_DEFAULT ||
2869 src_object->type == OBJT_SWAP)) {
2870 vm_object_collapse(src_object);
2871 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2872 vm_map_split(src_entry);
2873 src_object = src_entry->object.vm_object;
2874 }
2875 }
2876
2877 vm_object_reference(src_object);
2878 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2879 dst_entry->object.vm_object = src_object;
2880 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2881 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2882 dst_entry->offset = src_entry->offset;
2883 } else {
2884 dst_entry->object.vm_object = NULL;
2885 dst_entry->offset = 0;
2886 }
2887
2888 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2889 dst_entry->end - dst_entry->start, src_entry->start);
2890 } else {
2891 /*
2892 * Of course, wired down pages can't be set copy-on-write.
2893 * Cause wired pages to be copied into the new map by
2894 * simulating faults (the new pages are pageable)
2895 */
2896 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2897 }
2898}
2899
2900/*
2901 * vmspace_fork:
2902 * Create a new process vmspace structure and vm_map
2903 * based on those of an existing process. The new map
2904 * is based on the old map, according to the inheritance
2905 * values on the regions in that map.
2906 *
2907 * The source map must not be locked.
2908 */
2909struct vmspace *
a108bf71 2910vmspace_fork(struct vmspace *vm1)
984263bc
MD
2911{
2912 struct vmspace *vm2;
2913 vm_map_t old_map = &vm1->vm_map;
2914 vm_map_t new_map;
2915 vm_map_entry_t old_entry;
2916 vm_map_entry_t new_entry;
2917 vm_object_t object;
a108bf71 2918 int count;
984263bc
MD
2919
2920 vm_map_lock(old_map);
2921 old_map->infork = 1;
2922
239b4df9
MD
2923 /*
2924 * XXX Note: upcalls are not copied.
2925 */
984263bc
MD
2926 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2927 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
239b4df9 2928 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
984263bc
MD
2929 new_map = &vm2->vm_map; /* XXX */
2930 new_map->timestamp = 1;
2931
a108bf71 2932 count = 0;
984263bc 2933 old_entry = old_map->header.next;
a108bf71
MD
2934 while (old_entry != &old_map->header) {
2935 ++count;
2936 old_entry = old_entry->next;
2937 }
984263bc 2938
a108bf71
MD
2939 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
2940
2941 old_entry = old_map->header.next;
984263bc 2942 while (old_entry != &old_map->header) {
1b874851 2943 if (old_entry->maptype == VM_MAPTYPE_SUBMAP)
984263bc
MD
2944 panic("vm_map_fork: encountered a submap");
2945
2946 switch (old_entry->inheritance) {
2947 case VM_INHERIT_NONE:
2948 break;
2949
2950 case VM_INHERIT_SHARE:
2951 /*
1b874851
MD
2952 * Clone the entry, creating the shared object if
2953 * necessary.
984263bc
MD
2954 */
2955 object = old_entry->object.vm_object;
2956 if (object == NULL) {
53025830
MD
2957 vm_map_entry_allocate_object(old_entry);
2958 object = old_entry->object.vm_object;
984263bc
MD
2959 }
2960
2961 /*
53025830 2962 * Add the reference before calling vm_map_entry_shadow
984263bc
MD
2963 * to insure that a shadow object is created.
2964 */
2965 vm_object_reference(object);
2966 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
53025830 2967 vm_map_entry_shadow(old_entry);
984263bc
MD
2968 /* Transfer the second reference too. */
2969 vm_object_reference(
2970 old_entry->object.vm_object);
2971 vm_object_deallocate(object);
2972 object = old_entry->object.vm_object;
2973 }
2974 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2975
2976 /*
2977 * Clone the entry, referencing the shared object.
2978 */
a108bf71 2979 new_entry = vm_map_entry_create(new_map, &count);
984263bc
MD
2980 *new_entry = *old_entry;
2981 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2982 new_entry->wired_count = 0;
2983
2984 /*
2985 * Insert the entry into the new map -- we know we're
2986 * inserting at the end of the new map.
2987 */
2988
2989 vm_map_entry_link(new_map, new_map->header.prev,
2990 new_entry);
2991
2992 /*
2993 * Update the physical map
2994 */
2995
2996 pmap_copy(new_map->pmap, old_map->pmap,
2997 new_entry->start,
2998 (old_entry->end - old_entry->start),
2999 old_entry->start);
3000 break;
3001
3002 case VM_INHERIT_COPY:
3003 /*
3004 * Clone the entry and link into the map.
3005 */
a108bf71 3006 new_entry = vm_map_entry_create(new_map, &count);
984263bc
MD
3007 *new_entry = *old_entry;
3008 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3009 new_entry->wired_count = 0;
3010 new_entry->object.vm_object = NULL;
3011 vm_map_entry_link(new_map, new_map->header.prev,
3012 new_entry);
3013 vm_map_copy_entry(old_map, new_map, old_entry,
3014 new_entry);
3015 break;
3016 }
3017 old_entry = old_entry->next;
3018 }
3019
3020 new_map->size = old_map->size;
3021 old_map->infork = 0;
3022 vm_map_unlock(old_map);
a108bf71 3023 vm_map_entry_release(count);
984263bc
MD
3024
3025 return (vm2);
3026}
3027
3028int
3029vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
c809941b 3030 int flags, vm_prot_t prot, vm_prot_t max, int cow)
984263bc 3031{
85d25bcf
MD
3032 vm_map_entry_t prev_entry;
3033 vm_map_entry_t new_stack_entry;
3034 vm_size_t init_ssize;
3035 int rv;
a108bf71 3036 int count;
85d25bcf 3037 vm_offset_t tmpaddr;
984263bc 3038
c809941b 3039 cow |= MAP_IS_STACK;
984263bc
MD
3040
3041 if (max_ssize < sgrowsiz)
3042 init_ssize = max_ssize;
3043 else
3044 init_ssize = sgrowsiz;
3045
a108bf71 3046 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc
MD
3047 vm_map_lock(map);
3048
85d25bcf
MD
3049 /*
3050 * Find space for the mapping
3051 */
cadb984b 3052 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
c809941b
MD
3053 if (vm_map_findspace(map, addrbos, max_ssize, 1,
3054 flags, &tmpaddr)) {
85d25bcf
MD
3055 vm_map_unlock(map);
3056 vm_map_entry_release(count);
3057 return (KERN_NO_SPACE);
3058 }
3059 addrbos = tmpaddr;
3060 }
3061
984263bc
MD
3062 /* If addr is already mapped, no go */
3063 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3064 vm_map_unlock(map);
a108bf71 3065 vm_map_entry_release(count);
984263bc
MD
3066 return (KERN_NO_SPACE);
3067 }
3068
85d25bcf
MD
3069#if 0
3070 /* XXX already handled by kern_mmap() */
984263bc
MD
3071 /* If we would blow our VMEM resource limit, no go */
3072 if (map->size + init_ssize >
3073 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3074 vm_map_unlock(map);
a108bf71 3075 vm_map_entry_release(count);
984263bc
MD
3076 return (KERN_NO_SPACE);
3077 }
85d25bcf 3078#endif
984263bc 3079
85d25bcf
MD
3080 /*
3081 * If we can't accomodate max_ssize in the current mapping,
984263bc
MD
3082 * no go. However, we need to be aware that subsequent user
3083 * mappings might map into the space we have reserved for
3084 * stack, and currently this space is not protected.
3085 *
3086 * Hopefully we will at least detect this condition
3087 * when we try to grow the stack.
3088 */
3089 if ((prev_entry->next != &map->header) &&
3090 (prev_entry->next->start < addrbos + max_ssize)) {
3091 vm_map_unlock(map);
a108bf71 3092 vm_map_entry_release(count);
984263bc
MD
3093 return (KERN_NO_SPACE);
3094 }
3095
85d25bcf
MD
3096 /*
3097 * We initially map a stack of only init_ssize. We will
984263bc
MD
3098 * grow as needed later. Since this is to be a grow
3099 * down stack, we map at the top of the range.
3100 *
3101 * Note: we would normally expect prot and max to be
3102 * VM_PROT_ALL, and cow to be 0. Possibly we should
3103 * eliminate these as input parameters, and just
3104 * pass these values here in the insert call.
3105 */
a108bf71
MD
3106 rv = vm_map_insert(map, &count,
3107 NULL, 0, addrbos + max_ssize - init_ssize,
1b874851
MD
3108 addrbos + max_ssize,
3109 VM_MAPTYPE_NORMAL,
3110 prot, max,
3111 cow);
984263bc
MD
3112
3113 /* Now set the avail_ssize amount */
517e1666 3114 if (rv == KERN_SUCCESS) {
984263bc 3115 if (prev_entry != &map->header)
a108bf71 3116 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
984263bc
MD
3117 new_stack_entry = prev_entry->next;
3118 if (new_stack_entry->end != addrbos + max_ssize ||
3119 new_stack_entry->start != addrbos + max_ssize - init_ssize)
3120 panic ("Bad entry start/end for new stack entry");
3121 else
afeabdca 3122 new_stack_entry->aux.avail_ssize = max_ssize - init_ssize;
984263bc
MD
3123 }
3124
3125 vm_map_unlock(map);
a108bf71 3126 vm_map_entry_release(count);
984263bc
MD
3127 return (rv);
3128}
3129
3130/* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3131 * desired address is already mapped, or if we successfully grow
3132 * the stack. Also returns KERN_SUCCESS if addr is outside the
3133 * stack range (this is strange, but preserves compatibility with
3134 * the grow function in vm_machdep.c).
3135 */
3136int
3137vm_map_growstack (struct proc *p, vm_offset_t addr)
3138{
3139 vm_map_entry_t prev_entry;
3140 vm_map_entry_t stack_entry;
3141 vm_map_entry_t new_stack_entry;
3142 struct vmspace *vm = p->p_vmspace;
3143 vm_map_t map = &vm->vm_map;
3144 vm_offset_t end;
a108bf71
MD
3145 int grow_amount;
3146 int rv = KERN_SUCCESS;
3147 int is_procstack;
3148 int use_read_lock = 1;
3149 int count;
984263bc 3150
a108bf71 3151 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc
MD
3152Retry:
3153 if (use_read_lock)
3154 vm_map_lock_read(map);
3155 else
3156 vm_map_lock(map);
3157
3158 /* If addr is already in the entry range, no need to grow.*/
3159 if (vm_map_lookup_entry(map, addr, &prev_entry))
3160 goto done;
3161
3162 if ((stack_entry = prev_entry->next) == &map->header)
3163 goto done;
3164 if (prev_entry == &map->header)
afeabdca 3165 end = stack_entry->start - stack_entry->aux.avail_ssize;
984263bc
MD
3166 else
3167 end = prev_entry->end;
3168
c809941b
MD
3169 /*
3170 * This next test mimics the old grow function in vm_machdep.c.
984263bc
MD
3171 * It really doesn't quite make sense, but we do it anyway
3172 * for compatibility.
3173 *
3174 * If not growable stack, return success. This signals the
3175 * caller to proceed as he would normally with normal vm.
3176 */
afeabdca 3177 if (stack_entry->aux.avail_ssize < 1 ||
984263bc 3178 addr >= stack_entry->start ||
afeabdca 3179 addr < stack_entry->start - stack_entry->aux.avail_ssize) {
984263bc
MD
3180 goto done;
3181 }
3182
3183 /* Find the minimum grow amount */
3184 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
afeabdca 3185 if (grow_amount > stack_entry->aux.avail_ssize) {
984263bc
MD
3186 rv = KERN_NO_SPACE;
3187 goto done;
3188 }
3189
c809941b
MD
3190 /*
3191 * If there is no longer enough space between the entries
984263bc
MD
3192 * nogo, and adjust the available space. Note: this
3193 * should only happen if the user has mapped into the
3194 * stack area after the stack was created, and is
3195 * probably an error.
3196 *
3197 * This also effectively destroys any guard page the user
3198 * might have intended by limiting the stack size.
3199 */
3200 if (grow_amount > stack_entry->start - end) {
3201 if (use_read_lock && vm_map_lock_upgrade(map)) {
3202 use_read_lock = 0;
3203 goto Retry;
3204 }
3205 use_read_lock = 0;
afeabdca 3206 stack_entry->aux.avail_ssize = stack_entry->start - end;
984263bc
MD
3207 rv = KERN_NO_SPACE;
3208 goto done;
3209 }
3210
3211 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
3212
3213 /* If this is the main process stack, see if we're over the
3214 * stack limit.
3215 */
3216 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3217 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3218 rv = KERN_NO_SPACE;
3219 goto done;
3220 }
3221
3222 /* Round up the grow amount modulo SGROWSIZ */
3223 grow_amount = roundup (grow_amount, sgrowsiz);
afeabdca
MD
3224 if (grow_amount > stack_entry->aux.avail_ssize) {
3225 grow_amount = stack_entry->aux.avail_ssize;
984263bc
MD
3226 }
3227 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3228 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3229 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
3230 ctob(vm->vm_ssize);
3231 }
3232
3233 /* If we would blow our VMEM resource limit, no go */
be77b5f9 3234 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
984263bc
MD
3235 rv = KERN_NO_SPACE;
3236 goto done;
3237 }
3238
3239 if (use_read_lock && vm_map_lock_upgrade(map)) {
3240 use_read_lock = 0;
3241 goto Retry;
3242 }
3243 use_read_lock = 0;
3244
3245 /* Get the preliminary new entry start value */
3246 addr = stack_entry->start - grow_amount;
3247
3248 /* If this puts us into the previous entry, cut back our growth
3249 * to the available space. Also, see the note above.
3250 */
3251 if (addr < end) {
afeabdca 3252 stack_entry->aux.avail_ssize = stack_entry->start - end;
984263bc
MD
3253 addr = end;
3254 }
3255
a108bf71
MD
3256 rv = vm_map_insert(map, &count,
3257 NULL, 0, addr, stack_entry->start,
1b874851
MD
3258 VM_MAPTYPE_NORMAL,
3259 VM_PROT_ALL, VM_PROT_ALL,
984263bc
MD
3260 0);
3261
3262 /* Adjust the available stack space by the amount we grew. */
3263 if (rv == KERN_SUCCESS) {
3264 if (prev_entry != &map->header)
a108bf71 3265 vm_map_clip_end(map, prev_entry, addr, &count);
984263bc
MD
3266 new_stack_entry = prev_entry->next;
3267 if (new_stack_entry->end != stack_entry->start ||
3268 new_stack_entry->start != addr)
3269 panic ("Bad stack grow start/end in new stack entry");
3270 else {
afeabdca
MD
3271 new_stack_entry->aux.avail_ssize =
3272 stack_entry->aux.avail_ssize -
3273 (new_stack_entry->end - new_stack_entry->start);
984263bc
MD
3274 if (is_procstack)
3275 vm->vm_ssize += btoc(new_stack_entry->end -
3276 new_stack_entry->start);
3277 }
3278 }
3279
3280done:
3281 if (use_read_lock)
3282 vm_map_unlock_read(map);
3283 else
3284 vm_map_unlock(map);
a108bf71 3285 vm_map_entry_release(count);
984263bc
MD
3286 return (rv);
3287}
3288
3289/*
3290 * Unshare the specified VM space for exec. If other processes are
3291 * mapped to it, then create a new one. The new vmspace is null.
3292 */
984263bc 3293void
29802dbb 3294vmspace_exec(struct proc *p, struct vmspace *vmcopy)
a108bf71 3295{
984263bc
MD
3296 struct vmspace *oldvmspace = p->p_vmspace;
3297 struct vmspace *newvmspace;
3298 vm_map_t map = &p->p_vmspace->vm_map;
3299
29802dbb
MD
3300 /*
3301 * If we are execing a resident vmspace we fork it, otherwise
239b4df9
MD
3302 * we create a new vmspace. Note that exitingcnt and upcalls
3303 * are not copied to the new vmspace.
29802dbb
MD
3304 */
3305 if (vmcopy) {
3306 newvmspace = vmspace_fork(vmcopy);
3307 } else {
3308 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
3309 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
239b4df9
MD
3310 (caddr_t)&oldvmspace->vm_endcopy -
3311 (caddr_t)&oldvmspace->vm_startcopy);
29802dbb
MD
3312 }
3313
984263bc 3314 /*
e3161323
MD
3315 * Finish initializing the vmspace before assigning it
3316 * to the process. The vmspace will become the current vmspace
3317 * if p == curproc.
984263bc 3318 */
984263bc 3319 pmap_pinit2(vmspace_pmap(newvmspace));
e3161323
MD
3320 pmap_replacevm(p, newvmspace, 0);
3321 sysref_put(&oldvmspace->vm_sysref);
984263bc
MD
3322}
3323
3324/*
3325 * Unshare the specified VM space for forcing COW. This
3326 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
239b4df9
MD
3327 *
3328 * The exitingcnt test is not strictly necessary but has been
3329 * included for code sanity (to make the code a bit more deterministic).
984263bc
MD
3330 */
3331
3332void
a108bf71
MD
3333vmspace_unshare(struct proc *p)
3334{
984263bc
MD
3335 struct vmspace *oldvmspace = p->p_vmspace;
3336 struct vmspace *newvmspace;
3337
e3161323 3338 if (oldvmspace->vm_sysref.refcnt == 1 && oldvmspace->vm_exitingcnt == 0)
984263bc
MD
3339 return;
3340 newvmspace = vmspace_fork(oldvmspace);
984263bc 3341 pmap_pinit2(vmspace_pmap(newvmspace));
e3161323
MD
3342 pmap_replacevm(p, newvmspace, 0);
3343 sysref_put(&oldvmspace->vm_sysref);
984263bc 3344}
984263bc
MD
3345
3346/*
3347 * vm_map_lookup:
3348 *
3349 * Finds the VM object, offset, and
3350 * protection for a given virtual address in the
3351 * specified map, assuming a page fault of the
3352 * type specified.
3353 *
3354 * Leaves the map in question locked for read; return
3355 * values are guaranteed until a vm_map_lookup_done
3356 * call is performed. Note that the map argument
3357 * is in/out; the returned map must be used in
3358 * the call to vm_map_lookup_done.
3359 *
3360 * A handle (out_entry) is returned for use in
3361 * vm_map_lookup_done, to make that fast.
3362 *
3363 * If a lookup is requested with "write protection"
3364 * specified, the map may be changed to perform virtual
3365 * copying operations, although the data referenced will
3366 * remain the same.
3367 */
3368int
3369vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3370 vm_offset_t vaddr,
3371 vm_prot_t fault_typea,
3372 vm_map_entry_t *out_entry, /* OUT */
3373 vm_object_t *object, /* OUT */
3374 vm_pindex_t *pindex, /* OUT */
3375 vm_prot_t *out_prot, /* OUT */
3376 boolean_t *wired) /* OUT */
3377{
3378 vm_map_entry_t entry;
3379 vm_map_t map = *var_map;
3380 vm_prot_t prot;
3381 vm_prot_t fault_type = fault_typea;
3382 int use_read_lock = 1;
3383 int rv = KERN_SUCCESS;
3384
3385RetryLookup:
3386 if (use_read_lock)
3387 vm_map_lock_read(map);
3388 else
3389 vm_map_lock(map);
3390
3391 /*
3392 * If the map has an interesting hint, try it before calling full
3393 * blown lookup routine.
3394 */
3395 entry = map->hint;
3396 *out_entry = entry;
3397
3398 if ((entry == &map->header) ||
3399 (vaddr < entry->start) || (vaddr >= entry->end)) {
3400 vm_map_entry_t tmp_entry;
3401
3402 /*
3403 * Entry was either not a valid hint, or the vaddr was not
3404 * contained in the entry, so do a full lookup.
3405 */
3406 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
3407 rv = KERN_INVALID_ADDRESS;
3408 goto done;
3409 }
3410
3411 entry = tmp_entry;
3412 *out_entry = entry;
3413 }
3414
3415 /*
3416 * Handle submaps.
3417 */
1b874851 3418 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
984263bc
MD
3419 vm_map_t old_map = map;
3420
3421 *var_map = map = entry->object.sub_map;
3422 if (use_read_lock)
3423 vm_map_unlock_read(old_map);
3424 else
3425 vm_map_unlock(old_map);
3426 use_read_lock = 1;
3427 goto RetryLookup;
3428 }
3429
3430 /*
3431 * Check whether this task is allowed to have this page.
3432 * Note the special case for MAP_ENTRY_COW
3433 * pages with an override. This is to implement a forced
3434 * COW for debuggers.
3435 */
3436
3437 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3438 prot = entry->max_protection;
3439 else
3440 prot = entry->protection;
3441
3442 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3443 if ((fault_type & prot) != fault_type) {
3444 rv = KERN_PROTECTION_FAILURE;
3445 goto done;
3446 }
3447
3448 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3449 (entry->eflags & MAP_ENTRY_COW) &&
3450 (fault_type & VM_PROT_WRITE) &&
3451 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3452 rv = KERN_PROTECTION_FAILURE;
3453 goto done;
3454 }
3455
3456 /*
3457 * If this page is not pageable, we have to get it for all possible
3458 * accesses.
3459 */
984263bc
MD
3460 *wired = (entry->wired_count != 0);
3461 if (*wired)
3462 prot = fault_type = entry->protection;
3463
3464 /*
568e6804
MD
3465 * Virtual page tables may need to update the accessed (A) bit
3466 * in a page table entry. Upgrade the fault to a write fault for
3467 * that case if the map will support it. If the map does not support
3468 * it the page table entry simply will not be updated.
984263bc 3469 */
568e6804
MD
3470 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
3471 if (prot & VM_PROT_WRITE)
3472 fault_type |= VM_PROT_WRITE;
3473 }
984263bc 3474
568e6804
MD
3475 /*
3476 * If the entry was copy-on-write, we either ...
3477 */
984263bc
MD
3478 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3479 /*
3480 * If we want to write the page, we may as well handle that
3481 * now since we've got the map locked.
3482 *
3483 * If we don't need to write the page, we just demote the
3484 * permissions allowed.
3485 */
3486
3487 if (fault_type & VM_PROT_WRITE) {
3488 /*
3489 * Make a new object, and place it in the object
3490 * chain. Note that no new references have appeared
3491 * -- one just moved from the map to the new
3492 * object.
3493 */
3494
3495 if (use_read_lock && vm_map_lock_upgrade(map)) {
3496 use_read_lock = 0;
3497 goto RetryLookup;
3498 }
3499 use_read_lock = 0;
3500
53025830 3501 vm_map_entry_shadow(entry);
984263bc
MD
3502 } else {
3503 /*
3504 * We're attempting to read a copy-on-write page --
3505 * don't allow writes.
3506 */
3507
3508 prot &= ~VM_PROT_WRITE;
3509 }
3510 }
3511
3512 /*
3513 * Create an object if necessary.
3514 */
3515 if (entry->object.vm_object == NULL &&
3516 !map->system_map) {
3517 if (use_read_lock && vm_map_lock_upgrade(map)) {
3518 use_read_lock = 0;
3519 goto RetryLookup;
3520 }
3521 use_read_lock = 0;
53025830 3522 vm_map_entry_allocate_object(entry);
984263bc
MD
3523 }
3524
3525 /*
3526 * Return the object/offset from this entry. If the entry was
3527 * copy-on-write or empty, it has been fixed up.
3528 */
3529
3530 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3531 *object = entry->object.vm_object;
3532
3533 /*
3534 * Return whether this is the only map sharing this data. On
3535 * success we return with a read lock held on the map. On failure
3536 * we return with the map unlocked.
3537 */
3538 *out_prot = prot;
3539done:
3540 if (rv == KERN_SUCCESS) {
3541 if (use_read_lock == 0)
3542 vm_map_lock_downgrade(map);
3543 } else if (use_read_lock) {
3544 vm_map_unlock_read(map);
3545 } else {
3546 vm_map_unlock(map);
3547 }
3548 return (rv);
3549}
3550
3551/*
3552 * vm_map_lookup_done:
3553 *
3554 * Releases locks acquired by a vm_map_lookup
3555 * (according to the handle returned by that lookup).
3556 */
3557
3558void
a108bf71 3559vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
984263bc
MD
3560{
3561 /*
3562 * Unlock the main-level map
3563 */
984263bc 3564 vm_map_unlock_read(map);
a108bf71
MD
3565 if (count)
3566 vm_map_entry_release(count);
984263bc
MD
3567}
3568
984263bc
MD
3569#include "opt_ddb.h"
3570#ifdef DDB
3571#include <sys/kernel.h>
3572
3573#include <ddb/ddb.h>
3574
3575/*
3576 * vm_map_print: [ debug ]
3577 */
3578DB_SHOW_COMMAND(map, vm_map_print)
3579{
3580 static int nlines;
3581 /* XXX convert args. */
3582 vm_map_t map = (vm_map_t)addr;
3583 boolean_t full = have_addr;
3584
3585 vm_map_entry_t entry;
3586
3587 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3588 (void *)map,
3589 (void *)map->pmap, map->nentries, map->timestamp);
3590 nlines++;
3591
3592 if (!full && db_indent)
3593 return;
3594
3595 db_indent += 2;
3596 for (entry = map->header.next; entry != &map->header;
3597 entry = entry->next) {
3598 db_iprintf("map entry %p: start=%p, end=%p\n",
3599 (void *)entry, (void *)entry->start, (void *)entry->end);
3600 nlines++;
3601 {
3602 static char *inheritance_name[4] =
3603 {"share", "copy", "none", "donate_copy"};
3604
3605 db_iprintf(" prot=%x/%x/%s",
3606 entry->protection,
3607 entry->max_protection,
3608 inheritance_name[(int)(unsigned char)entry->inheritance]);
3609 if (entry->wired_count != 0)
3610 db_printf(", wired");
3611 }
1b874851 3612 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
984263bc
MD
3613 /* XXX no %qd in kernel. Truncate entry->offset. */
3614 db_printf(", share=%p, offset=0x%lx\n",
3615 (void *)entry->object.sub_map,
3616 (long)entry->offset);
3617 nlines++;
3618 if ((entry->prev == &map->header) ||
3619 (entry->prev->object.sub_map !=
3620 entry->object.sub_map)) {
3621 db_indent += 2;
3622 vm_map_print((db_expr_t)(intptr_t)
3623 entry->object.sub_map,
60233e58 3624 full, 0, NULL);
984263bc
MD
3625 db_indent -= 2;
3626 }
3627 } else {
3628 /* XXX no %qd in kernel. Truncate entry->offset. */
3629 db_printf(", object=%p, offset=0x%lx",
3630 (void *)entry->object.vm_object,
3631 (long)entry->offset);
3632 if (entry->eflags & MAP_ENTRY_COW)
3633 db_printf(", copy (%s)",
3634 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3635 db_printf("\n");
3636 nlines++;
3637
3638 if ((entry->prev == &map->header) ||
3639 (entry->prev->object.vm_object !=
3640 entry->object.vm_object)) {
3641 db_indent += 2;
3642 vm_object_print((db_expr_t)(intptr_t)
3643 entry->object.vm_object,