2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
40 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
41 * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $
42 * $DragonFly: src/sys/vm/vm_mmap.c,v 1.39 2007/04/30 07:18:57 dillon Exp $
46 * Mapped file (mmap) interface to VM
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/sysproto.h>
53 #include <sys/filedesc.h>
54 #include <sys/kern_syscall.h>
57 #include <sys/resource.h>
58 #include <sys/resourcevar.h>
59 #include <sys/vnode.h>
60 #include <sys/fcntl.h>
65 #include <sys/vmmeter.h>
66 #include <sys/sysctl.h>
69 #include <vm/vm_param.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_pager.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_kern.h>
81 #include <sys/file2.h>
82 #include <sys/thread.h>
83 #include <sys/thread2.h>
85 static int max_proc_mmap;
86 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
88 SYSCTL_INT(_vm, OID_AUTO, vkernel_enable, CTLFLAG_RW, &vkernel_enable, 0, "");
91 * Set the maximum number of vm_map_entry structures per process. Roughly
92 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
93 * of our KVM malloc space still results in generous limits. We want a
94 * default that is good enough to prevent the kernel running out of resources
95 * if attacked from compromised user account but generous enough such that
96 * multi-threaded processes are not unduly inconvenienced.
99 static void vmmapentry_rsrc_init (void *);
100 SYSINIT(vmmersrc, SI_BOOT1_POST, SI_ORDER_ANY, vmmapentry_rsrc_init, NULL)
103 vmmapentry_rsrc_init(void *dummy)
105 max_proc_mmap = KvaSize / sizeof(struct vm_map_entry);
106 max_proc_mmap /= 100;
113 sys_sbrk(struct sbrk_args *uap)
115 /* Not yet implemented */
120 * sstk_args(int incr)
125 sys_sstk(struct sstk_args *uap)
127 /* Not yet implemented */
132 * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
133 * long pad, off_t pos)
135 * Memory Map (mmap) system call. Note that the file offset
136 * and address are allowed to be NOT page aligned, though if
137 * the MAP_FIXED flag it set, both must have the same remainder
138 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
139 * page-aligned, the actual mapping starts at trunc_page(addr)
140 * and the return value is adjusted up by the page offset.
142 * Generally speaking, only character devices which are themselves
143 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
144 * there would be no cache coherency between a descriptor and a VM mapping
145 * both to the same character device.
147 * Block devices can be mmap'd no matter what they represent. Cache coherency
148 * is maintained as long as you do not write directly to the underlying
151 * Requires caller to hold vm_token.
154 kern_mmap(struct vmspace *vms, caddr_t uaddr, size_t ulen,
155 int uprot, int uflags, int fd, off_t upos, void **res)
157 struct thread *td = curthread;
158 struct proc *p = td->td_proc;
159 struct file *fp = NULL;
163 vm_size_t size, pageoff;
164 vm_prot_t prot, maxprot;
171 ASSERT_LWKT_TOKEN_HELD(&vm_token);
173 addr = (vm_offset_t) uaddr;
175 prot = uprot & VM_PROT_ALL;
180 * Make sure mapping fits into numeric range etc.
182 * NOTE: We support the full unsigned range for size now.
184 if (((flags & MAP_ANON) && (fd != -1 || pos != 0)))
187 if (flags & MAP_STACK) {
189 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
196 * Virtual page tables cannot be used with MAP_STACK. Apart from
197 * it not making any sense, the aux union is used by both
200 * Because the virtual page table is stored in the backing object
201 * and might be updated by the kernel, the mapping must be R+W.
203 if (flags & MAP_VPAGETABLE) {
204 if (vkernel_enable == 0)
206 if (flags & MAP_STACK)
208 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
213 * Align the file position to a page boundary,
214 * and save its page offset component.
216 pageoff = (pos & PAGE_MASK);
219 /* Adjust size for rounding (on both ends). */
220 size += pageoff; /* low end... */
221 size = (vm_size_t) round_page(size); /* hi end */
222 if (size < ulen) /* wrap */
226 * Check for illegal addresses. Watch out for address wrap... Note
227 * that VM_*_ADDRESS are not constants due to casts (argh).
229 if (flags & (MAP_FIXED | MAP_TRYFIXED)) {
231 * The specified address must have the same remainder
232 * as the file offset taken modulo PAGE_SIZE, so it
233 * should be aligned after adjustment by pageoff.
236 if (addr & PAGE_MASK)
240 * Address range must be all in user VM space and not wrap.
242 tmpaddr = addr + size;
245 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
247 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
251 * Set a reasonable start point for the hint if it was
252 * not specified or if it falls within the heap space.
253 * Hinted mmap()s do not allocate out of the heap space.
256 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
257 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz)))
258 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
261 if (flags & MAP_ANON) {
263 * Mapping blank space is trivial.
266 maxprot = VM_PROT_ALL;
269 * Mapping file, get fp for validation. Obtain vnode and make
270 * sure it is of appropriate type.
272 fp = holdfp(p->p_fd, fd, -1);
275 if (fp->f_type != DTYPE_VNODE) {
280 * POSIX shared-memory objects are defined to have
281 * kernel persistence, and are not defined to support
282 * read(2)/write(2) -- or even open(2). Thus, we can
283 * use MAP_ASYNC to trade on-disk coherence for speed.
284 * The shm_open(3) library routine turns on the FPOSIXSHM
285 * flag to request this behavior.
287 if (fp->f_flag & FPOSIXSHM)
289 vp = (struct vnode *) fp->f_data;
292 * Validate the vnode for the operation.
297 * Get the proper underlying object
299 if ((obj = vp->v_object) == NULL) {
303 KKASSERT((struct vnode *)obj->handle == vp);
307 * Make sure a device has not been revoked.
308 * Mappability is handled by the device layer.
310 if (vp->v_rdev == NULL) {
317 * Nothing else is mappable.
324 * XXX hack to handle use of /dev/zero to map anon memory (ala
327 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
329 maxprot = VM_PROT_ALL;
334 * cdevs does not provide private mappings of any kind.
336 if (vp->v_type == VCHR &&
337 (flags & (MAP_PRIVATE|MAP_COPY))) {
342 * Ensure that file and memory protections are
343 * compatible. Note that we only worry about
344 * writability if mapping is shared; in this case,
345 * current and max prot are dictated by the open file.
346 * XXX use the vnode instead? Problem is: what
347 * credentials do we use for determination? What if
348 * proc does a setuid?
350 maxprot = VM_PROT_EXECUTE; /* ??? */
351 if (fp->f_flag & FREAD) {
352 maxprot |= VM_PROT_READ;
353 } else if (prot & PROT_READ) {
358 * If we are sharing potential changes (either via
359 * MAP_SHARED or via the implicit sharing of character
360 * device mappings), and we are trying to get write
361 * permission although we opened it without asking
362 * for it, bail out. Check for superuser, only if
363 * we're at securelevel < 1, to allow the XIG X server
364 * to continue to work.
366 if ((flags & MAP_SHARED) != 0 || vp->v_type == VCHR) {
367 if ((fp->f_flag & FWRITE) != 0) {
369 if ((error = VOP_GETATTR(vp, &va))) {
373 (IMMUTABLE|APPEND)) == 0) {
374 maxprot |= VM_PROT_WRITE;
375 } else if (prot & PROT_WRITE) {
379 } else if ((prot & PROT_WRITE) != 0) {
384 maxprot |= VM_PROT_WRITE;
391 * Do not allow more then a certain number of vm_map_entry structures
392 * per process. Scale with the number of rforks sharing the map
393 * to make the limit reasonable for threads.
396 vms->vm_map.nentries >= max_proc_mmap * vms->vm_sysref.refcnt) {
401 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
404 *res = (void *)(addr + pageoff);
412 * mmap system call handler
417 sys_mmap(struct mmap_args *uap)
421 lwkt_gettoken(&vm_token);
422 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len,
423 uap->prot, uap->flags,
424 uap->fd, uap->pos, &uap->sysmsg_resultp);
425 lwkt_reltoken(&vm_token);
431 * msync system call handler
433 * msync_args(void *addr, size_t len, int flags)
438 sys_msync(struct msync_args *uap)
440 struct proc *p = curproc;
443 vm_size_t size, pageoff;
448 addr = (vm_offset_t) uap->addr;
452 pageoff = (addr & PAGE_MASK);
455 size = (vm_size_t) round_page(size);
456 if (size < uap->len) /* wrap */
458 tmpaddr = addr + size; /* workaround gcc4 opt */
459 if (tmpaddr < addr) /* wrap */
462 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
465 lwkt_gettoken(&vm_token);
466 map = &p->p_vmspace->vm_map;
469 * XXX Gak! If size is zero we are supposed to sync "all modified
470 * pages with the region containing addr". Unfortunately, we don't
471 * really keep track of individual mmaps so we approximate by flushing
472 * the range of the map entry containing addr. This can be incorrect
473 * if the region splits or is coalesced with a neighbor.
476 vm_map_entry_t entry;
478 vm_map_lock_read(map);
479 rv = vm_map_lookup_entry(map, addr, &entry);
481 vm_map_unlock_read(map);
482 rv = KERN_INVALID_ADDRESS;
486 size = entry->end - entry->start;
487 vm_map_unlock_read(map);
491 * Clean the pages and interpret the return value.
493 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
494 (flags & MS_INVALIDATE) != 0);
496 lwkt_reltoken(&vm_token);
501 case KERN_INVALID_ADDRESS:
502 return (EINVAL); /* Sun returns ENOMEM? */
513 * munmap system call handler
515 * munmap_args(void *addr, size_t len)
520 sys_munmap(struct munmap_args *uap)
522 struct proc *p = curproc;
525 vm_size_t size, pageoff;
528 addr = (vm_offset_t) uap->addr;
531 pageoff = (addr & PAGE_MASK);
534 size = (vm_size_t) round_page(size);
535 if (size < uap->len) /* wrap */
537 tmpaddr = addr + size; /* workaround gcc4 opt */
538 if (tmpaddr < addr) /* wrap */
545 * Check for illegal addresses. Watch out for address wrap... Note
546 * that VM_*_ADDRESS are not constants due to casts (argh).
548 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
550 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
553 lwkt_gettoken(&vm_token);
554 map = &p->p_vmspace->vm_map;
556 * Make sure entire range is allocated.
558 if (!vm_map_check_protection(map, addr, addr + size,
559 VM_PROT_NONE, FALSE)) {
560 lwkt_reltoken(&vm_token);
563 /* returns nothing but KERN_SUCCESS anyway */
564 vm_map_remove(map, addr, addr + size);
565 lwkt_reltoken(&vm_token);
570 * mprotect_args(const void *addr, size_t len, int prot)
575 sys_mprotect(struct mprotect_args *uap)
577 struct proc *p = curproc;
580 vm_size_t size, pageoff;
584 addr = (vm_offset_t) uap->addr;
586 prot = uap->prot & VM_PROT_ALL;
587 #if defined(VM_PROT_READ_IS_EXEC)
588 if (prot & VM_PROT_READ)
589 prot |= VM_PROT_EXECUTE;
592 pageoff = (addr & PAGE_MASK);
595 size = (vm_size_t) round_page(size);
596 if (size < uap->len) /* wrap */
598 tmpaddr = addr + size; /* workaround gcc4 opt */
599 if (tmpaddr < addr) /* wrap */
602 lwkt_gettoken(&vm_token);
603 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size,
608 case KERN_PROTECTION_FAILURE:
615 lwkt_reltoken(&vm_token);
620 * minherit system call handler
622 * minherit_args(void *addr, size_t len, int inherit)
627 sys_minherit(struct minherit_args *uap)
629 struct proc *p = curproc;
632 vm_size_t size, pageoff;
633 vm_inherit_t inherit;
636 addr = (vm_offset_t)uap->addr;
638 inherit = uap->inherit;
640 pageoff = (addr & PAGE_MASK);
643 size = (vm_size_t) round_page(size);
644 if (size < uap->len) /* wrap */
646 tmpaddr = addr + size; /* workaround gcc4 opt */
647 if (tmpaddr < addr) /* wrap */
650 lwkt_gettoken(&vm_token);
652 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr,
653 addr + size, inherit)) {
657 case KERN_PROTECTION_FAILURE:
664 lwkt_reltoken(&vm_token);
669 * madvise system call handler
671 * madvise_args(void *addr, size_t len, int behav)
676 sys_madvise(struct madvise_args *uap)
678 struct proc *p = curproc;
679 vm_offset_t start, end;
680 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
684 * Check for illegal behavior
686 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END)
689 * Check for illegal addresses. Watch out for address wrap... Note
690 * that VM_*_ADDRESS are not constants due to casts (argh).
692 if (tmpaddr < (vm_offset_t)uap->addr)
694 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
696 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
700 * Since this routine is only advisory, we default to conservative
703 start = trunc_page((vm_offset_t)uap->addr);
704 end = round_page(tmpaddr);
706 lwkt_gettoken(&vm_token);
707 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
709 lwkt_reltoken(&vm_token);
714 * mcontrol system call handler
716 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
721 sys_mcontrol(struct mcontrol_args *uap)
723 struct proc *p = curproc;
724 vm_offset_t start, end;
725 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
729 * Check for illegal behavior
731 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
734 * Check for illegal addresses. Watch out for address wrap... Note
735 * that VM_*_ADDRESS are not constants due to casts (argh).
737 if (tmpaddr < (vm_offset_t) uap->addr)
739 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
741 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
745 * Since this routine is only advisory, we default to conservative
748 start = trunc_page((vm_offset_t)uap->addr);
749 end = round_page(tmpaddr);
751 lwkt_gettoken(&vm_token);
752 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
753 uap->behav, uap->value);
754 lwkt_reltoken(&vm_token);
760 * mincore system call handler
762 * mincore_args(const void *addr, size_t len, char *vec)
767 sys_mincore(struct mincore_args *uap)
769 struct proc *p = curproc;
770 vm_offset_t addr, first_addr;
771 vm_offset_t end, cend;
776 int vecindex, lastvecindex;
777 vm_map_entry_t current;
778 vm_map_entry_t entry;
780 unsigned int timestamp;
783 * Make sure that the addresses presented are valid for user
786 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
787 end = addr + (vm_size_t)round_page(uap->len);
790 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS)
794 * Address of byte vector
798 map = &p->p_vmspace->vm_map;
799 pmap = vmspace_pmap(p->p_vmspace);
801 lwkt_gettoken(&vm_token);
802 vm_map_lock_read(map);
804 timestamp = map->timestamp;
806 if (!vm_map_lookup_entry(map, addr, &entry))
810 * Do this on a map entry basis so that if the pages are not
811 * in the current processes address space, we can easily look
812 * up the pages elsewhere.
816 (current != &map->header) && (current->start < end);
817 current = current->next) {
820 * ignore submaps (for now) or null objects
822 if (current->maptype != VM_MAPTYPE_NORMAL &&
823 current->maptype != VM_MAPTYPE_VPAGETABLE) {
826 if (current->object.vm_object == NULL)
830 * limit this scan to the current map entry and the
831 * limits for the mincore call
833 if (addr < current->start)
834 addr = current->start;
840 * scan this entry one page at a time
842 while (addr < cend) {
844 * Check pmap first, it is likely faster, also
845 * it can provide info as to whether we are the
846 * one referencing or modifying the page.
848 * If we have to check the VM object, only mess
849 * around with normal maps. Do not mess around
850 * with virtual page tables (XXX).
852 mincoreinfo = pmap_mincore(pmap, addr);
853 if (mincoreinfo == 0 &&
854 current->maptype == VM_MAPTYPE_NORMAL) {
860 * calculate the page index into the object
862 offset = current->offset + (addr - current->start);
863 pindex = OFF_TO_IDX(offset);
866 * if the page is resident, then gather
867 * information about it. spl protection is
868 * required to maintain the object
869 * association. And XXX what if the page is
870 * busy? What's the deal with that?
873 m = vm_page_lookup(current->object.vm_object,
876 mincoreinfo = MINCORE_INCORE;
879 mincoreinfo |= MINCORE_MODIFIED_OTHER;
880 if ((m->flags & PG_REFERENCED) ||
881 pmap_ts_referenced(m)) {
882 vm_page_flag_set(m, PG_REFERENCED);
883 mincoreinfo |= MINCORE_REFERENCED_OTHER;
890 * subyte may page fault. In case it needs to modify
891 * the map, we release the lock.
893 vm_map_unlock_read(map);
896 * calculate index into user supplied byte vector
898 vecindex = OFF_TO_IDX(addr - first_addr);
901 * If we have skipped map entries, we need to make sure that
902 * the byte vector is zeroed for those skipped entries.
904 while((lastvecindex + 1) < vecindex) {
905 error = subyte( vec + lastvecindex, 0);
914 * Pass the page information to the user
916 error = subyte( vec + vecindex, mincoreinfo);
923 * If the map has changed, due to the subyte, the previous
924 * output may be invalid.
926 vm_map_lock_read(map);
927 if (timestamp != map->timestamp)
930 lastvecindex = vecindex;
936 * subyte may page fault. In case it needs to modify
937 * the map, we release the lock.
939 vm_map_unlock_read(map);
942 * Zero the last entries in the byte vector.
944 vecindex = OFF_TO_IDX(end - first_addr);
945 while((lastvecindex + 1) < vecindex) {
946 error = subyte( vec + lastvecindex, 0);
955 * If the map has changed, due to the subyte, the previous
956 * output may be invalid.
958 vm_map_lock_read(map);
959 if (timestamp != map->timestamp)
961 vm_map_unlock_read(map);
965 lwkt_reltoken(&vm_token);
970 * mlock system call handler
972 * mlock_args(const void *addr, size_t len)
977 sys_mlock(struct mlock_args *uap)
981 vm_size_t size, pageoff;
982 struct thread *td = curthread;
983 struct proc *p = td->td_proc;
986 addr = (vm_offset_t) uap->addr;
989 pageoff = (addr & PAGE_MASK);
992 size = (vm_size_t) round_page(size);
993 if (size < uap->len) /* wrap */
995 tmpaddr = addr + size; /* workaround gcc4 opt */
996 if (tmpaddr < addr) /* wrap */
999 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
1002 lwkt_gettoken(&vm_token);
1003 #ifdef pmap_wired_count
1004 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
1005 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) {
1006 lwkt_reltoken(&vm_token);
1010 error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1012 lwkt_reltoken(&vm_token);
1016 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
1017 lwkt_reltoken(&vm_token);
1018 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1022 * mlockall_args(int how)
1024 * Dummy routine, doesn't actually do anything.
1029 sys_mlockall(struct mlockall_args *uap)
1035 * munlockall_args(void)
1037 * Dummy routine, doesn't actually do anything.
1042 sys_munlockall(struct munlockall_args *uap)
1048 * munlock system call handler
1050 * munlock_args(const void *addr, size_t len)
1055 sys_munlock(struct munlock_args *uap)
1057 struct thread *td = curthread;
1058 struct proc *p = td->td_proc;
1060 vm_offset_t tmpaddr;
1061 vm_size_t size, pageoff;
1064 addr = (vm_offset_t) uap->addr;
1067 pageoff = (addr & PAGE_MASK);
1070 size = (vm_size_t) round_page(size);
1072 tmpaddr = addr + size;
1073 if (tmpaddr < addr) /* wrap */
1076 #ifndef pmap_wired_count
1077 error = priv_check(td, PRIV_ROOT);
1082 lwkt_gettoken(&vm_token);
1083 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
1084 lwkt_reltoken(&vm_token);
1085 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1089 * Internal version of mmap.
1090 * Currently used by mmap, exec, and sys5 shared memory.
1091 * Handle is either a vnode pointer or NULL for MAP_ANON.
1093 * Requires that the MP lock is held by the caller
1096 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1097 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff)
1104 struct thread *td = curthread;
1106 int rv = KERN_SUCCESS;
1113 objsize = round_page(size);
1119 * XXX messy code, fixme
1121 * NOTE: Overflow checks require discrete statements or GCC4
1122 * will optimize it out.
1124 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) {
1125 esize = map->size + size; /* workaround gcc4 opt */
1126 if (esize < map->size ||
1127 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
1133 * We currently can only deal with page aligned file offsets.
1134 * The check is here rather than in the syscall because the
1135 * kernel calls this function internally for other mmaping
1136 * operations (such as in exec) and non-aligned offsets will
1137 * cause pmap inconsistencies...so we want to be sure to
1138 * disallow this in all cases.
1140 * NOTE: Overflow checks require discrete statements or GCC4
1141 * will optimize it out.
1143 if (foff & PAGE_MASK)
1146 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
1148 *addr = round_page(*addr);
1150 if (*addr != trunc_page(*addr))
1152 eaddr = *addr + size;
1156 if ((flags & MAP_TRYFIXED) == 0)
1157 vm_map_remove(map, *addr, *addr + size);
1161 * Lookup/allocate object.
1163 if (flags & MAP_ANON) {
1165 * Unnamed anonymous regions always start at 0.
1169 * Default memory object
1171 object = default_pager_alloc(handle, objsize,
1175 docow = MAP_PREFAULT_PARTIAL;
1178 * Implicit single instance of a default memory
1179 * object, so we don't need a VM object yet.
1187 vp = (struct vnode *)handle;
1188 if (vp->v_type == VCHR) {
1190 * Device mappings (device size unknown?).
1191 * Force them to be shared.
1193 handle = (void *)(intptr_t)vp->v_rdev;
1194 object = dev_pager_alloc(handle, objsize, prot, foff);
1197 docow = MAP_PREFAULT_PARTIAL;
1198 flags &= ~(MAP_PRIVATE|MAP_COPY);
1199 flags |= MAP_SHARED;
1202 * Regular file mapping (typically). The attribute
1203 * check is for the link count test only. Mmapble
1204 * vnodes must already have a VM object assigned.
1209 error = VOP_GETATTR(vp, &vat);
1212 docow = MAP_PREFAULT_PARTIAL;
1213 object = vnode_pager_reference(vp);
1214 if (object == NULL && vp->v_type == VREG) {
1215 kprintf("Warning: cannot mmap vnode %p, no "
1221 * If it is a regular file without any references
1222 * we do not need to sync it.
1224 if (vp->v_type == VREG && vat.va_nlink == 0) {
1225 flags |= MAP_NOSYNC;
1231 * Deal with the adjusted flags
1233 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1234 docow |= MAP_COPY_ON_WRITE;
1235 if (flags & MAP_NOSYNC)
1236 docow |= MAP_DISABLE_SYNCER;
1237 if (flags & MAP_NOCORE)
1238 docow |= MAP_DISABLE_COREDUMP;
1240 #if defined(VM_PROT_READ_IS_EXEC)
1241 if (prot & VM_PROT_READ)
1242 prot |= VM_PROT_EXECUTE;
1244 if (maxprot & VM_PROT_READ)
1245 maxprot |= VM_PROT_EXECUTE;
1249 * This may place the area in its own page directory if (size) is
1250 * large enough, otherwise it typically returns its argument.
1253 *addr = pmap_addr_hint(object, *addr, size);
1257 * Stack mappings need special attention.
1259 * Mappings that use virtual page tables will default to storing
1260 * the page table at offset 0.
1262 if (flags & MAP_STACK) {
1263 rv = vm_map_stack(map, *addr, size, flags,
1264 prot, maxprot, docow);
1265 } else if (flags & MAP_VPAGETABLE) {
1266 rv = vm_map_find(map, object, foff, addr, size, PAGE_SIZE,
1267 fitit, VM_MAPTYPE_VPAGETABLE,
1268 prot, maxprot, docow);
1270 rv = vm_map_find(map, object, foff, addr, size, PAGE_SIZE,
1271 fitit, VM_MAPTYPE_NORMAL,
1272 prot, maxprot, docow);
1275 if (rv != KERN_SUCCESS) {
1277 * Lose the object reference. Will destroy the
1278 * object if it's an unnamed anonymous mapping
1279 * or named anonymous without other references.
1281 vm_object_deallocate(object);
1286 * Shared memory is also shared with children.
1288 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1289 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1290 if (rv != KERN_SUCCESS) {
1291 vm_map_remove(map, *addr, *addr + size);
1297 * Set the access time on the vnode
1300 vn_mark_atime(vp, td);
1305 case KERN_INVALID_ADDRESS:
1308 case KERN_PROTECTION_FAILURE: