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