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 */
126 #ifndef _SYS_SYSPROTO_H_
139 struct sstk_args *uap;
141 /* Not yet implemented */
145 #if defined(COMPAT_43)
146 #ifndef _SYS_SYSPROTO_H_
147 struct getpagesize_args {
154 ogetpagesize(td, uap)
156 struct getpagesize_args *uap;
159 td->td_retval[0] = PAGE_SIZE;
162 #endif /* COMPAT_43 */
166 * Memory Map (mmap) system call. Note that the file offset
167 * and address are allowed to be NOT page aligned, though if
168 * the MAP_FIXED flag it set, both must have the same remainder
169 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
170 * page-aligned, the actual mapping starts at trunc_page(addr)
171 * and the return value is adjusted up by the page offset.
173 * Generally speaking, only character devices which are themselves
174 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
175 * there would be no cache coherency between a descriptor and a VM mapping
176 * both to the same character device.
178 * Block devices can be mmap'd no matter what they represent. Cache coherency
179 * is maintained as long as you do not write directly to the underlying
182 #ifndef _SYS_SYSPROTO_H_
200 struct mmap_args *uap;
205 vm_size_t size, pageoff;
206 vm_prot_t prot, maxprot;
210 struct vmspace *vms = td->td_proc->p_vmspace;
212 addr = (vm_offset_t) uap->addr;
214 prot = uap->prot & VM_PROT_ALL;
219 /* make sure mapping fits into numeric range etc */
220 if ((ssize_t) uap->len < 0 ||
221 ((flags & MAP_ANON) && uap->fd != -1))
224 if (flags & MAP_STACK) {
225 if ((uap->fd != -1) ||
226 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
233 * Align the file position to a page boundary,
234 * and save its page offset component.
236 pageoff = (pos & PAGE_MASK);
239 /* Adjust size for rounding (on both ends). */
240 size += pageoff; /* low end... */
241 size = (vm_size_t) round_page(size); /* hi end */
244 * Check for illegal addresses. Watch out for address wrap... Note
245 * that VM_*_ADDRESS are not constants due to casts (argh).
247 if (flags & MAP_FIXED) {
249 * The specified address must have the same remainder
250 * as the file offset taken modulo PAGE_SIZE, so it
251 * should be aligned after adjustment by pageoff.
254 if (addr & PAGE_MASK)
256 /* Address range must be all in user VM space. */
257 if (addr < vm_map_min(&vms->vm_map) ||
258 addr + size > vm_map_max(&vms->vm_map))
260 if (addr + size < addr)
264 * XXX for non-fixed mappings where no hint is provided or
265 * the hint would fall in the potential heap space,
266 * place it after the end of the largest possible heap.
268 * There should really be a pmap call to determine a reasonable
271 PROC_LOCK(td->td_proc);
273 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
274 addr < round_page((vm_offset_t)vms->vm_daddr +
275 lim_max(td->td_proc, RLIMIT_DATA))))
276 addr = round_page((vm_offset_t)vms->vm_daddr +
277 lim_max(td->td_proc, RLIMIT_DATA));
278 PROC_UNLOCK(td->td_proc);
280 if (flags & MAP_ANON) {
282 * Mapping blank space is trivial.
285 maxprot = VM_PROT_ALL;
289 * Mapping file, get fp for validation. Obtain vnode and make
290 * sure it is of appropriate type.
291 * don't let the descriptor disappear on us if we block
293 if ((error = fget(td, uap->fd, &fp)) != 0)
295 if (fp->f_type != DTYPE_VNODE) {
300 * POSIX shared-memory objects are defined to have
301 * kernel persistence, and are not defined to support
302 * read(2)/write(2) -- or even open(2). Thus, we can
303 * use MAP_ASYNC to trade on-disk coherence for speed.
304 * The shm_open(3) library routine turns on the FPOSIXSHM
305 * flag to request this behavior.
307 if (fp->f_flag & FPOSIXSHM)
311 * Ensure that file and memory protections are
312 * compatible. Note that we only worry about
313 * writability if mapping is shared; in this case,
314 * current and max prot are dictated by the open file.
315 * XXX use the vnode instead? Problem is: what
316 * credentials do we use for determination? What if
317 * proc does a setuid?
319 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
320 maxprot = VM_PROT_NONE;
322 maxprot = VM_PROT_EXECUTE;
323 if (fp->f_flag & FREAD) {
324 maxprot |= VM_PROT_READ;
325 } else if (prot & PROT_READ) {
330 * If we are sharing potential changes (either via
331 * MAP_SHARED or via the implicit sharing of character
332 * device mappings), and we are trying to get write
333 * permission although we opened it without asking
336 if ((flags & MAP_SHARED) != 0) {
337 if ((fp->f_flag & FWRITE) != 0) {
338 maxprot |= VM_PROT_WRITE;
339 } else if ((prot & PROT_WRITE) != 0) {
343 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
344 maxprot |= VM_PROT_WRITE;
350 * Do not allow more then a certain number of vm_map_entry structures
351 * per process. Scale with the number of rforks sharing the map
352 * to make the limit reasonable for threads.
355 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
360 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
363 td->td_retval[0] = (register_t) (addr + pageoff);
372 #ifndef _SYS_SYSPROTO_H_
385 struct ommap_args *uap;
387 struct mmap_args nargs;
388 static const char cvtbsdprot[8] = {
392 PROT_EXEC | PROT_WRITE,
394 PROT_EXEC | PROT_READ,
395 PROT_WRITE | PROT_READ,
396 PROT_EXEC | PROT_WRITE | PROT_READ,
399 #define OMAP_ANON 0x0002
400 #define OMAP_COPY 0x0020
401 #define OMAP_SHARED 0x0010
402 #define OMAP_FIXED 0x0100
404 nargs.addr = uap->addr;
405 nargs.len = uap->len;
406 nargs.prot = cvtbsdprot[uap->prot & 0x7];
408 if (uap->flags & OMAP_ANON)
409 nargs.flags |= MAP_ANON;
410 if (uap->flags & OMAP_COPY)
411 nargs.flags |= MAP_COPY;
412 if (uap->flags & OMAP_SHARED)
413 nargs.flags |= MAP_SHARED;
415 nargs.flags |= MAP_PRIVATE;
416 if (uap->flags & OMAP_FIXED)
417 nargs.flags |= MAP_FIXED;
419 nargs.pos = uap->pos;
420 return (mmap(td, &nargs));
422 #endif /* COMPAT_43 */
425 #ifndef _SYS_SYSPROTO_H_
438 struct msync_args *uap;
441 vm_size_t size, pageoff;
446 addr = (vm_offset_t) uap->addr;
450 pageoff = (addr & PAGE_MASK);
453 size = (vm_size_t) round_page(size);
454 if (addr + size < addr)
457 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
460 map = &td->td_proc->p_vmspace->vm_map;
463 * Clean the pages and interpret the return value.
465 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
466 (flags & MS_INVALIDATE) != 0);
470 case KERN_INVALID_ADDRESS:
471 return (EINVAL); /* Sun returns ENOMEM? */
472 case KERN_INVALID_ARGUMENT:
479 #ifndef _SYS_SYSPROTO_H_
491 struct munmap_args *uap;
494 vm_size_t size, pageoff;
497 addr = (vm_offset_t) uap->addr;
502 pageoff = (addr & PAGE_MASK);
505 size = (vm_size_t) round_page(size);
506 if (addr + size < addr)
510 * Check for illegal addresses. Watch out for address wrap...
512 map = &td->td_proc->p_vmspace->vm_map;
513 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
517 * Make sure entire range is allocated.
519 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) {
523 /* returns nothing but KERN_SUCCESS anyway */
524 vm_map_delete(map, addr, addr + size);
529 #ifndef _SYS_SYSPROTO_H_
530 struct mprotect_args {
542 struct mprotect_args *uap;
545 vm_size_t size, pageoff;
548 addr = (vm_offset_t) uap->addr;
550 prot = uap->prot & VM_PROT_ALL;
551 #if defined(VM_PROT_READ_IS_EXEC)
552 if (prot & VM_PROT_READ)
553 prot |= VM_PROT_EXECUTE;
556 pageoff = (addr & PAGE_MASK);
559 size = (vm_size_t) round_page(size);
560 if (addr + size < addr)
563 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
564 addr + size, prot, FALSE)) {
567 case KERN_PROTECTION_FAILURE:
573 #ifndef _SYS_SYSPROTO_H_
574 struct minherit_args {
586 struct minherit_args *uap;
589 vm_size_t size, pageoff;
590 vm_inherit_t inherit;
592 addr = (vm_offset_t)uap->addr;
594 inherit = uap->inherit;
596 pageoff = (addr & PAGE_MASK);
599 size = (vm_size_t) round_page(size);
600 if (addr + size < addr)
603 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
604 addr + size, inherit)) {
607 case KERN_PROTECTION_FAILURE:
613 #ifndef _SYS_SYSPROTO_H_
614 struct madvise_args {
628 struct madvise_args *uap;
630 vm_offset_t start, end;
636 * Check for our special case, advising the swap pager we are
639 if (uap->behav == MADV_PROTECT) {
644 p->p_flag |= P_PROTECTED;
650 * Check for illegal behavior
652 if (uap->behav < 0 || uap->behav > MADV_CORE)
655 * Check for illegal addresses. Watch out for address wrap... Note
656 * that VM_*_ADDRESS are not constants due to casts (argh).
658 map = &td->td_proc->p_vmspace->vm_map;
659 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
660 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
662 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
666 * Since this routine is only advisory, we default to conservative
669 start = trunc_page((vm_offset_t) uap->addr);
670 end = round_page((vm_offset_t) uap->addr + uap->len);
672 if (vm_map_madvise(map, start, end, uap->behav))
677 #ifndef _SYS_SYSPROTO_H_
678 struct mincore_args {
692 struct mincore_args *uap;
694 vm_offset_t addr, first_addr;
695 vm_offset_t end, cend;
700 int vecindex, lastvecindex;
701 vm_map_entry_t current;
702 vm_map_entry_t entry;
704 unsigned int timestamp;
707 * Make sure that the addresses presented are valid for user
710 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
711 end = addr + (vm_size_t)round_page(uap->len);
712 map = &td->td_proc->p_vmspace->vm_map;
713 if (end > vm_map_max(map) || end < addr)
717 * Address of byte vector
721 pmap = vmspace_pmap(td->td_proc->p_vmspace);
723 vm_map_lock_read(map);
725 timestamp = map->timestamp;
727 if (!vm_map_lookup_entry(map, addr, &entry))
731 * Do this on a map entry basis so that if the pages are not
732 * in the current processes address space, we can easily look
733 * up the pages elsewhere.
736 for (current = entry;
737 (current != &map->header) && (current->start < end);
738 current = current->next) {
741 * ignore submaps (for now) or null objects
743 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
744 current->object.vm_object == NULL)
748 * limit this scan to the current map entry and the
749 * limits for the mincore call
751 if (addr < current->start)
752 addr = current->start;
758 * scan this entry one page at a time
760 while (addr < cend) {
762 * Check pmap first, it is likely faster, also
763 * it can provide info as to whether we are the
764 * one referencing or modifying the page.
766 mincoreinfo = pmap_mincore(pmap, addr);
772 * calculate the page index into the object
774 offset = current->offset + (addr - current->start);
775 pindex = OFF_TO_IDX(offset);
776 VM_OBJECT_LOCK(current->object.vm_object);
777 m = vm_page_lookup(current->object.vm_object,
780 * if the page is resident, then gather information about
783 if (m != NULL && m->valid != 0) {
784 mincoreinfo = MINCORE_INCORE;
785 vm_page_lock_queues();
788 mincoreinfo |= MINCORE_MODIFIED_OTHER;
789 if ((m->flags & PG_REFERENCED) ||
790 pmap_ts_referenced(m)) {
791 vm_page_flag_set(m, PG_REFERENCED);
792 mincoreinfo |= MINCORE_REFERENCED_OTHER;
794 vm_page_unlock_queues();
796 VM_OBJECT_UNLOCK(current->object.vm_object);
800 * subyte may page fault. In case it needs to modify
801 * the map, we release the lock.
803 vm_map_unlock_read(map);
806 * calculate index into user supplied byte vector
808 vecindex = OFF_TO_IDX(addr - first_addr);
811 * If we have skipped map entries, we need to make sure that
812 * the byte vector is zeroed for those skipped entries.
814 while ((lastvecindex + 1) < vecindex) {
815 error = subyte(vec + lastvecindex, 0);
824 * Pass the page information to the user
826 error = subyte(vec + vecindex, mincoreinfo);
833 * If the map has changed, due to the subyte, the previous
834 * output may be invalid.
836 vm_map_lock_read(map);
837 if (timestamp != map->timestamp)
840 lastvecindex = vecindex;
846 * subyte may page fault. In case it needs to modify
847 * the map, we release the lock.
849 vm_map_unlock_read(map);
852 * Zero the last entries in the byte vector.
854 vecindex = OFF_TO_IDX(end - first_addr);
855 while ((lastvecindex + 1) < vecindex) {
856 error = subyte(vec + lastvecindex, 0);
865 * If the map has changed, due to the subyte, the previous
866 * output may be invalid.
868 vm_map_lock_read(map);
869 if (timestamp != map->timestamp)
871 vm_map_unlock_read(map);
876 #ifndef _SYS_SYSPROTO_H_
888 struct mlock_args *uap;
891 vm_offset_t addr, end, last, start;
892 vm_size_t npages, size;
898 addr = (vm_offset_t)uap->addr;
901 start = trunc_page(addr);
902 end = round_page(last);
903 if (last < addr || end < addr)
905 npages = atop(end - start);
906 if (npages > vm_page_max_wired)
911 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
912 lim_cur(proc, RLIMIT_MEMLOCK)) {
917 if (npages + cnt.v_wire_count > vm_page_max_wired)
919 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
920 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
921 return (error == KERN_SUCCESS ? 0 : ENOMEM);
924 #ifndef _SYS_SYSPROTO_H_
925 struct mlockall_args {
936 struct mlockall_args *uap;
941 map = &td->td_proc->p_vmspace->vm_map;
944 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
949 * If wiring all pages in the process would cause it to exceed
950 * a hard resource limit, return ENOMEM.
952 PROC_LOCK(td->td_proc);
953 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
954 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
955 PROC_UNLOCK(td->td_proc);
958 PROC_UNLOCK(td->td_proc);
965 if (uap->how & MCL_FUTURE) {
967 vm_map_modflags(map, MAP_WIREFUTURE, 0);
972 if (uap->how & MCL_CURRENT) {
974 * P1003.1-2001 mandates that all currently mapped pages
975 * will be memory resident and locked (wired) upon return
976 * from mlockall(). vm_map_wire() will wire pages, by
977 * calling vm_fault_wire() for each page in the region.
979 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
980 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
981 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
987 #ifndef _SYS_SYSPROTO_H_
988 struct munlockall_args {
999 struct munlockall_args *uap;
1004 map = &td->td_proc->p_vmspace->vm_map;
1009 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1011 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1014 /* Forcibly unwire all pages. */
1015 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1016 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1021 #ifndef _SYS_SYSPROTO_H_
1022 struct munlock_args {
1033 struct munlock_args *uap;
1035 vm_offset_t addr, end, last, start;
1042 addr = (vm_offset_t)uap->addr;
1045 start = trunc_page(addr);
1046 end = round_page(last);
1047 if (last < addr || end < addr)
1049 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1050 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1051 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1059 * Helper function for vm_mmap. Perform sanity check specific for mmap
1060 * operations on vnodes.
1063 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1064 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1065 struct vnode *vp, vm_ooffset_t foff, vm_object_t *objp)
1071 int error, flags, type;
1075 vfslocked = VFS_LOCK_GIANT(mp);
1076 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
1077 VFS_UNLOCK_GIANT(vfslocked);
1082 if (vp->v_type == VREG) {
1084 * Get the proper underlying object
1090 if (obj->handle != vp) {
1092 vp = (struct vnode*)obj->handle;
1093 vget(vp, LK_EXCLUSIVE, td);
1097 } else if (vp->v_type == VCHR) {
1099 handle = vp->v_rdev;
1101 /* XXX: lack thredref on device */
1102 if(vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON) {
1103 *maxprotp = VM_PROT_ALL;
1104 *flagsp |= MAP_ANON;
1109 * cdevs does not provide private mappings of any kind.
1111 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1112 (prot & PROT_WRITE) != 0) {
1116 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1121 * Force device mappings to be shared.
1123 flags |= MAP_SHARED;
1128 if ((error = VOP_GETATTR(vp, &va, td->td_ucred, td))) {
1131 if ((flags & MAP_SHARED) != 0) {
1132 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1133 if (prot & PROT_WRITE) {
1137 *maxprotp &= ~VM_PROT_WRITE;
1140 error = mac_check_vnode_mmap(td->td_ucred, vp, prot);
1146 * If it is a regular file without any references
1147 * we do not need to sync it.
1148 * Adjust object size to be the size of actual file.
1150 if (vp->v_type == VREG) {
1151 objsize = round_page(va.va_size);
1152 if (va.va_nlink == 0)
1153 flags |= MAP_NOSYNC;
1155 obj = vm_pager_allocate(type, handle, objsize, prot, foff);
1157 error = (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1164 VFS_UNLOCK_GIANT(vfslocked);
1173 * Internal version of mmap. Currently used by mmap, exec, and sys5
1174 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1177 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1178 vm_prot_t maxprot, int flags,
1184 int rv = KERN_SUCCESS;
1185 vm_ooffset_t objsize;
1187 struct thread *td = curthread;
1192 objsize = size = round_page(size);
1194 PROC_LOCK(td->td_proc);
1195 if (td->td_proc->p_vmspace->vm_map.size + size >
1196 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1197 PROC_UNLOCK(td->td_proc);
1200 PROC_UNLOCK(td->td_proc);
1203 * We currently can only deal with page aligned file offsets.
1204 * The check is here rather than in the syscall because the
1205 * kernel calls this function internally for other mmaping
1206 * operations (such as in exec) and non-aligned offsets will
1207 * cause pmap inconsistencies...so we want to be sure to
1208 * disallow this in all cases.
1210 if (foff & PAGE_MASK)
1213 if ((flags & MAP_FIXED) == 0) {
1215 *addr = round_page(*addr);
1217 if (*addr != trunc_page(*addr))
1220 (void) vm_map_remove(map, *addr, *addr + size);
1223 * Lookup/allocate object.
1225 if (handle != NULL) {
1226 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1227 handle, foff, &object);
1232 if (flags & MAP_ANON) {
1236 * Unnamed anonymous regions always start at 0.
1241 docow = MAP_PREFAULT_PARTIAL;
1244 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1245 docow |= MAP_COPY_ON_WRITE;
1246 if (flags & MAP_NOSYNC)
1247 docow |= MAP_DISABLE_SYNCER;
1248 if (flags & MAP_NOCORE)
1249 docow |= MAP_DISABLE_COREDUMP;
1251 #if defined(VM_PROT_READ_IS_EXEC)
1252 if (prot & VM_PROT_READ)
1253 prot |= VM_PROT_EXECUTE;
1255 if (maxprot & VM_PROT_READ)
1256 maxprot |= VM_PROT_EXECUTE;
1260 *addr = pmap_addr_hint(object, *addr, size);
1262 if (flags & MAP_STACK)
1263 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1264 docow | MAP_STACK_GROWS_DOWN);
1266 rv = vm_map_find(map, object, foff, addr, size, fitit,
1267 prot, maxprot, docow);
1269 if (rv != KERN_SUCCESS) {
1271 * Lose the object reference. Will destroy the
1272 * object if it's an unnamed anonymous mapping
1273 * or named anonymous without other references.
1275 vm_object_deallocate(object);
1276 } else if (flags & MAP_SHARED) {
1278 * Shared memory is also shared with children.
1280 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1281 if (rv != KERN_SUCCESS)
1282 (void) vm_map_remove(map, *addr, *addr + size);
1286 * If the process has requested that all future mappings
1287 * be wired, then heed this.
1289 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1290 vm_map_wire(map, *addr, *addr + size,
1291 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1296 case KERN_INVALID_ADDRESS:
1299 case KERN_PROTECTION_FAILURE: