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 *);
112 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
113 int *, struct cdev *, vm_ooffset_t, vm_object_t *);
122 struct sbrk_args *uap;
124 /* Not yet implemented */
128 #ifndef _SYS_SYSPROTO_H_
141 struct sstk_args *uap;
143 /* Not yet implemented */
147 #if defined(COMPAT_43)
148 #ifndef _SYS_SYSPROTO_H_
149 struct getpagesize_args {
156 ogetpagesize(td, uap)
158 struct getpagesize_args *uap;
161 td->td_retval[0] = PAGE_SIZE;
164 #endif /* COMPAT_43 */
168 * Memory Map (mmap) system call. Note that the file offset
169 * and address are allowed to be NOT page aligned, though if
170 * the MAP_FIXED flag it set, both must have the same remainder
171 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
172 * page-aligned, the actual mapping starts at trunc_page(addr)
173 * and the return value is adjusted up by the page offset.
175 * Generally speaking, only character devices which are themselves
176 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
177 * there would be no cache coherency between a descriptor and a VM mapping
178 * both to the same character device.
180 * Block devices can be mmap'd no matter what they represent. Cache coherency
181 * is maintained as long as you do not write directly to the underlying
184 #ifndef _SYS_SYSPROTO_H_
202 struct mmap_args *uap;
207 vm_size_t size, pageoff;
208 vm_prot_t prot, maxprot;
210 objtype_t handle_type;
213 struct vmspace *vms = td->td_proc->p_vmspace;
215 addr = (vm_offset_t) uap->addr;
217 prot = uap->prot & VM_PROT_ALL;
222 /* make sure mapping fits into numeric range etc */
223 if ((ssize_t) uap->len < 0 ||
224 ((flags & MAP_ANON) && uap->fd != -1))
227 if (flags & MAP_STACK) {
228 if ((uap->fd != -1) ||
229 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
236 * Align the file position to a page boundary,
237 * and save its page offset component.
239 pageoff = (pos & PAGE_MASK);
242 /* Adjust size for rounding (on both ends). */
243 size += pageoff; /* low end... */
244 size = (vm_size_t) round_page(size); /* hi end */
247 * Check for illegal addresses. Watch out for address wrap... Note
248 * that VM_*_ADDRESS are not constants due to casts (argh).
250 if (flags & MAP_FIXED) {
252 * The specified address must have the same remainder
253 * as the file offset taken modulo PAGE_SIZE, so it
254 * should be aligned after adjustment by pageoff.
257 if (addr & PAGE_MASK)
259 /* Address range must be all in user VM space. */
260 if (addr < vm_map_min(&vms->vm_map) ||
261 addr + size > vm_map_max(&vms->vm_map))
263 if (addr + size < addr)
267 * XXX for non-fixed mappings where no hint is provided or
268 * the hint would fall in the potential heap space,
269 * place it after the end of the largest possible heap.
271 * There should really be a pmap call to determine a reasonable
274 PROC_LOCK(td->td_proc);
276 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
277 addr < round_page((vm_offset_t)vms->vm_daddr +
278 lim_max(td->td_proc, RLIMIT_DATA))))
279 addr = round_page((vm_offset_t)vms->vm_daddr +
280 lim_max(td->td_proc, RLIMIT_DATA));
281 PROC_UNLOCK(td->td_proc);
283 if (flags & MAP_ANON) {
285 * Mapping blank space is trivial.
288 handle_type = OBJT_DEFAULT;
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;
351 handle_type = OBJT_VNODE;
355 * Do not allow more then a certain number of vm_map_entry structures
356 * per process. Scale with the number of rforks sharing the map
357 * to make the limit reasonable for threads.
360 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
365 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
366 flags, handle_type, handle, pos);
368 td->td_retval[0] = (register_t) (addr + pageoff);
377 #ifndef _SYS_SYSPROTO_H_
390 struct ommap_args *uap;
392 struct mmap_args nargs;
393 static const char cvtbsdprot[8] = {
397 PROT_EXEC | PROT_WRITE,
399 PROT_EXEC | PROT_READ,
400 PROT_WRITE | PROT_READ,
401 PROT_EXEC | PROT_WRITE | PROT_READ,
404 #define OMAP_ANON 0x0002
405 #define OMAP_COPY 0x0020
406 #define OMAP_SHARED 0x0010
407 #define OMAP_FIXED 0x0100
409 nargs.addr = uap->addr;
410 nargs.len = uap->len;
411 nargs.prot = cvtbsdprot[uap->prot & 0x7];
413 if (uap->flags & OMAP_ANON)
414 nargs.flags |= MAP_ANON;
415 if (uap->flags & OMAP_COPY)
416 nargs.flags |= MAP_COPY;
417 if (uap->flags & OMAP_SHARED)
418 nargs.flags |= MAP_SHARED;
420 nargs.flags |= MAP_PRIVATE;
421 if (uap->flags & OMAP_FIXED)
422 nargs.flags |= MAP_FIXED;
424 nargs.pos = uap->pos;
425 return (mmap(td, &nargs));
427 #endif /* COMPAT_43 */
430 #ifndef _SYS_SYSPROTO_H_
443 struct msync_args *uap;
446 vm_size_t size, pageoff;
451 addr = (vm_offset_t) uap->addr;
455 pageoff = (addr & PAGE_MASK);
458 size = (vm_size_t) round_page(size);
459 if (addr + size < addr)
462 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
465 map = &td->td_proc->p_vmspace->vm_map;
468 * Clean the pages and interpret the return value.
470 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
471 (flags & MS_INVALIDATE) != 0);
475 case KERN_INVALID_ADDRESS:
476 return (EINVAL); /* Sun returns ENOMEM? */
477 case KERN_INVALID_ARGUMENT:
484 #ifndef _SYS_SYSPROTO_H_
496 struct munmap_args *uap;
499 vm_size_t size, pageoff;
502 addr = (vm_offset_t) uap->addr;
507 pageoff = (addr & PAGE_MASK);
510 size = (vm_size_t) round_page(size);
511 if (addr + size < addr)
515 * Check for illegal addresses. Watch out for address wrap...
517 map = &td->td_proc->p_vmspace->vm_map;
518 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
522 * Make sure entire range is allocated.
524 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) {
528 /* returns nothing but KERN_SUCCESS anyway */
529 vm_map_delete(map, addr, addr + size);
534 #ifndef _SYS_SYSPROTO_H_
535 struct mprotect_args {
547 struct mprotect_args *uap;
550 vm_size_t size, pageoff;
553 addr = (vm_offset_t) uap->addr;
555 prot = uap->prot & VM_PROT_ALL;
556 #if defined(VM_PROT_READ_IS_EXEC)
557 if (prot & VM_PROT_READ)
558 prot |= VM_PROT_EXECUTE;
561 pageoff = (addr & PAGE_MASK);
564 size = (vm_size_t) round_page(size);
565 if (addr + size < addr)
568 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
569 addr + size, prot, FALSE)) {
572 case KERN_PROTECTION_FAILURE:
578 #ifndef _SYS_SYSPROTO_H_
579 struct minherit_args {
591 struct minherit_args *uap;
594 vm_size_t size, pageoff;
595 vm_inherit_t inherit;
597 addr = (vm_offset_t)uap->addr;
599 inherit = uap->inherit;
601 pageoff = (addr & PAGE_MASK);
604 size = (vm_size_t) round_page(size);
605 if (addr + size < addr)
608 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
609 addr + size, inherit)) {
612 case KERN_PROTECTION_FAILURE:
618 #ifndef _SYS_SYSPROTO_H_
619 struct madvise_args {
633 struct madvise_args *uap;
635 vm_offset_t start, end;
641 * Check for our special case, advising the swap pager we are
644 if (uap->behav == MADV_PROTECT) {
649 p->p_flag |= P_PROTECTED;
655 * Check for illegal behavior
657 if (uap->behav < 0 || uap->behav > MADV_CORE)
660 * Check for illegal addresses. Watch out for address wrap... Note
661 * that VM_*_ADDRESS are not constants due to casts (argh).
663 map = &td->td_proc->p_vmspace->vm_map;
664 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
665 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
667 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
671 * Since this routine is only advisory, we default to conservative
674 start = trunc_page((vm_offset_t) uap->addr);
675 end = round_page((vm_offset_t) uap->addr + uap->len);
677 if (vm_map_madvise(map, start, end, uap->behav))
682 #ifndef _SYS_SYSPROTO_H_
683 struct mincore_args {
697 struct mincore_args *uap;
699 vm_offset_t addr, first_addr;
700 vm_offset_t end, cend;
705 int vecindex, lastvecindex;
706 vm_map_entry_t current;
707 vm_map_entry_t entry;
709 unsigned int timestamp;
712 * Make sure that the addresses presented are valid for user
715 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
716 end = addr + (vm_size_t)round_page(uap->len);
717 map = &td->td_proc->p_vmspace->vm_map;
718 if (end > vm_map_max(map) || end < addr)
722 * Address of byte vector
726 pmap = vmspace_pmap(td->td_proc->p_vmspace);
728 vm_map_lock_read(map);
730 timestamp = map->timestamp;
732 if (!vm_map_lookup_entry(map, addr, &entry))
736 * Do this on a map entry basis so that if the pages are not
737 * in the current processes address space, we can easily look
738 * up the pages elsewhere.
741 for (current = entry;
742 (current != &map->header) && (current->start < end);
743 current = current->next) {
746 * ignore submaps (for now) or null objects
748 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
749 current->object.vm_object == NULL)
753 * limit this scan to the current map entry and the
754 * limits for the mincore call
756 if (addr < current->start)
757 addr = current->start;
763 * scan this entry one page at a time
765 while (addr < cend) {
767 * Check pmap first, it is likely faster, also
768 * it can provide info as to whether we are the
769 * one referencing or modifying the page.
771 mincoreinfo = pmap_mincore(pmap, addr);
777 * calculate the page index into the object
779 offset = current->offset + (addr - current->start);
780 pindex = OFF_TO_IDX(offset);
781 VM_OBJECT_LOCK(current->object.vm_object);
782 m = vm_page_lookup(current->object.vm_object,
785 * if the page is resident, then gather information about
788 if (m != NULL && m->valid != 0) {
789 mincoreinfo = MINCORE_INCORE;
790 vm_page_lock_queues();
793 mincoreinfo |= MINCORE_MODIFIED_OTHER;
794 if ((m->flags & PG_REFERENCED) ||
795 pmap_ts_referenced(m)) {
796 vm_page_flag_set(m, PG_REFERENCED);
797 mincoreinfo |= MINCORE_REFERENCED_OTHER;
799 vm_page_unlock_queues();
801 VM_OBJECT_UNLOCK(current->object.vm_object);
805 * subyte may page fault. In case it needs to modify
806 * the map, we release the lock.
808 vm_map_unlock_read(map);
811 * calculate index into user supplied byte vector
813 vecindex = OFF_TO_IDX(addr - first_addr);
816 * If we have skipped map entries, we need to make sure that
817 * the byte vector is zeroed for those skipped entries.
819 while ((lastvecindex + 1) < vecindex) {
820 error = subyte(vec + lastvecindex, 0);
829 * Pass the page information to the user
831 error = subyte(vec + vecindex, mincoreinfo);
838 * If the map has changed, due to the subyte, the previous
839 * output may be invalid.
841 vm_map_lock_read(map);
842 if (timestamp != map->timestamp)
845 lastvecindex = vecindex;
851 * subyte may page fault. In case it needs to modify
852 * the map, we release the lock.
854 vm_map_unlock_read(map);
857 * Zero the last entries in the byte vector.
859 vecindex = OFF_TO_IDX(end - first_addr);
860 while ((lastvecindex + 1) < vecindex) {
861 error = subyte(vec + lastvecindex, 0);
870 * If the map has changed, due to the subyte, the previous
871 * output may be invalid.
873 vm_map_lock_read(map);
874 if (timestamp != map->timestamp)
876 vm_map_unlock_read(map);
881 #ifndef _SYS_SYSPROTO_H_
893 struct mlock_args *uap;
896 vm_offset_t addr, end, last, start;
897 vm_size_t npages, size;
903 addr = (vm_offset_t)uap->addr;
906 start = trunc_page(addr);
907 end = round_page(last);
908 if (last < addr || end < addr)
910 npages = atop(end - start);
911 if (npages > vm_page_max_wired)
916 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
917 lim_cur(proc, RLIMIT_MEMLOCK)) {
922 if (npages + cnt.v_wire_count > vm_page_max_wired)
924 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
925 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
926 return (error == KERN_SUCCESS ? 0 : ENOMEM);
929 #ifndef _SYS_SYSPROTO_H_
930 struct mlockall_args {
941 struct mlockall_args *uap;
946 map = &td->td_proc->p_vmspace->vm_map;
949 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
954 * If wiring all pages in the process would cause it to exceed
955 * a hard resource limit, return ENOMEM.
957 PROC_LOCK(td->td_proc);
958 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
959 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
960 PROC_UNLOCK(td->td_proc);
963 PROC_UNLOCK(td->td_proc);
970 if (uap->how & MCL_FUTURE) {
972 vm_map_modflags(map, MAP_WIREFUTURE, 0);
977 if (uap->how & MCL_CURRENT) {
979 * P1003.1-2001 mandates that all currently mapped pages
980 * will be memory resident and locked (wired) upon return
981 * from mlockall(). vm_map_wire() will wire pages, by
982 * calling vm_fault_wire() for each page in the region.
984 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
985 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
986 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
992 #ifndef _SYS_SYSPROTO_H_
993 struct munlockall_args {
1004 struct munlockall_args *uap;
1009 map = &td->td_proc->p_vmspace->vm_map;
1014 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1016 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1019 /* Forcibly unwire all pages. */
1020 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1021 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1026 #ifndef _SYS_SYSPROTO_H_
1027 struct munlock_args {
1038 struct munlock_args *uap;
1040 vm_offset_t addr, end, last, start;
1047 addr = (vm_offset_t)uap->addr;
1050 start = trunc_page(addr);
1051 end = round_page(last);
1052 if (last < addr || end < addr)
1054 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1055 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1056 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1064 * Helper function for vm_mmap. Perform sanity check specific for mmap
1065 * operations on vnodes.
1068 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1069 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1070 struct vnode *vp, vm_ooffset_t foff, vm_object_t *objp)
1076 int error, flags, type;
1080 vfslocked = VFS_LOCK_GIANT(mp);
1081 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
1082 VFS_UNLOCK_GIANT(vfslocked);
1087 if (vp->v_type == VREG) {
1089 * Get the proper underlying object
1095 if (obj->handle != vp) {
1097 vp = (struct vnode*)obj->handle;
1098 vget(vp, LK_EXCLUSIVE, td);
1102 } else if (vp->v_type == VCHR) {
1104 handle = vp->v_rdev;
1106 /* XXX: lack thredref on device */
1107 if(vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON) {
1108 *maxprotp = VM_PROT_ALL;
1109 *flagsp |= MAP_ANON;
1114 * cdevs does not provide private mappings of any kind.
1116 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1117 (prot & PROT_WRITE) != 0) {
1121 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1126 * Force device mappings to be shared.
1128 flags |= MAP_SHARED;
1133 if ((error = VOP_GETATTR(vp, &va, td->td_ucred, td))) {
1137 error = mac_check_vnode_mmap(td->td_ucred, vp, prot, flags);
1141 if ((flags & MAP_SHARED) != 0) {
1142 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1143 if (prot & PROT_WRITE) {
1147 *maxprotp &= ~VM_PROT_WRITE;
1151 * If it is a regular file without any references
1152 * we do not need to sync it.
1153 * Adjust object size to be the size of actual file.
1155 if (vp->v_type == VREG) {
1156 objsize = round_page(va.va_size);
1157 if (va.va_nlink == 0)
1158 flags |= MAP_NOSYNC;
1160 obj = vm_pager_allocate(type, handle, objsize, prot, foff);
1162 error = (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1169 VFS_UNLOCK_GIANT(vfslocked);
1178 * Helper function for vm_mmap. Perform sanity check specific for mmap
1179 * operations on cdevs.
1182 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1183 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1184 struct cdev *cdev, vm_ooffset_t foff, vm_object_t *objp)
1191 /* XXX: lack thredref on device */
1192 if (cdev->si_devsw->d_flags & D_MMAP_ANON) {
1193 *maxprotp = VM_PROT_ALL;
1194 *flagsp |= MAP_ANON;
1198 * cdevs does not provide private mappings of any kind.
1200 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1201 (prot & PROT_WRITE) != 0)
1203 if (flags & (MAP_PRIVATE|MAP_COPY))
1206 * Force device mappings to be shared.
1208 flags |= MAP_SHARED;
1210 error = mac_check_cdev_mmap(td->td_ucred, cdev, prot);
1214 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, foff);
1227 * Internal version of mmap. Currently used by mmap, exec, and sys5
1228 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1231 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1232 vm_prot_t maxprot, int flags,
1233 objtype_t handle_type, void *handle,
1238 int rv = KERN_SUCCESS;
1239 vm_ooffset_t objsize;
1241 struct thread *td = curthread;
1246 objsize = size = round_page(size);
1248 PROC_LOCK(td->td_proc);
1249 if (td->td_proc->p_vmspace->vm_map.size + size >
1250 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1251 PROC_UNLOCK(td->td_proc);
1254 PROC_UNLOCK(td->td_proc);
1257 * We currently can only deal with page aligned file offsets.
1258 * The check is here rather than in the syscall because the
1259 * kernel calls this function internally for other mmaping
1260 * operations (such as in exec) and non-aligned offsets will
1261 * cause pmap inconsistencies...so we want to be sure to
1262 * disallow this in all cases.
1264 if (foff & PAGE_MASK)
1267 if ((flags & MAP_FIXED) == 0) {
1269 *addr = round_page(*addr);
1271 if (*addr != trunc_page(*addr))
1274 (void) vm_map_remove(map, *addr, *addr + size);
1277 * Lookup/allocate object.
1279 switch (handle_type) {
1281 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1282 handle, foff, &object);
1285 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1286 handle, foff, &object);
1289 if (handle == NULL) {
1299 if (flags & MAP_ANON) {
1303 * Unnamed anonymous regions always start at 0.
1308 docow = MAP_PREFAULT_PARTIAL;
1311 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1312 docow |= MAP_COPY_ON_WRITE;
1313 if (flags & MAP_NOSYNC)
1314 docow |= MAP_DISABLE_SYNCER;
1315 if (flags & MAP_NOCORE)
1316 docow |= MAP_DISABLE_COREDUMP;
1318 #if defined(VM_PROT_READ_IS_EXEC)
1319 if (prot & VM_PROT_READ)
1320 prot |= VM_PROT_EXECUTE;
1322 if (maxprot & VM_PROT_READ)
1323 maxprot |= VM_PROT_EXECUTE;
1327 *addr = pmap_addr_hint(object, *addr, size);
1329 if (flags & MAP_STACK)
1330 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1331 docow | MAP_STACK_GROWS_DOWN);
1333 rv = vm_map_find(map, object, foff, addr, size, fitit,
1334 prot, maxprot, docow);
1336 if (rv != KERN_SUCCESS) {
1338 * Lose the object reference. Will destroy the
1339 * object if it's an unnamed anonymous mapping
1340 * or named anonymous without other references.
1342 vm_object_deallocate(object);
1343 } else if (flags & MAP_SHARED) {
1345 * Shared memory is also shared with children.
1347 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1348 if (rv != KERN_SUCCESS)
1349 (void) vm_map_remove(map, *addr, *addr + size);
1353 * If the process has requested that all future mappings
1354 * be wired, then heed this.
1356 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1357 vm_map_wire(map, *addr, *addr + size,
1358 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1363 case KERN_INVALID_ADDRESS:
1366 case KERN_PROTECTION_FAILURE: