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