xref: /freebsd/sys/vm/vm_mmap.c (revision b483c7f6e2977dc904afd7057de0430634deee90)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1988 University of Utah.
3df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
7df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
8df8bae1dSRodney W. Grimes  * Science Department.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
195929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  *	@(#)vm_mmap.c	8.4 (Berkeley) 1/12/94
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43df8bae1dSRodney W. Grimes /*
44df8bae1dSRodney W. Grimes  * Mapped file (mmap) interface to VM
45df8bae1dSRodney W. Grimes  */
46df8bae1dSRodney W. Grimes 
47874651b1SDavid E. O'Brien #include <sys/cdefs.h>
48874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
49874651b1SDavid E. O'Brien 
505591b823SEivind Eklund #include "opt_compat.h"
513e732e7dSRobert Watson #include "opt_mac.h"
52e9822d92SJoerg Wunsch 
53df8bae1dSRodney W. Grimes #include <sys/param.h>
54df8bae1dSRodney W. Grimes #include <sys/systm.h>
55fb919e4dSMark Murray #include <sys/kernel.h>
56fb919e4dSMark Murray #include <sys/lock.h>
5723955314SAlfred Perlstein #include <sys/mutex.h>
58d2d3e875SBruce Evans #include <sys/sysproto.h>
59df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
60df8bae1dSRodney W. Grimes #include <sys/proc.h>
61070f64feSMatthew Dillon #include <sys/resource.h>
62070f64feSMatthew Dillon #include <sys/resourcevar.h>
63df8bae1dSRodney W. Grimes #include <sys/vnode.h>
643ac4d1efSBruce Evans #include <sys/fcntl.h>
65df8bae1dSRodney W. Grimes #include <sys/file.h>
663e732e7dSRobert Watson #include <sys/mac.h>
67df8bae1dSRodney W. Grimes #include <sys/mman.h>
68b483c7f6SGuido van Rooij #include <sys/mount.h>
69df8bae1dSRodney W. Grimes #include <sys/conf.h>
704183b6b6SPeter Wemm #include <sys/stat.h>
71efeaf95aSDavid Greenman #include <sys/vmmeter.h>
721f6889a1SMatthew Dillon #include <sys/sysctl.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <vm/vm.h>
75efeaf95aSDavid Greenman #include <vm/vm_param.h>
76efeaf95aSDavid Greenman #include <vm/pmap.h>
77efeaf95aSDavid Greenman #include <vm/vm_map.h>
78efeaf95aSDavid Greenman #include <vm/vm_object.h>
791c7c3c6aSMatthew Dillon #include <vm/vm_page.h>
80df8bae1dSRodney W. Grimes #include <vm/vm_pager.h>
81b5e8ce9fSBruce Evans #include <vm/vm_pageout.h>
82efeaf95aSDavid Greenman #include <vm/vm_extern.h>
83867a482dSJohn Dyson #include <vm/vm_page.h>
841f6889a1SMatthew Dillon #include <vm/vm_kern.h>
85df8bae1dSRodney W. Grimes 
86d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
87df8bae1dSRodney W. Grimes struct sbrk_args {
88df8bae1dSRodney W. Grimes 	int incr;
89df8bae1dSRodney W. Grimes };
90d2d3e875SBruce Evans #endif
910d94caffSDavid Greenman 
921f6889a1SMatthew Dillon static int max_proc_mmap;
931f6889a1SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
941f6889a1SMatthew Dillon 
951f6889a1SMatthew Dillon /*
961f6889a1SMatthew Dillon  * Set the maximum number of vm_map_entry structures per process.  Roughly
971f6889a1SMatthew Dillon  * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
981f6889a1SMatthew Dillon  * of our KVM malloc space still results in generous limits.  We want a
991f6889a1SMatthew Dillon  * default that is good enough to prevent the kernel running out of resources
1001f6889a1SMatthew Dillon  * if attacked from compromised user account but generous enough such that
1011f6889a1SMatthew Dillon  * multi-threaded processes are not unduly inconvenienced.
1021f6889a1SMatthew Dillon  */
10311caded3SAlfred Perlstein static void vmmapentry_rsrc_init(void *);
1041f6889a1SMatthew Dillon SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL)
1051f6889a1SMatthew Dillon 
1061f6889a1SMatthew Dillon static void
1071f6889a1SMatthew Dillon vmmapentry_rsrc_init(dummy)
1081f6889a1SMatthew Dillon         void *dummy;
1091f6889a1SMatthew Dillon {
1101f6889a1SMatthew Dillon     max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
1111f6889a1SMatthew Dillon     max_proc_mmap /= 100;
1121f6889a1SMatthew Dillon }
1131f6889a1SMatthew Dillon 
114c8daea13SAlexander Kabaev static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
115c8daea13SAlexander Kabaev     int *, struct vnode *, vm_ooffset_t, vm_object_t *);
116c8daea13SAlexander Kabaev 
117d2c60af8SMatthew Dillon /*
118d2c60af8SMatthew Dillon  * MPSAFE
119d2c60af8SMatthew Dillon  */
120df8bae1dSRodney W. Grimes /* ARGSUSED */
121df8bae1dSRodney W. Grimes int
122b40ce416SJulian Elischer sbrk(td, uap)
123b40ce416SJulian Elischer 	struct thread *td;
124df8bae1dSRodney W. Grimes 	struct sbrk_args *uap;
125df8bae1dSRodney W. Grimes {
126df8bae1dSRodney W. Grimes 	/* Not yet implemented */
1270cddd8f0SMatthew Dillon 	/* mtx_lock(&Giant); */
1280cddd8f0SMatthew Dillon 	/* mtx_unlock(&Giant); */
129df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
130df8bae1dSRodney W. Grimes }
131df8bae1dSRodney W. Grimes 
132d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
133df8bae1dSRodney W. Grimes struct sstk_args {
134df8bae1dSRodney W. Grimes 	int incr;
135df8bae1dSRodney W. Grimes };
136d2d3e875SBruce Evans #endif
1370d94caffSDavid Greenman 
138d2c60af8SMatthew Dillon /*
139d2c60af8SMatthew Dillon  * MPSAFE
140d2c60af8SMatthew Dillon  */
141df8bae1dSRodney W. Grimes /* ARGSUSED */
142df8bae1dSRodney W. Grimes int
143b40ce416SJulian Elischer sstk(td, uap)
144b40ce416SJulian Elischer 	struct thread *td;
145df8bae1dSRodney W. Grimes 	struct sstk_args *uap;
146df8bae1dSRodney W. Grimes {
147df8bae1dSRodney W. Grimes 	/* Not yet implemented */
1480cddd8f0SMatthew Dillon 	/* mtx_lock(&Giant); */
1490cddd8f0SMatthew Dillon 	/* mtx_unlock(&Giant); */
150df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
151df8bae1dSRodney W. Grimes }
152df8bae1dSRodney W. Grimes 
153df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
154d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
155df8bae1dSRodney W. Grimes struct getpagesize_args {
156df8bae1dSRodney W. Grimes 	int dummy;
157df8bae1dSRodney W. Grimes };
158d2d3e875SBruce Evans #endif
1590d94caffSDavid Greenman 
160df8bae1dSRodney W. Grimes /* ARGSUSED */
161df8bae1dSRodney W. Grimes int
162b40ce416SJulian Elischer ogetpagesize(td, uap)
163b40ce416SJulian Elischer 	struct thread *td;
164df8bae1dSRodney W. Grimes 	struct getpagesize_args *uap;
165df8bae1dSRodney W. Grimes {
1660cddd8f0SMatthew Dillon 	/* MP SAFE */
167b40ce416SJulian Elischer 	td->td_retval[0] = PAGE_SIZE;
168df8bae1dSRodney W. Grimes 	return (0);
169df8bae1dSRodney W. Grimes }
170df8bae1dSRodney W. Grimes #endif				/* COMPAT_43 || COMPAT_SUNOS */
171df8bae1dSRodney W. Grimes 
17254f42e4bSPeter Wemm 
17354f42e4bSPeter Wemm /*
17454f42e4bSPeter Wemm  * Memory Map (mmap) system call.  Note that the file offset
17554f42e4bSPeter Wemm  * and address are allowed to be NOT page aligned, though if
17654f42e4bSPeter Wemm  * the MAP_FIXED flag it set, both must have the same remainder
17754f42e4bSPeter Wemm  * modulo the PAGE_SIZE (POSIX 1003.1b).  If the address is not
17854f42e4bSPeter Wemm  * page-aligned, the actual mapping starts at trunc_page(addr)
17954f42e4bSPeter Wemm  * and the return value is adjusted up by the page offset.
180b4309055SMatthew Dillon  *
181b4309055SMatthew Dillon  * Generally speaking, only character devices which are themselves
182b4309055SMatthew Dillon  * memory-based, such as a video framebuffer, can be mmap'd.  Otherwise
183b4309055SMatthew Dillon  * there would be no cache coherency between a descriptor and a VM mapping
184b4309055SMatthew Dillon  * both to the same character device.
185b4309055SMatthew Dillon  *
186b4309055SMatthew Dillon  * Block devices can be mmap'd no matter what they represent.  Cache coherency
187b4309055SMatthew Dillon  * is maintained as long as you do not write directly to the underlying
188b4309055SMatthew Dillon  * character device.
18954f42e4bSPeter Wemm  */
190d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
191df8bae1dSRodney W. Grimes struct mmap_args {
192651bb817SAlexander Langer 	void *addr;
193df8bae1dSRodney W. Grimes 	size_t len;
194df8bae1dSRodney W. Grimes 	int prot;
195df8bae1dSRodney W. Grimes 	int flags;
196df8bae1dSRodney W. Grimes 	int fd;
197df8bae1dSRodney W. Grimes 	long pad;
198df8bae1dSRodney W. Grimes 	off_t pos;
199df8bae1dSRodney W. Grimes };
200d2d3e875SBruce Evans #endif
201df8bae1dSRodney W. Grimes 
202d2c60af8SMatthew Dillon /*
203d2c60af8SMatthew Dillon  * MPSAFE
204d2c60af8SMatthew Dillon  */
205df8bae1dSRodney W. Grimes int
206b40ce416SJulian Elischer mmap(td, uap)
207b40ce416SJulian Elischer 	struct thread *td;
20854d92145SMatthew Dillon 	struct mmap_args *uap;
209df8bae1dSRodney W. Grimes {
210c8daea13SAlexander Kabaev 	struct file *fp;
211df8bae1dSRodney W. Grimes 	struct vnode *vp;
212df8bae1dSRodney W. Grimes 	vm_offset_t addr;
2139154ee6aSPeter Wemm 	vm_size_t size, pageoff;
214df8bae1dSRodney W. Grimes 	vm_prot_t prot, maxprot;
215651bb817SAlexander Langer 	void *handle;
216df8bae1dSRodney W. Grimes 	int flags, error;
21754f42e4bSPeter Wemm 	off_t pos;
218b40ce416SJulian Elischer 	struct vmspace *vms = td->td_proc->p_vmspace;
219df8bae1dSRodney W. Grimes 
22054f42e4bSPeter Wemm 	addr = (vm_offset_t) uap->addr;
22154f42e4bSPeter Wemm 	size = uap->len;
222df8bae1dSRodney W. Grimes 	prot = uap->prot & VM_PROT_ALL;
223df8bae1dSRodney W. Grimes 	flags = uap->flags;
22454f42e4bSPeter Wemm 	pos = uap->pos;
22554f42e4bSPeter Wemm 
226426da3bcSAlfred Perlstein 	fp = NULL;
22754f42e4bSPeter Wemm 	/* make sure mapping fits into numeric range etc */
228fc565456SDmitrij Tejblum 	if ((ssize_t) uap->len < 0 ||
22954f42e4bSPeter Wemm 	    ((flags & MAP_ANON) && uap->fd != -1))
230df8bae1dSRodney W. Grimes 		return (EINVAL);
2319154ee6aSPeter Wemm 
2322267af78SJulian Elischer 	if (flags & MAP_STACK) {
2332267af78SJulian Elischer 		if ((uap->fd != -1) ||
2342267af78SJulian Elischer 		    ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
2352267af78SJulian Elischer 			return (EINVAL);
2362267af78SJulian Elischer 		flags |= MAP_ANON;
2372267af78SJulian Elischer 		pos = 0;
2382907af2aSJulian Elischer 	}
2392907af2aSJulian Elischer 
2409154ee6aSPeter Wemm 	/*
24154f42e4bSPeter Wemm 	 * Align the file position to a page boundary,
24254f42e4bSPeter Wemm 	 * and save its page offset component.
2439154ee6aSPeter Wemm 	 */
24454f42e4bSPeter Wemm 	pageoff = (pos & PAGE_MASK);
24554f42e4bSPeter Wemm 	pos -= pageoff;
24654f42e4bSPeter Wemm 
24754f42e4bSPeter Wemm 	/* Adjust size for rounding (on both ends). */
24854f42e4bSPeter Wemm 	size += pageoff;			/* low end... */
24954f42e4bSPeter Wemm 	size = (vm_size_t) round_page(size);	/* hi end */
2509154ee6aSPeter Wemm 
251df8bae1dSRodney W. Grimes 	/*
2520d94caffSDavid Greenman 	 * Check for illegal addresses.  Watch out for address wrap... Note
2530d94caffSDavid Greenman 	 * that VM_*_ADDRESS are not constants due to casts (argh).
254df8bae1dSRodney W. Grimes 	 */
255df8bae1dSRodney W. Grimes 	if (flags & MAP_FIXED) {
25654f42e4bSPeter Wemm 		/*
25754f42e4bSPeter Wemm 		 * The specified address must have the same remainder
25854f42e4bSPeter Wemm 		 * as the file offset taken modulo PAGE_SIZE, so it
25954f42e4bSPeter Wemm 		 * should be aligned after adjustment by pageoff.
26054f42e4bSPeter Wemm 		 */
26154f42e4bSPeter Wemm 		addr -= pageoff;
26254f42e4bSPeter Wemm 		if (addr & PAGE_MASK)
26354f42e4bSPeter Wemm 			return (EINVAL);
26454f42e4bSPeter Wemm 		/* Address range must be all in user VM space. */
26505ba50f5SJake Burkholder 		if (addr < vm_map_min(&vms->vm_map) ||
26605ba50f5SJake Burkholder 		    addr + size > vm_map_max(&vms->vm_map))
267df8bae1dSRodney W. Grimes 			return (EINVAL);
268bbc0ec52SDavid Greenman 		if (addr + size < addr)
269df8bae1dSRodney W. Grimes 			return (EINVAL);
27091d5354aSJohn Baldwin 	} else {
271df8bae1dSRodney W. Grimes 	/*
27254f42e4bSPeter Wemm 	 * XXX for non-fixed mappings where no hint is provided or
27354f42e4bSPeter Wemm 	 * the hint would fall in the potential heap space,
27454f42e4bSPeter Wemm 	 * place it after the end of the largest possible heap.
275df8bae1dSRodney W. Grimes 	 *
27654f42e4bSPeter Wemm 	 * There should really be a pmap call to determine a reasonable
27754f42e4bSPeter Wemm 	 * location.
278df8bae1dSRodney W. Grimes 	 */
27991d5354aSJohn Baldwin 		PROC_LOCK(td->td_proc);
28091d5354aSJohn Baldwin 		if (addr == 0 ||
2811f6889a1SMatthew Dillon 		    (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
282c460ac3aSPeter Wemm 		    addr < round_page((vm_offset_t)vms->vm_daddr +
28391d5354aSJohn Baldwin 		    lim_max(td->td_proc, RLIMIT_DATA))))
284c460ac3aSPeter Wemm 			addr = round_page((vm_offset_t)vms->vm_daddr +
28591d5354aSJohn Baldwin 			    lim_max(td->td_proc, RLIMIT_DATA));
28691d5354aSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
28791d5354aSJohn Baldwin 	}
288df8bae1dSRodney W. Grimes 	if (flags & MAP_ANON) {
289df8bae1dSRodney W. Grimes 		/*
290df8bae1dSRodney W. Grimes 		 * Mapping blank space is trivial.
291df8bae1dSRodney W. Grimes 		 */
292df8bae1dSRodney W. Grimes 		handle = NULL;
293df8bae1dSRodney W. Grimes 		maxprot = VM_PROT_ALL;
29454f42e4bSPeter Wemm 		pos = 0;
29530d4dd7eSAlexander Kabaev 	} else {
296df8bae1dSRodney W. Grimes 		/*
2970d94caffSDavid Greenman 		 * Mapping file, get fp for validation. Obtain vnode and make
2980d94caffSDavid Greenman 		 * sure it is of appropriate type.
299426da3bcSAlfred Perlstein 		 * don't let the descriptor disappear on us if we block
300df8bae1dSRodney W. Grimes 		 */
301a4db4953SAlfred Perlstein 		if ((error = fget(td, uap->fd, &fp)) != 0)
302426da3bcSAlfred Perlstein 			goto done;
303e4ca250dSJohn Baldwin 		if (fp->f_type != DTYPE_VNODE) {
304d2c60af8SMatthew Dillon 			error = EINVAL;
305426da3bcSAlfred Perlstein 			goto done;
306e4ca250dSJohn Baldwin 		}
307279d7226SMatthew Dillon 		/*
308aa543039SGarrett Wollman 		 * POSIX shared-memory objects are defined to have
309aa543039SGarrett Wollman 		 * kernel persistence, and are not defined to support
310aa543039SGarrett Wollman 		 * read(2)/write(2) -- or even open(2).  Thus, we can
311aa543039SGarrett Wollman 		 * use MAP_ASYNC to trade on-disk coherence for speed.
312aa543039SGarrett Wollman 		 * The shm_open(3) library routine turns on the FPOSIXSHM
313aa543039SGarrett Wollman 		 * flag to request this behavior.
314aa543039SGarrett Wollman 		 */
315aa543039SGarrett Wollman 		if (fp->f_flag & FPOSIXSHM)
316aa543039SGarrett Wollman 			flags |= MAP_NOSYNC;
3173b6d9652SPoul-Henning Kamp 		vp = fp->f_vnode;
318c8bdd56bSGuido van Rooij 		/*
319df8bae1dSRodney W. Grimes 		 * Ensure that file and memory protections are
320df8bae1dSRodney W. Grimes 		 * compatible.  Note that we only worry about
321df8bae1dSRodney W. Grimes 		 * writability if mapping is shared; in this case,
322df8bae1dSRodney W. Grimes 		 * current and max prot are dictated by the open file.
323df8bae1dSRodney W. Grimes 		 * XXX use the vnode instead?  Problem is: what
3240d94caffSDavid Greenman 		 * credentials do we use for determination? What if
3250d94caffSDavid Greenman 		 * proc does a setuid?
326df8bae1dSRodney W. Grimes 		 */
327b483c7f6SGuido van Rooij 		if (vp->v_mount->mnt_flag & MNT_NOEXEC)
328b483c7f6SGuido van Rooij 			maxprot = VM_PROT_NONE;
329b483c7f6SGuido van Rooij 		else
330b483c7f6SGuido van Rooij 			maxprot = VM_PROT_EXECUTE;
331279d7226SMatthew Dillon 		if (fp->f_flag & FREAD) {
332df8bae1dSRodney W. Grimes 			maxprot |= VM_PROT_READ;
333279d7226SMatthew Dillon 		} else if (prot & PROT_READ) {
334279d7226SMatthew Dillon 			error = EACCES;
335279d7226SMatthew Dillon 			goto done;
336279d7226SMatthew Dillon 		}
337c8bdd56bSGuido van Rooij 		/*
338c8bdd56bSGuido van Rooij 		 * If we are sharing potential changes (either via
339c8bdd56bSGuido van Rooij 		 * MAP_SHARED or via the implicit sharing of character
340c8bdd56bSGuido van Rooij 		 * device mappings), and we are trying to get write
341c8bdd56bSGuido van Rooij 		 * permission although we opened it without asking
342c8daea13SAlexander Kabaev 		 * for it, bail out.
343c8bdd56bSGuido van Rooij 		 */
344c8daea13SAlexander Kabaev 		if ((flags & MAP_SHARED) != 0 || vp->v_type == VCHR) {
34505feb99fSGuido van Rooij 			if ((fp->f_flag & FWRITE) != 0) {
346df8bae1dSRodney W. Grimes 				maxprot |= VM_PROT_WRITE;
347279d7226SMatthew Dillon 			} else if ((prot & PROT_WRITE) != 0) {
348279d7226SMatthew Dillon 				error = EACCES;
349279d7226SMatthew Dillon 				goto done;
350279d7226SMatthew Dillon 			}
351279d7226SMatthew Dillon 		} else {
35205feb99fSGuido van Rooij 			maxprot |= VM_PROT_WRITE;
353279d7226SMatthew Dillon 		}
354651bb817SAlexander Langer 		handle = (void *)vp;
35530d4dd7eSAlexander Kabaev 	}
3561f6889a1SMatthew Dillon 
3571f6889a1SMatthew Dillon 	/*
3581f6889a1SMatthew Dillon 	 * Do not allow more then a certain number of vm_map_entry structures
3591f6889a1SMatthew Dillon 	 * per process.  Scale with the number of rforks sharing the map
3601f6889a1SMatthew Dillon 	 * to make the limit reasonable for threads.
3611f6889a1SMatthew Dillon 	 */
3621f6889a1SMatthew Dillon 	if (max_proc_mmap &&
3631f6889a1SMatthew Dillon 	    vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
364279d7226SMatthew Dillon 		error = ENOMEM;
365279d7226SMatthew Dillon 		goto done;
3661f6889a1SMatthew Dillon 	}
3671f6889a1SMatthew Dillon 
3681f6889a1SMatthew Dillon 	error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
36954f42e4bSPeter Wemm 	    flags, handle, pos);
370df8bae1dSRodney W. Grimes 	if (error == 0)
371b40ce416SJulian Elischer 		td->td_retval[0] = (register_t) (addr + pageoff);
372279d7226SMatthew Dillon done:
373279d7226SMatthew Dillon 	if (fp)
374b40ce416SJulian Elischer 		fdrop(fp, td);
375f6b5b182SJeff Roberson 
376df8bae1dSRodney W. Grimes 	return (error);
377df8bae1dSRodney W. Grimes }
378df8bae1dSRodney W. Grimes 
37905f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43
380d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
38105f0fdd2SPoul-Henning Kamp struct ommap_args {
38205f0fdd2SPoul-Henning Kamp 	caddr_t addr;
38305f0fdd2SPoul-Henning Kamp 	int len;
38405f0fdd2SPoul-Henning Kamp 	int prot;
38505f0fdd2SPoul-Henning Kamp 	int flags;
38605f0fdd2SPoul-Henning Kamp 	int fd;
38705f0fdd2SPoul-Henning Kamp 	long pos;
38805f0fdd2SPoul-Henning Kamp };
389d2d3e875SBruce Evans #endif
39005f0fdd2SPoul-Henning Kamp int
391b40ce416SJulian Elischer ommap(td, uap)
392b40ce416SJulian Elischer 	struct thread *td;
39354d92145SMatthew Dillon 	struct ommap_args *uap;
39405f0fdd2SPoul-Henning Kamp {
39505f0fdd2SPoul-Henning Kamp 	struct mmap_args nargs;
39605f0fdd2SPoul-Henning Kamp 	static const char cvtbsdprot[8] = {
39705f0fdd2SPoul-Henning Kamp 		0,
39805f0fdd2SPoul-Henning Kamp 		PROT_EXEC,
39905f0fdd2SPoul-Henning Kamp 		PROT_WRITE,
40005f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE,
40105f0fdd2SPoul-Henning Kamp 		PROT_READ,
40205f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_READ,
40305f0fdd2SPoul-Henning Kamp 		PROT_WRITE | PROT_READ,
40405f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE | PROT_READ,
40505f0fdd2SPoul-Henning Kamp 	};
4060d94caffSDavid Greenman 
40705f0fdd2SPoul-Henning Kamp #define	OMAP_ANON	0x0002
40805f0fdd2SPoul-Henning Kamp #define	OMAP_COPY	0x0020
40905f0fdd2SPoul-Henning Kamp #define	OMAP_SHARED	0x0010
41005f0fdd2SPoul-Henning Kamp #define	OMAP_FIXED	0x0100
41105f0fdd2SPoul-Henning Kamp 
41205f0fdd2SPoul-Henning Kamp 	nargs.addr = uap->addr;
41305f0fdd2SPoul-Henning Kamp 	nargs.len = uap->len;
41405f0fdd2SPoul-Henning Kamp 	nargs.prot = cvtbsdprot[uap->prot & 0x7];
41505f0fdd2SPoul-Henning Kamp 	nargs.flags = 0;
41605f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_ANON)
41705f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_ANON;
41805f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_COPY)
41905f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_COPY;
42005f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_SHARED)
42105f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_SHARED;
42205f0fdd2SPoul-Henning Kamp 	else
42305f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_PRIVATE;
42405f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_FIXED)
42505f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_FIXED;
42605f0fdd2SPoul-Henning Kamp 	nargs.fd = uap->fd;
42705f0fdd2SPoul-Henning Kamp 	nargs.pos = uap->pos;
428b40ce416SJulian Elischer 	return (mmap(td, &nargs));
42905f0fdd2SPoul-Henning Kamp }
43005f0fdd2SPoul-Henning Kamp #endif				/* COMPAT_43 */
43105f0fdd2SPoul-Henning Kamp 
43205f0fdd2SPoul-Henning Kamp 
433d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
434df8bae1dSRodney W. Grimes struct msync_args {
435651bb817SAlexander Langer 	void *addr;
436df8bae1dSRodney W. Grimes 	int len;
437e6c6af11SDavid Greenman 	int flags;
438df8bae1dSRodney W. Grimes };
439d2d3e875SBruce Evans #endif
440d2c60af8SMatthew Dillon /*
441d2c60af8SMatthew Dillon  * MPSAFE
442d2c60af8SMatthew Dillon  */
443df8bae1dSRodney W. Grimes int
444b40ce416SJulian Elischer msync(td, uap)
445b40ce416SJulian Elischer 	struct thread *td;
446df8bae1dSRodney W. Grimes 	struct msync_args *uap;
447df8bae1dSRodney W. Grimes {
448df8bae1dSRodney W. Grimes 	vm_offset_t addr;
449dabee6feSPeter Wemm 	vm_size_t size, pageoff;
450e6c6af11SDavid Greenman 	int flags;
451df8bae1dSRodney W. Grimes 	vm_map_t map;
452df8bae1dSRodney W. Grimes 	int rv;
453df8bae1dSRodney W. Grimes 
454df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
4559154ee6aSPeter Wemm 	size = uap->len;
456e6c6af11SDavid Greenman 	flags = uap->flags;
457e6c6af11SDavid Greenman 
458dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
459dabee6feSPeter Wemm 	addr -= pageoff;
460dabee6feSPeter Wemm 	size += pageoff;
461dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
4629154ee6aSPeter Wemm 	if (addr + size < addr)
463dabee6feSPeter Wemm 		return (EINVAL);
464dabee6feSPeter Wemm 
465dabee6feSPeter Wemm 	if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
4661e62bc63SDavid Greenman 		return (EINVAL);
4671e62bc63SDavid Greenman 
468b40ce416SJulian Elischer 	map = &td->td_proc->p_vmspace->vm_map;
4699154ee6aSPeter Wemm 
470df8bae1dSRodney W. Grimes 	/*
471df8bae1dSRodney W. Grimes 	 * Clean the pages and interpret the return value.
472df8bae1dSRodney W. Grimes 	 */
473950f8459SAlan Cox 	rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
474e6c6af11SDavid Greenman 	    (flags & MS_INVALIDATE) != 0);
475df8bae1dSRodney W. Grimes 	switch (rv) {
476df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
477d2c60af8SMatthew Dillon 		return (0);
478df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
479df8bae1dSRodney W. Grimes 		return (EINVAL);	/* Sun returns ENOMEM? */
480b7b7cd44SAlan Cox 	case KERN_INVALID_ARGUMENT:
481b7b7cd44SAlan Cox 		return (EBUSY);
482df8bae1dSRodney W. Grimes 	default:
483df8bae1dSRodney W. Grimes 		return (EINVAL);
484df8bae1dSRodney W. Grimes 	}
485df8bae1dSRodney W. Grimes }
486df8bae1dSRodney W. Grimes 
487d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
488df8bae1dSRodney W. Grimes struct munmap_args {
489651bb817SAlexander Langer 	void *addr;
4909154ee6aSPeter Wemm 	size_t len;
491df8bae1dSRodney W. Grimes };
492d2d3e875SBruce Evans #endif
493d2c60af8SMatthew Dillon /*
494d2c60af8SMatthew Dillon  * MPSAFE
495d2c60af8SMatthew Dillon  */
496df8bae1dSRodney W. Grimes int
497b40ce416SJulian Elischer munmap(td, uap)
498b40ce416SJulian Elischer 	struct thread *td;
49954d92145SMatthew Dillon 	struct munmap_args *uap;
500df8bae1dSRodney W. Grimes {
501df8bae1dSRodney W. Grimes 	vm_offset_t addr;
502dabee6feSPeter Wemm 	vm_size_t size, pageoff;
503df8bae1dSRodney W. Grimes 	vm_map_t map;
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
5069154ee6aSPeter Wemm 	size = uap->len;
507d8834602SAlan Cox 	if (size == 0)
508d8834602SAlan Cox 		return (EINVAL);
509dabee6feSPeter Wemm 
510dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
511dabee6feSPeter Wemm 	addr -= pageoff;
512dabee6feSPeter Wemm 	size += pageoff;
513dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
5149154ee6aSPeter Wemm 	if (addr + size < addr)
515df8bae1dSRodney W. Grimes 		return (EINVAL);
5169154ee6aSPeter Wemm 
517df8bae1dSRodney W. Grimes 	/*
51805ba50f5SJake Burkholder 	 * Check for illegal addresses.  Watch out for address wrap...
519df8bae1dSRodney W. Grimes 	 */
520b40ce416SJulian Elischer 	map = &td->td_proc->p_vmspace->vm_map;
52105ba50f5SJake Burkholder 	if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
52205ba50f5SJake Burkholder 		return (EINVAL);
523d8834602SAlan Cox 	vm_map_lock(map);
524df8bae1dSRodney W. Grimes 	/*
525df8bae1dSRodney W. Grimes 	 * Make sure entire range is allocated.
526df8bae1dSRodney W. Grimes 	 */
527d8834602SAlan Cox 	if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) {
528d8834602SAlan Cox 		vm_map_unlock(map);
529df8bae1dSRodney W. Grimes 		return (EINVAL);
530d8834602SAlan Cox 	}
531df8bae1dSRodney W. Grimes 	/* returns nothing but KERN_SUCCESS anyway */
532d8834602SAlan Cox 	vm_map_delete(map, addr, addr + size);
533d8834602SAlan Cox 	vm_map_unlock(map);
534df8bae1dSRodney W. Grimes 	return (0);
535df8bae1dSRodney W. Grimes }
536df8bae1dSRodney W. Grimes 
537d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
538df8bae1dSRodney W. Grimes struct mprotect_args {
539651bb817SAlexander Langer 	const void *addr;
5409154ee6aSPeter Wemm 	size_t len;
541df8bae1dSRodney W. Grimes 	int prot;
542df8bae1dSRodney W. Grimes };
543d2d3e875SBruce Evans #endif
544d2c60af8SMatthew Dillon /*
545d2c60af8SMatthew Dillon  * MPSAFE
546d2c60af8SMatthew Dillon  */
547df8bae1dSRodney W. Grimes int
548b40ce416SJulian Elischer mprotect(td, uap)
549b40ce416SJulian Elischer 	struct thread *td;
550df8bae1dSRodney W. Grimes 	struct mprotect_args *uap;
551df8bae1dSRodney W. Grimes {
552df8bae1dSRodney W. Grimes 	vm_offset_t addr;
553dabee6feSPeter Wemm 	vm_size_t size, pageoff;
55454d92145SMatthew Dillon 	vm_prot_t prot;
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
5579154ee6aSPeter Wemm 	size = uap->len;
558df8bae1dSRodney W. Grimes 	prot = uap->prot & VM_PROT_ALL;
559d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC)
560d0aea04fSJohn Dyson 	if (prot & VM_PROT_READ)
561d0aea04fSJohn Dyson 		prot |= VM_PROT_EXECUTE;
562d0aea04fSJohn Dyson #endif
563df8bae1dSRodney W. Grimes 
564dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
565dabee6feSPeter Wemm 	addr -= pageoff;
566dabee6feSPeter Wemm 	size += pageoff;
567dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
5689154ee6aSPeter Wemm 	if (addr + size < addr)
569dabee6feSPeter Wemm 		return (EINVAL);
570dabee6feSPeter Wemm 
57143285049SAlan Cox 	switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
57243285049SAlan Cox 	    addr + size, prot, FALSE)) {
573df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
574df8bae1dSRodney W. Grimes 		return (0);
575df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
576df8bae1dSRodney W. Grimes 		return (EACCES);
577df8bae1dSRodney W. Grimes 	}
578df8bae1dSRodney W. Grimes 	return (EINVAL);
579df8bae1dSRodney W. Grimes }
580df8bae1dSRodney W. Grimes 
581d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
582dabee6feSPeter Wemm struct minherit_args {
583651bb817SAlexander Langer 	void *addr;
5849154ee6aSPeter Wemm 	size_t len;
585dabee6feSPeter Wemm 	int inherit;
586dabee6feSPeter Wemm };
587dabee6feSPeter Wemm #endif
588d2c60af8SMatthew Dillon /*
589d2c60af8SMatthew Dillon  * MPSAFE
590d2c60af8SMatthew Dillon  */
591dabee6feSPeter Wemm int
592b40ce416SJulian Elischer minherit(td, uap)
593b40ce416SJulian Elischer 	struct thread *td;
594dabee6feSPeter Wemm 	struct minherit_args *uap;
595dabee6feSPeter Wemm {
596dabee6feSPeter Wemm 	vm_offset_t addr;
597dabee6feSPeter Wemm 	vm_size_t size, pageoff;
59854d92145SMatthew Dillon 	vm_inherit_t inherit;
599dabee6feSPeter Wemm 
600dabee6feSPeter Wemm 	addr = (vm_offset_t)uap->addr;
6019154ee6aSPeter Wemm 	size = uap->len;
602dabee6feSPeter Wemm 	inherit = uap->inherit;
603dabee6feSPeter Wemm 
604dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
605dabee6feSPeter Wemm 	addr -= pageoff;
606dabee6feSPeter Wemm 	size += pageoff;
607dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
6089154ee6aSPeter Wemm 	if (addr + size < addr)
609dabee6feSPeter Wemm 		return (EINVAL);
610dabee6feSPeter Wemm 
611e0be79afSAlan Cox 	switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
612e0be79afSAlan Cox 	    addr + size, inherit)) {
613dabee6feSPeter Wemm 	case KERN_SUCCESS:
614dabee6feSPeter Wemm 		return (0);
615dabee6feSPeter Wemm 	case KERN_PROTECTION_FAILURE:
616dabee6feSPeter Wemm 		return (EACCES);
617dabee6feSPeter Wemm 	}
618dabee6feSPeter Wemm 	return (EINVAL);
619dabee6feSPeter Wemm }
620dabee6feSPeter Wemm 
621dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_
622df8bae1dSRodney W. Grimes struct madvise_args {
623651bb817SAlexander Langer 	void *addr;
6249154ee6aSPeter Wemm 	size_t len;
625df8bae1dSRodney W. Grimes 	int behav;
626df8bae1dSRodney W. Grimes };
627d2d3e875SBruce Evans #endif
6280d94caffSDavid Greenman 
629d2c60af8SMatthew Dillon /*
630d2c60af8SMatthew Dillon  * MPSAFE
631d2c60af8SMatthew Dillon  */
632df8bae1dSRodney W. Grimes /* ARGSUSED */
633df8bae1dSRodney W. Grimes int
634b40ce416SJulian Elischer madvise(td, uap)
635b40ce416SJulian Elischer 	struct thread *td;
636df8bae1dSRodney W. Grimes 	struct madvise_args *uap;
637df8bae1dSRodney W. Grimes {
638f35329acSJohn Dyson 	vm_offset_t start, end;
63905ba50f5SJake Burkholder 	vm_map_t map;
640f4cf2141SWes Peters 	struct proc *p;
641f4cf2141SWes Peters 	int error;
642b4309055SMatthew Dillon 
643b4309055SMatthew Dillon 	/*
644f4cf2141SWes Peters 	 * Check for our special case, advising the swap pager we are
645f4cf2141SWes Peters 	 * "immortal."
646f4cf2141SWes Peters 	 */
647f4cf2141SWes Peters 	if (uap->behav == MADV_PROTECT) {
64869297bf8SJohn Baldwin 		error = suser(td);
64969297bf8SJohn Baldwin 		if (error == 0) {
650f4cf2141SWes Peters 			p = td->td_proc;
651f4cf2141SWes Peters 			PROC_LOCK(p);
652f4cf2141SWes Peters 			p->p_flag |= P_PROTECTED;
653f4cf2141SWes Peters 			PROC_UNLOCK(p);
65469297bf8SJohn Baldwin 		}
655f4cf2141SWes Peters 		return (error);
656f4cf2141SWes Peters 	}
657f4cf2141SWes Peters 	/*
658b4309055SMatthew Dillon 	 * Check for illegal behavior
659b4309055SMatthew Dillon 	 */
6609730a5daSPaul Saab 	if (uap->behav < 0 || uap->behav > MADV_CORE)
661b4309055SMatthew Dillon 		return (EINVAL);
662867a482dSJohn Dyson 	/*
663867a482dSJohn Dyson 	 * Check for illegal addresses.  Watch out for address wrap... Note
664867a482dSJohn Dyson 	 * that VM_*_ADDRESS are not constants due to casts (argh).
665867a482dSJohn Dyson 	 */
66605ba50f5SJake Burkholder 	map = &td->td_proc->p_vmspace->vm_map;
66705ba50f5SJake Burkholder 	if ((vm_offset_t)uap->addr < vm_map_min(map) ||
66805ba50f5SJake Burkholder 	    (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
669867a482dSJohn Dyson 		return (EINVAL);
670867a482dSJohn Dyson 	if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
671867a482dSJohn Dyson 		return (EINVAL);
672867a482dSJohn Dyson 
673867a482dSJohn Dyson 	/*
674867a482dSJohn Dyson 	 * Since this routine is only advisory, we default to conservative
675867a482dSJohn Dyson 	 * behavior.
676867a482dSJohn Dyson 	 */
677cd6eea25SDavid Greenman 	start = trunc_page((vm_offset_t) uap->addr);
678cd6eea25SDavid Greenman 	end = round_page((vm_offset_t) uap->addr + uap->len);
679867a482dSJohn Dyson 
68005ba50f5SJake Burkholder 	if (vm_map_madvise(map, start, end, uap->behav))
681094f6d26SAlan Cox 		return (EINVAL);
682094f6d26SAlan Cox 	return (0);
683df8bae1dSRodney W. Grimes }
684df8bae1dSRodney W. Grimes 
685d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
686df8bae1dSRodney W. Grimes struct mincore_args {
687651bb817SAlexander Langer 	const void *addr;
6889154ee6aSPeter Wemm 	size_t len;
689df8bae1dSRodney W. Grimes 	char *vec;
690df8bae1dSRodney W. Grimes };
691d2d3e875SBruce Evans #endif
6920d94caffSDavid Greenman 
693d2c60af8SMatthew Dillon /*
694d2c60af8SMatthew Dillon  * MPSAFE
695d2c60af8SMatthew Dillon  */
696df8bae1dSRodney W. Grimes /* ARGSUSED */
697df8bae1dSRodney W. Grimes int
698b40ce416SJulian Elischer mincore(td, uap)
699b40ce416SJulian Elischer 	struct thread *td;
700df8bae1dSRodney W. Grimes 	struct mincore_args *uap;
701df8bae1dSRodney W. Grimes {
702867a482dSJohn Dyson 	vm_offset_t addr, first_addr;
703867a482dSJohn Dyson 	vm_offset_t end, cend;
704867a482dSJohn Dyson 	pmap_t pmap;
705867a482dSJohn Dyson 	vm_map_t map;
70602c04a2fSJohn Dyson 	char *vec;
707d2c60af8SMatthew Dillon 	int error = 0;
708867a482dSJohn Dyson 	int vecindex, lastvecindex;
70954d92145SMatthew Dillon 	vm_map_entry_t current;
710867a482dSJohn Dyson 	vm_map_entry_t entry;
711867a482dSJohn Dyson 	int mincoreinfo;
712dd2622a8SAlan Cox 	unsigned int timestamp;
713df8bae1dSRodney W. Grimes 
714867a482dSJohn Dyson 	/*
715867a482dSJohn Dyson 	 * Make sure that the addresses presented are valid for user
716867a482dSJohn Dyson 	 * mode.
717867a482dSJohn Dyson 	 */
718867a482dSJohn Dyson 	first_addr = addr = trunc_page((vm_offset_t) uap->addr);
7199154ee6aSPeter Wemm 	end = addr + (vm_size_t)round_page(uap->len);
72005ba50f5SJake Burkholder 	map = &td->td_proc->p_vmspace->vm_map;
72105ba50f5SJake Burkholder 	if (end > vm_map_max(map) || end < addr)
72202c04a2fSJohn Dyson 		return (EINVAL);
72302c04a2fSJohn Dyson 
724867a482dSJohn Dyson 	/*
725867a482dSJohn Dyson 	 * Address of byte vector
726867a482dSJohn Dyson 	 */
72702c04a2fSJohn Dyson 	vec = uap->vec;
728867a482dSJohn Dyson 
729b40ce416SJulian Elischer 	pmap = vmspace_pmap(td->td_proc->p_vmspace);
730867a482dSJohn Dyson 
731eff50fcdSAlan Cox 	vm_map_lock_read(map);
732dd2622a8SAlan Cox RestartScan:
733dd2622a8SAlan Cox 	timestamp = map->timestamp;
734867a482dSJohn Dyson 
735867a482dSJohn Dyson 	if (!vm_map_lookup_entry(map, addr, &entry))
736867a482dSJohn Dyson 		entry = entry->next;
737867a482dSJohn Dyson 
738867a482dSJohn Dyson 	/*
739867a482dSJohn Dyson 	 * Do this on a map entry basis so that if the pages are not
740867a482dSJohn Dyson 	 * in the current processes address space, we can easily look
741867a482dSJohn Dyson 	 * up the pages elsewhere.
742867a482dSJohn Dyson 	 */
743867a482dSJohn Dyson 	lastvecindex = -1;
744867a482dSJohn Dyson 	for (current = entry;
745867a482dSJohn Dyson 	    (current != &map->header) && (current->start < end);
746867a482dSJohn Dyson 	    current = current->next) {
747867a482dSJohn Dyson 
748867a482dSJohn Dyson 		/*
749867a482dSJohn Dyson 		 * ignore submaps (for now) or null objects
750867a482dSJohn Dyson 		 */
7519fdfe602SMatthew Dillon 		if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
752867a482dSJohn Dyson 			current->object.vm_object == NULL)
753867a482dSJohn Dyson 			continue;
754867a482dSJohn Dyson 
755867a482dSJohn Dyson 		/*
756867a482dSJohn Dyson 		 * limit this scan to the current map entry and the
757867a482dSJohn Dyson 		 * limits for the mincore call
758867a482dSJohn Dyson 		 */
759867a482dSJohn Dyson 		if (addr < current->start)
760867a482dSJohn Dyson 			addr = current->start;
761867a482dSJohn Dyson 		cend = current->end;
762867a482dSJohn Dyson 		if (cend > end)
763867a482dSJohn Dyson 			cend = end;
764867a482dSJohn Dyson 
765867a482dSJohn Dyson 		/*
766867a482dSJohn Dyson 		 * scan this entry one page at a time
767867a482dSJohn Dyson 		 */
768867a482dSJohn Dyson 		while (addr < cend) {
769867a482dSJohn Dyson 			/*
770867a482dSJohn Dyson 			 * Check pmap first, it is likely faster, also
771867a482dSJohn Dyson 			 * it can provide info as to whether we are the
772867a482dSJohn Dyson 			 * one referencing or modifying the page.
773867a482dSJohn Dyson 			 */
7747ebcee37SAlan Cox 			mtx_lock(&Giant);
775867a482dSJohn Dyson 			mincoreinfo = pmap_mincore(pmap, addr);
7767ebcee37SAlan Cox 			mtx_unlock(&Giant);
777867a482dSJohn Dyson 			if (!mincoreinfo) {
778867a482dSJohn Dyson 				vm_pindex_t pindex;
779867a482dSJohn Dyson 				vm_ooffset_t offset;
780867a482dSJohn Dyson 				vm_page_t m;
781867a482dSJohn Dyson 				/*
782867a482dSJohn Dyson 				 * calculate the page index into the object
783867a482dSJohn Dyson 				 */
784867a482dSJohn Dyson 				offset = current->offset + (addr - current->start);
785867a482dSJohn Dyson 				pindex = OFF_TO_IDX(offset);
786bc5b057fSAlan Cox 				VM_OBJECT_LOCK(current->object.vm_object);
787867a482dSJohn Dyson 				m = vm_page_lookup(current->object.vm_object,
788867a482dSJohn Dyson 					pindex);
789867a482dSJohn Dyson 				/*
790867a482dSJohn Dyson 				 * if the page is resident, then gather information about
791867a482dSJohn Dyson 				 * it.
792867a482dSJohn Dyson 				 */
793cafe836aSAlan Cox 				if (m != NULL && m->valid != 0) {
794867a482dSJohn Dyson 					mincoreinfo = MINCORE_INCORE;
7957ebcee37SAlan Cox 					vm_page_lock_queues();
796867a482dSJohn Dyson 					if (m->dirty ||
7970385347cSPeter Wemm 						pmap_is_modified(m))
798867a482dSJohn Dyson 						mincoreinfo |= MINCORE_MODIFIED_OTHER;
799867a482dSJohn Dyson 					if ((m->flags & PG_REFERENCED) ||
8000385347cSPeter Wemm 						pmap_ts_referenced(m)) {
801e69763a3SDoug Rabson 						vm_page_flag_set(m, PG_REFERENCED);
802867a482dSJohn Dyson 						mincoreinfo |= MINCORE_REFERENCED_OTHER;
80302c04a2fSJohn Dyson 					}
804e80b7b69SAlan Cox 					vm_page_unlock_queues();
8059b5a5d81SJohn Dyson 				}
8067ebcee37SAlan Cox 				VM_OBJECT_UNLOCK(current->object.vm_object);
8077ebcee37SAlan Cox 			}
808867a482dSJohn Dyson 
809867a482dSJohn Dyson 			/*
810dd2622a8SAlan Cox 			 * subyte may page fault.  In case it needs to modify
811dd2622a8SAlan Cox 			 * the map, we release the lock.
812dd2622a8SAlan Cox 			 */
813dd2622a8SAlan Cox 			vm_map_unlock_read(map);
814dd2622a8SAlan Cox 
815dd2622a8SAlan Cox 			/*
816867a482dSJohn Dyson 			 * calculate index into user supplied byte vector
817867a482dSJohn Dyson 			 */
818867a482dSJohn Dyson 			vecindex = OFF_TO_IDX(addr - first_addr);
819867a482dSJohn Dyson 
820867a482dSJohn Dyson 			/*
821867a482dSJohn Dyson 			 * If we have skipped map entries, we need to make sure that
822867a482dSJohn Dyson 			 * the byte vector is zeroed for those skipped entries.
823867a482dSJohn Dyson 			 */
824867a482dSJohn Dyson 			while ((lastvecindex + 1) < vecindex) {
825867a482dSJohn Dyson 				error = subyte(vec + lastvecindex, 0);
826867a482dSJohn Dyson 				if (error) {
827d2c60af8SMatthew Dillon 					error = EFAULT;
828d2c60af8SMatthew Dillon 					goto done2;
829867a482dSJohn Dyson 				}
830867a482dSJohn Dyson 				++lastvecindex;
831867a482dSJohn Dyson 			}
832867a482dSJohn Dyson 
833867a482dSJohn Dyson 			/*
834867a482dSJohn Dyson 			 * Pass the page information to the user
835867a482dSJohn Dyson 			 */
836867a482dSJohn Dyson 			error = subyte(vec + vecindex, mincoreinfo);
837867a482dSJohn Dyson 			if (error) {
838d2c60af8SMatthew Dillon 				error = EFAULT;
839d2c60af8SMatthew Dillon 				goto done2;
840867a482dSJohn Dyson 			}
841dd2622a8SAlan Cox 
842dd2622a8SAlan Cox 			/*
843dd2622a8SAlan Cox 			 * If the map has changed, due to the subyte, the previous
844dd2622a8SAlan Cox 			 * output may be invalid.
845dd2622a8SAlan Cox 			 */
846dd2622a8SAlan Cox 			vm_map_lock_read(map);
847dd2622a8SAlan Cox 			if (timestamp != map->timestamp)
848dd2622a8SAlan Cox 				goto RestartScan;
849dd2622a8SAlan Cox 
850867a482dSJohn Dyson 			lastvecindex = vecindex;
85102c04a2fSJohn Dyson 			addr += PAGE_SIZE;
85202c04a2fSJohn Dyson 		}
853867a482dSJohn Dyson 	}
854867a482dSJohn Dyson 
855867a482dSJohn Dyson 	/*
856dd2622a8SAlan Cox 	 * subyte may page fault.  In case it needs to modify
857dd2622a8SAlan Cox 	 * the map, we release the lock.
858dd2622a8SAlan Cox 	 */
859dd2622a8SAlan Cox 	vm_map_unlock_read(map);
860dd2622a8SAlan Cox 
861dd2622a8SAlan Cox 	/*
862867a482dSJohn Dyson 	 * Zero the last entries in the byte vector.
863867a482dSJohn Dyson 	 */
864867a482dSJohn Dyson 	vecindex = OFF_TO_IDX(end - first_addr);
865867a482dSJohn Dyson 	while ((lastvecindex + 1) < vecindex) {
866867a482dSJohn Dyson 		error = subyte(vec + lastvecindex, 0);
867867a482dSJohn Dyson 		if (error) {
868d2c60af8SMatthew Dillon 			error = EFAULT;
869d2c60af8SMatthew Dillon 			goto done2;
870867a482dSJohn Dyson 		}
871867a482dSJohn Dyson 		++lastvecindex;
872867a482dSJohn Dyson 	}
873867a482dSJohn Dyson 
874dd2622a8SAlan Cox 	/*
875dd2622a8SAlan Cox 	 * If the map has changed, due to the subyte, the previous
876dd2622a8SAlan Cox 	 * output may be invalid.
877dd2622a8SAlan Cox 	 */
878dd2622a8SAlan Cox 	vm_map_lock_read(map);
879dd2622a8SAlan Cox 	if (timestamp != map->timestamp)
880dd2622a8SAlan Cox 		goto RestartScan;
881eff50fcdSAlan Cox 	vm_map_unlock_read(map);
882d2c60af8SMatthew Dillon done2:
883d2c60af8SMatthew Dillon 	return (error);
884df8bae1dSRodney W. Grimes }
885df8bae1dSRodney W. Grimes 
886d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
887df8bae1dSRodney W. Grimes struct mlock_args {
888651bb817SAlexander Langer 	const void *addr;
889df8bae1dSRodney W. Grimes 	size_t len;
890df8bae1dSRodney W. Grimes };
891d2d3e875SBruce Evans #endif
892d2c60af8SMatthew Dillon /*
893d2c60af8SMatthew Dillon  * MPSAFE
894d2c60af8SMatthew Dillon  */
895df8bae1dSRodney W. Grimes int
896b40ce416SJulian Elischer mlock(td, uap)
897b40ce416SJulian Elischer 	struct thread *td;
898df8bae1dSRodney W. Grimes 	struct mlock_args *uap;
899df8bae1dSRodney W. Grimes {
900f0ea4612SDon Lewis 	struct proc *proc;
901bb734798SDon Lewis 	vm_offset_t addr, end, last, start;
902bb734798SDon Lewis 	vm_size_t npages, size;
903bb734798SDon Lewis 	int error;
904df8bae1dSRodney W. Grimes 
90547934cefSDon Lewis 	error = suser(td);
90647934cefSDon Lewis 	if (error)
90747934cefSDon Lewis 		return (error);
90816929939SDon Lewis 	addr = (vm_offset_t)uap->addr;
90916929939SDon Lewis 	size = uap->len;
910bb734798SDon Lewis 	last = addr + size;
91116929939SDon Lewis 	start = trunc_page(addr);
912bb734798SDon Lewis 	end = round_page(last);
913bb734798SDon Lewis 	if (last < addr || end < addr)
914df8bae1dSRodney W. Grimes 		return (EINVAL);
91516929939SDon Lewis 	npages = atop(end - start);
91616929939SDon Lewis 	if (npages > vm_page_max_wired)
91716929939SDon Lewis 		return (ENOMEM);
918f0ea4612SDon Lewis 	proc = td->td_proc;
91947934cefSDon Lewis 	PROC_LOCK(proc);
920bb734798SDon Lewis 	if (ptoa(npages +
921bb734798SDon Lewis 	    pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
922bb734798SDon Lewis 	    lim_cur(proc, RLIMIT_MEMLOCK)) {
92347934cefSDon Lewis 		PROC_UNLOCK(proc);
9244a40e3d4SJohn Dyson 		return (ENOMEM);
92591d5354aSJohn Baldwin 	}
92647934cefSDon Lewis 	PROC_UNLOCK(proc);
92716929939SDon Lewis 	if (npages + cnt.v_wire_count > vm_page_max_wired)
92816929939SDon Lewis 		return (EAGAIN);
92916929939SDon Lewis 	error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
93016929939SDon Lewis 	    VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
931df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
932df8bae1dSRodney W. Grimes }
933df8bae1dSRodney W. Grimes 
934d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
9354a40e3d4SJohn Dyson struct mlockall_args {
9364a40e3d4SJohn Dyson 	int	how;
9374a40e3d4SJohn Dyson };
9384a40e3d4SJohn Dyson #endif
9394a40e3d4SJohn Dyson 
940d2c60af8SMatthew Dillon /*
941d2c60af8SMatthew Dillon  * MPSAFE
942d2c60af8SMatthew Dillon  */
9434a40e3d4SJohn Dyson int
944b40ce416SJulian Elischer mlockall(td, uap)
945b40ce416SJulian Elischer 	struct thread *td;
9464a40e3d4SJohn Dyson 	struct mlockall_args *uap;
9474a40e3d4SJohn Dyson {
948abd498aaSBruce M Simpson 	vm_map_t map;
949abd498aaSBruce M Simpson 	int error;
950abd498aaSBruce M Simpson 
951abd498aaSBruce M Simpson 	map = &td->td_proc->p_vmspace->vm_map;
952abd498aaSBruce M Simpson 	error = 0;
953abd498aaSBruce M Simpson 
954abd498aaSBruce M Simpson 	if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
955abd498aaSBruce M Simpson 		return (EINVAL);
956abd498aaSBruce M Simpson 
95711f7ddc5SBruce M Simpson #if 0
958abd498aaSBruce M Simpson 	/*
959abd498aaSBruce M Simpson 	 * If wiring all pages in the process would cause it to exceed
960abd498aaSBruce M Simpson 	 * a hard resource limit, return ENOMEM.
961abd498aaSBruce M Simpson 	 */
96291d5354aSJohn Baldwin 	PROC_LOCK(td->td_proc);
963abd498aaSBruce M Simpson 	if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
96491d5354aSJohn Baldwin 		lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
96591d5354aSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
966abd498aaSBruce M Simpson 		return (ENOMEM);
96791d5354aSJohn Baldwin 	}
96891d5354aSJohn Baldwin 	PROC_UNLOCK(td->td_proc);
969abd498aaSBruce M Simpson #else
970abd498aaSBruce M Simpson 	error = suser(td);
971abd498aaSBruce M Simpson 	if (error)
972abd498aaSBruce M Simpson 		return (error);
973abd498aaSBruce M Simpson #endif
974abd498aaSBruce M Simpson 
975abd498aaSBruce M Simpson 	if (uap->how & MCL_FUTURE) {
976abd498aaSBruce M Simpson 		vm_map_lock(map);
977abd498aaSBruce M Simpson 		vm_map_modflags(map, MAP_WIREFUTURE, 0);
978abd498aaSBruce M Simpson 		vm_map_unlock(map);
979abd498aaSBruce M Simpson 		error = 0;
980abd498aaSBruce M Simpson 	}
981abd498aaSBruce M Simpson 
982abd498aaSBruce M Simpson 	if (uap->how & MCL_CURRENT) {
983abd498aaSBruce M Simpson 		/*
984abd498aaSBruce M Simpson 		 * P1003.1-2001 mandates that all currently mapped pages
985abd498aaSBruce M Simpson 		 * will be memory resident and locked (wired) upon return
986abd498aaSBruce M Simpson 		 * from mlockall(). vm_map_wire() will wire pages, by
987abd498aaSBruce M Simpson 		 * calling vm_fault_wire() for each page in the region.
988abd498aaSBruce M Simpson 		 */
989abd498aaSBruce M Simpson 		error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
990abd498aaSBruce M Simpson 		    VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
991abd498aaSBruce M Simpson 		error = (error == KERN_SUCCESS ? 0 : EAGAIN);
992abd498aaSBruce M Simpson 	}
993abd498aaSBruce M Simpson 
994abd498aaSBruce M Simpson 	return (error);
9954a40e3d4SJohn Dyson }
9964a40e3d4SJohn Dyson 
9974a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_
998fa721254SAlfred Perlstein struct munlockall_args {
999abd498aaSBruce M Simpson 	register_t dummy;
10004a40e3d4SJohn Dyson };
10014a40e3d4SJohn Dyson #endif
10024a40e3d4SJohn Dyson 
1003d2c60af8SMatthew Dillon /*
1004d2c60af8SMatthew Dillon  * MPSAFE
1005d2c60af8SMatthew Dillon  */
10064a40e3d4SJohn Dyson int
1007b40ce416SJulian Elischer munlockall(td, uap)
1008b40ce416SJulian Elischer 	struct thread *td;
10094a40e3d4SJohn Dyson 	struct munlockall_args *uap;
10104a40e3d4SJohn Dyson {
1011abd498aaSBruce M Simpson 	vm_map_t map;
1012abd498aaSBruce M Simpson 	int error;
1013abd498aaSBruce M Simpson 
1014abd498aaSBruce M Simpson 	map = &td->td_proc->p_vmspace->vm_map;
1015abd498aaSBruce M Simpson 	error = suser(td);
1016abd498aaSBruce M Simpson 	if (error)
1017abd498aaSBruce M Simpson 		return (error);
1018abd498aaSBruce M Simpson 
1019abd498aaSBruce M Simpson 	/* Clear the MAP_WIREFUTURE flag from this vm_map. */
1020abd498aaSBruce M Simpson 	vm_map_lock(map);
1021abd498aaSBruce M Simpson 	vm_map_modflags(map, 0, MAP_WIREFUTURE);
1022abd498aaSBruce M Simpson 	vm_map_unlock(map);
1023abd498aaSBruce M Simpson 
1024abd498aaSBruce M Simpson 	/* Forcibly unwire all pages. */
1025abd498aaSBruce M Simpson 	error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1026abd498aaSBruce M Simpson 	    VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1027abd498aaSBruce M Simpson 
1028abd498aaSBruce M Simpson 	return (error);
10294a40e3d4SJohn Dyson }
10304a40e3d4SJohn Dyson 
10314a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_
1032df8bae1dSRodney W. Grimes struct munlock_args {
1033651bb817SAlexander Langer 	const void *addr;
1034df8bae1dSRodney W. Grimes 	size_t len;
1035df8bae1dSRodney W. Grimes };
1036d2d3e875SBruce Evans #endif
1037d2c60af8SMatthew Dillon /*
1038d2c60af8SMatthew Dillon  * MPSAFE
1039d2c60af8SMatthew Dillon  */
1040df8bae1dSRodney W. Grimes int
1041b40ce416SJulian Elischer munlock(td, uap)
1042b40ce416SJulian Elischer 	struct thread *td;
1043df8bae1dSRodney W. Grimes 	struct munlock_args *uap;
1044df8bae1dSRodney W. Grimes {
1045bb734798SDon Lewis 	vm_offset_t addr, end, last, start;
104616929939SDon Lewis 	vm_size_t size;
1047df8bae1dSRodney W. Grimes 	int error;
1048df8bae1dSRodney W. Grimes 
104947934cefSDon Lewis 	error = suser(td);
105047934cefSDon Lewis 	if (error)
105147934cefSDon Lewis 		return (error);
105216929939SDon Lewis 	addr = (vm_offset_t)uap->addr;
105316929939SDon Lewis 	size = uap->len;
1054bb734798SDon Lewis 	last = addr + size;
105516929939SDon Lewis 	start = trunc_page(addr);
1056bb734798SDon Lewis 	end = round_page(last);
1057bb734798SDon Lewis 	if (last < addr || end < addr)
1058df8bae1dSRodney W. Grimes 		return (EINVAL);
105916929939SDon Lewis 	error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
106016929939SDon Lewis 	    VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1061df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
1062df8bae1dSRodney W. Grimes }
1063df8bae1dSRodney W. Grimes 
1064df8bae1dSRodney W. Grimes /*
1065c8daea13SAlexander Kabaev  * vm_mmap_vnode()
1066c8daea13SAlexander Kabaev  *
1067c8daea13SAlexander Kabaev  * MPSAFE
1068c8daea13SAlexander Kabaev  *
1069c8daea13SAlexander Kabaev  * Helper function for vm_mmap.  Perform sanity check specific for mmap
1070c8daea13SAlexander Kabaev  * operations on vnodes.
1071c8daea13SAlexander Kabaev  */
1072c8daea13SAlexander Kabaev int
1073c8daea13SAlexander Kabaev vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1074c8daea13SAlexander Kabaev     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1075c8daea13SAlexander Kabaev     struct vnode *vp, vm_ooffset_t foff, vm_object_t *objp)
1076c8daea13SAlexander Kabaev {
1077c8daea13SAlexander Kabaev 	struct vattr va;
1078c8daea13SAlexander Kabaev 	void *handle;
1079c8daea13SAlexander Kabaev 	vm_object_t obj;
1080c8daea13SAlexander Kabaev 	int disablexworkaround, error, flags, type;
1081c8daea13SAlexander Kabaev 
1082c8daea13SAlexander Kabaev 	mtx_lock(&Giant);
1083c8daea13SAlexander Kabaev 	if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
1084c8daea13SAlexander Kabaev 		mtx_unlock(&Giant);
1085c8daea13SAlexander Kabaev 		return (error);
1086c8daea13SAlexander Kabaev 	}
1087c8daea13SAlexander Kabaev 	flags = *flagsp;
1088c8daea13SAlexander Kabaev 	if (vp->v_type == VREG) {
1089c8daea13SAlexander Kabaev 		/*
1090c8daea13SAlexander Kabaev 		 * Get the proper underlying object
1091c8daea13SAlexander Kabaev 		 */
1092c8daea13SAlexander Kabaev 		if (VOP_GETVOBJECT(vp, &obj) != 0) {
1093c8daea13SAlexander Kabaev 			error = EINVAL;
1094c8daea13SAlexander Kabaev 			goto done;
1095c8daea13SAlexander Kabaev 		}
1096c8daea13SAlexander Kabaev 		if (obj->handle != vp) {
1097c8daea13SAlexander Kabaev 			vput(vp);
1098c8daea13SAlexander Kabaev 			vp = (struct vnode*)obj->handle;
1099c8daea13SAlexander Kabaev 			vget(vp, LK_EXCLUSIVE, td);
1100c8daea13SAlexander Kabaev 		}
1101c8daea13SAlexander Kabaev 		type = OBJT_VNODE;
1102c8daea13SAlexander Kabaev 		handle = vp;
1103c8daea13SAlexander Kabaev 	} else if (vp->v_type == VCHR) {
1104c8daea13SAlexander Kabaev 		type = OBJT_DEVICE;
1105c8daea13SAlexander Kabaev 		handle = vp->v_rdev;
1106c8daea13SAlexander Kabaev 
1107c8daea13SAlexander Kabaev 		if(vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON) {
1108c8daea13SAlexander Kabaev 			*maxprotp = VM_PROT_ALL;
1109c8daea13SAlexander Kabaev 			*flagsp |= MAP_ANON;
1110c8daea13SAlexander Kabaev 			error = 0;
1111c8daea13SAlexander Kabaev 			goto done;
1112c8daea13SAlexander Kabaev 		}
1113c8daea13SAlexander Kabaev 		/*
1114c8daea13SAlexander Kabaev 		 * cdevs does not provide private mappings of any kind.
1115c8daea13SAlexander Kabaev 		 */
1116c8daea13SAlexander Kabaev 		/*
1117c8daea13SAlexander Kabaev 		 * However, for XIG X server to continue to work,
1118c8daea13SAlexander Kabaev 		 * we should allow the superuser to do it anyway.
1119c8daea13SAlexander Kabaev 		 * We only allow it at securelevel < 1.
1120c8daea13SAlexander Kabaev 		 * (Because the XIG X server writes directly to video
1121c8daea13SAlexander Kabaev 		 * memory via /dev/mem, it should never work at any
1122c8daea13SAlexander Kabaev 		 * other securelevel.
1123c8daea13SAlexander Kabaev 		 * XXX this will have to go
1124c8daea13SAlexander Kabaev 		 */
1125c8daea13SAlexander Kabaev 		if (securelevel_ge(td->td_ucred, 1))
1126c8daea13SAlexander Kabaev 			disablexworkaround = 1;
1127c8daea13SAlexander Kabaev 		else
1128c8daea13SAlexander Kabaev 			disablexworkaround = suser(td);
1129c8daea13SAlexander Kabaev 		if (disablexworkaround && (flags & (MAP_PRIVATE|MAP_COPY))) {
1130c8daea13SAlexander Kabaev 			error = EINVAL;
1131c8daea13SAlexander Kabaev 			goto done;
1132c8daea13SAlexander Kabaev 		}
1133c8daea13SAlexander Kabaev 		/*
1134c8daea13SAlexander Kabaev 		 * Force device mappings to be shared.
1135c8daea13SAlexander Kabaev 		 */
1136c8daea13SAlexander Kabaev 		flags &= ~(MAP_PRIVATE|MAP_COPY);
1137c8daea13SAlexander Kabaev 		flags |= MAP_SHARED;
1138c8daea13SAlexander Kabaev 	} else {
1139c8daea13SAlexander Kabaev 		error = EINVAL;
1140c8daea13SAlexander Kabaev 		goto done;
1141c8daea13SAlexander Kabaev 	}
1142c8daea13SAlexander Kabaev 	if ((error = VOP_GETATTR(vp, &va, td->td_ucred, td))) {
1143c8daea13SAlexander Kabaev 		goto done;
1144c8daea13SAlexander Kabaev 	}
1145c8daea13SAlexander Kabaev 	if ((flags & MAP_SHARED) != 0) {
1146c8daea13SAlexander Kabaev 		if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1147c8daea13SAlexander Kabaev 			if (prot & PROT_WRITE) {
1148c8daea13SAlexander Kabaev 				error = EPERM;
1149c8daea13SAlexander Kabaev 				goto done;
1150c8daea13SAlexander Kabaev 			}
1151c8daea13SAlexander Kabaev 			*maxprotp &= ~VM_PROT_WRITE;
1152c8daea13SAlexander Kabaev 		}
1153c8daea13SAlexander Kabaev #ifdef MAC
1154c8daea13SAlexander Kabaev 		error = mac_check_vnode_mmap(td->td_ucred, vp, prot);
1155c8daea13SAlexander Kabaev 		if (error != 0)
1156c8daea13SAlexander Kabaev 			goto done;
1157c8daea13SAlexander Kabaev #endif
1158c8daea13SAlexander Kabaev 	}
1159c8daea13SAlexander Kabaev 	/*
1160c8daea13SAlexander Kabaev 	 * If it is a regular file without any references
1161c8daea13SAlexander Kabaev 	 * we do not need to sync it.
1162c8daea13SAlexander Kabaev 	 * Adjust object size to be the size of actual file.
1163c8daea13SAlexander Kabaev 	 */
1164c8daea13SAlexander Kabaev 	if (vp->v_type == VREG) {
1165c8daea13SAlexander Kabaev 		objsize = round_page(va.va_size);
1166c8daea13SAlexander Kabaev 		if (va.va_nlink == 0)
1167c8daea13SAlexander Kabaev 			flags |= MAP_NOSYNC;
1168c8daea13SAlexander Kabaev 	}
1169c8daea13SAlexander Kabaev 	obj = vm_pager_allocate(type, handle, objsize, prot, foff);
1170c8daea13SAlexander Kabaev 	if (obj == NULL) {
1171c8daea13SAlexander Kabaev 		error = (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1172c8daea13SAlexander Kabaev 		goto done;
1173c8daea13SAlexander Kabaev 	}
1174c8daea13SAlexander Kabaev 	*objp = obj;
1175c8daea13SAlexander Kabaev 	*flagsp = flags;
1176c8daea13SAlexander Kabaev done:
1177c8daea13SAlexander Kabaev 	vput(vp);
1178c8daea13SAlexander Kabaev 	mtx_unlock(&Giant);
1179c8daea13SAlexander Kabaev 	return (error);
1180c8daea13SAlexander Kabaev }
1181c8daea13SAlexander Kabaev 
1182c8daea13SAlexander Kabaev /*
1183d2c60af8SMatthew Dillon  * vm_mmap()
1184d2c60af8SMatthew Dillon  *
1185d2c60af8SMatthew Dillon  * MPSAFE
1186d2c60af8SMatthew Dillon  *
1187d2c60af8SMatthew Dillon  * Internal version of mmap.  Currently used by mmap, exec, and sys5
1188d2c60af8SMatthew Dillon  * shared memory.  Handle is either a vnode pointer or NULL for MAP_ANON.
1189df8bae1dSRodney W. Grimes  */
1190df8bae1dSRodney W. Grimes int
1191b9dcd593SBruce Evans vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1192b9dcd593SBruce Evans 	vm_prot_t maxprot, int flags,
1193651bb817SAlexander Langer 	void *handle,
1194b9dcd593SBruce Evans 	vm_ooffset_t foff)
1195df8bae1dSRodney W. Grimes {
1196df8bae1dSRodney W. Grimes 	boolean_t fitit;
1197fcae040bSJohn Dyson 	vm_object_t object;
1198df8bae1dSRodney W. Grimes 	int rv = KERN_SUCCESS;
1199bd7e5f99SJohn Dyson 	vm_ooffset_t objsize;
120020eec4bbSAlan Cox 	int docow, error;
1201b40ce416SJulian Elischer 	struct thread *td = curthread;
1202df8bae1dSRodney W. Grimes 
1203df8bae1dSRodney W. Grimes 	if (size == 0)
1204df8bae1dSRodney W. Grimes 		return (0);
1205df8bae1dSRodney W. Grimes 
120606cb7259SDavid Greenman 	objsize = size = round_page(size);
1207df8bae1dSRodney W. Grimes 
120891d5354aSJohn Baldwin 	PROC_LOCK(td->td_proc);
1209070f64feSMatthew Dillon 	if (td->td_proc->p_vmspace->vm_map.size + size >
121091d5354aSJohn Baldwin 	    lim_cur(td->td_proc, RLIMIT_VMEM)) {
121191d5354aSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
1212070f64feSMatthew Dillon 		return(ENOMEM);
1213070f64feSMatthew Dillon 	}
121491d5354aSJohn Baldwin 	PROC_UNLOCK(td->td_proc);
1215070f64feSMatthew Dillon 
1216df8bae1dSRodney W. Grimes 	/*
1217bc9ad247SDavid Greenman 	 * We currently can only deal with page aligned file offsets.
1218bc9ad247SDavid Greenman 	 * The check is here rather than in the syscall because the
1219bc9ad247SDavid Greenman 	 * kernel calls this function internally for other mmaping
1220bc9ad247SDavid Greenman 	 * operations (such as in exec) and non-aligned offsets will
1221bc9ad247SDavid Greenman 	 * cause pmap inconsistencies...so we want to be sure to
1222bc9ad247SDavid Greenman 	 * disallow this in all cases.
1223bc9ad247SDavid Greenman 	 */
1224bc9ad247SDavid Greenman 	if (foff & PAGE_MASK)
1225bc9ad247SDavid Greenman 		return (EINVAL);
1226bc9ad247SDavid Greenman 
122706cb7259SDavid Greenman 	if ((flags & MAP_FIXED) == 0) {
122806cb7259SDavid Greenman 		fitit = TRUE;
122906cb7259SDavid Greenman 		*addr = round_page(*addr);
123006cb7259SDavid Greenman 	} else {
123106cb7259SDavid Greenman 		if (*addr != trunc_page(*addr))
123206cb7259SDavid Greenman 			return (EINVAL);
123306cb7259SDavid Greenman 		fitit = FALSE;
123406cb7259SDavid Greenman 		(void) vm_map_remove(map, *addr, *addr + size);
123506cb7259SDavid Greenman 	}
1236bc9ad247SDavid Greenman 	/*
123724a1cce3SDavid Greenman 	 * Lookup/allocate object.
1238df8bae1dSRodney W. Grimes 	 */
1239c8daea13SAlexander Kabaev 	if (handle != NULL) {
1240c8daea13SAlexander Kabaev 		error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1241c8daea13SAlexander Kabaev 		    handle, foff, &object);
1242c8daea13SAlexander Kabaev 		if (error) {
1243c8daea13SAlexander Kabaev 			return (error);
1244c8daea13SAlexander Kabaev 		}
1245c8daea13SAlexander Kabaev 	}
12465f55e841SDavid Greenman 	if (flags & MAP_ANON) {
1247c8daea13SAlexander Kabaev 		object = NULL;
1248c8daea13SAlexander Kabaev 		docow = 0;
12495f55e841SDavid Greenman 		/*
12505f55e841SDavid Greenman 		 * Unnamed anonymous regions always start at 0.
12515f55e841SDavid Greenman 		 */
125267bf6868SJohn Dyson 		if (handle == 0)
12535f55e841SDavid Greenman 			foff = 0;
12545f55e841SDavid Greenman 	} else {
12554738fa09SAlan Cox 		docow = MAP_PREFAULT_PARTIAL;
125694328e90SJohn Dyson 	}
1257df8bae1dSRodney W. Grimes 
12584f79d873SMatthew Dillon 	if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
12594738fa09SAlan Cox 		docow |= MAP_COPY_ON_WRITE;
12604f79d873SMatthew Dillon 	if (flags & MAP_NOSYNC)
12614f79d873SMatthew Dillon 		docow |= MAP_DISABLE_SYNCER;
12629730a5daSPaul Saab 	if (flags & MAP_NOCORE)
12639730a5daSPaul Saab 		docow |= MAP_DISABLE_COREDUMP;
12645850152dSJohn Dyson 
1265d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC)
1266d0aea04fSJohn Dyson 	if (prot & VM_PROT_READ)
1267d0aea04fSJohn Dyson 		prot |= VM_PROT_EXECUTE;
1268d0aea04fSJohn Dyson 
1269d0aea04fSJohn Dyson 	if (maxprot & VM_PROT_READ)
1270d0aea04fSJohn Dyson 		maxprot |= VM_PROT_EXECUTE;
1271d0aea04fSJohn Dyson #endif
1272d0aea04fSJohn Dyson 
1273e4ca250dSJohn Baldwin 	if (fitit)
12740a0a85b3SJohn Dyson 		*addr = pmap_addr_hint(object, *addr, size);
12750a0a85b3SJohn Dyson 
12762267af78SJulian Elischer 	if (flags & MAP_STACK)
1277fd75d710SMarcel Moolenaar 		rv = vm_map_stack(map, *addr, size, prot, maxprot,
1278fd75d710SMarcel Moolenaar 		    docow | MAP_STACK_GROWS_DOWN);
12792267af78SJulian Elischer 	else
1280bd7e5f99SJohn Dyson 		rv = vm_map_find(map, object, foff, addr, size, fitit,
1281bd7e5f99SJohn Dyson 				 prot, maxprot, docow);
1282bd7e5f99SJohn Dyson 
1283d2c60af8SMatthew Dillon 	if (rv != KERN_SUCCESS) {
12847fb0c17eSDavid Greenman 		/*
128524a1cce3SDavid Greenman 		 * Lose the object reference. Will destroy the
128624a1cce3SDavid Greenman 		 * object if it's an unnamed anonymous mapping
128724a1cce3SDavid Greenman 		 * or named anonymous without other references.
12887fb0c17eSDavid Greenman 		 */
1289df8bae1dSRodney W. Grimes 		vm_object_deallocate(object);
1290d2c60af8SMatthew Dillon 	} else if (flags & MAP_SHARED) {
1291df8bae1dSRodney W. Grimes 		/*
1292df8bae1dSRodney W. Grimes 		 * Shared memory is also shared with children.
1293df8bae1dSRodney W. Grimes 		 */
1294df8bae1dSRodney W. Grimes 		rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1295e4ca250dSJohn Baldwin 		if (rv != KERN_SUCCESS)
12967fb0c17eSDavid Greenman 			(void) vm_map_remove(map, *addr, *addr + size);
1297df8bae1dSRodney W. Grimes 	}
1298abd498aaSBruce M Simpson 
1299abd498aaSBruce M Simpson 	/*
1300abd498aaSBruce M Simpson 	 * If the process has requested that all future mappings
1301abd498aaSBruce M Simpson 	 * be wired, then heed this.
1302abd498aaSBruce M Simpson 	 */
1303abd498aaSBruce M Simpson 	if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1304abd498aaSBruce M Simpson 		vm_map_wire(map, *addr, *addr + size,
1305abd498aaSBruce M Simpson 		    VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1306abd498aaSBruce M Simpson 
1307df8bae1dSRodney W. Grimes 	switch (rv) {
1308df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
1309df8bae1dSRodney W. Grimes 		return (0);
1310df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
1311df8bae1dSRodney W. Grimes 	case KERN_NO_SPACE:
1312df8bae1dSRodney W. Grimes 		return (ENOMEM);
1313df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
1314df8bae1dSRodney W. Grimes 		return (EACCES);
1315df8bae1dSRodney W. Grimes 	default:
1316df8bae1dSRodney W. Grimes 		return (EINVAL);
1317df8bae1dSRodney W. Grimes 	}
1318df8bae1dSRodney W. Grimes }
1319