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