xref: /freebsd/sys/vm/vm_mmap.c (revision 3e732e7d7d39d56c3509496a4df311566f5f6f60)
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
41c3aac50fSPeter Wemm  * $FreeBSD$
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes /*
45df8bae1dSRodney W. Grimes  * Mapped file (mmap) interface to VM
46df8bae1dSRodney W. Grimes  */
47df8bae1dSRodney W. Grimes 
485591b823SEivind Eklund #include "opt_compat.h"
493e732e7dSRobert Watson #include "opt_mac.h"
50e9822d92SJoerg Wunsch 
51df8bae1dSRodney W. Grimes #include <sys/param.h>
52df8bae1dSRodney W. Grimes #include <sys/systm.h>
53fb919e4dSMark Murray #include <sys/kernel.h>
54fb919e4dSMark Murray #include <sys/lock.h>
5523955314SAlfred Perlstein #include <sys/mutex.h>
56d2d3e875SBruce Evans #include <sys/sysproto.h>
57df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
58df8bae1dSRodney W. Grimes #include <sys/proc.h>
59070f64feSMatthew Dillon #include <sys/resource.h>
60070f64feSMatthew Dillon #include <sys/resourcevar.h>
61df8bae1dSRodney W. Grimes #include <sys/vnode.h>
623ac4d1efSBruce Evans #include <sys/fcntl.h>
63df8bae1dSRodney W. Grimes #include <sys/file.h>
643e732e7dSRobert Watson #include <sys/mac.h>
65df8bae1dSRodney W. Grimes #include <sys/mman.h>
66df8bae1dSRodney W. Grimes #include <sys/conf.h>
674183b6b6SPeter Wemm #include <sys/stat.h>
68efeaf95aSDavid Greenman #include <sys/vmmeter.h>
691f6889a1SMatthew Dillon #include <sys/sysctl.h>
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <vm/vm.h>
72efeaf95aSDavid Greenman #include <vm/vm_param.h>
73efeaf95aSDavid Greenman #include <vm/pmap.h>
74efeaf95aSDavid Greenman #include <vm/vm_map.h>
75efeaf95aSDavid Greenman #include <vm/vm_object.h>
761c7c3c6aSMatthew Dillon #include <vm/vm_page.h>
77df8bae1dSRodney W. Grimes #include <vm/vm_pager.h>
78b5e8ce9fSBruce Evans #include <vm/vm_pageout.h>
79efeaf95aSDavid Greenman #include <vm/vm_extern.h>
80867a482dSJohn Dyson #include <vm/vm_page.h>
811f6889a1SMatthew Dillon #include <vm/vm_kern.h>
82df8bae1dSRodney W. Grimes 
83d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
84df8bae1dSRodney W. Grimes struct sbrk_args {
85df8bae1dSRodney W. Grimes 	int incr;
86df8bae1dSRodney W. Grimes };
87d2d3e875SBruce Evans #endif
880d94caffSDavid Greenman 
891f6889a1SMatthew Dillon static int max_proc_mmap;
901f6889a1SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
911f6889a1SMatthew Dillon 
921f6889a1SMatthew Dillon /*
931f6889a1SMatthew Dillon  * Set the maximum number of vm_map_entry structures per process.  Roughly
941f6889a1SMatthew Dillon  * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
951f6889a1SMatthew Dillon  * of our KVM malloc space still results in generous limits.  We want a
961f6889a1SMatthew Dillon  * default that is good enough to prevent the kernel running out of resources
971f6889a1SMatthew Dillon  * if attacked from compromised user account but generous enough such that
981f6889a1SMatthew Dillon  * multi-threaded processes are not unduly inconvenienced.
991f6889a1SMatthew Dillon  */
10011caded3SAlfred Perlstein static void vmmapentry_rsrc_init(void *);
1011f6889a1SMatthew Dillon SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL)
1021f6889a1SMatthew Dillon 
1031f6889a1SMatthew Dillon static void
1041f6889a1SMatthew Dillon vmmapentry_rsrc_init(dummy)
1051f6889a1SMatthew Dillon         void *dummy;
1061f6889a1SMatthew Dillon {
1071f6889a1SMatthew Dillon     max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
1081f6889a1SMatthew Dillon     max_proc_mmap /= 100;
1091f6889a1SMatthew Dillon }
1101f6889a1SMatthew Dillon 
111d2c60af8SMatthew Dillon /*
112d2c60af8SMatthew Dillon  * MPSAFE
113d2c60af8SMatthew Dillon  */
114df8bae1dSRodney W. Grimes /* ARGSUSED */
115df8bae1dSRodney W. Grimes int
116b40ce416SJulian Elischer sbrk(td, uap)
117b40ce416SJulian Elischer 	struct thread *td;
118df8bae1dSRodney W. Grimes 	struct sbrk_args *uap;
119df8bae1dSRodney W. Grimes {
120df8bae1dSRodney W. Grimes 	/* Not yet implemented */
1210cddd8f0SMatthew Dillon 	/* mtx_lock(&Giant); */
1220cddd8f0SMatthew Dillon 	/* mtx_unlock(&Giant); */
123df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
124df8bae1dSRodney W. Grimes }
125df8bae1dSRodney W. Grimes 
126d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
127df8bae1dSRodney W. Grimes struct sstk_args {
128df8bae1dSRodney W. Grimes 	int incr;
129df8bae1dSRodney W. Grimes };
130d2d3e875SBruce Evans #endif
1310d94caffSDavid Greenman 
132d2c60af8SMatthew Dillon /*
133d2c60af8SMatthew Dillon  * MPSAFE
134d2c60af8SMatthew Dillon  */
135df8bae1dSRodney W. Grimes /* ARGSUSED */
136df8bae1dSRodney W. Grimes int
137b40ce416SJulian Elischer sstk(td, uap)
138b40ce416SJulian Elischer 	struct thread *td;
139df8bae1dSRodney W. Grimes 	struct sstk_args *uap;
140df8bae1dSRodney W. Grimes {
141df8bae1dSRodney W. Grimes 	/* Not yet implemented */
1420cddd8f0SMatthew Dillon 	/* mtx_lock(&Giant); */
1430cddd8f0SMatthew Dillon 	/* mtx_unlock(&Giant); */
144df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
145df8bae1dSRodney W. Grimes }
146df8bae1dSRodney W. Grimes 
147df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
148d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
149df8bae1dSRodney W. Grimes struct getpagesize_args {
150df8bae1dSRodney W. Grimes 	int dummy;
151df8bae1dSRodney W. Grimes };
152d2d3e875SBruce Evans #endif
1530d94caffSDavid Greenman 
154df8bae1dSRodney W. Grimes /* ARGSUSED */
155df8bae1dSRodney W. Grimes int
156b40ce416SJulian Elischer ogetpagesize(td, uap)
157b40ce416SJulian Elischer 	struct thread *td;
158df8bae1dSRodney W. Grimes 	struct getpagesize_args *uap;
159df8bae1dSRodney W. Grimes {
1600cddd8f0SMatthew Dillon 	/* MP SAFE */
161b40ce416SJulian Elischer 	td->td_retval[0] = PAGE_SIZE;
162df8bae1dSRodney W. Grimes 	return (0);
163df8bae1dSRodney W. Grimes }
164df8bae1dSRodney W. Grimes #endif				/* COMPAT_43 || COMPAT_SUNOS */
165df8bae1dSRodney W. Grimes 
16654f42e4bSPeter Wemm 
16754f42e4bSPeter Wemm /*
16854f42e4bSPeter Wemm  * Memory Map (mmap) system call.  Note that the file offset
16954f42e4bSPeter Wemm  * and address are allowed to be NOT page aligned, though if
17054f42e4bSPeter Wemm  * the MAP_FIXED flag it set, both must have the same remainder
17154f42e4bSPeter Wemm  * modulo the PAGE_SIZE (POSIX 1003.1b).  If the address is not
17254f42e4bSPeter Wemm  * page-aligned, the actual mapping starts at trunc_page(addr)
17354f42e4bSPeter Wemm  * and the return value is adjusted up by the page offset.
174b4309055SMatthew Dillon  *
175b4309055SMatthew Dillon  * Generally speaking, only character devices which are themselves
176b4309055SMatthew Dillon  * memory-based, such as a video framebuffer, can be mmap'd.  Otherwise
177b4309055SMatthew Dillon  * there would be no cache coherency between a descriptor and a VM mapping
178b4309055SMatthew Dillon  * both to the same character device.
179b4309055SMatthew Dillon  *
180b4309055SMatthew Dillon  * Block devices can be mmap'd no matter what they represent.  Cache coherency
181b4309055SMatthew Dillon  * is maintained as long as you do not write directly to the underlying
182b4309055SMatthew Dillon  * character device.
18354f42e4bSPeter Wemm  */
184d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
185df8bae1dSRodney W. Grimes struct mmap_args {
186651bb817SAlexander Langer 	void *addr;
187df8bae1dSRodney W. Grimes 	size_t len;
188df8bae1dSRodney W. Grimes 	int prot;
189df8bae1dSRodney W. Grimes 	int flags;
190df8bae1dSRodney W. Grimes 	int fd;
191df8bae1dSRodney W. Grimes 	long pad;
192df8bae1dSRodney W. Grimes 	off_t pos;
193df8bae1dSRodney W. Grimes };
194d2d3e875SBruce Evans #endif
195df8bae1dSRodney W. Grimes 
196d2c60af8SMatthew Dillon /*
197d2c60af8SMatthew Dillon  * MPSAFE
198d2c60af8SMatthew Dillon  */
199df8bae1dSRodney W. Grimes int
200b40ce416SJulian Elischer mmap(td, uap)
201b40ce416SJulian Elischer 	struct thread *td;
20254d92145SMatthew Dillon 	struct mmap_args *uap;
203df8bae1dSRodney W. Grimes {
20454d92145SMatthew Dillon 	struct file *fp = NULL;
205df8bae1dSRodney W. Grimes 	struct vnode *vp;
206df8bae1dSRodney W. Grimes 	vm_offset_t addr;
2079154ee6aSPeter Wemm 	vm_size_t size, pageoff;
208df8bae1dSRodney W. Grimes 	vm_prot_t prot, maxprot;
209651bb817SAlexander Langer 	void *handle;
210df8bae1dSRodney W. Grimes 	int flags, error;
211c8bdd56bSGuido van Rooij 	int disablexworkaround;
21254f42e4bSPeter Wemm 	off_t pos;
213b40ce416SJulian Elischer 	struct vmspace *vms = td->td_proc->p_vmspace;
2149ff5ce6bSBoris Popov 	vm_object_t obj;
215df8bae1dSRodney W. Grimes 
21654f42e4bSPeter Wemm 	addr = (vm_offset_t) uap->addr;
21754f42e4bSPeter Wemm 	size = uap->len;
218df8bae1dSRodney W. Grimes 	prot = uap->prot & VM_PROT_ALL;
219df8bae1dSRodney W. Grimes 	flags = uap->flags;
22054f42e4bSPeter Wemm 	pos = uap->pos;
22154f42e4bSPeter Wemm 
222f6b5b182SJeff Roberson 	vp = NULL;
223426da3bcSAlfred Perlstein 	fp = NULL;
22454f42e4bSPeter Wemm 	/* make sure mapping fits into numeric range etc */
225fc565456SDmitrij Tejblum 	if ((ssize_t) uap->len < 0 ||
22654f42e4bSPeter Wemm 	    ((flags & MAP_ANON) && uap->fd != -1))
227df8bae1dSRodney W. Grimes 		return (EINVAL);
2289154ee6aSPeter Wemm 
2292267af78SJulian Elischer 	if (flags & MAP_STACK) {
2302267af78SJulian Elischer 		if ((uap->fd != -1) ||
2312267af78SJulian Elischer 		    ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
2322267af78SJulian Elischer 			return (EINVAL);
2332267af78SJulian Elischer 		flags |= MAP_ANON;
2342267af78SJulian Elischer 		pos = 0;
2352907af2aSJulian Elischer 	}
2362907af2aSJulian Elischer 
2379154ee6aSPeter Wemm 	/*
23854f42e4bSPeter Wemm 	 * Align the file position to a page boundary,
23954f42e4bSPeter Wemm 	 * and save its page offset component.
2409154ee6aSPeter Wemm 	 */
24154f42e4bSPeter Wemm 	pageoff = (pos & PAGE_MASK);
24254f42e4bSPeter Wemm 	pos -= pageoff;
24354f42e4bSPeter Wemm 
24454f42e4bSPeter Wemm 	/* Adjust size for rounding (on both ends). */
24554f42e4bSPeter Wemm 	size += pageoff;			/* low end... */
24654f42e4bSPeter Wemm 	size = (vm_size_t) round_page(size);	/* hi end */
2479154ee6aSPeter Wemm 
248df8bae1dSRodney W. Grimes 	/*
2490d94caffSDavid Greenman 	 * Check for illegal addresses.  Watch out for address wrap... Note
2500d94caffSDavid Greenman 	 * that VM_*_ADDRESS are not constants due to casts (argh).
251df8bae1dSRodney W. Grimes 	 */
252df8bae1dSRodney W. Grimes 	if (flags & MAP_FIXED) {
25354f42e4bSPeter Wemm 		/*
25454f42e4bSPeter Wemm 		 * The specified address must have the same remainder
25554f42e4bSPeter Wemm 		 * as the file offset taken modulo PAGE_SIZE, so it
25654f42e4bSPeter Wemm 		 * should be aligned after adjustment by pageoff.
25754f42e4bSPeter Wemm 		 */
25854f42e4bSPeter Wemm 		addr -= pageoff;
25954f42e4bSPeter Wemm 		if (addr & PAGE_MASK)
26054f42e4bSPeter Wemm 			return (EINVAL);
26154f42e4bSPeter Wemm 		/* Address range must be all in user VM space. */
26205ba50f5SJake Burkholder 		if (addr < vm_map_min(&vms->vm_map) ||
26305ba50f5SJake Burkholder 		    addr + size > vm_map_max(&vms->vm_map))
264df8bae1dSRodney W. Grimes 			return (EINVAL);
265bbc0ec52SDavid Greenman 		if (addr + size < addr)
266df8bae1dSRodney W. Grimes 			return (EINVAL);
267df8bae1dSRodney W. Grimes 	}
268df8bae1dSRodney W. Grimes 	/*
26954f42e4bSPeter Wemm 	 * XXX for non-fixed mappings where no hint is provided or
27054f42e4bSPeter Wemm 	 * the hint would fall in the potential heap space,
27154f42e4bSPeter Wemm 	 * place it after the end of the largest possible heap.
272df8bae1dSRodney W. Grimes 	 *
27354f42e4bSPeter Wemm 	 * There should really be a pmap call to determine a reasonable
27454f42e4bSPeter Wemm 	 * location.
275df8bae1dSRodney W. Grimes 	 */
276d28ab90fSLuoqi Chen 	else if (addr == 0 ||
2771f6889a1SMatthew Dillon 	    (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
278cbc89bfbSPaul Saab 	     addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz)))
279cbc89bfbSPaul Saab 		addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
28054f42e4bSPeter Wemm 
2810cddd8f0SMatthew Dillon 	mtx_lock(&Giant);	/* syscall marked mp-safe but isn't */
282df8bae1dSRodney W. Grimes 	if (flags & MAP_ANON) {
283df8bae1dSRodney W. Grimes 		/*
284df8bae1dSRodney W. Grimes 		 * Mapping blank space is trivial.
285df8bae1dSRodney W. Grimes 		 */
286df8bae1dSRodney W. Grimes 		handle = NULL;
287df8bae1dSRodney W. Grimes 		maxprot = VM_PROT_ALL;
28854f42e4bSPeter Wemm 		pos = 0;
289df8bae1dSRodney W. Grimes 	} else {
290df8bae1dSRodney W. Grimes 		/*
2910d94caffSDavid Greenman 		 * Mapping file, get fp for validation. Obtain vnode and make
2920d94caffSDavid Greenman 		 * sure it is of appropriate type.
293426da3bcSAlfred Perlstein 		 * don't let the descriptor disappear on us if we block
294df8bae1dSRodney W. Grimes 		 */
295a4db4953SAlfred Perlstein 		if ((error = fget(td, uap->fd, &fp)) != 0)
296426da3bcSAlfred Perlstein 			goto done;
297e4ca250dSJohn Baldwin 		if (fp->f_type != DTYPE_VNODE) {
298d2c60af8SMatthew Dillon 			error = EINVAL;
299426da3bcSAlfred Perlstein 			goto done;
300e4ca250dSJohn Baldwin 		}
301279d7226SMatthew Dillon 
302279d7226SMatthew Dillon 		/*
303aa543039SGarrett Wollman 		 * POSIX shared-memory objects are defined to have
304aa543039SGarrett Wollman 		 * kernel persistence, and are not defined to support
305aa543039SGarrett Wollman 		 * read(2)/write(2) -- or even open(2).  Thus, we can
306aa543039SGarrett Wollman 		 * use MAP_ASYNC to trade on-disk coherence for speed.
307aa543039SGarrett Wollman 		 * The shm_open(3) library routine turns on the FPOSIXSHM
308aa543039SGarrett Wollman 		 * flag to request this behavior.
309aa543039SGarrett Wollman 		 */
310aa543039SGarrett Wollman 		if (fp->f_flag & FPOSIXSHM)
311aa543039SGarrett Wollman 			flags |= MAP_NOSYNC;
312df8bae1dSRodney W. Grimes 		vp = (struct vnode *) fp->f_data;
313f6b5b182SJeff Roberson 		error = vget(vp, LK_EXCLUSIVE, td);
314f6b5b182SJeff Roberson 		if (error)
315f6b5b182SJeff Roberson 			goto done;
316e4ca250dSJohn Baldwin 		if (vp->v_type != VREG && vp->v_type != VCHR) {
317e4ca250dSJohn Baldwin 			error = EINVAL;
318e4ca250dSJohn Baldwin 			goto done;
319e4ca250dSJohn Baldwin 		}
3209ff5ce6bSBoris Popov 		if (vp->v_type == VREG) {
3219ff5ce6bSBoris Popov 			/*
3229ff5ce6bSBoris Popov 			 * Get the proper underlying object
3239ff5ce6bSBoris Popov 			 */
3240cddd8f0SMatthew Dillon 			if (VOP_GETVOBJECT(vp, &obj) != 0) {
3250cddd8f0SMatthew Dillon 				error = EINVAL;
3260cddd8f0SMatthew Dillon 				goto done;
3270cddd8f0SMatthew Dillon 			}
328f6b5b182SJeff Roberson 			if (obj->handle != vp) {
329f6b5b182SJeff Roberson 				vput(vp);
3309ff5ce6bSBoris Popov 				vp = (struct vnode*)obj->handle;
331f6b5b182SJeff Roberson 				vget(vp, LK_EXCLUSIVE, td);
332f6b5b182SJeff Roberson 			}
3339ff5ce6bSBoris Popov 		}
334df8bae1dSRodney W. Grimes 		/*
3350d94caffSDavid Greenman 		 * XXX hack to handle use of /dev/zero to map anon memory (ala
3360d94caffSDavid Greenman 		 * SunOS).
337df8bae1dSRodney W. Grimes 		 */
3382589f249SMark Murray 		if ((vp->v_type == VCHR) &&
3392589f249SMark Murray 		    (vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON)) {
340df8bae1dSRodney W. Grimes 			handle = NULL;
341df8bae1dSRodney W. Grimes 			maxprot = VM_PROT_ALL;
342df8bae1dSRodney W. Grimes 			flags |= MAP_ANON;
34354f42e4bSPeter Wemm 			pos = 0;
344df8bae1dSRodney W. Grimes 		} else {
345df8bae1dSRodney W. Grimes 			/*
346c8bdd56bSGuido van Rooij 			 * cdevs does not provide private mappings of any kind.
347c8bdd56bSGuido van Rooij 			 */
348c8bdd56bSGuido van Rooij 			/*
349c8bdd56bSGuido van Rooij 			 * However, for XIG X server to continue to work,
350c8bdd56bSGuido van Rooij 			 * we should allow the superuser to do it anyway.
351c8bdd56bSGuido van Rooij 			 * We only allow it at securelevel < 1.
352c8bdd56bSGuido van Rooij 			 * (Because the XIG X server writes directly to video
353c8bdd56bSGuido van Rooij 			 * memory via /dev/mem, it should never work at any
354c8bdd56bSGuido van Rooij 			 * other securelevel.
355c8bdd56bSGuido van Rooij 			 * XXX this will have to go
356c8bdd56bSGuido van Rooij 			 */
357a854ed98SJohn Baldwin 			if (securelevel_ge(td->td_ucred, 1))
358c8bdd56bSGuido van Rooij 				disablexworkaround = 1;
359c8bdd56bSGuido van Rooij 			else
36044731cabSJohn Baldwin 				disablexworkaround = suser(td);
361c8bdd56bSGuido van Rooij 			if (vp->v_type == VCHR && disablexworkaround &&
362279d7226SMatthew Dillon 			    (flags & (MAP_PRIVATE|MAP_COPY))) {
363279d7226SMatthew Dillon 				error = EINVAL;
364279d7226SMatthew Dillon 				goto done;
365279d7226SMatthew Dillon 			}
366c8bdd56bSGuido van Rooij 			/*
367df8bae1dSRodney W. Grimes 			 * Ensure that file and memory protections are
368df8bae1dSRodney W. Grimes 			 * compatible.  Note that we only worry about
369df8bae1dSRodney W. Grimes 			 * writability if mapping is shared; in this case,
370df8bae1dSRodney W. Grimes 			 * current and max prot are dictated by the open file.
371df8bae1dSRodney W. Grimes 			 * XXX use the vnode instead?  Problem is: what
3720d94caffSDavid Greenman 			 * credentials do we use for determination? What if
3730d94caffSDavid Greenman 			 * proc does a setuid?
374df8bae1dSRodney W. Grimes 			 */
375df8bae1dSRodney W. Grimes 			maxprot = VM_PROT_EXECUTE;	/* ??? */
376279d7226SMatthew Dillon 			if (fp->f_flag & FREAD) {
377df8bae1dSRodney W. Grimes 				maxprot |= VM_PROT_READ;
378279d7226SMatthew Dillon 			} else if (prot & PROT_READ) {
379279d7226SMatthew Dillon 				error = EACCES;
380279d7226SMatthew Dillon 				goto done;
381279d7226SMatthew Dillon 			}
382c8bdd56bSGuido van Rooij 			/*
383c8bdd56bSGuido van Rooij 			 * If we are sharing potential changes (either via
384c8bdd56bSGuido van Rooij 			 * MAP_SHARED or via the implicit sharing of character
385c8bdd56bSGuido van Rooij 			 * device mappings), and we are trying to get write
386c8bdd56bSGuido van Rooij 			 * permission although we opened it without asking
387c8bdd56bSGuido van Rooij 			 * for it, bail out.  Check for superuser, only if
388c8bdd56bSGuido van Rooij 			 * we're at securelevel < 1, to allow the XIG X server
389c8bdd56bSGuido van Rooij 			 * to continue to work.
390c8bdd56bSGuido van Rooij 			 */
39105feb99fSGuido van Rooij 			if ((flags & MAP_SHARED) != 0 ||
39205feb99fSGuido van Rooij 			    (vp->v_type == VCHR && disablexworkaround)) {
39305feb99fSGuido van Rooij 				if ((fp->f_flag & FWRITE) != 0) {
3944183b6b6SPeter Wemm 					struct vattr va;
39505feb99fSGuido van Rooij 					if ((error =
39605feb99fSGuido van Rooij 					    VOP_GETATTR(vp, &va,
397a854ed98SJohn Baldwin 						        td->td_ucred, td))) {
398279d7226SMatthew Dillon 						goto done;
399279d7226SMatthew Dillon 					}
40005feb99fSGuido van Rooij 					if ((va.va_flags &
401279d7226SMatthew Dillon 					   (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0) {
402df8bae1dSRodney W. Grimes 						maxprot |= VM_PROT_WRITE;
403279d7226SMatthew Dillon 					} else if (prot & PROT_WRITE) {
404279d7226SMatthew Dillon 						error = EPERM;
405279d7226SMatthew Dillon 						goto done;
406279d7226SMatthew Dillon 					}
407279d7226SMatthew Dillon 				} else if ((prot & PROT_WRITE) != 0) {
408279d7226SMatthew Dillon 					error = EACCES;
409279d7226SMatthew Dillon 					goto done;
410279d7226SMatthew Dillon 				}
411279d7226SMatthew Dillon 			} else {
41205feb99fSGuido van Rooij 				maxprot |= VM_PROT_WRITE;
413279d7226SMatthew Dillon 			}
41405feb99fSGuido van Rooij 
415651bb817SAlexander Langer 			handle = (void *)vp;
416df8bae1dSRodney W. Grimes 		}
417df8bae1dSRodney W. Grimes 	}
4181f6889a1SMatthew Dillon 
4191f6889a1SMatthew Dillon 	/*
4201f6889a1SMatthew Dillon 	 * Do not allow more then a certain number of vm_map_entry structures
4211f6889a1SMatthew Dillon 	 * per process.  Scale with the number of rforks sharing the map
4221f6889a1SMatthew Dillon 	 * to make the limit reasonable for threads.
4231f6889a1SMatthew Dillon 	 */
4241f6889a1SMatthew Dillon 	if (max_proc_mmap &&
4251f6889a1SMatthew Dillon 	    vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
426279d7226SMatthew Dillon 		error = ENOMEM;
427279d7226SMatthew Dillon 		goto done;
4281f6889a1SMatthew Dillon 	}
4291f6889a1SMatthew Dillon 
430e4ca250dSJohn Baldwin 	mtx_unlock(&Giant);
4313e732e7dSRobert Watson 	error = 0;
4323e732e7dSRobert Watson #ifdef MAC
4333e732e7dSRobert Watson 	if (handle != NULL && (flags & MAP_SHARED) != 0) {
4343e732e7dSRobert Watson 		error = mac_check_vnode_mmap(td->td_ucred,
4353e732e7dSRobert Watson 		    (struct vnode *)handle, prot);
4363e732e7dSRobert Watson 	}
4373e732e7dSRobert Watson #endif
4383e732e7dSRobert Watson 	if (error == 0)
4391f6889a1SMatthew Dillon 		error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
44054f42e4bSPeter Wemm 		    flags, handle, pos);
441f6b5b182SJeff Roberson 	mtx_lock(&Giant);
442df8bae1dSRodney W. Grimes 	if (error == 0)
443b40ce416SJulian Elischer 		td->td_retval[0] = (register_t) (addr + pageoff);
444279d7226SMatthew Dillon done:
445f6b5b182SJeff Roberson 	if (vp)
446f6b5b182SJeff Roberson 		vput(vp);
4472cd301d1SAlan Cox 	mtx_unlock(&Giant);
448279d7226SMatthew Dillon 	if (fp)
449b40ce416SJulian Elischer 		fdrop(fp, td);
450f6b5b182SJeff Roberson 
451df8bae1dSRodney W. Grimes 	return (error);
452df8bae1dSRodney W. Grimes }
453df8bae1dSRodney W. Grimes 
45405f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43
455d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
45605f0fdd2SPoul-Henning Kamp struct ommap_args {
45705f0fdd2SPoul-Henning Kamp 	caddr_t addr;
45805f0fdd2SPoul-Henning Kamp 	int len;
45905f0fdd2SPoul-Henning Kamp 	int prot;
46005f0fdd2SPoul-Henning Kamp 	int flags;
46105f0fdd2SPoul-Henning Kamp 	int fd;
46205f0fdd2SPoul-Henning Kamp 	long pos;
46305f0fdd2SPoul-Henning Kamp };
464d2d3e875SBruce Evans #endif
46505f0fdd2SPoul-Henning Kamp int
466b40ce416SJulian Elischer ommap(td, uap)
467b40ce416SJulian Elischer 	struct thread *td;
46854d92145SMatthew Dillon 	struct ommap_args *uap;
46905f0fdd2SPoul-Henning Kamp {
47005f0fdd2SPoul-Henning Kamp 	struct mmap_args nargs;
47105f0fdd2SPoul-Henning Kamp 	static const char cvtbsdprot[8] = {
47205f0fdd2SPoul-Henning Kamp 		0,
47305f0fdd2SPoul-Henning Kamp 		PROT_EXEC,
47405f0fdd2SPoul-Henning Kamp 		PROT_WRITE,
47505f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE,
47605f0fdd2SPoul-Henning Kamp 		PROT_READ,
47705f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_READ,
47805f0fdd2SPoul-Henning Kamp 		PROT_WRITE | PROT_READ,
47905f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE | PROT_READ,
48005f0fdd2SPoul-Henning Kamp 	};
4810d94caffSDavid Greenman 
48205f0fdd2SPoul-Henning Kamp #define	OMAP_ANON	0x0002
48305f0fdd2SPoul-Henning Kamp #define	OMAP_COPY	0x0020
48405f0fdd2SPoul-Henning Kamp #define	OMAP_SHARED	0x0010
48505f0fdd2SPoul-Henning Kamp #define	OMAP_FIXED	0x0100
48605f0fdd2SPoul-Henning Kamp 
48705f0fdd2SPoul-Henning Kamp 	nargs.addr = uap->addr;
48805f0fdd2SPoul-Henning Kamp 	nargs.len = uap->len;
48905f0fdd2SPoul-Henning Kamp 	nargs.prot = cvtbsdprot[uap->prot & 0x7];
49005f0fdd2SPoul-Henning Kamp 	nargs.flags = 0;
49105f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_ANON)
49205f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_ANON;
49305f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_COPY)
49405f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_COPY;
49505f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_SHARED)
49605f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_SHARED;
49705f0fdd2SPoul-Henning Kamp 	else
49805f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_PRIVATE;
49905f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_FIXED)
50005f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_FIXED;
50105f0fdd2SPoul-Henning Kamp 	nargs.fd = uap->fd;
50205f0fdd2SPoul-Henning Kamp 	nargs.pos = uap->pos;
503b40ce416SJulian Elischer 	return (mmap(td, &nargs));
50405f0fdd2SPoul-Henning Kamp }
50505f0fdd2SPoul-Henning Kamp #endif				/* COMPAT_43 */
50605f0fdd2SPoul-Henning Kamp 
50705f0fdd2SPoul-Henning Kamp 
508d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
509df8bae1dSRodney W. Grimes struct msync_args {
510651bb817SAlexander Langer 	void *addr;
511df8bae1dSRodney W. Grimes 	int len;
512e6c6af11SDavid Greenman 	int flags;
513df8bae1dSRodney W. Grimes };
514d2d3e875SBruce Evans #endif
515d2c60af8SMatthew Dillon /*
516d2c60af8SMatthew Dillon  * MPSAFE
517d2c60af8SMatthew Dillon  */
518df8bae1dSRodney W. Grimes int
519b40ce416SJulian Elischer msync(td, uap)
520b40ce416SJulian Elischer 	struct thread *td;
521df8bae1dSRodney W. Grimes 	struct msync_args *uap;
522df8bae1dSRodney W. Grimes {
523df8bae1dSRodney W. Grimes 	vm_offset_t addr;
524dabee6feSPeter Wemm 	vm_size_t size, pageoff;
525e6c6af11SDavid Greenman 	int flags;
526df8bae1dSRodney W. Grimes 	vm_map_t map;
527df8bae1dSRodney W. Grimes 	int rv;
528df8bae1dSRodney W. Grimes 
529df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
5309154ee6aSPeter Wemm 	size = uap->len;
531e6c6af11SDavid Greenman 	flags = uap->flags;
532e6c6af11SDavid Greenman 
533dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
534dabee6feSPeter Wemm 	addr -= pageoff;
535dabee6feSPeter Wemm 	size += pageoff;
536dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
5379154ee6aSPeter Wemm 	if (addr + size < addr)
538dabee6feSPeter Wemm 		return (EINVAL);
539dabee6feSPeter Wemm 
540dabee6feSPeter Wemm 	if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
5411e62bc63SDavid Greenman 		return (EINVAL);
5421e62bc63SDavid Greenman 
5430cddd8f0SMatthew Dillon 	mtx_lock(&Giant);
5440cddd8f0SMatthew Dillon 
545b40ce416SJulian Elischer 	map = &td->td_proc->p_vmspace->vm_map;
5469154ee6aSPeter Wemm 
547df8bae1dSRodney W. Grimes 	/*
548df8bae1dSRodney W. Grimes 	 * XXX Gak!  If size is zero we are supposed to sync "all modified
5490d94caffSDavid Greenman 	 * pages with the region containing addr".  Unfortunately, we don't
5500d94caffSDavid Greenman 	 * really keep track of individual mmaps so we approximate by flushing
5510d94caffSDavid Greenman 	 * the range of the map entry containing addr. This can be incorrect
5520d94caffSDavid Greenman 	 * if the region splits or is coalesced with a neighbor.
553df8bae1dSRodney W. Grimes 	 */
554df8bae1dSRodney W. Grimes 	if (size == 0) {
555df8bae1dSRodney W. Grimes 		vm_map_entry_t entry;
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes 		vm_map_lock_read(map);
558df8bae1dSRodney W. Grimes 		rv = vm_map_lookup_entry(map, addr, &entry);
559df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
56023955314SAlfred Perlstein 		if (rv == FALSE) {
561d2c60af8SMatthew Dillon 			rv = -1;
562d2c60af8SMatthew Dillon 			goto done2;
56323955314SAlfred Perlstein 		}
564df8bae1dSRodney W. Grimes 		addr = entry->start;
565df8bae1dSRodney W. Grimes 		size = entry->end - entry->start;
566df8bae1dSRodney W. Grimes 	}
567e6c6af11SDavid Greenman 
568df8bae1dSRodney W. Grimes 	/*
569df8bae1dSRodney W. Grimes 	 * Clean the pages and interpret the return value.
570df8bae1dSRodney W. Grimes 	 */
5716c534ad8SDavid Greenman 	rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
572e6c6af11SDavid Greenman 	    (flags & MS_INVALIDATE) != 0);
573e6c6af11SDavid Greenman 
574d2c60af8SMatthew Dillon done2:
575190609ddSJohn Baldwin 	mtx_unlock(&Giant);
5760cddd8f0SMatthew Dillon 
577df8bae1dSRodney W. Grimes 	switch (rv) {
578df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
579d2c60af8SMatthew Dillon 		return (0);
580df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
581df8bae1dSRodney W. Grimes 		return (EINVAL);	/* Sun returns ENOMEM? */
582df8bae1dSRodney W. Grimes 	case KERN_FAILURE:
583df8bae1dSRodney W. Grimes 		return (EIO);
584df8bae1dSRodney W. Grimes 	default:
585df8bae1dSRodney W. Grimes 		return (EINVAL);
586df8bae1dSRodney W. Grimes 	}
587df8bae1dSRodney W. Grimes }
588df8bae1dSRodney W. Grimes 
589d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
590df8bae1dSRodney W. Grimes struct munmap_args {
591651bb817SAlexander Langer 	void *addr;
5929154ee6aSPeter Wemm 	size_t len;
593df8bae1dSRodney W. Grimes };
594d2d3e875SBruce Evans #endif
595d2c60af8SMatthew Dillon /*
596d2c60af8SMatthew Dillon  * MPSAFE
597d2c60af8SMatthew Dillon  */
598df8bae1dSRodney W. Grimes int
599b40ce416SJulian Elischer munmap(td, uap)
600b40ce416SJulian Elischer 	struct thread *td;
60154d92145SMatthew Dillon 	struct munmap_args *uap;
602df8bae1dSRodney W. Grimes {
603df8bae1dSRodney W. Grimes 	vm_offset_t addr;
604dabee6feSPeter Wemm 	vm_size_t size, pageoff;
605df8bae1dSRodney W. Grimes 	vm_map_t map;
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
6089154ee6aSPeter Wemm 	size = uap->len;
609dabee6feSPeter Wemm 
610dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
611dabee6feSPeter Wemm 	addr -= pageoff;
612dabee6feSPeter Wemm 	size += pageoff;
613dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
6149154ee6aSPeter Wemm 	if (addr + size < addr)
615df8bae1dSRodney W. Grimes 		return (EINVAL);
6169154ee6aSPeter Wemm 
617df8bae1dSRodney W. Grimes 	if (size == 0)
618df8bae1dSRodney W. Grimes 		return (0);
619dabee6feSPeter Wemm 
620df8bae1dSRodney W. Grimes 	/*
62105ba50f5SJake Burkholder 	 * Check for illegal addresses.  Watch out for address wrap...
622df8bae1dSRodney W. Grimes 	 */
623b40ce416SJulian Elischer 	map = &td->td_proc->p_vmspace->vm_map;
62405ba50f5SJake Burkholder 	if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
62505ba50f5SJake Burkholder 		return (EINVAL);
626df8bae1dSRodney W. Grimes 	/*
627df8bae1dSRodney W. Grimes 	 * Make sure entire range is allocated.
628df8bae1dSRodney W. Grimes 	 */
6298c5c5d04SAlan Cox 	if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
630df8bae1dSRodney W. Grimes 		return (EINVAL);
6318c5c5d04SAlan Cox 
632df8bae1dSRodney W. Grimes 	/* returns nothing but KERN_SUCCESS anyway */
633df8bae1dSRodney W. Grimes 	(void) vm_map_remove(map, addr, addr + size);
634df8bae1dSRodney W. Grimes 	return (0);
635df8bae1dSRodney W. Grimes }
636df8bae1dSRodney W. Grimes 
637279d7226SMatthew Dillon #if 0
638df8bae1dSRodney W. Grimes void
639b40ce416SJulian Elischer munmapfd(td, fd)
640b40ce416SJulian Elischer 	struct thread *td;
641df8bae1dSRodney W. Grimes 	int fd;
642df8bae1dSRodney W. Grimes {
643df8bae1dSRodney W. Grimes 	/*
644c4ed5a07SDavid Greenman 	 * XXX should unmap any regions mapped to this file
645df8bae1dSRodney W. Grimes 	 */
646426da3bcSAlfred Perlstein 	FILEDESC_LOCK(p->p_fd);
647b40ce416SJulian Elischer 	td->td_proc->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
648426da3bcSAlfred Perlstein 	FILEDESC_UNLOCK(p->p_fd);
649df8bae1dSRodney W. Grimes }
650279d7226SMatthew Dillon #endif
651df8bae1dSRodney W. Grimes 
652d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
653df8bae1dSRodney W. Grimes struct mprotect_args {
654651bb817SAlexander Langer 	const void *addr;
6559154ee6aSPeter Wemm 	size_t len;
656df8bae1dSRodney W. Grimes 	int prot;
657df8bae1dSRodney W. Grimes };
658d2d3e875SBruce Evans #endif
659d2c60af8SMatthew Dillon /*
660d2c60af8SMatthew Dillon  * MPSAFE
661d2c60af8SMatthew Dillon  */
662df8bae1dSRodney W. Grimes int
663b40ce416SJulian Elischer mprotect(td, uap)
664b40ce416SJulian Elischer 	struct thread *td;
665df8bae1dSRodney W. Grimes 	struct mprotect_args *uap;
666df8bae1dSRodney W. Grimes {
667df8bae1dSRodney W. Grimes 	vm_offset_t addr;
668dabee6feSPeter Wemm 	vm_size_t size, pageoff;
66954d92145SMatthew Dillon 	vm_prot_t prot;
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
6729154ee6aSPeter Wemm 	size = uap->len;
673df8bae1dSRodney W. Grimes 	prot = uap->prot & VM_PROT_ALL;
674d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC)
675d0aea04fSJohn Dyson 	if (prot & VM_PROT_READ)
676d0aea04fSJohn Dyson 		prot |= VM_PROT_EXECUTE;
677d0aea04fSJohn Dyson #endif
678df8bae1dSRodney W. Grimes 
679dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
680dabee6feSPeter Wemm 	addr -= pageoff;
681dabee6feSPeter Wemm 	size += pageoff;
682dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
6839154ee6aSPeter Wemm 	if (addr + size < addr)
684dabee6feSPeter Wemm 		return (EINVAL);
685dabee6feSPeter Wemm 
68643285049SAlan Cox 	switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
68743285049SAlan Cox 	    addr + size, prot, FALSE)) {
688df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
689df8bae1dSRodney W. Grimes 		return (0);
690df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
691df8bae1dSRodney W. Grimes 		return (EACCES);
692df8bae1dSRodney W. Grimes 	}
693df8bae1dSRodney W. Grimes 	return (EINVAL);
694df8bae1dSRodney W. Grimes }
695df8bae1dSRodney W. Grimes 
696d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
697dabee6feSPeter Wemm struct minherit_args {
698651bb817SAlexander Langer 	void *addr;
6999154ee6aSPeter Wemm 	size_t len;
700dabee6feSPeter Wemm 	int inherit;
701dabee6feSPeter Wemm };
702dabee6feSPeter Wemm #endif
703d2c60af8SMatthew Dillon /*
704d2c60af8SMatthew Dillon  * MPSAFE
705d2c60af8SMatthew Dillon  */
706dabee6feSPeter Wemm int
707b40ce416SJulian Elischer minherit(td, uap)
708b40ce416SJulian Elischer 	struct thread *td;
709dabee6feSPeter Wemm 	struct minherit_args *uap;
710dabee6feSPeter Wemm {
711dabee6feSPeter Wemm 	vm_offset_t addr;
712dabee6feSPeter Wemm 	vm_size_t size, pageoff;
71354d92145SMatthew Dillon 	vm_inherit_t inherit;
714dabee6feSPeter Wemm 
715dabee6feSPeter Wemm 	addr = (vm_offset_t)uap->addr;
7169154ee6aSPeter Wemm 	size = uap->len;
717dabee6feSPeter Wemm 	inherit = uap->inherit;
718dabee6feSPeter Wemm 
719dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
720dabee6feSPeter Wemm 	addr -= pageoff;
721dabee6feSPeter Wemm 	size += pageoff;
722dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
7239154ee6aSPeter Wemm 	if (addr + size < addr)
724dabee6feSPeter Wemm 		return (EINVAL);
725dabee6feSPeter Wemm 
726e0be79afSAlan Cox 	switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
727e0be79afSAlan Cox 	    addr + size, inherit)) {
728dabee6feSPeter Wemm 	case KERN_SUCCESS:
729dabee6feSPeter Wemm 		return (0);
730dabee6feSPeter Wemm 	case KERN_PROTECTION_FAILURE:
731dabee6feSPeter Wemm 		return (EACCES);
732dabee6feSPeter Wemm 	}
733dabee6feSPeter Wemm 	return (EINVAL);
734dabee6feSPeter Wemm }
735dabee6feSPeter Wemm 
736dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_
737df8bae1dSRodney W. Grimes struct madvise_args {
738651bb817SAlexander Langer 	void *addr;
7399154ee6aSPeter Wemm 	size_t len;
740df8bae1dSRodney W. Grimes 	int behav;
741df8bae1dSRodney W. Grimes };
742d2d3e875SBruce Evans #endif
7430d94caffSDavid Greenman 
744d2c60af8SMatthew Dillon /*
745d2c60af8SMatthew Dillon  * MPSAFE
746d2c60af8SMatthew Dillon  */
747df8bae1dSRodney W. Grimes /* ARGSUSED */
748df8bae1dSRodney W. Grimes int
749b40ce416SJulian Elischer madvise(td, uap)
750b40ce416SJulian Elischer 	struct thread *td;
751df8bae1dSRodney W. Grimes 	struct madvise_args *uap;
752df8bae1dSRodney W. Grimes {
753f35329acSJohn Dyson 	vm_offset_t start, end;
75405ba50f5SJake Burkholder 	vm_map_t map;
755b4309055SMatthew Dillon 
756b4309055SMatthew Dillon 	/*
757b4309055SMatthew Dillon 	 * Check for illegal behavior
758b4309055SMatthew Dillon 	 */
7599730a5daSPaul Saab 	if (uap->behav < 0 || uap->behav > MADV_CORE)
760b4309055SMatthew Dillon 		return (EINVAL);
761867a482dSJohn Dyson 	/*
762867a482dSJohn Dyson 	 * Check for illegal addresses.  Watch out for address wrap... Note
763867a482dSJohn Dyson 	 * that VM_*_ADDRESS are not constants due to casts (argh).
764867a482dSJohn Dyson 	 */
76505ba50f5SJake Burkholder 	map = &td->td_proc->p_vmspace->vm_map;
76605ba50f5SJake Burkholder 	if ((vm_offset_t)uap->addr < vm_map_min(map) ||
76705ba50f5SJake Burkholder 	    (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
768867a482dSJohn Dyson 		return (EINVAL);
769867a482dSJohn Dyson 	if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
770867a482dSJohn Dyson 		return (EINVAL);
771867a482dSJohn Dyson 
772867a482dSJohn Dyson 	/*
773867a482dSJohn Dyson 	 * Since this routine is only advisory, we default to conservative
774867a482dSJohn Dyson 	 * behavior.
775867a482dSJohn Dyson 	 */
776cd6eea25SDavid Greenman 	start = trunc_page((vm_offset_t) uap->addr);
777cd6eea25SDavid Greenman 	end = round_page((vm_offset_t) uap->addr + uap->len);
778867a482dSJohn Dyson 
77905ba50f5SJake Burkholder 	if (vm_map_madvise(map, start, end, uap->behav))
780094f6d26SAlan Cox 		return (EINVAL);
781094f6d26SAlan Cox 	return (0);
782df8bae1dSRodney W. Grimes }
783df8bae1dSRodney W. Grimes 
784d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
785df8bae1dSRodney W. Grimes struct mincore_args {
786651bb817SAlexander Langer 	const void *addr;
7879154ee6aSPeter Wemm 	size_t len;
788df8bae1dSRodney W. Grimes 	char *vec;
789df8bae1dSRodney W. Grimes };
790d2d3e875SBruce Evans #endif
7910d94caffSDavid Greenman 
792d2c60af8SMatthew Dillon /*
793d2c60af8SMatthew Dillon  * MPSAFE
794d2c60af8SMatthew Dillon  */
795df8bae1dSRodney W. Grimes /* ARGSUSED */
796df8bae1dSRodney W. Grimes int
797b40ce416SJulian Elischer mincore(td, uap)
798b40ce416SJulian Elischer 	struct thread *td;
799df8bae1dSRodney W. Grimes 	struct mincore_args *uap;
800df8bae1dSRodney W. Grimes {
801867a482dSJohn Dyson 	vm_offset_t addr, first_addr;
802867a482dSJohn Dyson 	vm_offset_t end, cend;
803867a482dSJohn Dyson 	pmap_t pmap;
804867a482dSJohn Dyson 	vm_map_t map;
80502c04a2fSJohn Dyson 	char *vec;
806d2c60af8SMatthew Dillon 	int error = 0;
807867a482dSJohn Dyson 	int vecindex, lastvecindex;
80854d92145SMatthew Dillon 	vm_map_entry_t current;
809867a482dSJohn Dyson 	vm_map_entry_t entry;
810867a482dSJohn Dyson 	int mincoreinfo;
811dd2622a8SAlan Cox 	unsigned int timestamp;
812df8bae1dSRodney W. Grimes 
813867a482dSJohn Dyson 	/*
814867a482dSJohn Dyson 	 * Make sure that the addresses presented are valid for user
815867a482dSJohn Dyson 	 * mode.
816867a482dSJohn Dyson 	 */
817867a482dSJohn Dyson 	first_addr = addr = trunc_page((vm_offset_t) uap->addr);
8189154ee6aSPeter Wemm 	end = addr + (vm_size_t)round_page(uap->len);
81905ba50f5SJake Burkholder 	map = &td->td_proc->p_vmspace->vm_map;
82005ba50f5SJake Burkholder 	if (end > vm_map_max(map) || end < addr)
82102c04a2fSJohn Dyson 		return (EINVAL);
82202c04a2fSJohn Dyson 
823867a482dSJohn Dyson 	/*
824867a482dSJohn Dyson 	 * Address of byte vector
825867a482dSJohn Dyson 	 */
82602c04a2fSJohn Dyson 	vec = uap->vec;
827867a482dSJohn Dyson 
828190609ddSJohn Baldwin 	mtx_lock(&Giant);
829b40ce416SJulian Elischer 	pmap = vmspace_pmap(td->td_proc->p_vmspace);
830867a482dSJohn Dyson 
831eff50fcdSAlan Cox 	vm_map_lock_read(map);
832dd2622a8SAlan Cox RestartScan:
833dd2622a8SAlan Cox 	timestamp = map->timestamp;
834867a482dSJohn Dyson 
835867a482dSJohn Dyson 	if (!vm_map_lookup_entry(map, addr, &entry))
836867a482dSJohn Dyson 		entry = entry->next;
837867a482dSJohn Dyson 
838867a482dSJohn Dyson 	/*
839867a482dSJohn Dyson 	 * Do this on a map entry basis so that if the pages are not
840867a482dSJohn Dyson 	 * in the current processes address space, we can easily look
841867a482dSJohn Dyson 	 * up the pages elsewhere.
842867a482dSJohn Dyson 	 */
843867a482dSJohn Dyson 	lastvecindex = -1;
844867a482dSJohn Dyson 	for (current = entry;
845867a482dSJohn Dyson 	    (current != &map->header) && (current->start < end);
846867a482dSJohn Dyson 	    current = current->next) {
847867a482dSJohn Dyson 
848867a482dSJohn Dyson 		/*
849867a482dSJohn Dyson 		 * ignore submaps (for now) or null objects
850867a482dSJohn Dyson 		 */
8519fdfe602SMatthew Dillon 		if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
852867a482dSJohn Dyson 			current->object.vm_object == NULL)
853867a482dSJohn Dyson 			continue;
854867a482dSJohn Dyson 
855867a482dSJohn Dyson 		/*
856867a482dSJohn Dyson 		 * limit this scan to the current map entry and the
857867a482dSJohn Dyson 		 * limits for the mincore call
858867a482dSJohn Dyson 		 */
859867a482dSJohn Dyson 		if (addr < current->start)
860867a482dSJohn Dyson 			addr = current->start;
861867a482dSJohn Dyson 		cend = current->end;
862867a482dSJohn Dyson 		if (cend > end)
863867a482dSJohn Dyson 			cend = end;
864867a482dSJohn Dyson 
865867a482dSJohn Dyson 		/*
866867a482dSJohn Dyson 		 * scan this entry one page at a time
867867a482dSJohn Dyson 		 */
868867a482dSJohn Dyson 		while (addr < cend) {
869867a482dSJohn Dyson 			/*
870867a482dSJohn Dyson 			 * Check pmap first, it is likely faster, also
871867a482dSJohn Dyson 			 * it can provide info as to whether we are the
872867a482dSJohn Dyson 			 * one referencing or modifying the page.
873867a482dSJohn Dyson 			 */
874867a482dSJohn Dyson 			mincoreinfo = pmap_mincore(pmap, addr);
875867a482dSJohn Dyson 			if (!mincoreinfo) {
876867a482dSJohn Dyson 				vm_pindex_t pindex;
877867a482dSJohn Dyson 				vm_ooffset_t offset;
878867a482dSJohn Dyson 				vm_page_t m;
879867a482dSJohn Dyson 				/*
880867a482dSJohn Dyson 				 * calculate the page index into the object
881867a482dSJohn Dyson 				 */
882867a482dSJohn Dyson 				offset = current->offset + (addr - current->start);
883867a482dSJohn Dyson 				pindex = OFF_TO_IDX(offset);
884867a482dSJohn Dyson 				m = vm_page_lookup(current->object.vm_object,
885867a482dSJohn Dyson 					pindex);
886867a482dSJohn Dyson 				/*
887867a482dSJohn Dyson 				 * if the page is resident, then gather information about
888867a482dSJohn Dyson 				 * it.
889867a482dSJohn Dyson 				 */
890867a482dSJohn Dyson 				if (m) {
891867a482dSJohn Dyson 					mincoreinfo = MINCORE_INCORE;
892867a482dSJohn Dyson 					if (m->dirty ||
8930385347cSPeter Wemm 						pmap_is_modified(m))
894867a482dSJohn Dyson 						mincoreinfo |= MINCORE_MODIFIED_OTHER;
895867a482dSJohn Dyson 					if ((m->flags & PG_REFERENCED) ||
8960385347cSPeter Wemm 						pmap_ts_referenced(m)) {
897e69763a3SDoug Rabson 						vm_page_flag_set(m, PG_REFERENCED);
898867a482dSJohn Dyson 						mincoreinfo |= MINCORE_REFERENCED_OTHER;
89902c04a2fSJohn Dyson 					}
900867a482dSJohn Dyson 				}
9019b5a5d81SJohn Dyson 			}
902867a482dSJohn Dyson 
903867a482dSJohn Dyson 			/*
904dd2622a8SAlan Cox 			 * subyte may page fault.  In case it needs to modify
905dd2622a8SAlan Cox 			 * the map, we release the lock.
906dd2622a8SAlan Cox 			 */
907dd2622a8SAlan Cox 			vm_map_unlock_read(map);
908dd2622a8SAlan Cox 
909dd2622a8SAlan Cox 			/*
910867a482dSJohn Dyson 			 * calculate index into user supplied byte vector
911867a482dSJohn Dyson 			 */
912867a482dSJohn Dyson 			vecindex = OFF_TO_IDX(addr - first_addr);
913867a482dSJohn Dyson 
914867a482dSJohn Dyson 			/*
915867a482dSJohn Dyson 			 * If we have skipped map entries, we need to make sure that
916867a482dSJohn Dyson 			 * the byte vector is zeroed for those skipped entries.
917867a482dSJohn Dyson 			 */
918867a482dSJohn Dyson 			while ((lastvecindex + 1) < vecindex) {
919867a482dSJohn Dyson 				error = subyte(vec + lastvecindex, 0);
920867a482dSJohn Dyson 				if (error) {
921d2c60af8SMatthew Dillon 					error = EFAULT;
922d2c60af8SMatthew Dillon 					goto done2;
923867a482dSJohn Dyson 				}
924867a482dSJohn Dyson 				++lastvecindex;
925867a482dSJohn Dyson 			}
926867a482dSJohn Dyson 
927867a482dSJohn Dyson 			/*
928867a482dSJohn Dyson 			 * Pass the page information to the user
929867a482dSJohn Dyson 			 */
930867a482dSJohn Dyson 			error = subyte(vec + vecindex, mincoreinfo);
931867a482dSJohn Dyson 			if (error) {
932d2c60af8SMatthew Dillon 				error = EFAULT;
933d2c60af8SMatthew Dillon 				goto done2;
934867a482dSJohn Dyson 			}
935dd2622a8SAlan Cox 
936dd2622a8SAlan Cox 			/*
937dd2622a8SAlan Cox 			 * If the map has changed, due to the subyte, the previous
938dd2622a8SAlan Cox 			 * output may be invalid.
939dd2622a8SAlan Cox 			 */
940dd2622a8SAlan Cox 			vm_map_lock_read(map);
941dd2622a8SAlan Cox 			if (timestamp != map->timestamp)
942dd2622a8SAlan Cox 				goto RestartScan;
943dd2622a8SAlan Cox 
944867a482dSJohn Dyson 			lastvecindex = vecindex;
94502c04a2fSJohn Dyson 			addr += PAGE_SIZE;
94602c04a2fSJohn Dyson 		}
947867a482dSJohn Dyson 	}
948867a482dSJohn Dyson 
949867a482dSJohn Dyson 	/*
950dd2622a8SAlan Cox 	 * subyte may page fault.  In case it needs to modify
951dd2622a8SAlan Cox 	 * the map, we release the lock.
952dd2622a8SAlan Cox 	 */
953dd2622a8SAlan Cox 	vm_map_unlock_read(map);
954dd2622a8SAlan Cox 
955dd2622a8SAlan Cox 	/*
956867a482dSJohn Dyson 	 * Zero the last entries in the byte vector.
957867a482dSJohn Dyson 	 */
958867a482dSJohn Dyson 	vecindex = OFF_TO_IDX(end - first_addr);
959867a482dSJohn Dyson 	while ((lastvecindex + 1) < vecindex) {
960867a482dSJohn Dyson 		error = subyte(vec + lastvecindex, 0);
961867a482dSJohn Dyson 		if (error) {
962d2c60af8SMatthew Dillon 			error = EFAULT;
963d2c60af8SMatthew Dillon 			goto done2;
964867a482dSJohn Dyson 		}
965867a482dSJohn Dyson 		++lastvecindex;
966867a482dSJohn Dyson 	}
967867a482dSJohn Dyson 
968dd2622a8SAlan Cox 	/*
969dd2622a8SAlan Cox 	 * If the map has changed, due to the subyte, the previous
970dd2622a8SAlan Cox 	 * output may be invalid.
971dd2622a8SAlan Cox 	 */
972dd2622a8SAlan Cox 	vm_map_lock_read(map);
973dd2622a8SAlan Cox 	if (timestamp != map->timestamp)
974dd2622a8SAlan Cox 		goto RestartScan;
975eff50fcdSAlan Cox 	vm_map_unlock_read(map);
976d2c60af8SMatthew Dillon done2:
977190609ddSJohn Baldwin 	mtx_unlock(&Giant);
978d2c60af8SMatthew Dillon 	return (error);
979df8bae1dSRodney W. Grimes }
980df8bae1dSRodney W. Grimes 
981d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
982df8bae1dSRodney W. Grimes struct mlock_args {
983651bb817SAlexander Langer 	const void *addr;
984df8bae1dSRodney W. Grimes 	size_t len;
985df8bae1dSRodney W. Grimes };
986d2d3e875SBruce Evans #endif
987d2c60af8SMatthew Dillon /*
988d2c60af8SMatthew Dillon  * MPSAFE
989d2c60af8SMatthew Dillon  */
990df8bae1dSRodney W. Grimes int
991b40ce416SJulian Elischer mlock(td, uap)
992b40ce416SJulian Elischer 	struct thread *td;
993df8bae1dSRodney W. Grimes 	struct mlock_args *uap;
994df8bae1dSRodney W. Grimes {
995df8bae1dSRodney W. Grimes 	vm_offset_t addr;
996dabee6feSPeter Wemm 	vm_size_t size, pageoff;
997df8bae1dSRodney W. Grimes 	int error;
998df8bae1dSRodney W. Grimes 
999df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
10009154ee6aSPeter Wemm 	size = uap->len;
10019154ee6aSPeter Wemm 
1002dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
1003dabee6feSPeter Wemm 	addr -= pageoff;
1004dabee6feSPeter Wemm 	size += pageoff;
1005dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
1006dabee6feSPeter Wemm 
1007dabee6feSPeter Wemm 	/* disable wrap around */
10089154ee6aSPeter Wemm 	if (addr + size < addr)
1009df8bae1dSRodney W. Grimes 		return (EINVAL);
1010dabee6feSPeter Wemm 
1011df8bae1dSRodney W. Grimes 	if (atop(size) + cnt.v_wire_count > vm_page_max_wired)
1012df8bae1dSRodney W. Grimes 		return (EAGAIN);
10139154ee6aSPeter Wemm 
1014df8bae1dSRodney W. Grimes #ifdef pmap_wired_count
1015b40ce416SJulian Elischer 	if (size + ptoa(pmap_wired_count(vm_map_pmap(&td->td_proc->p_vmspace->vm_map))) >
1016b40ce416SJulian Elischer 	    td->td_proc->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
10174a40e3d4SJohn Dyson 		return (ENOMEM);
1018df8bae1dSRodney W. Grimes #else
101944731cabSJohn Baldwin 	error = suser(td);
102005f0fdd2SPoul-Henning Kamp 	if (error)
1021df8bae1dSRodney W. Grimes 		return (error);
1022df8bae1dSRodney W. Grimes #endif
1023df8bae1dSRodney W. Grimes 
10241d7cf06cSAlan Cox 	error = vm_map_wire(&td->td_proc->p_vmspace->vm_map, addr,
10251d7cf06cSAlan Cox 		     addr + size, TRUE);
1026df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
1027df8bae1dSRodney W. Grimes }
1028df8bae1dSRodney W. Grimes 
1029d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
10304a40e3d4SJohn Dyson struct mlockall_args {
10314a40e3d4SJohn Dyson 	int	how;
10324a40e3d4SJohn Dyson };
10334a40e3d4SJohn Dyson #endif
10344a40e3d4SJohn Dyson 
1035d2c60af8SMatthew Dillon /*
1036d2c60af8SMatthew Dillon  * MPSAFE
1037d2c60af8SMatthew Dillon  */
10384a40e3d4SJohn Dyson int
1039b40ce416SJulian Elischer mlockall(td, uap)
1040b40ce416SJulian Elischer 	struct thread *td;
10414a40e3d4SJohn Dyson 	struct mlockall_args *uap;
10424a40e3d4SJohn Dyson {
10430cddd8f0SMatthew Dillon 	/* mtx_lock(&Giant); */
10440cddd8f0SMatthew Dillon 	/* mtx_unlock(&Giant); */
10454a40e3d4SJohn Dyson 	return 0;
10464a40e3d4SJohn Dyson }
10474a40e3d4SJohn Dyson 
10484a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_
1049fa721254SAlfred Perlstein struct munlockall_args {
10504a40e3d4SJohn Dyson 	int	how;
10514a40e3d4SJohn Dyson };
10524a40e3d4SJohn Dyson #endif
10534a40e3d4SJohn Dyson 
1054d2c60af8SMatthew Dillon /*
1055d2c60af8SMatthew Dillon  * MPSAFE
1056d2c60af8SMatthew Dillon  */
10574a40e3d4SJohn Dyson int
1058b40ce416SJulian Elischer munlockall(td, uap)
1059b40ce416SJulian Elischer 	struct thread *td;
10604a40e3d4SJohn Dyson 	struct munlockall_args *uap;
10614a40e3d4SJohn Dyson {
10620cddd8f0SMatthew Dillon 	/* mtx_lock(&Giant); */
10630cddd8f0SMatthew Dillon 	/* mtx_unlock(&Giant); */
10644a40e3d4SJohn Dyson 	return 0;
10654a40e3d4SJohn Dyson }
10664a40e3d4SJohn Dyson 
10674a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_
1068df8bae1dSRodney W. Grimes struct munlock_args {
1069651bb817SAlexander Langer 	const void *addr;
1070df8bae1dSRodney W. Grimes 	size_t len;
1071df8bae1dSRodney W. Grimes };
1072d2d3e875SBruce Evans #endif
1073d2c60af8SMatthew Dillon /*
1074d2c60af8SMatthew Dillon  * MPSAFE
1075d2c60af8SMatthew Dillon  */
1076df8bae1dSRodney W. Grimes int
1077b40ce416SJulian Elischer munlock(td, uap)
1078b40ce416SJulian Elischer 	struct thread *td;
1079df8bae1dSRodney W. Grimes 	struct munlock_args *uap;
1080df8bae1dSRodney W. Grimes {
1081df8bae1dSRodney W. Grimes 	vm_offset_t addr;
1082dabee6feSPeter Wemm 	vm_size_t size, pageoff;
1083df8bae1dSRodney W. Grimes 	int error;
1084df8bae1dSRodney W. Grimes 
1085df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
10869154ee6aSPeter Wemm 	size = uap->len;
10879154ee6aSPeter Wemm 
1088dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
1089dabee6feSPeter Wemm 	addr -= pageoff;
1090dabee6feSPeter Wemm 	size += pageoff;
1091dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
1092dabee6feSPeter Wemm 
1093dabee6feSPeter Wemm 	/* disable wrap around */
10949154ee6aSPeter Wemm 	if (addr + size < addr)
1095df8bae1dSRodney W. Grimes 		return (EINVAL);
1096dabee6feSPeter Wemm 
1097df8bae1dSRodney W. Grimes #ifndef pmap_wired_count
109844731cabSJohn Baldwin 	error = suser(td);
109905f0fdd2SPoul-Henning Kamp 	if (error)
1100df8bae1dSRodney W. Grimes 		return (error);
1101df8bae1dSRodney W. Grimes #endif
1102df8bae1dSRodney W. Grimes 
11031d7cf06cSAlan Cox 	error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, addr,
110423955314SAlfred Perlstein 		     addr + size, TRUE);
1105df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
1106df8bae1dSRodney W. Grimes }
1107df8bae1dSRodney W. Grimes 
1108df8bae1dSRodney W. Grimes /*
1109d2c60af8SMatthew Dillon  * vm_mmap()
1110d2c60af8SMatthew Dillon  *
1111d2c60af8SMatthew Dillon  * MPSAFE
1112d2c60af8SMatthew Dillon  *
1113d2c60af8SMatthew Dillon  * Internal version of mmap.  Currently used by mmap, exec, and sys5
1114d2c60af8SMatthew Dillon  * shared memory.  Handle is either a vnode pointer or NULL for MAP_ANON.
1115df8bae1dSRodney W. Grimes  */
1116df8bae1dSRodney W. Grimes int
1117b9dcd593SBruce Evans vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1118b9dcd593SBruce Evans 	vm_prot_t maxprot, int flags,
1119651bb817SAlexander Langer 	void *handle,
1120b9dcd593SBruce Evans 	vm_ooffset_t foff)
1121df8bae1dSRodney W. Grimes {
1122df8bae1dSRodney W. Grimes 	boolean_t fitit;
1123fcae040bSJohn Dyson 	vm_object_t object;
1124df8bae1dSRodney W. Grimes 	struct vnode *vp = NULL;
112524a1cce3SDavid Greenman 	objtype_t type;
1126df8bae1dSRodney W. Grimes 	int rv = KERN_SUCCESS;
1127bd7e5f99SJohn Dyson 	vm_ooffset_t objsize;
1128bd7e5f99SJohn Dyson 	int docow;
1129b40ce416SJulian Elischer 	struct thread *td = curthread;
1130df8bae1dSRodney W. Grimes 
1131df8bae1dSRodney W. Grimes 	if (size == 0)
1132df8bae1dSRodney W. Grimes 		return (0);
1133df8bae1dSRodney W. Grimes 
113406cb7259SDavid Greenman 	objsize = size = round_page(size);
1135df8bae1dSRodney W. Grimes 
1136070f64feSMatthew Dillon 	if (td->td_proc->p_vmspace->vm_map.size + size >
1137070f64feSMatthew Dillon 	    td->td_proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
1138070f64feSMatthew Dillon 		return(ENOMEM);
1139070f64feSMatthew Dillon 	}
1140070f64feSMatthew Dillon 
1141df8bae1dSRodney W. Grimes 	/*
1142bc9ad247SDavid Greenman 	 * We currently can only deal with page aligned file offsets.
1143bc9ad247SDavid Greenman 	 * The check is here rather than in the syscall because the
1144bc9ad247SDavid Greenman 	 * kernel calls this function internally for other mmaping
1145bc9ad247SDavid Greenman 	 * operations (such as in exec) and non-aligned offsets will
1146bc9ad247SDavid Greenman 	 * cause pmap inconsistencies...so we want to be sure to
1147bc9ad247SDavid Greenman 	 * disallow this in all cases.
1148bc9ad247SDavid Greenman 	 */
1149bc9ad247SDavid Greenman 	if (foff & PAGE_MASK)
1150bc9ad247SDavid Greenman 		return (EINVAL);
1151bc9ad247SDavid Greenman 
115206cb7259SDavid Greenman 	if ((flags & MAP_FIXED) == 0) {
115306cb7259SDavid Greenman 		fitit = TRUE;
115406cb7259SDavid Greenman 		*addr = round_page(*addr);
115506cb7259SDavid Greenman 	} else {
115606cb7259SDavid Greenman 		if (*addr != trunc_page(*addr))
115706cb7259SDavid Greenman 			return (EINVAL);
115806cb7259SDavid Greenman 		fitit = FALSE;
115906cb7259SDavid Greenman 		(void) vm_map_remove(map, *addr, *addr + size);
116006cb7259SDavid Greenman 	}
116106cb7259SDavid Greenman 
1162bc9ad247SDavid Greenman 	/*
116324a1cce3SDavid Greenman 	 * Lookup/allocate object.
1164df8bae1dSRodney W. Grimes 	 */
11655f55e841SDavid Greenman 	if (flags & MAP_ANON) {
1166851c12ffSJohn Dyson 		type = OBJT_DEFAULT;
11675f55e841SDavid Greenman 		/*
11685f55e841SDavid Greenman 		 * Unnamed anonymous regions always start at 0.
11695f55e841SDavid Greenman 		 */
117067bf6868SJohn Dyson 		if (handle == 0)
11715f55e841SDavid Greenman 			foff = 0;
11725f55e841SDavid Greenman 	} else {
1173df8bae1dSRodney W. Grimes 		vp = (struct vnode *) handle;
1174c04c996bSAlan Cox 		mtx_lock(&Giant);
1175f6b5b182SJeff Roberson 		ASSERT_VOP_LOCKED(vp, "vm_mmap");
1176df8bae1dSRodney W. Grimes 		if (vp->v_type == VCHR) {
117724a1cce3SDavid Greenman 			type = OBJT_DEVICE;
1178a23d65bfSBruce Evans 			handle = (void *)(intptr_t)vp->v_rdev;
117906cb7259SDavid Greenman 		} else {
118006cb7259SDavid Greenman 			struct vattr vat;
118106cb7259SDavid Greenman 			int error;
118206cb7259SDavid Greenman 
1183a854ed98SJohn Baldwin 			error = VOP_GETATTR(vp, &vat, td->td_ucred, td);
1184e4ca250dSJohn Baldwin 			if (error) {
118523955314SAlfred Perlstein 				mtx_unlock(&Giant);
118606cb7259SDavid Greenman 				return (error);
1187e4ca250dSJohn Baldwin 			}
1188bd7e5f99SJohn Dyson 			objsize = round_page(vat.va_size);
118924a1cce3SDavid Greenman 			type = OBJT_VNODE;
119000d76afeSGuido van Rooij 			/*
119100d76afeSGuido van Rooij 			 * if it is a regular file without any references
119200d76afeSGuido van Rooij 			 * we do not need to sync it.
119300d76afeSGuido van Rooij 			 */
119400d76afeSGuido van Rooij 			if (vp->v_type == VREG && vat.va_nlink == 0) {
119500d76afeSGuido van Rooij 				flags |= MAP_NOSYNC;
119600d76afeSGuido van Rooij 			}
1197df8bae1dSRodney W. Grimes 		}
1198c04c996bSAlan Cox 		mtx_unlock(&Giant);
119906cb7259SDavid Greenman 	}
120094328e90SJohn Dyson 
120194328e90SJohn Dyson 	if (handle == NULL) {
120294328e90SJohn Dyson 		object = NULL;
12034738fa09SAlan Cox 		docow = 0;
120494328e90SJohn Dyson 	} else {
12050a0a85b3SJohn Dyson 		object = vm_pager_allocate(type,
12066cde7a16SDavid Greenman 			handle, objsize, prot, foff);
1207e4ca250dSJohn Baldwin 		if (object == NULL) {
120824a1cce3SDavid Greenman 			return (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1209e4ca250dSJohn Baldwin 		}
12104738fa09SAlan Cox 		docow = MAP_PREFAULT_PARTIAL;
121194328e90SJohn Dyson 	}
1212df8bae1dSRodney W. Grimes 
12135850152dSJohn Dyson 	/*
12148f2ec877SDavid Greenman 	 * Force device mappings to be shared.
12155850152dSJohn Dyson 	 */
121624964514SPeter Wemm 	if (type == OBJT_DEVICE || type == OBJT_PHYS) {
12178f2ec877SDavid Greenman 		flags &= ~(MAP_PRIVATE|MAP_COPY);
12185850152dSJohn Dyson 		flags |= MAP_SHARED;
12198f2ec877SDavid Greenman 	}
12205850152dSJohn Dyson 
12214f79d873SMatthew Dillon 	if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
12224738fa09SAlan Cox 		docow |= MAP_COPY_ON_WRITE;
12234f79d873SMatthew Dillon 	if (flags & MAP_NOSYNC)
12244f79d873SMatthew Dillon 		docow |= MAP_DISABLE_SYNCER;
12259730a5daSPaul Saab 	if (flags & MAP_NOCORE)
12269730a5daSPaul Saab 		docow |= MAP_DISABLE_COREDUMP;
12275850152dSJohn Dyson 
1228d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC)
1229d0aea04fSJohn Dyson 	if (prot & VM_PROT_READ)
1230d0aea04fSJohn Dyson 		prot |= VM_PROT_EXECUTE;
1231d0aea04fSJohn Dyson 
1232d0aea04fSJohn Dyson 	if (maxprot & VM_PROT_READ)
1233d0aea04fSJohn Dyson 		maxprot |= VM_PROT_EXECUTE;
1234d0aea04fSJohn Dyson #endif
1235d0aea04fSJohn Dyson 
1236e4ca250dSJohn Baldwin 	if (fitit)
12370a0a85b3SJohn Dyson 		*addr = pmap_addr_hint(object, *addr, size);
12380a0a85b3SJohn Dyson 
12392267af78SJulian Elischer 	if (flags & MAP_STACK)
12402267af78SJulian Elischer 		rv = vm_map_stack (map, *addr, size, prot,
12412267af78SJulian Elischer 				   maxprot, docow);
12422267af78SJulian Elischer 	else
1243bd7e5f99SJohn Dyson 		rv = vm_map_find(map, object, foff, addr, size, fitit,
1244bd7e5f99SJohn Dyson 				 prot, maxprot, docow);
1245bd7e5f99SJohn Dyson 
1246d2c60af8SMatthew Dillon 	if (rv != KERN_SUCCESS) {
12477fb0c17eSDavid Greenman 		/*
124824a1cce3SDavid Greenman 		 * Lose the object reference. Will destroy the
124924a1cce3SDavid Greenman 		 * object if it's an unnamed anonymous mapping
125024a1cce3SDavid Greenman 		 * or named anonymous without other references.
12517fb0c17eSDavid Greenman 		 */
1252df8bae1dSRodney W. Grimes 		vm_object_deallocate(object);
1253d2c60af8SMatthew Dillon 	} else if (flags & MAP_SHARED) {
1254df8bae1dSRodney W. Grimes 		/*
1255df8bae1dSRodney W. Grimes 		 * Shared memory is also shared with children.
1256df8bae1dSRodney W. Grimes 		 */
1257df8bae1dSRodney W. Grimes 		rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1258e4ca250dSJohn Baldwin 		if (rv != KERN_SUCCESS)
12597fb0c17eSDavid Greenman 			(void) vm_map_remove(map, *addr, *addr + size);
1260df8bae1dSRodney W. Grimes 	}
1261df8bae1dSRodney W. Grimes 	switch (rv) {
1262df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
1263df8bae1dSRodney W. Grimes 		return (0);
1264df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
1265df8bae1dSRodney W. Grimes 	case KERN_NO_SPACE:
1266df8bae1dSRodney W. Grimes 		return (ENOMEM);
1267df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
1268df8bae1dSRodney W. Grimes 		return (EACCES);
1269df8bae1dSRodney W. Grimes 	default:
1270df8bae1dSRodney W. Grimes 		return (EINVAL);
1271df8bae1dSRodney W. Grimes 	}
1272df8bae1dSRodney W. Grimes }
1273