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
41 * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $
42 * $DragonFly: src/sys/vm/vm_mmap.c,v 1.39 2007/04/30 07:18:57 dillon Exp $
46 * Mapped file (mmap) interface to VM
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/sysproto.h>
53 #include <sys/filedesc.h>
54 #include <sys/kern_syscall.h>
57 #include <sys/resource.h>
58 #include <sys/resourcevar.h>
59 #include <sys/vnode.h>
60 #include <sys/fcntl.h>
65 #include <sys/vmmeter.h>
66 #include <sys/sysctl.h>
69 #include <vm/vm_param.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_pager.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_kern.h>
81 #include <sys/file2.h>
82 #include <sys/thread2.h>
83 #include <sys/mplock2.h>
85 static int max_proc_mmap;
86 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
88 SYSCTL_INT(_vm, OID_AUTO, vkernel_enable, CTLFLAG_RW, &vkernel_enable, 0, "");
91 * Set the maximum number of vm_map_entry structures per process. Roughly
92 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
93 * of our KVM malloc space still results in generous limits. We want a
94 * default that is good enough to prevent the kernel running out of resources
95 * if attacked from compromised user account but generous enough such that
96 * multi-threaded processes are not unduly inconvenienced.
99 static void vmmapentry_rsrc_init (void *);
100 SYSINIT(vmmersrc, SI_BOOT1_POST, SI_ORDER_ANY, vmmapentry_rsrc_init, NULL)
103 vmmapentry_rsrc_init(void *dummy)
105 max_proc_mmap = KvaSize / sizeof(struct vm_map_entry);
106 max_proc_mmap /= 100;
113 sys_sbrk(struct sbrk_args *uap)
115 /* Not yet implemented */
120 * sstk_args(int incr)
125 sys_sstk(struct sstk_args *uap)
127 /* Not yet implemented */
132 * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
133 * long pad, off_t pos)
135 * Memory Map (mmap) system call. Note that the file offset
136 * and address are allowed to be NOT page aligned, though if
137 * the MAP_FIXED flag it set, both must have the same remainder
138 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
139 * page-aligned, the actual mapping starts at trunc_page(addr)
140 * and the return value is adjusted up by the page offset.
142 * Generally speaking, only character devices which are themselves
143 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
144 * there would be no cache coherency between a descriptor and a VM mapping
145 * both to the same character device.
147 * Block devices can be mmap'd no matter what they represent. Cache coherency
148 * is maintained as long as you do not write directly to the underlying
153 kern_mmap(struct vmspace *vms, caddr_t uaddr, size_t ulen,
154 int uprot, int uflags, int fd, off_t upos, void **res)
156 struct thread *td = curthread;
157 struct proc *p = td->td_proc;
158 struct file *fp = NULL;
162 vm_size_t size, pageoff;
163 vm_prot_t prot, maxprot;
166 int disablexworkaround;
172 addr = (vm_offset_t) uaddr;
174 prot = uprot & VM_PROT_ALL;
179 * Make sure mapping fits into numeric range etc.
181 * NOTE: We support the full unsigned range for size now.
183 if (((flags & MAP_ANON) && (fd != -1 || pos != 0)))
186 if (flags & MAP_STACK) {
188 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
195 * Virtual page tables cannot be used with MAP_STACK. Apart from
196 * it not making any sense, the aux union is used by both
199 * Because the virtual page table is stored in the backing object
200 * and might be updated by the kernel, the mapping must be R+W.
202 if (flags & MAP_VPAGETABLE) {
203 if (vkernel_enable == 0)
205 if (flags & MAP_STACK)
207 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
212 * Align the file position to a page boundary,
213 * and save its page offset component.
215 pageoff = (pos & PAGE_MASK);
218 /* Adjust size for rounding (on both ends). */
219 size += pageoff; /* low end... */
220 size = (vm_size_t) round_page(size); /* hi end */
221 if (size < ulen) /* wrap */
225 * Check for illegal addresses. Watch out for address wrap... Note
226 * that VM_*_ADDRESS are not constants due to casts (argh).
228 if (flags & (MAP_FIXED | MAP_TRYFIXED)) {
230 * The specified address must have the same remainder
231 * as the file offset taken modulo PAGE_SIZE, so it
232 * should be aligned after adjustment by pageoff.
235 if (addr & PAGE_MASK)
239 * Address range must be all in user VM space and not wrap.
241 tmpaddr = addr + size;
244 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
246 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
250 * Set a reasonable start point for the hint if it was
251 * not specified or if it falls within the heap space.
252 * Hinted mmap()s do not allocate out of the heap space.
255 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
256 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz)))
257 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
260 if (flags & MAP_ANON) {
262 * Mapping blank space is trivial.
265 maxprot = VM_PROT_ALL;
268 * Mapping file, get fp for validation. Obtain vnode and make
269 * sure it is of appropriate type.
271 fp = holdfp(p->p_fd, fd, -1);
274 if (fp->f_type != DTYPE_VNODE) {
279 * POSIX shared-memory objects are defined to have
280 * kernel persistence, and are not defined to support
281 * read(2)/write(2) -- or even open(2). Thus, we can
282 * use MAP_ASYNC to trade on-disk coherence for speed.
283 * The shm_open(3) library routine turns on the FPOSIXSHM
284 * flag to request this behavior.
286 if (fp->f_flag & FPOSIXSHM)
288 vp = (struct vnode *) fp->f_data;
291 * Validate the vnode for the operation.
296 * Get the proper underlying object
298 if ((obj = vp->v_object) == NULL) {
302 KKASSERT((struct vnode *)obj->handle == vp);
306 * Make sure a device has not been revoked.
307 * Mappability is handled by the device layer.
309 if (vp->v_rdev == NULL) {
316 * Nothing else is mappable.
323 * XXX hack to handle use of /dev/zero to map anon memory (ala
326 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
328 maxprot = VM_PROT_ALL;
333 * cdevs does not provide private mappings of any kind.
336 * However, for XIG X server to continue to work,
337 * we should allow the superuser to do it anyway.
338 * We only allow it at securelevel < 1.
339 * (Because the XIG X server writes directly to video
340 * memory via /dev/mem, it should never work at any
342 * XXX this will have to go
344 if (securelevel >= 1)
345 disablexworkaround = 1;
347 disablexworkaround = priv_check(td, PRIV_ROOT);
348 if (vp->v_type == VCHR && disablexworkaround &&
349 (flags & (MAP_PRIVATE|MAP_COPY))) {
354 * Ensure that file and memory protections are
355 * compatible. Note that we only worry about
356 * writability if mapping is shared; in this case,
357 * current and max prot are dictated by the open file.
358 * XXX use the vnode instead? Problem is: what
359 * credentials do we use for determination? What if
360 * proc does a setuid?
362 maxprot = VM_PROT_EXECUTE; /* ??? */
363 if (fp->f_flag & FREAD) {
364 maxprot |= VM_PROT_READ;
365 } else if (prot & PROT_READ) {
370 * If we are sharing potential changes (either via
371 * MAP_SHARED or via the implicit sharing of character
372 * device mappings), and we are trying to get write
373 * permission although we opened it without asking
374 * for it, bail out. Check for superuser, only if
375 * we're at securelevel < 1, to allow the XIG X server
376 * to continue to work.
379 if ((flags & MAP_SHARED) != 0 ||
380 (vp->v_type == VCHR && disablexworkaround)) {
381 if ((fp->f_flag & FWRITE) != 0) {
383 if ((error = VOP_GETATTR(vp, &va))) {
387 (IMMUTABLE|APPEND)) == 0) {
388 maxprot |= VM_PROT_WRITE;
389 } else if (prot & PROT_WRITE) {
393 } else if ((prot & PROT_WRITE) != 0) {
398 maxprot |= VM_PROT_WRITE;
405 * Do not allow more then a certain number of vm_map_entry structures
406 * per process. Scale with the number of rforks sharing the map
407 * to make the limit reasonable for threads.
410 vms->vm_map.nentries >= max_proc_mmap * vms->vm_sysref.refcnt) {
415 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
418 *res = (void *)(addr + pageoff);
429 sys_mmap(struct mmap_args *uap)
434 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len,
435 uap->prot, uap->flags,
436 uap->fd, uap->pos, &uap->sysmsg_resultp);
443 * msync_args(void *addr, size_t len, int flags)
448 sys_msync(struct msync_args *uap)
450 struct proc *p = curproc;
453 vm_size_t size, pageoff;
458 addr = (vm_offset_t) uap->addr;
462 pageoff = (addr & PAGE_MASK);
465 size = (vm_size_t) round_page(size);
466 if (size < uap->len) /* wrap */
468 tmpaddr = addr + size; /* workaround gcc4 opt */
469 if (tmpaddr < addr) /* wrap */
472 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
476 map = &p->p_vmspace->vm_map;
479 * XXX Gak! If size is zero we are supposed to sync "all modified
480 * pages with the region containing addr". Unfortunately, we don't
481 * really keep track of individual mmaps so we approximate by flushing
482 * the range of the map entry containing addr. This can be incorrect
483 * if the region splits or is coalesced with a neighbor.
486 vm_map_entry_t entry;
488 vm_map_lock_read(map);
489 rv = vm_map_lookup_entry(map, addr, &entry);
491 vm_map_unlock_read(map);
492 rv = KERN_INVALID_ADDRESS;
496 size = entry->end - entry->start;
497 vm_map_unlock_read(map);
501 * Clean the pages and interpret the return value.
503 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
504 (flags & MS_INVALIDATE) != 0);
511 case KERN_INVALID_ADDRESS:
512 return (EINVAL); /* Sun returns ENOMEM? */
523 * munmap_args(void *addr, size_t len)
528 sys_munmap(struct munmap_args *uap)
530 struct proc *p = curproc;
533 vm_size_t size, pageoff;
536 addr = (vm_offset_t) uap->addr;
539 pageoff = (addr & PAGE_MASK);
542 size = (vm_size_t) round_page(size);
543 if (size < uap->len) /* wrap */
545 tmpaddr = addr + size; /* workaround gcc4 opt */
546 if (tmpaddr < addr) /* wrap */
553 * Check for illegal addresses. Watch out for address wrap... Note
554 * that VM_*_ADDRESS are not constants due to casts (argh).
556 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
558 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
562 map = &p->p_vmspace->vm_map;
564 * Make sure entire range is allocated.
566 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) {
570 /* returns nothing but KERN_SUCCESS anyway */
571 vm_map_remove(map, addr, addr + size);
577 * mprotect_args(const void *addr, size_t len, int prot)
582 sys_mprotect(struct mprotect_args *uap)
584 struct proc *p = curproc;
587 vm_size_t size, pageoff;
591 addr = (vm_offset_t) uap->addr;
593 prot = uap->prot & VM_PROT_ALL;
594 #if defined(VM_PROT_READ_IS_EXEC)
595 if (prot & VM_PROT_READ)
596 prot |= VM_PROT_EXECUTE;
599 pageoff = (addr & PAGE_MASK);
602 size = (vm_size_t) round_page(size);
603 if (size < uap->len) /* wrap */
605 tmpaddr = addr + size; /* workaround gcc4 opt */
606 if (tmpaddr < addr) /* wrap */
610 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size,
615 case KERN_PROTECTION_FAILURE:
627 * minherit_args(void *addr, size_t len, int inherit)
632 sys_minherit(struct minherit_args *uap)
634 struct proc *p = curproc;
637 vm_size_t size, pageoff;
638 vm_inherit_t inherit;
641 addr = (vm_offset_t)uap->addr;
643 inherit = uap->inherit;
645 pageoff = (addr & PAGE_MASK);
648 size = (vm_size_t) round_page(size);
649 if (size < uap->len) /* wrap */
651 tmpaddr = addr + size; /* workaround gcc4 opt */
652 if (tmpaddr < addr) /* wrap */
657 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr,
658 addr + size, inherit)) {
662 case KERN_PROTECTION_FAILURE:
674 * madvise_args(void *addr, size_t len, int behav)
679 sys_madvise(struct madvise_args *uap)
681 struct proc *p = curproc;
682 vm_offset_t start, end;
683 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
687 * Check for illegal behavior
689 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END)
692 * Check for illegal addresses. Watch out for address wrap... Note
693 * that VM_*_ADDRESS are not constants due to casts (argh).
695 if (tmpaddr < (vm_offset_t)uap->addr)
697 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
699 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
703 * Since this routine is only advisory, we default to conservative
706 start = trunc_page((vm_offset_t)uap->addr);
707 end = round_page(tmpaddr);
710 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
717 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
722 sys_mcontrol(struct mcontrol_args *uap)
724 struct proc *p = curproc;
725 vm_offset_t start, end;
726 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
730 * Check for illegal behavior
732 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
735 * Check for illegal addresses. Watch out for address wrap... Note
736 * that VM_*_ADDRESS are not constants due to casts (argh).
738 if (tmpaddr < (vm_offset_t) uap->addr)
740 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
742 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
746 * Since this routine is only advisory, we default to conservative
749 start = trunc_page((vm_offset_t)uap->addr);
750 end = round_page(tmpaddr);
753 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
754 uap->behav, uap->value);
761 * mincore_args(const void *addr, size_t len, char *vec)
766 sys_mincore(struct mincore_args *uap)
768 struct proc *p = curproc;
769 vm_offset_t addr, first_addr;
770 vm_offset_t end, cend;
775 int vecindex, lastvecindex;
776 vm_map_entry_t current;
777 vm_map_entry_t entry;
779 unsigned int timestamp;
782 * Make sure that the addresses presented are valid for user
785 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
786 end = addr + (vm_size_t)round_page(uap->len);
789 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS)
793 * Address of byte vector
797 map = &p->p_vmspace->vm_map;
798 pmap = vmspace_pmap(p->p_vmspace);
801 vm_map_lock_read(map);
803 timestamp = map->timestamp;
805 if (!vm_map_lookup_entry(map, addr, &entry))
809 * Do this on a map entry basis so that if the pages are not
810 * in the current processes address space, we can easily look
811 * up the pages elsewhere.
815 (current != &map->header) && (current->start < end);
816 current = current->next) {
819 * ignore submaps (for now) or null objects
821 if (current->maptype != VM_MAPTYPE_NORMAL &&
822 current->maptype != VM_MAPTYPE_VPAGETABLE) {
825 if (current->object.vm_object == NULL)
829 * limit this scan to the current map entry and the
830 * limits for the mincore call
832 if (addr < current->start)
833 addr = current->start;
839 * scan this entry one page at a time
841 while (addr < cend) {
843 * Check pmap first, it is likely faster, also
844 * it can provide info as to whether we are the
845 * one referencing or modifying the page.
847 * If we have to check the VM object, only mess
848 * around with normal maps. Do not mess around
849 * with virtual page tables (XXX).
851 mincoreinfo = pmap_mincore(pmap, addr);
852 if (mincoreinfo == 0 &&
853 current->maptype == VM_MAPTYPE_NORMAL) {
859 * calculate the page index into the object
861 offset = current->offset + (addr - current->start);
862 pindex = OFF_TO_IDX(offset);
865 * if the page is resident, then gather
866 * information about it. spl protection is
867 * required to maintain the object
868 * association. And XXX what if the page is
869 * busy? What's the deal with that?
872 m = vm_page_lookup(current->object.vm_object,
875 mincoreinfo = MINCORE_INCORE;
878 mincoreinfo |= MINCORE_MODIFIED_OTHER;
879 if ((m->flags & PG_REFERENCED) ||
880 pmap_ts_referenced(m)) {
881 vm_page_flag_set(m, PG_REFERENCED);
882 mincoreinfo |= MINCORE_REFERENCED_OTHER;
889 * subyte may page fault. In case it needs to modify
890 * the map, we release the lock.
892 vm_map_unlock_read(map);
895 * calculate index into user supplied byte vector
897 vecindex = OFF_TO_IDX(addr - first_addr);
900 * If we have skipped map entries, we need to make sure that
901 * the byte vector is zeroed for those skipped entries.
903 while((lastvecindex + 1) < vecindex) {
904 error = subyte( vec + lastvecindex, 0);
913 * Pass the page information to the user
915 error = subyte( vec + vecindex, mincoreinfo);
922 * If the map has changed, due to the subyte, the previous
923 * output may be invalid.
925 vm_map_lock_read(map);
926 if (timestamp != map->timestamp)
929 lastvecindex = vecindex;
935 * subyte may page fault. In case it needs to modify
936 * the map, we release the lock.
938 vm_map_unlock_read(map);
941 * Zero the last entries in the byte vector.
943 vecindex = OFF_TO_IDX(end - first_addr);
944 while((lastvecindex + 1) < vecindex) {
945 error = subyte( vec + lastvecindex, 0);
954 * If the map has changed, due to the subyte, the previous
955 * output may be invalid.
957 vm_map_lock_read(map);
958 if (timestamp != map->timestamp)
960 vm_map_unlock_read(map);
969 * mlock_args(const void *addr, size_t len)
974 sys_mlock(struct mlock_args *uap)
978 vm_size_t size, pageoff;
979 struct thread *td = curthread;
980 struct proc *p = td->td_proc;
983 addr = (vm_offset_t) uap->addr;
986 pageoff = (addr & PAGE_MASK);
989 size = (vm_size_t) round_page(size);
990 if (size < uap->len) /* wrap */
992 tmpaddr = addr + size; /* workaround gcc4 opt */
993 if (tmpaddr < addr) /* wrap */
996 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
1000 #ifdef pmap_wired_count
1001 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
1002 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) {
1007 error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1013 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
1015 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1019 * mlockall_args(int how)
1021 * Dummy routine, doesn't actually do anything.
1026 sys_mlockall(struct mlockall_args *uap)
1032 * munlockall_args(void)
1034 * Dummy routine, doesn't actually do anything.
1039 sys_munlockall(struct munlockall_args *uap)
1045 * munlock_args(const void *addr, size_t len)
1050 sys_munlock(struct munlock_args *uap)
1052 struct thread *td = curthread;
1053 struct proc *p = td->td_proc;
1055 vm_offset_t tmpaddr;
1056 vm_size_t size, pageoff;
1059 addr = (vm_offset_t) uap->addr;
1062 pageoff = (addr & PAGE_MASK);
1065 size = (vm_size_t) round_page(size);
1067 tmpaddr = addr + size;
1068 if (tmpaddr < addr) /* wrap */
1071 #ifndef pmap_wired_count
1072 error = priv_check(td, PRIV_ROOT);
1078 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
1080 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1084 * Internal version of mmap.
1085 * Currently used by mmap, exec, and sys5 shared memory.
1086 * Handle is either a vnode pointer or NULL for MAP_ANON.
1089 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1090 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff)
1097 struct thread *td = curthread;
1099 int rv = KERN_SUCCESS;
1106 objsize = round_page(size);
1112 * XXX messy code, fixme
1114 * NOTE: Overflow checks require discrete statements or GCC4
1115 * will optimize it out.
1117 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) {
1118 esize = map->size + size; /* workaround gcc4 opt */
1119 if (esize < map->size ||
1120 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
1126 * We currently can only deal with page aligned file offsets.
1127 * The check is here rather than in the syscall because the
1128 * kernel calls this function internally for other mmaping
1129 * operations (such as in exec) and non-aligned offsets will
1130 * cause pmap inconsistencies...so we want to be sure to
1131 * disallow this in all cases.
1133 * NOTE: Overflow checks require discrete statements or GCC4
1134 * will optimize it out.
1136 if (foff & PAGE_MASK)
1139 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
1141 *addr = round_page(*addr);
1143 if (*addr != trunc_page(*addr))
1145 eaddr = *addr + size;
1149 if ((flags & MAP_TRYFIXED) == 0)
1150 vm_map_remove(map, *addr, *addr + size);
1154 * Lookup/allocate object.
1156 if (flags & MAP_ANON) {
1158 * Unnamed anonymous regions always start at 0.
1162 * Default memory object
1164 object = default_pager_alloc(handle, objsize,
1168 docow = MAP_PREFAULT_PARTIAL;
1171 * Implicit single instance of a default memory
1172 * object, so we don't need a VM object yet.
1180 vp = (struct vnode *)handle;
1181 if (vp->v_type == VCHR) {
1183 * Device mappings (device size unknown?).
1184 * Force them to be shared.
1186 handle = (void *)(intptr_t)vp->v_rdev;
1187 object = dev_pager_alloc(handle, objsize, prot, foff);
1190 docow = MAP_PREFAULT_PARTIAL;
1191 flags &= ~(MAP_PRIVATE|MAP_COPY);
1192 flags |= MAP_SHARED;
1195 * Regular file mapping (typically). The attribute
1196 * check is for the link count test only. Mmapble
1197 * vnodes must already have a VM object assigned.
1202 error = VOP_GETATTR(vp, &vat);
1205 docow = MAP_PREFAULT_PARTIAL;
1206 object = vnode_pager_reference(vp);
1207 if (object == NULL && vp->v_type == VREG) {
1208 kprintf("Warning: cannot mmap vnode %p, no "
1214 * If it is a regular file without any references
1215 * we do not need to sync it.
1217 if (vp->v_type == VREG && vat.va_nlink == 0) {
1218 flags |= MAP_NOSYNC;
1224 * Deal with the adjusted flags
1226 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1227 docow |= MAP_COPY_ON_WRITE;
1228 if (flags & MAP_NOSYNC)
1229 docow |= MAP_DISABLE_SYNCER;
1230 if (flags & MAP_NOCORE)
1231 docow |= MAP_DISABLE_COREDUMP;
1233 #if defined(VM_PROT_READ_IS_EXEC)
1234 if (prot & VM_PROT_READ)
1235 prot |= VM_PROT_EXECUTE;
1237 if (maxprot & VM_PROT_READ)
1238 maxprot |= VM_PROT_EXECUTE;
1242 * This may place the area in its own page directory if (size) is
1243 * large enough, otherwise it typically returns its argument.
1246 *addr = pmap_addr_hint(object, *addr, size);
1250 * Stack mappings need special attention.
1252 * Mappings that use virtual page tables will default to storing
1253 * the page table at offset 0.
1255 if (flags & MAP_STACK) {
1256 rv = vm_map_stack(map, *addr, size, flags,
1257 prot, maxprot, docow);
1258 } else if (flags & MAP_VPAGETABLE) {
1259 rv = vm_map_find(map, object, foff, addr, size, PAGE_SIZE,
1260 fitit, VM_MAPTYPE_VPAGETABLE,
1261 prot, maxprot, docow);
1263 rv = vm_map_find(map, object, foff, addr, size, PAGE_SIZE,
1264 fitit, VM_MAPTYPE_NORMAL,
1265 prot, maxprot, docow);
1268 if (rv != KERN_SUCCESS) {
1270 * Lose the object reference. Will destroy the
1271 * object if it's an unnamed anonymous mapping
1272 * or named anonymous without other references.
1274 vm_object_deallocate(object);
1279 * Shared memory is also shared with children.
1281 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1282 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1283 if (rv != KERN_SUCCESS) {
1284 vm_map_remove(map, *addr, *addr + size);
1290 * Set the access time on the vnode
1293 vn_mark_atime(vp, td);
1298 case KERN_INVALID_ADDRESS:
1301 case KERN_PROTECTION_FAILURE: