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 * Requires caller to hold 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;
173 ASSERT_LWKT_TOKEN_HELD(&vm_token);
175 addr = (vm_offset_t) uaddr;
177 prot = uprot & VM_PROT_ALL;
182 * Make sure mapping fits into numeric range etc.
184 * NOTE: We support the full unsigned range for size now.
186 if (((flags & MAP_ANON) && (fd != -1 || pos != 0)))
189 if (flags & MAP_STACK) {
191 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
198 * Virtual page tables cannot be used with MAP_STACK. Apart from
199 * it not making any sense, the aux union is used by both
202 * Because the virtual page table is stored in the backing object
203 * and might be updated by the kernel, the mapping must be R+W.
205 if (flags & MAP_VPAGETABLE) {
206 if (vkernel_enable == 0)
208 if (flags & MAP_STACK)
210 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
215 * Align the file position to a page boundary,
216 * and save its page offset component.
218 pageoff = (pos & PAGE_MASK);
221 /* Adjust size for rounding (on both ends). */
222 size += pageoff; /* low end... */
223 size = (vm_size_t) round_page(size); /* hi end */
224 if (size < ulen) /* wrap */
228 * Check for illegal addresses. Watch out for address wrap... Note
229 * that VM_*_ADDRESS are not constants due to casts (argh).
231 if (flags & (MAP_FIXED | MAP_TRYFIXED)) {
233 * The specified address must have the same remainder
234 * as the file offset taken modulo PAGE_SIZE, so it
235 * should be aligned after adjustment by pageoff.
238 if (addr & PAGE_MASK)
242 * Address range must be all in user VM space and not wrap.
244 tmpaddr = addr + size;
247 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
249 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
253 * Set a reasonable start point for the hint if it was
254 * not specified or if it falls within the heap space.
255 * Hinted mmap()s do not allocate out of the heap space.
258 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
259 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz)))
260 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
263 if (flags & MAP_ANON) {
265 * Mapping blank space is trivial.
268 maxprot = VM_PROT_ALL;
271 * Mapping file, get fp for validation. Obtain vnode and make
272 * sure it is of appropriate type.
274 fp = holdfp(p->p_fd, fd, -1);
277 if (fp->f_type != DTYPE_VNODE) {
282 * POSIX shared-memory objects are defined to have
283 * kernel persistence, and are not defined to support
284 * read(2)/write(2) -- or even open(2). Thus, we can
285 * use MAP_ASYNC to trade on-disk coherence for speed.
286 * The shm_open(3) library routine turns on the FPOSIXSHM
287 * flag to request this behavior.
289 if (fp->f_flag & FPOSIXSHM)
291 vp = (struct vnode *) fp->f_data;
294 * Validate the vnode for the operation.
299 * Get the proper underlying object
301 if ((obj = vp->v_object) == NULL) {
305 KKASSERT((struct vnode *)obj->handle == vp);
309 * Make sure a device has not been revoked.
310 * Mappability is handled by the device layer.
312 if (vp->v_rdev == NULL) {
319 * Nothing else is mappable.
326 * XXX hack to handle use of /dev/zero to map anon memory (ala
329 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
331 maxprot = VM_PROT_ALL;
336 * cdevs does not provide private mappings of any kind.
338 if (vp->v_type == VCHR &&
339 (flags & (MAP_PRIVATE|MAP_COPY))) {
344 * Ensure that file and memory protections are
345 * compatible. Note that we only worry about
346 * writability if mapping is shared; in this case,
347 * current and max prot are dictated by the open file.
348 * XXX use the vnode instead? Problem is: what
349 * credentials do we use for determination? What if
350 * proc does a setuid?
352 maxprot = VM_PROT_EXECUTE; /* ??? */
353 if (fp->f_flag & FREAD) {
354 maxprot |= VM_PROT_READ;
355 } else if (prot & PROT_READ) {
360 * If we are sharing potential changes (either via
361 * MAP_SHARED or via the implicit sharing of character
362 * device mappings), and we are trying to get write
363 * permission although we opened it without asking
364 * for it, bail out. Check for superuser, only if
365 * we're at securelevel < 1, to allow the XIG X server
366 * to continue to work.
368 if ((flags & MAP_SHARED) != 0 || vp->v_type == VCHR) {
369 if ((fp->f_flag & FWRITE) != 0) {
371 if ((error = VOP_GETATTR(vp, &va))) {
375 (IMMUTABLE|APPEND)) == 0) {
376 maxprot |= VM_PROT_WRITE;
377 } else if (prot & PROT_WRITE) {
381 } else if ((prot & PROT_WRITE) != 0) {
386 maxprot |= VM_PROT_WRITE;
393 * Do not allow more then a certain number of vm_map_entry structures
394 * per process. Scale with the number of rforks sharing the map
395 * to make the limit reasonable for threads.
398 vms->vm_map.nentries >= max_proc_mmap * vms->vm_sysref.refcnt) {
403 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
406 *res = (void *)(addr + pageoff);
414 * mmap system call handler
419 sys_mmap(struct mmap_args *uap)
423 lwkt_gettoken(&vm_token);
424 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len,
425 uap->prot, uap->flags,
426 uap->fd, uap->pos, &uap->sysmsg_resultp);
427 lwkt_reltoken(&vm_token);
433 * msync system call handler
435 * msync_args(void *addr, size_t len, int flags)
440 sys_msync(struct msync_args *uap)
442 struct proc *p = curproc;
445 vm_size_t size, pageoff;
450 addr = (vm_offset_t) uap->addr;
454 pageoff = (addr & PAGE_MASK);
457 size = (vm_size_t) round_page(size);
458 if (size < uap->len) /* wrap */
460 tmpaddr = addr + size; /* workaround gcc4 opt */
461 if (tmpaddr < addr) /* wrap */
464 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
467 lwkt_gettoken(&vm_token);
468 map = &p->p_vmspace->vm_map;
471 * XXX Gak! If size is zero we are supposed to sync "all modified
472 * pages with the region containing addr". Unfortunately, we don't
473 * really keep track of individual mmaps so we approximate by flushing
474 * the range of the map entry containing addr. This can be incorrect
475 * if the region splits or is coalesced with a neighbor.
478 vm_map_entry_t entry;
480 vm_map_lock_read(map);
481 rv = vm_map_lookup_entry(map, addr, &entry);
483 vm_map_unlock_read(map);
484 rv = KERN_INVALID_ADDRESS;
488 size = entry->end - entry->start;
489 vm_map_unlock_read(map);
493 * Clean the pages and interpret the return value.
495 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
496 (flags & MS_INVALIDATE) != 0);
498 lwkt_reltoken(&vm_token);
503 case KERN_INVALID_ADDRESS:
504 return (EINVAL); /* Sun returns ENOMEM? */
515 * munmap system call handler
517 * munmap_args(void *addr, size_t len)
522 sys_munmap(struct munmap_args *uap)
524 struct proc *p = curproc;
527 vm_size_t size, pageoff;
530 addr = (vm_offset_t) uap->addr;
533 pageoff = (addr & PAGE_MASK);
536 size = (vm_size_t) round_page(size);
537 if (size < uap->len) /* wrap */
539 tmpaddr = addr + size; /* workaround gcc4 opt */
540 if (tmpaddr < addr) /* wrap */
547 * Check for illegal addresses. Watch out for address wrap... Note
548 * that VM_*_ADDRESS are not constants due to casts (argh).
550 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
552 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
555 lwkt_gettoken(&vm_token);
556 map = &p->p_vmspace->vm_map;
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(&vm_token);
565 /* returns nothing but KERN_SUCCESS anyway */
566 vm_map_remove(map, addr, addr + size);
567 lwkt_reltoken(&vm_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;
589 #if defined(VM_PROT_READ_IS_EXEC)
590 if (prot & VM_PROT_READ)
591 prot |= VM_PROT_EXECUTE;
594 pageoff = (addr & PAGE_MASK);
597 size = (vm_size_t) round_page(size);
598 if (size < uap->len) /* wrap */
600 tmpaddr = addr + size; /* workaround gcc4 opt */
601 if (tmpaddr < addr) /* wrap */
604 lwkt_gettoken(&vm_token);
605 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size,
610 case KERN_PROTECTION_FAILURE:
617 lwkt_reltoken(&vm_token);
622 * minherit system call handler
624 * minherit_args(void *addr, size_t len, int inherit)
629 sys_minherit(struct minherit_args *uap)
631 struct proc *p = curproc;
634 vm_size_t size, pageoff;
635 vm_inherit_t inherit;
638 addr = (vm_offset_t)uap->addr;
640 inherit = uap->inherit;
642 pageoff = (addr & PAGE_MASK);
645 size = (vm_size_t) round_page(size);
646 if (size < uap->len) /* wrap */
648 tmpaddr = addr + size; /* workaround gcc4 opt */
649 if (tmpaddr < addr) /* wrap */
652 lwkt_gettoken(&vm_token);
654 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr,
655 addr + size, inherit)) {
659 case KERN_PROTECTION_FAILURE:
666 lwkt_reltoken(&vm_token);
671 * madvise system call handler
673 * madvise_args(void *addr, size_t len, int behav)
678 sys_madvise(struct madvise_args *uap)
680 struct proc *p = curproc;
681 vm_offset_t start, end;
682 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
686 * Check for illegal behavior
688 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END)
691 * Check for illegal addresses. Watch out for address wrap... Note
692 * that VM_*_ADDRESS are not constants due to casts (argh).
694 if (tmpaddr < (vm_offset_t)uap->addr)
696 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
698 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
702 * Since this routine is only advisory, we default to conservative
705 start = trunc_page((vm_offset_t)uap->addr);
706 end = round_page(tmpaddr);
708 lwkt_gettoken(&vm_token);
709 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
711 lwkt_reltoken(&vm_token);
716 * mcontrol system call handler
718 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
723 sys_mcontrol(struct mcontrol_args *uap)
725 struct proc *p = curproc;
726 vm_offset_t start, end;
727 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
731 * Check for illegal behavior
733 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
736 * Check for illegal addresses. Watch out for address wrap... Note
737 * that VM_*_ADDRESS are not constants due to casts (argh).
739 if (tmpaddr < (vm_offset_t) uap->addr)
741 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
743 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
747 * Since this routine is only advisory, we default to conservative
750 start = trunc_page((vm_offset_t)uap->addr);
751 end = round_page(tmpaddr);
753 lwkt_gettoken(&vm_token);
754 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
755 uap->behav, uap->value);
756 lwkt_reltoken(&vm_token);
762 * mincore system call handler
764 * mincore_args(const void *addr, size_t len, char *vec)
769 sys_mincore(struct mincore_args *uap)
771 struct proc *p = curproc;
772 vm_offset_t addr, first_addr;
773 vm_offset_t end, cend;
778 int vecindex, lastvecindex;
779 vm_map_entry_t current;
780 vm_map_entry_t entry;
782 unsigned int timestamp;
785 * Make sure that the addresses presented are valid for user
788 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
789 end = addr + (vm_size_t)round_page(uap->len);
792 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS)
796 * Address of byte vector
800 map = &p->p_vmspace->vm_map;
801 pmap = vmspace_pmap(p->p_vmspace);
803 lwkt_gettoken(&vm_token);
804 vm_map_lock_read(map);
806 timestamp = map->timestamp;
808 if (!vm_map_lookup_entry(map, addr, &entry))
812 * Do this on a map entry basis so that if the pages are not
813 * in the current processes address space, we can easily look
814 * up the pages elsewhere.
818 (current != &map->header) && (current->start < end);
819 current = current->next) {
822 * ignore submaps (for now) or null objects
824 if (current->maptype != VM_MAPTYPE_NORMAL &&
825 current->maptype != VM_MAPTYPE_VPAGETABLE) {
828 if (current->object.vm_object == NULL)
832 * limit this scan to the current map entry and the
833 * limits for the mincore call
835 if (addr < current->start)
836 addr = current->start;
842 * scan this entry one page at a time
844 while (addr < cend) {
846 * Check pmap first, it is likely faster, also
847 * it can provide info as to whether we are the
848 * one referencing or modifying the page.
850 * If we have to check the VM object, only mess
851 * around with normal maps. Do not mess around
852 * with virtual page tables (XXX).
854 mincoreinfo = pmap_mincore(pmap, addr);
855 if (mincoreinfo == 0 &&
856 current->maptype == VM_MAPTYPE_NORMAL) {
862 * calculate the page index into the object
864 offset = current->offset + (addr - current->start);
865 pindex = OFF_TO_IDX(offset);
868 * if the page is resident, then gather
869 * information about it. spl protection is
870 * required to maintain the object
871 * association. And XXX what if the page is
872 * busy? What's the deal with that?
875 m = vm_page_lookup(current->object.vm_object,
878 mincoreinfo = MINCORE_INCORE;
881 mincoreinfo |= MINCORE_MODIFIED_OTHER;
882 if ((m->flags & PG_REFERENCED) ||
883 pmap_ts_referenced(m)) {
884 vm_page_flag_set(m, PG_REFERENCED);
885 mincoreinfo |= MINCORE_REFERENCED_OTHER;
892 * subyte may page fault. In case it needs to modify
893 * the map, we release the lock.
895 vm_map_unlock_read(map);
898 * calculate index into user supplied byte vector
900 vecindex = OFF_TO_IDX(addr - first_addr);
903 * If we have skipped map entries, we need to make sure that
904 * the byte vector is zeroed for those skipped entries.
906 while((lastvecindex + 1) < vecindex) {
907 error = subyte( vec + lastvecindex, 0);
916 * Pass the page information to the user
918 error = subyte( vec + vecindex, mincoreinfo);
925 * If the map has changed, due to the subyte, the previous
926 * output may be invalid.
928 vm_map_lock_read(map);
929 if (timestamp != map->timestamp)
932 lastvecindex = vecindex;
938 * subyte may page fault. In case it needs to modify
939 * the map, we release the lock.
941 vm_map_unlock_read(map);
944 * Zero the last entries in the byte vector.
946 vecindex = OFF_TO_IDX(end - first_addr);
947 while((lastvecindex + 1) < vecindex) {
948 error = subyte( vec + lastvecindex, 0);
957 * If the map has changed, due to the subyte, the previous
958 * output may be invalid.
960 vm_map_lock_read(map);
961 if (timestamp != map->timestamp)
963 vm_map_unlock_read(map);
967 lwkt_reltoken(&vm_token);
972 * mlock system call handler
974 * mlock_args(const void *addr, size_t len)
979 sys_mlock(struct mlock_args *uap)
983 vm_size_t size, pageoff;
984 struct thread *td = curthread;
985 struct proc *p = td->td_proc;
988 addr = (vm_offset_t) uap->addr;
991 pageoff = (addr & PAGE_MASK);
994 size = (vm_size_t) round_page(size);
995 if (size < uap->len) /* wrap */
997 tmpaddr = addr + size; /* workaround gcc4 opt */
998 if (tmpaddr < addr) /* wrap */
1001 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
1004 lwkt_gettoken(&vm_token);
1005 #ifdef pmap_wired_count
1006 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
1007 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) {
1008 lwkt_reltoken(&vm_token);
1012 error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1014 lwkt_reltoken(&vm_token);
1018 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
1019 lwkt_reltoken(&vm_token);
1020 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1024 * mlockall_args(int how)
1026 * Dummy routine, doesn't actually do anything.
1031 sys_mlockall(struct mlockall_args *uap)
1037 * munlockall_args(void)
1039 * Dummy routine, doesn't actually do anything.
1044 sys_munlockall(struct munlockall_args *uap)
1050 * munlock system call handler
1052 * munlock_args(const void *addr, size_t len)
1057 sys_munlock(struct munlock_args *uap)
1059 struct thread *td = curthread;
1060 struct proc *p = td->td_proc;
1062 vm_offset_t tmpaddr;
1063 vm_size_t size, pageoff;
1066 addr = (vm_offset_t) uap->addr;
1069 pageoff = (addr & PAGE_MASK);
1072 size = (vm_size_t) round_page(size);
1074 tmpaddr = addr + size;
1075 if (tmpaddr < addr) /* wrap */
1078 #ifndef pmap_wired_count
1079 error = priv_check(td, PRIV_ROOT);
1084 lwkt_gettoken(&vm_token);
1085 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
1086 lwkt_reltoken(&vm_token);
1087 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1091 * Internal version of mmap.
1092 * Currently used by mmap, exec, and sys5 shared memory.
1093 * Handle is either a vnode pointer or NULL for MAP_ANON.
1095 * No requirements; kern_mmap path holds the vm_token
1098 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1099 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff)
1106 struct thread *td = curthread;
1108 int rv = KERN_SUCCESS;
1115 objsize = round_page(size);
1120 lwkt_gettoken(&vm_token);
1123 * XXX messy code, fixme
1125 * NOTE: Overflow checks require discrete statements or GCC4
1126 * will optimize it out.
1128 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) {
1129 esize = map->size + size; /* workaround gcc4 opt */
1130 if (esize < map->size ||
1131 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
1132 lwkt_reltoken(&vm_token);
1138 * We currently can only deal with page aligned file offsets.
1139 * The check is here rather than in the syscall because the
1140 * kernel calls this function internally for other mmaping
1141 * operations (such as in exec) and non-aligned offsets will
1142 * cause pmap inconsistencies...so we want to be sure to
1143 * disallow this in all cases.
1145 * NOTE: Overflow checks require discrete statements or GCC4
1146 * will optimize it out.
1148 if (foff & PAGE_MASK) {
1149 lwkt_reltoken(&vm_token);
1153 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
1155 *addr = round_page(*addr);
1157 if (*addr != trunc_page(*addr)) {
1158 lwkt_reltoken(&vm_token);
1161 eaddr = *addr + size;
1162 if (eaddr < *addr) {
1163 lwkt_reltoken(&vm_token);
1167 if ((flags & MAP_TRYFIXED) == 0)
1168 vm_map_remove(map, *addr, *addr + size);
1172 * Lookup/allocate object.
1174 if (flags & MAP_ANON) {
1176 * Unnamed anonymous regions always start at 0.
1180 * Default memory object
1182 object = default_pager_alloc(handle, objsize,
1184 if (object == NULL) {
1185 lwkt_reltoken(&vm_token);
1188 docow = MAP_PREFAULT_PARTIAL;
1191 * Implicit single instance of a default memory
1192 * object, so we don't need a VM object yet.
1200 vp = (struct vnode *)handle;
1201 if (vp->v_type == VCHR) {
1203 * Device mappings (device size unknown?).
1204 * Force them to be shared.
1206 handle = (void *)(intptr_t)vp->v_rdev;
1207 object = dev_pager_alloc(handle, objsize, prot, foff);
1208 if (object == NULL) {
1209 lwkt_reltoken(&vm_token);
1212 docow = MAP_PREFAULT_PARTIAL;
1213 flags &= ~(MAP_PRIVATE|MAP_COPY);
1214 flags |= MAP_SHARED;
1217 * Regular file mapping (typically). The attribute
1218 * check is for the link count test only. Mmapble
1219 * vnodes must already have a VM object assigned.
1224 error = VOP_GETATTR(vp, &vat);
1226 lwkt_reltoken(&vm_token);
1229 docow = MAP_PREFAULT_PARTIAL;
1230 object = vnode_pager_reference(vp);
1231 if (object == NULL && vp->v_type == VREG) {
1232 lwkt_reltoken(&vm_token);
1233 kprintf("Warning: cannot mmap vnode %p, no "
1239 * If it is a regular file without any references
1240 * we do not need to sync it.
1242 if (vp->v_type == VREG && vat.va_nlink == 0) {
1243 flags |= MAP_NOSYNC;
1249 * Deal with the adjusted flags
1251 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1252 docow |= MAP_COPY_ON_WRITE;
1253 if (flags & MAP_NOSYNC)
1254 docow |= MAP_DISABLE_SYNCER;
1255 if (flags & MAP_NOCORE)
1256 docow |= MAP_DISABLE_COREDUMP;
1258 #if defined(VM_PROT_READ_IS_EXEC)
1259 if (prot & VM_PROT_READ)
1260 prot |= VM_PROT_EXECUTE;
1262 if (maxprot & VM_PROT_READ)
1263 maxprot |= VM_PROT_EXECUTE;
1267 * This may place the area in its own page directory if (size) is
1268 * large enough, otherwise it typically returns its argument.
1271 *addr = pmap_addr_hint(object, *addr, size);
1275 * Stack mappings need special attention.
1277 * Mappings that use virtual page tables will default to storing
1278 * the page table at offset 0.
1280 if (flags & MAP_STACK) {
1281 rv = vm_map_stack(map, *addr, size, flags,
1282 prot, maxprot, docow);
1283 } else if (flags & MAP_VPAGETABLE) {
1284 rv = vm_map_find(map, object, foff, addr, size, PAGE_SIZE,
1285 fitit, VM_MAPTYPE_VPAGETABLE,
1286 prot, maxprot, docow);
1288 rv = vm_map_find(map, object, foff, addr, size, PAGE_SIZE,
1289 fitit, VM_MAPTYPE_NORMAL,
1290 prot, maxprot, docow);
1293 if (rv != KERN_SUCCESS) {
1295 * Lose the object reference. Will destroy the
1296 * object if it's an unnamed anonymous mapping
1297 * or named anonymous without other references.
1299 vm_object_deallocate(object);
1304 * Shared memory is also shared with children.
1306 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1307 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1308 if (rv != KERN_SUCCESS) {
1309 vm_map_remove(map, *addr, *addr + size);
1315 * Set the access time on the vnode
1318 vn_mark_atime(vp, td);
1320 lwkt_reltoken(&vm_token);
1325 case KERN_INVALID_ADDRESS:
1328 case KERN_PROTECTION_FAILURE: