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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
36 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
40 * Mapped file (mmap) interface to VM
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_compat.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/filedesc.h>
57 #include <sys/resource.h>
58 #include <sys/resourcevar.h>
59 #include <sys/vnode.h>
60 #include <sys/fcntl.h>
64 #include <sys/mount.h>
67 #include <sys/vmmeter.h>
68 #include <sys/sysctl.h>
71 #include <vm/vm_param.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_pager.h>
77 #include <vm/vm_pageout.h>
78 #include <vm/vm_extern.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_kern.h>
82 #ifndef _SYS_SYSPROTO_H_
88 static int max_proc_mmap;
89 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
92 * Set the maximum number of vm_map_entry structures per process. Roughly
93 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
94 * of our KVM malloc space still results in generous limits. We want a
95 * default that is good enough to prevent the kernel running out of resources
96 * if attacked from compromised user account but generous enough such that
97 * multi-threaded processes are not unduly inconvenienced.
99 static void vmmapentry_rsrc_init(void *);
100 SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL)
103 vmmapentry_rsrc_init(dummy)
106 max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
107 max_proc_mmap /= 100;
110 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
111 int *, struct vnode *, vm_ooffset_t, vm_object_t *);
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)
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 */
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;
209 vm_size_t size, pageoff;
210 vm_prot_t prot, maxprot;
214 struct vmspace *vms = td->td_proc->p_vmspace;
216 addr = (vm_offset_t) uap->addr;
218 prot = uap->prot & VM_PROT_ALL;
223 /* make sure mapping fits into numeric range etc */
224 if ((ssize_t) uap->len < 0 ||
225 ((flags & MAP_ANON) && uap->fd != -1))
228 if (flags & MAP_STACK) {
229 if ((uap->fd != -1) ||
230 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
237 * Align the file position to a page boundary,
238 * and save its page offset component.
240 pageoff = (pos & PAGE_MASK);
243 /* Adjust size for rounding (on both ends). */
244 size += pageoff; /* low end... */
245 size = (vm_size_t) round_page(size); /* hi end */
248 * Check for illegal addresses. Watch out for address wrap... Note
249 * that VM_*_ADDRESS are not constants due to casts (argh).
251 if (flags & MAP_FIXED) {
253 * The specified address must have the same remainder
254 * as the file offset taken modulo PAGE_SIZE, so it
255 * should be aligned after adjustment by pageoff.
258 if (addr & PAGE_MASK)
260 /* Address range must be all in user VM space. */
261 if (addr < vm_map_min(&vms->vm_map) ||
262 addr + size > vm_map_max(&vms->vm_map))
264 if (addr + size < addr)
268 * XXX for non-fixed mappings where no hint is provided or
269 * the hint would fall in the potential heap space,
270 * place it after the end of the largest possible heap.
272 * There should really be a pmap call to determine a reasonable
275 PROC_LOCK(td->td_proc);
277 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
278 addr < round_page((vm_offset_t)vms->vm_daddr +
279 lim_max(td->td_proc, RLIMIT_DATA))))
280 addr = round_page((vm_offset_t)vms->vm_daddr +
281 lim_max(td->td_proc, RLIMIT_DATA));
282 PROC_UNLOCK(td->td_proc);
284 if (flags & MAP_ANON) {
286 * Mapping blank space is trivial.
289 maxprot = VM_PROT_ALL;
293 * Mapping file, get fp for validation. Obtain vnode and make
294 * sure it is of appropriate type.
295 * don't let the descriptor disappear on us if we block
297 if ((error = fget(td, uap->fd, &fp)) != 0)
299 if (fp->f_type != DTYPE_VNODE) {
304 * POSIX shared-memory objects are defined to have
305 * kernel persistence, and are not defined to support
306 * read(2)/write(2) -- or even open(2). Thus, we can
307 * use MAP_ASYNC to trade on-disk coherence for speed.
308 * The shm_open(3) library routine turns on the FPOSIXSHM
309 * flag to request this behavior.
311 if (fp->f_flag & FPOSIXSHM)
315 * Ensure that file and memory protections are
316 * compatible. Note that we only worry about
317 * writability if mapping is shared; in this case,
318 * current and max prot are dictated by the open file.
319 * XXX use the vnode instead? Problem is: what
320 * credentials do we use for determination? What if
321 * proc does a setuid?
323 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
324 maxprot = VM_PROT_NONE;
326 maxprot = VM_PROT_EXECUTE;
327 if (fp->f_flag & FREAD) {
328 maxprot |= VM_PROT_READ;
329 } else if (prot & PROT_READ) {
334 * If we are sharing potential changes (either via
335 * MAP_SHARED or via the implicit sharing of character
336 * device mappings), and we are trying to get write
337 * permission although we opened it without asking
340 if ((flags & MAP_SHARED) != 0) {
341 if ((fp->f_flag & FWRITE) != 0) {
342 maxprot |= VM_PROT_WRITE;
343 } else if ((prot & PROT_WRITE) != 0) {
347 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
348 maxprot |= VM_PROT_WRITE;
354 * Do not allow more then a certain number of vm_map_entry structures
355 * per process. Scale with the number of rforks sharing the map
356 * to make the limit reasonable for threads.
359 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
364 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
367 td->td_retval[0] = (register_t) (addr + pageoff);
376 #ifndef _SYS_SYSPROTO_H_
389 struct ommap_args *uap;
391 struct mmap_args nargs;
392 static const char cvtbsdprot[8] = {
396 PROT_EXEC | PROT_WRITE,
398 PROT_EXEC | PROT_READ,
399 PROT_WRITE | PROT_READ,
400 PROT_EXEC | PROT_WRITE | PROT_READ,
403 #define OMAP_ANON 0x0002
404 #define OMAP_COPY 0x0020
405 #define OMAP_SHARED 0x0010
406 #define OMAP_FIXED 0x0100
408 nargs.addr = uap->addr;
409 nargs.len = uap->len;
410 nargs.prot = cvtbsdprot[uap->prot & 0x7];
412 if (uap->flags & OMAP_ANON)
413 nargs.flags |= MAP_ANON;
414 if (uap->flags & OMAP_COPY)
415 nargs.flags |= MAP_COPY;
416 if (uap->flags & OMAP_SHARED)
417 nargs.flags |= MAP_SHARED;
419 nargs.flags |= MAP_PRIVATE;
420 if (uap->flags & OMAP_FIXED)
421 nargs.flags |= MAP_FIXED;
423 nargs.pos = uap->pos;
424 return (mmap(td, &nargs));
426 #endif /* COMPAT_43 */
429 #ifndef _SYS_SYSPROTO_H_
442 struct msync_args *uap;
445 vm_size_t size, pageoff;
450 addr = (vm_offset_t) uap->addr;
454 pageoff = (addr & PAGE_MASK);
457 size = (vm_size_t) round_page(size);
458 if (addr + size < addr)
461 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
464 map = &td->td_proc->p_vmspace->vm_map;
467 * Clean the pages and interpret the return value.
469 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
470 (flags & MS_INVALIDATE) != 0);
474 case KERN_INVALID_ADDRESS:
475 return (EINVAL); /* Sun returns ENOMEM? */
476 case KERN_INVALID_ARGUMENT:
483 #ifndef _SYS_SYSPROTO_H_
495 struct munmap_args *uap;
498 vm_size_t size, pageoff;
501 addr = (vm_offset_t) uap->addr;
506 pageoff = (addr & PAGE_MASK);
509 size = (vm_size_t) round_page(size);
510 if (addr + size < addr)
514 * Check for illegal addresses. Watch out for address wrap...
516 map = &td->td_proc->p_vmspace->vm_map;
517 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
521 * Make sure entire range is allocated.
523 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) {
527 /* returns nothing but KERN_SUCCESS anyway */
528 vm_map_delete(map, addr, addr + size);
533 #ifndef _SYS_SYSPROTO_H_
534 struct mprotect_args {
546 struct mprotect_args *uap;
549 vm_size_t size, pageoff;
552 addr = (vm_offset_t) uap->addr;
554 prot = uap->prot & VM_PROT_ALL;
555 #if defined(VM_PROT_READ_IS_EXEC)
556 if (prot & VM_PROT_READ)
557 prot |= VM_PROT_EXECUTE;
560 pageoff = (addr & PAGE_MASK);
563 size = (vm_size_t) round_page(size);
564 if (addr + size < addr)
567 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
568 addr + size, prot, FALSE)) {
571 case KERN_PROTECTION_FAILURE:
577 #ifndef _SYS_SYSPROTO_H_
578 struct minherit_args {
590 struct minherit_args *uap;
593 vm_size_t size, pageoff;
594 vm_inherit_t inherit;
596 addr = (vm_offset_t)uap->addr;
598 inherit = uap->inherit;
600 pageoff = (addr & PAGE_MASK);
603 size = (vm_size_t) round_page(size);
604 if (addr + size < addr)
607 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
608 addr + size, inherit)) {
611 case KERN_PROTECTION_FAILURE:
617 #ifndef _SYS_SYSPROTO_H_
618 struct madvise_args {
632 struct madvise_args *uap;
634 vm_offset_t start, end;
640 * Check for our special case, advising the swap pager we are
643 if (uap->behav == MADV_PROTECT) {
648 p->p_flag |= P_PROTECTED;
654 * Check for illegal behavior
656 if (uap->behav < 0 || uap->behav > MADV_CORE)
659 * Check for illegal addresses. Watch out for address wrap... Note
660 * that VM_*_ADDRESS are not constants due to casts (argh).
662 map = &td->td_proc->p_vmspace->vm_map;
663 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
664 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
666 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
670 * Since this routine is only advisory, we default to conservative
673 start = trunc_page((vm_offset_t) uap->addr);
674 end = round_page((vm_offset_t) uap->addr + uap->len);
676 if (vm_map_madvise(map, start, end, uap->behav))
681 #ifndef _SYS_SYSPROTO_H_
682 struct mincore_args {
696 struct mincore_args *uap;
698 vm_offset_t addr, first_addr;
699 vm_offset_t end, cend;
704 int vecindex, lastvecindex;
705 vm_map_entry_t current;
706 vm_map_entry_t entry;
708 unsigned int timestamp;
711 * Make sure that the addresses presented are valid for user
714 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
715 end = addr + (vm_size_t)round_page(uap->len);
716 map = &td->td_proc->p_vmspace->vm_map;
717 if (end > vm_map_max(map) || end < addr)
721 * Address of byte vector
725 pmap = vmspace_pmap(td->td_proc->p_vmspace);
727 vm_map_lock_read(map);
729 timestamp = map->timestamp;
731 if (!vm_map_lookup_entry(map, addr, &entry))
735 * Do this on a map entry basis so that if the pages are not
736 * in the current processes address space, we can easily look
737 * up the pages elsewhere.
740 for (current = entry;
741 (current != &map->header) && (current->start < end);
742 current = current->next) {
745 * ignore submaps (for now) or null objects
747 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
748 current->object.vm_object == NULL)
752 * limit this scan to the current map entry and the
753 * limits for the mincore call
755 if (addr < current->start)
756 addr = current->start;
762 * scan this entry one page at a time
764 while (addr < cend) {
766 * Check pmap first, it is likely faster, also
767 * it can provide info as to whether we are the
768 * one referencing or modifying the page.
771 mincoreinfo = pmap_mincore(pmap, addr);
778 * calculate the page index into the object
780 offset = current->offset + (addr - current->start);
781 pindex = OFF_TO_IDX(offset);
782 VM_OBJECT_LOCK(current->object.vm_object);
783 m = vm_page_lookup(current->object.vm_object,
786 * if the page is resident, then gather information about
789 if (m != NULL && m->valid != 0) {
790 mincoreinfo = MINCORE_INCORE;
791 vm_page_lock_queues();
794 mincoreinfo |= MINCORE_MODIFIED_OTHER;
795 if ((m->flags & PG_REFERENCED) ||
796 pmap_ts_referenced(m)) {
797 vm_page_flag_set(m, PG_REFERENCED);
798 mincoreinfo |= MINCORE_REFERENCED_OTHER;
800 vm_page_unlock_queues();
802 VM_OBJECT_UNLOCK(current->object.vm_object);
806 * subyte may page fault. In case it needs to modify
807 * the map, we release the lock.
809 vm_map_unlock_read(map);
812 * calculate index into user supplied byte vector
814 vecindex = OFF_TO_IDX(addr - first_addr);
817 * If we have skipped map entries, we need to make sure that
818 * the byte vector is zeroed for those skipped entries.
820 while ((lastvecindex + 1) < vecindex) {
821 error = subyte(vec + lastvecindex, 0);
830 * Pass the page information to the user
832 error = subyte(vec + vecindex, mincoreinfo);
839 * If the map has changed, due to the subyte, the previous
840 * output may be invalid.
842 vm_map_lock_read(map);
843 if (timestamp != map->timestamp)
846 lastvecindex = vecindex;
852 * subyte may page fault. In case it needs to modify
853 * the map, we release the lock.
855 vm_map_unlock_read(map);
858 * Zero the last entries in the byte vector.
860 vecindex = OFF_TO_IDX(end - first_addr);
861 while ((lastvecindex + 1) < vecindex) {
862 error = subyte(vec + lastvecindex, 0);
871 * If the map has changed, due to the subyte, the previous
872 * output may be invalid.
874 vm_map_lock_read(map);
875 if (timestamp != map->timestamp)
877 vm_map_unlock_read(map);
882 #ifndef _SYS_SYSPROTO_H_
894 struct mlock_args *uap;
897 vm_offset_t addr, end, last, start;
898 vm_size_t npages, size;
904 addr = (vm_offset_t)uap->addr;
907 start = trunc_page(addr);
908 end = round_page(last);
909 if (last < addr || end < addr)
911 npages = atop(end - start);
912 if (npages > vm_page_max_wired)
917 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
918 lim_cur(proc, RLIMIT_MEMLOCK)) {
923 if (npages + cnt.v_wire_count > vm_page_max_wired)
925 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
926 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
927 return (error == KERN_SUCCESS ? 0 : ENOMEM);
930 #ifndef _SYS_SYSPROTO_H_
931 struct mlockall_args {
942 struct mlockall_args *uap;
947 map = &td->td_proc->p_vmspace->vm_map;
950 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
955 * If wiring all pages in the process would cause it to exceed
956 * a hard resource limit, return ENOMEM.
958 PROC_LOCK(td->td_proc);
959 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
960 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
961 PROC_UNLOCK(td->td_proc);
964 PROC_UNLOCK(td->td_proc);
971 if (uap->how & MCL_FUTURE) {
973 vm_map_modflags(map, MAP_WIREFUTURE, 0);
978 if (uap->how & MCL_CURRENT) {
980 * P1003.1-2001 mandates that all currently mapped pages
981 * will be memory resident and locked (wired) upon return
982 * from mlockall(). vm_map_wire() will wire pages, by
983 * calling vm_fault_wire() for each page in the region.
985 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
986 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
987 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
993 #ifndef _SYS_SYSPROTO_H_
994 struct munlockall_args {
1005 struct munlockall_args *uap;
1010 map = &td->td_proc->p_vmspace->vm_map;
1015 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1017 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1020 /* Forcibly unwire all pages. */
1021 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1022 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1027 #ifndef _SYS_SYSPROTO_H_
1028 struct munlock_args {
1039 struct munlock_args *uap;
1041 vm_offset_t addr, end, last, start;
1048 addr = (vm_offset_t)uap->addr;
1051 start = trunc_page(addr);
1052 end = round_page(last);
1053 if (last < addr || end < addr)
1055 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1056 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1057 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1065 * Helper function for vm_mmap. Perform sanity check specific for mmap
1066 * operations on vnodes.
1069 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1070 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1071 struct vnode *vp, vm_ooffset_t foff, vm_object_t *objp)
1076 int disablexworkaround, error, flags, type;
1079 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
1084 if (vp->v_type == VREG) {
1086 * Get the proper underlying object
1088 if (VOP_GETVOBJECT(vp, &obj) != 0) {
1092 if (obj->handle != vp) {
1094 vp = (struct vnode*)obj->handle;
1095 vget(vp, LK_EXCLUSIVE, td);
1099 } else if (vp->v_type == VCHR) {
1101 handle = vp->v_rdev;
1103 if(vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON) {
1104 *maxprotp = VM_PROT_ALL;
1105 *flagsp |= MAP_ANON;
1110 * cdevs does not provide private mappings of any kind.
1112 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1113 (prot & PROT_WRITE) != 0) {
1118 * However, for XIG X server to continue to work,
1119 * we should allow the superuser to do it anyway.
1120 * We only allow it at securelevel < 1.
1121 * (Because the XIG X server writes directly to video
1122 * memory via /dev/mem, it should never work at any
1123 * other securelevel.
1124 * XXX this will have to go
1126 if (securelevel_ge(td->td_ucred, 1))
1127 disablexworkaround = 1;
1129 disablexworkaround = suser(td);
1130 if (disablexworkaround && (flags & (MAP_PRIVATE|MAP_COPY))) {
1135 * Force device mappings to be shared.
1137 flags &= ~(MAP_PRIVATE|MAP_COPY);
1138 flags |= MAP_SHARED;
1143 if ((error = VOP_GETATTR(vp, &va, td->td_ucred, td))) {
1146 if ((flags & MAP_SHARED) != 0) {
1147 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1148 if (prot & PROT_WRITE) {
1152 *maxprotp &= ~VM_PROT_WRITE;
1155 error = mac_check_vnode_mmap(td->td_ucred, vp, prot);
1161 * If it is a regular file without any references
1162 * we do not need to sync it.
1163 * Adjust object size to be the size of actual file.
1165 if (vp->v_type == VREG) {
1166 objsize = round_page(va.va_size);
1167 if (va.va_nlink == 0)
1168 flags |= MAP_NOSYNC;
1170 obj = vm_pager_allocate(type, handle, objsize, prot, foff);
1172 error = (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1188 * Internal version of mmap. Currently used by mmap, exec, and sys5
1189 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1192 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1193 vm_prot_t maxprot, int flags,
1199 int rv = KERN_SUCCESS;
1200 vm_ooffset_t objsize;
1202 struct thread *td = curthread;
1207 objsize = size = round_page(size);
1209 PROC_LOCK(td->td_proc);
1210 if (td->td_proc->p_vmspace->vm_map.size + size >
1211 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1212 PROC_UNLOCK(td->td_proc);
1215 PROC_UNLOCK(td->td_proc);
1218 * We currently can only deal with page aligned file offsets.
1219 * The check is here rather than in the syscall because the
1220 * kernel calls this function internally for other mmaping
1221 * operations (such as in exec) and non-aligned offsets will
1222 * cause pmap inconsistencies...so we want to be sure to
1223 * disallow this in all cases.
1225 if (foff & PAGE_MASK)
1228 if ((flags & MAP_FIXED) == 0) {
1230 *addr = round_page(*addr);
1232 if (*addr != trunc_page(*addr))
1235 (void) vm_map_remove(map, *addr, *addr + size);
1238 * Lookup/allocate object.
1240 if (handle != NULL) {
1241 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1242 handle, foff, &object);
1247 if (flags & MAP_ANON) {
1251 * Unnamed anonymous regions always start at 0.
1256 docow = MAP_PREFAULT_PARTIAL;
1259 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1260 docow |= MAP_COPY_ON_WRITE;
1261 if (flags & MAP_NOSYNC)
1262 docow |= MAP_DISABLE_SYNCER;
1263 if (flags & MAP_NOCORE)
1264 docow |= MAP_DISABLE_COREDUMP;
1266 #if defined(VM_PROT_READ_IS_EXEC)
1267 if (prot & VM_PROT_READ)
1268 prot |= VM_PROT_EXECUTE;
1270 if (maxprot & VM_PROT_READ)
1271 maxprot |= VM_PROT_EXECUTE;
1275 *addr = pmap_addr_hint(object, *addr, size);
1277 if (flags & MAP_STACK)
1278 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1279 docow | MAP_STACK_GROWS_DOWN);
1281 rv = vm_map_find(map, object, foff, addr, size, fitit,
1282 prot, maxprot, docow);
1284 if (rv != KERN_SUCCESS) {
1286 * Lose the object reference. Will destroy the
1287 * object if it's an unnamed anonymous mapping
1288 * or named anonymous without other references.
1290 vm_object_deallocate(object);
1291 } else if (flags & MAP_SHARED) {
1293 * Shared memory is also shared with children.
1295 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1296 if (rv != KERN_SUCCESS)
1297 (void) vm_map_remove(map, *addr, *addr + size);
1301 * If the process has requested that all future mappings
1302 * be wired, then heed this.
1304 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1305 vm_map_wire(map, *addr, *addr + size,
1306 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1311 case KERN_INVALID_ADDRESS:
1314 case KERN_PROTECTION_FAILURE: