xref: /freebsd/sys/vm/vm_mmap.c (revision 5929bcfaba4ee663511173d3241213fc86bb5fb4)
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"
49e9822d92SJoerg Wunsch #include "opt_rlimit.h"
50e9822d92SJoerg Wunsch 
51df8bae1dSRodney W. Grimes #include <sys/param.h>
521f6889a1SMatthew Dillon #include <sys/kernel.h>
53df8bae1dSRodney W. Grimes #include <sys/systm.h>
54d2d3e875SBruce Evans #include <sys/sysproto.h>
55df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
56df8bae1dSRodney W. Grimes #include <sys/proc.h>
57df8bae1dSRodney W. Grimes #include <sys/vnode.h>
583ac4d1efSBruce Evans #include <sys/fcntl.h>
59df8bae1dSRodney W. Grimes #include <sys/file.h>
60df8bae1dSRodney W. Grimes #include <sys/mman.h>
61df8bae1dSRodney W. Grimes #include <sys/conf.h>
624183b6b6SPeter Wemm #include <sys/stat.h>
63efeaf95aSDavid Greenman #include <sys/vmmeter.h>
641f6889a1SMatthew Dillon #include <sys/sysctl.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #include <vm/vm.h>
67efeaf95aSDavid Greenman #include <vm/vm_param.h>
68996c772fSJohn Dyson #include <sys/lock.h>
69efeaf95aSDavid Greenman #include <vm/pmap.h>
70efeaf95aSDavid Greenman #include <vm/vm_map.h>
71efeaf95aSDavid Greenman #include <vm/vm_object.h>
721c7c3c6aSMatthew Dillon #include <vm/vm_page.h>
73df8bae1dSRodney W. Grimes #include <vm/vm_pager.h>
74b5e8ce9fSBruce Evans #include <vm/vm_pageout.h>
75efeaf95aSDavid Greenman #include <vm/vm_extern.h>
76867a482dSJohn Dyson #include <vm/vm_page.h>
771f6889a1SMatthew Dillon #include <vm/vm_kern.h>
78df8bae1dSRodney W. Grimes 
79d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
80df8bae1dSRodney W. Grimes struct sbrk_args {
81df8bae1dSRodney W. Grimes 	int incr;
82df8bae1dSRodney W. Grimes };
83d2d3e875SBruce Evans #endif
840d94caffSDavid Greenman 
851f6889a1SMatthew Dillon static int max_proc_mmap;
861f6889a1SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
871f6889a1SMatthew Dillon 
881f6889a1SMatthew Dillon /*
891f6889a1SMatthew Dillon  * Set the maximum number of vm_map_entry structures per process.  Roughly
901f6889a1SMatthew Dillon  * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
911f6889a1SMatthew Dillon  * of our KVM malloc space still results in generous limits.  We want a
921f6889a1SMatthew Dillon  * default that is good enough to prevent the kernel running out of resources
931f6889a1SMatthew Dillon  * if attacked from compromised user account but generous enough such that
941f6889a1SMatthew Dillon  * multi-threaded processes are not unduly inconvenienced.
951f6889a1SMatthew Dillon  */
961f6889a1SMatthew Dillon 
971f6889a1SMatthew Dillon static void vmmapentry_rsrc_init __P((void *));
981f6889a1SMatthew Dillon SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL)
991f6889a1SMatthew Dillon 
1001f6889a1SMatthew Dillon static void
1011f6889a1SMatthew Dillon vmmapentry_rsrc_init(dummy)
1021f6889a1SMatthew Dillon         void *dummy;
1031f6889a1SMatthew Dillon {
1041f6889a1SMatthew Dillon     max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
1051f6889a1SMatthew Dillon     max_proc_mmap /= 100;
1061f6889a1SMatthew Dillon }
1071f6889a1SMatthew Dillon 
108df8bae1dSRodney W. Grimes /* ARGSUSED */
109df8bae1dSRodney W. Grimes int
110cb226aaaSPoul-Henning Kamp sbrk(p, uap)
111df8bae1dSRodney W. Grimes 	struct proc *p;
112df8bae1dSRodney W. Grimes 	struct sbrk_args *uap;
113df8bae1dSRodney W. Grimes {
114df8bae1dSRodney W. Grimes 
115df8bae1dSRodney W. Grimes 	/* Not yet implemented */
116df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
117df8bae1dSRodney W. Grimes }
118df8bae1dSRodney W. Grimes 
119d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
120df8bae1dSRodney W. Grimes struct sstk_args {
121df8bae1dSRodney W. Grimes 	int incr;
122df8bae1dSRodney W. Grimes };
123d2d3e875SBruce Evans #endif
1240d94caffSDavid Greenman 
125df8bae1dSRodney W. Grimes /* ARGSUSED */
126df8bae1dSRodney W. Grimes int
127cb226aaaSPoul-Henning Kamp sstk(p, uap)
128df8bae1dSRodney W. Grimes 	struct proc *p;
129df8bae1dSRodney W. Grimes 	struct sstk_args *uap;
130df8bae1dSRodney W. Grimes {
131df8bae1dSRodney W. Grimes 
132df8bae1dSRodney W. Grimes 	/* Not yet implemented */
133df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
134df8bae1dSRodney W. Grimes }
135df8bae1dSRodney W. Grimes 
136df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
137d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
138df8bae1dSRodney W. Grimes struct getpagesize_args {
139df8bae1dSRodney W. Grimes 	int dummy;
140df8bae1dSRodney W. Grimes };
141d2d3e875SBruce Evans #endif
1420d94caffSDavid Greenman 
143df8bae1dSRodney W. Grimes /* ARGSUSED */
144df8bae1dSRodney W. Grimes int
145cb226aaaSPoul-Henning Kamp ogetpagesize(p, uap)
146df8bae1dSRodney W. Grimes 	struct proc *p;
147df8bae1dSRodney W. Grimes 	struct getpagesize_args *uap;
148df8bae1dSRodney W. Grimes {
149df8bae1dSRodney W. Grimes 
150cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = PAGE_SIZE;
151df8bae1dSRodney W. Grimes 	return (0);
152df8bae1dSRodney W. Grimes }
153df8bae1dSRodney W. Grimes #endif				/* COMPAT_43 || COMPAT_SUNOS */
154df8bae1dSRodney W. Grimes 
15554f42e4bSPeter Wemm 
15654f42e4bSPeter Wemm /*
15754f42e4bSPeter Wemm  * Memory Map (mmap) system call.  Note that the file offset
15854f42e4bSPeter Wemm  * and address are allowed to be NOT page aligned, though if
15954f42e4bSPeter Wemm  * the MAP_FIXED flag it set, both must have the same remainder
16054f42e4bSPeter Wemm  * modulo the PAGE_SIZE (POSIX 1003.1b).  If the address is not
16154f42e4bSPeter Wemm  * page-aligned, the actual mapping starts at trunc_page(addr)
16254f42e4bSPeter Wemm  * and the return value is adjusted up by the page offset.
163b4309055SMatthew Dillon  *
164b4309055SMatthew Dillon  * Generally speaking, only character devices which are themselves
165b4309055SMatthew Dillon  * memory-based, such as a video framebuffer, can be mmap'd.  Otherwise
166b4309055SMatthew Dillon  * there would be no cache coherency between a descriptor and a VM mapping
167b4309055SMatthew Dillon  * both to the same character device.
168b4309055SMatthew Dillon  *
169b4309055SMatthew Dillon  * Block devices can be mmap'd no matter what they represent.  Cache coherency
170b4309055SMatthew Dillon  * is maintained as long as you do not write directly to the underlying
171b4309055SMatthew Dillon  * character device.
17254f42e4bSPeter Wemm  */
173d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
174df8bae1dSRodney W. Grimes struct mmap_args {
175651bb817SAlexander Langer 	void *addr;
176df8bae1dSRodney W. Grimes 	size_t len;
177df8bae1dSRodney W. Grimes 	int prot;
178df8bae1dSRodney W. Grimes 	int flags;
179df8bae1dSRodney W. Grimes 	int fd;
180df8bae1dSRodney W. Grimes 	long pad;
181df8bae1dSRodney W. Grimes 	off_t pos;
182df8bae1dSRodney W. Grimes };
183d2d3e875SBruce Evans #endif
184df8bae1dSRodney W. Grimes 
185df8bae1dSRodney W. Grimes int
186cb226aaaSPoul-Henning Kamp mmap(p, uap)
187df8bae1dSRodney W. Grimes 	struct proc *p;
188df8bae1dSRodney W. Grimes 	register struct mmap_args *uap;
189df8bae1dSRodney W. Grimes {
190df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
191df8bae1dSRodney W. Grimes 	register struct file *fp;
192df8bae1dSRodney W. Grimes 	struct vnode *vp;
193df8bae1dSRodney W. Grimes 	vm_offset_t addr;
1949154ee6aSPeter Wemm 	vm_size_t size, pageoff;
195df8bae1dSRodney W. Grimes 	vm_prot_t prot, maxprot;
196651bb817SAlexander Langer 	void *handle;
197df8bae1dSRodney W. Grimes 	int flags, error;
198c8bdd56bSGuido van Rooij 	int disablexworkaround;
19954f42e4bSPeter Wemm 	off_t pos;
2001f6889a1SMatthew Dillon 	struct vmspace *vms = p->p_vmspace;
201df8bae1dSRodney W. Grimes 
20254f42e4bSPeter Wemm 	addr = (vm_offset_t) uap->addr;
20354f42e4bSPeter Wemm 	size = uap->len;
204df8bae1dSRodney W. Grimes 	prot = uap->prot & VM_PROT_ALL;
205df8bae1dSRodney W. Grimes 	flags = uap->flags;
20654f42e4bSPeter Wemm 	pos = uap->pos;
20754f42e4bSPeter Wemm 
20854f42e4bSPeter Wemm 	/* make sure mapping fits into numeric range etc */
209fc565456SDmitrij Tejblum 	if ((ssize_t) uap->len < 0 ||
21054f42e4bSPeter Wemm 	    ((flags & MAP_ANON) && uap->fd != -1))
211df8bae1dSRodney W. Grimes 		return (EINVAL);
2129154ee6aSPeter Wemm 
2132267af78SJulian Elischer 	if (flags & MAP_STACK) {
2142267af78SJulian Elischer 		if ((uap->fd != -1) ||
2152267af78SJulian Elischer 		    ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
2162267af78SJulian Elischer 			return (EINVAL);
2172267af78SJulian Elischer 		flags |= MAP_ANON;
2182267af78SJulian Elischer 		pos = 0;
2192907af2aSJulian Elischer 	}
2202907af2aSJulian Elischer 
2219154ee6aSPeter Wemm 	/*
22254f42e4bSPeter Wemm 	 * Align the file position to a page boundary,
22354f42e4bSPeter Wemm 	 * and save its page offset component.
2249154ee6aSPeter Wemm 	 */
22554f42e4bSPeter Wemm 	pageoff = (pos & PAGE_MASK);
22654f42e4bSPeter Wemm 	pos -= pageoff;
22754f42e4bSPeter Wemm 
22854f42e4bSPeter Wemm 	/* Adjust size for rounding (on both ends). */
22954f42e4bSPeter Wemm 	size += pageoff;			/* low end... */
23054f42e4bSPeter Wemm 	size = (vm_size_t) round_page(size);	/* hi end */
2319154ee6aSPeter Wemm 
232df8bae1dSRodney W. Grimes 	/*
2330d94caffSDavid Greenman 	 * Check for illegal addresses.  Watch out for address wrap... Note
2340d94caffSDavid Greenman 	 * that VM_*_ADDRESS are not constants due to casts (argh).
235df8bae1dSRodney W. Grimes 	 */
236df8bae1dSRodney W. Grimes 	if (flags & MAP_FIXED) {
23754f42e4bSPeter Wemm 		/*
23854f42e4bSPeter Wemm 		 * The specified address must have the same remainder
23954f42e4bSPeter Wemm 		 * as the file offset taken modulo PAGE_SIZE, so it
24054f42e4bSPeter Wemm 		 * should be aligned after adjustment by pageoff.
24154f42e4bSPeter Wemm 		 */
24254f42e4bSPeter Wemm 		addr -= pageoff;
24354f42e4bSPeter Wemm 		if (addr & PAGE_MASK)
24454f42e4bSPeter Wemm 			return (EINVAL);
24554f42e4bSPeter Wemm 		/* Address range must be all in user VM space. */
246bbc0ec52SDavid Greenman 		if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
247df8bae1dSRodney W. Grimes 			return (EINVAL);
24826f9a767SRodney W. Grimes #ifndef i386
249df8bae1dSRodney W. Grimes 		if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
250df8bae1dSRodney W. Grimes 			return (EINVAL);
25126f9a767SRodney W. Grimes #endif
252bbc0ec52SDavid Greenman 		if (addr + size < addr)
253df8bae1dSRodney W. Grimes 			return (EINVAL);
254df8bae1dSRodney W. Grimes 	}
255df8bae1dSRodney W. Grimes 	/*
25654f42e4bSPeter Wemm 	 * XXX for non-fixed mappings where no hint is provided or
25754f42e4bSPeter Wemm 	 * the hint would fall in the potential heap space,
25854f42e4bSPeter Wemm 	 * place it after the end of the largest possible heap.
259df8bae1dSRodney W. Grimes 	 *
26054f42e4bSPeter Wemm 	 * There should really be a pmap call to determine a reasonable
26154f42e4bSPeter Wemm 	 * location.
262df8bae1dSRodney W. Grimes 	 */
263d28ab90fSLuoqi Chen 	else if (addr == 0 ||
2641f6889a1SMatthew Dillon 	    (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
2651f6889a1SMatthew Dillon 	     addr < round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ)))
2661f6889a1SMatthew Dillon 		addr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
26754f42e4bSPeter Wemm 
268df8bae1dSRodney W. Grimes 	if (flags & MAP_ANON) {
269df8bae1dSRodney W. Grimes 		/*
270df8bae1dSRodney W. Grimes 		 * Mapping blank space is trivial.
271df8bae1dSRodney W. Grimes 		 */
272df8bae1dSRodney W. Grimes 		handle = NULL;
273df8bae1dSRodney W. Grimes 		maxprot = VM_PROT_ALL;
27454f42e4bSPeter Wemm 		pos = 0;
275df8bae1dSRodney W. Grimes 	} else {
276df8bae1dSRodney W. Grimes 		/*
2770d94caffSDavid Greenman 		 * Mapping file, get fp for validation. Obtain vnode and make
2780d94caffSDavid Greenman 		 * sure it is of appropriate type.
279df8bae1dSRodney W. Grimes 		 */
280df8bae1dSRodney W. Grimes 		if (((unsigned) uap->fd) >= fdp->fd_nfiles ||
281df8bae1dSRodney W. Grimes 		    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
282df8bae1dSRodney W. Grimes 			return (EBADF);
283df8bae1dSRodney W. Grimes 		if (fp->f_type != DTYPE_VNODE)
284df8bae1dSRodney W. Grimes 			return (EINVAL);
285df8bae1dSRodney W. Grimes 		vp = (struct vnode *) fp->f_data;
286df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG && vp->v_type != VCHR)
287df8bae1dSRodney W. Grimes 			return (EINVAL);
288df8bae1dSRodney W. Grimes 		/*
2890d94caffSDavid Greenman 		 * XXX hack to handle use of /dev/zero to map anon memory (ala
2900d94caffSDavid Greenman 		 * SunOS).
291df8bae1dSRodney W. Grimes 		 */
292df8bae1dSRodney W. Grimes 		if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
293df8bae1dSRodney W. Grimes 			handle = NULL;
294df8bae1dSRodney W. Grimes 			maxprot = VM_PROT_ALL;
295df8bae1dSRodney W. Grimes 			flags |= MAP_ANON;
29654f42e4bSPeter Wemm 			pos = 0;
297df8bae1dSRodney W. Grimes 		} else {
298df8bae1dSRodney W. Grimes 			/*
299c8bdd56bSGuido van Rooij 			 * cdevs does not provide private mappings of any kind.
300c8bdd56bSGuido van Rooij 			 */
301c8bdd56bSGuido van Rooij 			/*
302c8bdd56bSGuido van Rooij 			 * However, for XIG X server to continue to work,
303c8bdd56bSGuido van Rooij 			 * we should allow the superuser to do it anyway.
304c8bdd56bSGuido van Rooij 			 * We only allow it at securelevel < 1.
305c8bdd56bSGuido van Rooij 			 * (Because the XIG X server writes directly to video
306c8bdd56bSGuido van Rooij 			 * memory via /dev/mem, it should never work at any
307c8bdd56bSGuido van Rooij 			 * other securelevel.
308c8bdd56bSGuido van Rooij 			 * XXX this will have to go
309c8bdd56bSGuido van Rooij 			 */
310c8bdd56bSGuido van Rooij 			if (securelevel >= 1)
311c8bdd56bSGuido van Rooij 				disablexworkaround = 1;
312c8bdd56bSGuido van Rooij 			else
313f711d546SPoul-Henning Kamp 				disablexworkaround = suser(p);
314c8bdd56bSGuido van Rooij 			if (vp->v_type == VCHR && disablexworkaround &&
315c8bdd56bSGuido van Rooij 				(flags & (MAP_PRIVATE|MAP_COPY)))
316c8bdd56bSGuido van Rooij 				 return (EINVAL);
317c8bdd56bSGuido van Rooij 			/*
318df8bae1dSRodney W. Grimes 			 * Ensure that file and memory protections are
319df8bae1dSRodney W. Grimes 			 * compatible.  Note that we only worry about
320df8bae1dSRodney W. Grimes 			 * writability if mapping is shared; in this case,
321df8bae1dSRodney W. Grimes 			 * current and max prot are dictated by the open file.
322df8bae1dSRodney W. Grimes 			 * XXX use the vnode instead?  Problem is: what
3230d94caffSDavid Greenman 			 * credentials do we use for determination? What if
3240d94caffSDavid Greenman 			 * proc does a setuid?
325df8bae1dSRodney W. Grimes 			 */
326df8bae1dSRodney W. Grimes 			maxprot = VM_PROT_EXECUTE;	/* ??? */
327df8bae1dSRodney W. Grimes 			if (fp->f_flag & FREAD)
328df8bae1dSRodney W. Grimes 				maxprot |= VM_PROT_READ;
329df8bae1dSRodney W. Grimes 			else if (prot & PROT_READ)
330df8bae1dSRodney W. Grimes 				return (EACCES);
331c8bdd56bSGuido van Rooij 			/*
332c8bdd56bSGuido van Rooij 			 * If we are sharing potential changes (either via
333c8bdd56bSGuido van Rooij 			 * MAP_SHARED or via the implicit sharing of character
334c8bdd56bSGuido van Rooij 			 * device mappings), and we are trying to get write
335c8bdd56bSGuido van Rooij 			 * permission although we opened it without asking
336c8bdd56bSGuido van Rooij 			 * for it, bail out.  Check for superuser, only if
337c8bdd56bSGuido van Rooij 			 * we're at securelevel < 1, to allow the XIG X server
338c8bdd56bSGuido van Rooij 			 * to continue to work.
339c8bdd56bSGuido van Rooij 			 */
34005feb99fSGuido van Rooij 
34105feb99fSGuido van Rooij 			if ((flags & MAP_SHARED) != 0 ||
34205feb99fSGuido van Rooij 			    (vp->v_type == VCHR && disablexworkaround)) {
34305feb99fSGuido van Rooij 				if ((fp->f_flag & FWRITE) != 0) {
3444183b6b6SPeter Wemm 					struct vattr va;
34505feb99fSGuido van Rooij 					if ((error =
34605feb99fSGuido van Rooij 					    VOP_GETATTR(vp, &va,
34705feb99fSGuido van Rooij 						        p->p_ucred, p)))
34805feb99fSGuido van Rooij 						return (error);
34905feb99fSGuido van Rooij 					if ((va.va_flags &
35005feb99fSGuido van Rooij 					    (IMMUTABLE|APPEND)) == 0)
351df8bae1dSRodney W. Grimes 						maxprot |= VM_PROT_WRITE;
35205feb99fSGuido van Rooij 					else if (prot & PROT_WRITE)
35305feb99fSGuido van Rooij 						return (EPERM);
35405feb99fSGuido van Rooij 				} else if ((prot & PROT_WRITE) != 0)
35505feb99fSGuido van Rooij 					return (EACCES);
35605feb99fSGuido van Rooij 			} else
35705feb99fSGuido van Rooij 				maxprot |= VM_PROT_WRITE;
35805feb99fSGuido van Rooij 
359651bb817SAlexander Langer 			handle = (void *)vp;
360df8bae1dSRodney W. Grimes 		}
361df8bae1dSRodney W. Grimes 	}
3621f6889a1SMatthew Dillon 
3631f6889a1SMatthew Dillon 	/*
3641f6889a1SMatthew Dillon 	 * Do not allow more then a certain number of vm_map_entry structures
3651f6889a1SMatthew Dillon 	 * per process.  Scale with the number of rforks sharing the map
3661f6889a1SMatthew Dillon 	 * to make the limit reasonable for threads.
3671f6889a1SMatthew Dillon 	 */
3681f6889a1SMatthew Dillon 	if (max_proc_mmap &&
3691f6889a1SMatthew Dillon 	    vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
3701f6889a1SMatthew Dillon 		return (ENOMEM);
3711f6889a1SMatthew Dillon 	}
3721f6889a1SMatthew Dillon 
3731f6889a1SMatthew Dillon 	error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
37454f42e4bSPeter Wemm 	    flags, handle, pos);
375df8bae1dSRodney W. Grimes 	if (error == 0)
376711458e3SDoug Rabson 		p->p_retval[0] = (register_t) (addr + pageoff);
377df8bae1dSRodney W. Grimes 	return (error);
378df8bae1dSRodney W. Grimes }
379df8bae1dSRodney W. Grimes 
38005f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43
381d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
38205f0fdd2SPoul-Henning Kamp struct ommap_args {
38305f0fdd2SPoul-Henning Kamp 	caddr_t addr;
38405f0fdd2SPoul-Henning Kamp 	int len;
38505f0fdd2SPoul-Henning Kamp 	int prot;
38605f0fdd2SPoul-Henning Kamp 	int flags;
38705f0fdd2SPoul-Henning Kamp 	int fd;
38805f0fdd2SPoul-Henning Kamp 	long pos;
38905f0fdd2SPoul-Henning Kamp };
390d2d3e875SBruce Evans #endif
39105f0fdd2SPoul-Henning Kamp int
392cb226aaaSPoul-Henning Kamp ommap(p, uap)
39305f0fdd2SPoul-Henning Kamp 	struct proc *p;
39405f0fdd2SPoul-Henning Kamp 	register struct ommap_args *uap;
39505f0fdd2SPoul-Henning Kamp {
39605f0fdd2SPoul-Henning Kamp 	struct mmap_args nargs;
39705f0fdd2SPoul-Henning Kamp 	static const char cvtbsdprot[8] = {
39805f0fdd2SPoul-Henning Kamp 		0,
39905f0fdd2SPoul-Henning Kamp 		PROT_EXEC,
40005f0fdd2SPoul-Henning Kamp 		PROT_WRITE,
40105f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE,
40205f0fdd2SPoul-Henning Kamp 		PROT_READ,
40305f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_READ,
40405f0fdd2SPoul-Henning Kamp 		PROT_WRITE | PROT_READ,
40505f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE | PROT_READ,
40605f0fdd2SPoul-Henning Kamp 	};
4070d94caffSDavid Greenman 
40805f0fdd2SPoul-Henning Kamp #define	OMAP_ANON	0x0002
40905f0fdd2SPoul-Henning Kamp #define	OMAP_COPY	0x0020
41005f0fdd2SPoul-Henning Kamp #define	OMAP_SHARED	0x0010
41105f0fdd2SPoul-Henning Kamp #define	OMAP_FIXED	0x0100
41205f0fdd2SPoul-Henning Kamp #define	OMAP_INHERIT	0x0800
41305f0fdd2SPoul-Henning Kamp 
41405f0fdd2SPoul-Henning Kamp 	nargs.addr = uap->addr;
41505f0fdd2SPoul-Henning Kamp 	nargs.len = uap->len;
41605f0fdd2SPoul-Henning Kamp 	nargs.prot = cvtbsdprot[uap->prot & 0x7];
41705f0fdd2SPoul-Henning Kamp 	nargs.flags = 0;
41805f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_ANON)
41905f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_ANON;
42005f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_COPY)
42105f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_COPY;
42205f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_SHARED)
42305f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_SHARED;
42405f0fdd2SPoul-Henning Kamp 	else
42505f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_PRIVATE;
42605f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_FIXED)
42705f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_FIXED;
42805f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_INHERIT)
42905f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_INHERIT;
43005f0fdd2SPoul-Henning Kamp 	nargs.fd = uap->fd;
43105f0fdd2SPoul-Henning Kamp 	nargs.pos = uap->pos;
432cb226aaaSPoul-Henning Kamp 	return (mmap(p, &nargs));
43305f0fdd2SPoul-Henning Kamp }
43405f0fdd2SPoul-Henning Kamp #endif				/* COMPAT_43 */
43505f0fdd2SPoul-Henning Kamp 
43605f0fdd2SPoul-Henning Kamp 
437d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
438df8bae1dSRodney W. Grimes struct msync_args {
439651bb817SAlexander Langer 	void *addr;
440df8bae1dSRodney W. Grimes 	int len;
441e6c6af11SDavid Greenman 	int flags;
442df8bae1dSRodney W. Grimes };
443d2d3e875SBruce Evans #endif
444df8bae1dSRodney W. Grimes int
445cb226aaaSPoul-Henning Kamp msync(p, uap)
446df8bae1dSRodney W. Grimes 	struct proc *p;
447df8bae1dSRodney W. Grimes 	struct msync_args *uap;
448df8bae1dSRodney W. Grimes {
449df8bae1dSRodney W. Grimes 	vm_offset_t addr;
450dabee6feSPeter Wemm 	vm_size_t size, pageoff;
451e6c6af11SDavid Greenman 	int flags;
452df8bae1dSRodney W. Grimes 	vm_map_t map;
453df8bae1dSRodney W. Grimes 	int rv;
454df8bae1dSRodney W. Grimes 
455df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
4569154ee6aSPeter Wemm 	size = uap->len;
457e6c6af11SDavid Greenman 	flags = uap->flags;
458e6c6af11SDavid Greenman 
459dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
460dabee6feSPeter Wemm 	addr -= pageoff;
461dabee6feSPeter Wemm 	size += pageoff;
462dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
4639154ee6aSPeter Wemm 	if (addr + size < addr)
464dabee6feSPeter Wemm 		return(EINVAL);
465dabee6feSPeter Wemm 
466dabee6feSPeter Wemm 	if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
4671e62bc63SDavid Greenman 		return (EINVAL);
4681e62bc63SDavid Greenman 
4699154ee6aSPeter Wemm 	map = &p->p_vmspace->vm_map;
4709154ee6aSPeter Wemm 
471df8bae1dSRodney W. Grimes 	/*
472df8bae1dSRodney W. Grimes 	 * XXX Gak!  If size is zero we are supposed to sync "all modified
4730d94caffSDavid Greenman 	 * pages with the region containing addr".  Unfortunately, we don't
4740d94caffSDavid Greenman 	 * really keep track of individual mmaps so we approximate by flushing
4750d94caffSDavid Greenman 	 * the range of the map entry containing addr. This can be incorrect
4760d94caffSDavid Greenman 	 * if the region splits or is coalesced with a neighbor.
477df8bae1dSRodney W. Grimes 	 */
478df8bae1dSRodney W. Grimes 	if (size == 0) {
479df8bae1dSRodney W. Grimes 		vm_map_entry_t entry;
480df8bae1dSRodney W. Grimes 
481df8bae1dSRodney W. Grimes 		vm_map_lock_read(map);
482df8bae1dSRodney W. Grimes 		rv = vm_map_lookup_entry(map, addr, &entry);
483df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
484fbcfcdf7SDavid Greenman 		if (rv == FALSE)
485df8bae1dSRodney W. Grimes 			return (EINVAL);
486df8bae1dSRodney W. Grimes 		addr = entry->start;
487df8bae1dSRodney W. Grimes 		size = entry->end - entry->start;
488df8bae1dSRodney W. Grimes 	}
489e6c6af11SDavid Greenman 
490df8bae1dSRodney W. Grimes 	/*
491df8bae1dSRodney W. Grimes 	 * Clean the pages and interpret the return value.
492df8bae1dSRodney W. Grimes 	 */
4936c534ad8SDavid Greenman 	rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
494e6c6af11SDavid Greenman 	    (flags & MS_INVALIDATE) != 0);
495e6c6af11SDavid Greenman 
496df8bae1dSRodney W. Grimes 	switch (rv) {
497df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
498df8bae1dSRodney W. Grimes 		break;
499df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
500df8bae1dSRodney W. Grimes 		return (EINVAL);	/* Sun returns ENOMEM? */
501df8bae1dSRodney W. Grimes 	case KERN_FAILURE:
502df8bae1dSRodney W. Grimes 		return (EIO);
503df8bae1dSRodney W. Grimes 	default:
504df8bae1dSRodney W. Grimes 		return (EINVAL);
505df8bae1dSRodney W. Grimes 	}
506e6c6af11SDavid Greenman 
507df8bae1dSRodney W. Grimes 	return (0);
508df8bae1dSRodney W. Grimes }
509df8bae1dSRodney W. Grimes 
510d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
511df8bae1dSRodney W. Grimes struct munmap_args {
512651bb817SAlexander Langer 	void *addr;
5139154ee6aSPeter Wemm 	size_t len;
514df8bae1dSRodney W. Grimes };
515d2d3e875SBruce Evans #endif
516df8bae1dSRodney W. Grimes int
517cb226aaaSPoul-Henning Kamp munmap(p, uap)
518df8bae1dSRodney W. Grimes 	register struct proc *p;
519df8bae1dSRodney W. Grimes 	register struct munmap_args *uap;
520df8bae1dSRodney W. Grimes {
521df8bae1dSRodney W. Grimes 	vm_offset_t addr;
522dabee6feSPeter Wemm 	vm_size_t size, pageoff;
523df8bae1dSRodney W. Grimes 	vm_map_t map;
524df8bae1dSRodney W. Grimes 
525df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
5269154ee6aSPeter Wemm 	size = uap->len;
527dabee6feSPeter Wemm 
528dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
529dabee6feSPeter Wemm 	addr -= pageoff;
530dabee6feSPeter Wemm 	size += pageoff;
531dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
5329154ee6aSPeter Wemm 	if (addr + size < addr)
533df8bae1dSRodney W. Grimes 		return(EINVAL);
5349154ee6aSPeter Wemm 
535df8bae1dSRodney W. Grimes 	if (size == 0)
536df8bae1dSRodney W. Grimes 		return (0);
537dabee6feSPeter Wemm 
538df8bae1dSRodney W. Grimes 	/*
5390d94caffSDavid Greenman 	 * Check for illegal addresses.  Watch out for address wrap... Note
5400d94caffSDavid Greenman 	 * that VM_*_ADDRESS are not constants due to casts (argh).
541df8bae1dSRodney W. Grimes 	 */
542bbc0ec52SDavid Greenman 	if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
543df8bae1dSRodney W. Grimes 		return (EINVAL);
54426f9a767SRodney W. Grimes #ifndef i386
545df8bae1dSRodney W. Grimes 	if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
546df8bae1dSRodney W. Grimes 		return (EINVAL);
54726f9a767SRodney W. Grimes #endif
548df8bae1dSRodney W. Grimes 	map = &p->p_vmspace->vm_map;
549df8bae1dSRodney W. Grimes 	/*
550df8bae1dSRodney W. Grimes 	 * Make sure entire range is allocated.
551df8bae1dSRodney W. Grimes 	 */
552df8bae1dSRodney W. Grimes 	if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
553df8bae1dSRodney W. Grimes 		return (EINVAL);
554df8bae1dSRodney W. Grimes 	/* returns nothing but KERN_SUCCESS anyway */
555df8bae1dSRodney W. Grimes 	(void) vm_map_remove(map, addr, addr + size);
556df8bae1dSRodney W. Grimes 	return (0);
557df8bae1dSRodney W. Grimes }
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes void
56090324b07SDavid Greenman munmapfd(p, fd)
56190324b07SDavid Greenman 	struct proc *p;
562df8bae1dSRodney W. Grimes 	int fd;
563df8bae1dSRodney W. Grimes {
564df8bae1dSRodney W. Grimes 	/*
565c4ed5a07SDavid Greenman 	 * XXX should unmap any regions mapped to this file
566df8bae1dSRodney W. Grimes 	 */
56790324b07SDavid Greenman 	p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
568df8bae1dSRodney W. Grimes }
569df8bae1dSRodney W. Grimes 
570d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
571df8bae1dSRodney W. Grimes struct mprotect_args {
572651bb817SAlexander Langer 	const void *addr;
5739154ee6aSPeter Wemm 	size_t len;
574df8bae1dSRodney W. Grimes 	int prot;
575df8bae1dSRodney W. Grimes };
576d2d3e875SBruce Evans #endif
577df8bae1dSRodney W. Grimes int
578cb226aaaSPoul-Henning Kamp mprotect(p, uap)
579df8bae1dSRodney W. Grimes 	struct proc *p;
580df8bae1dSRodney W. Grimes 	struct mprotect_args *uap;
581df8bae1dSRodney W. Grimes {
582df8bae1dSRodney W. Grimes 	vm_offset_t addr;
583dabee6feSPeter Wemm 	vm_size_t size, pageoff;
584df8bae1dSRodney W. Grimes 	register vm_prot_t prot;
585df8bae1dSRodney W. Grimes 
586df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
5879154ee6aSPeter Wemm 	size = uap->len;
588df8bae1dSRodney W. Grimes 	prot = uap->prot & VM_PROT_ALL;
589d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC)
590d0aea04fSJohn Dyson 	if (prot & VM_PROT_READ)
591d0aea04fSJohn Dyson 		prot |= VM_PROT_EXECUTE;
592d0aea04fSJohn Dyson #endif
593df8bae1dSRodney W. Grimes 
594dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
595dabee6feSPeter Wemm 	addr -= pageoff;
596dabee6feSPeter Wemm 	size += pageoff;
597dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
5989154ee6aSPeter Wemm 	if (addr + size < addr)
599dabee6feSPeter Wemm 		return(EINVAL);
600dabee6feSPeter Wemm 
601df8bae1dSRodney W. Grimes 	switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
602df8bae1dSRodney W. Grimes 		FALSE)) {
603df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
604df8bae1dSRodney W. Grimes 		return (0);
605df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
606df8bae1dSRodney W. Grimes 		return (EACCES);
607df8bae1dSRodney W. Grimes 	}
608df8bae1dSRodney W. Grimes 	return (EINVAL);
609df8bae1dSRodney W. Grimes }
610df8bae1dSRodney W. Grimes 
611d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
612dabee6feSPeter Wemm struct minherit_args {
613651bb817SAlexander Langer 	void *addr;
6149154ee6aSPeter Wemm 	size_t len;
615dabee6feSPeter Wemm 	int inherit;
616dabee6feSPeter Wemm };
617dabee6feSPeter Wemm #endif
618dabee6feSPeter Wemm int
619cb226aaaSPoul-Henning Kamp minherit(p, uap)
620dabee6feSPeter Wemm 	struct proc *p;
621dabee6feSPeter Wemm 	struct minherit_args *uap;
622dabee6feSPeter Wemm {
623dabee6feSPeter Wemm 	vm_offset_t addr;
624dabee6feSPeter Wemm 	vm_size_t size, pageoff;
625dabee6feSPeter Wemm 	register vm_inherit_t inherit;
626dabee6feSPeter Wemm 
627dabee6feSPeter Wemm 	addr = (vm_offset_t)uap->addr;
6289154ee6aSPeter Wemm 	size = uap->len;
629dabee6feSPeter Wemm 	inherit = uap->inherit;
630dabee6feSPeter Wemm 
631dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
632dabee6feSPeter Wemm 	addr -= pageoff;
633dabee6feSPeter Wemm 	size += pageoff;
634dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
6359154ee6aSPeter Wemm 	if (addr + size < addr)
636dabee6feSPeter Wemm 		return(EINVAL);
637dabee6feSPeter Wemm 
638dabee6feSPeter Wemm 	switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size,
639dabee6feSPeter Wemm 	    inherit)) {
640dabee6feSPeter Wemm 	case KERN_SUCCESS:
641dabee6feSPeter Wemm 		return (0);
642dabee6feSPeter Wemm 	case KERN_PROTECTION_FAILURE:
643dabee6feSPeter Wemm 		return (EACCES);
644dabee6feSPeter Wemm 	}
645dabee6feSPeter Wemm 	return (EINVAL);
646dabee6feSPeter Wemm }
647dabee6feSPeter Wemm 
648dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_
649df8bae1dSRodney W. Grimes struct madvise_args {
650651bb817SAlexander Langer 	void *addr;
6519154ee6aSPeter Wemm 	size_t len;
652df8bae1dSRodney W. Grimes 	int behav;
653df8bae1dSRodney W. Grimes };
654d2d3e875SBruce Evans #endif
6550d94caffSDavid Greenman 
656df8bae1dSRodney W. Grimes /* ARGSUSED */
657df8bae1dSRodney W. Grimes int
658cb226aaaSPoul-Henning Kamp madvise(p, uap)
659df8bae1dSRodney W. Grimes 	struct proc *p;
660df8bae1dSRodney W. Grimes 	struct madvise_args *uap;
661df8bae1dSRodney W. Grimes {
662f35329acSJohn Dyson 	vm_offset_t start, end;
663b4309055SMatthew Dillon 
664b4309055SMatthew Dillon 	/*
665b4309055SMatthew Dillon 	 * Check for illegal behavior
666b4309055SMatthew Dillon 	 */
6679730a5daSPaul Saab 	if (uap->behav < 0 || uap->behav > MADV_CORE)
668b4309055SMatthew Dillon 		return (EINVAL);
669867a482dSJohn Dyson 	/*
670867a482dSJohn Dyson 	 * Check for illegal addresses.  Watch out for address wrap... Note
671867a482dSJohn Dyson 	 * that VM_*_ADDRESS are not constants due to casts (argh).
672867a482dSJohn Dyson 	 */
673867a482dSJohn Dyson 	if (VM_MAXUSER_ADDRESS > 0 &&
674867a482dSJohn Dyson 		((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS)
675867a482dSJohn Dyson 		return (EINVAL);
676867a482dSJohn Dyson #ifndef i386
677867a482dSJohn Dyson 	if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS)
678867a482dSJohn Dyson 		return (EINVAL);
679867a482dSJohn Dyson #endif
680867a482dSJohn Dyson 	if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
681867a482dSJohn Dyson 		return (EINVAL);
682867a482dSJohn Dyson 
683867a482dSJohn Dyson 	/*
684867a482dSJohn Dyson 	 * Since this routine is only advisory, we default to conservative
685867a482dSJohn Dyson 	 * behavior.
686867a482dSJohn Dyson 	 */
687cd6eea25SDavid Greenman 	start = trunc_page((vm_offset_t) uap->addr);
688cd6eea25SDavid Greenman 	end = round_page((vm_offset_t) uap->addr + uap->len);
689867a482dSJohn Dyson 
690b4309055SMatthew Dillon 	if (vm_map_madvise(&p->p_vmspace->vm_map, start, end, uap->behav))
691b4309055SMatthew Dillon 		return (EINVAL);
692867a482dSJohn Dyson 	return (0);
693df8bae1dSRodney W. Grimes }
694df8bae1dSRodney W. Grimes 
695d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
696df8bae1dSRodney W. Grimes struct mincore_args {
697651bb817SAlexander Langer 	const void *addr;
6989154ee6aSPeter Wemm 	size_t len;
699df8bae1dSRodney W. Grimes 	char *vec;
700df8bae1dSRodney W. Grimes };
701d2d3e875SBruce Evans #endif
7020d94caffSDavid Greenman 
703df8bae1dSRodney W. Grimes /* ARGSUSED */
704df8bae1dSRodney W. Grimes int
705cb226aaaSPoul-Henning Kamp mincore(p, uap)
706df8bae1dSRodney W. Grimes 	struct proc *p;
707df8bae1dSRodney W. Grimes 	struct mincore_args *uap;
708df8bae1dSRodney W. Grimes {
709867a482dSJohn Dyson 	vm_offset_t addr, first_addr;
710867a482dSJohn Dyson 	vm_offset_t end, cend;
711867a482dSJohn Dyson 	pmap_t pmap;
712867a482dSJohn Dyson 	vm_map_t map;
71302c04a2fSJohn Dyson 	char *vec;
714867a482dSJohn Dyson 	int error;
715867a482dSJohn Dyson 	int vecindex, lastvecindex;
716867a482dSJohn Dyson 	register vm_map_entry_t current;
717867a482dSJohn Dyson 	vm_map_entry_t entry;
718867a482dSJohn Dyson 	int mincoreinfo;
719dd2622a8SAlan Cox 	unsigned int timestamp;
720df8bae1dSRodney W. Grimes 
721867a482dSJohn Dyson 	/*
722867a482dSJohn Dyson 	 * Make sure that the addresses presented are valid for user
723867a482dSJohn Dyson 	 * mode.
724867a482dSJohn Dyson 	 */
725867a482dSJohn Dyson 	first_addr = addr = trunc_page((vm_offset_t) uap->addr);
7269154ee6aSPeter Wemm 	end = addr + (vm_size_t)round_page(uap->len);
72702c04a2fSJohn Dyson 	if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS)
72802c04a2fSJohn Dyson 		return (EINVAL);
72902c04a2fSJohn Dyson 	if (end < addr)
73002c04a2fSJohn Dyson 		return (EINVAL);
73102c04a2fSJohn Dyson 
732867a482dSJohn Dyson 	/*
733867a482dSJohn Dyson 	 * Address of byte vector
734867a482dSJohn Dyson 	 */
73502c04a2fSJohn Dyson 	vec = uap->vec;
736867a482dSJohn Dyson 
737867a482dSJohn Dyson 	map = &p->p_vmspace->vm_map;
738b1028ad1SLuoqi Chen 	pmap = vmspace_pmap(p->p_vmspace);
739867a482dSJohn Dyson 
740eff50fcdSAlan Cox 	vm_map_lock_read(map);
741dd2622a8SAlan Cox RestartScan:
742dd2622a8SAlan Cox 	timestamp = map->timestamp;
743867a482dSJohn Dyson 
744867a482dSJohn Dyson 	if (!vm_map_lookup_entry(map, addr, &entry))
745867a482dSJohn Dyson 		entry = entry->next;
746867a482dSJohn Dyson 
747867a482dSJohn Dyson 	/*
748867a482dSJohn Dyson 	 * Do this on a map entry basis so that if the pages are not
749867a482dSJohn Dyson 	 * in the current processes address space, we can easily look
750867a482dSJohn Dyson 	 * up the pages elsewhere.
751867a482dSJohn Dyson 	 */
752867a482dSJohn Dyson 	lastvecindex = -1;
753867a482dSJohn Dyson 	for(current = entry;
754867a482dSJohn Dyson 		(current != &map->header) && (current->start < end);
755867a482dSJohn Dyson 		current = current->next) {
756867a482dSJohn Dyson 
757867a482dSJohn Dyson 		/*
758867a482dSJohn Dyson 		 * ignore submaps (for now) or null objects
759867a482dSJohn Dyson 		 */
7609fdfe602SMatthew Dillon 		if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
761867a482dSJohn Dyson 			current->object.vm_object == NULL)
762867a482dSJohn Dyson 			continue;
763867a482dSJohn Dyson 
764867a482dSJohn Dyson 		/*
765867a482dSJohn Dyson 		 * limit this scan to the current map entry and the
766867a482dSJohn Dyson 		 * limits for the mincore call
767867a482dSJohn Dyson 		 */
768867a482dSJohn Dyson 		if (addr < current->start)
769867a482dSJohn Dyson 			addr = current->start;
770867a482dSJohn Dyson 		cend = current->end;
771867a482dSJohn Dyson 		if (cend > end)
772867a482dSJohn Dyson 			cend = end;
773867a482dSJohn Dyson 
774867a482dSJohn Dyson 		/*
775867a482dSJohn Dyson 		 * scan this entry one page at a time
776867a482dSJohn Dyson 		 */
777867a482dSJohn Dyson 		while(addr < cend) {
778867a482dSJohn Dyson 			/*
779867a482dSJohn Dyson 			 * Check pmap first, it is likely faster, also
780867a482dSJohn Dyson 			 * it can provide info as to whether we are the
781867a482dSJohn Dyson 			 * one referencing or modifying the page.
782867a482dSJohn Dyson 			 */
783867a482dSJohn Dyson 			mincoreinfo = pmap_mincore(pmap, addr);
784867a482dSJohn Dyson 			if (!mincoreinfo) {
785867a482dSJohn Dyson 				vm_pindex_t pindex;
786867a482dSJohn Dyson 				vm_ooffset_t offset;
787867a482dSJohn Dyson 				vm_page_t m;
788867a482dSJohn Dyson 				/*
789867a482dSJohn Dyson 				 * calculate the page index into the object
790867a482dSJohn Dyson 				 */
791867a482dSJohn Dyson 				offset = current->offset + (addr - current->start);
792867a482dSJohn Dyson 				pindex = OFF_TO_IDX(offset);
793867a482dSJohn Dyson 				m = vm_page_lookup(current->object.vm_object,
794867a482dSJohn Dyson 					pindex);
795867a482dSJohn Dyson 				/*
796867a482dSJohn Dyson 				 * if the page is resident, then gather information about
797867a482dSJohn Dyson 				 * it.
798867a482dSJohn Dyson 				 */
799867a482dSJohn Dyson 				if (m) {
800867a482dSJohn Dyson 					mincoreinfo = MINCORE_INCORE;
801867a482dSJohn Dyson 					if (m->dirty ||
80267bf6868SJohn Dyson 						pmap_is_modified(VM_PAGE_TO_PHYS(m)))
803867a482dSJohn Dyson 						mincoreinfo |= MINCORE_MODIFIED_OTHER;
804867a482dSJohn Dyson 					if ((m->flags & PG_REFERENCED) ||
8059b5a5d81SJohn Dyson 						pmap_ts_referenced(VM_PAGE_TO_PHYS(m))) {
806e69763a3SDoug Rabson 						vm_page_flag_set(m, PG_REFERENCED);
807867a482dSJohn Dyson 						mincoreinfo |= MINCORE_REFERENCED_OTHER;
80802c04a2fSJohn Dyson 					}
809867a482dSJohn Dyson 				}
8109b5a5d81SJohn Dyson 			}
811867a482dSJohn Dyson 
812867a482dSJohn Dyson 			/*
813dd2622a8SAlan Cox 			 * subyte may page fault.  In case it needs to modify
814dd2622a8SAlan Cox 			 * the map, we release the lock.
815dd2622a8SAlan Cox 			 */
816dd2622a8SAlan Cox 			vm_map_unlock_read(map);
817dd2622a8SAlan Cox 
818dd2622a8SAlan Cox 			/*
819867a482dSJohn Dyson 			 * calculate index into user supplied byte vector
820867a482dSJohn Dyson 			 */
821867a482dSJohn Dyson 			vecindex = OFF_TO_IDX(addr - first_addr);
822867a482dSJohn Dyson 
823867a482dSJohn Dyson 			/*
824867a482dSJohn Dyson 			 * If we have skipped map entries, we need to make sure that
825867a482dSJohn Dyson 			 * the byte vector is zeroed for those skipped entries.
826867a482dSJohn Dyson 			 */
827867a482dSJohn Dyson 			while((lastvecindex + 1) < vecindex) {
828867a482dSJohn Dyson 				error = subyte( vec + lastvecindex, 0);
829867a482dSJohn Dyson 				if (error) {
830867a482dSJohn Dyson 					return (EFAULT);
831867a482dSJohn Dyson 				}
832867a482dSJohn Dyson 				++lastvecindex;
833867a482dSJohn Dyson 			}
834867a482dSJohn Dyson 
835867a482dSJohn Dyson 			/*
836867a482dSJohn Dyson 			 * Pass the page information to the user
837867a482dSJohn Dyson 			 */
838867a482dSJohn Dyson 			error = subyte( vec + vecindex, mincoreinfo);
839867a482dSJohn Dyson 			if (error) {
840867a482dSJohn Dyson 				return (EFAULT);
841867a482dSJohn Dyson 			}
842dd2622a8SAlan Cox 
843dd2622a8SAlan Cox 			/*
844dd2622a8SAlan Cox 			 * If the map has changed, due to the subyte, the previous
845dd2622a8SAlan Cox 			 * output may be invalid.
846dd2622a8SAlan Cox 			 */
847dd2622a8SAlan Cox 			vm_map_lock_read(map);
848dd2622a8SAlan Cox 			if (timestamp != map->timestamp)
849dd2622a8SAlan Cox 				goto RestartScan;
850dd2622a8SAlan Cox 
851867a482dSJohn Dyson 			lastvecindex = vecindex;
85202c04a2fSJohn Dyson 			addr += PAGE_SIZE;
85302c04a2fSJohn Dyson 		}
854867a482dSJohn Dyson 	}
855867a482dSJohn Dyson 
856867a482dSJohn Dyson 	/*
857dd2622a8SAlan Cox 	 * subyte may page fault.  In case it needs to modify
858dd2622a8SAlan Cox 	 * the map, we release the lock.
859dd2622a8SAlan Cox 	 */
860dd2622a8SAlan Cox 	vm_map_unlock_read(map);
861dd2622a8SAlan Cox 
862dd2622a8SAlan Cox 	/*
863867a482dSJohn Dyson 	 * Zero the last entries in the byte vector.
864867a482dSJohn Dyson 	 */
865867a482dSJohn Dyson 	vecindex = OFF_TO_IDX(end - first_addr);
866867a482dSJohn Dyson 	while((lastvecindex + 1) < vecindex) {
867867a482dSJohn Dyson 		error = subyte( vec + lastvecindex, 0);
868867a482dSJohn Dyson 		if (error) {
869867a482dSJohn Dyson 			return (EFAULT);
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);
882dd2622a8SAlan Cox 
88302c04a2fSJohn Dyson 	return (0);
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
892df8bae1dSRodney W. Grimes int
893cb226aaaSPoul-Henning Kamp mlock(p, uap)
894df8bae1dSRodney W. Grimes 	struct proc *p;
895df8bae1dSRodney W. Grimes 	struct mlock_args *uap;
896df8bae1dSRodney W. Grimes {
897df8bae1dSRodney W. Grimes 	vm_offset_t addr;
898dabee6feSPeter Wemm 	vm_size_t size, pageoff;
899df8bae1dSRodney W. Grimes 	int error;
900df8bae1dSRodney W. Grimes 
901df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
9029154ee6aSPeter Wemm 	size = uap->len;
9039154ee6aSPeter Wemm 
904dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
905dabee6feSPeter Wemm 	addr -= pageoff;
906dabee6feSPeter Wemm 	size += pageoff;
907dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
908dabee6feSPeter Wemm 
909dabee6feSPeter Wemm 	/* disable wrap around */
9109154ee6aSPeter Wemm 	if (addr + size < addr)
911df8bae1dSRodney W. Grimes 		return (EINVAL);
912dabee6feSPeter Wemm 
913df8bae1dSRodney W. Grimes 	if (atop(size) + cnt.v_wire_count > vm_page_max_wired)
914df8bae1dSRodney W. Grimes 		return (EAGAIN);
9159154ee6aSPeter Wemm 
916df8bae1dSRodney W. Grimes #ifdef pmap_wired_count
917df8bae1dSRodney W. Grimes 	if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
918df8bae1dSRodney W. Grimes 	    p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
9194a40e3d4SJohn Dyson 		return (ENOMEM);
920df8bae1dSRodney W. Grimes #else
921f711d546SPoul-Henning Kamp 	error = suser(p);
92205f0fdd2SPoul-Henning Kamp 	if (error)
923df8bae1dSRodney W. Grimes 		return (error);
924df8bae1dSRodney W. Grimes #endif
925df8bae1dSRodney W. Grimes 
9267aaaa4fdSJohn Dyson 	error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
927df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
928df8bae1dSRodney W. Grimes }
929df8bae1dSRodney W. Grimes 
930d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
9314a40e3d4SJohn Dyson struct mlockall_args {
9324a40e3d4SJohn Dyson 	int	how;
9334a40e3d4SJohn Dyson };
9344a40e3d4SJohn Dyson #endif
9354a40e3d4SJohn Dyson 
9364a40e3d4SJohn Dyson int
937cb226aaaSPoul-Henning Kamp mlockall(p, uap)
9384a40e3d4SJohn Dyson 	struct proc *p;
9394a40e3d4SJohn Dyson 	struct mlockall_args *uap;
9404a40e3d4SJohn Dyson {
9414a40e3d4SJohn Dyson 	return 0;
9424a40e3d4SJohn Dyson }
9434a40e3d4SJohn Dyson 
9444a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_
9454a40e3d4SJohn Dyson struct mlockall_args {
9464a40e3d4SJohn Dyson 	int	how;
9474a40e3d4SJohn Dyson };
9484a40e3d4SJohn Dyson #endif
9494a40e3d4SJohn Dyson 
9504a40e3d4SJohn Dyson int
951cb226aaaSPoul-Henning Kamp munlockall(p, uap)
9524a40e3d4SJohn Dyson 	struct proc *p;
9534a40e3d4SJohn Dyson 	struct munlockall_args *uap;
9544a40e3d4SJohn Dyson {
9554a40e3d4SJohn Dyson 	return 0;
9564a40e3d4SJohn Dyson }
9574a40e3d4SJohn Dyson 
9584a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_
959df8bae1dSRodney W. Grimes struct munlock_args {
960651bb817SAlexander Langer 	const void *addr;
961df8bae1dSRodney W. Grimes 	size_t len;
962df8bae1dSRodney W. Grimes };
963d2d3e875SBruce Evans #endif
964df8bae1dSRodney W. Grimes int
965cb226aaaSPoul-Henning Kamp munlock(p, uap)
966df8bae1dSRodney W. Grimes 	struct proc *p;
967df8bae1dSRodney W. Grimes 	struct munlock_args *uap;
968df8bae1dSRodney W. Grimes {
969df8bae1dSRodney W. Grimes 	vm_offset_t addr;
970dabee6feSPeter Wemm 	vm_size_t size, pageoff;
971df8bae1dSRodney W. Grimes 	int error;
972df8bae1dSRodney W. Grimes 
973df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
9749154ee6aSPeter Wemm 	size = uap->len;
9759154ee6aSPeter Wemm 
976dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
977dabee6feSPeter Wemm 	addr -= pageoff;
978dabee6feSPeter Wemm 	size += pageoff;
979dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
980dabee6feSPeter Wemm 
981dabee6feSPeter Wemm 	/* disable wrap around */
9829154ee6aSPeter Wemm 	if (addr + size < addr)
983df8bae1dSRodney W. Grimes 		return (EINVAL);
984dabee6feSPeter Wemm 
985df8bae1dSRodney W. Grimes #ifndef pmap_wired_count
986f711d546SPoul-Henning Kamp 	error = suser(p);
98705f0fdd2SPoul-Henning Kamp 	if (error)
988df8bae1dSRodney W. Grimes 		return (error);
989df8bae1dSRodney W. Grimes #endif
990df8bae1dSRodney W. Grimes 
9917aaaa4fdSJohn Dyson 	error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
992df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
993df8bae1dSRodney W. Grimes }
994df8bae1dSRodney W. Grimes 
995df8bae1dSRodney W. Grimes /*
996df8bae1dSRodney W. Grimes  * Internal version of mmap.
997df8bae1dSRodney W. Grimes  * Currently used by mmap, exec, and sys5 shared memory.
998df8bae1dSRodney W. Grimes  * Handle is either a vnode pointer or NULL for MAP_ANON.
999df8bae1dSRodney W. Grimes  */
1000df8bae1dSRodney W. Grimes int
1001b9dcd593SBruce Evans vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1002b9dcd593SBruce Evans 	vm_prot_t maxprot, int flags,
1003651bb817SAlexander Langer 	void *handle,
1004b9dcd593SBruce Evans 	vm_ooffset_t foff)
1005df8bae1dSRodney W. Grimes {
1006df8bae1dSRodney W. Grimes 	boolean_t fitit;
1007fcae040bSJohn Dyson 	vm_object_t object;
1008df8bae1dSRodney W. Grimes 	struct vnode *vp = NULL;
100924a1cce3SDavid Greenman 	objtype_t type;
1010df8bae1dSRodney W. Grimes 	int rv = KERN_SUCCESS;
1011bd7e5f99SJohn Dyson 	vm_ooffset_t objsize;
1012bd7e5f99SJohn Dyson 	int docow;
101306cb7259SDavid Greenman 	struct proc *p = curproc;
1014df8bae1dSRodney W. Grimes 
1015df8bae1dSRodney W. Grimes 	if (size == 0)
1016df8bae1dSRodney W. Grimes 		return (0);
1017df8bae1dSRodney W. Grimes 
101806cb7259SDavid Greenman 	objsize = size = round_page(size);
1019df8bae1dSRodney W. Grimes 
1020df8bae1dSRodney W. Grimes 	/*
1021bc9ad247SDavid Greenman 	 * We currently can only deal with page aligned file offsets.
1022bc9ad247SDavid Greenman 	 * The check is here rather than in the syscall because the
1023bc9ad247SDavid Greenman 	 * kernel calls this function internally for other mmaping
1024bc9ad247SDavid Greenman 	 * operations (such as in exec) and non-aligned offsets will
1025bc9ad247SDavid Greenman 	 * cause pmap inconsistencies...so we want to be sure to
1026bc9ad247SDavid Greenman 	 * disallow this in all cases.
1027bc9ad247SDavid Greenman 	 */
1028bc9ad247SDavid Greenman 	if (foff & PAGE_MASK)
1029bc9ad247SDavid Greenman 		return (EINVAL);
1030bc9ad247SDavid Greenman 
103106cb7259SDavid Greenman 	if ((flags & MAP_FIXED) == 0) {
103206cb7259SDavid Greenman 		fitit = TRUE;
103306cb7259SDavid Greenman 		*addr = round_page(*addr);
103406cb7259SDavid Greenman 	} else {
103506cb7259SDavid Greenman 		if (*addr != trunc_page(*addr))
103606cb7259SDavid Greenman 			return (EINVAL);
103706cb7259SDavid Greenman 		fitit = FALSE;
103806cb7259SDavid Greenman 		(void) vm_map_remove(map, *addr, *addr + size);
103906cb7259SDavid Greenman 	}
104006cb7259SDavid Greenman 
1041bc9ad247SDavid Greenman 	/*
104224a1cce3SDavid Greenman 	 * Lookup/allocate object.
1043df8bae1dSRodney W. Grimes 	 */
10445f55e841SDavid Greenman 	if (flags & MAP_ANON) {
1045851c12ffSJohn Dyson 		type = OBJT_DEFAULT;
10465f55e841SDavid Greenman 		/*
10475f55e841SDavid Greenman 		 * Unnamed anonymous regions always start at 0.
10485f55e841SDavid Greenman 		 */
104967bf6868SJohn Dyson 		if (handle == 0)
10505f55e841SDavid Greenman 			foff = 0;
10515f55e841SDavid Greenman 	} else {
1052df8bae1dSRodney W. Grimes 		vp = (struct vnode *) handle;
1053df8bae1dSRodney W. Grimes 		if (vp->v_type == VCHR) {
105424a1cce3SDavid Greenman 			type = OBJT_DEVICE;
1055a23d65bfSBruce Evans 			handle = (void *)(intptr_t)vp->v_rdev;
105606cb7259SDavid Greenman 		} else {
105706cb7259SDavid Greenman 			struct vattr vat;
105806cb7259SDavid Greenman 			int error;
105906cb7259SDavid Greenman 
106006cb7259SDavid Greenman 			error = VOP_GETATTR(vp, &vat, p->p_ucred, p);
106106cb7259SDavid Greenman 			if (error)
106206cb7259SDavid Greenman 				return (error);
1063bd7e5f99SJohn Dyson 			objsize = round_page(vat.va_size);
106424a1cce3SDavid Greenman 			type = OBJT_VNODE;
106500d76afeSGuido van Rooij 			/*
106600d76afeSGuido van Rooij 			 * if it is a regular file without any references
106700d76afeSGuido van Rooij 			 * we do not need to sync it.
106800d76afeSGuido van Rooij 			 */
106900d76afeSGuido van Rooij 			if (vp->v_type == VREG && vat.va_nlink == 0) {
107000d76afeSGuido van Rooij 				flags |= MAP_NOSYNC;
107100d76afeSGuido van Rooij 			}
1072df8bae1dSRodney W. Grimes 		}
107306cb7259SDavid Greenman 	}
107494328e90SJohn Dyson 
107594328e90SJohn Dyson 	if (handle == NULL) {
107694328e90SJohn Dyson 		object = NULL;
10774738fa09SAlan Cox 		docow = 0;
107894328e90SJohn Dyson 	} else {
10790a0a85b3SJohn Dyson 		object = vm_pager_allocate(type,
10806cde7a16SDavid Greenman 			handle, objsize, prot, foff);
108124a1cce3SDavid Greenman 		if (object == NULL)
108224a1cce3SDavid Greenman 			return (type == OBJT_DEVICE ? EINVAL : ENOMEM);
10834738fa09SAlan Cox 		docow = MAP_PREFAULT_PARTIAL;
108494328e90SJohn Dyson 	}
1085df8bae1dSRodney W. Grimes 
10865850152dSJohn Dyson 	/*
10878f2ec877SDavid Greenman 	 * Force device mappings to be shared.
10885850152dSJohn Dyson 	 */
10898f2ec877SDavid Greenman 	if (type == OBJT_DEVICE) {
10908f2ec877SDavid Greenman 		flags &= ~(MAP_PRIVATE|MAP_COPY);
10915850152dSJohn Dyson 		flags |= MAP_SHARED;
10928f2ec877SDavid Greenman 	}
10935850152dSJohn Dyson 
10944f79d873SMatthew Dillon 	if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
10954738fa09SAlan Cox 		docow |= MAP_COPY_ON_WRITE;
10964f79d873SMatthew Dillon 	if (flags & MAP_NOSYNC)
10974f79d873SMatthew Dillon 		docow |= MAP_DISABLE_SYNCER;
10989730a5daSPaul Saab 	if (flags & MAP_NOCORE)
10999730a5daSPaul Saab 		docow |= MAP_DISABLE_COREDUMP;
11005850152dSJohn Dyson 
1101d0aea04fSJohn Dyson #if defined(VM_PROT_READ_IS_EXEC)
1102d0aea04fSJohn Dyson 	if (prot & VM_PROT_READ)
1103d0aea04fSJohn Dyson 		prot |= VM_PROT_EXECUTE;
1104d0aea04fSJohn Dyson 
1105d0aea04fSJohn Dyson 	if (maxprot & VM_PROT_READ)
1106d0aea04fSJohn Dyson 		maxprot |= VM_PROT_EXECUTE;
1107d0aea04fSJohn Dyson #endif
1108d0aea04fSJohn Dyson 
11090a0a85b3SJohn Dyson 	if (fitit) {
11100a0a85b3SJohn Dyson 		*addr = pmap_addr_hint(object, *addr, size);
11110a0a85b3SJohn Dyson 	}
11120a0a85b3SJohn Dyson 
11132267af78SJulian Elischer 	if (flags & MAP_STACK)
11142267af78SJulian Elischer 		rv = vm_map_stack (map, *addr, size, prot,
11152267af78SJulian Elischer 				   maxprot, docow);
11162267af78SJulian Elischer 	else
1117bd7e5f99SJohn Dyson 		rv = vm_map_find(map, object, foff, addr, size, fitit,
1118bd7e5f99SJohn Dyson 				 prot, maxprot, docow);
1119bd7e5f99SJohn Dyson 
1120df8bae1dSRodney W. Grimes 	if (rv != KERN_SUCCESS) {
11217fb0c17eSDavid Greenman 		/*
112224a1cce3SDavid Greenman 		 * Lose the object reference. Will destroy the
112324a1cce3SDavid Greenman 		 * object if it's an unnamed anonymous mapping
112424a1cce3SDavid Greenman 		 * or named anonymous without other references.
11257fb0c17eSDavid Greenman 		 */
1126df8bae1dSRodney W. Grimes 		vm_object_deallocate(object);
1127df8bae1dSRodney W. Grimes 		goto out;
1128df8bae1dSRodney W. Grimes 	}
1129e17bed12SJohn Dyson 
1130df8bae1dSRodney W. Grimes 	/*
1131df8bae1dSRodney W. Grimes 	 * Shared memory is also shared with children.
1132df8bae1dSRodney W. Grimes 	 */
11335850152dSJohn Dyson 	if (flags & (MAP_SHARED|MAP_INHERIT)) {
1134df8bae1dSRodney W. Grimes 		rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1135df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
11367fb0c17eSDavid Greenman 			(void) vm_map_remove(map, *addr, *addr + size);
1137df8bae1dSRodney W. Grimes 			goto out;
1138df8bae1dSRodney W. Grimes 		}
1139df8bae1dSRodney W. Grimes 	}
1140df8bae1dSRodney W. Grimes out:
1141df8bae1dSRodney W. Grimes 	switch (rv) {
1142df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
1143df8bae1dSRodney W. Grimes 		return (0);
1144df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
1145df8bae1dSRodney W. Grimes 	case KERN_NO_SPACE:
1146df8bae1dSRodney W. Grimes 		return (ENOMEM);
1147df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
1148df8bae1dSRodney W. Grimes 		return (EACCES);
1149df8bae1dSRodney W. Grimes 	default:
1150df8bae1dSRodney W. Grimes 		return (EINVAL);
1151df8bae1dSRodney W. Grimes 	}
1152df8bae1dSRodney W. Grimes }
1153