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