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