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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
42 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
43 * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $
44 * $DragonFly: src/sys/vm/vm_mmap.c,v 1.39 2007/04/30 07:18:57 dillon Exp $
48 * Mapped file (mmap) interface to VM
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
54 #include <sys/sysproto.h>
55 #include <sys/filedesc.h>
56 #include <sys/kern_syscall.h>
59 #include <sys/resource.h>
60 #include <sys/resourcevar.h>
61 #include <sys/vnode.h>
62 #include <sys/fcntl.h>
67 #include <sys/vmmeter.h>
68 #include <sys/sysctl.h>
71 #include <vm/vm_param.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_pageout.h>
79 #include <vm/vm_extern.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_kern.h>
83 #include <sys/file2.h>
84 #include <sys/thread.h>
85 #include <sys/thread2.h>
87 static int max_proc_mmap;
88 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
90 SYSCTL_INT(_vm, OID_AUTO, vkernel_enable, CTLFLAG_RW, &vkernel_enable, 0, "");
93 * Set the maximum number of vm_map_entry structures per process. Roughly
94 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
95 * of our KVM malloc space still results in generous limits. We want a
96 * default that is good enough to prevent the kernel running out of resources
97 * if attacked from compromised user account but generous enough such that
98 * multi-threaded processes are not unduly inconvenienced.
101 static void vmmapentry_rsrc_init (void *);
102 SYSINIT(vmmersrc, SI_BOOT1_POST, SI_ORDER_ANY, vmmapentry_rsrc_init, NULL)
105 vmmapentry_rsrc_init(void *dummy)
107 max_proc_mmap = KvaSize / sizeof(struct vm_map_entry);
108 max_proc_mmap /= 100;
115 sys_sbrk(struct sbrk_args *uap)
117 /* Not yet implemented */
122 * sstk_args(int incr)
127 sys_sstk(struct sstk_args *uap)
129 /* Not yet implemented */
134 * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
135 * long pad, off_t pos)
137 * Memory Map (mmap) system call. Note that the file offset
138 * and address are allowed to be NOT page aligned, though if
139 * the MAP_FIXED flag it set, both must have the same remainder
140 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
141 * page-aligned, the actual mapping starts at trunc_page(addr)
142 * and the return value is adjusted up by the page offset.
144 * Generally speaking, only character devices which are themselves
145 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
146 * there would be no cache coherency between a descriptor and a VM mapping
147 * both to the same character device.
149 * Block devices can be mmap'd no matter what they represent. Cache coherency
150 * is maintained as long as you do not write directly to the underlying
153 * No requirements; sys_mmap path holds the vm_token
156 kern_mmap(struct vmspace *vms, caddr_t uaddr, size_t ulen,
157 int uprot, int uflags, int fd, off_t upos, void **res)
159 struct thread *td = curthread;
160 struct proc *p = td->td_proc;
161 struct file *fp = NULL;
165 vm_size_t size, pageoff;
166 vm_prot_t prot, maxprot;
174 addr = (vm_offset_t) uaddr;
176 prot = uprot & VM_PROT_ALL;
181 * Make sure mapping fits into numeric range etc.
183 * NOTE: We support the full unsigned range for size now.
185 if (((flags & MAP_ANON) && (fd != -1 || pos != 0)))
188 if (flags & MAP_STACK) {
190 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
197 * Virtual page tables cannot be used with MAP_STACK. Apart from
198 * it not making any sense, the aux union is used by both
201 * Because the virtual page table is stored in the backing object
202 * and might be updated by the kernel, the mapping must be R+W.
204 if (flags & MAP_VPAGETABLE) {
205 if (vkernel_enable == 0)
207 if (flags & MAP_STACK)
209 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
214 * Align the file position to a page boundary,
215 * and save its page offset component.
217 pageoff = (pos & PAGE_MASK);
220 /* Adjust size for rounding (on both ends). */
221 size += pageoff; /* low end... */
222 size = (vm_size_t) round_page(size); /* hi end */
223 if (size < ulen) /* wrap */
227 * Check for illegal addresses. Watch out for address wrap... Note
228 * that VM_*_ADDRESS are not constants due to casts (argh).
230 if (flags & (MAP_FIXED | MAP_TRYFIXED)) {
232 * The specified address must have the same remainder
233 * as the file offset taken modulo PAGE_SIZE, so it
234 * should be aligned after adjustment by pageoff.
237 if (addr & PAGE_MASK)
241 * Address range must be all in user VM space and not wrap.
243 tmpaddr = addr + size;
246 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
248 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
252 * Set a reasonable start point for the hint if it was
253 * not specified or if it falls within the heap space.
254 * Hinted mmap()s do not allocate out of the heap space.
257 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
258 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz)))
259 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
262 if (flags & MAP_ANON) {
264 * Mapping blank space is trivial.
267 maxprot = VM_PROT_ALL;
270 * Mapping file, get fp for validation. Obtain vnode and make
271 * sure it is of appropriate type.
273 fp = holdfp(p->p_fd, fd, -1);
276 if (fp->f_type != DTYPE_VNODE) {
281 * POSIX shared-memory objects are defined to have
282 * kernel persistence, and are not defined to support
283 * read(2)/write(2) -- or even open(2). Thus, we can
284 * use MAP_ASYNC to trade on-disk coherence for speed.
285 * The shm_open(3) library routine turns on the FPOSIXSHM
286 * flag to request this behavior.
288 if (fp->f_flag & FPOSIXSHM)
290 vp = (struct vnode *) fp->f_data;
293 * Validate the vnode for the operation.
298 * Get the proper underlying object
300 if ((obj = vp->v_object) == NULL) {
304 KKASSERT((struct vnode *)obj->handle == vp);
308 * Make sure a device has not been revoked.
309 * Mappability is handled by the device layer.
311 if (vp->v_rdev == NULL) {
318 * Nothing else is mappable.
325 * XXX hack to handle use of /dev/zero to map anon memory (ala
328 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
330 maxprot = VM_PROT_ALL;
335 * cdevs does not provide private mappings of any kind.
337 if (vp->v_type == VCHR &&
338 (flags & (MAP_PRIVATE|MAP_COPY))) {
343 * Ensure that file and memory protections are
344 * compatible. Note that we only worry about
345 * writability if mapping is shared; in this case,
346 * current and max prot are dictated by the open file.
347 * XXX use the vnode instead? Problem is: what
348 * credentials do we use for determination? What if
349 * proc does a setuid?
351 maxprot = VM_PROT_EXECUTE; /* ??? */
352 if (fp->f_flag & FREAD) {
353 maxprot |= VM_PROT_READ;
354 } else if (prot & PROT_READ) {
359 * If we are sharing potential changes (either via
360 * MAP_SHARED or via the implicit sharing of character
361 * device mappings), and we are trying to get write
362 * permission although we opened it without asking
363 * for it, bail out. Check for superuser, only if
364 * we're at securelevel < 1, to allow the XIG X server
365 * to continue to work.
367 if ((flags & MAP_SHARED) != 0 || vp->v_type == VCHR) {
368 if ((fp->f_flag & FWRITE) != 0) {
370 if ((error = VOP_GETATTR(vp, &va))) {
374 (IMMUTABLE|APPEND)) == 0) {
375 maxprot |= VM_PROT_WRITE;
376 } else if (prot & PROT_WRITE) {
380 } else if ((prot & PROT_WRITE) != 0) {
385 maxprot |= VM_PROT_WRITE;
391 /* Token serializes access to vm_map.nentries against vm_mmap */
392 lwkt_gettoken(&vm_token);
395 * Do not allow more then a certain number of vm_map_entry structures
396 * per process. Scale with the number of rforks sharing the map
397 * to make the limit reasonable for threads.
400 vms->vm_map.nentries >= max_proc_mmap * vms->vm_sysref.refcnt) {
402 lwkt_reltoken(&vm_token);
406 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
409 *res = (void *)(addr + pageoff);
411 lwkt_reltoken(&vm_token);
420 * mmap system call handler
425 sys_mmap(struct mmap_args *uap)
429 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len,
430 uap->prot, uap->flags,
431 uap->fd, uap->pos, &uap->sysmsg_resultp);
437 * msync system call handler
439 * msync_args(void *addr, size_t len, int flags)
444 sys_msync(struct msync_args *uap)
446 struct proc *p = curproc;
449 vm_size_t size, pageoff;
454 addr = (vm_offset_t) uap->addr;
458 pageoff = (addr & PAGE_MASK);
461 size = (vm_size_t) round_page(size);
462 if (size < uap->len) /* wrap */
464 tmpaddr = addr + size; /* workaround gcc4 opt */
465 if (tmpaddr < addr) /* wrap */
468 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
471 map = &p->p_vmspace->vm_map;
474 * vm_token serializes extracting the address range for size == 0
475 * msyncs with the vm_map_clean call; if the token were not held
476 * across the two calls, an intervening munmap/mmap pair, for example,
477 * could cause msync to occur on a wrong region.
479 lwkt_gettoken(&vm_token);
482 * XXX Gak! If size is zero we are supposed to sync "all modified
483 * pages with the region containing addr". Unfortunately, we don't
484 * really keep track of individual mmaps so we approximate by flushing
485 * the range of the map entry containing addr. This can be incorrect
486 * if the region splits or is coalesced with a neighbor.
489 vm_map_entry_t entry;
491 vm_map_lock_read(map);
492 rv = vm_map_lookup_entry(map, addr, &entry);
494 vm_map_unlock_read(map);
495 rv = KERN_INVALID_ADDRESS;
499 size = entry->end - entry->start;
500 vm_map_unlock_read(map);
504 * Clean the pages and interpret the return value.
506 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
507 (flags & MS_INVALIDATE) != 0);
509 lwkt_reltoken(&vm_token);
514 case KERN_INVALID_ADDRESS:
515 return (EINVAL); /* Sun returns ENOMEM? */
526 * munmap system call handler
528 * munmap_args(void *addr, size_t len)
533 sys_munmap(struct munmap_args *uap)
535 struct proc *p = curproc;
538 vm_size_t size, pageoff;
541 addr = (vm_offset_t) uap->addr;
544 pageoff = (addr & PAGE_MASK);
547 size = (vm_size_t) round_page(size);
548 if (size < uap->len) /* wrap */
550 tmpaddr = addr + size; /* workaround gcc4 opt */
551 if (tmpaddr < addr) /* wrap */
558 * Check for illegal addresses. Watch out for address wrap... Note
559 * that VM_*_ADDRESS are not constants due to casts (argh).
561 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
563 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
566 map = &p->p_vmspace->vm_map;
568 /* vm_token serializes between the map check and the actual unmap */
569 lwkt_gettoken(&vm_token);
572 * Make sure entire range is allocated.
574 if (!vm_map_check_protection(map, addr, addr + size,
575 VM_PROT_NONE, FALSE)) {
576 lwkt_reltoken(&vm_token);
579 /* returns nothing but KERN_SUCCESS anyway */
580 vm_map_remove(map, addr, addr + size);
581 lwkt_reltoken(&vm_token);
586 * mprotect_args(const void *addr, size_t len, int prot)
591 sys_mprotect(struct mprotect_args *uap)
593 struct proc *p = curproc;
596 vm_size_t size, pageoff;
600 addr = (vm_offset_t) uap->addr;
602 prot = uap->prot & VM_PROT_ALL;
603 #if defined(VM_PROT_READ_IS_EXEC)
604 if (prot & VM_PROT_READ)
605 prot |= VM_PROT_EXECUTE;
608 pageoff = (addr & PAGE_MASK);
611 size = (vm_size_t) round_page(size);
612 if (size < uap->len) /* wrap */
614 tmpaddr = addr + size; /* workaround gcc4 opt */
615 if (tmpaddr < addr) /* wrap */
618 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size,
623 case KERN_PROTECTION_FAILURE:
634 * minherit system call handler
636 * minherit_args(void *addr, size_t len, int inherit)
641 sys_minherit(struct minherit_args *uap)
643 struct proc *p = curproc;
646 vm_size_t size, pageoff;
647 vm_inherit_t inherit;
650 addr = (vm_offset_t)uap->addr;
652 inherit = uap->inherit;
654 pageoff = (addr & PAGE_MASK);
657 size = (vm_size_t) round_page(size);
658 if (size < uap->len) /* wrap */
660 tmpaddr = addr + size; /* workaround gcc4 opt */
661 if (tmpaddr < addr) /* wrap */
664 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr,
665 addr + size, inherit)) {
669 case KERN_PROTECTION_FAILURE:
680 * madvise system call handler
682 * madvise_args(void *addr, size_t len, int behav)
687 sys_madvise(struct madvise_args *uap)
689 struct proc *p = curproc;
690 vm_offset_t start, end;
691 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
695 * Check for illegal behavior
697 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END)
700 * Check for illegal addresses. Watch out for address wrap... Note
701 * that VM_*_ADDRESS are not constants due to casts (argh).
703 if (tmpaddr < (vm_offset_t)uap->addr)
705 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
707 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
711 * Since this routine is only advisory, we default to conservative
714 start = trunc_page((vm_offset_t)uap->addr);
715 end = round_page(tmpaddr);
717 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
723 * mcontrol system call handler
725 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
730 sys_mcontrol(struct mcontrol_args *uap)
732 struct proc *p = curproc;
733 vm_offset_t start, end;
734 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
738 * Check for illegal behavior
740 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
743 * Check for illegal addresses. Watch out for address wrap... Note
744 * that VM_*_ADDRESS are not constants due to casts (argh).
746 if (tmpaddr < (vm_offset_t) uap->addr)
748 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
750 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
754 * Since this routine is only advisory, we default to conservative
757 start = trunc_page((vm_offset_t)uap->addr);
758 end = round_page(tmpaddr);
760 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
761 uap->behav, uap->value);
767 * mincore system call handler
769 * mincore_args(const void *addr, size_t len, char *vec)
774 sys_mincore(struct mincore_args *uap)
776 struct proc *p = curproc;
777 vm_offset_t addr, first_addr;
778 vm_offset_t end, cend;
783 int vecindex, lastvecindex;
784 vm_map_entry_t current;
785 vm_map_entry_t entry;
787 unsigned int timestamp;
790 * Make sure that the addresses presented are valid for user
793 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
794 end = addr + (vm_size_t)round_page(uap->len);
797 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS)
801 * Address of byte vector
805 map = &p->p_vmspace->vm_map;
806 pmap = vmspace_pmap(p->p_vmspace);
808 lwkt_gettoken(&vm_token);
809 vm_map_lock_read(map);
811 timestamp = map->timestamp;
813 if (!vm_map_lookup_entry(map, addr, &entry))
817 * Do this on a map entry basis so that if the pages are not
818 * in the current processes address space, we can easily look
819 * up the pages elsewhere.
823 (current != &map->header) && (current->start < end);
824 current = current->next) {
827 * ignore submaps (for now) or null objects
829 if (current->maptype != VM_MAPTYPE_NORMAL &&
830 current->maptype != VM_MAPTYPE_VPAGETABLE) {
833 if (current->object.vm_object == NULL)
837 * limit this scan to the current map entry and the
838 * limits for the mincore call
840 if (addr < current->start)
841 addr = current->start;
847 * scan this entry one page at a time
849 while (addr < cend) {
851 * Check pmap first, it is likely faster, also
852 * it can provide info as to whether we are the
853 * one referencing or modifying the page.
855 * If we have to check the VM object, only mess
856 * around with normal maps. Do not mess around
857 * with virtual page tables (XXX).
859 mincoreinfo = pmap_mincore(pmap, addr);
860 if (mincoreinfo == 0 &&
861 current->maptype == VM_MAPTYPE_NORMAL) {
867 * calculate the page index into the object
869 offset = current->offset + (addr - current->start);
870 pindex = OFF_TO_IDX(offset);
873 * if the page is resident, then gather
874 * information about it. spl protection is
875 * required to maintain the object
876 * association. And XXX what if the page is
877 * busy? What's the deal with that?
880 m = vm_page_lookup(current->object.vm_object,
883 mincoreinfo = MINCORE_INCORE;
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;
897 * subyte may page fault. In case it needs to modify
898 * the map, we release the lock.
900 vm_map_unlock_read(map);
903 * calculate index into user supplied byte vector
905 vecindex = OFF_TO_IDX(addr - first_addr);
908 * If we have skipped map entries, we need to make sure that
909 * the byte vector is zeroed for those skipped entries.
911 while((lastvecindex + 1) < vecindex) {
912 error = subyte( vec + lastvecindex, 0);
921 * Pass the page information to the user
923 error = subyte( vec + vecindex, mincoreinfo);
930 * If the map has changed, due to the subyte, the previous
931 * output may be invalid.
933 vm_map_lock_read(map);
934 if (timestamp != map->timestamp)
937 lastvecindex = vecindex;
943 * subyte may page fault. In case it needs to modify
944 * the map, we release the lock.
946 vm_map_unlock_read(map);
949 * Zero the last entries in the byte vector.
951 vecindex = OFF_TO_IDX(end - first_addr);
952 while((lastvecindex + 1) < vecindex) {
953 error = subyte( vec + lastvecindex, 0);
962 * If the map has changed, due to the subyte, the previous
963 * output may be invalid.
965 vm_map_lock_read(map);
966 if (timestamp != map->timestamp)
968 vm_map_unlock_read(map);
972 lwkt_reltoken(&vm_token);
977 * mlock system call handler
979 * mlock_args(const void *addr, size_t len)
984 sys_mlock(struct mlock_args *uap)
988 vm_size_t size, pageoff;
989 struct thread *td = curthread;
990 struct proc *p = td->td_proc;
993 addr = (vm_offset_t) uap->addr;
996 pageoff = (addr & PAGE_MASK);
999 size = (vm_size_t) round_page(size);
1000 if (size < uap->len) /* wrap */
1002 tmpaddr = addr + size; /* workaround gcc4 opt */
1003 if (tmpaddr < addr) /* wrap */
1006 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
1010 * We do not need to synchronize against other threads updating ucred;
1011 * they update p->ucred, which is synchronized into td_ucred ourselves.
1013 #ifdef pmap_wired_count
1014 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
1015 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) {
1019 error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1024 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
1025 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1029 * mlockall_args(int how)
1031 * Dummy routine, doesn't actually do anything.
1036 sys_mlockall(struct mlockall_args *uap)
1044 * Unwire all user-wired map entries, cancel MCL_FUTURE.
1049 sys_munlockall(struct munlockall_args *uap)
1051 struct thread *td = curthread;
1052 struct proc *p = td->td_proc;
1053 vm_map_t map = &p->p_vmspace->vm_map;
1054 vm_map_entry_t entry;
1055 int rc = KERN_SUCCESS;
1059 /* Clear MAP_WIREFUTURE to cancel mlockall(MCL_FUTURE) */
1060 map->flags &= ~MAP_WIREFUTURE;
1063 for (entry = map->header.next;
1064 entry != &map->header;
1065 entry = entry->next) {
1066 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
1070 * If we encounter an in-transition entry, we release the
1071 * map lock and retry the scan; we do not decrement any
1072 * wired_count more than once because we do not touch
1073 * any entries with MAP_ENTRY_USER_WIRED not set.
1075 * There is a potential interleaving with concurrent
1076 * mlockall()s here -- if we abort a scan, an mlockall()
1077 * could start, wire a number of entries before our
1078 * current position in, and then stall itself on this
1079 * or any other in-transition entry. If that occurs, when
1080 * we resume, we will unwire those entries.
1082 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1083 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1084 ++mycpu->gd_cnt.v_intrans_coll;
1085 ++mycpu->gd_cnt.v_intrans_wait;
1086 vm_map_transition_wait(map);
1090 KASSERT(entry->wired_count > 0,
1091 ("wired_count was 0 with USER_WIRED set! %p", entry));
1093 /* Drop wired count, if it hits zero, unwire the entry */
1094 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1095 entry->wired_count--;
1096 if (entry->wired_count == 0)
1097 vm_fault_unwire(map, entry);
1107 * munlock system call handler
1109 * munlock_args(const void *addr, size_t len)
1114 sys_munlock(struct munlock_args *uap)
1116 struct thread *td = curthread;
1117 struct proc *p = td->td_proc;
1119 vm_offset_t tmpaddr;
1120 vm_size_t size, pageoff;
1123 addr = (vm_offset_t) uap->addr;
1126 pageoff = (addr & PAGE_MASK);
1129 size = (vm_size_t) round_page(size);
1131 tmpaddr = addr + size;
1132 if (tmpaddr < addr) /* wrap */
1135 #ifndef pmap_wired_count
1136 error = priv_check(td, PRIV_ROOT);
1141 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
1142 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1146 * Internal version of mmap.
1147 * Currently used by mmap, exec, and sys5 shared memory.
1148 * Handle is either a vnode pointer or NULL for MAP_ANON.
1150 * No requirements; kern_mmap path holds the vm_token
1153 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1154 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff)
1161 struct thread *td = curthread;
1163 int rv = KERN_SUCCESS;
1170 objsize = round_page(size);
1175 lwkt_gettoken(&vm_token);
1178 * XXX messy code, fixme
1180 * NOTE: Overflow checks require discrete statements or GCC4
1181 * will optimize it out.
1183 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) {
1184 esize = map->size + size; /* workaround gcc4 opt */
1185 if (esize < map->size ||
1186 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
1187 lwkt_reltoken(&vm_token);
1193 * We currently can only deal with page aligned file offsets.
1194 * The check is here rather than in the syscall because the
1195 * kernel calls this function internally for other mmaping
1196 * operations (such as in exec) and non-aligned offsets will
1197 * cause pmap inconsistencies...so we want to be sure to
1198 * disallow this in all cases.
1200 * NOTE: Overflow checks require discrete statements or GCC4
1201 * will optimize it out.
1203 if (foff & PAGE_MASK) {
1204 lwkt_reltoken(&vm_token);
1208 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
1210 *addr = round_page(*addr);
1212 if (*addr != trunc_page(*addr)) {
1213 lwkt_reltoken(&vm_token);
1216 eaddr = *addr + size;
1217 if (eaddr < *addr) {
1218 lwkt_reltoken(&vm_token);
1222 if ((flags & MAP_TRYFIXED) == 0)
1223 vm_map_remove(map, *addr, *addr + size);
1227 * Lookup/allocate object.
1229 if (flags & MAP_ANON) {
1231 * Unnamed anonymous regions always start at 0.
1235 * Default memory object
1237 object = default_pager_alloc(handle, objsize,
1239 if (object == NULL) {
1240 lwkt_reltoken(&vm_token);
1243 docow = MAP_PREFAULT_PARTIAL;
1246 * Implicit single instance of a default memory
1247 * object, so we don't need a VM object yet.
1255 vp = (struct vnode *)handle;
1256 if (vp->v_type == VCHR) {
1258 * Device mappings (device size unknown?).
1259 * Force them to be shared.
1261 handle = (void *)(intptr_t)vp->v_rdev;
1262 object = dev_pager_alloc(handle, objsize, prot, foff);
1263 if (object == NULL) {
1264 lwkt_reltoken(&vm_token);
1267 docow = MAP_PREFAULT_PARTIAL;
1268 flags &= ~(MAP_PRIVATE|MAP_COPY);
1269 flags |= MAP_SHARED;
1272 * Regular file mapping (typically). The attribute
1273 * check is for the link count test only. Mmapble
1274 * vnodes must already have a VM object assigned.
1279 error = VOP_GETATTR(vp, &vat);
1281 lwkt_reltoken(&vm_token);
1284 docow = MAP_PREFAULT_PARTIAL;
1285 object = vnode_pager_reference(vp);
1286 if (object == NULL && vp->v_type == VREG) {
1287 lwkt_reltoken(&vm_token);
1288 kprintf("Warning: cannot mmap vnode %p, no "
1294 * If it is a regular file without any references
1295 * we do not need to sync it.
1297 if (vp->v_type == VREG && vat.va_nlink == 0) {
1298 flags |= MAP_NOSYNC;
1304 * Deal with the adjusted flags
1306 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1307 docow |= MAP_COPY_ON_WRITE;
1308 if (flags & MAP_NOSYNC)
1309 docow |= MAP_DISABLE_SYNCER;
1310 if (flags & MAP_NOCORE)
1311 docow |= MAP_DISABLE_COREDUMP;
1313 #if defined(VM_PROT_READ_IS_EXEC)
1314 if (prot & VM_PROT_READ)
1315 prot |= VM_PROT_EXECUTE;
1317 if (maxprot & VM_PROT_READ)
1318 maxprot |= VM_PROT_EXECUTE;
1322 * This may place the area in its own page directory if (size) is
1323 * large enough, otherwise it typically returns its argument.
1326 *addr = pmap_addr_hint(object, *addr, size);
1330 * Stack mappings need special attention.
1332 * Mappings that use virtual page tables will default to storing
1333 * the page table at offset 0.
1335 if (flags & MAP_STACK) {
1336 rv = vm_map_stack(map, *addr, size, flags,
1337 prot, maxprot, docow);
1338 } else if (flags & MAP_VPAGETABLE) {
1339 rv = vm_map_find(map, object, foff, addr, size, PAGE_SIZE,
1340 fitit, VM_MAPTYPE_VPAGETABLE,
1341 prot, maxprot, docow);
1343 rv = vm_map_find(map, object, foff, addr, size, PAGE_SIZE,
1344 fitit, VM_MAPTYPE_NORMAL,
1345 prot, maxprot, docow);
1348 if (rv != KERN_SUCCESS) {
1350 * Lose the object reference. Will destroy the
1351 * object if it's an unnamed anonymous mapping
1352 * or named anonymous without other references.
1354 vm_object_deallocate(object);
1359 * Shared memory is also shared with children.
1361 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1362 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1363 if (rv != KERN_SUCCESS) {
1364 vm_map_remove(map, *addr, *addr + size);
1370 * Set the access time on the vnode
1373 vn_mark_atime(vp, td);
1375 lwkt_reltoken(&vm_token);
1380 case KERN_INVALID_ADDRESS:
1383 case KERN_PROTECTION_FAILURE: