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