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 <sys/thread2.h>
80 #include <vm/vm_page2.h>
82 static int max_proc_mmap = 1000000;
83 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
85 SYSCTL_INT(_vm, OID_AUTO, vkernel_enable, CTLFLAG_RW, &vkernel_enable, 0, "");
91 sys_sbrk(struct sbrk_args *uap)
93 /* Not yet implemented */
103 sys_sstk(struct sstk_args *uap)
105 /* Not yet implemented */
110 * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
111 * long pad, off_t pos)
113 * Memory Map (mmap) system call. Note that the file offset
114 * and address are allowed to be NOT page aligned, though if
115 * the MAP_FIXED flag it set, both must have the same remainder
116 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
117 * page-aligned, the actual mapping starts at trunc_page(addr)
118 * and the return value is adjusted up by the page offset.
120 * Generally speaking, only character devices which are themselves
121 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
122 * there would be no cache coherency between a descriptor and a VM mapping
123 * both to the same character device.
125 * Block devices can be mmap'd no matter what they represent. Cache coherency
126 * is maintained as long as you do not write directly to the underlying
132 kern_mmap(struct vmspace *vms, caddr_t uaddr, size_t ulen,
133 int uprot, int uflags, int fd, off_t upos, void **res)
135 struct thread *td = curthread;
136 struct proc *p = td->td_proc;
137 struct file *fp = NULL;
141 vm_size_t size, pageoff;
142 vm_prot_t prot, maxprot;
150 addr = (vm_offset_t) uaddr;
152 prot = uprot & VM_PROT_ALL;
157 * Make sure mapping fits into numeric range etc.
159 * NOTE: We support the full unsigned range for size now.
161 if (((flags & MAP_ANON) && (fd != -1 || pos != 0)))
167 if (flags & MAP_STACK) {
170 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
177 * Virtual page tables cannot be used with MAP_STACK. Apart from
178 * it not making any sense, the aux union is used by both
181 * Because the virtual page table is stored in the backing object
182 * and might be updated by the kernel, the mapping must be R+W.
184 if (flags & MAP_VPAGETABLE) {
185 if (vkernel_enable == 0)
187 if (flags & MAP_STACK)
189 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
194 * Align the file position to a page boundary,
195 * and save its page offset component.
197 pageoff = (pos & PAGE_MASK);
200 /* Adjust size for rounding (on both ends). */
201 size += pageoff; /* low end... */
202 size = (vm_size_t) round_page(size); /* hi end */
203 if (size < ulen) /* wrap */
207 * Check for illegal addresses. Watch out for address wrap... Note
208 * that VM_*_ADDRESS are not constants due to casts (argh).
210 if (flags & (MAP_FIXED | MAP_TRYFIXED)) {
212 * The specified address must have the same remainder
213 * as the file offset taken modulo PAGE_SIZE, so it
214 * should be aligned after adjustment by pageoff.
217 if (addr & PAGE_MASK)
221 * Address range must be all in user VM space and not wrap.
223 tmpaddr = addr + size;
226 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
228 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
232 * Get a hint of where to map. It also provides mmap offset
233 * randomization if enabled.
235 addr = vm_map_hint(p, addr, prot);
238 if (flags & MAP_ANON) {
240 * Mapping blank space is trivial.
243 maxprot = VM_PROT_ALL;
246 * Mapping file, get fp for validation. Obtain vnode and make
247 * sure it is of appropriate type.
249 fp = holdfp(p->p_fd, fd, -1);
252 if (fp->f_type != DTYPE_VNODE) {
257 * POSIX shared-memory objects are defined to have
258 * kernel persistence, and are not defined to support
259 * read(2)/write(2) -- or even open(2). Thus, we can
260 * use MAP_ASYNC to trade on-disk coherence for speed.
261 * The shm_open(3) library routine turns on the FPOSIXSHM
262 * flag to request this behavior.
264 if (fp->f_flag & FPOSIXSHM)
266 vp = (struct vnode *) fp->f_data;
269 * Validate the vnode for the operation.
274 * Get the proper underlying object
276 if ((obj = vp->v_object) == NULL) {
280 KKASSERT((struct vnode *)obj->handle == vp);
284 * Make sure a device has not been revoked.
285 * Mappability is handled by the device layer.
287 if (vp->v_rdev == NULL) {
294 * Nothing else is mappable.
301 * XXX hack to handle use of /dev/zero to map anon memory (ala
304 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
306 maxprot = VM_PROT_ALL;
311 * cdevs does not provide private mappings of any kind.
313 if (vp->v_type == VCHR &&
314 (flags & (MAP_PRIVATE|MAP_COPY))) {
319 * Ensure that file and memory protections are
320 * compatible. Note that we only worry about
321 * writability if mapping is shared; in this case,
322 * current and max prot are dictated by the open file.
323 * XXX use the vnode instead? Problem is: what
324 * credentials do we use for determination? What if
325 * proc does a setuid?
327 maxprot = VM_PROT_EXECUTE;
328 if (fp->f_flag & FREAD) {
329 maxprot |= VM_PROT_READ;
330 } else if (prot & PROT_READ) {
335 * If we are sharing potential changes (either via
336 * MAP_SHARED or via the implicit sharing of character
337 * device mappings), and we are trying to get write
338 * permission although we opened it without asking
339 * for it, bail out. Check for superuser, only if
340 * we're at securelevel < 1, to allow the XIG X server
341 * to continue to work.
343 * PROT_WRITE + MAP_SHARED
345 if ((flags & MAP_SHARED) != 0 || vp->v_type == VCHR) {
346 if ((fp->f_flag & FWRITE) != 0) {
348 if ((error = VOP_GETATTR(vp, &va))) {
352 (IMMUTABLE|APPEND)) == 0) {
353 maxprot |= VM_PROT_WRITE;
356 * SHARED+RW file mmap()
357 * updates v_lastwrite_ts.
359 if ((prot & PROT_WRITE) &&
360 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY) == 0) {
361 vfs_timestamp(&vp->v_lastwrite_ts);
362 vsetflags(vp, VLASTWRITETS);
365 } else if (prot & PROT_WRITE) {
369 } else if ((prot & PROT_WRITE) != 0) {
374 maxprot |= VM_PROT_WRITE;
380 lwkt_gettoken(&vms->vm_map.token);
383 * Do not allow more then a certain number of vm_map_entry structures
384 * per process. 0 to disable.
386 if (max_proc_mmap && vms->vm_map.nentries >= max_proc_mmap) {
388 lwkt_reltoken(&vms->vm_map.token);
392 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
395 *res = (void *)(addr + pageoff);
397 lwkt_reltoken(&vms->vm_map.token);
406 * mmap system call handler
411 sys_mmap(struct mmap_args *uap)
415 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len,
416 uap->prot, uap->flags,
417 uap->fd, uap->pos, &uap->sysmsg_resultp);
423 * msync system call handler
425 * msync_args(void *addr, size_t len, int flags)
430 sys_msync(struct msync_args *uap)
432 struct proc *p = curproc;
435 vm_size_t size, pageoff;
440 addr = (vm_offset_t) uap->addr;
444 pageoff = (addr & PAGE_MASK);
447 size = (vm_size_t) round_page(size);
448 if (size < uap->len) /* wrap */
450 tmpaddr = addr + size; /* workaround gcc4 opt */
451 if (tmpaddr < addr) /* wrap */
454 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
457 map = &p->p_vmspace->vm_map;
460 * map->token serializes extracting the address range for size == 0
461 * msyncs with the vm_map_clean call; if the token were not held
462 * across the two calls, an intervening munmap/mmap pair, for example,
463 * could cause msync to occur on a wrong region.
465 lwkt_gettoken(&map->token);
468 * XXX Gak! If size is zero we are supposed to sync "all modified
469 * pages with the region containing addr". Unfortunately, we don't
470 * really keep track of individual mmaps so we approximate by flushing
471 * the range of the map entry containing addr. This can be incorrect
472 * if the region splits or is coalesced with a neighbor.
475 vm_map_entry_t entry;
477 vm_map_lock_read(map);
478 rv = vm_map_lookup_entry(map, addr, &entry);
480 vm_map_unlock_read(map);
481 rv = KERN_INVALID_ADDRESS;
485 size = entry->end - entry->start;
486 vm_map_unlock_read(map);
490 * Clean the pages and interpret the return value.
492 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
493 (flags & MS_INVALIDATE) != 0);
495 lwkt_reltoken(&map->token);
500 case KERN_INVALID_ADDRESS:
501 return (EINVAL); /* Sun returns ENOMEM? */
512 * munmap system call handler
514 * munmap_args(void *addr, size_t len)
519 sys_munmap(struct munmap_args *uap)
521 struct proc *p = curproc;
524 vm_size_t size, pageoff;
527 addr = (vm_offset_t) uap->addr;
530 pageoff = (addr & PAGE_MASK);
533 size = (vm_size_t) round_page(size);
534 if (size < uap->len) /* wrap */
536 tmpaddr = addr + size; /* workaround gcc4 opt */
537 if (tmpaddr < addr) /* wrap */
544 * Check for illegal addresses. Watch out for address wrap... Note
545 * that VM_*_ADDRESS are not constants due to casts (argh).
547 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
549 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
552 map = &p->p_vmspace->vm_map;
554 /* map->token serializes between the map check and the actual unmap */
555 lwkt_gettoken(&map->token);
558 * Make sure entire range is allocated.
560 if (!vm_map_check_protection(map, addr, addr + size,
561 VM_PROT_NONE, FALSE)) {
562 lwkt_reltoken(&map->token);
565 /* returns nothing but KERN_SUCCESS anyway */
566 vm_map_remove(map, addr, addr + size);
567 lwkt_reltoken(&map->token);
572 * mprotect_args(const void *addr, size_t len, int prot)
577 sys_mprotect(struct mprotect_args *uap)
579 struct proc *p = curproc;
582 vm_size_t size, pageoff;
586 addr = (vm_offset_t) uap->addr;
588 prot = uap->prot & VM_PROT_ALL;
590 pageoff = (addr & PAGE_MASK);
593 size = (vm_size_t) round_page(size);
594 if (size < uap->len) /* wrap */
596 tmpaddr = addr + size; /* workaround gcc4 opt */
597 if (tmpaddr < addr) /* wrap */
600 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size,
605 case KERN_PROTECTION_FAILURE:
616 * minherit system call handler
618 * minherit_args(void *addr, size_t len, int inherit)
623 sys_minherit(struct minherit_args *uap)
625 struct proc *p = curproc;
628 vm_size_t size, pageoff;
629 vm_inherit_t inherit;
632 addr = (vm_offset_t)uap->addr;
634 inherit = uap->inherit;
636 pageoff = (addr & PAGE_MASK);
639 size = (vm_size_t) round_page(size);
640 if (size < uap->len) /* wrap */
642 tmpaddr = addr + size; /* workaround gcc4 opt */
643 if (tmpaddr < addr) /* wrap */
646 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr,
647 addr + size, inherit)) {
651 case KERN_PROTECTION_FAILURE:
662 * madvise system call handler
664 * madvise_args(void *addr, size_t len, int behav)
669 sys_madvise(struct madvise_args *uap)
671 struct proc *p = curproc;
672 vm_offset_t start, end;
673 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
677 * Check for illegal behavior
679 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END)
682 * Check for illegal addresses. Watch out for address wrap... Note
683 * that VM_*_ADDRESS are not constants due to casts (argh).
685 if (tmpaddr < (vm_offset_t)uap->addr)
687 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
689 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
693 * Since this routine is only advisory, we default to conservative
696 start = trunc_page((vm_offset_t)uap->addr);
697 end = round_page(tmpaddr);
699 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
705 * mcontrol system call handler
707 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
712 sys_mcontrol(struct mcontrol_args *uap)
714 struct proc *p = curproc;
715 vm_offset_t start, end;
716 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
720 * Check for illegal behavior
722 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
725 * Check for illegal addresses. Watch out for address wrap... Note
726 * that VM_*_ADDRESS are not constants due to casts (argh).
728 if (tmpaddr < (vm_offset_t) uap->addr)
730 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
732 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
736 * Since this routine is only advisory, we default to conservative
739 start = trunc_page((vm_offset_t)uap->addr);
740 end = round_page(tmpaddr);
742 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
743 uap->behav, uap->value);
749 * mincore system call handler
751 * mincore_args(const void *addr, size_t len, char *vec)
756 sys_mincore(struct mincore_args *uap)
758 struct proc *p = curproc;
759 vm_offset_t addr, first_addr;
760 vm_offset_t end, cend;
765 int vecindex, lastvecindex;
766 vm_map_entry_t current;
767 vm_map_entry_t entry;
769 unsigned int timestamp;
772 * Make sure that the addresses presented are valid for user
775 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
776 end = addr + (vm_size_t)round_page(uap->len);
779 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS)
783 * Address of byte vector
787 map = &p->p_vmspace->vm_map;
788 pmap = vmspace_pmap(p->p_vmspace);
790 lwkt_gettoken(&map->token);
791 vm_map_lock_read(map);
793 timestamp = map->timestamp;
795 if (!vm_map_lookup_entry(map, addr, &entry))
799 * Do this on a map entry basis so that if the pages are not
800 * in the current processes address space, we can easily look
801 * up the pages elsewhere.
805 (current != &map->header) && (current->start < end);
806 current = current->next) {
809 * ignore submaps (for now) or null objects
811 if (current->maptype != VM_MAPTYPE_NORMAL &&
812 current->maptype != VM_MAPTYPE_VPAGETABLE) {
815 if (current->object.vm_object == NULL)
819 * limit this scan to the current map entry and the
820 * limits for the mincore call
822 if (addr < current->start)
823 addr = current->start;
829 * scan this entry one page at a time
831 while (addr < cend) {
833 * Check pmap first, it is likely faster, also
834 * it can provide info as to whether we are the
835 * one referencing or modifying the page.
837 * If we have to check the VM object, only mess
838 * around with normal maps. Do not mess around
839 * with virtual page tables (XXX).
841 mincoreinfo = pmap_mincore(pmap, addr);
842 if (mincoreinfo == 0 &&
843 current->maptype == VM_MAPTYPE_NORMAL) {
849 * calculate the page index into the object
851 offset = current->offset + (addr - current->start);
852 pindex = OFF_TO_IDX(offset);
855 * if the page is resident, then gather
856 * information about it. spl protection is
857 * required to maintain the object
858 * association. And XXX what if the page is
859 * busy? What's the deal with that?
861 * XXX vm_token - legacy for pmap_ts_referenced
862 * in x86 and vkernel pmap code.
864 lwkt_gettoken(&vm_token);
865 vm_object_hold(current->object.vm_object);
866 m = vm_page_lookup(current->object.vm_object,
869 mincoreinfo = MINCORE_INCORE;
870 if (m->dirty || pmap_is_modified(m))
871 mincoreinfo |= MINCORE_MODIFIED_OTHER;
872 if ((m->flags & PG_REFERENCED) ||
873 pmap_ts_referenced(m)) {
874 vm_page_flag_set(m, PG_REFERENCED);
875 mincoreinfo |= MINCORE_REFERENCED_OTHER;
878 vm_object_drop(current->object.vm_object);
879 lwkt_reltoken(&vm_token);
883 * subyte may page fault. In case it needs to modify
884 * the map, we release the lock.
886 vm_map_unlock_read(map);
889 * calculate index into user supplied byte vector
891 vecindex = OFF_TO_IDX(addr - first_addr);
894 * If we have skipped map entries, we need to make sure that
895 * the byte vector is zeroed for those skipped entries.
897 while((lastvecindex + 1) < vecindex) {
898 error = subyte( vec + lastvecindex, 0);
907 * Pass the page information to the user
909 error = subyte(vec + vecindex, mincoreinfo);
916 * If the map has changed, due to the subyte,
917 * the previous output may be invalid.
919 vm_map_lock_read(map);
920 if (timestamp != map->timestamp)
923 lastvecindex = vecindex;
929 * subyte may page fault. In case it needs to modify
930 * the map, we release the lock.
932 vm_map_unlock_read(map);
935 * Zero the last entries in the byte vector.
937 vecindex = OFF_TO_IDX(end - first_addr);
938 while((lastvecindex + 1) < vecindex) {
939 error = subyte( vec + lastvecindex, 0);
948 * If the map has changed, due to the subyte, the previous
949 * output may be invalid.
951 vm_map_lock_read(map);
952 if (timestamp != map->timestamp)
954 vm_map_unlock_read(map);
958 lwkt_reltoken(&map->token);
963 * mlock system call handler
965 * mlock_args(const void *addr, size_t len)
970 sys_mlock(struct mlock_args *uap)
974 vm_size_t size, pageoff;
975 struct thread *td = curthread;
976 struct proc *p = td->td_proc;
979 addr = (vm_offset_t) uap->addr;
982 pageoff = (addr & PAGE_MASK);
985 size = (vm_size_t) round_page(size);
986 if (size < uap->len) /* wrap */
988 tmpaddr = addr + size; /* workaround gcc4 opt */
989 if (tmpaddr < addr) /* wrap */
992 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
996 * We do not need to synchronize against other threads updating ucred;
997 * they update p->ucred, which is synchronized into td_ucred ourselves.
999 #ifdef pmap_wired_count
1000 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
1001 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) {
1005 error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1010 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
1011 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1020 sys_mlockall(struct mlockall_args *uap)
1022 struct thread *td = curthread;
1023 struct proc *p = td->td_proc;
1024 vm_map_t map = &p->p_vmspace->vm_map;
1025 vm_map_entry_t entry;
1027 int rc = KERN_SUCCESS;
1029 if (((how & MCL_CURRENT) == 0) && ((how & MCL_FUTURE) == 0))
1032 rc = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1038 if (how & MCL_CURRENT) {
1039 for(entry = map->header.next;
1040 entry != &map->header;
1041 entry = entry->next);
1047 if (how & MCL_FUTURE)
1048 map->flags |= MAP_WIREFUTURE;
1058 * Unwire all user-wired map entries, cancel MCL_FUTURE.
1063 sys_munlockall(struct munlockall_args *uap)
1065 struct thread *td = curthread;
1066 struct proc *p = td->td_proc;
1067 vm_map_t map = &p->p_vmspace->vm_map;
1068 vm_map_entry_t entry;
1069 int rc = KERN_SUCCESS;
1073 /* Clear MAP_WIREFUTURE to cancel mlockall(MCL_FUTURE) */
1074 map->flags &= ~MAP_WIREFUTURE;
1077 for (entry = map->header.next;
1078 entry != &map->header;
1079 entry = entry->next) {
1080 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
1084 * If we encounter an in-transition entry, we release the
1085 * map lock and retry the scan; we do not decrement any
1086 * wired_count more than once because we do not touch
1087 * any entries with MAP_ENTRY_USER_WIRED not set.
1089 * There is a potential interleaving with concurrent
1090 * mlockall()s here -- if we abort a scan, an mlockall()
1091 * could start, wire a number of entries before our
1092 * current position in, and then stall itself on this
1093 * or any other in-transition entry. If that occurs, when
1094 * we resume, we will unwire those entries.
1096 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1097 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1098 ++mycpu->gd_cnt.v_intrans_coll;
1099 ++mycpu->gd_cnt.v_intrans_wait;
1100 vm_map_transition_wait(map, 1);
1104 KASSERT(entry->wired_count > 0,
1105 ("wired_count was 0 with USER_WIRED set! %p", entry));
1107 /* Drop wired count, if it hits zero, unwire the entry */
1108 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1109 entry->wired_count--;
1110 if (entry->wired_count == 0)
1111 vm_fault_unwire(map, entry);
1120 * munlock system call handler
1122 * munlock_args(const void *addr, size_t len)
1127 sys_munlock(struct munlock_args *uap)
1129 struct thread *td = curthread;
1130 struct proc *p = td->td_proc;
1132 vm_offset_t tmpaddr;
1133 vm_size_t size, pageoff;
1136 addr = (vm_offset_t) uap->addr;
1139 pageoff = (addr & PAGE_MASK);
1142 size = (vm_size_t) round_page(size);
1144 tmpaddr = addr + size;
1145 if (tmpaddr < addr) /* wrap */
1148 #ifndef pmap_wired_count
1149 error = priv_check(td, PRIV_ROOT);
1154 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
1155 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1159 * Internal version of mmap.
1160 * Currently used by mmap, exec, and sys5 shared memory.
1161 * Handle is either a vnode pointer or NULL for MAP_ANON.
1166 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1167 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff)
1174 int (*uksmap)(cdev_t dev, vm_page_t fake);
1176 struct thread *td = curthread;
1178 int rv = KERN_SUCCESS;
1186 objsize = round_page(size);
1191 lwkt_gettoken(&map->token);
1194 * XXX messy code, fixme
1196 * NOTE: Overflow checks require discrete statements or GCC4
1197 * will optimize it out.
1199 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) {
1200 esize = map->size + size; /* workaround gcc4 opt */
1201 if (esize < map->size ||
1202 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
1203 lwkt_reltoken(&map->token);
1209 * We currently can only deal with page aligned file offsets.
1210 * The check is here rather than in the syscall because the
1211 * kernel calls this function internally for other mmaping
1212 * operations (such as in exec) and non-aligned offsets will
1213 * cause pmap inconsistencies...so we want to be sure to
1214 * disallow this in all cases.
1216 * NOTE: Overflow checks require discrete statements or GCC4
1217 * will optimize it out.
1219 if (foff & PAGE_MASK) {
1220 lwkt_reltoken(&map->token);
1225 * Handle alignment. For large memory maps it is possible
1226 * that the MMU can optimize the page table so align anything
1227 * that is a multiple of SEG_SIZE to SEG_SIZE.
1229 * Also align any large mapping (bigger than 16x SG_SIZE) to a
1230 * SEG_SIZE address boundary.
1232 if (flags & MAP_SIZEALIGN) {
1234 if ((align ^ (align - 1)) != (align << 1) - 1) {
1235 lwkt_reltoken(&map->token);
1238 } else if ((flags & MAP_FIXED) == 0 &&
1239 ((size & SEG_MASK) == 0 || size > SEG_SIZE * 16)) {
1245 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
1247 *addr = round_page(*addr);
1249 if (*addr != trunc_page(*addr)) {
1250 lwkt_reltoken(&map->token);
1253 eaddr = *addr + size;
1254 if (eaddr < *addr) {
1255 lwkt_reltoken(&map->token);
1259 if ((flags & MAP_TRYFIXED) == 0)
1260 vm_map_remove(map, *addr, *addr + size);
1266 * Lookup/allocate object.
1268 if (flags & MAP_ANON) {
1270 * Unnamed anonymous regions always start at 0.
1274 * Default memory object
1276 object = default_pager_alloc(handle, objsize,
1278 if (object == NULL) {
1279 lwkt_reltoken(&map->token);
1282 docow = MAP_PREFAULT_PARTIAL;
1285 * Implicit single instance of a default memory
1286 * object, so we don't need a VM object yet.
1294 vp = (struct vnode *)handle;
1297 * Non-anonymous mappings of VCHR (aka not /dev/zero)
1298 * cannot specify MAP_STACK or MAP_VPAGETABLE.
1300 if (vp->v_type == VCHR) {
1301 if (flags & (MAP_STACK | MAP_VPAGETABLE)) {
1302 lwkt_reltoken(&map->token);
1307 if (vp->v_type == VCHR && vp->v_rdev->si_ops->d_uksmap) {
1309 * Device mappings without a VM object, typically
1310 * sharing permanently allocated kernel memory or
1311 * process-context-specific (per-process) data.
1313 * Force them to be shared.
1315 uksmap = vp->v_rdev->si_ops->d_uksmap;
1317 docow = MAP_PREFAULT_PARTIAL;
1318 flags &= ~(MAP_PRIVATE|MAP_COPY);
1319 flags |= MAP_SHARED;
1320 } else if (vp->v_type == VCHR) {
1322 * Device mappings (device size unknown?).
1323 * Force them to be shared.
1325 error = dev_dmmap_single(vp->v_rdev, &foff, objsize,
1326 &object, prot, NULL);
1328 if (error == ENODEV) {
1329 handle = (void *)(intptr_t)vp->v_rdev;
1330 object = dev_pager_alloc(handle, objsize, prot, foff);
1331 if (object == NULL) {
1332 lwkt_reltoken(&map->token);
1336 lwkt_reltoken(&map->token);
1340 docow = MAP_PREFAULT_PARTIAL;
1341 flags &= ~(MAP_PRIVATE|MAP_COPY);
1342 flags |= MAP_SHARED;
1345 * Regular file mapping (typically). The attribute
1346 * check is for the link count test only. mmapable
1347 * vnodes must already have a VM object assigned.
1352 error = VOP_GETATTR(vp, &vat);
1354 lwkt_reltoken(&map->token);
1357 docow = MAP_PREFAULT_PARTIAL;
1358 object = vnode_pager_reference(vp);
1359 if (object == NULL && vp->v_type == VREG) {
1360 lwkt_reltoken(&map->token);
1361 kprintf("Warning: cannot mmap vnode %p, no "
1367 * If it is a regular file without any references
1368 * we do not need to sync it.
1370 if (vp->v_type == VREG && vat.va_nlink == 0) {
1371 flags |= MAP_NOSYNC;
1377 * Deal with the adjusted flags
1379 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1380 docow |= MAP_COPY_ON_WRITE;
1381 if (flags & MAP_NOSYNC)
1382 docow |= MAP_DISABLE_SYNCER;
1383 if (flags & MAP_NOCORE)
1384 docow |= MAP_DISABLE_COREDUMP;
1387 * This may place the area in its own page directory if (size) is
1388 * large enough, otherwise it typically returns its argument.
1390 * (object can be NULL)
1393 *addr = pmap_addr_hint(object, *addr, size);
1397 * Stack mappings need special attention.
1399 * Mappings that use virtual page tables will default to storing
1400 * the page table at offset 0.
1403 rv = vm_map_find(map, uksmap, vp->v_rdev,
1406 VM_MAPTYPE_UKSMAP, VM_SUBSYS_MMAP,
1407 prot, maxprot, docow);
1408 } else if (flags & MAP_STACK) {
1409 rv = vm_map_stack(map, *addr, size, flags,
1410 prot, maxprot, docow);
1411 } else if (flags & MAP_VPAGETABLE) {
1412 rv = vm_map_find(map, object, NULL,
1415 VM_MAPTYPE_VPAGETABLE, VM_SUBSYS_MMAP,
1416 prot, maxprot, docow);
1418 rv = vm_map_find(map, object, NULL,
1421 VM_MAPTYPE_NORMAL, VM_SUBSYS_MMAP,
1422 prot, maxprot, docow);
1425 if (rv != KERN_SUCCESS) {
1427 * Lose the object reference. Will destroy the
1428 * object if it's an unnamed anonymous mapping
1429 * or named anonymous without other references.
1431 * (NOTE: object can be NULL)
1433 vm_object_deallocate(object);
1438 * Shared memory is also shared with children.
1440 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1441 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1442 if (rv != KERN_SUCCESS) {
1443 vm_map_remove(map, *addr, *addr + size);
1448 /* If a process has marked all future mappings for wiring, do so */
1449 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1450 vm_map_unwire(map, *addr, *addr + size, FALSE);
1453 * Set the access time on the vnode
1456 vn_mark_atime(vp, td);
1458 lwkt_reltoken(&map->token);
1463 case KERN_INVALID_ADDRESS:
1466 case KERN_PROTECTION_FAILURE:
1474 * Translate a Mach VM return code to zero on success or the appropriate errno
1478 vm_mmap_to_errno(int rv)
1484 case KERN_INVALID_ADDRESS:
1487 case KERN_PROTECTION_FAILURE: