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