xref: /freebsd/sys/vm/vm_mmap.c (revision cac597e4c08d49308f10e765b04ff126a73607de)
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
19df8bae1dSRodney W. Grimes  *    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
41cac597e4SBruce Evans  * $Id: vm_mmap.c,v 1.29 1995/11/12 06:43:24 bde Exp $
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes /*
45df8bae1dSRodney W. Grimes  * Mapped file (mmap) interface to VM
46df8bae1dSRodney W. Grimes  */
47df8bae1dSRodney W. Grimes 
48df8bae1dSRodney W. Grimes #include <sys/param.h>
49df8bae1dSRodney W. Grimes #include <sys/systm.h>
50d2d3e875SBruce Evans #include <sys/sysproto.h>
51df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
52df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
54df8bae1dSRodney W. Grimes #include <sys/vnode.h>
55df8bae1dSRodney W. Grimes #include <sys/file.h>
56df8bae1dSRodney W. Grimes #include <sys/mman.h>
57df8bae1dSRodney W. Grimes #include <sys/conf.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <miscfs/specfs/specdev.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <vm/vm.h>
62df8bae1dSRodney W. Grimes #include <vm/vm_pager.h>
63b5e8ce9fSBruce Evans #include <vm/vm_pageout.h>
64df8bae1dSRodney W. Grimes #include <vm/vm_prot.h>
65df8bae1dSRodney W. Grimes 
66cac597e4SBruce Evans extern void	pmap_object_init_pt __P((pmap_t pmap, vm_offset_t addr,
67cac597e4SBruce Evans 					 vm_object_t object, vm_offset_t offset,
68cac597e4SBruce Evans 					 vm_offset_t size));
69f720dc2cSDavid Greenman 
70d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
71df8bae1dSRodney W. Grimes struct sbrk_args {
72df8bae1dSRodney W. Grimes 	int incr;
73df8bae1dSRodney W. Grimes };
74d2d3e875SBruce Evans #endif
750d94caffSDavid Greenman 
76df8bae1dSRodney W. Grimes /* ARGSUSED */
77df8bae1dSRodney W. Grimes int
78df8bae1dSRodney W. Grimes sbrk(p, uap, retval)
79df8bae1dSRodney W. Grimes 	struct proc *p;
80df8bae1dSRodney W. Grimes 	struct sbrk_args *uap;
81df8bae1dSRodney W. Grimes 	int *retval;
82df8bae1dSRodney W. Grimes {
83df8bae1dSRodney W. Grimes 
84df8bae1dSRodney W. Grimes 	/* Not yet implemented */
85df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
86df8bae1dSRodney W. Grimes }
87df8bae1dSRodney W. Grimes 
88d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
89df8bae1dSRodney W. Grimes struct sstk_args {
90df8bae1dSRodney W. Grimes 	int incr;
91df8bae1dSRodney W. Grimes };
92d2d3e875SBruce Evans #endif
930d94caffSDavid Greenman 
94df8bae1dSRodney W. Grimes /* ARGSUSED */
95df8bae1dSRodney W. Grimes int
96df8bae1dSRodney W. Grimes sstk(p, uap, retval)
97df8bae1dSRodney W. Grimes 	struct proc *p;
98df8bae1dSRodney W. Grimes 	struct sstk_args *uap;
99df8bae1dSRodney W. Grimes 	int *retval;
100df8bae1dSRodney W. Grimes {
101df8bae1dSRodney W. Grimes 
102df8bae1dSRodney W. Grimes 	/* Not yet implemented */
103df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
104df8bae1dSRodney W. Grimes }
105df8bae1dSRodney W. Grimes 
106df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
107d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
108df8bae1dSRodney W. Grimes struct getpagesize_args {
109df8bae1dSRodney W. Grimes 	int dummy;
110df8bae1dSRodney W. Grimes };
111d2d3e875SBruce Evans #endif
1120d94caffSDavid Greenman 
113df8bae1dSRodney W. Grimes /* ARGSUSED */
114df8bae1dSRodney W. Grimes int
115df8bae1dSRodney W. Grimes ogetpagesize(p, uap, retval)
116df8bae1dSRodney W. Grimes 	struct proc *p;
117df8bae1dSRodney W. Grimes 	struct getpagesize_args *uap;
118df8bae1dSRodney W. Grimes 	int *retval;
119df8bae1dSRodney W. Grimes {
120df8bae1dSRodney W. Grimes 
121df8bae1dSRodney W. Grimes 	*retval = PAGE_SIZE;
122df8bae1dSRodney W. Grimes 	return (0);
123df8bae1dSRodney W. Grimes }
124df8bae1dSRodney W. Grimes #endif				/* COMPAT_43 || COMPAT_SUNOS */
125df8bae1dSRodney W. Grimes 
126d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
127df8bae1dSRodney W. Grimes struct mmap_args {
128df8bae1dSRodney W. Grimes 	caddr_t addr;
129df8bae1dSRodney W. Grimes 	size_t len;
130df8bae1dSRodney W. Grimes 	int prot;
131df8bae1dSRodney W. Grimes 	int flags;
132df8bae1dSRodney W. Grimes 	int fd;
133df8bae1dSRodney W. Grimes 	long pad;
134df8bae1dSRodney W. Grimes 	off_t pos;
135df8bae1dSRodney W. Grimes };
136d2d3e875SBruce Evans #endif
137df8bae1dSRodney W. Grimes 
138df8bae1dSRodney W. Grimes int
139df8bae1dSRodney W. Grimes mmap(p, uap, retval)
140df8bae1dSRodney W. Grimes 	struct proc *p;
141df8bae1dSRodney W. Grimes 	register struct mmap_args *uap;
142df8bae1dSRodney W. Grimes 	int *retval;
143df8bae1dSRodney W. Grimes {
144df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
145df8bae1dSRodney W. Grimes 	register struct file *fp;
146df8bae1dSRodney W. Grimes 	struct vnode *vp;
147df8bae1dSRodney W. Grimes 	vm_offset_t addr;
148df8bae1dSRodney W. Grimes 	vm_size_t size;
149df8bae1dSRodney W. Grimes 	vm_prot_t prot, maxprot;
150df8bae1dSRodney W. Grimes 	caddr_t handle;
151df8bae1dSRodney W. Grimes 	int flags, error;
152df8bae1dSRodney W. Grimes 
153df8bae1dSRodney W. Grimes 	prot = uap->prot & VM_PROT_ALL;
154df8bae1dSRodney W. Grimes 	flags = uap->flags;
155df8bae1dSRodney W. Grimes 	/*
1560d94caffSDavid Greenman 	 * Address (if FIXED) must be page aligned. Size is implicitly rounded
1570d94caffSDavid Greenman 	 * to a page boundary.
158df8bae1dSRodney W. Grimes 	 */
159df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
160df8bae1dSRodney W. Grimes 	if (((flags & MAP_FIXED) && (addr & PAGE_MASK)) ||
161df8bae1dSRodney W. Grimes 	    (ssize_t) uap->len < 0 || ((flags & MAP_ANON) && uap->fd != -1))
162df8bae1dSRodney W. Grimes 		return (EINVAL);
163df8bae1dSRodney W. Grimes 	size = (vm_size_t) round_page(uap->len);
164df8bae1dSRodney W. Grimes 	/*
1650d94caffSDavid Greenman 	 * Check for illegal addresses.  Watch out for address wrap... Note
1660d94caffSDavid Greenman 	 * that VM_*_ADDRESS are not constants due to casts (argh).
167df8bae1dSRodney W. Grimes 	 */
168df8bae1dSRodney W. Grimes 	if (flags & MAP_FIXED) {
169bbc0ec52SDavid Greenman 		if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
170df8bae1dSRodney W. Grimes 			return (EINVAL);
17126f9a767SRodney W. Grimes #ifndef i386
172df8bae1dSRodney W. Grimes 		if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
173df8bae1dSRodney W. Grimes 			return (EINVAL);
17426f9a767SRodney W. Grimes #endif
175bbc0ec52SDavid Greenman 		if (addr + size < addr)
176df8bae1dSRodney W. Grimes 			return (EINVAL);
177df8bae1dSRodney W. Grimes 	}
178df8bae1dSRodney W. Grimes 	/*
1790d94caffSDavid Greenman 	 * XXX if no hint provided for a non-fixed mapping place it after the
1800d94caffSDavid Greenman 	 * end of the largest possible heap.
181df8bae1dSRodney W. Grimes 	 *
1820d94caffSDavid Greenman 	 * There should really be a pmap call to determine a reasonable location.
183df8bae1dSRodney W. Grimes 	 */
184df8bae1dSRodney W. Grimes 	if (addr == 0 && (flags & MAP_FIXED) == 0)
185df8bae1dSRodney W. Grimes 		addr = round_page(p->p_vmspace->vm_daddr + MAXDSIZ);
186df8bae1dSRodney W. Grimes 	if (flags & MAP_ANON) {
187df8bae1dSRodney W. Grimes 		/*
188df8bae1dSRodney W. Grimes 		 * Mapping blank space is trivial.
189df8bae1dSRodney W. Grimes 		 */
190df8bae1dSRodney W. Grimes 		handle = NULL;
191df8bae1dSRodney W. Grimes 		maxprot = VM_PROT_ALL;
192df8bae1dSRodney W. Grimes 	} else {
193df8bae1dSRodney W. Grimes 		/*
1940d94caffSDavid Greenman 		 * Mapping file, get fp for validation. Obtain vnode and make
1950d94caffSDavid Greenman 		 * sure it is of appropriate type.
196df8bae1dSRodney W. Grimes 		 */
197df8bae1dSRodney W. Grimes 		if (((unsigned) uap->fd) >= fdp->fd_nfiles ||
198df8bae1dSRodney W. Grimes 		    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
199df8bae1dSRodney W. Grimes 			return (EBADF);
200df8bae1dSRodney W. Grimes 		if (fp->f_type != DTYPE_VNODE)
201df8bae1dSRodney W. Grimes 			return (EINVAL);
202df8bae1dSRodney W. Grimes 		vp = (struct vnode *) fp->f_data;
203df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG && vp->v_type != VCHR)
204df8bae1dSRodney W. Grimes 			return (EINVAL);
205df8bae1dSRodney W. Grimes 		/*
2060d94caffSDavid Greenman 		 * XXX hack to handle use of /dev/zero to map anon memory (ala
2070d94caffSDavid Greenman 		 * SunOS).
208df8bae1dSRodney W. Grimes 		 */
209df8bae1dSRodney W. Grimes 		if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
210df8bae1dSRodney W. Grimes 			handle = NULL;
211df8bae1dSRodney W. Grimes 			maxprot = VM_PROT_ALL;
212df8bae1dSRodney W. Grimes 			flags |= MAP_ANON;
213df8bae1dSRodney W. Grimes 		} else {
214df8bae1dSRodney W. Grimes 			/*
215df8bae1dSRodney W. Grimes 			 * Ensure that file and memory protections are
216df8bae1dSRodney W. Grimes 			 * compatible.  Note that we only worry about
217df8bae1dSRodney W. Grimes 			 * writability if mapping is shared; in this case,
218df8bae1dSRodney W. Grimes 			 * current and max prot are dictated by the open file.
219df8bae1dSRodney W. Grimes 			 * XXX use the vnode instead?  Problem is: what
2200d94caffSDavid Greenman 			 * credentials do we use for determination? What if
2210d94caffSDavid Greenman 			 * proc does a setuid?
222df8bae1dSRodney W. Grimes 			 */
223df8bae1dSRodney W. Grimes 			maxprot = VM_PROT_EXECUTE;	/* ??? */
224df8bae1dSRodney W. Grimes 			if (fp->f_flag & FREAD)
225df8bae1dSRodney W. Grimes 				maxprot |= VM_PROT_READ;
226df8bae1dSRodney W. Grimes 			else if (prot & PROT_READ)
227df8bae1dSRodney W. Grimes 				return (EACCES);
228df8bae1dSRodney W. Grimes 			if (flags & MAP_SHARED) {
229df8bae1dSRodney W. Grimes 				if (fp->f_flag & FWRITE)
230df8bae1dSRodney W. Grimes 					maxprot |= VM_PROT_WRITE;
231df8bae1dSRodney W. Grimes 				else if (prot & PROT_WRITE)
232df8bae1dSRodney W. Grimes 					return (EACCES);
233df8bae1dSRodney W. Grimes 			} else
234df8bae1dSRodney W. Grimes 				maxprot |= VM_PROT_WRITE;
235df8bae1dSRodney W. Grimes 			handle = (caddr_t) vp;
236df8bae1dSRodney W. Grimes 		}
237df8bae1dSRodney W. Grimes 	}
238df8bae1dSRodney W. Grimes 	error = vm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
239df8bae1dSRodney W. Grimes 	    flags, handle, (vm_offset_t) uap->pos);
240df8bae1dSRodney W. Grimes 	if (error == 0)
241df8bae1dSRodney W. Grimes 		*retval = (int) addr;
242df8bae1dSRodney W. Grimes 	return (error);
243df8bae1dSRodney W. Grimes }
244df8bae1dSRodney W. Grimes 
24505f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43
246d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
24705f0fdd2SPoul-Henning Kamp struct ommap_args {
24805f0fdd2SPoul-Henning Kamp 	caddr_t addr;
24905f0fdd2SPoul-Henning Kamp 	int len;
25005f0fdd2SPoul-Henning Kamp 	int prot;
25105f0fdd2SPoul-Henning Kamp 	int flags;
25205f0fdd2SPoul-Henning Kamp 	int fd;
25305f0fdd2SPoul-Henning Kamp 	long pos;
25405f0fdd2SPoul-Henning Kamp };
255d2d3e875SBruce Evans #endif
25605f0fdd2SPoul-Henning Kamp int
25705f0fdd2SPoul-Henning Kamp ommap(p, uap, retval)
25805f0fdd2SPoul-Henning Kamp 	struct proc *p;
25905f0fdd2SPoul-Henning Kamp 	register struct ommap_args *uap;
26005f0fdd2SPoul-Henning Kamp 	int *retval;
26105f0fdd2SPoul-Henning Kamp {
26205f0fdd2SPoul-Henning Kamp 	struct mmap_args nargs;
26305f0fdd2SPoul-Henning Kamp 	static const char cvtbsdprot[8] = {
26405f0fdd2SPoul-Henning Kamp 		0,
26505f0fdd2SPoul-Henning Kamp 		PROT_EXEC,
26605f0fdd2SPoul-Henning Kamp 		PROT_WRITE,
26705f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE,
26805f0fdd2SPoul-Henning Kamp 		PROT_READ,
26905f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_READ,
27005f0fdd2SPoul-Henning Kamp 		PROT_WRITE | PROT_READ,
27105f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE | PROT_READ,
27205f0fdd2SPoul-Henning Kamp 	};
2730d94caffSDavid Greenman 
27405f0fdd2SPoul-Henning Kamp #define	OMAP_ANON	0x0002
27505f0fdd2SPoul-Henning Kamp #define	OMAP_COPY	0x0020
27605f0fdd2SPoul-Henning Kamp #define	OMAP_SHARED	0x0010
27705f0fdd2SPoul-Henning Kamp #define	OMAP_FIXED	0x0100
27805f0fdd2SPoul-Henning Kamp #define	OMAP_INHERIT	0x0800
27905f0fdd2SPoul-Henning Kamp 
28005f0fdd2SPoul-Henning Kamp 	nargs.addr = uap->addr;
28105f0fdd2SPoul-Henning Kamp 	nargs.len = uap->len;
28205f0fdd2SPoul-Henning Kamp 	nargs.prot = cvtbsdprot[uap->prot & 0x7];
28305f0fdd2SPoul-Henning Kamp 	nargs.flags = 0;
28405f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_ANON)
28505f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_ANON;
28605f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_COPY)
28705f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_COPY;
28805f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_SHARED)
28905f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_SHARED;
29005f0fdd2SPoul-Henning Kamp 	else
29105f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_PRIVATE;
29205f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_FIXED)
29305f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_FIXED;
29405f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_INHERIT)
29505f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_INHERIT;
29605f0fdd2SPoul-Henning Kamp 	nargs.fd = uap->fd;
29705f0fdd2SPoul-Henning Kamp 	nargs.pos = uap->pos;
29805f0fdd2SPoul-Henning Kamp 	return (mmap(p, &nargs, retval));
29905f0fdd2SPoul-Henning Kamp }
30005f0fdd2SPoul-Henning Kamp #endif				/* COMPAT_43 */
30105f0fdd2SPoul-Henning Kamp 
30205f0fdd2SPoul-Henning Kamp 
303d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
304df8bae1dSRodney W. Grimes struct msync_args {
305df8bae1dSRodney W. Grimes 	caddr_t addr;
306df8bae1dSRodney W. Grimes 	int len;
307e6c6af11SDavid Greenman 	int flags;
308df8bae1dSRodney W. Grimes };
309d2d3e875SBruce Evans #endif
310df8bae1dSRodney W. Grimes int
311df8bae1dSRodney W. Grimes msync(p, uap, retval)
312df8bae1dSRodney W. Grimes 	struct proc *p;
313df8bae1dSRodney W. Grimes 	struct msync_args *uap;
314df8bae1dSRodney W. Grimes 	int *retval;
315df8bae1dSRodney W. Grimes {
316df8bae1dSRodney W. Grimes 	vm_offset_t addr;
317df8bae1dSRodney W. Grimes 	vm_size_t size;
318e6c6af11SDavid Greenman 	int flags;
319df8bae1dSRodney W. Grimes 	vm_map_t map;
320df8bae1dSRodney W. Grimes 	int rv;
321df8bae1dSRodney W. Grimes 
322df8bae1dSRodney W. Grimes 	map = &p->p_vmspace->vm_map;
323df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
324df8bae1dSRodney W. Grimes 	size = (vm_size_t) uap->len;
325e6c6af11SDavid Greenman 	flags = uap->flags;
326e6c6af11SDavid Greenman 
3271e62bc63SDavid Greenman 	if (((int) addr & PAGE_MASK) || addr + size < addr ||
3281e62bc63SDavid Greenman 	    (flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
3291e62bc63SDavid Greenman 		return (EINVAL);
3301e62bc63SDavid Greenman 
331df8bae1dSRodney W. Grimes 	/*
332df8bae1dSRodney W. Grimes 	 * XXX Gak!  If size is zero we are supposed to sync "all modified
3330d94caffSDavid Greenman 	 * pages with the region containing addr".  Unfortunately, we don't
3340d94caffSDavid Greenman 	 * really keep track of individual mmaps so we approximate by flushing
3350d94caffSDavid Greenman 	 * the range of the map entry containing addr. This can be incorrect
3360d94caffSDavid Greenman 	 * if the region splits or is coalesced with a neighbor.
337df8bae1dSRodney W. Grimes 	 */
338df8bae1dSRodney W. Grimes 	if (size == 0) {
339df8bae1dSRodney W. Grimes 		vm_map_entry_t entry;
340df8bae1dSRodney W. Grimes 
341df8bae1dSRodney W. Grimes 		vm_map_lock_read(map);
342df8bae1dSRodney W. Grimes 		rv = vm_map_lookup_entry(map, addr, &entry);
343df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
344fbcfcdf7SDavid Greenman 		if (rv == FALSE)
345df8bae1dSRodney W. Grimes 			return (EINVAL);
346df8bae1dSRodney W. Grimes 		addr = entry->start;
347df8bae1dSRodney W. Grimes 		size = entry->end - entry->start;
348df8bae1dSRodney W. Grimes 	}
349e6c6af11SDavid Greenman 
350df8bae1dSRodney W. Grimes 	/*
351df8bae1dSRodney W. Grimes 	 * Clean the pages and interpret the return value.
352df8bae1dSRodney W. Grimes 	 */
3536c534ad8SDavid Greenman 	rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
354e6c6af11SDavid Greenman 	    (flags & MS_INVALIDATE) != 0);
355e6c6af11SDavid Greenman 
356df8bae1dSRodney W. Grimes 	switch (rv) {
357df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
358df8bae1dSRodney W. Grimes 		break;
359df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
360df8bae1dSRodney W. Grimes 		return (EINVAL);	/* Sun returns ENOMEM? */
361df8bae1dSRodney W. Grimes 	case KERN_FAILURE:
362df8bae1dSRodney W. Grimes 		return (EIO);
363df8bae1dSRodney W. Grimes 	default:
364df8bae1dSRodney W. Grimes 		return (EINVAL);
365df8bae1dSRodney W. Grimes 	}
366e6c6af11SDavid Greenman 
367df8bae1dSRodney W. Grimes 	return (0);
368df8bae1dSRodney W. Grimes }
369df8bae1dSRodney W. Grimes 
370d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
371df8bae1dSRodney W. Grimes struct munmap_args {
372df8bae1dSRodney W. Grimes 	caddr_t addr;
373df8bae1dSRodney W. Grimes 	int len;
374df8bae1dSRodney W. Grimes };
375d2d3e875SBruce Evans #endif
376df8bae1dSRodney W. Grimes int
377df8bae1dSRodney W. Grimes munmap(p, uap, retval)
378df8bae1dSRodney W. Grimes 	register struct proc *p;
379df8bae1dSRodney W. Grimes 	register struct munmap_args *uap;
380df8bae1dSRodney W. Grimes 	int *retval;
381df8bae1dSRodney W. Grimes {
382df8bae1dSRodney W. Grimes 	vm_offset_t addr;
383df8bae1dSRodney W. Grimes 	vm_size_t size;
384df8bae1dSRodney W. Grimes 	vm_map_t map;
385df8bae1dSRodney W. Grimes 
386df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
387df8bae1dSRodney W. Grimes 	if ((addr & PAGE_MASK) || uap->len < 0)
388df8bae1dSRodney W. Grimes 		return (EINVAL);
389df8bae1dSRodney W. Grimes 	size = (vm_size_t) round_page(uap->len);
390df8bae1dSRodney W. Grimes 	if (size == 0)
391df8bae1dSRodney W. Grimes 		return (0);
392df8bae1dSRodney W. Grimes 	/*
3930d94caffSDavid Greenman 	 * Check for illegal addresses.  Watch out for address wrap... Note
3940d94caffSDavid Greenman 	 * that VM_*_ADDRESS are not constants due to casts (argh).
395df8bae1dSRodney W. Grimes 	 */
396bbc0ec52SDavid Greenman 	if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
397df8bae1dSRodney W. Grimes 		return (EINVAL);
39826f9a767SRodney W. Grimes #ifndef i386
399df8bae1dSRodney W. Grimes 	if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
400df8bae1dSRodney W. Grimes 		return (EINVAL);
40126f9a767SRodney W. Grimes #endif
402bbc0ec52SDavid Greenman 	if (addr + size < addr)
403df8bae1dSRodney W. Grimes 		return (EINVAL);
404df8bae1dSRodney W. Grimes 	map = &p->p_vmspace->vm_map;
405df8bae1dSRodney W. Grimes 	/*
406df8bae1dSRodney W. Grimes 	 * Make sure entire range is allocated.
407df8bae1dSRodney W. Grimes 	 */
408df8bae1dSRodney W. Grimes 	if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
409df8bae1dSRodney W. Grimes 		return (EINVAL);
410df8bae1dSRodney W. Grimes 	/* returns nothing but KERN_SUCCESS anyway */
411df8bae1dSRodney W. Grimes 	(void) vm_map_remove(map, addr, addr + size);
412df8bae1dSRodney W. Grimes 	return (0);
413df8bae1dSRodney W. Grimes }
414df8bae1dSRodney W. Grimes 
415df8bae1dSRodney W. Grimes void
41690324b07SDavid Greenman munmapfd(p, fd)
41790324b07SDavid Greenman 	struct proc *p;
418df8bae1dSRodney W. Grimes 	int fd;
419df8bae1dSRodney W. Grimes {
420df8bae1dSRodney W. Grimes 	/*
421c4ed5a07SDavid Greenman 	 * XXX should unmap any regions mapped to this file
422df8bae1dSRodney W. Grimes 	 */
42390324b07SDavid Greenman 	p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
424df8bae1dSRodney W. Grimes }
425df8bae1dSRodney W. Grimes 
426d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
427df8bae1dSRodney W. Grimes struct mprotect_args {
428df8bae1dSRodney W. Grimes 	caddr_t addr;
429df8bae1dSRodney W. Grimes 	int len;
430df8bae1dSRodney W. Grimes 	int prot;
431df8bae1dSRodney W. Grimes };
432d2d3e875SBruce Evans #endif
433df8bae1dSRodney W. Grimes int
434df8bae1dSRodney W. Grimes mprotect(p, uap, retval)
435df8bae1dSRodney W. Grimes 	struct proc *p;
436df8bae1dSRodney W. Grimes 	struct mprotect_args *uap;
437df8bae1dSRodney W. Grimes 	int *retval;
438df8bae1dSRodney W. Grimes {
439df8bae1dSRodney W. Grimes 	vm_offset_t addr;
440df8bae1dSRodney W. Grimes 	vm_size_t size;
441df8bae1dSRodney W. Grimes 	register vm_prot_t prot;
442df8bae1dSRodney W. Grimes 
443df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
444df8bae1dSRodney W. Grimes 	if ((addr & PAGE_MASK) || uap->len < 0)
445df8bae1dSRodney W. Grimes 		return (EINVAL);
446df8bae1dSRodney W. Grimes 	size = (vm_size_t) uap->len;
447df8bae1dSRodney W. Grimes 	prot = uap->prot & VM_PROT_ALL;
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes 	switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
450df8bae1dSRodney W. Grimes 		FALSE)) {
451df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
452df8bae1dSRodney W. Grimes 		return (0);
453df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
454df8bae1dSRodney W. Grimes 		return (EACCES);
455df8bae1dSRodney W. Grimes 	}
456df8bae1dSRodney W. Grimes 	return (EINVAL);
457df8bae1dSRodney W. Grimes }
458df8bae1dSRodney W. Grimes 
459d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
460df8bae1dSRodney W. Grimes struct madvise_args {
461df8bae1dSRodney W. Grimes 	caddr_t addr;
462df8bae1dSRodney W. Grimes 	int len;
463df8bae1dSRodney W. Grimes 	int behav;
464df8bae1dSRodney W. Grimes };
465d2d3e875SBruce Evans #endif
4660d94caffSDavid Greenman 
467df8bae1dSRodney W. Grimes /* ARGSUSED */
468df8bae1dSRodney W. Grimes int
469df8bae1dSRodney W. Grimes madvise(p, uap, retval)
470df8bae1dSRodney W. Grimes 	struct proc *p;
471df8bae1dSRodney W. Grimes 	struct madvise_args *uap;
472df8bae1dSRodney W. Grimes 	int *retval;
473df8bae1dSRodney W. Grimes {
474df8bae1dSRodney W. Grimes 
475df8bae1dSRodney W. Grimes 	/* Not yet implemented */
476df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
477df8bae1dSRodney W. Grimes }
478df8bae1dSRodney W. Grimes 
479d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
480df8bae1dSRodney W. Grimes struct mincore_args {
481df8bae1dSRodney W. Grimes 	caddr_t addr;
482df8bae1dSRodney W. Grimes 	int len;
483df8bae1dSRodney W. Grimes 	char *vec;
484df8bae1dSRodney W. Grimes };
485d2d3e875SBruce Evans #endif
4860d94caffSDavid Greenman 
487df8bae1dSRodney W. Grimes /* ARGSUSED */
488df8bae1dSRodney W. Grimes int
489df8bae1dSRodney W. Grimes mincore(p, uap, retval)
490df8bae1dSRodney W. Grimes 	struct proc *p;
491df8bae1dSRodney W. Grimes 	struct mincore_args *uap;
492df8bae1dSRodney W. Grimes 	int *retval;
493df8bae1dSRodney W. Grimes {
49402c04a2fSJohn Dyson 	vm_offset_t addr;
49502c04a2fSJohn Dyson 	vm_offset_t end;
49602c04a2fSJohn Dyson 	char *vec;
497df8bae1dSRodney W. Grimes 
49802c04a2fSJohn Dyson 	addr = trunc_page((vm_offset_t) uap->addr);
49902c04a2fSJohn Dyson 	end = addr + round_page((vm_size_t) uap->len);
50002c04a2fSJohn Dyson 	if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS)
50102c04a2fSJohn Dyson 		return (EINVAL);
50202c04a2fSJohn Dyson 	if (end < addr)
50302c04a2fSJohn Dyson 		return (EINVAL);
50402c04a2fSJohn Dyson 
50502c04a2fSJohn Dyson 	vec = uap->vec;
50602c04a2fSJohn Dyson 	while(addr < end) {
50702c04a2fSJohn Dyson 		int error;
50802c04a2fSJohn Dyson 		if (pmap_extract(&p->p_vmspace->vm_pmap, addr)) {
50902c04a2fSJohn Dyson 			error = subyte( vec, 1);
51002c04a2fSJohn Dyson 		} else {
51102c04a2fSJohn Dyson 			error = subyte( vec, 0);
51202c04a2fSJohn Dyson 		}
51302c04a2fSJohn Dyson 		if (error)
51402c04a2fSJohn Dyson 			return EFAULT;
51502c04a2fSJohn Dyson 		vec++;
51602c04a2fSJohn Dyson 		addr += PAGE_SIZE;
51702c04a2fSJohn Dyson 	}
51802c04a2fSJohn Dyson 	return (0);
519df8bae1dSRodney W. Grimes }
520df8bae1dSRodney W. Grimes 
521d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
522df8bae1dSRodney W. Grimes struct mlock_args {
523df8bae1dSRodney W. Grimes 	caddr_t addr;
524df8bae1dSRodney W. Grimes 	size_t len;
525df8bae1dSRodney W. Grimes };
526d2d3e875SBruce Evans #endif
527df8bae1dSRodney W. Grimes int
528df8bae1dSRodney W. Grimes mlock(p, uap, retval)
529df8bae1dSRodney W. Grimes 	struct proc *p;
530df8bae1dSRodney W. Grimes 	struct mlock_args *uap;
531df8bae1dSRodney W. Grimes 	int *retval;
532df8bae1dSRodney W. Grimes {
533df8bae1dSRodney W. Grimes 	vm_offset_t addr;
534df8bae1dSRodney W. Grimes 	vm_size_t size;
535df8bae1dSRodney W. Grimes 	int error;
536df8bae1dSRodney W. Grimes 
537df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
538df8bae1dSRodney W. Grimes 	if ((addr & PAGE_MASK) || uap->addr + uap->len < uap->addr)
539df8bae1dSRodney W. Grimes 		return (EINVAL);
540df8bae1dSRodney W. Grimes 	size = round_page((vm_size_t) uap->len);
541df8bae1dSRodney W. Grimes 	if (atop(size) + cnt.v_wire_count > vm_page_max_wired)
542df8bae1dSRodney W. Grimes 		return (EAGAIN);
543df8bae1dSRodney W. Grimes #ifdef pmap_wired_count
544df8bae1dSRodney W. Grimes 	if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
545df8bae1dSRodney W. Grimes 	    p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
546df8bae1dSRodney W. Grimes 		return (EAGAIN);
547df8bae1dSRodney W. Grimes #else
54805f0fdd2SPoul-Henning Kamp 	error = suser(p->p_ucred, &p->p_acflag);
54905f0fdd2SPoul-Henning Kamp 	if (error)
550df8bae1dSRodney W. Grimes 		return (error);
551df8bae1dSRodney W. Grimes #endif
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes 	error = vm_map_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
554df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
555df8bae1dSRodney W. Grimes }
556df8bae1dSRodney W. Grimes 
557d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
558df8bae1dSRodney W. Grimes struct munlock_args {
559df8bae1dSRodney W. Grimes 	caddr_t addr;
560df8bae1dSRodney W. Grimes 	size_t len;
561df8bae1dSRodney W. Grimes };
562d2d3e875SBruce Evans #endif
563df8bae1dSRodney W. Grimes int
564df8bae1dSRodney W. Grimes munlock(p, uap, retval)
565df8bae1dSRodney W. Grimes 	struct proc *p;
566df8bae1dSRodney W. Grimes 	struct munlock_args *uap;
567df8bae1dSRodney W. Grimes 	int *retval;
568df8bae1dSRodney W. Grimes {
569df8bae1dSRodney W. Grimes 	vm_offset_t addr;
570df8bae1dSRodney W. Grimes 	vm_size_t size;
571df8bae1dSRodney W. Grimes 	int error;
572df8bae1dSRodney W. Grimes 
573df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
574df8bae1dSRodney W. Grimes 	if ((addr & PAGE_MASK) || uap->addr + uap->len < uap->addr)
575df8bae1dSRodney W. Grimes 		return (EINVAL);
576df8bae1dSRodney W. Grimes #ifndef pmap_wired_count
57705f0fdd2SPoul-Henning Kamp 	error = suser(p->p_ucred, &p->p_acflag);
57805f0fdd2SPoul-Henning Kamp 	if (error)
579df8bae1dSRodney W. Grimes 		return (error);
580df8bae1dSRodney W. Grimes #endif
581df8bae1dSRodney W. Grimes 	size = round_page((vm_size_t) uap->len);
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes 	error = vm_map_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
584df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
585df8bae1dSRodney W. Grimes }
586df8bae1dSRodney W. Grimes 
587df8bae1dSRodney W. Grimes /*
588df8bae1dSRodney W. Grimes  * Internal version of mmap.
589df8bae1dSRodney W. Grimes  * Currently used by mmap, exec, and sys5 shared memory.
590df8bae1dSRodney W. Grimes  * Handle is either a vnode pointer or NULL for MAP_ANON.
591df8bae1dSRodney W. Grimes  */
592df8bae1dSRodney W. Grimes int
593df8bae1dSRodney W. Grimes vm_mmap(map, addr, size, prot, maxprot, flags, handle, foff)
594df8bae1dSRodney W. Grimes 	register vm_map_t map;
595df8bae1dSRodney W. Grimes 	register vm_offset_t *addr;
596df8bae1dSRodney W. Grimes 	register vm_size_t size;
597df8bae1dSRodney W. Grimes 	vm_prot_t prot, maxprot;
598df8bae1dSRodney W. Grimes 	register int flags;
599df8bae1dSRodney W. Grimes 	caddr_t handle;		/* XXX should be vp */
600df8bae1dSRodney W. Grimes 	vm_offset_t foff;
601df8bae1dSRodney W. Grimes {
602df8bae1dSRodney W. Grimes 	boolean_t fitit;
603df8bae1dSRodney W. Grimes 	vm_object_t object;
604df8bae1dSRodney W. Grimes 	struct vnode *vp = NULL;
60524a1cce3SDavid Greenman 	objtype_t type;
606df8bae1dSRodney W. Grimes 	int rv = KERN_SUCCESS;
60706cb7259SDavid Greenman 	vm_size_t objsize;
60806cb7259SDavid Greenman 	struct proc *p = curproc;
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes 	if (size == 0)
611df8bae1dSRodney W. Grimes 		return (0);
612df8bae1dSRodney W. Grimes 
61306cb7259SDavid Greenman 	objsize = size = round_page(size);
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes 	/*
616bc9ad247SDavid Greenman 	 * We currently can only deal with page aligned file offsets.
617bc9ad247SDavid Greenman 	 * The check is here rather than in the syscall because the
618bc9ad247SDavid Greenman 	 * kernel calls this function internally for other mmaping
619bc9ad247SDavid Greenman 	 * operations (such as in exec) and non-aligned offsets will
620bc9ad247SDavid Greenman 	 * cause pmap inconsistencies...so we want to be sure to
621bc9ad247SDavid Greenman 	 * disallow this in all cases.
622bc9ad247SDavid Greenman 	 */
623bc9ad247SDavid Greenman 	if (foff & PAGE_MASK)
624bc9ad247SDavid Greenman 		return (EINVAL);
625bc9ad247SDavid Greenman 
62606cb7259SDavid Greenman 	if ((flags & MAP_FIXED) == 0) {
62706cb7259SDavid Greenman 		fitit = TRUE;
62806cb7259SDavid Greenman 		*addr = round_page(*addr);
62906cb7259SDavid Greenman 	} else {
63006cb7259SDavid Greenman 		if (*addr != trunc_page(*addr))
63106cb7259SDavid Greenman 			return (EINVAL);
63206cb7259SDavid Greenman 		fitit = FALSE;
63306cb7259SDavid Greenman 		(void) vm_map_remove(map, *addr, *addr + size);
63406cb7259SDavid Greenman 	}
63506cb7259SDavid Greenman 
636bc9ad247SDavid Greenman 	/*
63724a1cce3SDavid Greenman 	 * Lookup/allocate object.
638df8bae1dSRodney W. Grimes 	 */
6395f55e841SDavid Greenman 	if (flags & MAP_ANON) {
64024a1cce3SDavid Greenman 		type = OBJT_SWAP;
6415f55e841SDavid Greenman 		/*
6425f55e841SDavid Greenman 		 * Unnamed anonymous regions always start at 0.
6435f55e841SDavid Greenman 		 */
6445f55e841SDavid Greenman 		if (handle == 0)
6455f55e841SDavid Greenman 			foff = 0;
6465f55e841SDavid Greenman 	} else {
647df8bae1dSRodney W. Grimes 		vp = (struct vnode *) handle;
648df8bae1dSRodney W. Grimes 		if (vp->v_type == VCHR) {
64924a1cce3SDavid Greenman 			type = OBJT_DEVICE;
650df8bae1dSRodney W. Grimes 			handle = (caddr_t) vp->v_rdev;
65106cb7259SDavid Greenman 		} else {
65206cb7259SDavid Greenman 			struct vattr vat;
65306cb7259SDavid Greenman 			int error;
65406cb7259SDavid Greenman 
65506cb7259SDavid Greenman 			error = VOP_GETATTR(vp, &vat, p->p_ucred, p);
65606cb7259SDavid Greenman 			if (error)
65706cb7259SDavid Greenman 				return (error);
65806cb7259SDavid Greenman 			objsize = vat.va_size;
65924a1cce3SDavid Greenman 			type = OBJT_VNODE;
660df8bae1dSRodney W. Grimes 		}
66106cb7259SDavid Greenman 	}
66224a1cce3SDavid Greenman 	object = vm_pager_allocate(type, handle, objsize, prot, foff);
66324a1cce3SDavid Greenman 	if (object == NULL)
66424a1cce3SDavid Greenman 		return (type == OBJT_DEVICE ? EINVAL : ENOMEM);
665df8bae1dSRodney W. Grimes 
6667fb0c17eSDavid Greenman 	rv = vm_map_find(map, object, foff, addr, size, fitit);
667df8bae1dSRodney W. Grimes 	if (rv != KERN_SUCCESS) {
6687fb0c17eSDavid Greenman 		/*
66924a1cce3SDavid Greenman 		 * Lose the object reference. Will destroy the
67024a1cce3SDavid Greenman 		 * object if it's an unnamed anonymous mapping
67124a1cce3SDavid Greenman 		 * or named anonymous without other references.
6727fb0c17eSDavid Greenman 		 */
673df8bae1dSRodney W. Grimes 		vm_object_deallocate(object);
674df8bae1dSRodney W. Grimes 		goto out;
675df8bae1dSRodney W. Grimes 	}
676e17bed12SJohn Dyson 
677df8bae1dSRodney W. Grimes 	/*
67850ce2102SDavid Greenman 	 * mmap a COW regular file
679df8bae1dSRodney W. Grimes 	 */
680e17bed12SJohn Dyson 	if ((flags & (MAP_ANON|MAP_SHARED)) == 0 && (type != OBJT_DEVICE)) {
68150ce2102SDavid Greenman 		vm_map_entry_t entry;
68250ce2102SDavid Greenman 		if (!vm_map_lookup_entry(map, *addr, &entry)) {
683edf8a815SDavid Greenman 			panic("vm_mmap: missing map entry!!!");
68450ce2102SDavid Greenman 		}
68550ce2102SDavid Greenman 		entry->copy_on_write = TRUE;
686e17bed12SJohn Dyson 		/*
687e17bed12SJohn Dyson 		 * This will create the processes private object on
688e17bed12SJohn Dyson 		 * an as needed basis.
689e17bed12SJohn Dyson 		 */
690e17bed12SJohn Dyson 		entry->needs_copy = TRUE;
69150ce2102SDavid Greenman 
69250ce2102SDavid Greenman 		/*
69350ce2102SDavid Greenman 		 * set pages COW and protect for read access only
69450ce2102SDavid Greenman 		 */
69550ce2102SDavid Greenman 		vm_object_pmap_copy(object, foff, foff + size);
69650ce2102SDavid Greenman 
697df8bae1dSRodney W. Grimes 	}
6987fb0c17eSDavid Greenman 
6997fb0c17eSDavid Greenman 	/*
7007fb0c17eSDavid Greenman 	 * "Pre-fault" resident pages.
7017fb0c17eSDavid Greenman 	 */
70224a1cce3SDavid Greenman 	if ((type == OBJT_VNODE) && (map->pmap != NULL)) {
7037fb0c17eSDavid Greenman 		pmap_object_init_pt(map->pmap, *addr, object, foff, size);
704df8bae1dSRodney W. Grimes 	}
7057fb0c17eSDavid Greenman 
706df8bae1dSRodney W. Grimes 	/*
7070d94caffSDavid Greenman 	 * Correct protection (default is VM_PROT_ALL). If maxprot is
7080d94caffSDavid Greenman 	 * different than prot, we must set both explicitly.
709df8bae1dSRodney W. Grimes 	 */
710df8bae1dSRodney W. Grimes 	rv = KERN_SUCCESS;
711df8bae1dSRodney W. Grimes 	if (maxprot != VM_PROT_ALL)
712df8bae1dSRodney W. Grimes 		rv = vm_map_protect(map, *addr, *addr + size, maxprot, TRUE);
713df8bae1dSRodney W. Grimes 	if (rv == KERN_SUCCESS && prot != maxprot)
714df8bae1dSRodney W. Grimes 		rv = vm_map_protect(map, *addr, *addr + size, prot, FALSE);
715df8bae1dSRodney W. Grimes 	if (rv != KERN_SUCCESS) {
7167fb0c17eSDavid Greenman 		(void) vm_map_remove(map, *addr, *addr + size);
717df8bae1dSRodney W. Grimes 		goto out;
718df8bae1dSRodney W. Grimes 	}
719df8bae1dSRodney W. Grimes 	/*
720df8bae1dSRodney W. Grimes 	 * Shared memory is also shared with children.
721df8bae1dSRodney W. Grimes 	 */
722df8bae1dSRodney W. Grimes 	if (flags & MAP_SHARED) {
723df8bae1dSRodney W. Grimes 		rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
724df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
7257fb0c17eSDavid Greenman 			(void) vm_map_remove(map, *addr, *addr + size);
726df8bae1dSRodney W. Grimes 			goto out;
727df8bae1dSRodney W. Grimes 		}
728df8bae1dSRodney W. Grimes 	}
729df8bae1dSRodney W. Grimes out:
730df8bae1dSRodney W. Grimes 	switch (rv) {
731df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
732df8bae1dSRodney W. Grimes 		return (0);
733df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
734df8bae1dSRodney W. Grimes 	case KERN_NO_SPACE:
735df8bae1dSRodney W. Grimes 		return (ENOMEM);
736df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
737df8bae1dSRodney W. Grimes 		return (EACCES);
738df8bae1dSRodney W. Grimes 	default:
739df8bae1dSRodney W. Grimes 		return (EINVAL);
740df8bae1dSRodney W. Grimes 	}
741df8bae1dSRodney W. Grimes }
742