4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
38 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
39 * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $
43 * Mapped file (mmap) interface to VM
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/systm.h>
49 #include <sys/sysproto.h>
50 #include <sys/filedesc.h>
51 #include <sys/kern_syscall.h>
54 #include <sys/resource.h>
55 #include <sys/resourcevar.h>
56 #include <sys/vnode.h>
57 #include <sys/fcntl.h>
62 #include <sys/vmmeter.h>
63 #include <sys/sysctl.h>
66 #include <vm/vm_param.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_pager.h>
73 #include <vm/vm_pageout.h>
74 #include <vm/vm_extern.h>
75 #include <vm/vm_kern.h>
77 #include <sys/file2.h>
78 #include <sys/thread.h>
79 #include <vm/vm_page2.h>
81 static int max_proc_mmap = 1000000;
82 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
84 SYSCTL_INT(_vm, OID_AUTO, vkernel_enable, CTLFLAG_RW, &vkernel_enable, 0, "");
92 sys_sstk(struct sstk_args *uap)
94 /* Not yet implemented */
99 * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
100 * long pad, off_t pos)
102 * Memory Map (mmap) system call. Note that the file offset
103 * and address are allowed to be NOT page aligned, though if
104 * the MAP_FIXED flag it set, both must have the same remainder
105 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
106 * page-aligned, the actual mapping starts at trunc_page(addr)
107 * and the return value is adjusted up by the page offset.
109 * Generally speaking, only character devices which are themselves
110 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
111 * there would be no cache coherency between a descriptor and a VM mapping
112 * both to the same character device.
114 * Block devices can be mmap'd no matter what they represent. Cache coherency
115 * is maintained as long as you do not write directly to the underlying
121 kern_mmap(struct vmspace *vms, caddr_t uaddr, size_t ulen,
122 int uprot, int uflags, int fd, off_t upos, void **res)
124 struct thread *td = curthread;
125 struct proc *p = td->td_proc;
126 struct file *fp = NULL;
130 vm_size_t size, pageoff;
131 vm_prot_t prot, maxprot;
139 addr = (vm_offset_t) uaddr;
141 prot = uprot & VM_PROT_ALL;
146 * Make sure mapping fits into numeric range etc.
148 * NOTE: We support the full unsigned range for size now.
150 if (((flags & MAP_ANON) && (fd != -1 || pos != 0)))
156 if (flags & MAP_STACK) {
159 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
166 * Virtual page tables cannot be used with MAP_STACK. Apart from
167 * it not making any sense, the aux union is used by both
170 * Because the virtual page table is stored in the backing object
171 * and might be updated by the kernel, the mapping must be R+W.
173 if (flags & MAP_VPAGETABLE) {
174 if (vkernel_enable == 0)
176 if (flags & MAP_STACK)
178 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
183 * Align the file position to a page boundary,
184 * and save its page offset component.
186 pageoff = (pos & PAGE_MASK);
189 /* Adjust size for rounding (on both ends). */
190 size += pageoff; /* low end... */
191 size = (vm_size_t) round_page(size); /* hi end */
192 if (size < ulen) /* wrap */
196 * Check for illegal addresses. Watch out for address wrap... Note
197 * that VM_*_ADDRESS are not constants due to casts (argh).
199 if (flags & (MAP_FIXED | MAP_TRYFIXED)) {
201 * The specified address must have the same remainder
202 * as the file offset taken modulo PAGE_SIZE, so it
203 * should be aligned after adjustment by pageoff.
206 if (addr & PAGE_MASK)
210 * Address range must be all in user VM space and not wrap.
212 tmpaddr = addr + size;
215 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
217 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
221 * Get a hint of where to map. It also provides mmap offset
222 * randomization if enabled.
224 addr = vm_map_hint(p, addr, prot);
227 if (flags & MAP_ANON) {
229 * Mapping blank space is trivial.
232 maxprot = VM_PROT_ALL;
235 * Mapping file, get fp for validation. Obtain vnode and make
236 * sure it is of appropriate type.
238 fp = holdfp(td, fd, -1);
241 if (fp->f_type != DTYPE_VNODE) {
246 * POSIX shared-memory objects are defined to have
247 * kernel persistence, and are not defined to support
248 * read(2)/write(2) -- or even open(2). Thus, we can
249 * use MAP_ASYNC to trade on-disk coherence for speed.
250 * The shm_open(3) library routine turns on the FPOSIXSHM
251 * flag to request this behavior.
253 if (fp->f_flag & FPOSIXSHM)
255 vp = (struct vnode *) fp->f_data;
258 * Validate the vnode for the operation.
263 * Get the proper underlying object
265 if ((obj = vp->v_object) == NULL) {
269 KKASSERT((struct vnode *)obj->handle == vp);
273 * Make sure a device has not been revoked.
274 * Mappability is handled by the device layer.
276 if (vp->v_rdev == NULL) {
283 * Nothing else is mappable.
290 * XXX hack to handle use of /dev/zero to map anon memory (ala
293 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
295 maxprot = VM_PROT_ALL;
300 * cdevs does not provide private mappings of any kind.
302 if (vp->v_type == VCHR &&
303 (flags & (MAP_PRIVATE|MAP_COPY))) {
308 * Ensure that file and memory protections are
309 * compatible. Note that we only worry about
310 * writability if mapping is shared; in this case,
311 * current and max prot are dictated by the open file.
312 * XXX use the vnode instead? Problem is: what
313 * credentials do we use for determination? What if
314 * proc does a setuid?
316 maxprot = VM_PROT_EXECUTE;
317 if (fp->f_flag & FREAD) {
318 maxprot |= VM_PROT_READ;
319 } else if (prot & PROT_READ) {
324 * If we are sharing potential changes (either via
325 * MAP_SHARED or via the implicit sharing of character
326 * device mappings), and we are trying to get write
327 * permission although we opened it without asking
328 * for it, bail out. Check for superuser, only if
329 * we're at securelevel < 1, to allow the XIG X server
330 * to continue to work.
332 * PROT_WRITE + MAP_SHARED
334 if ((flags & MAP_SHARED) != 0 || vp->v_type == VCHR) {
335 if ((fp->f_flag & FWRITE) != 0) {
337 if ((error = VOP_GETATTR(vp, &va))) {
341 (IMMUTABLE|APPEND)) == 0) {
342 maxprot |= VM_PROT_WRITE;
345 * SHARED+RW file mmap()
346 * updates v_lastwrite_ts.
348 if ((prot & PROT_WRITE) &&
349 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY) == 0) {
350 vfs_timestamp(&vp->v_lastwrite_ts);
351 vsetflags(vp, VLASTWRITETS);
354 } else if (prot & PROT_WRITE) {
358 } else if ((prot & PROT_WRITE) != 0) {
363 maxprot |= VM_PROT_WRITE;
369 lwkt_gettoken(&vms->vm_map.token);
372 * Do not allow more then a certain number of vm_map_entry structures
373 * per process. 0 to disable.
375 if (max_proc_mmap && vms->vm_map.nentries >= max_proc_mmap) {
377 lwkt_reltoken(&vms->vm_map.token);
381 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
384 *res = (void *)(addr + pageoff);
386 lwkt_reltoken(&vms->vm_map.token);
395 * mmap system call handler
400 sys_mmap(struct mmap_args *uap)
404 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len,
405 uap->prot, uap->flags,
406 uap->fd, uap->pos, &uap->sysmsg_resultp);
412 * msync system call handler
414 * msync_args(void *addr, size_t len, int flags)
419 sys_msync(struct msync_args *uap)
421 struct proc *p = curproc;
424 vm_size_t size, pageoff;
429 addr = (vm_offset_t) uap->addr;
433 pageoff = (addr & PAGE_MASK);
436 size = (vm_size_t) round_page(size);
437 if (size < uap->len) /* wrap */
439 tmpaddr = addr + size; /* workaround gcc4 opt */
440 if (tmpaddr < addr) /* wrap */
443 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
446 map = &p->p_vmspace->vm_map;
449 * map->token serializes extracting the address range for size == 0
450 * msyncs with the vm_map_clean call; if the token were not held
451 * across the two calls, an intervening munmap/mmap pair, for example,
452 * could cause msync to occur on a wrong region.
454 lwkt_gettoken(&map->token);
457 * XXX Gak! If size is zero we are supposed to sync "all modified
458 * pages with the region containing addr". Unfortunately, we don't
459 * really keep track of individual mmaps so we approximate by flushing
460 * the range of the map entry containing addr. This can be incorrect
461 * if the region splits or is coalesced with a neighbor.
464 vm_map_entry_t entry;
466 vm_map_lock_read(map);
467 rv = vm_map_lookup_entry(map, addr, &entry);
469 vm_map_unlock_read(map);
470 rv = KERN_INVALID_ADDRESS;
474 size = entry->end - entry->start;
475 vm_map_unlock_read(map);
479 * Clean the pages and interpret the return value.
481 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
482 (flags & MS_INVALIDATE) != 0);
484 lwkt_reltoken(&map->token);
489 case KERN_INVALID_ADDRESS:
490 return (EINVAL); /* Sun returns ENOMEM? */
501 * munmap system call handler
503 * munmap_args(void *addr, size_t len)
508 sys_munmap(struct munmap_args *uap)
510 struct proc *p = curproc;
513 vm_size_t size, pageoff;
516 addr = (vm_offset_t) uap->addr;
519 pageoff = (addr & PAGE_MASK);
522 size = (vm_size_t) round_page(size);
523 if (size < uap->len) /* wrap */
525 tmpaddr = addr + size; /* workaround gcc4 opt */
526 if (tmpaddr < addr) /* wrap */
533 * Check for illegal addresses. Watch out for address wrap... Note
534 * that VM_*_ADDRESS are not constants due to casts (argh).
536 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
538 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
541 map = &p->p_vmspace->vm_map;
543 /* map->token serializes between the map check and the actual unmap */
544 lwkt_gettoken(&map->token);
547 * Make sure entire range is allocated.
549 if (!vm_map_check_protection(map, addr, addr + size,
550 VM_PROT_NONE, FALSE)) {
551 lwkt_reltoken(&map->token);
554 /* returns nothing but KERN_SUCCESS anyway */
555 vm_map_remove(map, addr, addr + size);
556 lwkt_reltoken(&map->token);
561 * mprotect_args(const void *addr, size_t len, int prot)
566 sys_mprotect(struct mprotect_args *uap)
568 struct proc *p = curproc;
571 vm_size_t size, pageoff;
575 addr = (vm_offset_t) uap->addr;
577 prot = uap->prot & VM_PROT_ALL;
579 pageoff = (addr & PAGE_MASK);
582 size = (vm_size_t) round_page(size);
583 if (size < uap->len) /* wrap */
585 tmpaddr = addr + size; /* workaround gcc4 opt */
586 if (tmpaddr < addr) /* wrap */
589 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size,
594 case KERN_PROTECTION_FAILURE:
605 * minherit system call handler
607 * minherit_args(void *addr, size_t len, int inherit)
612 sys_minherit(struct minherit_args *uap)
614 struct proc *p = curproc;
617 vm_size_t size, pageoff;
618 vm_inherit_t inherit;
621 addr = (vm_offset_t)uap->addr;
623 inherit = uap->inherit;
625 pageoff = (addr & PAGE_MASK);
628 size = (vm_size_t) round_page(size);
629 if (size < uap->len) /* wrap */
631 tmpaddr = addr + size; /* workaround gcc4 opt */
632 if (tmpaddr < addr) /* wrap */
635 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr,
636 addr + size, inherit)) {
640 case KERN_PROTECTION_FAILURE:
651 * madvise system call handler
653 * madvise_args(void *addr, size_t len, int behav)
658 sys_madvise(struct madvise_args *uap)
660 struct proc *p = curproc;
661 vm_offset_t start, end;
662 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
666 * Check for illegal behavior
668 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END)
671 * Check for illegal addresses. Watch out for address wrap... Note
672 * that VM_*_ADDRESS are not constants due to casts (argh).
674 if (tmpaddr < (vm_offset_t)uap->addr)
676 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
678 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
682 * Since this routine is only advisory, we default to conservative
685 start = trunc_page((vm_offset_t)uap->addr);
686 end = round_page(tmpaddr);
688 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
694 * mcontrol system call handler
696 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
701 sys_mcontrol(struct mcontrol_args *uap)
703 struct proc *p = curproc;
704 vm_offset_t start, end;
705 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
709 * Check for illegal behavior
711 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
714 * Check for illegal addresses. Watch out for address wrap... Note
715 * that VM_*_ADDRESS are not constants due to casts (argh).
717 if (tmpaddr < (vm_offset_t) uap->addr)
719 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
721 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
725 * Since this routine is only advisory, we default to conservative
728 start = trunc_page((vm_offset_t)uap->addr);
729 end = round_page(tmpaddr);
731 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
732 uap->behav, uap->value);
738 * mincore system call handler
740 * mincore_args(const void *addr, size_t len, char *vec)
745 sys_mincore(struct mincore_args *uap)
747 struct proc *p = curproc;
748 vm_offset_t addr, first_addr;
749 vm_offset_t end, cend;
754 int vecindex, lastvecindex;
755 vm_map_entry_t current;
756 vm_map_entry_t entry;
758 unsigned int timestamp;
761 * Make sure that the addresses presented are valid for user
764 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
765 end = addr + (vm_size_t)round_page(uap->len);
768 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS)
772 * Address of byte vector
776 map = &p->p_vmspace->vm_map;
777 pmap = vmspace_pmap(p->p_vmspace);
779 lwkt_gettoken(&map->token);
780 vm_map_lock_read(map);
782 timestamp = map->timestamp;
784 if (!vm_map_lookup_entry(map, addr, &entry))
788 * Do this on a map entry basis so that if the pages are not
789 * in the current processes address space, we can easily look
790 * up the pages elsewhere.
794 (current != &map->header) && (current->start < end);
795 current = current->next) {
798 * ignore submaps (for now) or null objects
800 if (current->maptype != VM_MAPTYPE_NORMAL &&
801 current->maptype != VM_MAPTYPE_VPAGETABLE) {
804 if (current->object.vm_object == NULL)
808 * limit this scan to the current map entry and the
809 * limits for the mincore call
811 if (addr < current->start)
812 addr = current->start;
818 * scan this entry one page at a time
820 while (addr < cend) {
822 * Check pmap first, it is likely faster, also
823 * it can provide info as to whether we are the
824 * one referencing or modifying the page.
826 * If we have to check the VM object, only mess
827 * around with normal maps. Do not mess around
828 * with virtual page tables (XXX).
830 mincoreinfo = pmap_mincore(pmap, addr);
831 if (mincoreinfo == 0 &&
832 current->maptype == VM_MAPTYPE_NORMAL) {
838 * calculate the page index into the object
840 offset = current->offset + (addr - current->start);
841 pindex = OFF_TO_IDX(offset);
844 * if the page is resident, then gather
845 * information about it. spl protection is
846 * required to maintain the object
847 * association. And XXX what if the page is
848 * busy? What's the deal with that?
850 * XXX vm_token - legacy for pmap_ts_referenced
851 * in x86 and vkernel pmap code.
853 lwkt_gettoken(&vm_token);
854 vm_object_hold(current->object.vm_object);
855 m = vm_page_lookup(current->object.vm_object,
858 mincoreinfo = MINCORE_INCORE;
859 if (m->dirty || pmap_is_modified(m))
860 mincoreinfo |= MINCORE_MODIFIED_OTHER;
861 if ((m->flags & PG_REFERENCED) ||
862 pmap_ts_referenced(m)) {
863 vm_page_flag_set(m, PG_REFERENCED);
864 mincoreinfo |= MINCORE_REFERENCED_OTHER;
867 vm_object_drop(current->object.vm_object);
868 lwkt_reltoken(&vm_token);
872 * subyte may page fault. In case it needs to modify
873 * the map, we release the lock.
875 vm_map_unlock_read(map);
878 * calculate index into user supplied byte vector
880 vecindex = OFF_TO_IDX(addr - first_addr);
883 * If we have skipped map entries, we need to make sure that
884 * the byte vector is zeroed for those skipped entries.
886 while((lastvecindex + 1) < vecindex) {
887 error = subyte( vec + lastvecindex, 0);
896 * Pass the page information to the user
898 error = subyte(vec + vecindex, mincoreinfo);
905 * If the map has changed, due to the subyte,
906 * the previous output may be invalid.
908 vm_map_lock_read(map);
909 if (timestamp != map->timestamp)
912 lastvecindex = vecindex;
918 * subyte may page fault. In case it needs to modify
919 * the map, we release the lock.
921 vm_map_unlock_read(map);
924 * Zero the last entries in the byte vector.
926 vecindex = OFF_TO_IDX(end - first_addr);
927 while((lastvecindex + 1) < vecindex) {
928 error = subyte( vec + lastvecindex, 0);
937 * If the map has changed, due to the subyte, the previous
938 * output may be invalid.
940 vm_map_lock_read(map);
941 if (timestamp != map->timestamp)
943 vm_map_unlock_read(map);
947 lwkt_reltoken(&map->token);
952 * mlock system call handler
954 * mlock_args(const void *addr, size_t len)
959 sys_mlock(struct mlock_args *uap)
963 vm_size_t size, pageoff;
964 struct thread *td = curthread;
965 struct proc *p = td->td_proc;
968 addr = (vm_offset_t) uap->addr;
971 pageoff = (addr & PAGE_MASK);
974 size = (vm_size_t) round_page(size);
975 if (size < uap->len) /* wrap */
977 if (size == 0) /* silently allow 0 size */
979 tmpaddr = addr + size; /* workaround gcc4 opt */
980 if (tmpaddr < addr) /* wrap */
983 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
987 * We do not need to synchronize against other threads updating ucred;
988 * they update p->ucred, which is synchronized into td_ucred ourselves.
990 #ifdef pmap_wired_count
991 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
992 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) {
996 error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1001 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
1002 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1011 sys_mlockall(struct mlockall_args *uap)
1013 struct thread *td = curthread;
1014 struct proc *p = td->td_proc;
1015 vm_map_t map = &p->p_vmspace->vm_map;
1016 vm_map_entry_t entry;
1018 int rc = KERN_SUCCESS;
1020 if (((how & MCL_CURRENT) == 0) && ((how & MCL_FUTURE) == 0))
1023 rc = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1029 if (how & MCL_CURRENT) {
1030 for(entry = map->header.next;
1031 entry != &map->header;
1032 entry = entry->next);
1038 if (how & MCL_FUTURE)
1039 map->flags |= MAP_WIREFUTURE;
1049 * Unwire all user-wired map entries, cancel MCL_FUTURE.
1054 sys_munlockall(struct munlockall_args *uap)
1056 struct thread *td = curthread;
1057 struct proc *p = td->td_proc;
1058 vm_map_t map = &p->p_vmspace->vm_map;
1059 vm_map_entry_t entry;
1060 int rc = KERN_SUCCESS;
1064 /* Clear MAP_WIREFUTURE to cancel mlockall(MCL_FUTURE) */
1065 map->flags &= ~MAP_WIREFUTURE;
1068 for (entry = map->header.next;
1069 entry != &map->header;
1070 entry = entry->next) {
1071 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
1075 * If we encounter an in-transition entry, we release the
1076 * map lock and retry the scan; we do not decrement any
1077 * wired_count more than once because we do not touch
1078 * any entries with MAP_ENTRY_USER_WIRED not set.
1080 * There is a potential interleaving with concurrent
1081 * mlockall()s here -- if we abort a scan, an mlockall()
1082 * could start, wire a number of entries before our
1083 * current position in, and then stall itself on this
1084 * or any other in-transition entry. If that occurs, when
1085 * we resume, we will unwire those entries.
1087 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1088 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1089 ++mycpu->gd_cnt.v_intrans_coll;
1090 ++mycpu->gd_cnt.v_intrans_wait;
1091 vm_map_transition_wait(map, 1);
1095 KASSERT(entry->wired_count > 0,
1096 ("wired_count was 0 with USER_WIRED set! %p", entry));
1098 /* Drop wired count, if it hits zero, unwire the entry */
1099 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1100 entry->wired_count--;
1101 if (entry->wired_count == 0)
1102 vm_fault_unwire(map, entry);
1111 * munlock system call handler
1113 * munlock_args(const void *addr, size_t len)
1118 sys_munlock(struct munlock_args *uap)
1120 struct thread *td = curthread;
1121 struct proc *p = td->td_proc;
1123 vm_offset_t tmpaddr;
1124 vm_size_t size, pageoff;
1127 addr = (vm_offset_t) uap->addr;
1130 pageoff = (addr & PAGE_MASK);
1133 size = (vm_size_t) round_page(size);
1135 tmpaddr = addr + size;
1136 if (tmpaddr < addr) /* wrap */
1138 if (size == 0) /* silently allow 0 size */
1141 #ifndef pmap_wired_count
1142 error = priv_check(td, PRIV_ROOT);
1147 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
1148 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1152 * Internal version of mmap.
1153 * Currently used by mmap, exec, and sys5 shared memory.
1154 * Handle is either a vnode pointer or NULL for MAP_ANON.
1159 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1160 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff)
1167 int (*uksmap)(cdev_t dev, vm_page_t fake);
1169 struct thread *td = curthread;
1171 int rv = KERN_SUCCESS;
1179 objsize = round_page(size);
1184 lwkt_gettoken(&map->token);
1187 * XXX messy code, fixme
1189 * NOTE: Overflow checks require discrete statements or GCC4
1190 * will optimize it out.
1192 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) {
1193 esize = map->size + size; /* workaround gcc4 opt */
1194 if (esize < map->size ||
1195 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
1196 lwkt_reltoken(&map->token);
1202 * We currently can only deal with page aligned file offsets.
1203 * The check is here rather than in the syscall because the
1204 * kernel calls this function internally for other mmaping
1205 * operations (such as in exec) and non-aligned offsets will
1206 * cause pmap inconsistencies...so we want to be sure to
1207 * disallow this in all cases.
1209 * NOTE: Overflow checks require discrete statements or GCC4
1210 * will optimize it out.
1212 if (foff & PAGE_MASK) {
1213 lwkt_reltoken(&map->token);
1218 * Handle alignment. For large memory maps it is possible
1219 * that the MMU can optimize the page table so align anything
1220 * that is a multiple of SEG_SIZE to SEG_SIZE.
1222 * Also align any large mapping (bigger than 16x SG_SIZE) to a
1223 * SEG_SIZE address boundary.
1225 if (flags & MAP_SIZEALIGN) {
1227 if ((align ^ (align - 1)) != (align << 1) - 1) {
1228 lwkt_reltoken(&map->token);
1231 } else if ((flags & MAP_FIXED) == 0 &&
1232 ((size & SEG_MASK) == 0 || size > SEG_SIZE * 16)) {
1238 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
1240 *addr = round_page(*addr);
1242 if (*addr != trunc_page(*addr)) {
1243 lwkt_reltoken(&map->token);
1246 eaddr = *addr + size;
1247 if (eaddr < *addr) {
1248 lwkt_reltoken(&map->token);
1252 if ((flags & MAP_TRYFIXED) == 0)
1253 vm_map_remove(map, *addr, *addr + size);
1259 * Lookup/allocate object.
1261 if (flags & MAP_ANON) {
1263 * Unnamed anonymous regions always start at 0.
1267 * Default memory object
1269 object = default_pager_alloc(handle, objsize,
1271 if (object == NULL) {
1272 lwkt_reltoken(&map->token);
1275 docow = MAP_PREFAULT_PARTIAL;
1278 * Implicit single instance of a default memory
1279 * object, so we don't need a VM object yet.
1287 vp = (struct vnode *)handle;
1290 * Non-anonymous mappings of VCHR (aka not /dev/zero)
1291 * cannot specify MAP_STACK or MAP_VPAGETABLE.
1293 if (vp->v_type == VCHR) {
1294 if (flags & (MAP_STACK | MAP_VPAGETABLE)) {
1295 lwkt_reltoken(&map->token);
1300 if (vp->v_type == VCHR && vp->v_rdev->si_ops->d_uksmap) {
1302 * Device mappings without a VM object, typically
1303 * sharing permanently allocated kernel memory or
1304 * process-context-specific (per-process) data.
1306 * Force them to be shared.
1308 uksmap = vp->v_rdev->si_ops->d_uksmap;
1310 docow = MAP_PREFAULT_PARTIAL;
1311 flags &= ~(MAP_PRIVATE|MAP_COPY);
1312 flags |= MAP_SHARED;
1313 } else if (vp->v_type == VCHR) {
1315 * Device mappings (device size unknown?).
1316 * Force them to be shared.
1318 error = dev_dmmap_single(vp->v_rdev, &foff, objsize,
1319 &object, prot, NULL);
1321 if (error == ENODEV) {
1322 handle = (void *)(intptr_t)vp->v_rdev;
1323 object = dev_pager_alloc(handle, objsize, prot, foff);
1324 if (object == NULL) {
1325 lwkt_reltoken(&map->token);
1329 lwkt_reltoken(&map->token);
1333 docow = MAP_PREFAULT_PARTIAL;
1334 flags &= ~(MAP_PRIVATE|MAP_COPY);
1335 flags |= MAP_SHARED;
1338 * Regular file mapping (typically). The attribute
1339 * check is for the link count test only. mmapable
1340 * vnodes must already have a VM object assigned.
1345 error = VOP_GETATTR(vp, &vat);
1347 lwkt_reltoken(&map->token);
1350 docow = MAP_PREFAULT_PARTIAL;
1351 object = vnode_pager_reference(vp);
1352 if (object == NULL && vp->v_type == VREG) {
1353 lwkt_reltoken(&map->token);
1354 kprintf("Warning: cannot mmap vnode %p, no "
1360 * If it is a regular file without any references
1361 * we do not need to sync it.
1363 if (vp->v_type == VREG && vat.va_nlink == 0) {
1364 flags |= MAP_NOSYNC;
1370 * Deal with the adjusted flags
1372 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1373 docow |= MAP_COPY_ON_WRITE;
1374 if (flags & MAP_NOSYNC)
1375 docow |= MAP_DISABLE_SYNCER;
1376 if (flags & MAP_NOCORE)
1377 docow |= MAP_DISABLE_COREDUMP;
1380 * This may place the area in its own page directory if (size) is
1381 * large enough, otherwise it typically returns its argument.
1383 * (object can be NULL)
1386 *addr = pmap_addr_hint(object, *addr, size);
1390 * Stack mappings need special attention.
1392 * Mappings that use virtual page tables will default to storing
1393 * the page table at offset 0.
1396 rv = vm_map_find(map, uksmap, vp->v_rdev,
1399 VM_MAPTYPE_UKSMAP, VM_SUBSYS_MMAP,
1400 prot, maxprot, docow);
1401 } else if (flags & MAP_STACK) {
1402 rv = vm_map_stack(map, *addr, size, flags,
1403 prot, maxprot, docow);
1404 } else if (flags & MAP_VPAGETABLE) {
1405 rv = vm_map_find(map, object, NULL,
1408 VM_MAPTYPE_VPAGETABLE, VM_SUBSYS_MMAP,
1409 prot, maxprot, docow);
1411 rv = vm_map_find(map, object, NULL,
1414 VM_MAPTYPE_NORMAL, VM_SUBSYS_MMAP,
1415 prot, maxprot, docow);
1418 if (rv != KERN_SUCCESS) {
1420 * Lose the object reference. Will destroy the
1421 * object if it's an unnamed anonymous mapping
1422 * or named anonymous without other references.
1424 * (NOTE: object can be NULL)
1426 vm_object_deallocate(object);
1431 * Shared memory is also shared with children.
1433 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1434 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1435 if (rv != KERN_SUCCESS) {
1436 vm_map_remove(map, *addr, *addr + size);
1441 /* If a process has marked all future mappings for wiring, do so */
1442 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1443 vm_map_unwire(map, *addr, *addr + size, FALSE);
1446 * Set the access time on the vnode
1449 vn_mark_atime(vp, td);
1451 lwkt_reltoken(&map->token);
1456 case KERN_INVALID_ADDRESS:
1459 case KERN_PROTECTION_FAILURE:
1467 * Translate a Mach VM return code to zero on success or the appropriate errno
1471 vm_mmap_to_errno(int rv)
1477 case KERN_INVALID_ADDRESS:
1480 case KERN_PROTECTION_FAILURE: