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