xref: /freebsd/sys/vm/vm_mmap.c (revision 94328e9057c361defa678da7c83e0f72968a9dc0)
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
4194328e90SJohn Dyson  * $Id: vm_mmap.c,v 1.55 1996/12/22 23:17:09 joerg 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 
48e9822d92SJoerg Wunsch #include "opt_rlimit.h"
49e9822d92SJoerg Wunsch 
50df8bae1dSRodney W. Grimes #include <sys/param.h>
51df8bae1dSRodney W. Grimes #include <sys/systm.h>
52d2d3e875SBruce Evans #include <sys/sysproto.h>
53df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
54df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
55df8bae1dSRodney W. Grimes #include <sys/proc.h>
56df8bae1dSRodney W. Grimes #include <sys/vnode.h>
57df8bae1dSRodney W. Grimes #include <sys/file.h>
58df8bae1dSRodney W. Grimes #include <sys/mman.h>
59df8bae1dSRodney W. Grimes #include <sys/conf.h>
60efeaf95aSDavid Greenman #include <sys/vmmeter.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <miscfs/specfs/specdev.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <vm/vm.h>
65efeaf95aSDavid Greenman #include <vm/vm_param.h>
66efeaf95aSDavid Greenman #include <vm/vm_prot.h>
67efeaf95aSDavid Greenman #include <vm/vm_inherit.h>
68efeaf95aSDavid Greenman #include <vm/lock.h>
69efeaf95aSDavid Greenman #include <vm/pmap.h>
70efeaf95aSDavid Greenman #include <vm/vm_map.h>
71efeaf95aSDavid Greenman #include <vm/vm_object.h>
72df8bae1dSRodney W. Grimes #include <vm/vm_pager.h>
73b5e8ce9fSBruce Evans #include <vm/vm_pageout.h>
74efeaf95aSDavid Greenman #include <vm/vm_extern.h>
75bd7e5f99SJohn Dyson #include <vm/vm_kern.h>
76867a482dSJohn Dyson #include <vm/vm_page.h>
77df8bae1dSRodney W. Grimes 
78d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
79df8bae1dSRodney W. Grimes struct sbrk_args {
80df8bae1dSRodney W. Grimes 	int incr;
81df8bae1dSRodney W. Grimes };
82d2d3e875SBruce Evans #endif
830d94caffSDavid Greenman 
84df8bae1dSRodney W. Grimes /* ARGSUSED */
85df8bae1dSRodney W. Grimes int
86df8bae1dSRodney W. Grimes sbrk(p, uap, retval)
87df8bae1dSRodney W. Grimes 	struct proc *p;
88df8bae1dSRodney W. Grimes 	struct sbrk_args *uap;
89df8bae1dSRodney W. Grimes 	int *retval;
90df8bae1dSRodney W. Grimes {
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes 	/* Not yet implemented */
93df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
94df8bae1dSRodney W. Grimes }
95df8bae1dSRodney W. Grimes 
96d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
97df8bae1dSRodney W. Grimes struct sstk_args {
98df8bae1dSRodney W. Grimes 	int incr;
99df8bae1dSRodney W. Grimes };
100d2d3e875SBruce Evans #endif
1010d94caffSDavid Greenman 
102df8bae1dSRodney W. Grimes /* ARGSUSED */
103df8bae1dSRodney W. Grimes int
104df8bae1dSRodney W. Grimes sstk(p, uap, retval)
105df8bae1dSRodney W. Grimes 	struct proc *p;
106df8bae1dSRodney W. Grimes 	struct sstk_args *uap;
107df8bae1dSRodney W. Grimes 	int *retval;
108df8bae1dSRodney W. Grimes {
109df8bae1dSRodney W. Grimes 
110df8bae1dSRodney W. Grimes 	/* Not yet implemented */
111df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
112df8bae1dSRodney W. Grimes }
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
115d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
116df8bae1dSRodney W. Grimes struct getpagesize_args {
117df8bae1dSRodney W. Grimes 	int dummy;
118df8bae1dSRodney W. Grimes };
119d2d3e875SBruce Evans #endif
1200d94caffSDavid Greenman 
121df8bae1dSRodney W. Grimes /* ARGSUSED */
122df8bae1dSRodney W. Grimes int
123df8bae1dSRodney W. Grimes ogetpagesize(p, uap, retval)
124df8bae1dSRodney W. Grimes 	struct proc *p;
125df8bae1dSRodney W. Grimes 	struct getpagesize_args *uap;
126df8bae1dSRodney W. Grimes 	int *retval;
127df8bae1dSRodney W. Grimes {
128df8bae1dSRodney W. Grimes 
129df8bae1dSRodney W. Grimes 	*retval = PAGE_SIZE;
130df8bae1dSRodney W. Grimes 	return (0);
131df8bae1dSRodney W. Grimes }
132df8bae1dSRodney W. Grimes #endif				/* COMPAT_43 || COMPAT_SUNOS */
133df8bae1dSRodney W. Grimes 
134d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
135df8bae1dSRodney W. Grimes struct mmap_args {
136df8bae1dSRodney W. Grimes 	caddr_t addr;
137df8bae1dSRodney W. Grimes 	size_t len;
138df8bae1dSRodney W. Grimes 	int prot;
139df8bae1dSRodney W. Grimes 	int flags;
140df8bae1dSRodney W. Grimes 	int fd;
141df8bae1dSRodney W. Grimes 	long pad;
142df8bae1dSRodney W. Grimes 	off_t pos;
143df8bae1dSRodney W. Grimes };
144d2d3e875SBruce Evans #endif
145df8bae1dSRodney W. Grimes 
146df8bae1dSRodney W. Grimes int
147df8bae1dSRodney W. Grimes mmap(p, uap, retval)
148df8bae1dSRodney W. Grimes 	struct proc *p;
149df8bae1dSRodney W. Grimes 	register struct mmap_args *uap;
150df8bae1dSRodney W. Grimes 	int *retval;
151df8bae1dSRodney W. Grimes {
152df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
153df8bae1dSRodney W. Grimes 	register struct file *fp;
154df8bae1dSRodney W. Grimes 	struct vnode *vp;
155df8bae1dSRodney W. Grimes 	vm_offset_t addr;
1569154ee6aSPeter Wemm 	vm_size_t size, pageoff;
157df8bae1dSRodney W. Grimes 	vm_prot_t prot, maxprot;
158df8bae1dSRodney W. Grimes 	caddr_t handle;
159df8bae1dSRodney W. Grimes 	int flags, error;
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes 	prot = uap->prot & VM_PROT_ALL;
162df8bae1dSRodney W. Grimes 	flags = uap->flags;
163df8bae1dSRodney W. Grimes 	/*
1640d94caffSDavid Greenman 	 * Address (if FIXED) must be page aligned. Size is implicitly rounded
1650d94caffSDavid Greenman 	 * to a page boundary.
166df8bae1dSRodney W. Grimes 	 */
167df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
168df8bae1dSRodney W. Grimes 	if (((flags & MAP_FIXED) && (addr & PAGE_MASK)) ||
169df8bae1dSRodney W. Grimes 	    (ssize_t) uap->len < 0 || ((flags & MAP_ANON) && uap->fd != -1))
170df8bae1dSRodney W. Grimes 		return (EINVAL);
1719154ee6aSPeter Wemm 
1729154ee6aSPeter Wemm 	/*
1739154ee6aSPeter Wemm 	 * Round page if not already disallowed by above test
1749154ee6aSPeter Wemm 	 * XXX: Is there any point in the MAP_FIXED align requirement above?
1759154ee6aSPeter Wemm 	 */
1769154ee6aSPeter Wemm 	size = uap->len;
1779154ee6aSPeter Wemm 	pageoff = (addr & PAGE_MASK);
1789154ee6aSPeter Wemm 	addr -= pageoff;
1799154ee6aSPeter Wemm 	size += pageoff;
1809154ee6aSPeter Wemm 	size = (vm_size_t) round_page(size);
1819154ee6aSPeter Wemm 
182df8bae1dSRodney W. Grimes 	/*
1830d94caffSDavid Greenman 	 * Check for illegal addresses.  Watch out for address wrap... Note
1840d94caffSDavid Greenman 	 * that VM_*_ADDRESS are not constants due to casts (argh).
185df8bae1dSRodney W. Grimes 	 */
186df8bae1dSRodney W. Grimes 	if (flags & MAP_FIXED) {
187bbc0ec52SDavid Greenman 		if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
188df8bae1dSRodney W. Grimes 			return (EINVAL);
18926f9a767SRodney W. Grimes #ifndef i386
190df8bae1dSRodney W. Grimes 		if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
191df8bae1dSRodney W. Grimes 			return (EINVAL);
19226f9a767SRodney W. Grimes #endif
193bbc0ec52SDavid Greenman 		if (addr + size < addr)
194df8bae1dSRodney W. Grimes 			return (EINVAL);
195df8bae1dSRodney W. Grimes 	}
196df8bae1dSRodney W. Grimes 	/*
1970d94caffSDavid Greenman 	 * XXX if no hint provided for a non-fixed mapping place it after the
1980d94caffSDavid Greenman 	 * end of the largest possible heap.
199df8bae1dSRodney W. Grimes 	 *
2000d94caffSDavid Greenman 	 * There should really be a pmap call to determine a reasonable location.
201df8bae1dSRodney W. Grimes 	 */
202df8bae1dSRodney W. Grimes 	if (addr == 0 && (flags & MAP_FIXED) == 0)
203df8bae1dSRodney W. Grimes 		addr = round_page(p->p_vmspace->vm_daddr + MAXDSIZ);
204df8bae1dSRodney W. Grimes 	if (flags & MAP_ANON) {
205df8bae1dSRodney W. Grimes 		/*
206df8bae1dSRodney W. Grimes 		 * Mapping blank space is trivial.
207df8bae1dSRodney W. Grimes 		 */
208df8bae1dSRodney W. Grimes 		handle = NULL;
209df8bae1dSRodney W. Grimes 		maxprot = VM_PROT_ALL;
210df8bae1dSRodney W. Grimes 	} else {
211df8bae1dSRodney W. Grimes 		/*
2120d94caffSDavid Greenman 		 * Mapping file, get fp for validation. Obtain vnode and make
2130d94caffSDavid Greenman 		 * sure it is of appropriate type.
214df8bae1dSRodney W. Grimes 		 */
215df8bae1dSRodney W. Grimes 		if (((unsigned) uap->fd) >= fdp->fd_nfiles ||
216df8bae1dSRodney W. Grimes 		    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
217df8bae1dSRodney W. Grimes 			return (EBADF);
218df8bae1dSRodney W. Grimes 		if (fp->f_type != DTYPE_VNODE)
219df8bae1dSRodney W. Grimes 			return (EINVAL);
220df8bae1dSRodney W. Grimes 		vp = (struct vnode *) fp->f_data;
221df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG && vp->v_type != VCHR)
222df8bae1dSRodney W. Grimes 			return (EINVAL);
223df8bae1dSRodney W. Grimes 		/*
2240d94caffSDavid Greenman 		 * XXX hack to handle use of /dev/zero to map anon memory (ala
2250d94caffSDavid Greenman 		 * SunOS).
226df8bae1dSRodney W. Grimes 		 */
227df8bae1dSRodney W. Grimes 		if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
228df8bae1dSRodney W. Grimes 			handle = NULL;
229df8bae1dSRodney W. Grimes 			maxprot = VM_PROT_ALL;
230df8bae1dSRodney W. Grimes 			flags |= MAP_ANON;
231df8bae1dSRodney W. Grimes 		} else {
232df8bae1dSRodney W. Grimes 			/*
233df8bae1dSRodney W. Grimes 			 * Ensure that file and memory protections are
234df8bae1dSRodney W. Grimes 			 * compatible.  Note that we only worry about
235df8bae1dSRodney W. Grimes 			 * writability if mapping is shared; in this case,
236df8bae1dSRodney W. Grimes 			 * current and max prot are dictated by the open file.
237df8bae1dSRodney W. Grimes 			 * XXX use the vnode instead?  Problem is: what
2380d94caffSDavid Greenman 			 * credentials do we use for determination? What if
2390d94caffSDavid Greenman 			 * proc does a setuid?
240df8bae1dSRodney W. Grimes 			 */
241df8bae1dSRodney W. Grimes 			maxprot = VM_PROT_EXECUTE;	/* ??? */
242df8bae1dSRodney W. Grimes 			if (fp->f_flag & FREAD)
243df8bae1dSRodney W. Grimes 				maxprot |= VM_PROT_READ;
244df8bae1dSRodney W. Grimes 			else if (prot & PROT_READ)
245df8bae1dSRodney W. Grimes 				return (EACCES);
246df8bae1dSRodney W. Grimes 			if (flags & MAP_SHARED) {
247df8bae1dSRodney W. Grimes 				if (fp->f_flag & FWRITE)
248df8bae1dSRodney W. Grimes 					maxprot |= VM_PROT_WRITE;
249df8bae1dSRodney W. Grimes 				else if (prot & PROT_WRITE)
250df8bae1dSRodney W. Grimes 					return (EACCES);
251df8bae1dSRodney W. Grimes 			} else
252df8bae1dSRodney W. Grimes 				maxprot |= VM_PROT_WRITE;
253df8bae1dSRodney W. Grimes 			handle = (caddr_t) vp;
254df8bae1dSRodney W. Grimes 		}
255df8bae1dSRodney W. Grimes 	}
256df8bae1dSRodney W. Grimes 	error = vm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
257a316d390SJohn Dyson 	    flags, handle, uap->pos);
258df8bae1dSRodney W. Grimes 	if (error == 0)
259df8bae1dSRodney W. Grimes 		*retval = (int) addr;
260df8bae1dSRodney W. Grimes 	return (error);
261df8bae1dSRodney W. Grimes }
262df8bae1dSRodney W. Grimes 
26305f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43
264d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
26505f0fdd2SPoul-Henning Kamp struct ommap_args {
26605f0fdd2SPoul-Henning Kamp 	caddr_t addr;
26705f0fdd2SPoul-Henning Kamp 	int len;
26805f0fdd2SPoul-Henning Kamp 	int prot;
26905f0fdd2SPoul-Henning Kamp 	int flags;
27005f0fdd2SPoul-Henning Kamp 	int fd;
27105f0fdd2SPoul-Henning Kamp 	long pos;
27205f0fdd2SPoul-Henning Kamp };
273d2d3e875SBruce Evans #endif
27405f0fdd2SPoul-Henning Kamp int
27505f0fdd2SPoul-Henning Kamp ommap(p, uap, retval)
27605f0fdd2SPoul-Henning Kamp 	struct proc *p;
27705f0fdd2SPoul-Henning Kamp 	register struct ommap_args *uap;
27805f0fdd2SPoul-Henning Kamp 	int *retval;
27905f0fdd2SPoul-Henning Kamp {
28005f0fdd2SPoul-Henning Kamp 	struct mmap_args nargs;
28105f0fdd2SPoul-Henning Kamp 	static const char cvtbsdprot[8] = {
28205f0fdd2SPoul-Henning Kamp 		0,
28305f0fdd2SPoul-Henning Kamp 		PROT_EXEC,
28405f0fdd2SPoul-Henning Kamp 		PROT_WRITE,
28505f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE,
28605f0fdd2SPoul-Henning Kamp 		PROT_READ,
28705f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_READ,
28805f0fdd2SPoul-Henning Kamp 		PROT_WRITE | PROT_READ,
28905f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE | PROT_READ,
29005f0fdd2SPoul-Henning Kamp 	};
2910d94caffSDavid Greenman 
29205f0fdd2SPoul-Henning Kamp #define	OMAP_ANON	0x0002
29305f0fdd2SPoul-Henning Kamp #define	OMAP_COPY	0x0020
29405f0fdd2SPoul-Henning Kamp #define	OMAP_SHARED	0x0010
29505f0fdd2SPoul-Henning Kamp #define	OMAP_FIXED	0x0100
29605f0fdd2SPoul-Henning Kamp #define	OMAP_INHERIT	0x0800
29705f0fdd2SPoul-Henning Kamp 
29805f0fdd2SPoul-Henning Kamp 	nargs.addr = uap->addr;
29905f0fdd2SPoul-Henning Kamp 	nargs.len = uap->len;
30005f0fdd2SPoul-Henning Kamp 	nargs.prot = cvtbsdprot[uap->prot & 0x7];
30105f0fdd2SPoul-Henning Kamp 	nargs.flags = 0;
30205f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_ANON)
30305f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_ANON;
30405f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_COPY)
30505f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_COPY;
30605f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_SHARED)
30705f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_SHARED;
30805f0fdd2SPoul-Henning Kamp 	else
30905f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_PRIVATE;
31005f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_FIXED)
31105f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_FIXED;
31205f0fdd2SPoul-Henning Kamp 	if (uap->flags & OMAP_INHERIT)
31305f0fdd2SPoul-Henning Kamp 		nargs.flags |= MAP_INHERIT;
31405f0fdd2SPoul-Henning Kamp 	nargs.fd = uap->fd;
31505f0fdd2SPoul-Henning Kamp 	nargs.pos = uap->pos;
31605f0fdd2SPoul-Henning Kamp 	return (mmap(p, &nargs, retval));
31705f0fdd2SPoul-Henning Kamp }
31805f0fdd2SPoul-Henning Kamp #endif				/* COMPAT_43 */
31905f0fdd2SPoul-Henning Kamp 
32005f0fdd2SPoul-Henning Kamp 
321d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
322df8bae1dSRodney W. Grimes struct msync_args {
323df8bae1dSRodney W. Grimes 	caddr_t addr;
324df8bae1dSRodney W. Grimes 	int len;
325e6c6af11SDavid Greenman 	int flags;
326df8bae1dSRodney W. Grimes };
327d2d3e875SBruce Evans #endif
328df8bae1dSRodney W. Grimes int
329df8bae1dSRodney W. Grimes msync(p, uap, retval)
330df8bae1dSRodney W. Grimes 	struct proc *p;
331df8bae1dSRodney W. Grimes 	struct msync_args *uap;
332df8bae1dSRodney W. Grimes 	int *retval;
333df8bae1dSRodney W. Grimes {
334df8bae1dSRodney W. Grimes 	vm_offset_t addr;
335dabee6feSPeter Wemm 	vm_size_t size, pageoff;
336e6c6af11SDavid Greenman 	int flags;
337df8bae1dSRodney W. Grimes 	vm_map_t map;
338df8bae1dSRodney W. Grimes 	int rv;
339df8bae1dSRodney W. Grimes 
340df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
3419154ee6aSPeter Wemm 	size = uap->len;
342e6c6af11SDavid Greenman 	flags = uap->flags;
343e6c6af11SDavid Greenman 
344dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
345dabee6feSPeter Wemm 	addr -= pageoff;
346dabee6feSPeter Wemm 	size += pageoff;
347dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
3489154ee6aSPeter Wemm 	if (addr + size < addr)
349dabee6feSPeter Wemm 		return(EINVAL);
350dabee6feSPeter Wemm 
351dabee6feSPeter Wemm 	if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
3521e62bc63SDavid Greenman 		return (EINVAL);
3531e62bc63SDavid Greenman 
3549154ee6aSPeter Wemm 	map = &p->p_vmspace->vm_map;
3559154ee6aSPeter Wemm 
356df8bae1dSRodney W. Grimes 	/*
357df8bae1dSRodney W. Grimes 	 * XXX Gak!  If size is zero we are supposed to sync "all modified
3580d94caffSDavid Greenman 	 * pages with the region containing addr".  Unfortunately, we don't
3590d94caffSDavid Greenman 	 * really keep track of individual mmaps so we approximate by flushing
3600d94caffSDavid Greenman 	 * the range of the map entry containing addr. This can be incorrect
3610d94caffSDavid Greenman 	 * if the region splits or is coalesced with a neighbor.
362df8bae1dSRodney W. Grimes 	 */
363df8bae1dSRodney W. Grimes 	if (size == 0) {
364df8bae1dSRodney W. Grimes 		vm_map_entry_t entry;
365df8bae1dSRodney W. Grimes 
366df8bae1dSRodney W. Grimes 		vm_map_lock_read(map);
367df8bae1dSRodney W. Grimes 		rv = vm_map_lookup_entry(map, addr, &entry);
368df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
369fbcfcdf7SDavid Greenman 		if (rv == FALSE)
370df8bae1dSRodney W. Grimes 			return (EINVAL);
371df8bae1dSRodney W. Grimes 		addr = entry->start;
372df8bae1dSRodney W. Grimes 		size = entry->end - entry->start;
373df8bae1dSRodney W. Grimes 	}
374e6c6af11SDavid Greenman 
375df8bae1dSRodney W. Grimes 	/*
376df8bae1dSRodney W. Grimes 	 * Clean the pages and interpret the return value.
377df8bae1dSRodney W. Grimes 	 */
3786c534ad8SDavid Greenman 	rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
379e6c6af11SDavid Greenman 	    (flags & MS_INVALIDATE) != 0);
380e6c6af11SDavid Greenman 
381df8bae1dSRodney W. Grimes 	switch (rv) {
382df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
383df8bae1dSRodney W. Grimes 		break;
384df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
385df8bae1dSRodney W. Grimes 		return (EINVAL);	/* Sun returns ENOMEM? */
386df8bae1dSRodney W. Grimes 	case KERN_FAILURE:
387df8bae1dSRodney W. Grimes 		return (EIO);
388df8bae1dSRodney W. Grimes 	default:
389df8bae1dSRodney W. Grimes 		return (EINVAL);
390df8bae1dSRodney W. Grimes 	}
391e6c6af11SDavid Greenman 
392df8bae1dSRodney W. Grimes 	return (0);
393df8bae1dSRodney W. Grimes }
394df8bae1dSRodney W. Grimes 
395d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
396df8bae1dSRodney W. Grimes struct munmap_args {
397df8bae1dSRodney W. Grimes 	caddr_t addr;
3989154ee6aSPeter Wemm 	size_t len;
399df8bae1dSRodney W. Grimes };
400d2d3e875SBruce Evans #endif
401df8bae1dSRodney W. Grimes int
402df8bae1dSRodney W. Grimes munmap(p, uap, retval)
403df8bae1dSRodney W. Grimes 	register struct proc *p;
404df8bae1dSRodney W. Grimes 	register struct munmap_args *uap;
405df8bae1dSRodney W. Grimes 	int *retval;
406df8bae1dSRodney W. Grimes {
407df8bae1dSRodney W. Grimes 	vm_offset_t addr;
408dabee6feSPeter Wemm 	vm_size_t size, pageoff;
409df8bae1dSRodney W. Grimes 	vm_map_t map;
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
4129154ee6aSPeter Wemm 	size = uap->len;
413dabee6feSPeter Wemm 
414dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
415dabee6feSPeter Wemm 	addr -= pageoff;
416dabee6feSPeter Wemm 	size += pageoff;
417dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
4189154ee6aSPeter Wemm 	if (addr + size < addr)
419df8bae1dSRodney W. Grimes 		return(EINVAL);
4209154ee6aSPeter Wemm 
421df8bae1dSRodney W. Grimes 	if (size == 0)
422df8bae1dSRodney W. Grimes 		return (0);
423dabee6feSPeter Wemm 
424df8bae1dSRodney W. Grimes 	/*
4250d94caffSDavid Greenman 	 * Check for illegal addresses.  Watch out for address wrap... Note
4260d94caffSDavid Greenman 	 * that VM_*_ADDRESS are not constants due to casts (argh).
427df8bae1dSRodney W. Grimes 	 */
428bbc0ec52SDavid Greenman 	if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
429df8bae1dSRodney W. Grimes 		return (EINVAL);
43026f9a767SRodney W. Grimes #ifndef i386
431df8bae1dSRodney W. Grimes 	if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
432df8bae1dSRodney W. Grimes 		return (EINVAL);
43326f9a767SRodney W. Grimes #endif
434bbc0ec52SDavid Greenman 	if (addr + size < addr)
435df8bae1dSRodney W. Grimes 		return (EINVAL);
436df8bae1dSRodney W. Grimes 	map = &p->p_vmspace->vm_map;
437df8bae1dSRodney W. Grimes 	/*
438df8bae1dSRodney W. Grimes 	 * Make sure entire range is allocated.
439df8bae1dSRodney W. Grimes 	 */
440df8bae1dSRodney W. Grimes 	if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
441df8bae1dSRodney W. Grimes 		return (EINVAL);
442df8bae1dSRodney W. Grimes 	/* returns nothing but KERN_SUCCESS anyway */
443df8bae1dSRodney W. Grimes 	(void) vm_map_remove(map, addr, addr + size);
444df8bae1dSRodney W. Grimes 	return (0);
445df8bae1dSRodney W. Grimes }
446df8bae1dSRodney W. Grimes 
447df8bae1dSRodney W. Grimes void
44890324b07SDavid Greenman munmapfd(p, fd)
44990324b07SDavid Greenman 	struct proc *p;
450df8bae1dSRodney W. Grimes 	int fd;
451df8bae1dSRodney W. Grimes {
452df8bae1dSRodney W. Grimes 	/*
453c4ed5a07SDavid Greenman 	 * XXX should unmap any regions mapped to this file
454df8bae1dSRodney W. Grimes 	 */
45590324b07SDavid Greenman 	p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
456df8bae1dSRodney W. Grimes }
457df8bae1dSRodney W. Grimes 
458d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
459df8bae1dSRodney W. Grimes struct mprotect_args {
460df8bae1dSRodney W. Grimes 	caddr_t addr;
4619154ee6aSPeter Wemm 	size_t len;
462df8bae1dSRodney W. Grimes 	int prot;
463df8bae1dSRodney W. Grimes };
464d2d3e875SBruce Evans #endif
465df8bae1dSRodney W. Grimes int
466df8bae1dSRodney W. Grimes mprotect(p, uap, retval)
467df8bae1dSRodney W. Grimes 	struct proc *p;
468df8bae1dSRodney W. Grimes 	struct mprotect_args *uap;
469df8bae1dSRodney W. Grimes 	int *retval;
470df8bae1dSRodney W. Grimes {
471df8bae1dSRodney W. Grimes 	vm_offset_t addr;
472dabee6feSPeter Wemm 	vm_size_t size, pageoff;
473df8bae1dSRodney W. Grimes 	register vm_prot_t prot;
474df8bae1dSRodney W. Grimes 
475df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
4769154ee6aSPeter Wemm 	size = uap->len;
477df8bae1dSRodney W. Grimes 	prot = uap->prot & VM_PROT_ALL;
478df8bae1dSRodney W. Grimes 
479dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
480dabee6feSPeter Wemm 	addr -= pageoff;
481dabee6feSPeter Wemm 	size += pageoff;
482dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
4839154ee6aSPeter Wemm 	if (addr + size < addr)
484dabee6feSPeter Wemm 		return(EINVAL);
485dabee6feSPeter Wemm 
486df8bae1dSRodney W. Grimes 	switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
487df8bae1dSRodney W. Grimes 		FALSE)) {
488df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
489df8bae1dSRodney W. Grimes 		return (0);
490df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
491df8bae1dSRodney W. Grimes 		return (EACCES);
492df8bae1dSRodney W. Grimes 	}
493df8bae1dSRodney W. Grimes 	return (EINVAL);
494df8bae1dSRodney W. Grimes }
495df8bae1dSRodney W. Grimes 
496d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
497dabee6feSPeter Wemm struct minherit_args {
498dabee6feSPeter Wemm 	caddr_t addr;
4999154ee6aSPeter Wemm 	size_t len;
500dabee6feSPeter Wemm 	int inherit;
501dabee6feSPeter Wemm };
502dabee6feSPeter Wemm #endif
503dabee6feSPeter Wemm int
504dabee6feSPeter Wemm minherit(p, uap, retval)
505dabee6feSPeter Wemm 	struct proc *p;
506dabee6feSPeter Wemm 	struct minherit_args *uap;
507dabee6feSPeter Wemm 	int *retval;
508dabee6feSPeter Wemm {
509dabee6feSPeter Wemm 	vm_offset_t addr;
510dabee6feSPeter Wemm 	vm_size_t size, pageoff;
511dabee6feSPeter Wemm 	register vm_inherit_t inherit;
512dabee6feSPeter Wemm 
513dabee6feSPeter Wemm 	addr = (vm_offset_t)uap->addr;
5149154ee6aSPeter Wemm 	size = uap->len;
515dabee6feSPeter Wemm 	inherit = uap->inherit;
516dabee6feSPeter Wemm 
517dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
518dabee6feSPeter Wemm 	addr -= pageoff;
519dabee6feSPeter Wemm 	size += pageoff;
520dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
5219154ee6aSPeter Wemm 	if (addr + size < addr)
522dabee6feSPeter Wemm 		return(EINVAL);
523dabee6feSPeter Wemm 
524dabee6feSPeter Wemm 	switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size,
525dabee6feSPeter Wemm 	    inherit)) {
526dabee6feSPeter Wemm 	case KERN_SUCCESS:
527dabee6feSPeter Wemm 		return (0);
528dabee6feSPeter Wemm 	case KERN_PROTECTION_FAILURE:
529dabee6feSPeter Wemm 		return (EACCES);
530dabee6feSPeter Wemm 	}
531dabee6feSPeter Wemm 	return (EINVAL);
532dabee6feSPeter Wemm }
533dabee6feSPeter Wemm 
534dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_
535df8bae1dSRodney W. Grimes struct madvise_args {
536df8bae1dSRodney W. Grimes 	caddr_t addr;
5379154ee6aSPeter Wemm 	size_t len;
538df8bae1dSRodney W. Grimes 	int behav;
539df8bae1dSRodney W. Grimes };
540d2d3e875SBruce Evans #endif
5410d94caffSDavid Greenman 
542df8bae1dSRodney W. Grimes /* ARGSUSED */
543df8bae1dSRodney W. Grimes int
544df8bae1dSRodney W. Grimes madvise(p, uap, retval)
545df8bae1dSRodney W. Grimes 	struct proc *p;
546df8bae1dSRodney W. Grimes 	struct madvise_args *uap;
547df8bae1dSRodney W. Grimes 	int *retval;
548df8bae1dSRodney W. Grimes {
549867a482dSJohn Dyson 	vm_map_t map;
550867a482dSJohn Dyson 	pmap_t pmap;
551f35329acSJohn Dyson 	vm_offset_t start, end;
552867a482dSJohn Dyson 	/*
553867a482dSJohn Dyson 	 * Check for illegal addresses.  Watch out for address wrap... Note
554867a482dSJohn Dyson 	 * that VM_*_ADDRESS are not constants due to casts (argh).
555867a482dSJohn Dyson 	 */
556867a482dSJohn Dyson 	if (VM_MAXUSER_ADDRESS > 0 &&
557867a482dSJohn Dyson 		((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS)
558867a482dSJohn Dyson 		return (EINVAL);
559867a482dSJohn Dyson #ifndef i386
560867a482dSJohn Dyson 	if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS)
561867a482dSJohn Dyson 		return (EINVAL);
562867a482dSJohn Dyson #endif
563867a482dSJohn Dyson 	if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
564867a482dSJohn Dyson 		return (EINVAL);
565867a482dSJohn Dyson 
566867a482dSJohn Dyson 	/*
567867a482dSJohn Dyson 	 * Since this routine is only advisory, we default to conservative
568867a482dSJohn Dyson 	 * behavior.
569867a482dSJohn Dyson 	 */
570cd6eea25SDavid Greenman 	start = trunc_page((vm_offset_t) uap->addr);
571cd6eea25SDavid Greenman 	end = round_page((vm_offset_t) uap->addr + uap->len);
572867a482dSJohn Dyson 
573867a482dSJohn Dyson 	map = &p->p_vmspace->vm_map;
574867a482dSJohn Dyson 	pmap = &p->p_vmspace->vm_pmap;
575867a482dSJohn Dyson 
576867a482dSJohn Dyson 	vm_map_madvise(map, pmap, start, end, uap->behav);
577df8bae1dSRodney W. Grimes 
578867a482dSJohn Dyson 	return (0);
579df8bae1dSRodney W. Grimes }
580df8bae1dSRodney W. Grimes 
581d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
582df8bae1dSRodney W. Grimes struct mincore_args {
583df8bae1dSRodney W. Grimes 	caddr_t addr;
5849154ee6aSPeter Wemm 	size_t len;
585df8bae1dSRodney W. Grimes 	char *vec;
586df8bae1dSRodney W. Grimes };
587d2d3e875SBruce Evans #endif
5880d94caffSDavid Greenman 
589df8bae1dSRodney W. Grimes /* ARGSUSED */
590df8bae1dSRodney W. Grimes int
591df8bae1dSRodney W. Grimes mincore(p, uap, retval)
592df8bae1dSRodney W. Grimes 	struct proc *p;
593df8bae1dSRodney W. Grimes 	struct mincore_args *uap;
594df8bae1dSRodney W. Grimes 	int *retval;
595df8bae1dSRodney W. Grimes {
596867a482dSJohn Dyson 	vm_offset_t addr, first_addr;
597867a482dSJohn Dyson 	vm_offset_t end, cend;
598867a482dSJohn Dyson 	pmap_t pmap;
599867a482dSJohn Dyson 	vm_map_t map;
60002c04a2fSJohn Dyson 	char *vec;
601867a482dSJohn Dyson 	int error;
602867a482dSJohn Dyson 	int vecindex, lastvecindex;
603867a482dSJohn Dyson 	register vm_map_entry_t current;
604867a482dSJohn Dyson 	vm_map_entry_t entry;
605867a482dSJohn Dyson 	int mincoreinfo;
606df8bae1dSRodney W. Grimes 
607867a482dSJohn Dyson 	/*
608867a482dSJohn Dyson 	 * Make sure that the addresses presented are valid for user
609867a482dSJohn Dyson 	 * mode.
610867a482dSJohn Dyson 	 */
611867a482dSJohn Dyson 	first_addr = addr = trunc_page((vm_offset_t) uap->addr);
6129154ee6aSPeter Wemm 	end = addr + (vm_size_t)round_page(uap->len);
61302c04a2fSJohn Dyson 	if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS)
61402c04a2fSJohn Dyson 		return (EINVAL);
61502c04a2fSJohn Dyson 	if (end < addr)
61602c04a2fSJohn Dyson 		return (EINVAL);
61702c04a2fSJohn Dyson 
618867a482dSJohn Dyson 	/*
619867a482dSJohn Dyson 	 * Address of byte vector
620867a482dSJohn Dyson 	 */
62102c04a2fSJohn Dyson 	vec = uap->vec;
622867a482dSJohn Dyson 
623867a482dSJohn Dyson 	map = &p->p_vmspace->vm_map;
624867a482dSJohn Dyson 	pmap = &p->p_vmspace->vm_pmap;
625867a482dSJohn Dyson 
626867a482dSJohn Dyson 	vm_map_lock(map);
627867a482dSJohn Dyson 
628867a482dSJohn Dyson 	/*
629867a482dSJohn Dyson 	 * Not needed here
630867a482dSJohn Dyson 	 */
631867a482dSJohn Dyson #if 0
632867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, addr, end);
633867a482dSJohn Dyson #endif
634867a482dSJohn Dyson 
635867a482dSJohn Dyson 	if (!vm_map_lookup_entry(map, addr, &entry))
636867a482dSJohn Dyson 		entry = entry->next;
637867a482dSJohn Dyson 
638867a482dSJohn Dyson 	/*
639867a482dSJohn Dyson 	 * Do this on a map entry basis so that if the pages are not
640867a482dSJohn Dyson 	 * in the current processes address space, we can easily look
641867a482dSJohn Dyson 	 * up the pages elsewhere.
642867a482dSJohn Dyson 	 */
643867a482dSJohn Dyson 	lastvecindex = -1;
644867a482dSJohn Dyson 	for(current = entry;
645867a482dSJohn Dyson 		(current != &map->header) && (current->start < end);
646867a482dSJohn Dyson 		current = current->next) {
647867a482dSJohn Dyson 
648867a482dSJohn Dyson 		/*
649867a482dSJohn Dyson 		 * ignore submaps (for now) or null objects
650867a482dSJohn Dyson 		 */
651867a482dSJohn Dyson 		if (current->is_a_map || current->is_sub_map ||
652867a482dSJohn Dyson 			current->object.vm_object == NULL)
653867a482dSJohn Dyson 			continue;
654867a482dSJohn Dyson 
655867a482dSJohn Dyson 		/*
656867a482dSJohn Dyson 		 * limit this scan to the current map entry and the
657867a482dSJohn Dyson 		 * limits for the mincore call
658867a482dSJohn Dyson 		 */
659867a482dSJohn Dyson 		if (addr < current->start)
660867a482dSJohn Dyson 			addr = current->start;
661867a482dSJohn Dyson 		cend = current->end;
662867a482dSJohn Dyson 		if (cend > end)
663867a482dSJohn Dyson 			cend = end;
664867a482dSJohn Dyson 
665867a482dSJohn Dyson 		/*
666867a482dSJohn Dyson 		 * scan this entry one page at a time
667867a482dSJohn Dyson 		 */
668867a482dSJohn Dyson 		while(addr < cend) {
669867a482dSJohn Dyson 			/*
670867a482dSJohn Dyson 			 * Check pmap first, it is likely faster, also
671867a482dSJohn Dyson 			 * it can provide info as to whether we are the
672867a482dSJohn Dyson 			 * one referencing or modifying the page.
673867a482dSJohn Dyson 			 */
674867a482dSJohn Dyson 			mincoreinfo = pmap_mincore(pmap, addr);
675867a482dSJohn Dyson 			if (!mincoreinfo) {
676867a482dSJohn Dyson 				vm_pindex_t pindex;
677867a482dSJohn Dyson 				vm_ooffset_t offset;
678867a482dSJohn Dyson 				vm_page_t m;
679867a482dSJohn Dyson 				/*
680867a482dSJohn Dyson 				 * calculate the page index into the object
681867a482dSJohn Dyson 				 */
682867a482dSJohn Dyson 				offset = current->offset + (addr - current->start);
683867a482dSJohn Dyson 				pindex = OFF_TO_IDX(offset);
684867a482dSJohn Dyson 				m = vm_page_lookup(current->object.vm_object,
685867a482dSJohn Dyson 					pindex);
686867a482dSJohn Dyson 				/*
687867a482dSJohn Dyson 				 * if the page is resident, then gather information about
688867a482dSJohn Dyson 				 * it.
689867a482dSJohn Dyson 				 */
690867a482dSJohn Dyson 				if (m) {
691867a482dSJohn Dyson 					mincoreinfo = MINCORE_INCORE;
692867a482dSJohn Dyson 					if (m->dirty ||
69367bf6868SJohn Dyson 						pmap_is_modified(VM_PAGE_TO_PHYS(m)))
694867a482dSJohn Dyson 						mincoreinfo |= MINCORE_MODIFIED_OTHER;
695867a482dSJohn Dyson 					if ((m->flags & PG_REFERENCED) ||
69667bf6868SJohn Dyson 						pmap_is_referenced(VM_PAGE_TO_PHYS(m)))
697867a482dSJohn Dyson 						mincoreinfo |= MINCORE_REFERENCED_OTHER;
69802c04a2fSJohn Dyson 				}
699867a482dSJohn Dyson 			}
700867a482dSJohn Dyson 
701867a482dSJohn Dyson 			/*
702867a482dSJohn Dyson 			 * calculate index into user supplied byte vector
703867a482dSJohn Dyson 			 */
704867a482dSJohn Dyson 			vecindex = OFF_TO_IDX(addr - first_addr);
705867a482dSJohn Dyson 
706867a482dSJohn Dyson 			/*
707867a482dSJohn Dyson 			 * If we have skipped map entries, we need to make sure that
708867a482dSJohn Dyson 			 * the byte vector is zeroed for those skipped entries.
709867a482dSJohn Dyson 			 */
710867a482dSJohn Dyson 			while((lastvecindex + 1) < vecindex) {
711867a482dSJohn Dyson 				error = subyte( vec + lastvecindex, 0);
712867a482dSJohn Dyson 				if (error) {
713867a482dSJohn Dyson 					vm_map_unlock(map);
714867a482dSJohn Dyson 					return (EFAULT);
715867a482dSJohn Dyson 				}
716867a482dSJohn Dyson 				++lastvecindex;
717867a482dSJohn Dyson 			}
718867a482dSJohn Dyson 
719867a482dSJohn Dyson 			/*
720867a482dSJohn Dyson 			 * Pass the page information to the user
721867a482dSJohn Dyson 			 */
722867a482dSJohn Dyson 			error = subyte( vec + vecindex, mincoreinfo);
723867a482dSJohn Dyson 			if (error) {
724867a482dSJohn Dyson 				vm_map_unlock(map);
725867a482dSJohn Dyson 				return (EFAULT);
726867a482dSJohn Dyson 			}
727867a482dSJohn Dyson 			lastvecindex = vecindex;
72802c04a2fSJohn Dyson 			addr += PAGE_SIZE;
72902c04a2fSJohn Dyson 		}
730867a482dSJohn Dyson 	}
731867a482dSJohn Dyson 
732867a482dSJohn Dyson 	/*
733867a482dSJohn Dyson 	 * Zero the last entries in the byte vector.
734867a482dSJohn Dyson 	 */
735867a482dSJohn Dyson 	vecindex = OFF_TO_IDX(end - first_addr);
736867a482dSJohn Dyson 	while((lastvecindex + 1) < vecindex) {
737867a482dSJohn Dyson 		error = subyte( vec + lastvecindex, 0);
738867a482dSJohn Dyson 		if (error) {
739867a482dSJohn Dyson 			vm_map_unlock(map);
740867a482dSJohn Dyson 			return (EFAULT);
741867a482dSJohn Dyson 		}
742867a482dSJohn Dyson 		++lastvecindex;
743867a482dSJohn Dyson 	}
744867a482dSJohn Dyson 
745867a482dSJohn Dyson 	vm_map_unlock(map);
74602c04a2fSJohn Dyson 	return (0);
747df8bae1dSRodney W. Grimes }
748df8bae1dSRodney W. Grimes 
749d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
750df8bae1dSRodney W. Grimes struct mlock_args {
751df8bae1dSRodney W. Grimes 	caddr_t addr;
752df8bae1dSRodney W. Grimes 	size_t len;
753df8bae1dSRodney W. Grimes };
754d2d3e875SBruce Evans #endif
755df8bae1dSRodney W. Grimes int
756df8bae1dSRodney W. Grimes mlock(p, uap, retval)
757df8bae1dSRodney W. Grimes 	struct proc *p;
758df8bae1dSRodney W. Grimes 	struct mlock_args *uap;
759df8bae1dSRodney W. Grimes 	int *retval;
760df8bae1dSRodney W. Grimes {
761df8bae1dSRodney W. Grimes 	vm_offset_t addr;
762dabee6feSPeter Wemm 	vm_size_t size, pageoff;
763df8bae1dSRodney W. Grimes 	int error;
764df8bae1dSRodney W. Grimes 
765df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
7669154ee6aSPeter Wemm 	size = uap->len;
7679154ee6aSPeter Wemm 
768dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
769dabee6feSPeter Wemm 	addr -= pageoff;
770dabee6feSPeter Wemm 	size += pageoff;
771dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
772dabee6feSPeter Wemm 
773dabee6feSPeter Wemm 	/* disable wrap around */
7749154ee6aSPeter Wemm 	if (addr + size < addr)
775df8bae1dSRodney W. Grimes 		return (EINVAL);
776dabee6feSPeter Wemm 
777df8bae1dSRodney W. Grimes 	if (atop(size) + cnt.v_wire_count > vm_page_max_wired)
778df8bae1dSRodney W. Grimes 		return (EAGAIN);
7799154ee6aSPeter Wemm 
780df8bae1dSRodney W. Grimes #ifdef pmap_wired_count
781df8bae1dSRodney W. Grimes 	if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
782df8bae1dSRodney W. Grimes 	    p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
783df8bae1dSRodney W. Grimes 		return (EAGAIN);
784df8bae1dSRodney W. Grimes #else
78505f0fdd2SPoul-Henning Kamp 	error = suser(p->p_ucred, &p->p_acflag);
78605f0fdd2SPoul-Henning Kamp 	if (error)
787df8bae1dSRodney W. Grimes 		return (error);
788df8bae1dSRodney W. Grimes #endif
789df8bae1dSRodney W. Grimes 
7907aaaa4fdSJohn Dyson 	error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
791df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
792df8bae1dSRodney W. Grimes }
793df8bae1dSRodney W. Grimes 
794d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
795df8bae1dSRodney W. Grimes struct munlock_args {
796df8bae1dSRodney W. Grimes 	caddr_t addr;
797df8bae1dSRodney W. Grimes 	size_t len;
798df8bae1dSRodney W. Grimes };
799d2d3e875SBruce Evans #endif
800df8bae1dSRodney W. Grimes int
801df8bae1dSRodney W. Grimes munlock(p, uap, retval)
802df8bae1dSRodney W. Grimes 	struct proc *p;
803df8bae1dSRodney W. Grimes 	struct munlock_args *uap;
804df8bae1dSRodney W. Grimes 	int *retval;
805df8bae1dSRodney W. Grimes {
806df8bae1dSRodney W. Grimes 	vm_offset_t addr;
807dabee6feSPeter Wemm 	vm_size_t size, pageoff;
808df8bae1dSRodney W. Grimes 	int error;
809df8bae1dSRodney W. Grimes 
810df8bae1dSRodney W. Grimes 	addr = (vm_offset_t) uap->addr;
8119154ee6aSPeter Wemm 	size = uap->len;
8129154ee6aSPeter Wemm 
813dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
814dabee6feSPeter Wemm 	addr -= pageoff;
815dabee6feSPeter Wemm 	size += pageoff;
816dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
817dabee6feSPeter Wemm 
818dabee6feSPeter Wemm 	/* disable wrap around */
8199154ee6aSPeter Wemm 	if (addr + size < addr)
820df8bae1dSRodney W. Grimes 		return (EINVAL);
821dabee6feSPeter Wemm 
822df8bae1dSRodney W. Grimes #ifndef pmap_wired_count
82305f0fdd2SPoul-Henning Kamp 	error = suser(p->p_ucred, &p->p_acflag);
82405f0fdd2SPoul-Henning Kamp 	if (error)
825df8bae1dSRodney W. Grimes 		return (error);
826df8bae1dSRodney W. Grimes #endif
827df8bae1dSRodney W. Grimes 
8287aaaa4fdSJohn Dyson 	error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
829df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
830df8bae1dSRodney W. Grimes }
831df8bae1dSRodney W. Grimes 
832df8bae1dSRodney W. Grimes /*
833df8bae1dSRodney W. Grimes  * Internal version of mmap.
834df8bae1dSRodney W. Grimes  * Currently used by mmap, exec, and sys5 shared memory.
835df8bae1dSRodney W. Grimes  * Handle is either a vnode pointer or NULL for MAP_ANON.
836df8bae1dSRodney W. Grimes  */
837df8bae1dSRodney W. Grimes int
838df8bae1dSRodney W. Grimes vm_mmap(map, addr, size, prot, maxprot, flags, handle, foff)
839df8bae1dSRodney W. Grimes 	register vm_map_t map;
840df8bae1dSRodney W. Grimes 	register vm_offset_t *addr;
841df8bae1dSRodney W. Grimes 	register vm_size_t size;
842df8bae1dSRodney W. Grimes 	vm_prot_t prot, maxprot;
843df8bae1dSRodney W. Grimes 	register int flags;
844df8bae1dSRodney W. Grimes 	caddr_t handle;		/* XXX should be vp */
845a316d390SJohn Dyson 	vm_ooffset_t foff;
846df8bae1dSRodney W. Grimes {
847df8bae1dSRodney W. Grimes 	boolean_t fitit;
848fcae040bSJohn Dyson 	vm_object_t object;
849df8bae1dSRodney W. Grimes 	struct vnode *vp = NULL;
85024a1cce3SDavid Greenman 	objtype_t type;
851df8bae1dSRodney W. Grimes 	int rv = KERN_SUCCESS;
852bd7e5f99SJohn Dyson 	vm_ooffset_t objsize;
853bd7e5f99SJohn Dyson 	int docow;
85406cb7259SDavid Greenman 	struct proc *p = curproc;
855df8bae1dSRodney W. Grimes 
856df8bae1dSRodney W. Grimes 	if (size == 0)
857df8bae1dSRodney W. Grimes 		return (0);
858df8bae1dSRodney W. Grimes 
85906cb7259SDavid Greenman 	objsize = size = round_page(size);
860df8bae1dSRodney W. Grimes 
861df8bae1dSRodney W. Grimes 	/*
862bc9ad247SDavid Greenman 	 * We currently can only deal with page aligned file offsets.
863bc9ad247SDavid Greenman 	 * The check is here rather than in the syscall because the
864bc9ad247SDavid Greenman 	 * kernel calls this function internally for other mmaping
865bc9ad247SDavid Greenman 	 * operations (such as in exec) and non-aligned offsets will
866bc9ad247SDavid Greenman 	 * cause pmap inconsistencies...so we want to be sure to
867bc9ad247SDavid Greenman 	 * disallow this in all cases.
868bc9ad247SDavid Greenman 	 */
869bc9ad247SDavid Greenman 	if (foff & PAGE_MASK)
870bc9ad247SDavid Greenman 		return (EINVAL);
871bc9ad247SDavid Greenman 
87206cb7259SDavid Greenman 	if ((flags & MAP_FIXED) == 0) {
87306cb7259SDavid Greenman 		fitit = TRUE;
87406cb7259SDavid Greenman 		*addr = round_page(*addr);
87506cb7259SDavid Greenman 	} else {
87606cb7259SDavid Greenman 		if (*addr != trunc_page(*addr))
87706cb7259SDavid Greenman 			return (EINVAL);
87806cb7259SDavid Greenman 		fitit = FALSE;
87906cb7259SDavid Greenman 		(void) vm_map_remove(map, *addr, *addr + size);
88006cb7259SDavid Greenman 	}
88106cb7259SDavid Greenman 
882bc9ad247SDavid Greenman 	/*
88324a1cce3SDavid Greenman 	 * Lookup/allocate object.
884df8bae1dSRodney W. Grimes 	 */
8855f55e841SDavid Greenman 	if (flags & MAP_ANON) {
886851c12ffSJohn Dyson 		type = OBJT_DEFAULT;
8875f55e841SDavid Greenman 		/*
8885f55e841SDavid Greenman 		 * Unnamed anonymous regions always start at 0.
8895f55e841SDavid Greenman 		 */
89067bf6868SJohn Dyson 		if (handle == 0)
8915f55e841SDavid Greenman 			foff = 0;
8925f55e841SDavid Greenman 	} else {
893df8bae1dSRodney W. Grimes 		vp = (struct vnode *) handle;
894df8bae1dSRodney W. Grimes 		if (vp->v_type == VCHR) {
89524a1cce3SDavid Greenman 			type = OBJT_DEVICE;
896df8bae1dSRodney W. Grimes 			handle = (caddr_t) vp->v_rdev;
89706cb7259SDavid Greenman 		} else {
89806cb7259SDavid Greenman 			struct vattr vat;
89906cb7259SDavid Greenman 			int error;
90006cb7259SDavid Greenman 
90106cb7259SDavid Greenman 			error = VOP_GETATTR(vp, &vat, p->p_ucred, p);
90206cb7259SDavid Greenman 			if (error)
90306cb7259SDavid Greenman 				return (error);
904bd7e5f99SJohn Dyson 			objsize = round_page(vat.va_size);
90524a1cce3SDavid Greenman 			type = OBJT_VNODE;
906df8bae1dSRodney W. Grimes 		}
90706cb7259SDavid Greenman 	}
90894328e90SJohn Dyson 
90994328e90SJohn Dyson 	if (handle == NULL) {
91094328e90SJohn Dyson 		object = NULL;
91194328e90SJohn Dyson 	} else {
91267bf6868SJohn Dyson 		object = vm_pager_allocate(type, handle, OFF_TO_IDX(objsize), prot, foff);
91324a1cce3SDavid Greenman 		if (object == NULL)
91424a1cce3SDavid Greenman 			return (type == OBJT_DEVICE ? EINVAL : ENOMEM);
91594328e90SJohn Dyson 	}
916df8bae1dSRodney W. Grimes 
9175850152dSJohn Dyson 	/*
9188f2ec877SDavid Greenman 	 * Force device mappings to be shared.
9195850152dSJohn Dyson 	 */
9208f2ec877SDavid Greenman 	if (type == OBJT_DEVICE) {
9218f2ec877SDavid Greenman 		flags &= ~(MAP_PRIVATE|MAP_COPY);
9225850152dSJohn Dyson 		flags |= MAP_SHARED;
9238f2ec877SDavid Greenman 	}
9245850152dSJohn Dyson 
925bd7e5f99SJohn Dyson 	docow = 0;
9265850152dSJohn Dyson 	if ((flags & (MAP_ANON|MAP_SHARED)) == 0) {
927fcae040bSJohn Dyson 		docow = MAP_COPY_ON_WRITE | MAP_COPY_NEEDED;
928bd7e5f99SJohn Dyson 	}
9295850152dSJohn Dyson 
930bd7e5f99SJohn Dyson 	rv = vm_map_find(map, object, foff, addr, size, fitit,
931bd7e5f99SJohn Dyson 			prot, maxprot, docow);
932bd7e5f99SJohn Dyson 
93367bf6868SJohn Dyson 
934df8bae1dSRodney W. Grimes 	if (rv != KERN_SUCCESS) {
9357fb0c17eSDavid Greenman 		/*
93624a1cce3SDavid Greenman 		 * Lose the object reference. Will destroy the
93724a1cce3SDavid Greenman 		 * object if it's an unnamed anonymous mapping
93824a1cce3SDavid Greenman 		 * or named anonymous without other references.
9397fb0c17eSDavid Greenman 		 */
940df8bae1dSRodney W. Grimes 		vm_object_deallocate(object);
941df8bae1dSRodney W. Grimes 		goto out;
942df8bae1dSRodney W. Grimes 	}
943e17bed12SJohn Dyson 
944df8bae1dSRodney W. Grimes 	/*
9457fb0c17eSDavid Greenman 	 * "Pre-fault" resident pages.
9467fb0c17eSDavid Greenman 	 */
94794328e90SJohn Dyson 	if ((type == OBJT_VNODE) && (map->pmap != NULL) && (object != NULL)) {
948a316d390SJohn Dyson 		pmap_object_init_pt(map->pmap, *addr,
949867a482dSJohn Dyson 			object, (vm_pindex_t) OFF_TO_IDX(foff), size, 1);
950df8bae1dSRodney W. Grimes 	}
9517fb0c17eSDavid Greenman 
952df8bae1dSRodney W. Grimes 	/*
953df8bae1dSRodney W. Grimes 	 * Shared memory is also shared with children.
954df8bae1dSRodney W. Grimes 	 */
9555850152dSJohn Dyson 	if (flags & (MAP_SHARED|MAP_INHERIT)) {
956df8bae1dSRodney W. Grimes 		rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
957df8bae1dSRodney W. Grimes 		if (rv != KERN_SUCCESS) {
9587fb0c17eSDavid Greenman 			(void) vm_map_remove(map, *addr, *addr + size);
959df8bae1dSRodney W. Grimes 			goto out;
960df8bae1dSRodney W. Grimes 		}
961df8bae1dSRodney W. Grimes 	}
962df8bae1dSRodney W. Grimes out:
963df8bae1dSRodney W. Grimes 	switch (rv) {
964df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
965df8bae1dSRodney W. Grimes 		return (0);
966df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
967df8bae1dSRodney W. Grimes 	case KERN_NO_SPACE:
968df8bae1dSRodney W. Grimes 		return (ENOMEM);
969df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
970df8bae1dSRodney W. Grimes 		return (EACCES);
971df8bae1dSRodney W. Grimes 	default:
972df8bae1dSRodney W. Grimes 		return (EINVAL);
973df8bae1dSRodney W. Grimes 	}
974df8bae1dSRodney W. Grimes }
975