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)
403 int flags = uap->flags;
404 off_t upos = uap->pos;
407 * Work around fairly serious problems with trying to have an
408 * auto-grow stack segment related to other unrelated calls to
409 * mmap() potentially getting addresses within such segments.
411 * Our attempt to use TRYFIXED to mediate the problem basically
412 * failed. For example, rtld-elf uses it to try to optimize
413 * shlib placement, but could run afoul of this issue.
415 * The only remaining true MAP_STACK we allow is the user stack as
416 * created by the exec code. All userland MAP_STACK's are converted
417 * to normal mmap()s right here.
419 if (flags & MAP_STACK) {
422 if ((uap->prot & (PROT_READ|PROT_WRITE)) !=
423 (PROT_READ|PROT_WRITE)) {
431 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len,
433 uap->fd, upos, &uap->sysmsg_resultp);
439 * msync system call handler
441 * msync_args(void *addr, size_t len, int flags)
446 sys_msync(struct msync_args *uap)
448 struct proc *p = curproc;
451 vm_size_t size, pageoff;
456 addr = (vm_offset_t) uap->addr;
460 pageoff = (addr & PAGE_MASK);
463 size = (vm_size_t) round_page(size);
464 if (size < uap->len) /* wrap */
466 tmpaddr = addr + size; /* workaround gcc4 opt */
467 if (tmpaddr < addr) /* wrap */
470 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
473 map = &p->p_vmspace->vm_map;
476 * map->token serializes extracting the address range for size == 0
477 * msyncs with the vm_map_clean call; if the token were not held
478 * across the two calls, an intervening munmap/mmap pair, for example,
479 * could cause msync to occur on a wrong region.
481 lwkt_gettoken(&map->token);
484 * XXX Gak! If size is zero we are supposed to sync "all modified
485 * pages with the region containing addr". Unfortunately, we don't
486 * really keep track of individual mmaps so we approximate by flushing
487 * the range of the map entry containing addr. This can be incorrect
488 * if the region splits or is coalesced with a neighbor.
491 vm_map_entry_t entry;
493 vm_map_lock_read(map);
494 rv = vm_map_lookup_entry(map, addr, &entry);
496 vm_map_unlock_read(map);
497 rv = KERN_INVALID_ADDRESS;
501 size = entry->end - entry->start;
502 vm_map_unlock_read(map);
506 * Clean the pages and interpret the return value.
508 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
509 (flags & MS_INVALIDATE) != 0);
511 lwkt_reltoken(&map->token);
516 case KERN_INVALID_ADDRESS:
517 return (EINVAL); /* Sun returns ENOMEM? */
528 * munmap system call handler
530 * munmap_args(void *addr, size_t len)
535 sys_munmap(struct munmap_args *uap)
537 struct proc *p = curproc;
540 vm_size_t size, pageoff;
543 addr = (vm_offset_t) uap->addr;
546 pageoff = (addr & PAGE_MASK);
549 size = (vm_size_t) round_page(size);
550 if (size < uap->len) /* wrap */
552 tmpaddr = addr + size; /* workaround gcc4 opt */
553 if (tmpaddr < addr) /* wrap */
560 * Check for illegal addresses. Watch out for address wrap... Note
561 * that VM_*_ADDRESS are not constants due to casts (argh).
563 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
565 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
568 map = &p->p_vmspace->vm_map;
570 /* map->token serializes between the map check and the actual unmap */
571 lwkt_gettoken(&map->token);
574 * Make sure entire range is allocated.
576 if (!vm_map_check_protection(map, addr, addr + size,
577 VM_PROT_NONE, FALSE)) {
578 lwkt_reltoken(&map->token);
581 /* returns nothing but KERN_SUCCESS anyway */
582 vm_map_remove(map, addr, addr + size);
583 lwkt_reltoken(&map->token);
588 * mprotect_args(const void *addr, size_t len, int prot)
593 sys_mprotect(struct mprotect_args *uap)
595 struct proc *p = curproc;
598 vm_size_t size, pageoff;
602 addr = (vm_offset_t) uap->addr;
604 prot = uap->prot & VM_PROT_ALL;
606 pageoff = (addr & PAGE_MASK);
609 size = (vm_size_t) round_page(size);
610 if (size < uap->len) /* wrap */
612 tmpaddr = addr + size; /* workaround gcc4 opt */
613 if (tmpaddr < addr) /* wrap */
616 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size,
621 case KERN_PROTECTION_FAILURE:
632 * minherit system call handler
634 * minherit_args(void *addr, size_t len, int inherit)
639 sys_minherit(struct minherit_args *uap)
641 struct proc *p = curproc;
644 vm_size_t size, pageoff;
645 vm_inherit_t inherit;
648 addr = (vm_offset_t)uap->addr;
650 inherit = uap->inherit;
652 pageoff = (addr & PAGE_MASK);
655 size = (vm_size_t) round_page(size);
656 if (size < uap->len) /* wrap */
658 tmpaddr = addr + size; /* workaround gcc4 opt */
659 if (tmpaddr < addr) /* wrap */
662 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr,
663 addr + size, inherit)) {
667 case KERN_PROTECTION_FAILURE:
678 * madvise system call handler
680 * madvise_args(void *addr, size_t len, int behav)
685 sys_madvise(struct madvise_args *uap)
687 struct proc *p = curproc;
688 vm_offset_t start, end;
689 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
693 * Check for illegal behavior
695 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END)
698 * Check for illegal addresses. Watch out for address wrap... Note
699 * that VM_*_ADDRESS are not constants due to casts (argh).
701 if (tmpaddr < (vm_offset_t)uap->addr)
703 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
705 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
709 * Since this routine is only advisory, we default to conservative
712 start = trunc_page((vm_offset_t)uap->addr);
713 end = round_page(tmpaddr);
715 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
721 * mcontrol system call handler
723 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
728 sys_mcontrol(struct mcontrol_args *uap)
730 struct proc *p = curproc;
731 vm_offset_t start, end;
732 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
736 * Check for illegal behavior
738 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
741 * Check for illegal addresses. Watch out for address wrap... Note
742 * that VM_*_ADDRESS are not constants due to casts (argh).
744 if (tmpaddr < (vm_offset_t) uap->addr)
746 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
748 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
752 * Since this routine is only advisory, we default to conservative
755 start = trunc_page((vm_offset_t)uap->addr);
756 end = round_page(tmpaddr);
758 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
759 uap->behav, uap->value);
765 * mincore system call handler
767 * mincore_args(const void *addr, size_t len, char *vec)
772 sys_mincore(struct mincore_args *uap)
774 struct proc *p = curproc;
775 vm_offset_t addr, first_addr;
776 vm_offset_t end, cend;
781 int vecindex, lastvecindex;
782 vm_map_entry_t current;
783 vm_map_entry_t entry;
785 unsigned int timestamp;
788 * Make sure that the addresses presented are valid for user
791 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
792 end = addr + (vm_size_t)round_page(uap->len);
795 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS)
799 * Address of byte vector
803 map = &p->p_vmspace->vm_map;
804 pmap = vmspace_pmap(p->p_vmspace);
806 lwkt_gettoken(&map->token);
807 vm_map_lock_read(map);
809 timestamp = map->timestamp;
811 if (!vm_map_lookup_entry(map, addr, &entry))
812 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
815 * Do this on a map entry basis so that if the pages are not
816 * in the current processes address space, we can easily look
817 * up the pages elsewhere.
820 for (current = entry;
821 current && current->start < end;
822 current = vm_map_rb_tree_RB_NEXT(current)) {
824 * ignore submaps (for now) or null objects
826 if (current->maptype != VM_MAPTYPE_NORMAL &&
827 current->maptype != VM_MAPTYPE_VPAGETABLE) {
830 if (current->object.vm_object == NULL)
834 * limit this scan to the current map entry and the
835 * limits for the mincore call
837 if (addr < current->start)
838 addr = current->start;
844 * scan this entry one page at a time
846 while (addr < cend) {
848 * Check pmap first, it is likely faster, also
849 * it can provide info as to whether we are the
850 * one referencing or modifying the page.
852 * If we have to check the VM object, only mess
853 * around with normal maps. Do not mess around
854 * with virtual page tables (XXX).
856 mincoreinfo = pmap_mincore(pmap, addr);
857 if (mincoreinfo == 0 &&
858 current->maptype == VM_MAPTYPE_NORMAL) {
864 * calculate the page index into the object
866 offset = current->offset + (addr - current->start);
867 pindex = OFF_TO_IDX(offset);
870 * if the page is resident, then gather
871 * information about it. spl protection is
872 * required to maintain the object
873 * association. And XXX what if the page is
874 * busy? What's the deal with that?
876 * XXX vm_token - legacy for pmap_ts_referenced
877 * in x86 and vkernel pmap code.
879 lwkt_gettoken(&vm_token);
880 vm_object_hold(current->object.vm_object);
881 m = vm_page_lookup(current->object.vm_object,
884 mincoreinfo = MINCORE_INCORE;
885 if (m->dirty || pmap_is_modified(m))
886 mincoreinfo |= MINCORE_MODIFIED_OTHER;
887 if ((m->flags & PG_REFERENCED) ||
888 pmap_ts_referenced(m)) {
889 vm_page_flag_set(m, PG_REFERENCED);
890 mincoreinfo |= MINCORE_REFERENCED_OTHER;
893 vm_object_drop(current->object.vm_object);
894 lwkt_reltoken(&vm_token);
898 * subyte may page fault. In case it needs to modify
899 * the map, we release the lock.
901 vm_map_unlock_read(map);
904 * calculate index into user supplied byte vector
906 vecindex = OFF_TO_IDX(addr - first_addr);
909 * If we have skipped map entries, we need to make sure that
910 * the byte vector is zeroed for those skipped entries.
912 while((lastvecindex + 1) < vecindex) {
913 error = subyte( vec + lastvecindex, 0);
922 * Pass the page information to the user
924 error = subyte(vec + vecindex, mincoreinfo);
931 * If the map has changed, due to the subyte,
932 * the previous output may be invalid.
934 vm_map_lock_read(map);
935 if (timestamp != map->timestamp)
938 lastvecindex = vecindex;
944 * subyte may page fault. In case it needs to modify
945 * the map, we release the lock.
947 vm_map_unlock_read(map);
950 * Zero the last entries in the byte vector.
952 vecindex = OFF_TO_IDX(end - first_addr);
953 while((lastvecindex + 1) < vecindex) {
954 error = subyte( vec + lastvecindex, 0);
963 * If the map has changed, due to the subyte, the previous
964 * output may be invalid.
966 vm_map_lock_read(map);
967 if (timestamp != map->timestamp)
969 vm_map_unlock_read(map);
973 lwkt_reltoken(&map->token);
978 * mlock system call handler
980 * mlock_args(const void *addr, size_t len)
985 sys_mlock(struct mlock_args *uap)
989 vm_size_t size, pageoff;
990 struct thread *td = curthread;
991 struct proc *p = td->td_proc;
994 addr = (vm_offset_t) uap->addr;
997 pageoff = (addr & PAGE_MASK);
1000 size = (vm_size_t) round_page(size);
1001 if (size < uap->len) /* wrap */
1003 if (size == 0) /* silently allow 0 size */
1005 tmpaddr = addr + size; /* workaround gcc4 opt */
1006 if (tmpaddr < addr) /* wrap */
1009 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
1013 * We do not need to synchronize against other threads updating ucred;
1014 * they update p->ucred, which is synchronized into td_ucred ourselves.
1016 #ifdef pmap_wired_count
1017 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
1018 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) {
1022 error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1027 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
1028 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1037 sys_mlockall(struct mlockall_args *uap)
1039 struct thread *td = curthread;
1040 struct proc *p = td->td_proc;
1041 vm_map_t map = &p->p_vmspace->vm_map;
1042 vm_map_entry_t entry;
1044 int rc = KERN_SUCCESS;
1046 if (((how & MCL_CURRENT) == 0) && ((how & MCL_FUTURE) == 0))
1049 rc = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1055 if (how & MCL_CURRENT) {
1056 RB_FOREACH(entry, vm_map_rb_tree, &map->rb_root) {
1057 ; /* NOT IMPLEMENTED YET */
1062 if (how & MCL_FUTURE)
1063 map->flags |= MAP_WIREFUTURE;
1073 * Unwire all user-wired map entries, cancel MCL_FUTURE.
1078 sys_munlockall(struct munlockall_args *uap)
1080 struct thread *td = curthread;
1081 struct proc *p = td->td_proc;
1082 vm_map_t map = &p->p_vmspace->vm_map;
1083 vm_map_entry_t entry;
1084 int rc = KERN_SUCCESS;
1088 /* Clear MAP_WIREFUTURE to cancel mlockall(MCL_FUTURE) */
1089 map->flags &= ~MAP_WIREFUTURE;
1092 RB_FOREACH(entry, vm_map_rb_tree, &map->rb_root) {
1093 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
1097 * If we encounter an in-transition entry, we release the
1098 * map lock and retry the scan; we do not decrement any
1099 * wired_count more than once because we do not touch
1100 * any entries with MAP_ENTRY_USER_WIRED not set.
1102 * There is a potential interleaving with concurrent
1103 * mlockall()s here -- if we abort a scan, an mlockall()
1104 * could start, wire a number of entries before our
1105 * current position in, and then stall itself on this
1106 * or any other in-transition entry. If that occurs, when
1107 * we resume, we will unwire those entries.
1109 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1110 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1111 ++mycpu->gd_cnt.v_intrans_coll;
1112 ++mycpu->gd_cnt.v_intrans_wait;
1113 vm_map_transition_wait(map, 1);
1117 KASSERT(entry->wired_count > 0,
1118 ("wired_count was 0 with USER_WIRED set! %p", entry));
1120 /* Drop wired count, if it hits zero, unwire the entry */
1121 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1122 entry->wired_count--;
1123 if (entry->wired_count == 0)
1124 vm_fault_unwire(map, entry);
1133 * munlock system call handler
1135 * munlock_args(const void *addr, size_t len)
1140 sys_munlock(struct munlock_args *uap)
1142 struct thread *td = curthread;
1143 struct proc *p = td->td_proc;
1145 vm_offset_t tmpaddr;
1146 vm_size_t size, pageoff;
1149 addr = (vm_offset_t) uap->addr;
1152 pageoff = (addr & PAGE_MASK);
1155 size = (vm_size_t) round_page(size);
1157 tmpaddr = addr + size;
1158 if (tmpaddr < addr) /* wrap */
1160 if (size == 0) /* silently allow 0 size */
1163 #ifndef pmap_wired_count
1164 error = priv_check(td, PRIV_ROOT);
1169 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
1170 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1174 * Internal version of mmap.
1175 * Currently used by mmap, exec, and sys5 shared memory.
1176 * Handle is either a vnode pointer or NULL for MAP_ANON.
1181 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1182 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff)
1189 int (*uksmap)(cdev_t dev, vm_page_t fake);
1191 struct thread *td = curthread;
1193 int rv = KERN_SUCCESS;
1201 objsize = round_page(size);
1206 lwkt_gettoken(&map->token);
1209 * XXX messy code, fixme
1211 * NOTE: Overflow checks require discrete statements or GCC4
1212 * will optimize it out.
1214 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) {
1215 esize = map->size + size; /* workaround gcc4 opt */
1216 if (esize < map->size ||
1217 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
1218 lwkt_reltoken(&map->token);
1224 * We currently can only deal with page aligned file offsets.
1225 * The check is here rather than in the syscall because the
1226 * kernel calls this function internally for other mmaping
1227 * operations (such as in exec) and non-aligned offsets will
1228 * cause pmap inconsistencies...so we want to be sure to
1229 * disallow this in all cases.
1231 * NOTE: Overflow checks require discrete statements or GCC4
1232 * will optimize it out.
1234 if (foff & PAGE_MASK) {
1235 lwkt_reltoken(&map->token);
1240 * Handle alignment. For large memory maps it is possible
1241 * that the MMU can optimize the page table so align anything
1242 * that is a multiple of SEG_SIZE to SEG_SIZE.
1244 * Also align any large mapping (bigger than 16x SG_SIZE) to a
1245 * SEG_SIZE address boundary.
1247 if (flags & MAP_SIZEALIGN) {
1249 if ((align ^ (align - 1)) != (align << 1) - 1) {
1250 lwkt_reltoken(&map->token);
1253 } else if ((flags & MAP_FIXED) == 0 &&
1254 ((size & SEG_MASK) == 0 || size > SEG_SIZE * 16)) {
1260 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
1262 *addr = round_page(*addr);
1264 if (*addr != trunc_page(*addr)) {
1265 lwkt_reltoken(&map->token);
1268 eaddr = *addr + size;
1269 if (eaddr < *addr) {
1270 lwkt_reltoken(&map->token);
1274 if ((flags & MAP_TRYFIXED) == 0)
1275 vm_map_remove(map, *addr, *addr + size);
1281 * Lookup/allocate object.
1283 if (flags & MAP_ANON) {
1285 * Unnamed anonymous regions always start at 0.
1289 * Default memory object
1291 object = default_pager_alloc(handle, objsize,
1293 if (object == NULL) {
1294 lwkt_reltoken(&map->token);
1297 docow = MAP_PREFAULT_PARTIAL;
1300 * Implicit single instance of a default memory
1301 * object, so we don't need a VM object yet.
1309 vp = (struct vnode *)handle;
1312 * Non-anonymous mappings of VCHR (aka not /dev/zero)
1313 * cannot specify MAP_STACK or MAP_VPAGETABLE.
1315 if (vp->v_type == VCHR) {
1316 if (flags & (MAP_STACK | MAP_VPAGETABLE)) {
1317 lwkt_reltoken(&map->token);
1322 if (vp->v_type == VCHR && vp->v_rdev->si_ops->d_uksmap) {
1324 * Device mappings without a VM object, typically
1325 * sharing permanently allocated kernel memory or
1326 * process-context-specific (per-process) data.
1328 * Force them to be shared.
1330 uksmap = vp->v_rdev->si_ops->d_uksmap;
1332 docow = MAP_PREFAULT_PARTIAL;
1333 flags &= ~(MAP_PRIVATE|MAP_COPY);
1334 flags |= MAP_SHARED;
1335 } else if (vp->v_type == VCHR) {
1337 * Device mappings (device size unknown?).
1338 * Force them to be shared.
1340 error = dev_dmmap_single(vp->v_rdev, &foff, objsize,
1341 &object, prot, NULL);
1343 if (error == ENODEV) {
1344 handle = (void *)(intptr_t)vp->v_rdev;
1345 object = dev_pager_alloc(handle, objsize, prot, foff);
1346 if (object == NULL) {
1347 lwkt_reltoken(&map->token);
1351 lwkt_reltoken(&map->token);
1355 docow = MAP_PREFAULT_PARTIAL;
1356 flags &= ~(MAP_PRIVATE|MAP_COPY);
1357 flags |= MAP_SHARED;
1360 * Regular file mapping (typically). The attribute
1361 * check is for the link count test only. mmapable
1362 * vnodes must already have a VM object assigned.
1367 error = VOP_GETATTR(vp, &vat);
1369 lwkt_reltoken(&map->token);
1372 docow = MAP_PREFAULT_PARTIAL;
1373 object = vnode_pager_reference(vp);
1374 if (object == NULL && vp->v_type == VREG) {
1375 lwkt_reltoken(&map->token);
1376 kprintf("Warning: cannot mmap vnode %p, no "
1382 * If it is a regular file without any references
1383 * we do not need to sync it.
1385 if (vp->v_type == VREG && vat.va_nlink == 0) {
1386 flags |= MAP_NOSYNC;
1392 * Deal with the adjusted flags
1394 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1395 docow |= MAP_COPY_ON_WRITE;
1396 if (flags & MAP_NOSYNC)
1397 docow |= MAP_DISABLE_SYNCER;
1398 if (flags & MAP_NOCORE)
1399 docow |= MAP_DISABLE_COREDUMP;
1402 * This may place the area in its own page directory if (size) is
1403 * large enough, otherwise it typically returns its argument.
1405 * (object can be NULL)
1408 *addr = pmap_addr_hint(object, *addr, size);
1412 * Stack mappings need special attention.
1414 * Mappings that use virtual page tables will default to storing
1415 * the page table at offset 0.
1418 rv = vm_map_find(map, uksmap, vp->v_rdev,
1421 VM_MAPTYPE_UKSMAP, VM_SUBSYS_MMAP,
1422 prot, maxprot, docow);
1423 } else if (flags & MAP_STACK) {
1424 rv = vm_map_stack(map, addr, size, flags,
1425 prot, maxprot, docow);
1426 } else if (flags & MAP_VPAGETABLE) {
1427 rv = vm_map_find(map, object, NULL,
1430 VM_MAPTYPE_VPAGETABLE, VM_SUBSYS_MMAP,
1431 prot, maxprot, docow);
1433 rv = vm_map_find(map, object, NULL,
1436 VM_MAPTYPE_NORMAL, VM_SUBSYS_MMAP,
1437 prot, maxprot, docow);
1440 if (rv != KERN_SUCCESS) {
1442 * Lose the object reference. Will destroy the
1443 * object if it's an unnamed anonymous mapping
1444 * or named anonymous without other references.
1446 * (NOTE: object can be NULL)
1448 vm_object_deallocate(object);
1453 * Shared memory is also shared with children.
1455 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1456 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1457 if (rv != KERN_SUCCESS) {
1458 vm_map_remove(map, *addr, *addr + size);
1463 /* If a process has marked all future mappings for wiring, do so */
1464 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1465 vm_map_unwire(map, *addr, *addr + size, FALSE);
1468 * Set the access time on the vnode
1471 vn_mark_atime(vp, td);
1473 lwkt_reltoken(&map->token);
1478 case KERN_INVALID_ADDRESS:
1481 case KERN_PROTECTION_FAILURE:
1489 * Translate a Mach VM return code to zero on success or the appropriate errno
1493 vm_mmap_to_errno(int rv)
1499 case KERN_INVALID_ADDRESS:
1502 case KERN_PROTECTION_FAILURE: