2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
40 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
44 * Mapped file (mmap) interface to VM
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include "opt_compat.h"
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/mutex.h>
58 #include <sys/sysproto.h>
59 #include <sys/filedesc.h>
61 #include <sys/resource.h>
62 #include <sys/resourcevar.h>
63 #include <sys/vnode.h>
64 #include <sys/fcntl.h>
70 #include <sys/vmmeter.h>
71 #include <sys/sysctl.h>
74 #include <vm/vm_param.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_pager.h>
80 #include <vm/vm_pageout.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_kern.h>
85 #ifndef _SYS_SYSPROTO_H_
91 static int max_proc_mmap;
92 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
95 * Set the maximum number of vm_map_entry structures per process. Roughly
96 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
97 * of our KVM malloc space still results in generous limits. We want a
98 * default that is good enough to prevent the kernel running out of resources
99 * if attacked from compromised user account but generous enough such that
100 * multi-threaded processes are not unduly inconvenienced.
102 static void vmmapentry_rsrc_init(void *);
103 SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL)
106 vmmapentry_rsrc_init(dummy)
109 max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
110 max_proc_mmap /= 100;
120 struct sbrk_args *uap;
122 /* Not yet implemented */
123 /* mtx_lock(&Giant); */
124 /* mtx_unlock(&Giant); */
128 #ifndef _SYS_SYSPROTO_H_
141 struct sstk_args *uap;
143 /* Not yet implemented */
144 /* mtx_lock(&Giant); */
145 /* mtx_unlock(&Giant); */
149 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
150 #ifndef _SYS_SYSPROTO_H_
151 struct getpagesize_args {
158 ogetpagesize(td, uap)
160 struct getpagesize_args *uap;
163 td->td_retval[0] = PAGE_SIZE;
166 #endif /* COMPAT_43 || COMPAT_SUNOS */
170 * Memory Map (mmap) system call. Note that the file offset
171 * and address are allowed to be NOT page aligned, though if
172 * the MAP_FIXED flag it set, both must have the same remainder
173 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
174 * page-aligned, the actual mapping starts at trunc_page(addr)
175 * and the return value is adjusted up by the page offset.
177 * Generally speaking, only character devices which are themselves
178 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
179 * there would be no cache coherency between a descriptor and a VM mapping
180 * both to the same character device.
182 * Block devices can be mmap'd no matter what they represent. Cache coherency
183 * is maintained as long as you do not write directly to the underlying
186 #ifndef _SYS_SYSPROTO_H_
204 struct mmap_args *uap;
206 struct file *fp = NULL;
209 vm_size_t size, pageoff;
210 vm_prot_t prot, maxprot;
213 int disablexworkaround;
215 struct vmspace *vms = td->td_proc->p_vmspace;
218 addr = (vm_offset_t) uap->addr;
220 prot = uap->prot & VM_PROT_ALL;
226 /* make sure mapping fits into numeric range etc */
227 if ((ssize_t) uap->len < 0 ||
228 ((flags & MAP_ANON) && uap->fd != -1))
231 if (flags & MAP_STACK) {
232 if ((uap->fd != -1) ||
233 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
240 * Align the file position to a page boundary,
241 * and save its page offset component.
243 pageoff = (pos & PAGE_MASK);
246 /* Adjust size for rounding (on both ends). */
247 size += pageoff; /* low end... */
248 size = (vm_size_t) round_page(size); /* hi end */
251 * Check for illegal addresses. Watch out for address wrap... Note
252 * that VM_*_ADDRESS are not constants due to casts (argh).
254 if (flags & MAP_FIXED) {
256 * The specified address must have the same remainder
257 * as the file offset taken modulo PAGE_SIZE, so it
258 * should be aligned after adjustment by pageoff.
261 if (addr & PAGE_MASK)
263 /* Address range must be all in user VM space. */
264 if (addr < vm_map_min(&vms->vm_map) ||
265 addr + size > vm_map_max(&vms->vm_map))
267 if (addr + size < addr)
271 * XXX for non-fixed mappings where no hint is provided or
272 * the hint would fall in the potential heap space,
273 * place it after the end of the largest possible heap.
275 * There should really be a pmap call to determine a reasonable
278 else if (addr == 0 ||
279 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
280 addr < round_page((vm_offset_t)vms->vm_daddr +
281 td->td_proc->p_rlimit[RLIMIT_DATA].rlim_max)))
282 addr = round_page((vm_offset_t)vms->vm_daddr +
283 td->td_proc->p_rlimit[RLIMIT_DATA].rlim_max);
285 mtx_lock(&Giant); /* syscall marked mp-safe but isn't */
287 if (flags & MAP_ANON) {
289 * Mapping blank space is trivial.
292 maxprot = VM_PROT_ALL;
297 * Mapping file, get fp for validation. Obtain vnode and make
298 * sure it is of appropriate type.
299 * don't let the descriptor disappear on us if we block
301 if ((error = fget(td, uap->fd, &fp)) != 0)
303 if (fp->f_type != DTYPE_VNODE) {
309 * POSIX shared-memory objects are defined to have
310 * kernel persistence, and are not defined to support
311 * read(2)/write(2) -- or even open(2). Thus, we can
312 * use MAP_ASYNC to trade on-disk coherence for speed.
313 * The shm_open(3) library routine turns on the FPOSIXSHM
314 * flag to request this behavior.
316 if (fp->f_flag & FPOSIXSHM)
319 error = vget(vp, LK_EXCLUSIVE, td);
322 if (vp->v_type != VREG && vp->v_type != VCHR) {
326 if (vp->v_type == VREG) {
328 * Get the proper underlying object
330 if (VOP_GETVOBJECT(vp, &obj) != 0) {
334 if (obj->handle != vp) {
336 vp = (struct vnode*)obj->handle;
337 vget(vp, LK_EXCLUSIVE, td);
341 * XXX hack to handle use of /dev/zero to map anon memory (ala
344 if ((vp->v_type == VCHR) &&
345 (vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON)) {
347 maxprot = VM_PROT_ALL;
353 * cdevs does not provide private mappings of any kind.
356 * However, for XIG X server to continue to work,
357 * we should allow the superuser to do it anyway.
358 * We only allow it at securelevel < 1.
359 * (Because the XIG X server writes directly to video
360 * memory via /dev/mem, it should never work at any
362 * XXX this will have to go
364 if (securelevel_ge(td->td_ucred, 1))
365 disablexworkaround = 1;
367 disablexworkaround = suser(td);
368 if (vp->v_type == VCHR && disablexworkaround &&
369 (flags & (MAP_PRIVATE|MAP_COPY))) {
374 * Ensure that file and memory protections are
375 * compatible. Note that we only worry about
376 * writability if mapping is shared; in this case,
377 * current and max prot are dictated by the open file.
378 * XXX use the vnode instead? Problem is: what
379 * credentials do we use for determination? What if
380 * proc does a setuid?
382 maxprot = VM_PROT_EXECUTE; /* ??? */
383 if (fp->f_flag & FREAD) {
384 maxprot |= VM_PROT_READ;
385 } else if (prot & PROT_READ) {
390 * If we are sharing potential changes (either via
391 * MAP_SHARED or via the implicit sharing of character
392 * device mappings), and we are trying to get write
393 * permission although we opened it without asking
394 * for it, bail out. Check for superuser, only if
395 * we're at securelevel < 1, to allow the XIG X server
396 * to continue to work.
398 if ((flags & MAP_SHARED) != 0 ||
399 (vp->v_type == VCHR && disablexworkaround)) {
400 if ((fp->f_flag & FWRITE) != 0) {
404 td->td_ucred, td))) {
408 (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0) {
409 maxprot |= VM_PROT_WRITE;
410 } else if (prot & PROT_WRITE) {
414 } else if ((prot & PROT_WRITE) != 0) {
419 maxprot |= VM_PROT_WRITE;
426 * Do not allow more then a certain number of vm_map_entry structures
427 * per process. Scale with the number of rforks sharing the map
428 * to make the limit reasonable for threads.
431 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
439 if (handle != NULL && (flags & MAP_SHARED) != 0) {
440 error = mac_check_vnode_mmap(td->td_ucred,
441 (struct vnode *)handle, prot);
445 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
449 td->td_retval[0] = (register_t) (addr + pageoff);
461 #ifndef _SYS_SYSPROTO_H_
474 struct ommap_args *uap;
476 struct mmap_args nargs;
477 static const char cvtbsdprot[8] = {
481 PROT_EXEC | PROT_WRITE,
483 PROT_EXEC | PROT_READ,
484 PROT_WRITE | PROT_READ,
485 PROT_EXEC | PROT_WRITE | PROT_READ,
488 #define OMAP_ANON 0x0002
489 #define OMAP_COPY 0x0020
490 #define OMAP_SHARED 0x0010
491 #define OMAP_FIXED 0x0100
493 nargs.addr = uap->addr;
494 nargs.len = uap->len;
495 nargs.prot = cvtbsdprot[uap->prot & 0x7];
497 if (uap->flags & OMAP_ANON)
498 nargs.flags |= MAP_ANON;
499 if (uap->flags & OMAP_COPY)
500 nargs.flags |= MAP_COPY;
501 if (uap->flags & OMAP_SHARED)
502 nargs.flags |= MAP_SHARED;
504 nargs.flags |= MAP_PRIVATE;
505 if (uap->flags & OMAP_FIXED)
506 nargs.flags |= MAP_FIXED;
508 nargs.pos = uap->pos;
509 return (mmap(td, &nargs));
511 #endif /* COMPAT_43 */
514 #ifndef _SYS_SYSPROTO_H_
527 struct msync_args *uap;
530 vm_size_t size, pageoff;
535 addr = (vm_offset_t) uap->addr;
539 pageoff = (addr & PAGE_MASK);
542 size = (vm_size_t) round_page(size);
543 if (addr + size < addr)
546 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
551 map = &td->td_proc->p_vmspace->vm_map;
554 * XXX Gak! If size is zero we are supposed to sync "all modified
555 * pages with the region containing addr". Unfortunately, we don't
556 * really keep track of individual mmaps so we approximate by flushing
557 * the range of the map entry containing addr. This can be incorrect
558 * if the region splits or is coalesced with a neighbor.
561 vm_map_entry_t entry;
563 vm_map_lock_read(map);
564 rv = vm_map_lookup_entry(map, addr, &entry);
565 vm_map_unlock_read(map);
571 size = entry->end - entry->start;
575 * Clean the pages and interpret the return value.
577 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
578 (flags & MS_INVALIDATE) != 0);
586 case KERN_INVALID_ADDRESS:
587 return (EINVAL); /* Sun returns ENOMEM? */
595 #ifndef _SYS_SYSPROTO_H_
607 struct munmap_args *uap;
610 vm_size_t size, pageoff;
613 addr = (vm_offset_t) uap->addr;
616 pageoff = (addr & PAGE_MASK);
619 size = (vm_size_t) round_page(size);
620 if (addr + size < addr)
627 * Check for illegal addresses. Watch out for address wrap...
629 map = &td->td_proc->p_vmspace->vm_map;
630 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
633 * Make sure entire range is allocated.
635 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
638 /* returns nothing but KERN_SUCCESS anyway */
639 (void) vm_map_remove(map, addr, addr + size);
650 * XXX should unmap any regions mapped to this file
652 FILEDESC_LOCK(p->p_fd);
653 td->td_proc->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
654 FILEDESC_UNLOCK(p->p_fd);
658 #ifndef _SYS_SYSPROTO_H_
659 struct mprotect_args {
671 struct mprotect_args *uap;
674 vm_size_t size, pageoff;
677 addr = (vm_offset_t) uap->addr;
679 prot = uap->prot & VM_PROT_ALL;
680 #if defined(VM_PROT_READ_IS_EXEC)
681 if (prot & VM_PROT_READ)
682 prot |= VM_PROT_EXECUTE;
685 pageoff = (addr & PAGE_MASK);
688 size = (vm_size_t) round_page(size);
689 if (addr + size < addr)
692 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
693 addr + size, prot, FALSE)) {
696 case KERN_PROTECTION_FAILURE:
702 #ifndef _SYS_SYSPROTO_H_
703 struct minherit_args {
715 struct minherit_args *uap;
718 vm_size_t size, pageoff;
719 vm_inherit_t inherit;
721 addr = (vm_offset_t)uap->addr;
723 inherit = uap->inherit;
725 pageoff = (addr & PAGE_MASK);
728 size = (vm_size_t) round_page(size);
729 if (addr + size < addr)
732 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
733 addr + size, inherit)) {
736 case KERN_PROTECTION_FAILURE:
742 #ifndef _SYS_SYSPROTO_H_
743 struct madvise_args {
757 struct madvise_args *uap;
759 vm_offset_t start, end;
765 * Check for our special case, advising the swap pager we are
768 if (uap->behav == MADV_PROTECT) {
773 p->p_flag |= P_PROTECTED;
779 * Check for illegal behavior
781 if (uap->behav < 0 || uap->behav > MADV_CORE)
784 * Check for illegal addresses. Watch out for address wrap... Note
785 * that VM_*_ADDRESS are not constants due to casts (argh).
787 map = &td->td_proc->p_vmspace->vm_map;
788 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
789 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
791 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
795 * Since this routine is only advisory, we default to conservative
798 start = trunc_page((vm_offset_t) uap->addr);
799 end = round_page((vm_offset_t) uap->addr + uap->len);
801 if (vm_map_madvise(map, start, end, uap->behav))
806 #ifndef _SYS_SYSPROTO_H_
807 struct mincore_args {
821 struct mincore_args *uap;
823 vm_offset_t addr, first_addr;
824 vm_offset_t end, cend;
829 int vecindex, lastvecindex;
830 vm_map_entry_t current;
831 vm_map_entry_t entry;
833 unsigned int timestamp;
836 * Make sure that the addresses presented are valid for user
839 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
840 end = addr + (vm_size_t)round_page(uap->len);
841 map = &td->td_proc->p_vmspace->vm_map;
842 if (end > vm_map_max(map) || end < addr)
846 * Address of byte vector
850 pmap = vmspace_pmap(td->td_proc->p_vmspace);
852 vm_map_lock_read(map);
854 timestamp = map->timestamp;
856 if (!vm_map_lookup_entry(map, addr, &entry))
860 * Do this on a map entry basis so that if the pages are not
861 * in the current processes address space, we can easily look
862 * up the pages elsewhere.
865 for (current = entry;
866 (current != &map->header) && (current->start < end);
867 current = current->next) {
870 * ignore submaps (for now) or null objects
872 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
873 current->object.vm_object == NULL)
877 * limit this scan to the current map entry and the
878 * limits for the mincore call
880 if (addr < current->start)
881 addr = current->start;
887 * scan this entry one page at a time
889 while (addr < cend) {
891 * Check pmap first, it is likely faster, also
892 * it can provide info as to whether we are the
893 * one referencing or modifying the page.
896 mincoreinfo = pmap_mincore(pmap, addr);
903 * calculate the page index into the object
905 offset = current->offset + (addr - current->start);
906 pindex = OFF_TO_IDX(offset);
907 VM_OBJECT_LOCK(current->object.vm_object);
908 m = vm_page_lookup(current->object.vm_object,
911 * if the page is resident, then gather information about
915 mincoreinfo = MINCORE_INCORE;
916 vm_page_lock_queues();
919 mincoreinfo |= MINCORE_MODIFIED_OTHER;
920 if ((m->flags & PG_REFERENCED) ||
921 pmap_ts_referenced(m)) {
922 vm_page_flag_set(m, PG_REFERENCED);
923 mincoreinfo |= MINCORE_REFERENCED_OTHER;
925 vm_page_unlock_queues();
927 VM_OBJECT_UNLOCK(current->object.vm_object);
931 * subyte may page fault. In case it needs to modify
932 * the map, we release the lock.
934 vm_map_unlock_read(map);
937 * calculate index into user supplied byte vector
939 vecindex = OFF_TO_IDX(addr - first_addr);
942 * If we have skipped map entries, we need to make sure that
943 * the byte vector is zeroed for those skipped entries.
945 while ((lastvecindex + 1) < vecindex) {
946 error = subyte(vec + lastvecindex, 0);
955 * Pass the page information to the user
957 error = subyte(vec + vecindex, mincoreinfo);
964 * If the map has changed, due to the subyte, the previous
965 * output may be invalid.
967 vm_map_lock_read(map);
968 if (timestamp != map->timestamp)
971 lastvecindex = vecindex;
977 * subyte may page fault. In case it needs to modify
978 * the map, we release the lock.
980 vm_map_unlock_read(map);
983 * Zero the last entries in the byte vector.
985 vecindex = OFF_TO_IDX(end - first_addr);
986 while ((lastvecindex + 1) < vecindex) {
987 error = subyte(vec + lastvecindex, 0);
996 * If the map has changed, due to the subyte, the previous
997 * output may be invalid.
999 vm_map_lock_read(map);
1000 if (timestamp != map->timestamp)
1002 vm_map_unlock_read(map);
1007 #ifndef _SYS_SYSPROTO_H_
1019 struct mlock_args *uap;
1022 vm_size_t size, pageoff;
1025 addr = (vm_offset_t) uap->addr;
1028 pageoff = (addr & PAGE_MASK);
1031 size = (vm_size_t) round_page(size);
1033 /* disable wrap around */
1034 if (addr + size < addr)
1037 if (atop(size) + cnt.v_wire_count > vm_page_max_wired)
1041 if (size + ptoa(pmap_wired_count(vm_map_pmap(&td->td_proc->p_vmspace->vm_map))) >
1042 td->td_proc->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
1050 error = vm_map_wire(&td->td_proc->p_vmspace->vm_map, addr,
1051 addr + size, VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1052 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1055 #ifndef _SYS_SYSPROTO_H_
1056 struct mlockall_args {
1067 struct mlockall_args *uap;
1072 map = &td->td_proc->p_vmspace->vm_map;
1075 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1080 * If wiring all pages in the process would cause it to exceed
1081 * a hard resource limit, return ENOMEM.
1083 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
1084 td->td_proc->p_rlimit[RLIMIT_MEMLOCK].rlim_cur))
1092 if (uap->how & MCL_FUTURE) {
1094 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1099 if (uap->how & MCL_CURRENT) {
1101 * P1003.1-2001 mandates that all currently mapped pages
1102 * will be memory resident and locked (wired) upon return
1103 * from mlockall(). vm_map_wire() will wire pages, by
1104 * calling vm_fault_wire() for each page in the region.
1106 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1107 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1108 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1114 #ifndef _SYS_SYSPROTO_H_
1115 struct munlockall_args {
1126 struct munlockall_args *uap;
1131 map = &td->td_proc->p_vmspace->vm_map;
1136 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1138 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1141 /* Forcibly unwire all pages. */
1142 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1143 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1148 #ifndef _SYS_SYSPROTO_H_
1149 struct munlock_args {
1160 struct munlock_args *uap;
1163 vm_size_t size, pageoff;
1166 addr = (vm_offset_t) uap->addr;
1169 pageoff = (addr & PAGE_MASK);
1172 size = (vm_size_t) round_page(size);
1174 /* disable wrap around */
1175 if (addr + size < addr)
1182 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, addr,
1183 addr + size, VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1184 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1192 * Internal version of mmap. Currently used by mmap, exec, and sys5
1193 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1196 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1197 vm_prot_t maxprot, int flags,
1203 struct vnode *vp = NULL;
1205 int rv = KERN_SUCCESS;
1206 vm_ooffset_t objsize;
1208 struct thread *td = curthread;
1213 objsize = size = round_page(size);
1215 if (td->td_proc->p_vmspace->vm_map.size + size >
1216 td->td_proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
1221 * We currently can only deal with page aligned file offsets.
1222 * The check is here rather than in the syscall because the
1223 * kernel calls this function internally for other mmaping
1224 * operations (such as in exec) and non-aligned offsets will
1225 * cause pmap inconsistencies...so we want to be sure to
1226 * disallow this in all cases.
1228 if (foff & PAGE_MASK)
1231 if ((flags & MAP_FIXED) == 0) {
1233 *addr = round_page(*addr);
1235 if (*addr != trunc_page(*addr))
1238 (void) vm_map_remove(map, *addr, *addr + size);
1242 * Lookup/allocate object.
1244 if (flags & MAP_ANON) {
1245 type = OBJT_DEFAULT;
1247 * Unnamed anonymous regions always start at 0.
1252 vp = (struct vnode *) handle;
1254 ASSERT_VOP_LOCKED(vp, "vm_mmap");
1255 if (vp->v_type == VCHR) {
1257 handle = vp->v_rdev;
1262 error = VOP_GETATTR(vp, &vat, td->td_ucred, td);
1267 objsize = round_page(vat.va_size);
1270 * if it is a regular file without any references
1271 * we do not need to sync it.
1273 if (vp->v_type == VREG && vat.va_nlink == 0) {
1274 flags |= MAP_NOSYNC;
1280 if (handle == NULL) {
1284 object = vm_pager_allocate(type,
1285 handle, objsize, prot, foff);
1286 if (object == NULL) {
1287 return (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1289 docow = MAP_PREFAULT_PARTIAL;
1293 * Force device mappings to be shared.
1295 if (type == OBJT_DEVICE) {
1296 flags &= ~(MAP_PRIVATE|MAP_COPY);
1297 flags |= MAP_SHARED;
1300 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1301 docow |= MAP_COPY_ON_WRITE;
1302 if (flags & MAP_NOSYNC)
1303 docow |= MAP_DISABLE_SYNCER;
1304 if (flags & MAP_NOCORE)
1305 docow |= MAP_DISABLE_COREDUMP;
1307 #if defined(VM_PROT_READ_IS_EXEC)
1308 if (prot & VM_PROT_READ)
1309 prot |= VM_PROT_EXECUTE;
1311 if (maxprot & VM_PROT_READ)
1312 maxprot |= VM_PROT_EXECUTE;
1316 *addr = pmap_addr_hint(object, *addr, size);
1318 if (flags & MAP_STACK)
1319 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1320 docow | MAP_STACK_GROWS_DOWN);
1322 rv = vm_map_find(map, object, foff, addr, size, fitit,
1323 prot, maxprot, docow);
1325 if (rv != KERN_SUCCESS) {
1327 * Lose the object reference. Will destroy the
1328 * object if it's an unnamed anonymous mapping
1329 * or named anonymous without other references.
1331 vm_object_deallocate(object);
1332 } else if (flags & MAP_SHARED) {
1334 * Shared memory is also shared with children.
1336 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1337 if (rv != KERN_SUCCESS)
1338 (void) vm_map_remove(map, *addr, *addr + size);
1342 * If the process has requested that all future mappings
1343 * be wired, then heed this.
1345 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1346 vm_map_wire(map, *addr, *addr + size,
1347 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1352 case KERN_INVALID_ADDRESS:
1355 case KERN_PROTECTION_FAILURE: