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